QXmpp: StringLiterals.h Source File

QXmpp Version: 1.14.3

Loading...
Searching...
No Matches
StringLiterals.h
1// SPDX-FileCopyrightText: 2021 Linus Jahn <lnj@kaidan.im>
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4
5#ifndef STRINGLITERALS_H
6#define STRINGLITERALS_H
7
8#include <QString>
9
10#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
11using namespace Qt::Literals::StringLiterals;
12#elif QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
13inline QString operator"" _s(const char16_t *str, size_t size) noexcept
14{
15 return QString(QStringPrivate(nullptr, const_cast<char16_t *>(str), qsizetype(size)));
16}
17
18constexpr inline QLatin1String operator"" _L1(const char *str, size_t size) noexcept
19{
20 return QLatin1String { str, int(size) };
21}
22#else
23namespace QXmpp::Private {
24
25template<std::size_t N>
26struct StringLiteralData {
27 char16_t data[N];
28 std::size_t size = N;
29
30 constexpr StringLiteralData(const char16_t (&str)[N])
31 {
32 std::ranges::copy(str, data);
33 }
34};
35
36template<std::size_t N>
37struct StaticStringData {
38 QArrayData str = Q_STATIC_STRING_DATA_HEADER_INITIALIZER(N - 1);
39 char16_t data[N];
40
41 StaticStringData(const char16_t (&str)[N])
42 {
43 std::ranges::copy(str, data);
44 }
45
46 QStringData *data_ptr() const
47 {
48 return const_cast<QStringData *>(static_cast<const QStringData *>(&str));
49 }
50};
51
52} // namespace QXmpp::Private
53
54template<QXmpp::Private::StringLiteralData str>
55QString operator""_s()
56{
57 static const auto staticData = QXmpp::Private::StaticStringData<str.size>(str.data);
58 return QString(QStringDataPtr { staticData.data_ptr() });
59}
60
61constexpr inline QLatin1String operator"" _L1(const char *str, size_t size) noexcept
62{
63 return QLatin1String { str, int(size) };
64}
65#endif
66
67#endif // STRINGLITERALS_H