1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

Merge pull request #4575

Vincas Dargis (1):
      docs(coding): fix string usage guidelines
This commit is contained in:
Diadlo 2017-08-18 23:26:55 +03:00
commit 12a49b5fa4
No known key found for this signature in database
GPG Key ID: 5AF9F2E29107C727

View File

@ -489,23 +489,23 @@ temporary QString and thus avoids malloc:
handleToxSave(firstParam.toUtf8()); handleToxSave(firstParam.toUtf8());
``` ```
* Use `QLatin1String` when appending to string, joining two strings. * Use `QStringBuilder` and `QLatin1String` when joining strings (and chars)
together.
QLatin1String is literal type and knows string length at compile time `QLatin1String` is literal type and knows string length at compile time
(compared to `QString(const char*)` run-time cost with plain C++ (compared to `QString(const char*)` run-time cost with plain C++
string literal). Also, copying 8-bit latin string requires less memory string literal). Also, copying 8-bit latin string requires less memory
bandwith compared to 16-bit `QStringLiteral` mentioned earlier, and bandwith compared to 16-bit `QStringLiteral` mentioned earlier, and
copying here is unavoidable (and thus `QStringLiteral` loses it's purpose): 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 Include `<QStringBuilder>` and use `%` operator for optimized single-pass
concatination with help of expression template's lazy evaluation: concatenation with help of expression template's lazy evaluation:
```
if (!dir.rename(logFileDir % QLatin1String("qtox.log"),
logFileDir % QLatin1String("qtox.log.1")))
qCritical() << "Unable to move logs";
```
``` ```
QCommandLineParser parser; QCommandLineParser parser;
parser.setApplicationDescription(QLatin1String("qTox, version: ") parser.setApplicationDescription(QLatin1String("qTox, version: ")
@ -513,7 +513,8 @@ concatination with help of expression template's lazy evaluation:
% QLatin1String(__TIME__) % QLatin1Char(' ') % QLatin1String(__DATE__)); % QLatin1String(__TIME__) % QLatin1Char(' ') % QLatin1String(__DATE__));
``` ```
* Use `QLatin1Char` to avoid UTF-16-char handling (same as in previous example): * Use `QLatin1Char` to avoid UTF-16-char handling (same as in previous
example):
``` ```
QString path = QString(__FILE__); QString path = QString(__FILE__);
path = path.left(path.lastIndexOf(QLatin1Char('/')) + 1); path = path.left(path.lastIndexOf(QLatin1Char('/')) + 1);