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 noexcept {
return (*
this)[n]; }
215 [[nodiscard]]
constexpr QStringView mid(qsizetype pos, qsizetype n = -1)
const noexcept
218 auto result = QContainerImplHelper::mid(size(), &pos, &n);
219 return result == QContainerImplHelper::Null ? QStringView() : QStringView(m_data + pos, n);
223 if (size_t(n) >= size_t(size()))
229 if (size_t(n) >= size_t(size()))
231 return QStringView(m_data + m_size - n, n);
235 { verify(0, n);
return sliced(0, n); }
237 { verify(0, n);
return sliced(size() - n, n); }
239 { verify(pos, 0);
return QStringView(m_data + pos, size() - pos); }
241 { verify(pos, n);
return QStringView(m_data + pos, n); }
243 { verify(0, n);
return sliced(0, m_size - n); }
246 { verify(0, n); ; m_size = n; }
247 constexpr void chop(qsizetype n)
noexcept
248 { verify(0, n); m_size -= n; }
253 { *
this = sliced(pos);
return *
this; }
255 { *
this = sliced(pos, n);
return *
this; }
257 template <
typename Needle,
typename...Flags>
258 [[nodiscard]]
constexpr inline auto tokenize(Needle &&needle, Flags...flags)
const
260 ->
decltype(qTokenize(*
this,
std::forward<Needle>(needle), flags...))
261 {
return qTokenize(*
this,
std::forward<Needle>(needle), flags...); }
266 if (__builtin_constant_p(other.m_size) && other.size() == 1)
267 return compare(other.front(), cs);
269 return QtPrivate::compareStrings(*
this, other, cs);
271 [[nodiscard]]
inline int compare(QLatin1StringView other, Qt::CaseSensitivity cs =
Qt::
CaseSensitive)
const noexcept;
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
278 if (__builtin_constant_p(cs) && cs == Qt::CaseSensitive)
281 return QtPrivate::compareStrings(*
this, QStringView(&c, 1), cs);
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
300 if (__builtin_constant_p(cs) && cs == Qt::CaseSensitive)
301 return startsWith(c);
303 return QtPrivate::startsWith(*
this, QStringView(&c, 1), cs);
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
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); }
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); }
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); }
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);
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 if (from < -str.size())
538 from = qMax(from + str.size(), qsizetype(0));
539 if (from < str.size()) {
541 char16_t c = ch.unicode();
542 const char16_t *n = s + from;
543 const char16_t *e = s + str.size();
544 if (cs == Qt::CaseSensitive)
545 n = qustrchr(QStringView(n, e), c);
547 n = qustrcasechr(QStringView(n, e), c);
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.
constexpr QStringView & slice(qsizetype pos)
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
constexpr QStringView(const Char *str, qsizetype len)
constexpr const storage_type * utf16() const noexcept
char16_t storage_type
Alias for {char16_t}.
constexpr QStringView right(qsizetype n) const noexcept
qsizetype count(QStringView s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
const QChar value_type
Alias for {const QChar}.
constexpr QStringView & slice(qsizetype pos, qsizetype n)
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
constexpr QStringView last(qsizetype n) const noexcept
constexpr void truncate(qsizetype n) noexcept
Truncates this string view to length length.
constexpr QStringView sliced(qsizetype pos, qsizetype n) 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...
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 *}.
constexpr QStringView first(qsizetype n) const noexcept
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.
int compare(QStringView other, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
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...
bool endsWith(QLatin1StringView s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
qsizetype size_type
Alias for qsizetype.
constexpr auto tokenize(Needle &&needle, Flags...flags) const noexcept(noexcept(qTokenize(std::declval< const QStringView & >(), std::forward< Needle >(needle), flags...))) -> decltype(qTokenize(*this, std::forward< Needle >(needle), flags...))
pointer iterator
This typedef provides an STL-style const iterator for QStringView.
constexpr QStringView left(qsizetype n) const noexcept
qsizetype indexOf(QStringView s, qsizetype from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
constexpr QStringView chopped(qsizetype n) const noexcept
Returns the substring of length size() - length starting at the beginning of this object.
bool isUpper() const noexcept
qsizetype toWCharArray(wchar_t *array) const
constexpr QStringView mid(qsizetype pos, qsizetype n=-1) const noexcept
Returns the substring of length length starting at position start in this object.
QStringView trimmed() const noexcept
Strips leading and trailing whitespace and returns the result.
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
constexpr void chop(qsizetype n) noexcept
Truncates this string view by length characters.
constexpr QChar at(qsizetype n) const noexcept
Returns the character at position n in this string view.
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
constexpr QStringView sliced(qsizetype pos) const noexcept
int compare(QLatin1StringView other, 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)