[Interest] String best practice

Viktor Engelmann viktor.engelmann at qt.io
Wed Mar 15 10:59:23 CET 2017



On 14.03.2017 10:50, Konstantin Tokarev wrote:
>
> 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.
If the program is small and you don't want it to ever grow beyond ASCII,
using byte arrays is okay, but in my experience, if you want to be
future-proof, you should interpret byte-arrays *as soon as possible*.

Then you have an object with a controlled format and you can use that
throughout your program, without worrying about encodings. Keeping the
data raw will increase the probability that some module does something
wrong because it assumes a wrong encoding and breaks your results (i.e.
using bytewise comparison for string comparison, which works for ASCII,
but not for unicode - even if both have the same encoding, because there
are letters that have multiple different unicode codepoints).

-- 

Viktor Engelmann
Software Engineer

The Qt Company GmbH
Rudower Chaussee 13
D-12489 Berlin

Viktor.Engelmann at qt.io
+49 151 26784521

http://qt.io
Geschäftsführer: Mika Pälsi, Juha Varelius, Mika Harjuaho
Sitz der Gesellschaft: Berlin
Registergericht: Amtsgericht Charlottenburg, HRB 144331 B





More information about the Interest mailing list