summaryrefslogtreecommitdiffstats
path: root/src/corelib/text/qstringview.h
blob: c63ff6fbea187dcc7f6cdf847c34bc00513eb874 (plain)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
// Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com>// Copyright (C) 2019 Mail.ru Group.// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only#ifndef QSTRINGVIEW_H#define QSTRINGVIEW_H#include <QtCore/qchar.h>#include <QtCore/qcompare.h>#include <QtCore/qcontainerfwd.h>#include <QtCore/qbytearray.h>#include <QtCore/qstringfwd.h>#include <QtCore/qstringliteral.h>#include <QtCore/qstringalgorithms.h>#include <string>#include <string_view>#include <QtCore/q20type_traits.h>#if defined(Q_OS_DARWIN) || defined(Q_QDOC)Q_FORWARD_DECLARE_CF_TYPE(CFString);Q_FORWARD_DECLARE_OBJC_CLASS(NSString);#endif QT_BEGIN_NAMESPACE class QRegularExpression;class QRegularExpressionMatch;namespace QtPrivate {template<typename Char>struct IsCompatibleCharTypeHelper :std::integral_constant<bool,std::is_same<Char, QChar>::value ||std::is_same<Char, ushort>::value ||std::is_same<Char, char16_t>::value ||(std::is_same<Char,wchar_t>::value &&sizeof(wchar_t) ==sizeof(QChar))> {};template<typename Char>struct IsCompatibleCharType : IsCompatibleCharTypeHelper<q20::remove_cvref_t<Char>> {};template<typename Pointer>struct IsCompatiblePointerHelper :std::false_type {};template<typename Char>struct IsCompatiblePointerHelper<Char*>: IsCompatibleCharType<Char> {};template<typename Pointer>struct IsCompatiblePointer : IsCompatiblePointerHelper<q20::remove_cvref_t<Pointer>> {};template<typename T, typename Enable =void>struct IsContainerCompatibleWithQStringView :std::false_type {};template<typename T>struct IsContainerCompatibleWithQStringView<T,std::enable_if_t<std::conjunction_v<// lacking concepts and ranges, we accept any T whose std::data yields a suitable pointer ... IsCompatiblePointer<decltype(std::data(std::declval<const T &>()) )>,// ... and that has a suitable size ...std::is_convertible<decltype(std::size(std::declval<const T &>()) ), qsizetype>,// ... and it's a range as it defines an iterator-like API IsCompatibleCharType<typename std::iterator_traits<decltype(std::begin(std::declval<const T &>()) )>::value_type>,std::is_convertible<decltype(std::begin(std::declval<const T &>()) !=std::end(std::declval<const T &>()) ),bool>,// These need to be treated specially due to the empty vs null distinctionstd::negation<std::is_same<std::decay_t<T>, QString>>,#define QSTRINGVIEW_REFUSES_QSTRINGREF 1std::negation<std::is_same<q20::remove_cvref_t<T>, QStringRef>>,// QStringRef::op QStringView()// Don't make an accidental copy constructorstd::negation<std::is_same<std::decay_t<T>, QStringView>>>>> :std::true_type {};}// namespace QtPrivateclass QStringView {public:typedef char16_t storage_type;typedefconst QChar value_type;typedef std::ptrdiff_t difference_type;typedef qsizetype size_type;typedef value_type &reference;typedef value_type &const_reference;typedef value_type *pointer;typedef value_type *const_pointer;typedef pointer iterator;typedef const_pointer const_iterator;typedef std::reverse_iterator<iterator> reverse_iterator;typedef std::reverse_iterator<const_iterator> const_reverse_iterator;private:template<typename Char>using if_compatible_char = typename std::enable_if<QtPrivate::IsCompatibleCharType<Char>::value,bool>::type;template<typename Pointer>using if_compatible_pointer = typename std::enable_if<QtPrivate::IsCompatiblePointer<Pointer>::value,bool>::type;template<typename T>using if_compatible_qstring_like = typename std::enable_if<std::is_same<T, QString>::value,bool>::type;template<typename T>using if_compatible_container = typename std::enable_if<QtPrivate::IsContainerCompatibleWithQStringView<T>::value,bool>::type;template<typename Char>staticconstexpr qsizetype lengthHelperPointer(const Char *str) noexcept {if(q20::is_constant_evaluated())returnQtPrivate::lengthHelperPointer(str);returnQtPrivate::qustrlen(reinterpret_cast<const char16_t *>(str));}static qsizetype lengthHelperPointer(const QChar *str) noexcept {returnQtPrivate::qustrlen(reinterpret_cast<const char16_t *>(str));}template<typename Char>static const storage_type *castHelper(const Char *str) noexcept {return reinterpret_cast<const storage_type*>(str); }staticconstexprconst storage_type *castHelper(const storage_type *str) noexcept {return str; }public:constexprQStringView() noexcept {}constexprQStringView(std::nullptr_t) noexcept :QStringView() {}template<typename Char, if_compatible_char<Char> =true>constexprQStringView(const Char *str, qsizetype len)#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED):m_data(castHelper(str)),m_size((Q_PRE(len >=0),Q_PRE(str || !len), len))#else:m_size((Q_PRE(len >=0),Q_PRE(str || !len), len)),m_data(castHelper(str))#endif{}template<typename Char, if_compatible_char<Char> =true>constexprQStringView(const Char *f,const Char *l):QStringView(f, l - f) {}#ifdef Q_QDOCtemplate<typename Char,size_t N>constexprQStringView(constChar(&array)[N]) noexcept;template<typename Char>constexprQStringView(const Char *str) noexcept;#elsetemplate<typename Pointer, if_compatible_pointer<Pointer> =true>constexprQStringView(const Pointer &str) noexcept :QStringView(str, str ?lengthHelperPointer(str) :0) {}template<typename Char, if_compatible_char<Char> =true>constexprQStringView(constChar(&str)[]) noexcept // array of unknown bounds: QStringView{&*str} {}// decay to pointer#endif#ifdef Q_QDOCQStringView(const QString &str) noexcept;#elsetemplate<typename String, if_compatible_qstring_like<String> =true>QStringView(const String &str) noexcept : QStringView{str.begin(), str.size()} {}#endiftemplate<typename Container, if_compatible_container<Container> =true> Q_ALWAYS_INLINE constexprQStringView(const Container &c) noexcept :QStringView(std::data(c),QtPrivate::lengthHelperContainer(c)) {}template<typename Char,size_t Size, if_compatible_char<Char> =true>[[nodiscard]]constexprstatic QStringView fromArray(constChar(&string)[Size]) noexcept {returnQStringView(string, Size); }[[nodiscard]]inline QString toString()const;// defined in qstring.h#if defined(Q_OS_DARWIN) || defined(Q_QDOC)// defined in qcore_foundation.mm[[nodiscard]] Q_CORE_EXPORT CFStringRef toCFString()const Q_DECL_CF_RETURNS_RETAINED;[[nodiscard]] Q_CORE_EXPORT NSString *toNSString()const Q_DECL_NS_RETURNS_AUTORELEASED;#endif[[nodiscard]]constexpr qsizetype size()const noexcept {return m_size; }[[nodiscard]] const_pointer data()const noexcept {return reinterpret_cast<const_pointer>(m_data); }[[nodiscard]] const_pointer constData()const noexcept {returndata(); }[[nodiscard]]constexprconst storage_type *utf16()const noexcept {return m_data; }[[nodiscard]]constexpr QChar operator[](qsizetype n)const{verify(n,1);returnQChar(m_data[n]); }//// QString API//template<typename...Args>[[nodiscard]]inline QString arg(Args &&...args)const;// defined in qstring.h[[nodiscard]] QByteArray toLatin1()const{returnQtPrivate::convertToLatin1(*this); }[[nodiscard]] QByteArray toUtf8()const{returnQtPrivate::convertToUtf8(*this); }[[nodiscard]] QByteArray toLocal8Bit()const{returnQtPrivate::convertToLocal8Bit(*this); }[[nodiscard]]inline QList<uint>toUcs4()const;// defined in qlist.h ### Qt 7 char32_t[[nodiscard]]constexpr QChar at(qsizetype n)const noexcept {return(*this)[n]; }[[nodiscard]]constexpr QStringView mid(qsizetype pos, qsizetype n = -1)const noexcept {using namespace QtPrivate;auto result =QContainerImplHelper::mid(size(), &pos, &n);return result ==QContainerImplHelper::Null ?QStringView() :QStringView(m_data + pos, n);}[[nodiscard]]constexpr QStringView left(qsizetype n)const noexcept {if(size_t(n) >=size_t(size())) n =size();returnQStringView(m_data, n);}[[nodiscard]]constexpr QStringView right(qsizetype n)const noexcept {if(size_t(n) >=size_t(size())) n =size();returnQStringView(m_data + m_size - n, n);}[[nodiscard]]constexpr QStringView first(qsizetype n)const noexcept {verify(0, n);returnsliced(0, n); }[[nodiscard]]constexpr QStringView last(qsizetype n)const noexcept {verify(0, n);returnsliced(size() - n, n); }[[nodiscard]]constexpr QStringView sliced(qsizetype pos)const noexcept {verify(pos,0);returnQStringView(m_data + pos,size() - pos); }[[nodiscard]]constexpr QStringView sliced(qsizetype pos, qsizetype n)const noexcept {verify(pos, n);returnQStringView(m_data + pos, n); }[[nodiscard]]constexpr QStringView chopped(qsizetype n)const noexcept {verify(0, n);returnsliced(0, m_size - n); }constexprvoidtruncate(qsizetype n) noexcept {verify(0, n); ; m_size = n; }constexprvoidchop(qsizetype n) noexcept {verify(0, n); m_size -= n; }[[nodiscard]] QStringView trimmed()const noexcept {returnQtPrivate::trimmed(*this); }constexpr QStringView &slice(qsizetype pos){ *this=sliced(pos);return*this; }constexpr QStringView &slice(qsizetype pos, qsizetype n){ *this=sliced(pos, n);return*this; }template<typename Needle, typename...Flags>[[nodiscard]]constexpr inline autotokenize(Needle &&needle, Flags...flags)constnoexcept(noexcept(qTokenize(std::declval<const QStringView&>(),std::forward<Needle>(needle), flags...)))->decltype(qTokenize(*this,std::forward<Needle>(needle), flags...)){returnqTokenize(*this,std::forward<Needle>(needle), flags...); }[[nodiscard]]intcompare(QStringView other,Qt::CaseSensitivity cs =Qt::CaseSensitive)const noexcept {returnQtPrivate::compareStrings(*this, other, cs); }[[nodiscard]]inlineintcompare(QLatin1StringView other,Qt::CaseSensitivity cs =Qt::CaseSensitive)const noexcept;[[nodiscard]]inlineintcompare(QUtf8StringView other,Qt::CaseSensitivity cs =Qt::CaseSensitive)const noexcept;[[nodiscard]]constexprintcompare(QChar c)const noexcept {returnsize() >=1?compare_single_char_helper(*utf16() - c.unicode()) : -1; }[[nodiscard]]intcompare(QChar c,Qt::CaseSensitivity cs)const noexcept {returnQtPrivate::compareStrings(*this,QStringView(&c,1), cs); }[[nodiscard]]inlineintlocaleAwareCompare(QStringView other)const;[[nodiscard]]boolstartsWith(QStringView s,Qt::CaseSensitivity cs =Qt::CaseSensitive)const noexcept {returnQtPrivate::startsWith(*this, s, cs); }[[nodiscard]]inlineboolstartsWith(QLatin1StringView s,Qt::CaseSensitivity cs =Qt::CaseSensitive)const noexcept;[[nodiscard]]boolstartsWith(QChar c)const noexcept {return!empty() &&front() == c; }[[nodiscard]]boolstartsWith(QChar c,Qt::CaseSensitivity cs)const noexcept {returnQtPrivate::startsWith(*this,QStringView(&c,1), cs); }[[nodiscard]]boolendsWith(QStringView s,Qt::CaseSensitivity cs =Qt::CaseSensitive)const noexcept {returnQtPrivate::endsWith(*this, s, cs); }[[nodiscard]]inlineboolendsWith(QLatin1StringView s,Qt::CaseSensitivity cs =Qt::CaseSensitive)const noexcept;[[nodiscard]]boolendsWith(QChar c)const noexcept {return!empty() &&back() == c; }[[nodiscard]]boolendsWith(QChar c,Qt::CaseSensitivity cs)const noexcept {returnQtPrivate::endsWith(*this,QStringView(&c,1), cs); }[[nodiscard]] qsizetype indexOf(QChar c, qsizetype from =0,Qt::CaseSensitivity cs =Qt::CaseSensitive)const noexcept {returnQtPrivate::findString(*this, from, c.unicode(), cs); }[[nodiscard]] qsizetype indexOf(QStringView s, qsizetype from =0,Qt::CaseSensitivity cs =Qt::CaseSensitive)const noexcept {returnQtPrivate::findString(*this, from, s, cs); }[[nodiscard]]inline qsizetype indexOf(QLatin1StringView s, qsizetype from =0,Qt::CaseSensitivity cs =Qt::CaseSensitive)const noexcept;[[nodiscard]]boolcontains(QChar c,Qt::CaseSensitivity cs =Qt::CaseSensitive)const noexcept {returnindexOf(QStringView(&c,1),0, cs) !=qsizetype(-1); }[[nodiscard]]boolcontains(QStringView s,Qt::CaseSensitivity cs =Qt::CaseSensitive)const noexcept {returnindexOf(s,0, cs) !=qsizetype(-1); }[[nodiscard]]inlineboolcontains(QLatin1StringView s,Qt::CaseSensitivity cs =Qt::CaseSensitive)const noexcept;[[nodiscard]] qsizetype count(QChar c,Qt::CaseSensitivity cs =Qt::CaseSensitive)const noexcept {returnQtPrivate::count(*this, c, cs); }[[nodiscard]] qsizetype count(QStringView s,Qt::CaseSensitivity cs =Qt::CaseSensitive)const noexcept {returnQtPrivate::count(*this, s, cs); }[[nodiscard]]inline qsizetype count(QLatin1StringView s,Qt::CaseSensitivity cs =Qt::CaseSensitive)const;[[nodiscard]] qsizetype lastIndexOf(QChar c,Qt::CaseSensitivity cs =Qt::CaseSensitive)const noexcept {returnlastIndexOf(c, -1, cs); }[[nodiscard]] qsizetype lastIndexOf(QChar c, qsizetype from,Qt::CaseSensitivity cs =Qt::CaseSensitive)const noexcept {returnQtPrivate::lastIndexOf(*this, from, c.unicode(), cs); }[[nodiscard]] qsizetype lastIndexOf(QStringView s,Qt::CaseSensitivity cs =Qt::CaseSensitive)const noexcept {returnlastIndexOf(s,size(), cs); }[[nodiscard]] qsizetype lastIndexOf(QStringView s, qsizetype from,Qt::CaseSensitivity cs =Qt::CaseSensitive)const noexcept {returnQtPrivate::lastIndexOf(*this, from, s, cs); }[[nodiscard]]inline qsizetype lastIndexOf(QLatin1StringView s,Qt::CaseSensitivity cs =Qt::CaseSensitive)const noexcept;[[nodiscard]]inline qsizetype lastIndexOf(QLatin1StringView s, qsizetype from,Qt::CaseSensitivity cs =Qt::CaseSensitive)const noexcept;#if QT_CONFIG(regularexpression)[[nodiscard]] qsizetype indexOf(const QRegularExpression &re, qsizetype from =0, QRegularExpressionMatch *rmatch =nullptr)const{returnQtPrivate::indexOf(*this, re, from, rmatch);}#ifdef Q_QDOC[[nodiscard]] qsizetype lastIndexOf(const QRegularExpression &re, QRegularExpressionMatch *rmatch =nullptr)const;#else// prevent an ambiguity when called like this: lastIndexOf(re, 0)template<typename T = QRegularExpressionMatch,std::enable_if_t<std::is_same_v<T, QRegularExpressionMatch>,bool> =false>[[nodiscard]] qsizetype lastIndexOf(const QRegularExpression &re, T *rmatch =nullptr)const{returnQtPrivate::lastIndexOf(*this, re,size(), rmatch);}#endif[[nodiscard]] qsizetype lastIndexOf(const QRegularExpression &re, qsizetype from, QRegularExpressionMatch *rmatch =nullptr)const{returnQtPrivate::lastIndexOf(*this, re, from, rmatch);}[[nodiscard]]boolcontains(const QRegularExpression &re, QRegularExpressionMatch *rmatch =nullptr)const{returnQtPrivate::contains(*this, re, rmatch);}[[nodiscard]] qsizetype count(const QRegularExpression &re)const{returnQtPrivate::count(*this, re);}#endif[[nodiscard]]boolisRightToLeft()const noexcept {returnQtPrivate::isRightToLeft(*this); }[[nodiscard]]boolisValidUtf16()const noexcept {returnQtPrivate::isValidUtf16(*this); }[[nodiscard]]boolisUpper()const noexcept {returnQtPrivate::isUpper(*this); }[[nodiscard]]boolisLower()const noexcept {returnQtPrivate::isLower(*this); }[[nodiscard]]inlineshorttoShort(bool*ok =nullptr,int base =10)const;[[nodiscard]]inline ushort toUShort(bool*ok =nullptr,int base =10)const;[[nodiscard]]inlineinttoInt(bool*ok =nullptr,int base =10)const;[[nodiscard]]inline uint toUInt(bool*ok =nullptr,int base =10)const;[[nodiscard]]inlinelongtoLong(bool*ok =nullptr,int base =10)const;[[nodiscard]]inline ulong toULong(bool*ok =nullptr,int base =10)const;[[nodiscard]]inline qlonglong toLongLong(bool*ok =nullptr,int base =10)const;[[nodiscard]]inline qulonglong toULongLong(bool*ok =nullptr,int base =10)const;[[nodiscard]] Q_CORE_EXPORT floattoFloat(bool*ok =nullptr)const;[[nodiscard]] Q_CORE_EXPORT doubletoDouble(bool*ok =nullptr)const;[[nodiscard]]inline qsizetype toWCharArray(wchar_t*array)const;// defined in qstring.h[[nodiscard]] Q_CORE_EXPORT QList<QStringView>split(QStringView sep,Qt::SplitBehavior behavior =Qt::KeepEmptyParts,Qt::CaseSensitivity cs =Qt::CaseSensitive)const;[[nodiscard]] Q_CORE_EXPORT QList<QStringView>split(QChar sep,Qt::SplitBehavior behavior =Qt::KeepEmptyParts,Qt::CaseSensitivity cs =Qt::CaseSensitive)const;#if QT_CONFIG(regularexpression)[[nodiscard]] Q_CORE_EXPORT QList<QStringView>split(const QRegularExpression &sep,Qt::SplitBehavior behavior =Qt::KeepEmptyParts)const;#endif// QStringView <> QStringViewfriendboolcomparesEqual(const QStringView &lhs,const QStringView &rhs) noexcept {return lhs.size() == rhs.size() &&QtPrivate::equalStrings(lhs, rhs); }friend Qt::strong_ordering compareThreeWay(const QStringView &lhs,const QStringView &rhs) noexcept {const int res =QtPrivate::compareStrings(lhs, rhs);returnQt::compareThreeWay(res,0);}Q_DECLARE_STRONGLY_ORDERED(QStringView)// QStringView <> QCharfriendboolcomparesEqual(const QStringView &lhs, QChar rhs) noexcept {return lhs.size() ==1&& lhs[0] == rhs; }friend Qt::strong_ordering compareThreeWay(const QStringView &lhs, QChar rhs) noexcept {returncompareThreeWay(lhs,QStringView(&rhs,1)); }Q_DECLARE_STRONGLY_ORDERED(QStringView, QChar)//// STL compatibility API://[[nodiscard]] const_iterator begin()const noexcept {returndata(); }[[nodiscard]] const_iterator end()const noexcept {returndata() +size(); }[[nodiscard]] const_iterator cbegin()const noexcept {returnbegin(); }[[nodiscard]] const_iterator cend()const noexcept {returnend(); }[[nodiscard]] const_reverse_iterator rbegin()const noexcept {returnconst_reverse_iterator(end()); }[[nodiscard]] const_reverse_iterator rend()const noexcept {returnconst_reverse_iterator(begin()); }[[nodiscard]] const_reverse_iterator crbegin()const noexcept {returnrbegin(); }[[nodiscard]] const_reverse_iterator crend()const noexcept {returnrend(); }[[nodiscard]]constexprboolempty()const noexcept {returnsize() ==0; }[[nodiscard]]constexpr QChar front()const{returnQ_PRE(!empty()),QChar(m_data[0]); }[[nodiscard]]constexpr QChar back()const{returnQ_PRE(!empty()),QChar(m_data[m_size -1]); }[[nodiscard]] Q_IMPLICIT operator std::u16string_view()const noexcept {returnstd::u16string_view(m_data,size_t(m_size)); }[[nodiscard]]constexpr qsizetype max_size()const noexcept {returnmaxSize(); }//// Qt compatibility API://[[nodiscard]] const_iterator constBegin()const noexcept {returnbegin(); }[[nodiscard]] const_iterator constEnd()const noexcept {returnend(); }[[nodiscard]]constexprboolisNull()const noexcept {return!m_data; }[[nodiscard]]constexprboolisEmpty()const noexcept {returnempty(); }[[nodiscard]]constexpr qsizetype length()const noexcept {returnsize(); }[[nodiscard]]constexpr QChar first()const{returnfront(); }[[nodiscard]]constexpr QChar last()const{returnback(); }[[nodiscard]]staticconstexpr qsizetype maxSize() noexcept {// -1 to deal with the pointer one-past-the-end;returnQtPrivate::MaxAllocSize /sizeof(storage_type) -1;}private:#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED)const storage_type *m_data =nullptr; qsizetype m_size =0;#else qsizetype m_size =0;const storage_type *m_data =nullptr;#endif Q_ALWAYS_INLINE constexprvoidverify([[maybe_unused]] qsizetype pos =0,[[maybe_unused]] qsizetype n =1)const{Q_PRE(pos >=0);Q_PRE(pos <=size());Q_PRE(n >=0);Q_PRE(n <=size() - pos);}constexprintcompare_single_char_helper(int diff)const noexcept {return diff ? diff :size() >1?1:0; } Q_CORE_EXPORT static boolequal_helper(QStringView sv,const char*data, qsizetype len); Q_CORE_EXPORT static intcompare_helper(QStringView sv,const char*data, qsizetype len);#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)friendboolcomparesEqual(const QStringView &lhs,const QByteArrayView &rhs) noexcept {returnequal_helper(lhs, rhs.data(), rhs.size()); }friend Qt::strong_ordering compareThreeWay(const QStringView &lhs,const QByteArrayView &rhs) noexcept {const int res =compare_helper(lhs, rhs.data(), rhs.size());returnQt::compareThreeWay(res,0);}Q_DECLARE_STRONGLY_ORDERED(QStringView, QByteArrayView, QT_ASCII_CAST_WARN)Q_DECLARE_STRONGLY_ORDERED(QStringView, QByteArray, QT_ASCII_CAST_WARN)Q_DECLARE_STRONGLY_ORDERED(QStringView,const char*, QT_ASCII_CAST_WARN)#endif// !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)};Q_DECLARE_TYPEINFO(QStringView, Q_PRIMITIVE_TYPE);template<typename QStringLike, typename std::enable_if<std::is_same<QStringLike, QString>::value,bool>::type =true>inline QStringView qToStringViewIgnoringNull(const QStringLike &s) noexcept {returnQStringView(s.begin(), s.size()); }// QChar inline functions:[[nodiscard]]constexpr auto QChar::fromUcs4(char32_t c) noexcept {struct R { char16_t chars[2];[[nodiscard]]constexpr operatorQStringView()const noexcept {return{begin(),end()}; }[[nodiscard]]constexpr qsizetype size()const noexcept {return chars[1] ?2:1; }[[nodiscard]]constexprconst char16_t *begin()const noexcept {return chars; }[[nodiscard]]constexprconst char16_t *end()const noexcept {returnbegin() +size(); }};returnrequiresSurrogates(c) ? R{{QChar::highSurrogate(c),QChar::lowSurrogate(c)}} : R{{char16_t(c), u'\0'}} ;} qsizetype QtPrivate::findString(QStringView str, qsizetype from, QChar ch,Qt::CaseSensitivity cs) noexcept {if(from < -str.size())// from < 0 && abs(from) > str.size(), avoiding overflowreturn-1;if(from <0) from =qMax(from + str.size(),qsizetype(0));if(from < str.size()) {const char16_t *s = str.utf16(); char16_t c = ch.unicode();const char16_t *n = s + from;const char16_t *e = s + str.size();if(cs ==Qt::CaseSensitive) n =qustrchr(QStringView(n, e), c);else n =qustrcasechr(QStringView(n, e), c);if(n != e)return n - s;}return-1;}namespace Qt {inlinenamespace Literals {inlinenamespace StringLiterals {constexpr QStringView operator""_sv(const char16_t *str,size_t size) noexcept {returnQStringView(str,qsizetype(size));}}// StringLiterals}// Literals}// Qt QT_END_NAMESPACE #endif/* QSTRINGVIEW_H */
close