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