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