8#include <QtCore/qchar.h>
9#include <QtCore/qcompare.h>
10#include <QtCore/qcontainerfwd.h>
11#include <QtCore/qbytearray.h>
12#include <QtCore/qstringfwd.h>
13#include <QtCore/qstringalgorithms.h>
17#include <QtCore/q20type_traits.h>
19#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
20Q_FORWARD_DECLARE_CF_TYPE(CFString);
21Q_FORWARD_DECLARE_OBJC_CLASS(NSString);
26class QRegularExpression;
27class QRegularExpressionMatch;
30template <
typename Char>
37template <
typename Char>
41template <
typename Pointer>
43template <
typename Char>
46template <
typename Pointer>
50template <
typename T,
typename Enable =
void>
67#define QSTRINGVIEW_REFUSES_QSTRINGREF 1
94 template <
typename Char>
97 template <
typename Pointer>
100 template <
typename T>
103 template <
typename T>
106 template <
typename Char>
107 static constexpr qsizetype lengthHelperPointer(
const Char *str)
noexcept
109 if (q20::is_constant_evaluated())
110 return QtPrivate::lengthHelperPointer(str);
111 return QtPrivate::qustrlen(
reinterpret_cast<
const char16_t *>(str));
113 static qsizetype lengthHelperPointer(
const QChar *str)
noexcept
115 return QtPrivate::qustrlen(
reinterpret_cast<
const char16_t *>(str));
118 template <
typename Char>
119 static const storage_type *castHelper(
const Char *str)
noexcept
129 template <
typename Char, if_compatible_char<Char> =
true>
131#if QT_VERSION >= QT_VERSION_CHECK(7
, 0
, 0
) || defined(QT_BOOTSTRAPPED)
136 m_data(castHelper(str))
140 template <
typename Char, if_compatible_char<Char> =
true>
148 template <
typename Char>
151 template <
typename Pointer, if_compatible_pointer<Pointer> =
true>
153 :
QStringView(str, str ? lengthHelperPointer(str) : 0) {}
155 template <
typename Char, if_compatible_char<Char> =
true>
163 template <
typename String, if_compatible_qstring_like<String> =
true>
172 template <
typename Char, size_t Size, if_compatible_char<Char> =
true>
177#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
186 return QtPrivate::MaxAllocSize /
sizeof(storage_type) - 1;
190 constexpr size_t MaxSize = maxSize();
191 Q_PRESUME(size_t(m_size) <= MaxSize);
199 { verify(n, 1);
return QChar(m_data[n]); }
205 template <
typename...Args>
213 [[nodiscard]]
constexpr QChar at(qsizetype n)
const QT6_ONLY(
noexcept) {
return (*
this)[
n]; }
265#if (!defined(Q_CC_GNU_ONLY) || Q_CC_GNU_ONLY >= 1700
) && __has_builtin(__builtin_constant_p)
272 [[nodiscard]]
inline int compare(QUtf8StringView other, Qt::CaseSensitivity cs =
Qt::
CaseSensitive)
const noexcept;
273 [[nodiscard]]
constexpr int compare(QChar c)
const noexcept
274 {
return size() >= 1 ? compare_single_char_helper(*utf16() - c.unicode()) : -1; }
275 [[nodiscard]]
int compare(QChar c, Qt::CaseSensitivity cs)
const noexcept
277#if (!defined(Q_CC_GNU_ONLY) || Q_CC_GNU_ONLY >= 1700
) && __has_builtin(__builtin_constant_p)
278 if (__builtin_constant_p(cs) && cs == Qt::CaseSensitive)
281 return QtPrivate::compareStrings(*
this, QStringView(&c, 1), cs);
288#if (!defined(Q_CC_GNU_ONLY) || Q_CC_GNU_ONLY >= 1700
) && __has_builtin(__builtin_constant_p)
289 if (__builtin_constant_p(s.m_size) && s.size() == 1)
290 return startsWith(s.front(), cs);
292 return QtPrivate::startsWith(*
this, s, cs);
296 {
return !empty() && front() == c; }
297 [[nodiscard]]
bool startsWith(QChar c, Qt::CaseSensitivity cs)
const noexcept
299#if (!defined(Q_CC_GNU_ONLY) || Q_CC_GNU_ONLY >= 1700
) && __has_builtin(__builtin_constant_p)
300 if (__builtin_constant_p(cs) && cs == Qt::CaseSensitive)
301 return startsWith(c);
303 return QtPrivate::startsWith(*
this, QStringView(&c, 1), cs);
308#if (!defined(Q_CC_GNU_ONLY) || Q_CC_GNU_ONLY >= 1700
) && __has_builtin(__builtin_constant_p)
309 if (__builtin_constant_p(s.m_size) && s.size() == 1)
310 return endsWith(s.front(), cs);
312 return QtPrivate::endsWith(*
this, s, cs);
314 [[nodiscard]]
inline bool endsWith(QLatin1StringView s, Qt::CaseSensitivity cs =
Qt::
CaseSensitive)
const noexcept;
315 [[nodiscard]]
bool endsWith(QChar c)
const noexcept
316 {
return !empty() && back() == c; }
317 [[nodiscard]]
bool endsWith(QChar c, Qt::CaseSensitivity cs)
const noexcept
319#if (!defined(Q_CC_GNU_ONLY) || Q_CC_GNU_ONLY >= 1700
) && __has_builtin(__builtin_constant_p)
320 if (__builtin_constant_p(cs) && cs == Qt::CaseSensitive)
323 return QtPrivate::endsWith(*
this, QStringView(&c, 1), cs);
327 {
return QtPrivate::findString(*
this, from, c.unicode(), cs); }
330#if (!defined(Q_CC_GNU_ONLY) || Q_CC_GNU_ONLY >= 1700
) && __has_builtin(__builtin_constant_p)
331 if (__builtin_constant_p(s.m_size) && s.size() == 1)
332 return indexOf(s.front(), from, cs);
334 return QtPrivate::findString(*
this, from, s, cs);
339 {
return indexOf(c, 0, cs) != qsizetype(-1); }
341 {
return indexOf(s, 0, cs) != qsizetype(-1); }
342 [[nodiscard]]
inline bool contains(QLatin1StringView s, Qt::CaseSensitivity cs =
Qt::
CaseSensitive)
const noexcept;
345 {
return QtPrivate::count(*
this, c, cs); }
348#if (!defined(Q_CC_GNU_ONLY) || Q_CC_GNU_ONLY >= 1700
) && __has_builtin(__builtin_constant_p)
349 if (__builtin_constant_p(s.m_size) && s.size() == 1)
350 return count(s.front(), cs);
352 return QtPrivate::count(*
this, s, cs);
357 {
return lastIndexOf(c, -1, cs); }
359 {
return QtPrivate::lastIndexOf(*
this, from, c.unicode(), cs); }
361 {
return lastIndexOf(s, size(), cs); }
364#if (!defined(Q_CC_GNU_ONLY) || Q_CC_GNU_ONLY >= 1700
) && __has_builtin(__builtin_constant_p)
365 if (__builtin_constant_p(s.m_size) && s.size() == 1)
366 return lastIndexOf(s.front(), from, cs);
368 return QtPrivate::lastIndexOf(*
this, from, s, cs);
373#if QT_CONFIG(regularexpression)
400 {
return QtPrivate::isRightToLeft(*
this); }
402 {
return QtPrivate::isValidUtf16(*
this); }
405 {
return QtPrivate::isUpper(*
this); }
407 {
return QtPrivate::isLower(*
this); }
409 [[nodiscard]]
inline short toShort(
bool *ok =
nullptr,
int base = 10)
const;
410 [[nodiscard]]
inline ushort
toUShort(
bool *ok =
nullptr,
int base = 10)
const;
411 [[nodiscard]]
inline int toInt(
bool *ok =
nullptr,
int base = 10)
const;
412 [[nodiscard]]
inline uint
toUInt(
bool *ok =
nullptr,
int base = 10)
const;
413 [[nodiscard]]
inline long toLong(
bool *ok =
nullptr,
int base = 10)
const;
414 [[nodiscard]]
inline ulong
toULong(
bool *ok =
nullptr,
int base = 10)
const;
423 [[nodiscard]] Q_CORE_EXPORT
431#if QT_CONFIG(regularexpression)
439 {
return lhs.size() == rhs.size() && QtPrivate::equalStrings(lhs, rhs); }
443 const int res = QtPrivate::compareStrings(lhs, rhs);
444 return Qt::compareThreeWay(res, 0);
450 {
return lhs.size() == 1 && lhs
[0
] == rhs; }
488#if QT_VERSION >= QT_VERSION_CHECK(7
, 0
, 0
) || defined(QT_BOOTSTRAPPED)
496 Q_ALWAYS_INLINE
constexpr void verify([[maybe_unused]] qsizetype pos = 0,
497 [[maybe_unused]] qsizetype n = 1)
const
500 Q_PRE(pos <= size());
502 Q_PRE(n <= size() - pos);
505 constexpr int compare_single_char_helper(
int diff)
const noexcept
506 {
return diff ? diff : size() > 1 ? 1 : 0; }
508 Q_CORE_EXPORT
static bool equal_helper(
QStringView sv,
const char *data, qsizetype len);
509 Q_CORE_EXPORT
static int compare_helper(
QStringView sv,
const char *data, qsizetype len);
511#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
513 {
return equal_helper(lhs, rhs.data(), rhs.size()); }
517 const int res = compare_helper(lhs, rhs.data(), rhs.size());
518 return Qt::compareThreeWay(res, 0);
520 Q_DECLARE_STRONGLY_ORDERED(
QStringView, QByteArrayView, QT_ASCII_CAST_WARN)
527template <
typename QStringLike,
typename std::enable_if<
528 std::is_same<QStringLike, QString>::value,
535 const qsizetype size = str.size();
538 if (size_t(from) < size_t(size)) {
540 char16_t c = ch.unicode();
541 const char16_t *n = s + from;
542 const char16_t *e = s + size;
543 if (cs == Qt::CaseSensitive)
544 n = qustrchr(QStringView(n, e), c);
546 n = qustrcasechr(QStringView(n, e), c);
548 qsizetype ret = n - s;
QString arg(Args &&...args) const
bool isLower() const noexcept
bool startsWith(QChar c) const noexcept
bool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
qsizetype lastIndexOf(QStringView s, qsizetype from, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
Q_CORE_EXPORT double toDouble(bool *ok=nullptr) const
Returns the string view converted to a double value.
bool isRightToLeft() const noexcept
constexpr QStringView(std::nullptr_t) noexcept
Constructs a null string view.
Q_CORE_EXPORT float toFloat(bool *ok=nullptr) const
Returns the string view converted to a float value.
bool startsWith(QStringView s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
constexpr QStringView() noexcept
Constructs a null string view.
qsizetype count(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
int localeAwareCompare(QStringView other) const
value_type & reference
Alias for {value_type &}.
std::ptrdiff_t difference_type
Alias for {std::ptrdiff_t}.
static constexpr qsizetype maxSize() noexcept
friend bool comparesEqual(const QStringView &lhs, const QStringView &rhs) noexcept
ushort toUShort(bool *ok=nullptr, int base=10) const
Returns the string view converted to an {unsigned short} using base base, which is 10 by default and ...
constexpr int compare(QChar c) const noexcept
friend Qt::strong_ordering compareThreeWay(const QStringView &lhs, QChar rhs) noexcept
constexpr QStringView(const Char *str, qsizetype len)
constexpr const storage_type * utf16() const noexcept
char16_t storage_type
Alias for {char16_t}.
qsizetype count(QStringView s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
const QChar value_type
Alias for {const QChar}.
const_pointer data() const noexcept
qsizetype lastIndexOf(QChar c, qsizetype from, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
const_pointer constData() const noexcept
static constexpr QStringView fromArray(const Char(&string)[Size]) noexcept
Constructs a string view on the full character string literal string of Size elements,...
QList< uint > toUcs4() const
Returns a UCS-4/UTF-32 representation of the string view as a QList<uint>.
QByteArray toLatin1() const
Returns a Latin-1 representation of the string as a QByteArray.
bool endsWith(QStringView s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
QByteArray toUtf8() const
Returns a UTF-8 representation of the string view as a QByteArray.
QByteArray toLocal8Bit() const
Returns a local 8-bit representation of the string as a QByteArray.
bool endsWith(QChar c) const noexcept
bool contains(QStringView s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
const_pointer const_iterator
This typedef provides an STL-style const iterator for QStringView.
value_type * pointer
Alias for {value_type *}.
QString toString() const
Returns a deep copy of this string view's data as a QString.
int toInt(bool *ok=nullptr, int base=10) const
Returns the string view converted to an int using base base, which is 10 by default and must be betwe...
constexpr QChar at(qsizetype n) const QT6_ONLY(noexcept)
Returns the character at position n in this string view.
qlonglong toLongLong(bool *ok=nullptr, int base=10) const
Returns the string view converted to a {long long} using base base, which is 10 by default and must b...
friend Qt::strong_ordering compareThreeWay(const QStringView &lhs, const QByteArrayView &rhs) noexcept
value_type * const_pointer
Alias for {value_type *}.
ulong toULong(bool *ok=nullptr, int base=10) const
Returns the string view converted to an {unsigned long} using base base, which is 10 by default and m...
bool isValidUtf16() const noexcept
constexpr QChar operator[](qsizetype n) const
Returns the character at position n in this string view.
constexpr qsizetype size() const noexcept
Returns the size of this string view, in UTF-16 code units (that is, surrogate pairs count as two for...
qulonglong toULongLong(bool *ok=nullptr, int base=10) const
Returns the string view converted to an {unsigned long long} using base base, which is 10 by default ...
constexpr QStringView(const Char *f, const Char *l)
Constructs a string view on first with length (last - first).
constexpr QStringView(const Char(&str)[]) noexcept
value_type & const_reference
Alias for {value_type &}.
short toShort(bool *ok=nullptr, int base=10) const
Returns the string view converted to a short using base base, which is 10 by default and must be betw...
int compare(QUtf8StringView other, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
bool endsWith(QLatin1StringView s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
friend bool comparesEqual(const QStringView &lhs, QChar rhs) noexcept
qsizetype size_type
Alias for qsizetype.
pointer iterator
This typedef provides an STL-style const iterator for QStringView.
qsizetype indexOf(QStringView s, qsizetype from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
bool isUpper() const noexcept
qsizetype toWCharArray(wchar_t *array) const
bool startsWith(QLatin1StringView s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
qsizetype lastIndexOf(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
friend bool comparesEqual(const QStringView &lhs, const QByteArrayView &rhs) noexcept
uint toUInt(bool *ok=nullptr, int base=10) const
Returns the string view converted to an {unsigned int} using base base, which is 10 by default and mu...
qsizetype lastIndexOf(QStringView s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
qsizetype indexOf(QChar c, qsizetype from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
long toLong(bool *ok=nullptr, int base=10) const
Returns the string view converted to a long using base base, which is 10 by default and must be betwe...
std::reverse_iterator< const_iterator > const_reverse_iterator
This typedef provides an STL-style const reverse iterator for QStringView.
friend Qt::strong_ordering compareThreeWay(const QStringView &lhs, const QStringView &rhs) noexcept
constexpr QStringView(const Pointer &str) noexcept
Combined button and popup list for selecting options.
constexpr QStringView operator""_sv(const char16_t *str, size_t size) noexcept
QStringView qToStringViewIgnoringNull(const QStringLike &s) noexcept
Q_DECLARE_TYPEINFO(QStringView, Q_PRIMITIVE_TYPE)