Qt 6.x
The Qt SDK
•All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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/qnamespace.h>
8#include <QtCore/qstringfwd.h>
9#include <QtCore/qcontainerfwd.h>
10#if 0
11#pragma qt_class(QStringAlgorithms)
12#endif
13
14#include <algorithm> // std::find
15#include <string> // std::char_traits
16
18
19namespace QtPrivate {
20
21[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype qustrlen(const char16_t *str) noexcept;
22[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype qustrnlen(const char16_t *str, qsizetype maxlen) noexcept;
23[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION const char16_t *qustrchr(QStringView str, char16_t ch) noexcept;
24
34
35[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QStringView lhs, QStringView rhs) noexcept;
36[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QStringView lhs, QLatin1StringView rhs) noexcept;
37[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QStringView lhs, QBasicUtf8StringView<false> rhs) noexcept;
38[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QLatin1StringView lhs, QStringView rhs) noexcept;
39[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QLatin1StringView lhs, QLatin1StringView rhs) noexcept;
40[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QLatin1StringView lhs, QBasicUtf8StringView<false> rhs) noexcept;
41[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QBasicUtf8StringView<false> lhs, QStringView rhs) noexcept;
42[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QBasicUtf8StringView<false> lhs, QLatin1StringView rhs) noexcept;
43[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QBasicUtf8StringView<false> lhs, QBasicUtf8StringView<false> rhs) noexcept;
44
45[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool startsWith(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
46[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool startsWith(QStringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
47[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool startsWith(QLatin1StringView haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
48[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool startsWith(QLatin1StringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
49
50[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool endsWith(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
51[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool endsWith(QStringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
52[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool endsWith(QLatin1StringView haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
53[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool endsWith(QLatin1StringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
54
55[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype findString(QStringView haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
56[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype findString(QStringView haystack, qsizetype from, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
57[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype findString(QLatin1StringView haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
59
60[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype lastIndexOf(QStringView haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
61[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype lastIndexOf(QStringView haystack, qsizetype from, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
62[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype lastIndexOf(QLatin1StringView haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
64
65[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION QStringView trimmed(QStringView s) noexcept;
67
68[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QStringView haystack, QChar needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
69[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
73[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QLatin1StringView haystack, QChar needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
74
75#if QT_CONFIG(regularexpression)
76// ### Qt 7: unify these overloads;
77// remove the ones taking only a QStringView, export the others, adjust callers
78[[nodiscard]] qsizetype indexOf(QStringView viewHaystack,
79 const QString *stringHaystack,
80 const QRegularExpression &re,
81 qsizetype from = 0,
82 QRegularExpressionMatch *rmatch = nullptr);
83[[nodiscard]] Q_CORE_EXPORT qsizetype indexOf(QStringView haystack,
84 const QRegularExpression &re,
85 qsizetype from = 0,
86 QRegularExpressionMatch *rmatch = nullptr);
87[[nodiscard]] qsizetype lastIndexOf(QStringView viewHaystack,
88 const QString *stringHaystack,
89 const QRegularExpression &re,
90 qsizetype from = -1,
91 QRegularExpressionMatch *rmatch = nullptr);
92[[nodiscard]] Q_CORE_EXPORT qsizetype lastIndexOf(QStringView haystack,
93 const QRegularExpression &re,
94 qsizetype from = -1,
95 QRegularExpressionMatch *rmatch = nullptr);
96[[nodiscard]] bool contains(QStringView viewHaystack,
97 const QString *stringHaystack,
98 const QRegularExpression &re,
99 QRegularExpressionMatch *rmatch = nullptr);
100[[nodiscard]] Q_CORE_EXPORT bool contains(QStringView haystack,
101 const QRegularExpression &re,
102 QRegularExpressionMatch *rmatch = nullptr);
103[[nodiscard]] Q_CORE_EXPORT qsizetype count(QStringView haystack, const QRegularExpression &re);
104#endif
105
106[[nodiscard]] Q_CORE_EXPORT QString convertToQString(QAnyStringView s);
107
108[[nodiscard]] Q_CORE_EXPORT QByteArray convertToLatin1(QStringView str);
109[[nodiscard]] Q_CORE_EXPORT QByteArray convertToUtf8(QStringView str);
110[[nodiscard]] Q_CORE_EXPORT QByteArray convertToLocal8Bit(QStringView str);
111[[nodiscard]] Q_CORE_EXPORT QList<uint> convertToUcs4(QStringView str); // ### Qt 7 char32_t
112
113[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isRightToLeft(QStringView string) noexcept;
114
115[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isAscii(QLatin1StringView s) noexcept;
116[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isAscii(QStringView s) noexcept;
117[[nodiscard]] constexpr inline bool isLatin1(QLatin1StringView s) noexcept;
118[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isLatin1(QStringView s) noexcept;
119[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isValidUtf16(QStringView s) noexcept;
120
121template <typename Char, size_t N> [[nodiscard]] constexpr Q_ALWAYS_INLINE
122std::enable_if_t<sizeof(Char) == sizeof(char16_t), qsizetype>
124{
125 // The following values were empirically determined to detect the threshold
126 // at which the compiler gives up pre-calculating the std::find() below and
127 // instead inserts code to be executed at runtime.
128 constexpr size_t RuntimeThreshold =
129#if defined(Q_CC_CLANG) // tested through Clang 16.0.0
130 100
131#elif defined(Q_CC_GNU) // tested through GCC 13.1 at -O3 compilation level
132 __cplusplus >= 202002L ? 39 : 17
133#else
134 0
135#endif
136 ;
137 if constexpr (N == 1) {
138 return str[0] == Char(0) ? 0 : 1;
139 } else if constexpr (N > RuntimeThreshold) {
140#ifdef QT_SUPPORTS_IS_CONSTANT_EVALUATED
141 if (!qIsConstantEvaluated())
142 return QtPrivate::qustrnlen(reinterpret_cast<const char16_t *>(str), N);
143#endif
144 }
145
146 // libstdc++'s std::find_if yields a higher threshold than
147 // std::char_traits::find
148
149#if __cplusplus >= 202002 && defined(__cpp_lib_constexpr_algorithms)
150 const auto it = std::find(str, str + N, Char(0));
151 return it - str;
152#else
153 const auto it = std::char_traits<Char>::find(str, N, Char(0));
154 return it ? std::distance(str, it) : ptrdiff_t(N);
155#endif
156}
157
158template <typename Char, size_t N> [[nodiscard]] constexpr inline
159std::enable_if_t<sizeof(Char) == 1, qsizetype> lengthHelperContainer(const Char (&str)[N])
160{
161 // std::char_traits::find will call memchr or __builtin_memchr for us
162 const auto it = std::char_traits<Char>::find(str, N, Char(0));
163 return it ? std::distance(str, it) : ptrdiff_t(N);
164}
165
166template <typename Container>
167constexpr qsizetype lengthHelperContainer(const Container &c) noexcept
168{
169 return qsizetype(std::size(c));
170}
171} // namespace QtPrivate
172
174
175#endif // QSTRINGALGORTIHMS_H
\inmodule QtCore
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore
Definition qchar.h:48
Definition qlist.h:74
\inmodule QtCore \reentrant
\inmodule QtCore \reentrant
\inmodule QtCore
Definition qstringview.h:76
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
QString str
[2]
QSet< QString >::iterator it
Combined button and popup list for selecting options.
\macro QT_NAMESPACE
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 qustrlen(const char16_t *str) noexcept
Definition qstring.cpp:686
Q_CORE_EXPORT QList< uint > convertToUcs4(QStringView str)
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isValidUtf16(QStringView s) noexcept
Definition qstring.cpp:915
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 Q_DECL_PURE_FUNCTION qsizetype lastIndexOf(QByteArrayView haystack, qsizetype from, 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:1405
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype findString(QStringView haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs=Qt::CaseSensitive) noexcept
Definition qstring.cpp:9631
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:701
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
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION const char16_t * qustrchr(QStringView str, char16_t ch) noexcept
Definition qstring.cpp:714
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isAscii(QLatin1StringView s) noexcept
Definition qstring.cpp:860
Q_CORE_EXPORT QByteArray convertToUtf8(QStringView str)
constexpr bool isLatin1(QLatin1StringView s) noexcept
Definition qstring.h:64
CaseSensitivity
@ CaseSensitive
#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
const GLubyte * c
GLdouble s
[6]
Definition qopenglext.h:235
char Char
ptrdiff_t qsizetype
Definition qtypes.h:70