[Qt-interest] QLatin1Literal suggestion

Philippe philwave at gmail.com
Mon Mar 7 10:04:57 CET 2011


Yesterday I have discovered QLatin1Literal. This is a great little class for performance improvement (cf. QString documentation, the concept is that the const string's length if known at compile time, hence strlen can be skipped).
But it lacks a QString constructor, by design I'm sure. But then why not adding these:

QString QString::fromLatin1(const QLatin1Literal&);
QString QString::fromAscii(const QLatin1Literal&);
QString QString::fromUtf8(const QLatin1Literal&);
QString QString::fromLocal8Bit(const QLatin1Literal&);

I currently simulate this with:

inline QString Latin1(const QLatin1Literal& s) { return QString::fromLatin1(s.data(), s.size()); }
inline QString Ascii(const QLatin1Literal& s) { return QString::fromAscii(s.data(), s.size()); }
inline QString Utf8(const QLatin1Literal& s) { return QString::fromUtf8(s.data(), s.size()); }
inline QString Local8Bit(const QLatin1Literal& s) { return QString::fromLocal8Bit(s.data(), s.size()); }

eg.:

QString test = Latin1("Hello");

This seems to work without any problem so far, assembly generated code
is fine, and there is no conflitct with QT_NO_CAST_FROM_ASCII /
QT_NO_CAST_TO_ASCII / QT_NO_CAST_FROM_BYTEARRAY being defined or not

NB: QLatin1Literal 's name is a bit misleading above, because QLatin1Literal's implementation knows nothing about the latin1 character set in fact. And would better be replaced with "QLiteral"
(cf. src\corelib\tools\qstringbuilder.h)

If anyone sees a problem with the above approach...




More information about the Qt-interest-old mailing list