7#ifndef QLATIN1STRINGVIEW_H
8#define QLATIN1STRINGVIEW_H
10#include <QtCore/qchar.h>
11#include <QtCore/qcompare.h>
12#include <QtCore/qcontainerfwd.h>
13#include <QtCore/qnamespace.h>
14#include <QtCore/qtversionchecks.h>
15#include <QtCore/qstringview.h>
19#pragma qt_class(QLatin1String)
20#pragma qt_class(QLatin1StringView)
27#if QT_VERSION >= QT_VERSION_CHECK(7
, 0
, 0
) || defined(QT_BOOTSTRAPPED) || defined(Q_QDOC)
28# define Q_L1S_VIEW_IS_PRIMARY
29class QLatin1StringView
35#ifdef Q_L1S_VIEW_IS_PRIMARY
36 constexpr QLatin1StringView()
noexcept {}
37 constexpr QLatin1StringView(std::nullptr_t)
noexcept : QLatin1StringView() {}
38 constexpr explicit QLatin1StringView(
const char *s)
noexcept
39 : QLatin1StringView(s, s ? qsizetype(QtPrivate::lengthHelperPointer(s)) : 0) {}
40 constexpr QLatin1StringView(
const char *f,
const char *l)
41 : QLatin1StringView(f, qsizetype(l - f)) {}
42 constexpr QLatin1StringView(
const char *s, qsizetype sz)
noexcept : m_data(s), m_size(sz) {}
43 explicit QLatin1StringView(
const QByteArray &s)
noexcept
44 : QLatin1StringView{s.begin(), s.size()} {}
45 constexpr explicit QLatin1StringView(QByteArrayView s)
noexcept
46 : QLatin1StringView(s.constData(), s.size()) {}
48 constexpr QLatin1String()
noexcept : m_size(0), m_data(
nullptr) {}
50 constexpr QLatin1String(std::nullptr_t)
noexcept : QLatin1String() {}
51 constexpr explicit QLatin1String(
const char *s)
noexcept
52 : m_size(s ? qsizetype(QtPrivate::lengthHelperPointer(s)) : 0), m_data(s) {}
53 constexpr QLatin1String(
const char *f,
const char *l)
54 : QLatin1String(f, qsizetype(l - f)) {}
55 constexpr QLatin1String(
const char *s, qsizetype sz)
noexcept : m_size(sz), m_data(s) {}
56 explicit QLatin1String(
const QByteArray &s)
noexcept : QLatin1String(s.begin(), s.size()) {}
57 constexpr explicit QLatin1String(QByteArrayView s)
noexcept : m_size(s.size()), m_data(s.data()) {}
63 constexpr const char *
latin1()
const noexcept {
return m_data; }
65 constexpr const char *
data()
const noexcept {
return m_data; }
68 [[nodiscard]]
constexpr const char *
constEnd()
const noexcept {
return end(); }
76 constexpr bool isEmpty()
const noexcept {
return !size(); }
78 [[nodiscard]]
constexpr bool empty()
const noexcept {
return size() == 0; }
80 template <
typename...Args>
81 [[nodiscard]]
inline QString
arg(Args &&...args)
const;
87 return QLatin1Char(m_data[i]);
95 {
return QtPrivate::compareStrings(*
this, other, cs); }
97 {
return QtPrivate::compareStrings(*
this, other, cs); }
98 [[nodiscard]]
inline int compare(QUtf8StringView other, Qt::CaseSensitivity cs =
Qt::
CaseSensitive)
const noexcept;
99 [[nodiscard]]
constexpr int compare(QChar c)
const noexcept
100 {
return isEmpty() ? -1 : front() == c ?
int(size() > 1) : uchar(m_data[0]) - c.unicode(); }
101 [[nodiscard]]
int compare(QChar c, Qt::CaseSensitivity cs)
const noexcept
102 {
return QtPrivate::compareStrings(*
this, QStringView(&c, 1), cs); }
105 {
return QtPrivate::startsWith(*
this, s, cs); }
107 {
return QtPrivate::startsWith(*
this, s, cs); }
108 [[nodiscard]]
constexpr bool startsWith(QChar c)
const noexcept
109 {
return !isEmpty() && front() == c; }
110 [[nodiscard]]
bool startsWith(QChar c, Qt::CaseSensitivity cs)
const noexcept
111 {
return QtPrivate::startsWith(*
this, QStringView(&c, 1), cs); }
114 {
return QtPrivate::endsWith(*
this, s, cs); }
116 {
return QtPrivate::endsWith(*
this, s, cs); }
117 [[nodiscard]]
constexpr bool endsWith(QChar c)
const noexcept
118 {
return !isEmpty() && back() == c; }
119 [[nodiscard]]
bool endsWith(QChar c, Qt::CaseSensitivity cs)
const noexcept
120 {
return QtPrivate::endsWith(*
this, QStringView(&c, 1), cs); }
123 {
return QtPrivate::findString(*
this, from, s, cs); }
125 {
return QtPrivate::findString(*
this, from, s, cs); }
127 {
return QtPrivate::findString(*
this, from, QStringView(&c, 1), cs); }
130 {
return indexOf(s, 0, cs) != -1; }
132 {
return indexOf(s, 0, cs) != -1; }
134 {
return indexOf(QStringView(&c, 1), 0, cs) != -1; }
137 {
return lastIndexOf(s, size(), cs); }
139 {
return QtPrivate::lastIndexOf(*
this, from, s, cs); }
141 {
return lastIndexOf(s, size(), cs); }
143 {
return QtPrivate::lastIndexOf(*
this, from, s, cs); }
145 {
return lastIndexOf(c, -1, cs); }
147 {
return QtPrivate::lastIndexOf(*
this, from, QStringView(&c, 1), cs); }
150 {
return QtPrivate::count(*
this, str, cs); }
152 {
return QtPrivate::count(*
this, str, cs); }
154 {
return QtPrivate::count(*
this, ch, cs); }
156 [[nodiscard]]
short toShort(
bool *ok =
nullptr,
int base = 10)
const
157 {
return QtPrivate::toIntegral<
short>(QByteArrayView(*
this), ok, base); }
159 {
return QtPrivate::toIntegral<ushort>(QByteArrayView(*
this), ok, base); }
160 [[nodiscard]]
int toInt(
bool *ok =
nullptr,
int base = 10)
const
161 {
return QtPrivate::toIntegral<
int>(QByteArrayView(*
this), ok, base); }
162 [[nodiscard]]
uint toUInt(
bool *ok =
nullptr,
int base = 10)
const
163 {
return QtPrivate::toIntegral<uint>(QByteArrayView(*
this), ok, base); }
164 [[nodiscard]]
long toLong(
bool *ok =
nullptr,
int base = 10)
const
165 {
return QtPrivate::toIntegral<
long>(QByteArrayView(*
this), ok, base); }
167 {
return QtPrivate::toIntegral<ulong>(QByteArrayView(*
this), ok, base); }
169 {
return QtPrivate::toIntegral<qlonglong>(QByteArrayView(*
this), ok, base); }
171 {
return QtPrivate::toIntegral<qulonglong>(QByteArrayView(*
this), ok, base); }
172 [[nodiscard]]
float toFloat(
bool *ok =
nullptr)
const
174 const auto r = QtPrivate::toFloat(*
this);
177 return r.value_or(0.0f);
179 [[nodiscard]]
double toDouble(
bool *ok =
nullptr)
const
181 const auto r = QtPrivate::toDouble(*
this);
184 return r.value_or(0.0);
187 using value_type =
const char;
188 using pointer = value_type*;
189 using const_pointer = pointer;
190 using reference = value_type&;
191 using const_reference = reference;
192 using iterator = value_type*;
193 using const_iterator = iterator;
194 using difference_type = qsizetype;
195 using size_type = qsizetype;
197 constexpr const_iterator
begin()
const noexcept {
return data(); }
199 constexpr const_iterator
end()
const noexcept {
return data() + size(); }
200 constexpr const_iterator
cend()
const noexcept {
return data() + size(); }
202 using reverse_iterator = std::reverse_iterator<iterator>;
203 using const_reverse_iterator = reverse_iterator;
215 return QtPrivate::MaxAllocSize - 1;
220 using namespace QtPrivate;
221 auto result = QContainerImplHelper::mid(size(), &pos, &n);
222 return result == QContainerImplHelper::Null ? QLatin1StringView()
223 : QLatin1StringView(m_data + pos, n);
227 if (size_t(n) >= size_t(size()))
233 if (size_t(n) >= size_t(size()))
235 return {m_data + m_size - n, n};
239 { verify(pos, 0);
return {m_data + pos, m_size - pos}; }
241 { verify(pos, n);
return {m_data + pos, n}; }
243 { verify(0, n);
return sliced(0, n); }
245 { verify(0, n);
return sliced(size() - n, n); }
247 { verify(0, n);
return sliced(0, size() - n); }
250 { *
this = sliced(pos);
return *
this; }
252 { *
this = sliced(pos, n);
return *
this; }
254 constexpr void chop(qsizetype n)
255 { verify(0, n); m_size -= n; }
257 { verify(0, n); m_size = n; }
261 template <
typename Needle,
typename...Flags>
262 [[nodiscard]]
constexpr auto tokenize(Needle &&needle, Flags...flags)
const
266 {
return qTokenize(*
this, std::forward<Needle>(needle), flags...); }
268 friend bool comparesEqual(
const QLatin1StringView &s1,
const QLatin1StringView &s2)
noexcept
269 {
return s1.size() == s2.size() && QtPrivate::equalStrings(s1, s2); }
273 const int res = QtPrivate::compareStrings(s1, s2);
274 return Qt::compareThreeWay(res, 0);
276 Q_DECLARE_STRONGLY_ORDERED(QLatin1StringView)
279 friend bool comparesEqual(
const QLatin1StringView &lhs, QChar rhs)
noexcept
280 {
return lhs.size() == 1 && rhs == lhs.front(); }
281 friend Qt::strong_ordering
282 compareThreeWay(
const QLatin1StringView &lhs, QChar rhs)
noexcept
285 const int res = -compare_helper(&rhs, 1, lhs);
286 return Qt::compareThreeWay(res, 0);
288 Q_DECLARE_STRONGLY_ORDERED(QLatin1StringView, QChar)
291 friend bool comparesEqual(
const QLatin1StringView &lhs,
const QStringView &rhs)
noexcept
292 {
return lhs.size() == rhs.size() && QtPrivate::equalStrings(lhs, rhs); }
293 friend Qt::strong_ordering
294 compareThreeWay(
const QLatin1StringView &lhs,
const QStringView &rhs)
noexcept
296 const int res = QtPrivate::compareStrings(lhs, rhs);
297 return Qt::compareThreeWay(res, 0);
299 Q_DECLARE_STRONGLY_ORDERED(QLatin1StringView, QStringView)
305 friend bool comparesEqual(
const QStringView &lhs,
const QLatin1StringView &rhs)
noexcept
306 {
return comparesEqual(rhs, lhs); }
307 friend Qt::strong_ordering
308 compareThreeWay(
const QStringView &lhs,
const QLatin1StringView &rhs)
noexcept
309 {
return QtOrderingPrivate::reversed(compareThreeWay(rhs, lhs)); }
312 friend bool comparesEqual(
const QLatin1StringView &lhs,
const QByteArrayView &rhs)
noexcept
313 {
return equal_helper(lhs, rhs.data(), rhs.size()); }
314 friend Qt::strong_ordering
315 compareThreeWay(
const QLatin1StringView &lhs,
const QByteArrayView &rhs)
noexcept
317 const int res = compare_helper(lhs, rhs.data(), rhs.size());
318 return Qt::compareThreeWay(res, 0);
324 friend bool comparesEqual(
const QByteArrayView &lhs,
const QLatin1StringView &rhs)
noexcept
325 {
return comparesEqual(rhs, lhs); }
326 friend Qt::strong_ordering
327 compareThreeWay(
const QByteArrayView &lhs,
const QLatin1StringView &rhs)
noexcept
328 {
return QtOrderingPrivate::reversed(compareThreeWay(rhs, lhs)); }
331#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
332 Q_DECLARE_STRONGLY_ORDERED(QLatin1StringView, QByteArrayView, QT_ASCII_CAST_WARN)
333 Q_DECLARE_STRONGLY_ORDERED(QLatin1StringView, QByteArray, QT_ASCII_CAST_WARN)
334 Q_DECLARE_STRONGLY_ORDERED(QLatin1StringView,
const char *, QT_ASCII_CAST_WARN)
338 Q_ALWAYS_INLINE
constexpr void verify([[maybe_unused]] qsizetype pos,
339 [[maybe_unused]] qsizetype n = 1)
const
342 Q_ASSERT(pos <= size());
344 Q_ASSERT(n <= size() - pos);
346 static int compare_helper(
const QLatin1StringView &s1,
const char *s2)
noexcept
347 {
return compare_helper(s1, s2, qstrlen(s2)); }
348 Q_CORE_EXPORT
static bool equal_helper(QLatin1StringView s1,
const char *s2, qsizetype len)
noexcept;
349 Q_CORE_EXPORT
static int compare_helper(
const QLatin1StringView &s1,
const char *s2, qsizetype len)
noexcept;
350 Q_CORE_EXPORT
static int compare_helper(
const QChar *data1, qsizetype length1,
351 QLatin1StringView s2,
352 Qt::CaseSensitivity cs = Qt::CaseSensitive)
noexcept;
353#if QT_VERSION >= QT_VERSION_CHECK(7
, 0
, 0
) || defined(QT_BOOTSTRAPPED)
354 const char *m_data =
nullptr;
355 qsizetype m_size = 0;
361#ifdef Q_L1S_VIEW_IS_PRIMARY
362Q_DECLARE_TYPEINFO(QLatin1StringView, Q_RELOCATABLE_TYPE);
364Q_DECLARE_TYPEINFO(QLatin1String, Q_RELOCATABLE_TYPE);
373 return {str, qsizetype(size)};
382#ifdef Q_L1S_VIEW_IS_PRIMARY
383# undef Q_L1S_VIEW_IS_PRIMARY
constexpr int compare(QChar c) const noexcept
constexpr QLatin1Char back() const
static constexpr qsizetype maxSize() noexcept
constexpr const char * data() const noexcept
constexpr const char * constBegin() const noexcept
QByteArray toUtf8() const
ulong toULong(bool *ok=nullptr, int base=10) const
const_reverse_iterator rend() const noexcept
constexpr bool isEmpty() const noexcept
short toShort(bool *ok=nullptr, int base=10) const
constexpr qsizetype length() const noexcept
constexpr const char * latin1() const noexcept
constexpr QLatin1StringView last(qsizetype n) const
constexpr const_iterator cbegin() const noexcept
constexpr bool endsWith(QChar c) const noexcept
constexpr const char * constData() const noexcept
const_reverse_iterator crend() const noexcept
constexpr QLatin1StringView mid(qsizetype pos, qsizetype n=-1) const
constexpr bool isNull() const noexcept
constexpr QLatin1Char front() const
constexpr QLatin1StringView left(qsizetype n) const
qsizetype count(QStringView str, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
int compare(QStringView other, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
constexpr const_iterator end() const noexcept
const_reverse_iterator crbegin() const noexcept
QLatin1StringView trimmed() const noexcept
int toInt(bool *ok=nullptr, int base=10) const
constexpr void chop(qsizetype n)
qsizetype lastIndexOf(QStringView s, qsizetype from, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
constexpr QLatin1Char last() const
constexpr QLatin1StringView right(qsizetype n) const
constexpr QLatin1Char first() const
constexpr bool empty() const noexcept
qlonglong toLongLong(bool *ok=nullptr, int base=10) const
long toLong(bool *ok=nullptr, int base=10) const
constexpr const_iterator cend() const noexcept
float toFloat(bool *ok=nullptr) const
constexpr QLatin1StringView & slice(qsizetype pos, qsizetype n)
constexpr QLatin1StringView sliced(qsizetype pos) const
constexpr QLatin1Char at(qsizetype i) const
bool contains(QStringView s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
constexpr QLatin1StringView & slice(qsizetype pos)
qsizetype lastIndexOf(QStringView s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
constexpr QLatin1StringView chopped(qsizetype n) const
constexpr QLatin1StringView sliced(qsizetype pos, qsizetype n) const
constexpr auto tokenize(Needle &&needle, Flags...flags) const noexcept(noexcept(qTokenize(std::declval< const QLatin1StringView & >(), std::forward< Needle >(needle), flags...))) -> decltype(qTokenize(*this, std::forward< Needle >(needle), flags...))
bool endsWith(QStringView s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
constexpr QLatin1Char operator[](qsizetype i) const
constexpr const_iterator begin() const noexcept
constexpr QLatin1StringView first(qsizetype n) const
const_reverse_iterator rbegin() const noexcept
uint toUInt(bool *ok=nullptr, int base=10) const
constexpr qsizetype size() const noexcept
constexpr qsizetype max_size() const noexcept
qsizetype indexOf(QStringView s, qsizetype from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
constexpr bool startsWith(QChar c) const noexcept
constexpr const char * constEnd() const noexcept
double toDouble(bool *ok=nullptr) const
bool startsWith(QStringView s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
constexpr void truncate(qsizetype n)
QString arg(Args &&...args) const
qulonglong toULongLong(bool *ok=nullptr, int base=10) const
ushort toUShort(bool *ok=nullptr, int base=10) const
constexpr QLatin1StringView operator""_L1(const char *str, size_t size) noexcept
bool comparesEqual(const QFileInfo &lhs, const QFileInfo &rhs)
Qt::strong_ordering compareThreeWay(const QByteArrayView &lhs, const QChar &rhs) noexcept