Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use arg() to concatenate strings for better readability #723

Merged
merged 1 commit into from
Nov 10, 2024

Conversation

Integral-Tech
Copy link
Contributor

No description provided.

Copy link
Collaborator

@zjeffer zjeffer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps QStringBuilder can be used in some of these cases? https://doc.qt.io/qt-6/qstring.html#more-efficient-string-construction

@zjeffer zjeffer self-assigned this Nov 3, 2024
@Integral-Tech
Copy link
Contributor Author

Perhaps QStringBuilder can be used in some of these cases? https://doc.qt.io/qt-6/qstring.html#more-efficient-string-construction

Both QStringBuilder() and QStringLiteral().arg() are efficient I think

More efficient string construction
Many strings are known at compile time. The QString constructor from C++ string literals will copy the contents of the string, treating the contents as UTF-8. This requires memory allocation and re-encoding string data, operations that will happen at runtime. If the string data is known at compile time, you can use the QStringLiteral macro or similarly operator""_s to create QString's payload at compile time instead.

@nuttyartist
Copy link
Owner

Perhaps QStringBuilder can be used in some of these cases? https://doc.qt.io/qt-6/qstring.html#more-efficient-string-construction

These allocations can be optimized by an internal class QStringBuilder. This class is marked internal and does not appear in the documentation, because you aren't meant to instantiate it in your code. Its use will be automatic, as described below. The class is found in src/corelib/tools/qstringbuilder.cpp if you want to have a look at it.

Seems like it's not needed to instantiate it? Probably Qt optimize this automatically during compilation?

@zjeffer
Copy link
Collaborator

zjeffer commented Nov 3, 2024

Seems like it's not needed to instantiate it? Probably Qt optimize this automatically during compilation?

Indeed, I think you're supposed to use the % operator as an alternative to +, like it says in the example:

#include <QStringBuilder>

QString hello("hello");
QStringView el = QStringView{ hello }.mid(2, 3);
QLatin1StringView world("world");
QString message =  hello % el % world % QChar('!');

But I'm not sure what the difference is with using .args(). Maybe % is more readable?
I'll approve anyway.

@zjeffer zjeffer requested a review from nuttyartist November 8, 2024 22:02
@nuttyartist nuttyartist merged commit 39fc4ec into nuttyartist:master Nov 10, 2024
11 checks passed
@Integral-Tech Integral-Tech deleted the arg-refactor branch November 10, 2024 09:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants