File indexing completed on 2026-01-06 10:21:20
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef QLATIN1STRINGVIEW_H
0009 #define QLATIN1STRINGVIEW_H
0010
0011 #include <QtCore/qchar.h>
0012 #include <QtCore/qcompare.h>
0013 #include <QtCore/qcontainerfwd.h>
0014 #include <QtCore/qnamespace.h>
0015 #include <QtCore/qtversionchecks.h>
0016 #include <QtCore/qstringfwd.h>
0017 #include <QtCore/qstringview.h>
0018
0019 #if 0
0020
0021 #pragma qt_class(QLatin1String)
0022 #pragma qt_class(QLatin1StringView)
0023 #endif
0024
0025 QT_BEGIN_NAMESPACE
0026
0027 class QString;
0028
0029 #ifdef Q_L1S_VIEW_IS_PRIMARY
0030 class QLatin1StringView
0031 #else
0032 class QLatin1String
0033 #endif
0034 {
0035 public:
0036 #ifdef Q_L1S_VIEW_IS_PRIMARY
0037 constexpr QLatin1StringView() noexcept {}
0038 constexpr QLatin1StringView(std::nullptr_t) noexcept : QLatin1StringView() {}
0039 constexpr explicit QLatin1StringView(const char *s) noexcept
0040 : QLatin1StringView(s, s ? qsizetype(QtPrivate::lengthHelperPointer(s)) : 0) {}
0041 constexpr QLatin1StringView(const char *f, const char *l)
0042 : QLatin1StringView(f, qsizetype(l - f)) {}
0043 constexpr QLatin1StringView(const char *s, qsizetype sz) noexcept : m_data(s), m_size(sz) {}
0044 explicit QLatin1StringView(const QByteArray &s) noexcept
0045 : QLatin1StringView{s.begin(), s.size()} {}
0046 constexpr explicit QLatin1StringView(QByteArrayView s) noexcept
0047 : QLatin1StringView(s.constData(), s.size()) {}
0048 #else
0049 constexpr QLatin1String() noexcept : m_size(0), m_data(nullptr) {}
0050 Q_WEAK_OVERLOAD
0051 constexpr QLatin1String(std::nullptr_t) noexcept : QLatin1String() {}
0052 constexpr explicit QLatin1String(const char *s) noexcept
0053 : m_size(s ? qsizetype(QtPrivate::lengthHelperPointer(s)) : 0), m_data(s) {}
0054 constexpr QLatin1String(const char *f, const char *l)
0055 : QLatin1String(f, qsizetype(l - f)) {}
0056 constexpr QLatin1String(const char *s, qsizetype sz) noexcept : m_size(sz), m_data(s) {}
0057 explicit QLatin1String(const QByteArray &s) noexcept : QLatin1String(s.begin(), s.size()) {}
0058 constexpr explicit QLatin1String(QByteArrayView s) noexcept : m_size(s.size()), m_data(s.data()) {}
0059 #endif
0060
0061 inline QString toString() const;
0062 QByteArray toUtf8() const { return QtPrivate::convertToUtf8(*this); }
0063
0064 constexpr const char *latin1() const noexcept { return m_data; }
0065 constexpr qsizetype size() const noexcept { return m_size; }
0066 constexpr const char *data() const noexcept { return m_data; }
0067 [[nodiscard]] constexpr const char *constData() const noexcept { return data(); }
0068 [[nodiscard]] constexpr const char *constBegin() const noexcept { return begin(); }
0069 [[nodiscard]] constexpr const char *constEnd() const noexcept { return end(); }
0070
0071 [[nodiscard]] constexpr QLatin1Char first() const { return front(); }
0072 [[nodiscard]] constexpr QLatin1Char last() const { return back(); }
0073
0074 [[nodiscard]] constexpr qsizetype length() const noexcept { return size(); }
0075
0076 constexpr bool isNull() const noexcept { return !data(); }
0077 constexpr bool isEmpty() const noexcept { return !size(); }
0078
0079 [[nodiscard]] constexpr bool empty() const noexcept { return size() == 0; }
0080
0081 template <typename...Args>
0082 [[nodiscard]] inline QString arg(Args &&...args) const;
0083
0084 [[nodiscard]] constexpr QLatin1Char at(qsizetype i) const
0085 {
0086 Q_ASSERT(i >= 0);
0087 Q_ASSERT(i < size());
0088 return QLatin1Char(m_data[i]);
0089 }
0090 [[nodiscard]] constexpr QLatin1Char operator[](qsizetype i) const { return at(i); }
0091
0092 [[nodiscard]] constexpr QLatin1Char front() const { return at(0); }
0093 [[nodiscard]] constexpr QLatin1Char back() const { return at(size() - 1); }
0094
0095 [[nodiscard]] int compare(QStringView other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0096 { return QtPrivate::compareStrings(*this, other, cs); }
0097 [[nodiscard]] int compare(QLatin1StringView other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0098 { return QtPrivate::compareStrings(*this, other, cs); }
0099 [[nodiscard]] inline int compare(QUtf8StringView other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
0100 [[nodiscard]] constexpr int compare(QChar c) const noexcept
0101 { return isEmpty() ? -1 : front() == c ? int(size() > 1) : uchar(m_data[0]) - c.unicode(); }
0102 [[nodiscard]] int compare(QChar c, Qt::CaseSensitivity cs) const noexcept
0103 { return QtPrivate::compareStrings(*this, QStringView(&c, 1), cs); }
0104
0105 [[nodiscard]] bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0106 { return QtPrivate::startsWith(*this, s, cs); }
0107 [[nodiscard]] bool startsWith(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0108 { return QtPrivate::startsWith(*this, s, cs); }
0109 [[nodiscard]] constexpr bool startsWith(QChar c) const noexcept
0110 { return !isEmpty() && front() == c; }
0111 [[nodiscard]] bool startsWith(QChar c, Qt::CaseSensitivity cs) const noexcept
0112 { return QtPrivate::startsWith(*this, QStringView(&c, 1), cs); }
0113
0114 [[nodiscard]] bool endsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0115 { return QtPrivate::endsWith(*this, s, cs); }
0116 [[nodiscard]] bool endsWith(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0117 { return QtPrivate::endsWith(*this, s, cs); }
0118 [[nodiscard]] constexpr bool endsWith(QChar c) const noexcept
0119 { return !isEmpty() && back() == c; }
0120 [[nodiscard]] bool endsWith(QChar c, Qt::CaseSensitivity cs) const noexcept
0121 { return QtPrivate::endsWith(*this, QStringView(&c, 1), cs); }
0122
0123 [[nodiscard]] qsizetype indexOf(QStringView s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0124 { return QtPrivate::findString(*this, from, s, cs); }
0125 [[nodiscard]] qsizetype indexOf(QLatin1StringView s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0126 { return QtPrivate::findString(*this, from, s, cs); }
0127 [[nodiscard]] qsizetype indexOf(QChar c, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0128 { return QtPrivate::findString(*this, from, QStringView(&c, 1), cs); }
0129
0130 [[nodiscard]] bool contains(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0131 { return indexOf(s, 0, cs) != -1; }
0132 [[nodiscard]] bool contains(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0133 { return indexOf(s, 0, cs) != -1; }
0134 [[nodiscard]] bool contains(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0135 { return indexOf(QStringView(&c, 1), 0, cs) != -1; }
0136
0137 [[nodiscard]] qsizetype lastIndexOf(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0138 { return lastIndexOf(s, size(), cs); }
0139 [[nodiscard]] qsizetype lastIndexOf(QStringView s, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0140 { return QtPrivate::lastIndexOf(*this, from, s, cs); }
0141 [[nodiscard]] qsizetype lastIndexOf(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0142 { return lastIndexOf(s, size(), cs); }
0143 [[nodiscard]] qsizetype lastIndexOf(QLatin1StringView s, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0144 { return QtPrivate::lastIndexOf(*this, from, s, cs); }
0145 [[nodiscard]] qsizetype lastIndexOf(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0146 { return lastIndexOf(c, -1, cs); }
0147 [[nodiscard]] qsizetype lastIndexOf(QChar c, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0148 { return QtPrivate::lastIndexOf(*this, from, QStringView(&c, 1), cs); }
0149
0150 [[nodiscard]] qsizetype count(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
0151 { return QtPrivate::count(*this, str, cs); }
0152 [[nodiscard]] qsizetype count(QLatin1StringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
0153 { return QtPrivate::count(*this, str, cs); }
0154 [[nodiscard]] qsizetype count(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0155 { return QtPrivate::count(*this, ch, cs); }
0156
0157 [[nodiscard]] short toShort(bool *ok = nullptr, int base = 10) const
0158 { return QtPrivate::toIntegral<short>(QByteArrayView(*this), ok, base); }
0159 [[nodiscard]] ushort toUShort(bool *ok = nullptr, int base = 10) const
0160 { return QtPrivate::toIntegral<ushort>(QByteArrayView(*this), ok, base); }
0161 [[nodiscard]] int toInt(bool *ok = nullptr, int base = 10) const
0162 { return QtPrivate::toIntegral<int>(QByteArrayView(*this), ok, base); }
0163 [[nodiscard]] uint toUInt(bool *ok = nullptr, int base = 10) const
0164 { return QtPrivate::toIntegral<uint>(QByteArrayView(*this), ok, base); }
0165 [[nodiscard]] long toLong(bool *ok = nullptr, int base = 10) const
0166 { return QtPrivate::toIntegral<long>(QByteArrayView(*this), ok, base); }
0167 [[nodiscard]] ulong toULong(bool *ok = nullptr, int base = 10) const
0168 { return QtPrivate::toIntegral<ulong>(QByteArrayView(*this), ok, base); }
0169 [[nodiscard]] qlonglong toLongLong(bool *ok = nullptr, int base = 10) const
0170 { return QtPrivate::toIntegral<qlonglong>(QByteArrayView(*this), ok, base); }
0171 [[nodiscard]] qulonglong toULongLong(bool *ok = nullptr, int base = 10) const
0172 { return QtPrivate::toIntegral<qulonglong>(QByteArrayView(*this), ok, base); }
0173 [[nodiscard]] float toFloat(bool *ok = nullptr) const
0174 {
0175 const auto r = QtPrivate::toFloat(*this);
0176 if (ok)
0177 *ok = bool(r);
0178 return r.value_or(0.0f);
0179 }
0180 [[nodiscard]] double toDouble(bool *ok = nullptr) const
0181 {
0182 const auto r = QtPrivate::toDouble(*this);
0183 if (ok)
0184 *ok = bool(r);
0185 return r.value_or(0.0);
0186 }
0187
0188 using value_type = const char;
0189 using pointer = value_type*;
0190 using const_pointer = pointer;
0191 using reference = value_type&;
0192 using const_reference = reference;
0193 using iterator = value_type*;
0194 using const_iterator = iterator;
0195 using difference_type = qsizetype;
0196 using size_type = qsizetype;
0197
0198 constexpr const_iterator begin() const noexcept { return data(); }
0199 constexpr const_iterator cbegin() const noexcept { return data(); }
0200 constexpr const_iterator end() const noexcept { return data() + size(); }
0201 constexpr const_iterator cend() const noexcept { return data() + size(); }
0202
0203 using reverse_iterator = std::reverse_iterator<iterator>;
0204 using const_reverse_iterator = reverse_iterator;
0205
0206 const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
0207 const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(end()); }
0208 const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); }
0209 const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); }
0210
0211 [[nodiscard]] constexpr qsizetype max_size() const noexcept { return maxSize(); }
0212
0213 [[nodiscard]] static constexpr qsizetype maxSize() noexcept
0214 {
0215
0216 return QtPrivate::MaxAllocSize - 1;
0217 }
0218
0219 [[nodiscard]] constexpr QLatin1StringView mid(qsizetype pos, qsizetype n = -1) const
0220 {
0221 using namespace QtPrivate;
0222 auto result = QContainerImplHelper::mid(size(), &pos, &n);
0223 return result == QContainerImplHelper::Null ? QLatin1StringView()
0224 : QLatin1StringView(m_data + pos, n);
0225 }
0226 [[nodiscard]] constexpr QLatin1StringView left(qsizetype n) const
0227 {
0228 if (size_t(n) >= size_t(size()))
0229 n = size();
0230 return {m_data, n};
0231 }
0232 [[nodiscard]] constexpr QLatin1StringView right(qsizetype n) const
0233 {
0234 if (size_t(n) >= size_t(size()))
0235 n = size();
0236 return {m_data + m_size - n, n};
0237 }
0238
0239 [[nodiscard]] constexpr QLatin1StringView sliced(qsizetype pos) const
0240 { verify(pos, 0); return {m_data + pos, m_size - pos}; }
0241 [[nodiscard]] constexpr QLatin1StringView sliced(qsizetype pos, qsizetype n) const
0242 { verify(pos, n); return {m_data + pos, n}; }
0243 [[nodiscard]] constexpr QLatin1StringView first(qsizetype n) const
0244 { verify(0, n); return sliced(0, n); }
0245 [[nodiscard]] constexpr QLatin1StringView last(qsizetype n) const
0246 { verify(0, n); return sliced(size() - n, n); }
0247 [[nodiscard]] constexpr QLatin1StringView chopped(qsizetype n) const
0248 { verify(0, n); return sliced(0, size() - n); }
0249
0250 constexpr QLatin1StringView &slice(qsizetype pos)
0251 { *this = sliced(pos); return *this; }
0252 constexpr QLatin1StringView &slice(qsizetype pos, qsizetype n)
0253 { *this = sliced(pos, n); return *this; }
0254
0255 constexpr void chop(qsizetype n)
0256 { verify(0, n); m_size -= n; }
0257 constexpr void truncate(qsizetype n)
0258 { verify(0, n); m_size = n; }
0259
0260 [[nodiscard]] QLatin1StringView trimmed() const noexcept { return QtPrivate::trimmed(*this); }
0261
0262 template <typename Needle, typename...Flags>
0263 [[nodiscard]] constexpr auto tokenize(Needle &&needle, Flags...flags) const
0264 noexcept(noexcept(qTokenize(std::declval<const QLatin1StringView &>(),
0265 std::forward<Needle>(needle), flags...)))
0266 -> decltype(qTokenize(*this, std::forward<Needle>(needle), flags...))
0267 { return qTokenize(*this, std::forward<Needle>(needle), flags...); }
0268
0269 friend bool comparesEqual(const QLatin1StringView &s1, const QLatin1StringView &s2) noexcept
0270 { return s1.size() == s2.size() && QtPrivate::equalStrings(s1, s2); }
0271 friend Qt::strong_ordering
0272 compareThreeWay(const QLatin1StringView &s1, const QLatin1StringView &s2) noexcept
0273 {
0274 const int res = QtPrivate::compareStrings(s1, s2);
0275 return Qt::compareThreeWay(res, 0);
0276 }
0277 Q_DECLARE_STRONGLY_ORDERED(QLatin1StringView)
0278
0279
0280 friend bool comparesEqual(const QLatin1StringView &lhs, QChar rhs) noexcept
0281 { return lhs.size() == 1 && rhs == lhs.front(); }
0282 friend Qt::strong_ordering
0283 compareThreeWay(const QLatin1StringView &lhs, QChar rhs) noexcept
0284 {
0285
0286 const int res = -compare_helper(&rhs, 1, lhs);
0287 return Qt::compareThreeWay(res, 0);
0288 }
0289 Q_DECLARE_STRONGLY_ORDERED(QLatin1StringView, QChar)
0290
0291
0292 friend bool comparesEqual(const QLatin1StringView &lhs, const QStringView &rhs) noexcept
0293 { return lhs.size() == rhs.size() && QtPrivate::equalStrings(lhs, rhs); }
0294 friend Qt::strong_ordering
0295 compareThreeWay(const QLatin1StringView &lhs, const QStringView &rhs) noexcept
0296 {
0297 const int res = QtPrivate::compareStrings(lhs, rhs);
0298 return Qt::compareThreeWay(res, 0);
0299 }
0300 Q_DECLARE_STRONGLY_ORDERED(QLatin1StringView, QStringView)
0301
0302
0303
0304
0305
0306 friend bool comparesEqual(const QStringView &lhs, const QLatin1StringView &rhs) noexcept
0307 { return comparesEqual(rhs, lhs); }
0308 friend Qt::strong_ordering
0309 compareThreeWay(const QStringView &lhs, const QLatin1StringView &rhs) noexcept
0310 { return QtOrderingPrivate::reversed(compareThreeWay(rhs, lhs)); }
0311
0312 private:
0313 friend bool comparesEqual(const QLatin1StringView &lhs, const QByteArrayView &rhs) noexcept
0314 { return equal_helper(lhs, rhs.data(), rhs.size()); }
0315 friend Qt::strong_ordering
0316 compareThreeWay(const QLatin1StringView &lhs, const QByteArrayView &rhs) noexcept
0317 {
0318 const int res = compare_helper(lhs, rhs.data(), rhs.size());
0319 return Qt::compareThreeWay(res, 0);
0320 }
0321
0322
0323
0324
0325 friend bool comparesEqual(const QByteArrayView &lhs, const QLatin1StringView &rhs) noexcept
0326 { return comparesEqual(rhs, lhs); }
0327 friend Qt::strong_ordering
0328 compareThreeWay(const QByteArrayView &lhs, const QLatin1StringView &rhs) noexcept
0329 { return QtOrderingPrivate::reversed(compareThreeWay(rhs, lhs)); }
0330
0331 public:
0332 #if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
0333 Q_DECLARE_STRONGLY_ORDERED(QLatin1StringView, QByteArrayView, QT_ASCII_CAST_WARN)
0334 Q_DECLARE_STRONGLY_ORDERED(QLatin1StringView, QByteArray, QT_ASCII_CAST_WARN)
0335 Q_DECLARE_STRONGLY_ORDERED(QLatin1StringView, const char *, QT_ASCII_CAST_WARN)
0336 #endif
0337
0338 private:
0339 Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos,
0340 [[maybe_unused]] qsizetype n = 1) const
0341 {
0342 Q_ASSERT(pos >= 0);
0343 Q_ASSERT(pos <= size());
0344 Q_ASSERT(n >= 0);
0345 Q_ASSERT(n <= size() - pos);
0346 }
0347 static int compare_helper(const QLatin1StringView &s1, const char *s2) noexcept
0348 { return compare_helper(s1, s2, qstrlen(s2)); }
0349 Q_CORE_EXPORT static bool equal_helper(QLatin1StringView s1, const char *s2, qsizetype len) noexcept;
0350 Q_CORE_EXPORT static int compare_helper(const QLatin1StringView &s1, const char *s2, qsizetype len) noexcept;
0351 Q_CORE_EXPORT static int compare_helper(const QChar *data1, qsizetype length1,
0352 QLatin1StringView s2,
0353 Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
0354 #if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED)
0355 const char *m_data = nullptr;
0356 qsizetype m_size = 0;
0357 #else
0358 qsizetype m_size;
0359 const char *m_data;
0360 #endif
0361 };
0362 #ifdef Q_L1S_VIEW_IS_PRIMARY
0363 Q_DECLARE_TYPEINFO(QLatin1StringView, Q_RELOCATABLE_TYPE);
0364 #else
0365 Q_DECLARE_TYPEINFO(QLatin1String, Q_RELOCATABLE_TYPE);
0366 #endif
0367
0368 namespace Qt {
0369 inline namespace Literals {
0370 inline namespace StringLiterals {
0371
0372 constexpr inline QLatin1StringView operator""_L1(const char *str, size_t size) noexcept
0373 {
0374 return {str, qsizetype(size)};
0375 }
0376
0377 }
0378 }
0379 }
0380
0381 QT_END_NAMESPACE
0382
0383 #ifdef Q_L1S_VIEW_IS_PRIMARY
0384 # undef Q_L1S_VIEW_IS_PRIMARY
0385 #endif
0386
0387 #endif