Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/QtCore/qstringalgorithms.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // Copyright (C) 2017 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com>
0002 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
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 
0028 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QStringView   lhs, QStringView   rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0029 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QStringView   lhs, QLatin1StringView rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0030 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QStringView   lhs, QBasicUtf8StringView<false> rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0031 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QLatin1StringView lhs, QStringView   rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0032 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QLatin1StringView lhs, QLatin1StringView rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0033 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QLatin1StringView lhs, QBasicUtf8StringView<false> rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0034 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QBasicUtf8StringView<false> lhs, QStringView   rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0035 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QBasicUtf8StringView<false> lhs, QLatin1StringView rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0036 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QBasicUtf8StringView<false> lhs, QBasicUtf8StringView<false> rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0037 
0038 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QStringView   lhs, QStringView   rhs) noexcept;
0039 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QStringView   lhs, QLatin1StringView rhs) noexcept;
0040 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QStringView   lhs, QBasicUtf8StringView<false> rhs) noexcept;
0041 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QLatin1StringView lhs, QStringView   rhs) noexcept;
0042 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QLatin1StringView lhs, QLatin1StringView rhs) noexcept;
0043 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QLatin1StringView lhs, QBasicUtf8StringView<false> rhs) noexcept;
0044 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QBasicUtf8StringView<false> lhs, QStringView   rhs) noexcept;
0045 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QBasicUtf8StringView<false> lhs, QLatin1StringView rhs) noexcept;
0046 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QBasicUtf8StringView<false> lhs, QBasicUtf8StringView<false> rhs) noexcept;
0047 
0048 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool startsWith(QStringView   haystack, QStringView   needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0049 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool startsWith(QStringView   haystack, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0050 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool startsWith(QLatin1StringView haystack, QStringView   needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0051 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool startsWith(QLatin1StringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0052 
0053 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool endsWith(QStringView   haystack, QStringView   needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0054 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool endsWith(QStringView   haystack, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0055 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool endsWith(QLatin1StringView haystack, QStringView   needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0056 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool endsWith(QLatin1StringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0057 
0058 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype findString(QStringView haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0059 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype findString(QStringView haystack, qsizetype from, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0060 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype findString(QLatin1StringView haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0061 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype findString(QLatin1StringView haystack, qsizetype from, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0062 
0063 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype lastIndexOf(QStringView haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0064 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype lastIndexOf(QStringView haystack, qsizetype from, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0065 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype lastIndexOf(QLatin1StringView haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0066 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype lastIndexOf(QLatin1StringView haystack, qsizetype from, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0067 
0068 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION QStringView   trimmed(QStringView   s) noexcept;
0069 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION QLatin1StringView trimmed(QLatin1StringView s) noexcept;
0070 
0071 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isLower(QStringView s) noexcept;
0072 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isUpper(QStringView s) noexcept;
0073 
0074 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QStringView haystack, QChar needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0075 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0076 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QStringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive);
0077 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QLatin1StringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive);
0078 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QLatin1StringView haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive);
0079 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QLatin1StringView haystack, QChar needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0080 
0081 #if QT_CONFIG(regularexpression)
0082 // ### Qt 7: unify these overloads;
0083 // remove the ones taking only a QStringView, export the others, adjust callers
0084 [[nodiscard]] qsizetype indexOf(QStringView viewHaystack,
0085                                 const QString *stringHaystack,
0086                                 const QRegularExpression &re,
0087                                 qsizetype from = 0,
0088                                 QRegularExpressionMatch *rmatch = nullptr);
0089 [[nodiscard]] Q_CORE_EXPORT qsizetype indexOf(QStringView haystack,
0090                                               const QRegularExpression &re,
0091                                               qsizetype from = 0,
0092                                               QRegularExpressionMatch *rmatch = nullptr);
0093 [[nodiscard]] qsizetype lastIndexOf(QStringView viewHaystack,
0094                                     const QString *stringHaystack,
0095                                     const QRegularExpression &re,
0096                                     qsizetype from = -1,
0097                                     QRegularExpressionMatch *rmatch = nullptr);
0098 [[nodiscard]] Q_CORE_EXPORT qsizetype lastIndexOf(QStringView haystack,
0099                                                   const QRegularExpression &re,
0100                                                   qsizetype from = -1,
0101                                                   QRegularExpressionMatch *rmatch = nullptr);
0102 [[nodiscard]] bool contains(QStringView viewHaystack,
0103                             const QString *stringHaystack,
0104                             const QRegularExpression &re,
0105                             QRegularExpressionMatch *rmatch = nullptr);
0106 [[nodiscard]] Q_CORE_EXPORT bool contains(QStringView haystack,
0107                                           const QRegularExpression &re,
0108                                           QRegularExpressionMatch *rmatch = nullptr);
0109 [[nodiscard]] Q_CORE_EXPORT qsizetype count(QStringView haystack, const QRegularExpression &re);
0110 #endif
0111 
0112 [[nodiscard]] Q_CORE_EXPORT QString convertToQString(QAnyStringView s);
0113 
0114 [[nodiscard]] Q_CORE_EXPORT QByteArray convertToLatin1(QStringView str);
0115 [[nodiscard]] Q_CORE_EXPORT QByteArray convertToUtf8(QStringView str);
0116 [[nodiscard]] Q_CORE_EXPORT QByteArray convertToLocal8Bit(QStringView str);
0117 [[nodiscard]] Q_CORE_EXPORT QList<uint> convertToUcs4(QStringView str); // ### Qt 7 char32_t
0118 
0119 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isRightToLeft(QStringView string) noexcept;
0120 
0121 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isAscii(QLatin1StringView s) noexcept;
0122 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isAscii(QStringView   s) noexcept;
0123 [[nodiscard]] constexpr inline                   bool isLatin1(QLatin1StringView s) noexcept;
0124 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isLatin1(QStringView   s) noexcept;
0125 [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isValidUtf16(QStringView s) noexcept;
0126 
0127 template <typename Char, size_t N> [[nodiscard]] constexpr Q_ALWAYS_INLINE
0128 qsizetype lengthHelperContainerLoop(const Char (&str)[N])
0129 {
0130 #if defined(__cpp_lib_constexpr_algorithms) && defined(Q_CC_GNU_ONLY)
0131     // libstdc++'s std::find / std::find_if manages to execute more steps
0132     // than the loop below
0133     const auto it = std::find(str, str + N, Char(0));
0134     return it - str;
0135 #else
0136     // std::char_traits<C> is deprecated for C not one of the standard char
0137     // types, so we have to roll out our own loop.
0138     for (size_t i = 0; i < N; ++i) {
0139         if (str[i] == Char(0))
0140             return qsizetype(i);
0141     }
0142     return qsizetype(N);
0143 #endif
0144 }
0145 
0146 template <typename Char, size_t N> [[nodiscard]] constexpr Q_ALWAYS_INLINE
0147 std::enable_if_t<sizeof(Char) == sizeof(char16_t), qsizetype>
0148 lengthHelperContainer(const Char (&str)[N])
0149 {
0150     // The following values were empirically determined to detect the threshold
0151     // at which the compiler gives up pre-calculating the std::find() below and
0152     // instead inserts code to be executed at runtime.
0153     constexpr size_t RuntimeThreshold =
0154 #if defined(Q_CC_CLANG)
0155             // tested on Clang 15, 16 & 17
0156             1023
0157 #elif defined(Q_CC_GNU)
0158             // tested through GCC 13.1 at -O3 compilation level
0159             // note: at -O2, GCC always generates a loop!
0160             __cplusplus >= 202002L ? 39 : 17
0161 #else
0162             0
0163 #endif
0164             ;
0165     if constexpr (N == 1) {
0166         return str[0] == Char(0) ? 0 : 1;
0167     } else if constexpr (N > RuntimeThreshold) {
0168 #ifdef QT_SUPPORTS_IS_CONSTANT_EVALUATED
0169         if (!q20::is_constant_evaluated())
0170             return QtPrivate::qustrnlen(reinterpret_cast<const char16_t *>(str), N);
0171 #endif
0172     }
0173 
0174     return lengthHelperContainerLoop(str);
0175 }
0176 
0177 inline qsizetype qstrnlen_helper(const char *str, size_t maxlen)
0178 {
0179 #if !defined(Q_COMPILER_SLOW_QSTRNLEN_COMPILATION)
0180     return qstrnlen(str, maxlen);
0181 #else
0182     return strnlen_s(str, maxlen);
0183 #endif
0184 }
0185 
0186 template <typename Char, size_t N> [[nodiscard]] constexpr inline
0187 std::enable_if_t<sizeof(Char) == 1, qsizetype> lengthHelperContainer(const Char (&str)[N])
0188 {
0189 #ifdef QT_SUPPORTS_IS_CONSTANT_EVALUATED
0190     if (!q20::is_constant_evaluated())
0191         return qstrnlen_helper(reinterpret_cast<const char *>(str), N);
0192 #endif
0193 
0194     return lengthHelperContainerLoop(str);
0195 }
0196 
0197 template <typename Container>
0198 constexpr qsizetype lengthHelperContainer(const Container &c) noexcept
0199 {
0200     return qsizetype(std::size(c));
0201 }
0202 } // namespace QtPrivate
0203 
0204 QT_END_NAMESPACE
0205 
0206 #endif // QSTRINGALGORTIHMS_H