File indexing completed on 2026-05-17 08:20:22
0001
0002
0003
0004
0005 #ifndef QSTRINGALGORITHMS_H
0006 #define QSTRINGALGORITHMS_H
0007
0008 #include <QtCore/qbytearrayalgorithms.h>
0009 #include <QtCore/qcontainerfwd.h>
0010 #include <QtCore/qnamespace.h>
0011 #include <QtCore/qstringfwd.h>
0012 #if 0
0013 #pragma qt_class(QStringAlgorithms)
0014 #endif
0015
0016 #include <algorithm> // std::find
0017 #include <iterator> // std::size
0018
0019 #include <QtCore/q20type_traits.h> // q20::is_constant_evaluated
0020
0021 QT_BEGIN_NAMESPACE
0022
0023 namespace QtPrivate {
0024
0025 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype qustrlen(const char16_t *str) noexcept;
0026 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype qustrnlen(const char16_t *str, qsizetype maxlen) noexcept;
0027 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION const char16_t *qustrchr(QStringView str, char16_t ch) noexcept;
0028 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION const char16_t *qustrcasechr(QStringView str, char16_t ch) noexcept;
0029
0030 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QStringView lhs, QStringView rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0031 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QStringView lhs, QLatin1StringView rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0032 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QStringView lhs, QBasicUtf8StringView<false> rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0033 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QLatin1StringView lhs, QStringView rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0034 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QLatin1StringView lhs, QLatin1StringView rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0035 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QLatin1StringView lhs, QBasicUtf8StringView<false> rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0036 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QBasicUtf8StringView<false> lhs, QStringView rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0037 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QBasicUtf8StringView<false> lhs, QLatin1StringView rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0038 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QBasicUtf8StringView<false> lhs, QBasicUtf8StringView<false> rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0039
0040 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QStringView lhs, QStringView rhs) noexcept;
0041 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QStringView lhs, QLatin1StringView rhs) noexcept;
0042 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QStringView lhs, QBasicUtf8StringView<false> rhs) noexcept;
0043 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QLatin1StringView lhs, QStringView rhs) noexcept;
0044 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QLatin1StringView lhs, QLatin1StringView rhs) noexcept;
0045 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QLatin1StringView lhs, QBasicUtf8StringView<false> rhs) noexcept;
0046 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QBasicUtf8StringView<false> lhs, QStringView rhs) noexcept;
0047 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QBasicUtf8StringView<false> lhs, QLatin1StringView rhs) noexcept;
0048 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QBasicUtf8StringView<false> lhs, QBasicUtf8StringView<false> rhs) noexcept;
0049
0050 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool startsWith(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0051 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool startsWith(QStringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0052 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool startsWith(QLatin1StringView haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0053 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool startsWith(QLatin1StringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0054
0055 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool endsWith(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0056 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool endsWith(QStringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0057 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool endsWith(QLatin1StringView haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0058 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool endsWith(QLatin1StringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0059
0060 [[nodiscard]] inline qsizetype findString(QStringView str, qsizetype from, QChar needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0061 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype findString(QStringView haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0062 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype findString(QStringView haystack, qsizetype from, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0063 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype findString(QLatin1StringView haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0064 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype findString(QLatin1StringView haystack, qsizetype from, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0065
0066 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype lastIndexOf(QStringView haystack, qsizetype from, char16_t needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0067 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype lastIndexOf(QStringView haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0068 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype lastIndexOf(QStringView haystack, qsizetype from, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0069 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype lastIndexOf(QLatin1StringView haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0070 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype lastIndexOf(QLatin1StringView haystack, qsizetype from, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0071
0072 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION QStringView trimmed(QStringView s) noexcept;
0073 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION QLatin1StringView trimmed(QLatin1StringView s) noexcept;
0074
0075 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isLower(QStringView s) noexcept;
0076 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isUpper(QStringView s) noexcept;
0077
0078 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QStringView haystack, QChar needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0079 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0080 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QStringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive);
0081 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QLatin1StringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive);
0082 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QLatin1StringView haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive);
0083 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QLatin1StringView haystack, QChar needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0084
0085 #if QT_CONFIG(regularexpression)
0086
0087
0088 [[nodiscard]] qsizetype indexOf(QStringView viewHaystack,
0089 const QString *stringHaystack,
0090 const QRegularExpression &re,
0091 qsizetype from = 0,
0092 QRegularExpressionMatch *rmatch = nullptr);
0093 [[nodiscard]] Q_CORE_EXPORT qsizetype indexOf(QStringView haystack,
0094 const QRegularExpression &re,
0095 qsizetype from = 0,
0096 QRegularExpressionMatch *rmatch = nullptr);
0097 [[nodiscard]] qsizetype lastIndexOf(QStringView viewHaystack,
0098 const QString *stringHaystack,
0099 const QRegularExpression &re,
0100 qsizetype from = -1,
0101 QRegularExpressionMatch *rmatch = nullptr);
0102 [[nodiscard]] Q_CORE_EXPORT qsizetype lastIndexOf(QStringView haystack,
0103 const QRegularExpression &re,
0104 qsizetype from = -1,
0105 QRegularExpressionMatch *rmatch = nullptr);
0106 [[nodiscard]] bool contains(QStringView viewHaystack,
0107 const QString *stringHaystack,
0108 const QRegularExpression &re,
0109 QRegularExpressionMatch *rmatch = nullptr);
0110 [[nodiscard]] Q_CORE_EXPORT bool contains(QStringView haystack,
0111 const QRegularExpression &re,
0112 QRegularExpressionMatch *rmatch = nullptr);
0113 [[nodiscard]] Q_CORE_EXPORT qsizetype count(QStringView haystack, const QRegularExpression &re);
0114 #endif
0115
0116 [[nodiscard]] Q_CORE_EXPORT QString convertToQString(QAnyStringView s);
0117
0118 [[nodiscard]] Q_CORE_EXPORT QByteArray convertToLatin1(QStringView str);
0119 [[nodiscard]] Q_CORE_EXPORT QByteArray convertToUtf8(QStringView str);
0120 [[nodiscard]] Q_CORE_EXPORT QByteArray convertToLocal8Bit(QStringView str);
0121 [[nodiscard]] Q_CORE_EXPORT QList<uint> convertToUcs4(QStringView str);
0122
0123 [[nodiscard]] Q_CORE_EXPORT QByteArray convertToUtf8(QLatin1StringView str);
0124
0125 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isRightToLeft(QStringView string) noexcept;
0126
0127 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isAscii(QLatin1StringView s) noexcept;
0128 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isAscii(QStringView s) noexcept;
0129 [[nodiscard]] constexpr inline bool isLatin1(QLatin1StringView s) noexcept;
0130 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isLatin1(QStringView s) noexcept;
0131 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isValidUtf16(QStringView s) noexcept;
0132
0133 template <typename Char, size_t N> [[nodiscard]] Q_ALWAYS_INLINE constexpr
0134 qsizetype lengthHelperContainerLoop(const Char (&str)[N])
0135 {
0136 #if defined(__cpp_lib_constexpr_algorithms) && defined(Q_CC_GNU_ONLY)
0137
0138
0139 const auto it = std::find(str, str + N, Char(0));
0140 return it - str;
0141 #else
0142
0143
0144 for (size_t i = 0; i < N; ++i) {
0145 if (str[i] == Char(0))
0146 return qsizetype(i);
0147 }
0148 return qsizetype(N);
0149 #endif
0150 }
0151
0152 template <typename Char, size_t N> [[nodiscard]] Q_ALWAYS_INLINE constexpr
0153 std::enable_if_t<sizeof(Char) == sizeof(char16_t), qsizetype>
0154 lengthHelperContainer(const Char (&str)[N])
0155 {
0156
0157
0158
0159 constexpr size_t RuntimeThreshold =
0160 #if defined(Q_CC_CLANG)
0161
0162 1023
0163 #elif defined(Q_CC_GNU)
0164
0165
0166 __cplusplus >= 202002L ? 39 : 17
0167 #else
0168 0
0169 #endif
0170 ;
0171 if constexpr (N == 1) {
0172 return str[0] == Char(0) ? 0 : 1;
0173 } else if constexpr (N > RuntimeThreshold) {
0174 #ifdef QT_SUPPORTS_IS_CONSTANT_EVALUATED
0175 if (!q20::is_constant_evaluated())
0176 return QtPrivate::qustrnlen(reinterpret_cast<const char16_t *>(str), N);
0177 #endif
0178 }
0179
0180 return lengthHelperContainerLoop(str);
0181 }
0182
0183 inline qsizetype qstrnlen_helper(const char *str, size_t maxlen)
0184 {
0185 #if !defined(Q_COMPILER_SLOW_QSTRNLEN_COMPILATION)
0186 return qstrnlen(str, maxlen);
0187 #else
0188 return strnlen_s(str, maxlen);
0189 #endif
0190 }
0191
0192 template <typename Char, size_t N> [[nodiscard]] constexpr inline
0193 std::enable_if_t<sizeof(Char) == 1, qsizetype> lengthHelperContainer(const Char (&str)[N])
0194 {
0195 #ifdef QT_SUPPORTS_IS_CONSTANT_EVALUATED
0196 if (!q20::is_constant_evaluated())
0197 return qstrnlen_helper(reinterpret_cast<const char *>(str), N);
0198 #endif
0199
0200 return lengthHelperContainerLoop(str);
0201 }
0202
0203 template <typename Container>
0204 constexpr qsizetype lengthHelperContainer(const Container &c) noexcept
0205 {
0206 return qsizetype(std::size(c));
0207 }
0208 }
0209
0210 QT_END_NAMESPACE
0211
0212 #endif