Line data Source code
1 : // SPDX-FileCopyrightText: 2022-2026 Paul Colby <git@colby.id.au>
2 : // SPDX-License-Identifier: LGPL-3.0-or-later
3 :
4 : /*!
5 : * \file
6 : * Declares the DOKIT_USE_STRINGLITERALS macro, and related functions.
7 : *
8 : * This is only required to support Qt versions earlier than 6.4, since Qt added string literal operators in that
9 : * versions. This header can, and will, be removed entirely when Dokit no longer supports Qt versions earlier than 6.4.
10 : */
11 :
12 : #ifndef DOKIT_STRINGLITERALS_P_H
13 : #define DOKIT_STRINGLITERALS_P_H
14 :
15 : /// \cond internal
16 :
17 : /*!
18 : * \def DOKIT_STRING_INDEX_TYPE
19 : *
20 : * Internal macro for matching the index type used by QString methods. Specifically, from Qt 6.0.0 onwards, QString
21 : * (and related classes, and methods) use qsizetype for index positions, while Qt5 used int instead.
22 : *
23 : * \todo Remove this macro when Dokit no longer supports Qt5.
24 : */
25 :
26 : /*!
27 : * \def DOKIT_USE_STRINGLITERALS
28 : *
29 : * Internal macro for using either official Qt string literals (added in Qt 6.4), or our own equivalent ones for older
30 : * Qt versions.
31 : *
32 : * \todo Remove this macro when Dokit no longer supports Qt versions earlier than 6.4.
33 : */
34 :
35 : #include <QtGlobal>
36 :
37 : // Although qsizetype was added in Qt 5.10.0, it wasn't used for QString indexes until 6.0.
38 : #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
39 242499 : #define DOKIT_STRING_INDEX_TYPE int
40 : #else
41 64911 : #define DOKIT_STRING_INDEX_TYPE qsizetype
42 : #endif
43 :
44 : #if (QT_VERSION < QT_VERSION_CHECK(6, 4, 0)) // String literals added in Qt 6.4.0.
45 :
46 : namespace _dokit {
47 : inline namespace Literals {
48 : inline namespace StringLiterals {
49 :
50 : constexpr inline QLatin1Char operator""_L1(char ch) noexcept
51 20287 : {
52 20287 : return QLatin1Char(ch);
53 20287 : }
54 :
55 : constexpr inline QLatin1String/*View*/ operator""_L1(const char *str, size_t size) noexcept
56 0 : {
57 0 : //return {str, qsizetype(size)};
58 0 : return QLatin1String(str, DOKIT_STRING_INDEX_TYPE(size));
59 0 : }
60 :
61 : inline QString operator""_s(const char16_t *str, size_t size) noexcept
62 305773 : {
63 : //return QString(QStringPrivate(nullptr, const_cast<char16_t *>(str), qsizetype(size)));
64 350729 : return QString::fromUtf16(str, DOKIT_STRING_INDEX_TYPE(size));
65 305773 : }
66 :
67 : } } } // _dokit::Literals::StringLiterals
68 :
69 : #define DOKIT_USE_STRINGLITERALS using namespace _dokit::Literals::StringLiterals;
70 : #else
71 : #define DOKIT_USE_STRINGLITERALS using namespace Qt::Literals::StringLiterals;
72 : #endif
73 :
74 : /// \endcond
75 :
76 : #endif // DOKIT_STRINGLITERALS_P_H
|