[Interest] String best practice

Konstantin Tokarev annulen at yandex.ru
Tue Mar 14 10:50:57 CET 2017



14.03.2017, 12:44, "Harald Vistnes" <harald.vistnes at gmail.com>:
> Hi,
>
> I'm currently working on reading and parsing large ASCII based text files and I am wondering what is the current best practice. There are so many classes and macros available, so it can be a bit confusing to know what to use when.
>
> QString, QLatin1String, QByteArray, QStringLiteral, QLatin1Literal, QByteArrayLiteral, plain C++ string literal, QStringRef, QStringBuilder and so on. And then std::string and raw const char* strings.
>
> In my case I want to read a large ASCII file line by line, so I don't need unicode. I need to compare a string with a literal, extract substrings and convert some strings to numbers.
>
> Should I just use QString all the way, or is it faster to use some other classes when you know you don't need unicode?

You should use QByteArray here, which is what QIODevice::readLine() returns. Avoid using QString as long as possible because that will trigger conversion of your text to UTF16 encoding, which may be totally useless in your use case.

>
> Any hints on fast file parsing code in Qt itself that I could use as a guildeline?

If you don't limit yourself with Qt, take a look at re2c. It's a very convenient generator of lexers which can generate much more efficient code than what you will get by reading file line by line and splitting lines in ad hoc way.

>
> Thanks,
> Harald
> ,
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest


-- 
Regards,
Konstantin



More information about the Interest mailing list