7#include <QtCore/qchar.h>
8#include <QtCore/qcompare.h>
9#include <QtCore/qcontainerfwd.h>
10#include <QtCore/qbytearray.h>
11#include <QtCore/qstringliteral.h>
12#include <QtCore/qstringalgorithms.h>
16#include <QtCore/q20type_traits.h>
18#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
19Q_FORWARD_DECLARE_CF_TYPE(CFString);
20Q_FORWARD_DECLARE_OBJC_CLASS(NSString);
27class QRegularExpression;
28class QRegularExpressionMatch;
34template <
typename Char>
41template <
typename Char>
45template <
typename Pointer>
47template <
typename Char>
50template <
typename Pointer>
54template <
typename T,
typename Enable =
void>
96 template <
typename Char>
99 template <
typename Pointer>
102 template <
typename T>
103 using if_compatible_qstring_like =
typename std::enable_if<
std::is_same<T, QString>::value,
bool>::type;
105 template <
typename T>
108 template <
typename Char>
109 static constexpr qsizetype lengthHelperPointer(
const Char *str)
noexcept
111 if (q20::is_constant_evaluated())
112 return QtPrivate::lengthHelperPointer(str);
113 return QtPrivate::qustrlen(
reinterpret_cast<
const char16_t *>(str));
115 static qsizetype lengthHelperPointer(
const QChar *str)
noexcept
117 return QtPrivate::qustrlen(
reinterpret_cast<
const char16_t *>(str));
120 template <
typename Char>
121 static const storage_type *castHelper(
const Char *str)
noexcept
131 template <
typename Char, if_compatible_char<Char> =
true>
133#if QT_VERSION >= QT_VERSION_CHECK(7
, 0
, 0
) || defined(QT_BOOTSTRAPPED)
138 m_data(castHelper(str))
142 template <
typename Char, if_compatible_char<Char> =
true>
150 template <
typename Char>
154 template <
typename Pointer, if_compatible_pointer<Pointer> =
true>
156 :
QStringView(str, str ? lengthHelperPointer(str) : 0) {}
162 template <
typename String, if_compatible_qstring_like<String> =
true>
171 template <
typename Char, size_t Size, if_compatible_char<Char> =
true>
176#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
188 { verify(n, 1);
return QChar(m_data[n]); }
194 template <
typename...Args>
195 [[nodiscard]]
inline QString
arg(Args &&...args)
const;
207 auto result = QContainerImplHelper::mid(size(), &pos, &n);
208 return result == QContainerImplHelper::Null ? QStringView() : QStringView(m_data + pos, n);
212 if (size_t(n) >= size_t(size()))
218 if (size_t(n) >= size_t(size()))
220 return QStringView(m_data + m_size - n, n);
224 { verify(0, n);
return sliced(0, n); }
226 { verify(0, n);
return sliced(size() - n, n); }
228 { verify(pos, 0);
return QStringView(m_data + pos, size() - pos); }
230 { verify(pos, n);
return QStringView(m_data + pos, n); }
232 { verify(0, n);
return sliced(0, m_size - n); }
235 { verify(0, n); ; m_size = n; }
236 constexpr void chop(qsizetype n)
noexcept
237 { verify(0, n); m_size -= n; }
242 { *
this = sliced(pos);
return *
this; }
244 { *
this = sliced(pos, n);
return *
this; }
246 template <
typename Needle,
typename...Flags>
247 [[nodiscard]]
constexpr inline auto tokenize(Needle &&needle, Flags...flags)
const
249 ->
decltype(qTokenize(*
this,
std::forward<Needle>(needle), flags...))
250 {
return qTokenize(*
this,
std::forward<Needle>(needle), flags...); }
253 {
return QtPrivate::compareStrings(*
this, other, cs); }
257 {
return size() >= 1 ? compare_single_char_helper(*utf16() - c.unicode()) : -1; }
259 {
return QtPrivate::compareStrings(*
this, QStringView(&c, 1), cs); }
264 {
return QtPrivate::startsWith(*
this, s, cs); }
267 {
return !empty() && front() == c; }
269 {
return QtPrivate::startsWith(*
this, QStringView(&c, 1), cs); }
272 {
return QtPrivate::endsWith(*
this, s, cs); }
275 {
return !empty() && back() == c; }
277 {
return QtPrivate::endsWith(*
this, QStringView(&c, 1), cs); }
280 {
return QtPrivate::findString(*
this, from, c.unicode(), cs); }
282 {
return QtPrivate::findString(*
this, from, s, cs); }
286 {
return indexOf(QStringView(&c, 1), 0, cs) != qsizetype(-1); }
288 {
return indexOf(s, 0, cs) != qsizetype(-1); }
292 {
return QtPrivate::count(*
this, c, cs); }
294 {
return QtPrivate::count(*
this, s, cs); }
298 {
return lastIndexOf(c, -1, cs); }
300 {
return QtPrivate::lastIndexOf(*
this, from, c.unicode(), cs); }
302 {
return lastIndexOf(s, size(), cs); }
304 {
return QtPrivate::lastIndexOf(*
this, from, s, cs); }
308#if QT_CONFIG(regularexpression)
338 {
return QtPrivate::isRightToLeft(*
this); }
340 {
return QtPrivate::isValidUtf16(*
this); }
343 {
return QtPrivate::isUpper(*
this); }
345 {
return QtPrivate::isLower(*
this); }
347 [[
nodiscard]]
inline short toShort(
bool *ok =
nullptr,
int base = 10)
const;
349 [[
nodiscard]]
inline int toInt(
bool *ok =
nullptr,
int base = 10)
const;
350 [[
nodiscard]]
inline uint
toUInt(
bool *ok =
nullptr,
int base = 10)
const;
351 [[
nodiscard]]
inline long toLong(
bool *ok =
nullptr,
int base = 10)
const;
352 [[
nodiscard]]
inline ulong
toULong(
bool *ok =
nullptr,
int base = 10)
const;
369#if QT_CONFIG(regularexpression)
377 {
return lhs.size() == rhs.size() && QtPrivate::equalStrings(lhs, rhs); }
381 const int res = QtPrivate::compareStrings(lhs, rhs);
382 return Qt::compareThreeWay(res, 0);
432#if QT_VERSION >= QT_VERSION_CHECK(7
, 0
, 0
) || defined(QT_BOOTSTRAPPED)
440 Q_ALWAYS_INLINE
constexpr void verify([[maybe_unused]] qsizetype pos = 0,
441 [[maybe_unused]] qsizetype n = 1)
const
444 Q_ASSERT(pos <= size());
446 Q_ASSERT(n <= size() - pos);
449 constexpr int compare_single_char_helper(
int diff)
const noexcept
450 {
return diff ? diff : size() > 1 ? 1 : 0; }
452 Q_CORE_EXPORT
static bool equal_helper(
QStringView sv,
const char *data, qsizetype len);
453 Q_CORE_EXPORT
static int compare_helper(
QStringView sv,
const char *data, qsizetype len);
455#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
457 {
return equal_helper(lhs, rhs.data(), rhs.size()); }
461 const int res = compare_helper(lhs, rhs.data(), rhs.size());
462 return Qt::compareThreeWay(res, 0);
464 Q_DECLARE_STRONGLY_ORDERED(
QStringView, QByteArrayView, QT_ASCII_CAST_WARN)
471template <
typename QStringLike,
typename std::enable_if<
472 std::is_same<QStringLike, QString>::value,
479[[nodiscard]]
constexpr auto QChar::fromUcs4(
char32_t c)
noexcept
483 [[nodiscard]]
constexpr operator QStringView()
const noexcept {
return {begin(), end()}; }
484 [[nodiscard]]
constexpr qsizetype size()
const noexcept {
return chars[1] ? 2 : 1; }
485 [[nodiscard]]
constexpr const char16_t *begin()
const noexcept {
return chars; }
486 [[nodiscard]]
constexpr const char16_t *end()
const noexcept {
return begin() + size(); }
488 return requiresSurrogates(c) ? R{{QChar::highSurrogate(c),
489 QChar::lowSurrogate(c)}} :
490 R{{
char16_t(c), u'\0'}} ;
495 if (from < -str.size())
498 from = qMax(from + str.size(), qsizetype(0));
499 if (from < str.size()) {
501 char16_t c = ch.unicode();
502 const char16_t *n = s + from;
503 const char16_t *e = s + str.size();
504 if (cs == Qt::CaseSensitive)
505 n = qustrchr(QStringView(n, e), c);
507 n = qustrcasechr(QStringView(n, e), c);
QString arg(Args &&...args) const
bool isLower() const noexcept
bool startsWith(QChar c) const noexcept
const QChar value_type
Alias for {const QChar}.
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
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
qsizetype size_type
Alias for qsizetype.
value_type * const_pointer
Alias for {value_type *}.
value_type & const_reference
Alias for {value_type &}.
constexpr QStringView right(qsizetype n) const noexcept
qsizetype count(QStringView s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
constexpr QStringView & slice(qsizetype pos, qsizetype n)
value_type & reference
Alias for {value_type &}.
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
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
constexpr QStringView first(qsizetype n) const noexcept
pointer iterator
This typedef provides an STL-style const iterator for QStringView.
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).
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
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...))
const_pointer const_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.
std::reverse_iterator< const_iterator > const_reverse_iterator
This typedef provides an STL-style const reverse iterator for QStringView.
bool startsWith(QLatin1StringView s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
std::ptrdiff_t difference_type
Alias for {std::ptrdiff_t}.
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...
friend Qt::strong_ordering compareThreeWay(const QStringView &lhs, const QStringView &rhs) noexcept
constexpr QStringView(const Pointer &str) noexcept
char16_t storage_type
Alias for {char16_t}.
Combined button and popup list for selecting options.
QStringView qToStringViewIgnoringNull(const QStringLike &s) noexcept
Q_DECLARE_TYPEINFO(QStringView, Q_PRIMITIVE_TYPE)