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

Add string literal suffixes to create QStrings, QByteArrays, and QLatin1String(View)s at compile time #4706

Merged
merged 3 commits into from
Jul 1, 2023

Conversation

Nerixyz
Copy link
Contributor

@Nerixyz Nerixyz commented Jun 26, 2023

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 the Qt::Literals::StringLiterals namespace, which provides suffixes to create the aforementioned types (and QLatin1Char). From the documentation of the QString suffix (applies to the others as well):

The QString is created at compile time, and the generated string data is stored in the read-only segment of the compiled object file. Duplicate literals may share the same read-only memory. This functionality is interchangeable with QStringLiteral, but saves typing when many string literals are present in the code.

The last point is the most important one in my opinion. To create a QString you can now do u"Chatterino"_s instead of QStringLiteral("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.

@pajlada pajlada merged commit 61566e2 into Chatterino:master Jul 1, 2023
@Nerixyz Nerixyz deleted the feat/literals branch July 1, 2023 15:34
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.

2 participants