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