Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-16 08:20:07

0001 // Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com>
0002 // Copyright (C) 2019 Mail.ru Group.
0003 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0004 // Qt-Security score:critical reason:data-parser
0005 #ifndef QSTRINGVIEW_H
0006 #define QSTRINGVIEW_H
0007 
0008 #include <QtCore/qchar.h>
0009 #include <QtCore/qcompare.h>
0010 #include <QtCore/qcontainerfwd.h>
0011 #include <QtCore/qbytearray.h>
0012 #include <QtCore/qstringfwd.h>
0013 #include <QtCore/qstringliteral.h>
0014 #include <QtCore/qstringalgorithms.h>
0015 
0016 #include <string>
0017 #include <string_view>
0018 #include <QtCore/q20type_traits.h>
0019 
0020 #if defined(Q_OS_DARWIN) || defined(Q_QDOC)
0021 Q_FORWARD_DECLARE_CF_TYPE(CFString);
0022 Q_FORWARD_DECLARE_OBJC_CLASS(NSString);
0023 #endif
0024 
0025 QT_BEGIN_NAMESPACE
0026 
0027 class QRegularExpression;
0028 class QRegularExpressionMatch;
0029 
0030 namespace QtPrivate {
0031 template <typename Char>
0032 struct IsCompatibleCharTypeHelper
0033     : std::integral_constant<bool,
0034                              std::is_same<Char, QChar>::value ||
0035                              std::is_same<Char, ushort>::value ||
0036                              std::is_same<Char, char16_t>::value ||
0037                              (std::is_same<Char, wchar_t>::value && sizeof(wchar_t) == sizeof(QChar))> {};
0038 template <typename Char>
0039 struct IsCompatibleCharType
0040     : IsCompatibleCharTypeHelper<q20::remove_cvref_t<Char>> {};
0041 
0042 template <typename Pointer>
0043 struct IsCompatiblePointerHelper : std::false_type {};
0044 template <typename Char>
0045 struct IsCompatiblePointerHelper<Char*>
0046     : IsCompatibleCharType<Char> {};
0047 template <typename Pointer>
0048 struct IsCompatiblePointer
0049     : IsCompatiblePointerHelper<q20::remove_cvref_t<Pointer>> {};
0050 
0051 template <typename T, typename Enable = void>
0052 struct IsContainerCompatibleWithQStringView : std::false_type {};
0053 
0054 template <typename T>
0055 struct IsContainerCompatibleWithQStringView<T, std::enable_if_t<std::conjunction_v<
0056             // lacking concepts and ranges, we accept any T whose std::data yields a suitable pointer ...
0057             IsCompatiblePointer<decltype( std::data(std::declval<const T &>()) )>,
0058             // ... and that has a suitable size ...
0059             std::is_convertible<decltype( std::size(std::declval<const T &>()) ), qsizetype>,
0060             // ... and it's a range as it defines an iterator-like API
0061             IsCompatibleCharType<typename std::iterator_traits<decltype( std::begin(std::declval<const T &>()) )>::value_type>,
0062             std::is_convertible<
0063                 decltype( std::begin(std::declval<const T &>()) != std::end(std::declval<const T &>()) ),
0064                 bool>,
0065 
0066             // These need to be treated specially due to the empty vs null distinction
0067             std::negation<std::is_same<std::decay_t<T>, QString>>,
0068 #define QSTRINGVIEW_REFUSES_QSTRINGREF 1
0069             std::negation<std::is_same<q20::remove_cvref_t<T>, QStringRef>>, // QStringRef::op QStringView()
0070 
0071             // Don't make an accidental copy constructor
0072             std::negation<std::is_same<std::decay_t<T>, QStringView>>
0073         >>> : std::true_type {};
0074 
0075 } // namespace QtPrivate
0076 
0077 class QStringView
0078 {
0079 public:
0080     typedef char16_t storage_type;
0081     typedef const QChar value_type;
0082     typedef std::ptrdiff_t difference_type;
0083     typedef qsizetype size_type;
0084     typedef value_type &reference;
0085     typedef value_type &const_reference;
0086     typedef value_type *pointer;
0087     typedef value_type *const_pointer;
0088 
0089     typedef pointer iterator;
0090     typedef const_pointer const_iterator;
0091     typedef std::reverse_iterator<iterator> reverse_iterator;
0092     typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
0093 
0094 private:
0095     template <typename Char>
0096     using if_compatible_char = typename std::enable_if<QtPrivate::IsCompatibleCharType<Char>::value, bool>::type;
0097 
0098     template <typename Pointer>
0099     using if_compatible_pointer = typename std::enable_if<QtPrivate::IsCompatiblePointer<Pointer>::value, bool>::type;
0100 
0101     template <typename T>
0102     using if_compatible_qstring_like = typename std::enable_if<std::is_same<T, QString>::value, bool>::type;
0103 
0104     template <typename T>
0105     using if_compatible_container = typename std::enable_if<QtPrivate::IsContainerCompatibleWithQStringView<T>::value, bool>::type;
0106 
0107     template <typename Char>
0108     static constexpr qsizetype lengthHelperPointer(const Char *str) noexcept
0109     {
0110         if (q20::is_constant_evaluated())
0111             return QtPrivate::lengthHelperPointer(str);
0112         return QtPrivate::qustrlen(reinterpret_cast<const char16_t *>(str));
0113     }
0114     static qsizetype lengthHelperPointer(const QChar *str) noexcept
0115     {
0116         return QtPrivate::qustrlen(reinterpret_cast<const char16_t *>(str));
0117     }
0118 
0119     template <typename Char>
0120     static const storage_type *castHelper(const Char *str) noexcept
0121     { return reinterpret_cast<const storage_type*>(str); }
0122     static constexpr const storage_type *castHelper(const storage_type *str) noexcept
0123     { return str; }
0124 
0125 public:
0126     constexpr QStringView() noexcept {}
0127     constexpr QStringView(std::nullptr_t) noexcept
0128         : QStringView() {}
0129 
0130     template <typename Char, if_compatible_char<Char> = true>
0131     constexpr QStringView(const Char *str, qsizetype len)
0132 #if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED)
0133         : m_data(castHelper(str)),
0134           m_size((Q_PRE(len >= 0), Q_PRE(str || !len), len))
0135 #else
0136         : m_size((Q_PRE(len >= 0), Q_PRE(str || !len), len)),
0137           m_data(castHelper(str))
0138 #endif
0139     {}
0140 
0141     template <typename Char, if_compatible_char<Char> = true>
0142     constexpr QStringView(const Char *f, const Char *l)
0143         : QStringView(f, l - f) {}
0144 
0145 #ifdef Q_QDOC
0146     template <typename Char, size_t N>
0147     constexpr QStringView(const Char (&array)[N]) noexcept;
0148 
0149     template <typename Char>
0150     constexpr QStringView(const Char *str) noexcept;
0151 #else
0152     template <typename Pointer, if_compatible_pointer<Pointer> = true>
0153     constexpr QStringView(const Pointer &str) noexcept
0154         : QStringView(str, str ? lengthHelperPointer(str) : 0) {}
0155 
0156     template <typename Char, if_compatible_char<Char> = true>
0157     constexpr QStringView(const Char (&str)[]) noexcept // array of unknown bounds
0158         : QStringView{&*str} {} // decay to pointer
0159 #endif
0160 
0161 #ifdef Q_QDOC
0162     QStringView(const QString &str) noexcept;
0163 #else
0164     template <typename String, if_compatible_qstring_like<String> = true>
0165     QStringView(const String &str) noexcept
0166         : QStringView{str.begin(), str.size()} {}
0167 #endif
0168 
0169     template <typename Container, if_compatible_container<Container> = true>
0170     Q_ALWAYS_INLINE constexpr QStringView(const Container &c) noexcept
0171         : QStringView(std::data(c), QtPrivate::lengthHelperContainer(c)) {}
0172 
0173     template <typename Char, size_t Size, if_compatible_char<Char> = true>
0174     [[nodiscard]] constexpr static QStringView fromArray(const Char (&string)[Size]) noexcept
0175     { return QStringView(string, Size); }
0176 
0177     [[nodiscard]] inline QString toString() const; // defined in qstring.h
0178 #if defined(Q_OS_DARWIN) || defined(Q_QDOC)
0179     // defined in qcore_foundation.mm
0180     [[nodiscard]] Q_CORE_EXPORT CFStringRef toCFString() const Q_DECL_CF_RETURNS_RETAINED;
0181     [[nodiscard]] Q_CORE_EXPORT NSString *toNSString() const Q_DECL_NS_RETURNS_AUTORELEASED;
0182 #endif
0183 
0184     [[nodiscard]] constexpr qsizetype size() const noexcept { return m_size; }
0185     [[nodiscard]] const_pointer data() const noexcept { return reinterpret_cast<const_pointer>(m_data); }
0186     [[nodiscard]] const_pointer constData() const noexcept { return data(); }
0187     [[nodiscard]] constexpr const storage_type *utf16() const noexcept { return m_data; }
0188 
0189     [[nodiscard]] constexpr QChar operator[](qsizetype n) const
0190     { verify(n, 1); return QChar(m_data[n]); }
0191 
0192     //
0193     // QString API
0194     //
0195 
0196     template <typename...Args>
0197     [[nodiscard]] inline QString arg(Args &&...args) const; // defined in qstring.h
0198 
0199     [[nodiscard]] QByteArray toLatin1() const { return QtPrivate::convertToLatin1(*this); }
0200     [[nodiscard]] QByteArray toUtf8() const { return QtPrivate::convertToUtf8(*this); }
0201     [[nodiscard]] QByteArray toLocal8Bit() const { return QtPrivate::convertToLocal8Bit(*this); }
0202     [[nodiscard]] inline QList<uint> toUcs4() const; // defined in qlist.h ### Qt 7 char32_t
0203 
0204     [[nodiscard]] constexpr QChar at(qsizetype n) const noexcept { return (*this)[n]; }
0205 
0206     [[nodiscard]] constexpr QStringView mid(qsizetype pos, qsizetype n = -1) const noexcept
0207     {
0208         using namespace QtPrivate;
0209         auto result = QContainerImplHelper::mid(size(), &pos, &n);
0210         return result == QContainerImplHelper::Null ? QStringView() : QStringView(m_data + pos, n);
0211     }
0212     [[nodiscard]] constexpr QStringView left(qsizetype n) const noexcept
0213     {
0214         if (size_t(n) >= size_t(size()))
0215             n = size();
0216         return QStringView(m_data, n);
0217     }
0218     [[nodiscard]] constexpr QStringView right(qsizetype n) const noexcept
0219     {
0220         if (size_t(n) >= size_t(size()))
0221             n = size();
0222         return QStringView(m_data + m_size - n, n);
0223     }
0224 
0225     [[nodiscard]] constexpr QStringView first(qsizetype n) const noexcept
0226     { verify(0, n); return sliced(0, n); }
0227     [[nodiscard]] constexpr QStringView last(qsizetype n) const noexcept
0228     { verify(0, n); return sliced(size() - n, n); }
0229     [[nodiscard]] constexpr QStringView sliced(qsizetype pos) const noexcept
0230     { verify(pos, 0); return QStringView(m_data + pos, size() - pos); }
0231     [[nodiscard]] constexpr QStringView sliced(qsizetype pos, qsizetype n) const noexcept
0232     { verify(pos, n); return QStringView(m_data + pos, n); }
0233     [[nodiscard]] constexpr QStringView chopped(qsizetype n) const noexcept
0234     { verify(0, n); return sliced(0, m_size - n); }
0235 
0236     constexpr void truncate(qsizetype n) noexcept
0237     { verify(0, n); ; m_size = n; }
0238     constexpr void chop(qsizetype n) noexcept
0239     { verify(0, n); m_size -= n; }
0240 
0241     [[nodiscard]] QStringView trimmed() const noexcept { return QtPrivate::trimmed(*this); }
0242 
0243     constexpr QStringView &slice(qsizetype pos)
0244     { *this = sliced(pos); return *this; }
0245     constexpr QStringView &slice(qsizetype pos, qsizetype n)
0246     { *this = sliced(pos, n); return *this; }
0247 
0248     template <typename Needle, typename...Flags>
0249     [[nodiscard]] constexpr inline auto tokenize(Needle &&needle, Flags...flags) const
0250         noexcept(noexcept(qTokenize(std::declval<const QStringView&>(), std::forward<Needle>(needle), flags...)))
0251             -> decltype(qTokenize(*this, std::forward<Needle>(needle), flags...))
0252     { return qTokenize(*this, std::forward<Needle>(needle), flags...); }
0253 
0254     [[nodiscard]] int compare(QStringView other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0255     { return QtPrivate::compareStrings(*this, other, cs); }
0256     [[nodiscard]] inline int compare(QLatin1StringView other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
0257     [[nodiscard]] inline int compare(QUtf8StringView other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
0258     [[nodiscard]] constexpr int compare(QChar c) const noexcept
0259     { return size() >= 1 ? compare_single_char_helper(*utf16() - c.unicode()) : -1; }
0260     [[nodiscard]] int compare(QChar c, Qt::CaseSensitivity cs) const noexcept
0261     { return QtPrivate::compareStrings(*this, QStringView(&c, 1), cs); }
0262 
0263     [[nodiscard]] inline int localeAwareCompare(QStringView other) const;
0264 
0265     [[nodiscard]] bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0266     { return QtPrivate::startsWith(*this, s, cs); }
0267     [[nodiscard]] inline bool startsWith(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
0268     [[nodiscard]] bool startsWith(QChar c) const noexcept
0269     { return !empty() && front() == c; }
0270     [[nodiscard]] bool startsWith(QChar c, Qt::CaseSensitivity cs) const noexcept
0271     { return QtPrivate::startsWith(*this, QStringView(&c, 1), cs); }
0272 
0273     [[nodiscard]] bool endsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0274     { return QtPrivate::endsWith(*this, s, cs); }
0275     [[nodiscard]] inline bool endsWith(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
0276     [[nodiscard]] bool endsWith(QChar c) const noexcept
0277     { return !empty() && back() == c; }
0278     [[nodiscard]] bool endsWith(QChar c, Qt::CaseSensitivity cs) const noexcept
0279     { return QtPrivate::endsWith(*this, QStringView(&c, 1), cs); }
0280 
0281     [[nodiscard]] qsizetype indexOf(QChar c, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0282     { return QtPrivate::findString(*this, from, c.unicode(), cs); }
0283     [[nodiscard]] qsizetype indexOf(QStringView s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0284     { return QtPrivate::findString(*this, from, s, cs); }
0285     [[nodiscard]] inline qsizetype indexOf(QLatin1StringView s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
0286 
0287     [[nodiscard]] bool contains(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0288     { return indexOf(QStringView(&c, 1), 0, cs) != qsizetype(-1); }
0289     [[nodiscard]] bool contains(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0290     { return indexOf(s, 0, cs) != qsizetype(-1); }
0291     [[nodiscard]] inline bool contains(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
0292 
0293     [[nodiscard]] qsizetype count(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0294     { return QtPrivate::count(*this, c, cs); }
0295     [[nodiscard]] qsizetype count(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0296     { return QtPrivate::count(*this, s, cs); }
0297     [[nodiscard]] inline qsizetype count(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
0298 
0299     [[nodiscard]] qsizetype lastIndexOf(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0300     { return lastIndexOf(c, -1, cs); }
0301     [[nodiscard]] qsizetype lastIndexOf(QChar c, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0302     { return QtPrivate::lastIndexOf(*this, from, c.unicode(), cs); }
0303     [[nodiscard]] qsizetype lastIndexOf(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0304     { return lastIndexOf(s, size(), cs); }
0305     [[nodiscard]] qsizetype lastIndexOf(QStringView s, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0306     { return QtPrivate::lastIndexOf(*this, from, s, cs); }
0307     [[nodiscard]] inline qsizetype lastIndexOf(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
0308     [[nodiscard]] inline qsizetype lastIndexOf(QLatin1StringView s, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
0309 
0310 #if QT_CONFIG(regularexpression)
0311     [[nodiscard]] qsizetype indexOf(const QRegularExpression &re, qsizetype from = 0, QRegularExpressionMatch *rmatch = nullptr) const
0312     {
0313         return QtPrivate::indexOf(*this, re, from, rmatch);
0314     }
0315 #ifdef Q_QDOC
0316     [[nodiscard]] qsizetype lastIndexOf(const QRegularExpression &re, QRegularExpressionMatch *rmatch = nullptr) const;
0317 #else
0318     // prevent an ambiguity when called like this: lastIndexOf(re, 0)
0319     template <typename T = QRegularExpressionMatch, std::enable_if_t<std::is_same_v<T, QRegularExpressionMatch>, bool> = false>
0320     [[nodiscard]] qsizetype lastIndexOf(const QRegularExpression &re, T *rmatch = nullptr) const
0321     {
0322         return QtPrivate::lastIndexOf(*this, re, size(), rmatch);
0323     }
0324 #endif
0325     [[nodiscard]] qsizetype lastIndexOf(const QRegularExpression &re, qsizetype from, QRegularExpressionMatch *rmatch = nullptr) const
0326     {
0327         return QtPrivate::lastIndexOf(*this, re, from, rmatch);
0328     }
0329     [[nodiscard]] bool contains(const QRegularExpression &re, QRegularExpressionMatch *rmatch = nullptr) const
0330     {
0331         return QtPrivate::contains(*this, re, rmatch);
0332     }
0333     [[nodiscard]] qsizetype count(const QRegularExpression &re) const
0334     {
0335         return QtPrivate::count(*this, re);
0336     }
0337 #endif
0338 
0339     [[nodiscard]] bool isRightToLeft() const noexcept
0340     { return QtPrivate::isRightToLeft(*this); }
0341     [[nodiscard]] bool isValidUtf16() const noexcept
0342     { return QtPrivate::isValidUtf16(*this); }
0343 
0344     [[nodiscard]] bool isUpper() const noexcept
0345     { return QtPrivate::isUpper(*this); }
0346     [[nodiscard]] bool isLower() const noexcept
0347     { return QtPrivate::isLower(*this); }
0348 
0349     [[nodiscard]] inline short toShort(bool *ok = nullptr, int base = 10) const;
0350     [[nodiscard]] inline ushort toUShort(bool *ok = nullptr, int base = 10) const;
0351     [[nodiscard]] inline int toInt(bool *ok = nullptr, int base = 10) const;
0352     [[nodiscard]] inline uint toUInt(bool *ok = nullptr, int base = 10) const;
0353     [[nodiscard]] inline long toLong(bool *ok = nullptr, int base = 10) const;
0354     [[nodiscard]] inline ulong toULong(bool *ok = nullptr, int base = 10) const;
0355     [[nodiscard]] inline qlonglong toLongLong(bool *ok = nullptr, int base = 10) const;
0356     [[nodiscard]] inline qulonglong toULongLong(bool *ok = nullptr, int base = 10) const;
0357     [[nodiscard]] Q_CORE_EXPORT float toFloat(bool *ok = nullptr) const;
0358     [[nodiscard]] Q_CORE_EXPORT double toDouble(bool *ok = nullptr) const;
0359 
0360     [[nodiscard]] inline qsizetype toWCharArray(wchar_t *array) const; // defined in qstring.h
0361 
0362 
0363     [[nodiscard]] Q_CORE_EXPORT
0364     QList<QStringView> split(QStringView sep,
0365                              Qt::SplitBehavior behavior = Qt::KeepEmptyParts,
0366                              Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
0367     [[nodiscard]] Q_CORE_EXPORT
0368     QList<QStringView> split(QChar sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts,
0369                              Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
0370 
0371 #if QT_CONFIG(regularexpression)
0372     [[nodiscard]] Q_CORE_EXPORT
0373     QList<QStringView> split(const QRegularExpression &sep,
0374                              Qt::SplitBehavior behavior = Qt::KeepEmptyParts) const;
0375 #endif
0376 
0377     // QStringView <> QStringView
0378     friend bool comparesEqual(const QStringView &lhs, const QStringView &rhs) noexcept
0379     { return lhs.size() == rhs.size() && QtPrivate::equalStrings(lhs, rhs); }
0380     friend Qt::strong_ordering
0381     compareThreeWay(const QStringView &lhs, const QStringView &rhs) noexcept
0382     {
0383         const int res = QtPrivate::compareStrings(lhs, rhs);
0384         return Qt::compareThreeWay(res, 0);
0385     }
0386     Q_DECLARE_STRONGLY_ORDERED(QStringView)
0387 
0388     // QStringView <> QChar
0389     friend bool comparesEqual(const QStringView &lhs, QChar rhs) noexcept
0390     { return lhs.size() == 1 && lhs[0] == rhs; }
0391     friend Qt::strong_ordering compareThreeWay(const QStringView &lhs, QChar rhs) noexcept
0392     { return compareThreeWay(lhs, QStringView(&rhs, 1)); }
0393     Q_DECLARE_STRONGLY_ORDERED(QStringView, QChar)
0394 
0395     //
0396     // STL compatibility API:
0397     //
0398     [[nodiscard]] const_iterator begin()   const noexcept { return data(); }
0399     [[nodiscard]] const_iterator end()     const noexcept { return data() + size(); }
0400     [[nodiscard]] const_iterator cbegin()  const noexcept { return begin(); }
0401     [[nodiscard]] const_iterator cend()    const noexcept { return end(); }
0402     [[nodiscard]] const_reverse_iterator rbegin()  const noexcept { return const_reverse_iterator(end()); }
0403     [[nodiscard]] const_reverse_iterator rend()    const noexcept { return const_reverse_iterator(begin()); }
0404     [[nodiscard]] const_reverse_iterator crbegin() const noexcept { return rbegin(); }
0405     [[nodiscard]] const_reverse_iterator crend()   const noexcept { return rend(); }
0406 
0407     [[nodiscard]] constexpr bool empty() const noexcept { return size() == 0; }
0408     [[nodiscard]] constexpr QChar front() const { return Q_PRE(!empty()), QChar(m_data[0]); }
0409     [[nodiscard]] constexpr QChar back()  const { return Q_PRE(!empty()), QChar(m_data[m_size - 1]); }
0410 
0411     [[nodiscard]] Q_IMPLICIT operator std::u16string_view() const noexcept
0412     { return std::u16string_view(m_data, size_t(m_size)); }
0413 
0414     [[nodiscard]] constexpr qsizetype max_size() const noexcept { return maxSize(); }
0415 
0416     //
0417     // Qt compatibility API:
0418     //
0419     [[nodiscard]] const_iterator constBegin() const noexcept { return begin(); }
0420     [[nodiscard]] const_iterator constEnd() const noexcept { return end(); }
0421     [[nodiscard]] constexpr bool isNull() const noexcept { return !m_data; }
0422     [[nodiscard]] constexpr bool isEmpty() const noexcept { return empty(); }
0423     [[nodiscard]] constexpr qsizetype length() const noexcept
0424     { return size(); }
0425     [[nodiscard]] constexpr QChar first() const { return front(); }
0426     [[nodiscard]] constexpr QChar last()  const { return back(); }
0427 
0428     [[nodiscard]] static constexpr qsizetype maxSize() noexcept
0429     {
0430         // -1 to deal with the pointer one-past-the-end;
0431         return QtPrivate::MaxAllocSize / sizeof(storage_type) - 1;
0432     }
0433 private:
0434 #if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED)
0435     const storage_type *m_data = nullptr;
0436     qsizetype m_size = 0;
0437 #else
0438     qsizetype m_size = 0;
0439     const storage_type *m_data = nullptr;
0440 #endif
0441 
0442     Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos = 0,
0443                                           [[maybe_unused]] qsizetype n = 1) const
0444     {
0445         Q_PRE(pos >= 0);
0446         Q_PRE(pos <= size());
0447         Q_PRE(n >= 0);
0448         Q_PRE(n <= size() - pos);
0449     }
0450 
0451     constexpr int compare_single_char_helper(int diff) const noexcept
0452     { return diff ? diff : size() > 1 ? 1 : 0; }
0453 
0454     Q_CORE_EXPORT static bool equal_helper(QStringView sv, const char *data, qsizetype len);
0455     Q_CORE_EXPORT static int compare_helper(QStringView sv, const char *data, qsizetype len);
0456 
0457 #if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
0458     friend bool comparesEqual(const QStringView &lhs, const QByteArrayView &rhs) noexcept
0459     { return equal_helper(lhs, rhs.data(), rhs.size()); }
0460     friend Qt::strong_ordering
0461     compareThreeWay(const QStringView &lhs, const QByteArrayView &rhs) noexcept
0462     {
0463         const int res = compare_helper(lhs, rhs.data(), rhs.size());
0464         return Qt::compareThreeWay(res, 0);
0465     }
0466     Q_DECLARE_STRONGLY_ORDERED(QStringView, QByteArrayView, QT_ASCII_CAST_WARN)
0467     Q_DECLARE_STRONGLY_ORDERED(QStringView, QByteArray, QT_ASCII_CAST_WARN)
0468     Q_DECLARE_STRONGLY_ORDERED(QStringView, const char *, QT_ASCII_CAST_WARN)
0469 #endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
0470 };
0471 Q_DECLARE_TYPEINFO(QStringView, Q_PRIMITIVE_TYPE);
0472 
0473 template <typename QStringLike, typename std::enable_if<
0474     std::is_same<QStringLike, QString>::value,
0475     bool>::type = true>
0476 inline QStringView qToStringViewIgnoringNull(const QStringLike &s) noexcept
0477 { return QStringView(s.begin(), s.size()); }
0478 
0479 // QChar inline functions:
0480 
0481 [[nodiscard]] constexpr auto QChar::fromUcs4(char32_t c) noexcept
0482 {
0483     struct R {
0484         char16_t chars[2];
0485         [[nodiscard]] constexpr operator QStringView() const noexcept { return {begin(), end()}; }
0486         [[nodiscard]] constexpr qsizetype size() const noexcept { return chars[1] ? 2 : 1; }
0487         [[nodiscard]] constexpr const char16_t *begin() const noexcept { return chars; }
0488         [[nodiscard]] constexpr const char16_t *end() const noexcept { return begin() + size(); }
0489     };
0490     return requiresSurrogates(c) ? R{{QChar::highSurrogate(c),
0491                                       QChar::lowSurrogate(c)}} :
0492                                    R{{char16_t(c), u'\0'}} ;
0493 }
0494 
0495 qsizetype QtPrivate::findString(QStringView str, qsizetype from, QChar ch, Qt::CaseSensitivity cs) noexcept
0496 {
0497     if (from < -str.size()) // from < 0 && abs(from) > str.size(), avoiding overflow
0498         return -1;
0499     if (from < 0)
0500         from = qMax(from + str.size(), qsizetype(0));
0501     if (from < str.size()) {
0502         const char16_t *s = str.utf16();
0503         char16_t c = ch.unicode();
0504         const char16_t *n = s + from;
0505         const char16_t *e = s + str.size();
0506         if (cs == Qt::CaseSensitive)
0507             n = qustrchr(QStringView(n, e), c);
0508         else
0509             n = qustrcasechr(QStringView(n, e), c);
0510         if (n != e)
0511             return n - s;
0512     }
0513     return -1;
0514 }
0515 
0516 namespace Qt {
0517 inline namespace Literals {
0518 inline namespace StringLiterals {
0519 constexpr QStringView operator""_sv(const char16_t *str, size_t size) noexcept
0520 {
0521     return QStringView(str, qsizetype(size));
0522 }
0523 } // StringLiterals
0524 } // Literals
0525 } // Qt
0526 
0527 QT_END_NAMESPACE
0528 
0529 #endif /* QSTRINGVIEW_H */