Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qstringalgorithms.h
Go to the documentation of this file.
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
21
22namespace 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[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION const char16_t *qustrcasechr(QStringView str, char16_t ch) noexcept;
28
31[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QStringView lhs, QBasicUtf8StringView<false> rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
34[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QLatin1StringView lhs, QBasicUtf8StringView<false> rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
35[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QBasicUtf8StringView<false> lhs, QStringView rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
36[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QBasicUtf8StringView<false> lhs, QLatin1StringView rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
37[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QBasicUtf8StringView<false> lhs, QBasicUtf8StringView<false> rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
38
39[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QStringView lhs, QStringView rhs) noexcept;
40[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QStringView lhs, QLatin1StringView rhs) noexcept;
41[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QStringView lhs, QBasicUtf8StringView<false> rhs) noexcept;
42[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QLatin1StringView lhs, QStringView rhs) noexcept;
43[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QLatin1StringView lhs, QLatin1StringView rhs) noexcept;
44[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QLatin1StringView lhs, QBasicUtf8StringView<false> rhs) noexcept;
45[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QBasicUtf8StringView<false> lhs, QStringView rhs) noexcept;
46[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QBasicUtf8StringView<false> lhs, QLatin1StringView rhs) noexcept;
47[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QBasicUtf8StringView<false> lhs, QBasicUtf8StringView<false> rhs) noexcept;
48
49[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool startsWith(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
50[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool startsWith(QStringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
51[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool startsWith(QLatin1StringView haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
52[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool startsWith(QLatin1StringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
53
54[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool endsWith(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
55[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool endsWith(QStringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
56[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool endsWith(QLatin1StringView haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
57[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool endsWith(QLatin1StringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
58
59[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype findStringInsensitive(QStringView haystack, qsizetype from, char16_t needle) noexcept;
60[[nodiscard]] inline qsizetype findString(QStringView str, qsizetype from, QChar needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
61[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype findString(QStringView haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
62[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype findString(QStringView haystack, qsizetype from, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
63[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype findString(QLatin1StringView haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
65
66[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype lastIndexOf(QStringView haystack, qsizetype from, char16_t needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
67[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype lastIndexOf(QStringView haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
68[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype lastIndexOf(QStringView haystack, qsizetype from, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
69[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype lastIndexOf(QLatin1StringView haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
71
72[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION QStringView trimmed(QStringView s) noexcept;
74
75[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isLower(QStringView s) noexcept;
76[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isUpper(QStringView s) noexcept;
77
78[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QStringView haystack, QChar needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
79[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
83[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QLatin1StringView haystack, QChar needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
84
85#if QT_CONFIG(regularexpression)
86// ### Qt 7: unify these overloads;
87// remove the ones taking only a QStringView, export the others, adjust callers
88[[nodiscard]] qsizetype indexOf(QStringView viewHaystack,
89 const QString *stringHaystack,
90 const QRegularExpression &re,
91 qsizetype from = 0,
92 QRegularExpressionMatch *rmatch = nullptr);
93[[nodiscard]] Q_CORE_EXPORT qsizetype indexOf(QStringView haystack,
94 const QRegularExpression &re,
95 qsizetype from = 0,
96 QRegularExpressionMatch *rmatch = nullptr);
97[[nodiscard]] qsizetype lastIndexOf(QStringView viewHaystack,
98 const QString *stringHaystack,
99 const QRegularExpression &re,
100 qsizetype from = -1,
101 QRegularExpressionMatch *rmatch = nullptr);
102[[nodiscard]] Q_CORE_EXPORT qsizetype lastIndexOf(QStringView haystack,
103 const QRegularExpression &re,
104 qsizetype from = -1,
105 QRegularExpressionMatch *rmatch = nullptr);
106[[nodiscard]] bool contains(QStringView viewHaystack,
107 const QString *stringHaystack,
108 const QRegularExpression &re,
109 QRegularExpressionMatch *rmatch = nullptr);
110[[nodiscard]] Q_CORE_EXPORT bool contains(QStringView haystack,
111 const QRegularExpression &re,
112 QRegularExpressionMatch *rmatch = nullptr);
113[[nodiscard]] Q_CORE_EXPORT qsizetype count(QStringView haystack, const QRegularExpression &re);
114#endif
115
116[[nodiscard]] Q_CORE_EXPORT QString convertToQString(QAnyStringView s);
117
118[[nodiscard]] Q_CORE_EXPORT QByteArray convertToLatin1(QStringView str);
119[[nodiscard]] Q_CORE_EXPORT QByteArray convertToUtf8(QStringView str);
120[[nodiscard]] Q_CORE_EXPORT QByteArray convertToLocal8Bit(QStringView str);
121[[nodiscard]] Q_CORE_EXPORT QList<uint> convertToUcs4(QStringView str); // ### Qt 7 char32_t
122
123[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isRightToLeft(QStringView string) noexcept;
124
125[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isAscii(QLatin1StringView s) noexcept;
126[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isAscii(QStringView s) noexcept;
127[[nodiscard]] constexpr inline bool isLatin1(QLatin1StringView s) noexcept;
128[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isLatin1(QStringView s) noexcept;
129[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isValidUtf16(QStringView s) noexcept;
130
131template <typename Char, size_t N> [[nodiscard]] constexpr Q_ALWAYS_INLINE
133{
134#if defined(__cpp_lib_constexpr_algorithms) && defined(Q_CC_GNU_ONLY)
135 // libstdc++'s std::find / std::find_if manages to execute more steps
136 // than the loop below
137 const auto it = std::find(str, str + N, Char(0));
138 return it - str;
139#else
140 // std::char_traits<C> is deprecated for C not one of the standard char
141 // types, so we have to roll out our own loop.
142 for (size_t i = 0; i < N; ++i) {
143 if (str[i] == Char(0))
144 return qsizetype(i);
145 }
146 return qsizetype(N);
147#endif
148}
149
150template <typename Char, size_t N> [[nodiscard]] constexpr Q_ALWAYS_INLINE
151std::enable_if_t<sizeof(Char) == sizeof(char16_t), qsizetype>
153{
154 // The following values were empirically determined to detect the threshold
155 // at which the compiler gives up pre-calculating the std::find() below and
156 // instead inserts code to be executed at runtime.
157 constexpr size_t RuntimeThreshold =
158#if defined(Q_CC_CLANG)
159 // tested on Clang 15, 16 & 17
160 1023
161#elif defined(Q_CC_GNU)
162 // tested through GCC 13.1 at -O3 compilation level
163 // note: at -O2, GCC always generates a loop!
164 __cplusplus >= 202002L ? 39 : 17
165#else
166 0
167#endif
168 ;
169 if constexpr (N == 1) {
170 return str[0] == Char(0) ? 0 : 1;
171 } else if constexpr (N > RuntimeThreshold) {
172#ifdef QT_SUPPORTS_IS_CONSTANT_EVALUATED
174 return QtPrivate::qustrnlen(reinterpret_cast<const char16_t *>(str), N);
175#endif
176 }
177
179}
180
181inline qsizetype qstrnlen_helper(const char *str, size_t maxlen)
182{
183#if !defined(Q_COMPILER_SLOW_QSTRNLEN_COMPILATION)
184 return qstrnlen(str, maxlen);
185#else
186 return strnlen_s(str, maxlen);
187#endif
188}
189
190template <typename Char, size_t N> [[nodiscard]] constexpr inline
191std::enable_if_t<sizeof(Char) == 1, qsizetype> lengthHelperContainer(const Char (&str)[N])
192{
193#ifdef QT_SUPPORTS_IS_CONSTANT_EVALUATED
195 return qstrnlen_helper(reinterpret_cast<const char *>(str), N);
196#endif
197
199}
200
201template <typename Container>
202constexpr qsizetype lengthHelperContainer(const Container &c) noexcept
203{
204 return qsizetype(std::size(c));
205}
206} // namespace QtPrivate
207
209
210#endif // QSTRINGALGORTIHMS_H
\inmodule QtCore
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore
\inmodule QtCore \reentrant
\inmodule QtCore \reentrant
\inmodule QtCore
Definition qstringview.h:78
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QString str
[2]
QSet< QString >::iterator it
Combined button and popup list for selecting options.
\macro QT_NO_KEYWORDS >
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype lastIndexOf(QByteArrayView haystack, qsizetype from, char needle) noexcept
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool endsWith(QByteArrayView haystack, QByteArrayView needle) noexcept
constexpr Q_ALWAYS_INLINE std::enable_if_t< sizeof(Char)==sizeof(char16_t), qsizetype > lengthHelperContainer(const Char(&str)[N])
Q_CORE_EXPORT QString convertToQString(QAnyStringView s)
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype findStringInsensitive(QStringView haystack, qsizetype from, char16_t needle) noexcept
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isLower(QStringView s) noexcept
Definition qstring.cpp:5557
Q_CORE_EXPORT QList< uint > convertToUcs4(QStringView str)
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isValidUtf16(QStringView s) noexcept
Definition qstring.cpp:905
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION QByteArrayView trimmed(QByteArrayView s) noexcept
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool startsWith(QByteArrayView haystack, QByteArrayView needle) noexcept
Q_CORE_EXPORT QByteArray convertToLocal8Bit(QStringView str)
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QStringView lhs, QStringView rhs) noexcept
Definition qstring.cpp:1393
qsizetype findString(QStringView str, qsizetype from, QChar needle, Qt::CaseSensitivity cs=Qt::CaseSensitive) noexcept
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isRightToLeft(QStringView string) noexcept
Q_CORE_EXPORT QByteArray convertToLatin1(QStringView str)
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype qustrnlen(const char16_t *str, qsizetype maxlen) noexcept
Definition qstring.cpp:673
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QStringView lhs, QStringView rhs, Qt::CaseSensitivity cs=Qt::CaseSensitive) noexcept
qsizetype indexOf(const QList< V > &list, const U &u, qsizetype from) noexcept
qsizetype qstrnlen_helper(const char *str, size_t maxlen)
constexpr Q_ALWAYS_INLINE qsizetype lengthHelperContainerLoop(const Char(&str)[N])
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isAscii(QLatin1StringView s) noexcept
Definition qstring.cpp:850
Q_CORE_EXPORT QByteArray convertToUtf8(QStringView str)
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype qustrlen(const char16_t *str) noexcept
Definition qstring.cpp:658
constexpr bool isLatin1(QLatin1StringView s) noexcept
Definition qstring.h:66
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION const char16_t * qustrcasechr(QStringView str, char16_t ch) noexcept
Definition qstring.cpp:775
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isUpper(QStringView s) noexcept
Definition qstring.cpp:5562
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION const char16_t * qustrchr(QStringView str, char16_t ch) noexcept
Definition qstring.cpp:687
CaseSensitivity
@ CaseSensitive
constexpr bool is_constant_evaluated() noexcept
size_t qstrnlen(const char *str, size_t maxlen)
#define Q_DECL_PURE_FUNCTION
#define Q_ALWAYS_INLINE
static bool contains(const QJsonArray &haystack, unsigned needle)
Definition qopengl.cpp:116
GLenum GLenum GLsizei count
GLdouble s
[6]
Definition qopenglext.h:235
const GLubyte * c
char Char
ptrdiff_t qsizetype
Definition qtypes.h:165