7#include <QtCore/qchar.h>
8#include <QtCore/qcompare.h>
9#include <QtCore/qcontainerfwd.h>
10#include <QtCore/qbytearray.h>
11#include <QtCore/qstringfwd.h>
12#include <QtCore/qstringliteral.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)
189 { verify(n, 1);
return QChar(m_data[n]); }
195 template <
typename...Args>
208 auto result = QContainerImplHelper::mid(size(), &pos, &n);
209 return result == QContainerImplHelper::Null ? QStringView() : QStringView(m_data + pos, n);
213 if (size_t(n) >= size_t(size()))
219 if (size_t(n) >= size_t(size()))
221 return QStringView(m_data + m_size - n, n);
225 { verify(0, n);
return sliced(0, n); }
227 { verify(0, n);
return sliced(size() - n, n); }
229 { verify(pos, 0);
return QStringView(m_data + pos, size() - pos); }
231 { verify(pos, n);
return QStringView(m_data + pos, n); }
233 { verify(0, n);
return sliced(0, m_size - n); }
236 { verify(0, n); ; m_size = n; }
237 constexpr void chop(qsizetype n)
noexcept
238 { verify(0, n); m_size -= n; }
243 { *
this = sliced(pos);
return *
this; }
245 { *
this = sliced(pos, n);
return *
this; }
247 template <
typename Needle,
typename...Flags>
248 [[nodiscard]]
constexpr inline auto tokenize(Needle &&needle, Flags...flags)
const
250 ->
decltype(qTokenize(*
this,
std::forward<Needle>(needle), flags...))
251 {
return qTokenize(*
this,
std::forward<Needle>(needle), flags...); }
254 {
return QtPrivate::compareStrings(*
this, other, cs); }
258 {
return size() >= 1 ? compare_single_char_helper(*utf16() - c.unicode()) : -1; }
260 {
return QtPrivate::compareStrings(*
this, QStringView(&c, 1), cs); }
265 {
return QtPrivate::startsWith(*
this, s, cs); }
268 {
return !empty() && front() == c; }
270 {
return QtPrivate::startsWith(*
this, QStringView(&c, 1), cs); }
273 {
return QtPrivate::endsWith(*
this, s, cs); }
276 {
return !empty() && back() == c; }
278 {
return QtPrivate::endsWith(*
this, QStringView(&c, 1), cs); }
281 {
return QtPrivate::findString(*
this, from, c.unicode(), cs); }
283 {
return QtPrivate::findString(*
this, from, s, cs); }
287 {
return indexOf(QStringView(&c, 1), 0, cs) != qsizetype(-1); }
289 {
return indexOf(s, 0, cs) != qsizetype(-1); }
293 {
return QtPrivate::count(*
this, c, cs); }
295 {
return QtPrivate::count(*
this, s, cs); }
299 {
return lastIndexOf(c, -1, cs); }
301 {
return QtPrivate::lastIndexOf(*
this, from, c.unicode(), cs); }
303 {
return lastIndexOf(s, size(), cs); }
305 {
return QtPrivate::lastIndexOf(*
this, from, s, cs); }
309#if QT_CONFIG(regularexpression)
339 {
return QtPrivate::isRightToLeft(*
this); }
341 {
return QtPrivate::isValidUtf16(*
this); }
344 {
return QtPrivate::isUpper(*
this); }
346 {
return QtPrivate::isLower(*
this); }
348 [[
nodiscard]]
inline short toShort(
bool *ok =
nullptr,
int base = 10)
const;
350 [[
nodiscard]]
inline int toInt(
bool *ok =
nullptr,
int base = 10)
const;
351 [[
nodiscard]]
inline uint
toUInt(
bool *ok =
nullptr,
int base = 10)
const;
352 [[
nodiscard]]
inline long toLong(
bool *ok =
nullptr,
int base = 10)
const;
353 [[
nodiscard]]
inline ulong
toULong(
bool *ok =
nullptr,
int base = 10)
const;
370#if QT_CONFIG(regularexpression)
378 {
return lhs.size() == rhs.size() && QtPrivate::equalStrings(lhs, rhs); }
382 const int res = QtPrivate::compareStrings(lhs, rhs);
383 return Qt::compareThreeWay(res, 0);
433#if QT_VERSION >= QT_VERSION_CHECK(7
, 0
, 0
) || defined(QT_BOOTSTRAPPED)
441 Q_ALWAYS_INLINE
constexpr void verify([[maybe_unused]] qsizetype pos = 0,
442 [[maybe_unused]] qsizetype n = 1)
const
445 Q_ASSERT(pos <= size());
447 Q_ASSERT(n <= size() - pos);
450 constexpr int compare_single_char_helper(
int diff)
const noexcept
451 {
return diff ? diff : size() > 1 ? 1 : 0; }
453 Q_CORE_EXPORT
static bool equal_helper(
QStringView sv,
const char *data, qsizetype len);
454 Q_CORE_EXPORT
static int compare_helper(
QStringView sv,
const char *data, qsizetype len);
456#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
458 {
return equal_helper(lhs, rhs.data(), rhs.size()); }
462 const int res = compare_helper(lhs, rhs.data(), rhs.size());
463 return Qt::compareThreeWay(res, 0);
465 Q_DECLARE_STRONGLY_ORDERED(
QStringView, QByteArrayView, QT_ASCII_CAST_WARN)
472template <
typename QStringLike,
typename std::enable_if<
473 std::is_same<QStringLike, QString>::value,
480[[nodiscard]]
constexpr auto QChar::fromUcs4(
char32_t c)
noexcept
484 [[nodiscard]]
constexpr operator QStringView()
const noexcept {
return {begin(), end()}; }
485 [[nodiscard]]
constexpr qsizetype size()
const noexcept {
return chars[1] ? 2 : 1; }
486 [[nodiscard]]
constexpr const char16_t *begin()
const noexcept {
return chars; }
487 [[nodiscard]]
constexpr const char16_t *end()
const noexcept {
return begin() + size(); }
489 return requiresSurrogates(c) ? R{{QChar::highSurrogate(c),
490 QChar::lowSurrogate(c)}} :
491 R{{
char16_t(c), u'\0'}} ;
496 if (from < -str.size())
499 from = qMax(from + str.size(), qsizetype(0));
500 if (from < str.size()) {
502 char16_t c = ch.unicode();
503 const char16_t *n = s + from;
504 const char16_t *e = s + str.size();
505 if (cs == Qt::CaseSensitive)
506 n = qustrchr(QStringView(n, e), c);
508 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}.
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, including any trailing {Char(0)...
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)