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