Add guides about using QStringLiteral, QLatin1String, QLatin1Char and QStringBuilder for more efficient strings.
Qt uses uses these techniques internally and can be considered as good coding practice when working with Qt libraries.
@ -467,8 +467,76 @@ someWidget->setTooltip(QStringLiteral("<html><!-- some HTML text -->") + tr("Tra
@@ -467,8 +467,76 @@ someWidget->setTooltip(QStringLiteral("<html><!-- some HTML text -->") + tr("Tra
+ QStringLiteral("</html>"));
```
## Strings
* Use `QStringLiteral` macro when creating new string.
In this example, string is not intended to be modified or copied (like
appending) into other string:
```
QApplication a(argc, argv);
a.setApplicationName(QStringLiteral("qTox"));
```
* Use `QLatin1String` when specialized overload exists.
Overload such as `QString::operator==(QLatin1String)` helps to avoid creating
temporary QString and thus avoids malloc:
```
if (eventType == QLatin1String("uri"))
handleToxURI(firstParam.toUtf8());
else if (eventType == QLatin1String("save"))
handleToxSave(firstParam.toUtf8());
```
* Use `QLatin1String` when appending to string, joining two strings.
QLatin1String is literal type and knows string length at compile time
(compared to `QString(const char*)` run-time cost with plain C++
string literal). Also, copying 8-bit latin string requires less memory
bandwith compared to 16-bit `QStringLiteral` mentioned earlier, and
copying here is unavoidable (and thus `QStringLiteral` loses it's purpose):
```
if (!dir.rename(logFileDir + QLatin1String("qtox.log"),
logFileDir + QLatin1String("qtox.log.1")))
qCritical() << "Unable to move logs";
```
* Use `QStringBuilder` when joining more than two strings (and chars) together.
Include `<QStringBuilder>` and use `%` operator for optimized single-pass
concatination with help of expression template's lazy evaluation: