Add string literal suffixes to create QString
s, QByteArray
s, and QLatin1String(View)
s at compile time
#4706
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
To create a string/byte-array at compile-time before this PR, you'd need to use
Q{String, ByteArray}Literal
. These macros would embed the generated data into the executable, so no allocation at runtime would be required. Qt 6.4 introduced theQt::Literals::StringLiterals
namespace, which provides suffixes to create the aforementioned types (andQLatin1Char
). From the documentation of theQString
suffix (applies to the others as well):The last point is the most important one in my opinion. To create a
QString
you can now dou"Chatterino"_s
instead ofQStringLiteral("Chatterino")
.Since Chatterino also supports Qt 5, I ported the functionality using the implementation from desktop-app/lib_base which itself is based on the
QStringLiteral
macro from Qt 5. I added tests to ensure the implementation yields the same result.The implementation for Qt 6 is a lot simpler thanks to refactored
QString
internals.Ideally, most literals should use this suffix, since it reduces small, temporary allocations.