1 | // Copyright (C) 2017 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com> |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QSTRINGALGORITHMS_H |
5 | #define QSTRINGALGORITHMS_H |
6 | |
7 | #include <QtCore/qbytearrayalgorithms.h> |
8 | #include <QtCore/qcontainerfwd.h> |
9 | #include <QtCore/qnamespace.h> |
10 | #include <QtCore/qstringfwd.h> |
11 | #if 0 |
12 | #pragma qt_class(QStringAlgorithms) |
13 | #endif |
14 | |
15 | #include <algorithm> // std::find |
16 | #include <iterator> // std::size |
17 | |
18 | #include <QtCore/q20type_traits.h> // q20::is_constant_evaluated |
19 | |
20 | QT_BEGIN_NAMESPACE |
21 | |
22 | namespace QtPrivate { |
23 | |
24 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype qustrlen(const char16_t *str) noexcept; |
25 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype qustrnlen(const char16_t *str, qsizetype maxlen) noexcept; |
26 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION const char16_t *qustrchr(QStringView str, char16_t ch) noexcept; |
27 | |
28 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QStringView lhs, QStringView rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; |
29 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QStringView lhs, QLatin1StringView rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; |
30 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QStringView lhs, QBasicUtf8StringView<false> rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; |
31 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QLatin1StringView lhs, QStringView rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; |
32 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QLatin1StringView lhs, QLatin1StringView rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; |
33 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QLatin1StringView lhs, QBasicUtf8StringView<false> rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; |
34 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QBasicUtf8StringView<false> lhs, QStringView rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; |
35 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QBasicUtf8StringView<false> lhs, QLatin1StringView rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; |
36 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QBasicUtf8StringView<false> lhs, QBasicUtf8StringView<false> rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; |
37 | |
38 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QStringView lhs, QStringView rhs) noexcept; |
39 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QStringView lhs, QLatin1StringView rhs) noexcept; |
40 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QStringView lhs, QBasicUtf8StringView<false> rhs) noexcept; |
41 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QLatin1StringView lhs, QStringView rhs) noexcept; |
42 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QLatin1StringView lhs, QLatin1StringView rhs) noexcept; |
43 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QLatin1StringView lhs, QBasicUtf8StringView<false> rhs) noexcept; |
44 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QBasicUtf8StringView<false> lhs, QStringView rhs) noexcept; |
45 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QBasicUtf8StringView<false> lhs, QLatin1StringView rhs) noexcept; |
46 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QBasicUtf8StringView<false> lhs, QBasicUtf8StringView<false> rhs) noexcept; |
47 | |
48 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool startsWith(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; |
49 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool startsWith(QStringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; |
50 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool startsWith(QLatin1StringView haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; |
51 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool startsWith(QLatin1StringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; |
52 | |
53 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool endsWith(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; |
54 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool endsWith(QStringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; |
55 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool endsWith(QLatin1StringView haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; |
56 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool endsWith(QLatin1StringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; |
57 | |
58 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype findString(QStringView haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; |
59 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype findString(QStringView haystack, qsizetype from, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; |
60 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype findString(QLatin1StringView haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; |
61 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype findString(QLatin1StringView haystack, qsizetype from, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; |
62 | |
63 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype lastIndexOf(QStringView haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; |
64 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype lastIndexOf(QStringView haystack, qsizetype from, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; |
65 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype lastIndexOf(QLatin1StringView haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; |
66 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype lastIndexOf(QLatin1StringView haystack, qsizetype from, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; |
67 | |
68 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION QStringView trimmed(QStringView s) noexcept; |
69 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION QLatin1StringView trimmed(QLatin1StringView s) noexcept; |
70 | |
71 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isLower(QStringView s) noexcept; |
72 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isUpper(QStringView s) noexcept; |
73 | |
74 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QStringView haystack, QChar needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; |
75 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; |
76 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QStringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive); |
77 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QLatin1StringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive); |
78 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QLatin1StringView haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive); |
79 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QLatin1StringView haystack, QChar needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; |
80 | |
81 | #if QT_CONFIG(regularexpression) |
82 | // ### Qt 7: unify these overloads; |
83 | // remove the ones taking only a QStringView, export the others, adjust callers |
84 | [[nodiscard]] qsizetype indexOf(QStringView viewHaystack, |
85 | const QString *stringHaystack, |
86 | const QRegularExpression &re, |
87 | qsizetype from = 0, |
88 | QRegularExpressionMatch *rmatch = nullptr); |
89 | [[nodiscard]] Q_CORE_EXPORT qsizetype indexOf(QStringView haystack, |
90 | const QRegularExpression &re, |
91 | qsizetype from = 0, |
92 | QRegularExpressionMatch *rmatch = nullptr); |
93 | [[nodiscard]] qsizetype lastIndexOf(QStringView viewHaystack, |
94 | const QString *stringHaystack, |
95 | const QRegularExpression &re, |
96 | qsizetype from = -1, |
97 | QRegularExpressionMatch *rmatch = nullptr); |
98 | [[nodiscard]] Q_CORE_EXPORT qsizetype lastIndexOf(QStringView haystack, |
99 | const QRegularExpression &re, |
100 | qsizetype from = -1, |
101 | QRegularExpressionMatch *rmatch = nullptr); |
102 | [[nodiscard]] bool contains(QStringView viewHaystack, |
103 | const QString *stringHaystack, |
104 | const QRegularExpression &re, |
105 | QRegularExpressionMatch *rmatch = nullptr); |
106 | [[nodiscard]] Q_CORE_EXPORT bool contains(QStringView haystack, |
107 | const QRegularExpression &re, |
108 | QRegularExpressionMatch *rmatch = nullptr); |
109 | [[nodiscard]] Q_CORE_EXPORT qsizetype count(QStringView haystack, const QRegularExpression &re); |
110 | #endif |
111 | |
112 | [[nodiscard]] Q_CORE_EXPORT QString convertToQString(QAnyStringView s); |
113 | |
114 | [[nodiscard]] Q_CORE_EXPORT QByteArray convertToLatin1(QStringView str); |
115 | [[nodiscard]] Q_CORE_EXPORT QByteArray convertToUtf8(QStringView str); |
116 | [[nodiscard]] Q_CORE_EXPORT QByteArray convertToLocal8Bit(QStringView str); |
117 | [[nodiscard]] Q_CORE_EXPORT QList<uint> convertToUcs4(QStringView str); // ### Qt 7 char32_t |
118 | |
119 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isRightToLeft(QStringView string) noexcept; |
120 | |
121 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isAscii(QLatin1StringView s) noexcept; |
122 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isAscii(QStringView s) noexcept; |
123 | [[nodiscard]] constexpr inline bool isLatin1(QLatin1StringView s) noexcept; |
124 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isLatin1(QStringView s) noexcept; |
125 | [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isValidUtf16(QStringView s) noexcept; |
126 | |
127 | template <typename Char, size_t N> [[nodiscard]] constexpr Q_ALWAYS_INLINE |
128 | qsizetype lengthHelperContainerLoop(const Char (&str)[N]) |
129 | { |
130 | #if defined(__cpp_lib_constexpr_algorithms) && defined(Q_CC_GNU_ONLY) |
131 | // libstdc++'s std::find / std::find_if manages to execute more steps |
132 | // than the loop below |
133 | const auto it = std::find(str, str + N, Char(0)); |
134 | return it - str; |
135 | #else |
136 | // std::char_traits<C> is deprecated for C not one of the standard char |
137 | // types, so we have to roll out our own loop. |
138 | for (size_t i = 0; i < N; ++i) { |
139 | if (str[i] == Char(0)) |
140 | return qsizetype(i); |
141 | } |
142 | return qsizetype(N); |
143 | #endif |
144 | } |
145 | |
146 | template <typename Char, size_t N> [[nodiscard]] constexpr Q_ALWAYS_INLINE |
147 | std::enable_if_t<sizeof(Char) == sizeof(char16_t), qsizetype> |
148 | lengthHelperContainer(const Char (&str)[N]) |
149 | { |
150 | // The following values were empirically determined to detect the threshold |
151 | // at which the compiler gives up pre-calculating the std::find() below and |
152 | // instead inserts code to be executed at runtime. |
153 | constexpr size_t RuntimeThreshold = |
154 | #if defined(Q_CC_CLANG) |
155 | // tested on Clang 15, 16 & 17 |
156 | 1023 |
157 | #elif defined(Q_CC_GNU) |
158 | // tested through GCC 13.1 at -O3 compilation level |
159 | // note: at -O2, GCC always generates a loop! |
160 | __cplusplus >= 202002L ? 39 : 17 |
161 | #else |
162 | 0 |
163 | #endif |
164 | ; |
165 | if constexpr (N == 1) { |
166 | return str[0] == Char(0) ? 0 : 1; |
167 | } else if constexpr (N > RuntimeThreshold) { |
168 | #ifdef QT_SUPPORTS_IS_CONSTANT_EVALUATED |
169 | if (!q20::is_constant_evaluated()) |
170 | return QtPrivate::qustrnlen(str: reinterpret_cast<const char16_t *>(str), maxlen: N); |
171 | #endif |
172 | } |
173 | |
174 | return lengthHelperContainerLoop(str); |
175 | } |
176 | |
177 | inline qsizetype qstrnlen_helper(const char *str, size_t maxlen) |
178 | { |
179 | #if !defined(Q_COMPILER_SLOW_QSTRNLEN_COMPILATION) |
180 | return qstrnlen(str, maxlen); |
181 | #else |
182 | return strnlen_s(str, maxlen); |
183 | #endif |
184 | } |
185 | |
186 | template <typename Char, size_t N> [[nodiscard]] constexpr inline |
187 | std::enable_if_t<sizeof(Char) == 1, qsizetype> lengthHelperContainer(const Char (&str)[N]) |
188 | { |
189 | #ifdef QT_SUPPORTS_IS_CONSTANT_EVALUATED |
190 | if (!q20::is_constant_evaluated()) |
191 | return qstrnlen_helper(str: reinterpret_cast<const char *>(str), maxlen: N); |
192 | #endif |
193 | |
194 | return lengthHelperContainerLoop(str); |
195 | } |
196 | |
197 | template <typename Container> |
198 | constexpr qsizetype lengthHelperContainer(const Container &c) noexcept |
199 | { |
200 | return qsizetype(std::size(c)); |
201 | } |
202 | } // namespace QtPrivate |
203 | |
204 | QT_END_NAMESPACE |
205 | |
206 | #endif // QSTRINGALGORTIHMS_H |
207 | |