Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

QStringView Diaries: Zero-Allocation String Splitting

QStringTokenizer merged for 6.0

const QString s = ~~~;
for (QStringView line : QStringTokenizer{s, u'\n'})
    use(line);
const std::u16string s = ~~~;
for (QStringView line : QStringTokenizer{s, u'\n'})
    use(line);
const QLatin1String s = ~~~;
for (QLatin1String line : QStringTokenizer{s, u'\n'})
    use(line);
std::mutex m;
std::unique_lock lock(m); // not "std::unique_lock<std::mutex> lock(m);"
const QLatin1String s = ~~~;
for (QLatin1String line : qTokenize(s, u'\n'))
    use(line);
auto expr = QString::number(42) % " is the answer"; // decltype(expr) is QStringBuilder<~~~, ~~~>
QString s = expr; // oops, accessing the temporary return value of QString::number(), since deleted
for (QStringView line : QStringTokenizer{widget->text(), u'\n'})
    use(line);
{
   auto&& __range = QStringTokenizer{widget->text(), u'\n'};
   auto __b = __range.begin(); // I know, this is not the full truth
   auto __e = __range.end();   // it's what happens for QStringTokenizer, though!
   for ( ; __b != __e; ++__b) {
     QStringView line = *__b;
     use(line);
   }
}

About KDAB