File indexing completed on 2026-06-02 08:53:14
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef QSTRING_H
0009 #define QSTRING_H
0010
0011 #if defined(QT_NO_CAST_FROM_ASCII) && defined(QT_RESTRICTED_CAST_FROM_ASCII)
0012 #error QT_NO_CAST_FROM_ASCII and QT_RESTRICTED_CAST_FROM_ASCII must not be defined at the same time
0013 #endif
0014
0015 #include <QtCore/qchar.h>
0016 #include <QtCore/qcompare.h>
0017 #include <QtCore/qbytearray.h>
0018 #include <QtCore/qbytearrayview.h>
0019 #include <QtCore/qarraydata.h>
0020 #include <QtCore/qarraydatapointer.h>
0021 #include <QtCore/qlatin1stringview.h>
0022 #include <QtCore/qnamespace.h>
0023 #include <QtCore/qstringalgorithms.h>
0024 #include <QtCore/qanystringview.h>
0025 #include <QtCore/qstringtokenizer.h>
0026
0027 #include <string>
0028 #include <iterator>
0029 #include <QtCore/q20memory.h>
0030 #include <string_view>
0031 #include <QtCore/q23type_traits.h>
0032
0033 #include <stdarg.h>
0034
0035 #ifdef truncate
0036 #error qstring.h must be included before any header file that defines truncate
0037 #endif
0038
0039 #if defined(Q_OS_DARWIN) || defined(Q_QDOC)
0040 Q_FORWARD_DECLARE_CF_TYPE(CFString);
0041 Q_FORWARD_DECLARE_OBJC_CLASS(NSString);
0042 #endif
0043
0044 class tst_QString;
0045
0046 QT_BEGIN_NAMESPACE
0047
0048 class qfloat16;
0049 class QRegularExpression;
0050 class QRegularExpressionMatch;
0051 class QString;
0052
0053 namespace QtPrivate {
0054 template <bool...B> class BoolList;
0055
0056 template <typename Char>
0057 using IsCompatibleChar32TypeHelper =
0058 std::is_same<Char, char32_t>;
0059 template <typename Char>
0060 using IsCompatibleChar32Type
0061 = IsCompatibleChar32TypeHelper<q20::remove_cvref_t<Char>>;
0062
0063
0064
0065 template <typename T> struct treat_as_integral_arg : std::false_type {};
0066 template <> struct treat_as_integral_arg<unsigned short> : std::true_type {};
0067 template <> struct treat_as_integral_arg< signed short> : std::true_type {};
0068 template <> struct treat_as_integral_arg<unsigned char> : std::true_type {};
0069 template <> struct treat_as_integral_arg< signed char> : std::true_type {};
0070 }
0071
0072
0073
0074
0075
0076
0077 constexpr bool QtPrivate::isLatin1(QLatin1StringView) noexcept
0078 { return true; }
0079
0080
0081
0082
0083 int QStringView::compare(QLatin1StringView s, Qt::CaseSensitivity cs) const noexcept
0084 { return QtPrivate::compareStrings(*this, s, cs); }
0085 bool QStringView::startsWith(QLatin1StringView s, Qt::CaseSensitivity cs) const noexcept
0086 { return QtPrivate::startsWith(*this, s, cs); }
0087 bool QStringView::endsWith(QLatin1StringView s, Qt::CaseSensitivity cs) const noexcept
0088 { return QtPrivate::endsWith(*this, s, cs); }
0089 qsizetype QStringView::indexOf(QLatin1StringView s, qsizetype from, Qt::CaseSensitivity cs) const noexcept
0090 { return QtPrivate::findString(*this, from, s, cs); }
0091 bool QStringView::contains(QLatin1StringView s, Qt::CaseSensitivity cs) const noexcept
0092 { return indexOf(s, 0, cs) != qsizetype(-1); }
0093 qsizetype QStringView::lastIndexOf(QLatin1StringView s, Qt::CaseSensitivity cs) const noexcept
0094 { return QtPrivate::lastIndexOf(*this, size(), s, cs); }
0095 qsizetype QStringView::lastIndexOf(QLatin1StringView s, qsizetype from, Qt::CaseSensitivity cs) const noexcept
0096 { return QtPrivate::lastIndexOf(*this, from, s, cs); }
0097 qsizetype QStringView::count(QLatin1StringView s, Qt::CaseSensitivity cs) const
0098 { return QtPrivate::count(*this, s, cs); }
0099
0100
0101
0102
0103
0104 constexpr QAnyStringView::QAnyStringView(QLatin1StringView str) noexcept
0105 : m_data{str.data()}, m_size{size_t(str.size() << SizeShift) | Tag::Latin1} {}
0106
0107 constexpr QLatin1StringView QAnyStringView::asLatin1StringView() const
0108 {
0109 Q_ASSERT(isLatin1());
0110 return {m_data_utf8, size()};
0111 }
0112
0113
0114 template <typename Visitor>
0115 constexpr decltype(auto) QAnyStringView::visit(Visitor &&v) const
0116 {
0117 if (isUtf16())
0118 return std::forward<Visitor>(v)(asStringView());
0119 else if (isLatin1())
0120 return std::forward<Visitor>(v)(asLatin1StringView());
0121 else
0122 return std::forward<Visitor>(v)(asUtf8StringView());
0123 }
0124
0125
0126
0127
0128
0129 constexpr QChar QAnyStringView::front() const
0130 {
0131 return visit([] (auto that) { return QAnyStringView::toQChar(that.front()); });
0132 }
0133 constexpr QChar QAnyStringView::back() const
0134 {
0135 return visit([] (auto that) { return QAnyStringView::toQChar(that.back()); });
0136 }
0137
0138 using QStringPrivate = QArrayDataPointer<char16_t>;
0139
0140 class Q_CORE_EXPORT QString
0141 {
0142 typedef QTypedArrayData<char16_t> Data;
0143
0144 friend class ::tst_QString;
0145
0146 template <typename Iterator>
0147 static constexpr bool is_contiguous_iterator_v =
0148
0149
0150
0151
0152 std::is_pointer_v<Iterator>;
0153
0154 template <typename Char>
0155 using is_compatible_char_helper = std::disjunction<
0156 QtPrivate::IsCompatibleCharType<Char>,
0157 QtPrivate::IsCompatibleChar32Type<Char>,
0158 QtPrivate::IsCompatibleChar8Type<Char>,
0159 std::is_same<Char, QLatin1Char>
0160 >;
0161
0162 template <typename T>
0163 using is_string_like = std::conjunction<
0164 std::negation<QtPrivate::treat_as_integral_arg<std::remove_cv_t<T>>>,
0165 std::is_convertible<T, QAnyStringView>
0166 >;
0167
0168 template <typename T>
0169 using if_string_like = std::enable_if_t<is_string_like<T>::value, bool>;
0170
0171 template <typename T>
0172 using is_floating_point_like = std::disjunction<
0173 #if QFLOAT16_IS_NATIVE
0174 std::is_same<q20::remove_cvref_t<T>, QtPrivate::NativeFloat16Type>,
0175 #endif
0176 std::is_same<q20::remove_cvref_t<T>, qfloat16>,
0177 std::is_floating_point<T>
0178 >;
0179
0180 template <typename T>
0181 using if_floating_point = std::enable_if_t<is_floating_point_like<T>::value, bool>;
0182
0183 template <typename T>
0184 using if_integral_non_char = std::enable_if_t<std::conjunction_v<
0185 std::disjunction<
0186 std::is_integral<T>,
0187 std::conjunction<
0188 std::is_enum<T>,
0189 std::negation<q23::is_scoped_enum<T>>
0190 >
0191 >,
0192 std::negation<is_floating_point_like<T>>,
0193 std::negation<is_string_like<T>>
0194 >, bool>;
0195
0196 template <typename Iterator>
0197 static constexpr bool is_compatible_iterator_v = std::conjunction_v<
0198 std::is_convertible<
0199 typename std::iterator_traits<Iterator>::iterator_category,
0200 std::input_iterator_tag
0201 >,
0202 is_compatible_char_helper<typename std::iterator_traits<Iterator>::value_type>
0203 >;
0204
0205 template <typename Iterator>
0206 using if_compatible_iterator = std::enable_if_t<is_compatible_iterator_v<Iterator>, bool>;
0207
0208 public:
0209 typedef QStringPrivate DataPointer;
0210
0211 constexpr QString() noexcept;
0212 explicit QString(const QChar *unicode, qsizetype size = -1);
0213 QString(QChar c);
0214 QString(qsizetype size, QChar c);
0215 inline QString(QLatin1StringView latin1);
0216 explicit QString(QStringView sv) : QString(sv.data(), sv.size()) {}
0217 #if defined(__cpp_char8_t) || defined(Q_QDOC)
0218 Q_WEAK_OVERLOAD
0219 inline QString(const char8_t *str)
0220 : QString(fromUtf8(str))
0221 {}
0222 #endif
0223 inline QString(const QString &) noexcept;
0224 inline ~QString();
0225 QString &operator=(QChar c);
0226 QString &operator=(const QString &) noexcept;
0227 QString &operator=(QLatin1StringView latin1);
0228 inline QString(QString &&other) noexcept
0229 = default;
0230 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QString)
0231 void swap(QString &other) noexcept { d.swap(other.d); }
0232
0233 static constexpr qsizetype maxSize() noexcept
0234 {
0235
0236 return Data::maxSize() - 1;
0237 }
0238 constexpr qsizetype size() const noexcept
0239 {
0240 constexpr size_t MaxSize = maxSize();
0241 Q_PRESUME(size_t(d.size) <= MaxSize);
0242 return d.size;
0243 }
0244 #if QT_DEPRECATED_SINCE(6, 4)
0245 QT_DEPRECATED_VERSION_X_6_4("Use size() or length() instead.")
0246 constexpr qsizetype count() const { return size(); }
0247 #endif
0248 constexpr qsizetype length() const noexcept { return size(); }
0249 constexpr bool isEmpty() const noexcept { return size() == 0; }
0250 void resize(qsizetype size);
0251 void resize(qsizetype size, QChar fillChar);
0252 void resizeForOverwrite(qsizetype size);
0253
0254 QString &fill(QChar c, qsizetype size = -1);
0255 void truncate(qsizetype pos);
0256 void chop(qsizetype n);
0257
0258 QString &slice(qsizetype pos)
0259 { verify(pos, 0); return remove(0, pos); }
0260 QString &slice(qsizetype pos, qsizetype n)
0261 {
0262 verify(pos, n);
0263 if (isNull())
0264 return *this;
0265 resize(pos + n);
0266 return remove(0, pos);
0267 }
0268
0269 inline qsizetype capacity() const;
0270 inline void reserve(qsizetype size);
0271 inline void squeeze();
0272
0273 inline const QChar *unicode() const;
0274 inline QChar *data();
0275 inline const QChar *data() const;
0276 inline const QChar *constData() const;
0277
0278 inline void detach();
0279 inline bool isDetached() const;
0280 inline bool isSharedWith(const QString &other) const { return d.isSharedWith(other.d); }
0281 inline void clear();
0282
0283 inline const QChar at(qsizetype i) const;
0284 inline const QChar operator[](qsizetype i) const;
0285 [[nodiscard]] inline QChar &operator[](qsizetype i);
0286
0287 [[nodiscard]] inline QChar front() const { return at(0); }
0288 [[nodiscard]] inline QChar &front();
0289 [[nodiscard]] inline QChar back() const { return at(size() - 1); }
0290 [[nodiscard]] inline QChar &back();
0291
0292 #if QT_CORE_REMOVED_SINCE(6, 9)
0293 [[nodiscard]] QString arg(qlonglong a, int fieldwidth=0, int base=10,
0294 QChar fillChar = u' ') const;
0295 [[nodiscard]] QString arg(qulonglong a, int fieldwidth=0, int base=10,
0296 QChar fillChar = u' ') const;
0297 [[nodiscard]] inline QString arg(long a, int fieldwidth=0, int base=10,
0298 QChar fillChar = u' ') const;
0299 [[nodiscard]] inline QString arg(ulong a, int fieldwidth=0, int base=10,
0300 QChar fillChar = u' ') const;
0301 [[nodiscard]] inline QString arg(int a, int fieldWidth = 0, int base = 10,
0302 QChar fillChar = u' ') const;
0303 [[nodiscard]] inline QString arg(uint a, int fieldWidth = 0, int base = 10,
0304 QChar fillChar = u' ') const;
0305 [[nodiscard]] inline QString arg(short a, int fieldWidth = 0, int base = 10,
0306 QChar fillChar = u' ') const;
0307 [[nodiscard]] inline QString arg(ushort a, int fieldWidth = 0, int base = 10,
0308 QChar fillChar = u' ') const;
0309 [[nodiscard]] QString arg(double a, int fieldWidth = 0, char format = 'g', int precision = -1,
0310 QChar fillChar = u' ') const;
0311 [[nodiscard]] QString arg(char a, int fieldWidth = 0,
0312 QChar fillChar = u' ') const;
0313 [[nodiscard]] QString arg(QChar a, int fieldWidth = 0,
0314 QChar fillChar = u' ') const;
0315 [[nodiscard]] QString arg(const QString &a, int fieldWidth = 0,
0316 QChar fillChar = u' ') const;
0317 [[nodiscard]] QString arg(QStringView a, int fieldWidth = 0,
0318 QChar fillChar = u' ') const;
0319 [[nodiscard]] QString arg(QLatin1StringView a, int fieldWidth = 0,
0320 QChar fillChar = u' ') const;
0321 #endif
0322
0323 template <typename T, if_integral_non_char<T> = true>
0324 [[nodiscard]] QString arg(T a, int fieldWidth = 0, int base = 10,
0325 QChar fillChar = u' ') const
0326 {
0327 using U = typename std::conditional<
0328
0329 std::is_enum_v<T>, std::underlying_type<T>,
0330 q20::type_identity<T>
0331 >::type::type;
0332 if constexpr (std::is_signed_v<U>)
0333 return arg_impl(qlonglong(a), fieldWidth, base, fillChar);
0334 else
0335 return arg_impl(qulonglong(a), fieldWidth, base, fillChar);
0336 }
0337
0338 template <typename T, if_floating_point<T> = true>
0339 [[nodiscard]] QString arg(T a, int fieldWidth = 0, char format = 'g', int precision = -1,
0340 QChar fillChar = u' ') const
0341 { return arg_impl(double(a), fieldWidth, format, precision, fillChar); }
0342
0343 template <typename T, if_string_like<T> = true>
0344 [[nodiscard]] QString arg(const T &a, int fieldWidth = 0, QChar fillChar = u' ') const
0345 { return arg_impl(QAnyStringView(a), fieldWidth, fillChar); }
0346
0347 private:
0348 QString arg_impl(qlonglong a, int fieldwidth, int base, QChar fillChar) const;
0349 QString arg_impl(qulonglong a, int fieldwidth, int base, QChar fillChar) const;
0350 QString arg_impl(double a, int fieldWidth, char format, int precision, QChar fillChar) const;
0351 QString arg_impl(QAnyStringView a, int fieldWidth, QChar fillChar) const;
0352
0353 public:
0354 template <typename...Args>
0355 [[nodiscard]]
0356 #ifdef Q_QDOC
0357 QString
0358 #else
0359 typename std::enable_if<
0360 sizeof...(Args) >= 2 && std::conjunction_v<is_string_like<Args>...>,
0361 QString
0362 >::type
0363 #endif
0364 arg(Args &&...args) const
0365 { return qToStringViewIgnoringNull(*this).arg(std::forward<Args>(args)...); }
0366
0367 static QString vasprintf(const char *format, va_list ap) Q_ATTRIBUTE_FORMAT_PRINTF(1, 0);
0368 static QString asprintf(const char *format, ...) Q_ATTRIBUTE_FORMAT_PRINTF(1, 2);
0369
0370 [[nodiscard]] QT_CORE_INLINE_SINCE(6, 8)
0371 qsizetype indexOf(QChar c, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
0372 [[nodiscard]] qsizetype indexOf(QLatin1StringView s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
0373 [[nodiscard]] qsizetype indexOf(const QString &s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
0374 [[nodiscard]] qsizetype indexOf(QStringView s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0375 { return QtPrivate::findString(*this, from, s, cs); }
0376 [[nodiscard]] qsizetype lastIndexOf(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0377 { return lastIndexOf(c, -1, cs); }
0378 [[nodiscard]] QT_CORE_INLINE_SINCE(6, 8)
0379 qsizetype lastIndexOf(QChar c, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
0380 [[nodiscard]] qsizetype lastIndexOf(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
0381 { return lastIndexOf(s, size(), cs); }
0382 [[nodiscard]] qsizetype lastIndexOf(QLatin1StringView s, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
0383 [[nodiscard]] qsizetype lastIndexOf(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
0384 { return lastIndexOf(s, size(), cs); }
0385 [[nodiscard]] qsizetype lastIndexOf(const QString &s, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
0386
0387 [[nodiscard]] qsizetype lastIndexOf(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0388 { return lastIndexOf(s, size(), cs); }
0389 [[nodiscard]] qsizetype lastIndexOf(QStringView s, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0390 { return QtPrivate::lastIndexOf(*this, from, s, cs); }
0391
0392 [[nodiscard]] inline bool contains(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
0393 [[nodiscard]] inline bool contains(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
0394 [[nodiscard]] inline bool contains(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
0395 [[nodiscard]] inline bool contains(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
0396 [[nodiscard]] qsizetype count(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
0397 [[nodiscard]] qsizetype count(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
0398 [[nodiscard]] qsizetype count(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
0399
0400 #if QT_CONFIG(regularexpression)
0401 [[nodiscard]] qsizetype indexOf(const QRegularExpression &re, qsizetype from = 0,
0402 QRegularExpressionMatch *rmatch = nullptr) const;
0403 #ifdef Q_QDOC
0404 [[nodiscard]] qsizetype lastIndexOf(const QRegularExpression &re, QRegularExpressionMatch *rmatch = nullptr) const;
0405 #else
0406
0407 template <typename T = QRegularExpressionMatch, std::enable_if_t<std::is_same_v<T, QRegularExpressionMatch>, bool> = false>
0408 [[nodiscard]] qsizetype lastIndexOf(const QRegularExpression &re, T *rmatch = nullptr) const
0409 { return lastIndexOf(re, size(), rmatch); }
0410 #endif
0411 [[nodiscard]] qsizetype lastIndexOf(const QRegularExpression &re, qsizetype from,
0412 QRegularExpressionMatch *rmatch = nullptr) const;
0413 [[nodiscard]] bool contains(const QRegularExpression &re, QRegularExpressionMatch *rmatch = nullptr) const;
0414 [[nodiscard]] qsizetype count(const QRegularExpression &re) const;
0415 #endif
0416
0417 enum SectionFlag {
0418 SectionDefault = 0x00,
0419 SectionSkipEmpty = 0x01,
0420 SectionIncludeLeadingSep = 0x02,
0421 SectionIncludeTrailingSep = 0x04,
0422 SectionCaseInsensitiveSeps = 0x08
0423 };
0424 Q_DECLARE_FLAGS(SectionFlags, SectionFlag)
0425
0426 [[nodiscard]] inline QString section(QChar sep, qsizetype start, qsizetype end = -1, SectionFlags flags = SectionDefault) const;
0427 [[nodiscard]] QString section(const QString &in_sep, qsizetype start, qsizetype end = -1, SectionFlags flags = SectionDefault) const;
0428 #if QT_CONFIG(regularexpression)
0429 [[nodiscard]] QString section(const QRegularExpression &re, qsizetype start, qsizetype end = -1, SectionFlags flags = SectionDefault) const;
0430 #endif
0431
0432 #if QT_CORE_REMOVED_SINCE(6, 7)
0433 QString left(qsizetype n) const;
0434 QString right(qsizetype n) const;
0435 QString mid(qsizetype position, qsizetype n = -1) const;
0436
0437 QString first(qsizetype n) const;
0438 QString last(qsizetype n) const;
0439 QString sliced(qsizetype pos) const;
0440 QString sliced(qsizetype pos, qsizetype n) const;
0441 QString chopped(qsizetype n) const;
0442 #else
0443 [[nodiscard]] QString left(qsizetype n) const &
0444 {
0445 if (size_t(n) >= size_t(size()))
0446 return *this;
0447 return first(n);
0448 }
0449 [[nodiscard]] QString left(qsizetype n) &&
0450 {
0451 if (size_t(n) >= size_t(size()))
0452 return std::move(*this);
0453 return std::move(*this).first(n);
0454 }
0455 [[nodiscard]] QString right(qsizetype n) const &
0456 {
0457 if (size_t(n) >= size_t(size()))
0458 return *this;
0459 return last(n);
0460 }
0461 [[nodiscard]] QString right(qsizetype n) &&
0462 {
0463 if (size_t(n) >= size_t(size()))
0464 return std::move(*this);
0465 return std::move(*this).last(n);
0466 }
0467 [[nodiscard]] QString mid(qsizetype position, qsizetype n = -1) const &;
0468 [[nodiscard]] QString mid(qsizetype position, qsizetype n = -1) &&;
0469
0470 [[nodiscard]] QString first(qsizetype n) const &
0471 { verify(0, n); return sliced(0, n); }
0472 [[nodiscard]] QString last(qsizetype n) const &
0473 { verify(0, n); return sliced(size() - n, n); }
0474 [[nodiscard]] QString sliced(qsizetype pos) const &
0475 { verify(pos, 0); return sliced(pos, size() - pos); }
0476 [[nodiscard]] QString sliced(qsizetype pos, qsizetype n) const &
0477 { verify(pos, n); return QString(begin() + pos, n); }
0478 [[nodiscard]] QString chopped(qsizetype n) const &
0479 { verify(0, n); return sliced(0, size() - n); }
0480
0481 [[nodiscard]] QString first(qsizetype n) &&
0482 {
0483 verify(0, n);
0484 resize(n);
0485 return std::move(*this);
0486 }
0487 [[nodiscard]] QString last(qsizetype n) &&
0488 { verify(0, n); return sliced_helper(*this, size() - n, n); }
0489 [[nodiscard]] QString sliced(qsizetype pos) &&
0490 { verify(pos, 0); return sliced_helper(*this, pos, size() - pos); }
0491 [[nodiscard]] QString sliced(qsizetype pos, qsizetype n) &&
0492 { verify(pos, n); return sliced_helper(*this, pos, n); }
0493 [[nodiscard]] QString chopped(qsizetype n) &&
0494 { verify(0, n); return std::move(*this).first(size() - n); }
0495 #endif
0496 bool startsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
0497 [[nodiscard]] bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0498 { return QtPrivate::startsWith(*this, s, cs); }
0499 bool startsWith(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
0500 bool startsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
0501
0502 bool endsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
0503 [[nodiscard]] bool endsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0504 { return QtPrivate::endsWith(*this, s, cs); }
0505 bool endsWith(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
0506 bool endsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
0507
0508 bool isUpper() const;
0509 bool isLower() const;
0510
0511 [[nodiscard]] QString leftJustified(qsizetype width, QChar fill = u' ', bool trunc = false) const;
0512 [[nodiscard]] QString rightJustified(qsizetype width, QChar fill = u' ', bool trunc = false) const;
0513
0514 #if !defined(Q_QDOC)
0515 [[nodiscard]] QString toLower() const &
0516 { return toLower_helper(*this); }
0517 [[nodiscard]] QString toLower() &&
0518 { return toLower_helper(*this); }
0519 [[nodiscard]] QString toUpper() const &
0520 { return toUpper_helper(*this); }
0521 [[nodiscard]] QString toUpper() &&
0522 { return toUpper_helper(*this); }
0523 [[nodiscard]] QString toCaseFolded() const &
0524 { return toCaseFolded_helper(*this); }
0525 [[nodiscard]] QString toCaseFolded() &&
0526 { return toCaseFolded_helper(*this); }
0527 [[nodiscard]] QString trimmed() const &
0528 { return trimmed_helper(*this); }
0529 [[nodiscard]] QString trimmed() &&
0530 { return trimmed_helper(*this); }
0531 [[nodiscard]] QString simplified() const &
0532 { return simplified_helper(*this); }
0533 [[nodiscard]] QString simplified() &&
0534 { return simplified_helper(*this); }
0535 #else
0536 [[nodiscard]] QString toLower() const;
0537 [[nodiscard]] QString toUpper() const;
0538 [[nodiscard]] QString toCaseFolded() const;
0539 [[nodiscard]] QString trimmed() const;
0540 [[nodiscard]] QString simplified() const;
0541 #endif
0542 [[nodiscard]] QString toHtmlEscaped() const;
0543
0544 QString &insert(qsizetype i, QChar c);
0545 QString &insert(qsizetype i, const QChar *uc, qsizetype len);
0546 inline QString &insert(qsizetype i, const QString &s) { return insert(i, s.constData(), s.size()); }
0547 inline QString &insert(qsizetype i, QStringView v) { return insert(i, v.data(), v.size()); }
0548 QString &insert(qsizetype i, QLatin1StringView s);
0549 QString &insert(qsizetype i, QUtf8StringView s);
0550
0551 QString &append(QChar c);
0552 QString &append(const QChar *uc, qsizetype len);
0553 QString &append(const QString &s);
0554 inline QString &append(QStringView v) { return append(v.data(), v.size()); }
0555 QString &append(QLatin1StringView s);
0556 QString &append(QUtf8StringView s);
0557
0558 inline QString &prepend(QChar c) { return insert(0, c); }
0559 inline QString &prepend(const QChar *uc, qsizetype len) { return insert(0, uc, len); }
0560 inline QString &prepend(const QString &s) { return insert(0, s); }
0561 inline QString &prepend(QStringView v) { return prepend(v.data(), v.size()); }
0562 inline QString &prepend(QLatin1StringView s) { return insert(0, s); }
0563 QString &prepend(QUtf8StringView s) { return insert(0, s); }
0564
0565 QString &assign(QAnyStringView s);
0566 inline QString &assign(qsizetype n, QChar c)
0567 {
0568 Q_ASSERT(n >= 0);
0569 return fill(c, n);
0570 }
0571 template <typename InputIterator, if_compatible_iterator<InputIterator> = true>
0572 QString &assign(InputIterator first, InputIterator last)
0573 {
0574 using V = typename std::iterator_traits<InputIterator>::value_type;
0575 constexpr bool IsL1C = std::is_same_v<std::remove_cv_t<V>, QLatin1Char>;
0576 constexpr bool IsFwdIt = std::is_convertible_v<
0577 typename std::iterator_traits<InputIterator>::iterator_category,
0578 std::forward_iterator_tag
0579 >;
0580
0581 if constexpr (is_contiguous_iterator_v<InputIterator>) {
0582 const auto p = q20::to_address(first);
0583 const auto len = qsizetype(last - first);
0584 if constexpr (IsL1C)
0585 return assign(QLatin1StringView(reinterpret_cast<const char*>(p), len));
0586 else if constexpr (sizeof(V) == 4)
0587 return assign_helper(p, len);
0588 else
0589 return assign(QAnyStringView(p, len));
0590 } else if constexpr (sizeof(V) == 4) {
0591 resize(0);
0592 if constexpr (IsFwdIt) {
0593 const qsizetype requiredCapacity = 2 * std::distance(first, last);
0594 reserve(requiredCapacity);
0595 }
0596 while (first != last) {
0597 append(QChar::fromUcs4(*first));
0598 ++first;
0599 }
0600 return *this;
0601 } else if constexpr (QtPrivate::IsCompatibleChar8Type<V>::value) {
0602 assign_helper_char8(first, last);
0603 if (d.constAllocatedCapacity())
0604 d.data()[d.size] = u'\0';
0605 return *this;
0606 } else {
0607 d.assign(first, last, [](QChar ch) noexcept -> char16_t { return ch.unicode(); });
0608 if (d.constAllocatedCapacity())
0609 d.data()[d.size] = u'\0';
0610 return *this;
0611 }
0612 }
0613
0614 inline QString &operator+=(QChar c) { return append(c); }
0615
0616 inline QString &operator+=(const QString &s) { return append(s); }
0617 inline QString &operator+=(QStringView v) { return append(v); }
0618 inline QString &operator+=(QLatin1StringView s) { return append(s); }
0619 QString &operator+=(QUtf8StringView s) { return append(s); }
0620
0621 #if defined(QT_RESTRICTED_CAST_FROM_ASCII)
0622 template <qsizetype N>
0623 QString &insert(qsizetype i, const char (&ch)[N]) { return insert(i, QUtf8StringView(ch)); }
0624 template <qsizetype N>
0625 QString &append(const char (&ch)[N]) { return append(QUtf8StringView(ch)); }
0626 template <qsizetype N>
0627 QString &prepend(const char (&ch)[N]) { return prepend(QUtf8StringView(ch)); }
0628 template <qsizetype N>
0629 QString &operator+=(const char (&ch)[N]) { return append(QUtf8StringView(ch)); }
0630 #endif
0631
0632 QString &remove(qsizetype i, qsizetype len);
0633 QString &remove(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive);
0634 QString &remove(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive);
0635 QString &remove(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive);
0636
0637 QString &removeAt(qsizetype pos)
0638 { return size_t(pos) < size_t(size()) ? remove(pos, 1) : *this; }
0639 QString &removeFirst() { return !isEmpty() ? remove(0, 1) : *this; }
0640 QString &removeLast() { return !isEmpty() ? remove(size() - 1, 1) : *this; }
0641
0642 template <typename Predicate>
0643 QString &removeIf(Predicate pred)
0644 {
0645 removeIf_helper(pred);
0646 return *this;
0647 }
0648
0649 QString &replace(qsizetype i, qsizetype len, QChar after);
0650 QString &replace(qsizetype i, qsizetype len, const QChar *s, qsizetype slen);
0651 QString &replace(qsizetype i, qsizetype len, const QString &after);
0652 QString &replace(QChar before, QChar after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
0653 QString &replace(const QChar *before, qsizetype blen, const QChar *after, qsizetype alen, Qt::CaseSensitivity cs = Qt::CaseSensitive);
0654 QString &replace(QLatin1StringView before, QLatin1StringView after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
0655 QString &replace(QLatin1StringView before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
0656 QString &replace(const QString &before, QLatin1StringView after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
0657 QString &replace(const QString &before, const QString &after,
0658 Qt::CaseSensitivity cs = Qt::CaseSensitive);
0659 QString &replace(QChar c, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
0660 QString &replace(QChar c, QLatin1StringView after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
0661 #if QT_CONFIG(regularexpression)
0662 QString &replace(const QRegularExpression &re, const QString &after);
0663 inline QString &remove(const QRegularExpression &re)
0664 { return replace(re, QString()); }
0665 #endif
0666
0667 public:
0668 [[nodiscard]]
0669 QStringList split(const QString &sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts,
0670 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
0671 [[nodiscard]]
0672 QStringList split(QChar sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts,
0673 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
0674 #ifndef QT_NO_REGULAREXPRESSION
0675 [[nodiscard]]
0676 QStringList split(const QRegularExpression &sep,
0677 Qt::SplitBehavior behavior = Qt::KeepEmptyParts) const;
0678 #endif
0679
0680 template <typename Needle, typename...Flags>
0681 [[nodiscard]] inline auto tokenize(Needle &&needle, Flags...flags) const &
0682 noexcept(noexcept(qTokenize(std::declval<const QString &>(), std::forward<Needle>(needle), flags...)))
0683 -> decltype(qTokenize(*this, std::forward<Needle>(needle), flags...))
0684 { return qTokenize(qToStringViewIgnoringNull(*this), std::forward<Needle>(needle), flags...); }
0685
0686 template <typename Needle, typename...Flags>
0687 [[nodiscard]] inline auto tokenize(Needle &&needle, Flags...flags) const &&
0688 noexcept(noexcept(qTokenize(std::declval<const QString>(), std::forward<Needle>(needle), flags...)))
0689 -> decltype(qTokenize(std::move(*this), std::forward<Needle>(needle), flags...))
0690 { return qTokenize(std::move(*this), std::forward<Needle>(needle), flags...); }
0691
0692 template <typename Needle, typename...Flags>
0693 [[nodiscard]] inline auto tokenize(Needle &&needle, Flags...flags) &&
0694 noexcept(noexcept(qTokenize(std::declval<QString>(), std::forward<Needle>(needle), flags...)))
0695 -> decltype(qTokenize(std::move(*this), std::forward<Needle>(needle), flags...))
0696 { return qTokenize(std::move(*this), std::forward<Needle>(needle), flags...); }
0697
0698
0699 enum NormalizationForm {
0700 NormalizationForm_D,
0701 NormalizationForm_C,
0702 NormalizationForm_KD,
0703 NormalizationForm_KC
0704 };
0705 [[nodiscard]] QString normalized(NormalizationForm mode, QChar::UnicodeVersion version = QChar::Unicode_Unassigned) const;
0706
0707 [[nodiscard]] QString repeated(qsizetype times) const;
0708
0709 const ushort *utf16() const;
0710 [[nodiscard]] QString nullTerminated() const &;
0711 [[nodiscard]] QString nullTerminated() &&;
0712 QString &nullTerminate();
0713
0714 #if !defined(Q_QDOC)
0715 [[nodiscard]] QByteArray toLatin1() const &
0716 { return toLatin1_helper(*this); }
0717 [[nodiscard]] QByteArray toLatin1() &&
0718 { return toLatin1_helper_inplace(*this); }
0719 [[nodiscard]] QByteArray toUtf8() const &
0720 { return toUtf8_helper(*this); }
0721 [[nodiscard]] QByteArray toUtf8() &&
0722 { return toUtf8_helper(*this); }
0723 [[nodiscard]] QByteArray toLocal8Bit() const &
0724 { return toLocal8Bit_helper(isNull() ? nullptr : constData(), size()); }
0725 [[nodiscard]] QByteArray toLocal8Bit() &&
0726 { return toLocal8Bit_helper(isNull() ? nullptr : constData(), size()); }
0727 #else
0728 [[nodiscard]] QByteArray toLatin1() const;
0729 [[nodiscard]] QByteArray toUtf8() const;
0730 [[nodiscard]] QByteArray toLocal8Bit() const;
0731 #endif
0732 [[nodiscard]] QList<uint> toUcs4() const;
0733
0734
0735 static QString fromLatin1(QByteArrayView ba);
0736 Q_WEAK_OVERLOAD
0737 static inline QString fromLatin1(const QByteArray &ba) { return fromLatin1(QByteArrayView(ba)); }
0738 static inline QString fromLatin1(const char *str, qsizetype size)
0739 {
0740 return fromLatin1(QByteArrayView(str, !str || size < 0 ? qstrlen(str) : size));
0741 }
0742 static QString fromUtf8(QByteArrayView utf8);
0743 Q_WEAK_OVERLOAD
0744 static inline QString fromUtf8(const QByteArray &ba) { return fromUtf8(QByteArrayView(ba)); }
0745 static inline QString fromUtf8(const char *utf8, qsizetype size)
0746 {
0747 return fromUtf8(QByteArrayView(utf8, !utf8 || size < 0 ? qstrlen(utf8) : size));
0748 }
0749 #if defined(__cpp_char8_t) || defined(Q_QDOC)
0750 Q_WEAK_OVERLOAD
0751 static inline QString fromUtf8(const char8_t *str)
0752 { return fromUtf8(reinterpret_cast<const char *>(str)); }
0753 Q_WEAK_OVERLOAD
0754 static inline QString fromUtf8(const char8_t *str, qsizetype size)
0755 { return fromUtf8(reinterpret_cast<const char *>(str), size); }
0756 #endif
0757 static QString fromLocal8Bit(QByteArrayView ba);
0758 Q_WEAK_OVERLOAD
0759 static inline QString fromLocal8Bit(const QByteArray &ba) { return fromLocal8Bit(QByteArrayView(ba)); }
0760 static inline QString fromLocal8Bit(const char *str, qsizetype size)
0761 {
0762 return fromLocal8Bit(QByteArrayView(str, !str || size < 0 ? qstrlen(str) : size));
0763 }
0764 static QString fromUtf16(const char16_t *, qsizetype size = -1);
0765 static QString fromUcs4(const char32_t *, qsizetype size = -1);
0766 static QString fromRawData(const char16_t *unicode, qsizetype size)
0767 {
0768 return QString(DataPointer::fromRawData(unicode, size));
0769 }
0770 QT_CORE_INLINE_SINCE(6, 10)
0771 static QString fromRawData(const QChar *, qsizetype size);
0772
0773 #if QT_DEPRECATED_SINCE(6, 0)
0774 QT_DEPRECATED_VERSION_X_6_0("Use char16_t* overload.")
0775 static QString fromUtf16(const ushort *str, qsizetype size = -1)
0776 { return fromUtf16(reinterpret_cast<const char16_t *>(str), size); }
0777 QT_DEPRECATED_VERSION_X_6_0("Use char32_t* overload.")
0778 static QString fromUcs4(const uint *str, qsizetype size = -1)
0779 { return fromUcs4(reinterpret_cast<const char32_t *>(str), size); }
0780 #endif
0781
0782 inline qsizetype toWCharArray(wchar_t *array) const;
0783 [[nodiscard]] static inline QString fromWCharArray(const wchar_t *string, qsizetype size = -1);
0784
0785 QString &setRawData(const QChar *unicode, qsizetype size);
0786 QString &setUnicode(const QChar *unicode, qsizetype size);
0787 Q_WEAK_OVERLOAD
0788 QString &setUnicode(const char16_t *utf16, qsizetype size)
0789 { return setUnicode(reinterpret_cast<const QChar *>(utf16), size); }
0790 QString &setUtf16(const char16_t *utf16, qsizetype size)
0791 { return setUnicode(reinterpret_cast<const QChar *>(utf16), size); }
0792
0793 #if !QT_CORE_REMOVED_SINCE(6, 9)
0794 Q_WEAK_OVERLOAD
0795 #endif
0796 QString &setUtf16(const ushort *autf16, qsizetype asize)
0797 { return setUnicode(reinterpret_cast<const QChar *>(autf16), asize); }
0798
0799 int compare(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
0800 int compare(QLatin1StringView other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
0801 inline int compare(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
0802 int compare(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0803 { return compare(QStringView{&ch, 1}, cs); }
0804
0805 static inline int compare(const QString &s1, const QString &s2,
0806 Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept
0807 { return s1.compare(s2, cs); }
0808
0809 static inline int compare(const QString &s1, QLatin1StringView s2,
0810 Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept
0811 { return s1.compare(s2, cs); }
0812 static inline int compare(QLatin1StringView s1, const QString &s2,
0813 Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept
0814 { return -s2.compare(s1, cs); }
0815 static int compare(const QString &s1, QStringView s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept
0816 { return s1.compare(s2, cs); }
0817 static int compare(QStringView s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept
0818 { return -s2.compare(s1, cs); }
0819
0820 int localeAwareCompare(const QString& s) const;
0821 inline int localeAwareCompare(QStringView s) const;
0822 static int localeAwareCompare(const QString& s1, const QString& s2)
0823 { return s1.localeAwareCompare(s2); }
0824
0825 static inline int localeAwareCompare(QStringView s1, QStringView s2);
0826
0827 short toShort(bool *ok=nullptr, int base=10) const
0828 { return toIntegral_helper<short>(*this, ok, base); }
0829 ushort toUShort(bool *ok=nullptr, int base=10) const
0830 { return toIntegral_helper<ushort>(*this, ok, base); }
0831 int toInt(bool *ok=nullptr, int base=10) const
0832 { return toIntegral_helper<int>(*this, ok, base); }
0833 uint toUInt(bool *ok=nullptr, int base=10) const
0834 { return toIntegral_helper<uint>(*this, ok, base); }
0835 long toLong(bool *ok=nullptr, int base=10) const
0836 { return toIntegral_helper<long>(*this, ok, base); }
0837 ulong toULong(bool *ok=nullptr, int base=10) const
0838 { return toIntegral_helper<ulong>(*this, ok, base); }
0839 QT_CORE_INLINE_SINCE(6, 5)
0840 qlonglong toLongLong(bool *ok=nullptr, int base=10) const;
0841 QT_CORE_INLINE_SINCE(6, 5)
0842 qulonglong toULongLong(bool *ok=nullptr, int base=10) const;
0843 float toFloat(bool *ok=nullptr) const;
0844 double toDouble(bool *ok=nullptr) const;
0845
0846 inline QString &setNum(short, int base=10);
0847 inline QString &setNum(ushort, int base=10);
0848 inline QString &setNum(int, int base=10);
0849 inline QString &setNum(uint, int base=10);
0850 inline QString &setNum(long, int base=10);
0851 inline QString &setNum(ulong, int base=10);
0852 QString &setNum(qlonglong, int base=10);
0853 QString &setNum(qulonglong, int base=10);
0854 inline QString &setNum(float, char format='g', int precision=6);
0855 QString &setNum(double, char format='g', int precision=6);
0856
0857 static QString number(int, int base=10);
0858 static QString number(uint, int base=10);
0859 static QString number(long, int base=10);
0860 static QString number(ulong, int base=10);
0861 static QString number(qlonglong, int base=10);
0862 static QString number(qulonglong, int base=10);
0863 static QString number(double, char format='g', int precision=6);
0864
0865 friend bool comparesEqual(const QString &s1, const QString &s2) noexcept
0866 { return comparesEqual(QStringView(s1), QStringView(s2)); }
0867 friend Qt::strong_ordering compareThreeWay(const QString &s1, const QString &s2) noexcept
0868 { return compareThreeWay(QStringView(s1), QStringView(s2)); }
0869 Q_DECLARE_STRONGLY_ORDERED(QString)
0870
0871 Q_WEAK_OVERLOAD
0872 friend bool comparesEqual(const QString &s1, QUtf8StringView s2) noexcept
0873 { return QtPrivate::equalStrings(s1, s2); }
0874 Q_WEAK_OVERLOAD
0875 friend Qt::strong_ordering compareThreeWay(const QString &s1, QUtf8StringView s2) noexcept
0876 {
0877 const int res = QtPrivate::compareStrings(s1, s2, Qt::CaseSensitive);
0878 return Qt::compareThreeWay(res, 0);
0879 }
0880 Q_DECLARE_STRONGLY_ORDERED(QString, QUtf8StringView, Q_WEAK_OVERLOAD)
0881
0882 #ifdef __cpp_char8_t
0883 friend bool comparesEqual(const QString &s1, const char8_t *s2) noexcept
0884 { return comparesEqual(s1, QUtf8StringView(s2)); }
0885 friend Qt::strong_ordering compareThreeWay(const QString &s1, const char8_t *s2) noexcept
0886 { return compareThreeWay(s1, QUtf8StringView(s2)); }
0887 Q_DECLARE_STRONGLY_ORDERED(QString, const char8_t *)
0888 #endif
0889
0890 friend bool comparesEqual(const QString &s1, QLatin1StringView s2) noexcept
0891 { return (s1.size() == s2.size()) && QtPrivate::equalStrings(s1, s2); }
0892 friend Qt::strong_ordering
0893 compareThreeWay(const QString &s1, QLatin1StringView s2) noexcept
0894 {
0895 const int res = QtPrivate::compareStrings(s1, s2, Qt::CaseSensitive);
0896 return Qt::compareThreeWay(res, 0);
0897 }
0898 Q_DECLARE_STRONGLY_ORDERED(QString, QLatin1StringView)
0899
0900
0901 friend bool comparesEqual(const QString &s1, std::nullptr_t) noexcept
0902 { return s1.isEmpty(); }
0903 friend Qt::strong_ordering compareThreeWay(const QString &s1, std::nullptr_t) noexcept
0904 { return s1.isEmpty() ? Qt::strong_ordering::equivalent : Qt::strong_ordering::greater; }
0905 Q_DECLARE_STRONGLY_ORDERED(QString, std::nullptr_t)
0906
0907 friend bool comparesEqual(const QString &s1, const char16_t *s2) noexcept
0908 { return comparesEqual(s1, QStringView(s2)); }
0909 friend Qt::strong_ordering compareThreeWay(const QString &s1, const char16_t *s2) noexcept
0910 { return compareThreeWay(s1, QStringView(s2)); }
0911 Q_DECLARE_STRONGLY_ORDERED(QString, const char16_t *)
0912
0913
0914 friend bool comparesEqual(const QString &lhs, QChar rhs) noexcept
0915 { return lhs.size() == 1 && rhs == lhs.front(); }
0916 friend Qt::strong_ordering compareThreeWay(const QString &lhs, QChar rhs) noexcept
0917 {
0918 const int res = compare_helper(lhs.data(), lhs.size(), &rhs, 1);
0919 return Qt::compareThreeWay(res, 0);
0920 }
0921 Q_DECLARE_STRONGLY_ORDERED(QString, QChar)
0922
0923
0924 #if defined(QT_RESTRICTED_CAST_FROM_ASCII)
0925 template <qsizetype N>
0926 inline QString(const char (&ch)[N])
0927 : QString(fromUtf8(ch))
0928 {}
0929 template <qsizetype N>
0930 QString(char (&)[N]) = delete;
0931 template <qsizetype N>
0932 inline QString &operator=(const char (&ch)[N])
0933 { return (*this = fromUtf8(ch, N - 1)); }
0934 template <qsizetype N>
0935 QString &operator=(char (&)[N]) = delete;
0936 #endif
0937 #if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
0938 QT_ASCII_CAST_WARN inline QString(const char *ch)
0939 : QString(fromUtf8(ch))
0940 {}
0941 QT_ASCII_CAST_WARN inline QString(const QByteArray &a)
0942 : QString(fromUtf8(a))
0943 {}
0944 QT_ASCII_CAST_WARN inline QString &operator=(const char *ch)
0945 {
0946 if (!ch) {
0947 clear();
0948 return *this;
0949 }
0950 return assign(ch);
0951 }
0952 QT_ASCII_CAST_WARN inline QString &operator=(const QByteArray &a)
0953 {
0954 if (a.isNull()) {
0955 clear();
0956 return *this;
0957 }
0958 return assign(a);
0959 }
0960
0961 QT_ASCII_CAST_WARN inline QString &prepend(const char *s)
0962 { return prepend(QUtf8StringView(s)); }
0963 QT_ASCII_CAST_WARN inline QString &prepend(const QByteArray &s)
0964 { return prepend(QUtf8StringView(s)); }
0965 QT_ASCII_CAST_WARN inline QString &append(const char *s)
0966 { return append(QUtf8StringView(s)); }
0967 QT_ASCII_CAST_WARN inline QString &append(const QByteArray &s)
0968 { return append(QUtf8StringView(s)); }
0969 QT_ASCII_CAST_WARN inline QString &insert(qsizetype i, const char *s)
0970 { return insert(i, QUtf8StringView(s)); }
0971 QT_ASCII_CAST_WARN inline QString &insert(qsizetype i, const QByteArray &s)
0972 { return insert(i, QUtf8StringView(s)); }
0973 QT_ASCII_CAST_WARN inline QString &operator+=(const char *s)
0974 { return append(QUtf8StringView(s)); }
0975 QT_ASCII_CAST_WARN inline QString &operator+=(const QByteArray &s)
0976 { return append(QUtf8StringView(s)); }
0977
0978 #if QT_CORE_REMOVED_SINCE(6, 8)
0979 QT_ASCII_CAST_WARN inline bool operator==(const char *s) const;
0980 QT_ASCII_CAST_WARN inline bool operator!=(const char *s) const;
0981 QT_ASCII_CAST_WARN inline bool operator<(const char *s) const;
0982 QT_ASCII_CAST_WARN inline bool operator<=(const char *s) const;
0983 QT_ASCII_CAST_WARN inline bool operator>(const char *s) const;
0984 QT_ASCII_CAST_WARN inline bool operator>=(const char *s) const;
0985
0986 QT_ASCII_CAST_WARN inline bool operator==(const QByteArray &s) const;
0987 QT_ASCII_CAST_WARN inline bool operator!=(const QByteArray &s) const;
0988 QT_ASCII_CAST_WARN inline bool operator<(const QByteArray &s) const;
0989 QT_ASCII_CAST_WARN inline bool operator>(const QByteArray &s) const;
0990 QT_ASCII_CAST_WARN inline bool operator<=(const QByteArray &s) const;
0991 QT_ASCII_CAST_WARN inline bool operator>=(const QByteArray &s) const;
0992 #else
0993 friend bool comparesEqual(const QString &lhs, QByteArrayView rhs) noexcept
0994 {
0995 return QString::compare_helper(lhs.constData(), lhs.size(),
0996 rhs.constData(), rhs.size()) == 0;
0997 }
0998 friend Qt::strong_ordering
0999 compareThreeWay(const QString &lhs, QByteArrayView rhs) noexcept
1000 {
1001 const int res = QString::compare_helper(lhs.constData(), lhs.size(),
1002 rhs.constData(), rhs.size());
1003 return Qt::compareThreeWay(res, 0);
1004 }
1005 Q_DECLARE_STRONGLY_ORDERED(QString, QByteArrayView, QT_ASCII_CAST_WARN)
1006
1007 friend bool comparesEqual(const QString &lhs, const QByteArray &rhs) noexcept
1008 { return comparesEqual(lhs, QByteArrayView(rhs)); }
1009 friend Qt::strong_ordering
1010 compareThreeWay(const QString &lhs, const QByteArray &rhs) noexcept
1011 {
1012 return compareThreeWay(lhs, QByteArrayView(rhs));
1013 }
1014 Q_DECLARE_STRONGLY_ORDERED(QString, QByteArray, QT_ASCII_CAST_WARN)
1015
1016 friend bool comparesEqual(const QString &lhs, const char *rhs) noexcept
1017 { return comparesEqual(lhs, QByteArrayView(rhs)); }
1018 friend Qt::strong_ordering
1019 compareThreeWay(const QString &lhs, const char *rhs) noexcept
1020 {
1021 return compareThreeWay(lhs, QByteArrayView(rhs));
1022 }
1023 Q_DECLARE_STRONGLY_ORDERED(QString, const char *, QT_ASCII_CAST_WARN)
1024 #endif
1025
1026 #endif
1027
1028 typedef QChar *iterator;
1029 typedef const QChar *const_iterator;
1030 typedef iterator Iterator;
1031 typedef const_iterator ConstIterator;
1032 typedef std::reverse_iterator<iterator> reverse_iterator;
1033 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
1034 inline iterator begin();
1035 inline const_iterator begin() const;
1036 inline const_iterator cbegin() const;
1037 inline const_iterator constBegin() const;
1038 inline iterator end();
1039 inline const_iterator end() const;
1040 inline const_iterator cend() const;
1041 inline const_iterator constEnd() const;
1042 reverse_iterator rbegin() { return reverse_iterator(end()); }
1043 reverse_iterator rend() { return reverse_iterator(begin()); }
1044 const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
1045 const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
1046 const_reverse_iterator crbegin() const { return const_reverse_iterator(end()); }
1047 const_reverse_iterator crend() const { return const_reverse_iterator(begin()); }
1048
1049
1050 typedef qsizetype size_type;
1051 typedef qptrdiff difference_type;
1052 typedef const QChar & const_reference;
1053 typedef QChar & reference;
1054 typedef QChar *pointer;
1055 typedef const QChar *const_pointer;
1056 typedef QChar value_type;
1057 inline void push_back(QChar c) { append(c); }
1058 inline void push_back(const QString &s) { append(s); }
1059 inline void push_front(QChar c) { prepend(c); }
1060 inline void push_front(const QString &s) { prepend(s); }
1061 void shrink_to_fit() { squeeze(); }
1062 iterator erase(const_iterator first, const_iterator last);
1063 inline iterator erase(const_iterator it) { return erase(it, it + 1); }
1064 constexpr qsizetype max_size() const noexcept
1065 {
1066 return maxSize();
1067 }
1068
1069 static inline QString fromStdString(const std::string &s);
1070 std::string toStdString() const;
1071 static inline QString fromStdWString(const std::wstring &s);
1072 inline std::wstring toStdWString() const;
1073
1074 static inline QString fromStdU16String(const std::u16string &s);
1075 inline std::u16string toStdU16String() const;
1076 static inline QString fromStdU32String(const std::u32string &s);
1077 inline std::u32string toStdU32String() const;
1078
1079 Q_IMPLICIT inline operator std::u16string_view() const noexcept;
1080
1081 #if defined(Q_OS_DARWIN) || defined(Q_QDOC)
1082 static QString fromCFString(CFStringRef string);
1083 CFStringRef toCFString() const Q_DECL_CF_RETURNS_RETAINED;
1084 static QString fromNSString(const NSString *string);
1085 NSString *toNSString() const Q_DECL_NS_RETURNS_AUTORELEASED;
1086 #endif
1087
1088 #if defined(Q_OS_WASM) || defined(Q_QDOC)
1089 static QString fromEcmaString(emscripten::val jsString);
1090 emscripten::val toEcmaString() const;
1091 #endif
1092
1093 constexpr bool isNull() const { return d.isNull(); }
1094
1095 bool isRightToLeft() const;
1096 [[nodiscard]] bool isValidUtf16() const noexcept
1097 { return QStringView(*this).isValidUtf16(); }
1098
1099 QString(qsizetype size, Qt::Initialization);
1100 explicit QString(DataPointer &&dd) : d(std::move(dd)) {}
1101
1102 private:
1103 #if defined(QT_NO_CAST_FROM_ASCII)
1104 #define QSTRING_DECL_DELETED_ASCII_OP Q_DECL_EQ_DELETE_X("This function is not available under QT_NO_CAST_FROM_ASCII")
1105 QString &operator+=(const char *s) QSTRING_DECL_DELETED_ASCII_OP;
1106 QString &operator+=(const QByteArray &s) QSTRING_DECL_DELETED_ASCII_OP;
1107 QString(const char *ch) QSTRING_DECL_DELETED_ASCII_OP;
1108 QString(const QByteArray &a) QSTRING_DECL_DELETED_ASCII_OP;
1109 QString &operator=(const char *ch) QSTRING_DECL_DELETED_ASCII_OP;
1110 QString &operator=(const QByteArray &a) QSTRING_DECL_DELETED_ASCII_OP;
1111 #undef QSTRING_DECL_DELETED_ASCII_OP
1112 #endif
1113
1114 DataPointer d;
1115 static const char16_t _empty;
1116
1117 void reallocData(qsizetype alloc, QArrayData::AllocationOption option);
1118 void reallocGrowData(qsizetype n);
1119
1120 QString &assign_helper(const char32_t *data, qsizetype len);
1121
1122 template <typename InputIterator>
1123 void assign_helper_char8(InputIterator first, InputIterator last);
1124 static int compare_helper(const QChar *data1, qsizetype length1,
1125 const QChar *data2, qsizetype length2,
1126 Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
1127 static int compare_helper(const QChar *data1, qsizetype length1,
1128 const char *data2, qsizetype length2,
1129 Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
1130 static int localeAwareCompare_helper(const QChar *data1, qsizetype length1,
1131 const QChar *data2, qsizetype length2);
1132 static QString sliced_helper(QString &str, qsizetype pos, qsizetype n);
1133 static QString toLower_helper(const QString &str);
1134 static QString toLower_helper(QString &str);
1135 static QString toUpper_helper(const QString &str);
1136 static QString toUpper_helper(QString &str);
1137 static QString toCaseFolded_helper(const QString &str);
1138 static QString toCaseFolded_helper(QString &str);
1139 static QString trimmed_helper(const QString &str);
1140 static QString trimmed_helper(QString &str);
1141 static QString simplified_helper(const QString &str);
1142 static QString simplified_helper(QString &str);
1143 static QByteArray toLatin1_helper(const QString &);
1144 static QByteArray toLatin1_helper_inplace(QString &);
1145 static QByteArray toUtf8_helper(const QString &);
1146 static QByteArray toLocal8Bit_helper(const QChar *data, qsizetype size);
1147 #if QT_CORE_REMOVED_SINCE(6, 6)
1148 static qsizetype toUcs4_helper(const ushort *uc, qsizetype length, uint *out);
1149 #endif
1150 static qsizetype toUcs4_helper(const char16_t *uc, qsizetype length, char32_t *out);
1151 static qlonglong toIntegral_helper(QStringView string, bool *ok, int base);
1152 static qulonglong toIntegral_helper(QStringView string, bool *ok, uint base);
1153 template <typename Predicate>
1154 qsizetype removeIf_helper(Predicate pred)
1155 {
1156 const qsizetype result = d->eraseIf(pred);
1157 if (result > 0)
1158 d.data()[d.size] = u'\0';
1159 return result;
1160 }
1161
1162 friend class QStringView;
1163 friend class QByteArray;
1164 friend struct QAbstractConcatenable;
1165 template <typename T> friend qsizetype erase(QString &s, const T &t);
1166 template <typename Predicate> friend qsizetype erase_if(QString &s, Predicate pred);
1167
1168 template <typename T> static
1169 T toIntegral_helper(QStringView string, bool *ok, int base)
1170 {
1171 using Int64 = typename std::conditional<std::is_unsigned<T>::value, qulonglong, qlonglong>::type;
1172 using Int32 = typename std::conditional<std::is_unsigned<T>::value, uint, int>::type;
1173
1174
1175 Int64 val = toIntegral_helper(string, ok, Int32(base));
1176 if (T(val) != val) {
1177 if (ok)
1178 *ok = false;
1179 val = 0;
1180 }
1181 return T(val);
1182 }
1183
1184 Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos = 0,
1185 [[maybe_unused]] qsizetype n = 1) const
1186 {
1187 Q_ASSERT(pos >= 0);
1188 Q_ASSERT(pos <= d.size);
1189 Q_ASSERT(n >= 0);
1190 Q_ASSERT(n <= d.size - pos);
1191 }
1192
1193 public:
1194 inline DataPointer &data_ptr() { return d; }
1195 inline const DataPointer &data_ptr() const { return d; }
1196 };
1197
1198
1199
1200
1201
1202 int QLatin1StringView::compare(QUtf8StringView other, Qt::CaseSensitivity cs) const noexcept
1203 { return QtPrivate::compareStrings(*this, other, cs); }
1204
1205
1206
1207
1208
1209 QString QLatin1StringView::toString() const { return *this; }
1210
1211
1212
1213
1214
1215 int QStringView::compare(QUtf8StringView other, Qt::CaseSensitivity cs) const noexcept
1216 { return QtPrivate::compareStrings(*this, other, cs); }
1217
1218
1219
1220
1221
1222 QString QStringView::toString() const
1223 { return QString(*this); }
1224
1225 qint64 QStringView::toLongLong(bool *ok, int base) const
1226 { return QString::toIntegral_helper<qint64>(*this, ok, base); }
1227 quint64 QStringView::toULongLong(bool *ok, int base) const
1228 { return QString::toIntegral_helper<quint64>(*this, ok, base); }
1229 long QStringView::toLong(bool *ok, int base) const
1230 { return QString::toIntegral_helper<long>(*this, ok, base); }
1231 ulong QStringView::toULong(bool *ok, int base) const
1232 { return QString::toIntegral_helper<ulong>(*this, ok, base); }
1233 int QStringView::toInt(bool *ok, int base) const
1234 { return QString::toIntegral_helper<int>(*this, ok, base); }
1235 uint QStringView::toUInt(bool *ok, int base) const
1236 { return QString::toIntegral_helper<uint>(*this, ok, base); }
1237 short QStringView::toShort(bool *ok, int base) const
1238 { return QString::toIntegral_helper<short>(*this, ok, base); }
1239 ushort QStringView::toUShort(bool *ok, int base) const
1240 { return QString::toIntegral_helper<ushort>(*this, ok, base); }
1241
1242
1243
1244
1245
1246 template <bool UseChar8T>
1247 int QBasicUtf8StringView<UseChar8T>::compare(QChar other, Qt::CaseSensitivity cs) const noexcept
1248 {
1249 return QtPrivate::compareStrings(*this, QStringView(&other, 1), cs);
1250 }
1251
1252 template <bool UseChar8T>
1253 int QBasicUtf8StringView<UseChar8T>::compare(QStringView other, Qt::CaseSensitivity cs) const noexcept
1254 {
1255 return QtPrivate::compareStrings(*this, other, cs);
1256 }
1257
1258 template <bool UseChar8T>
1259 [[nodiscard]] bool QBasicUtf8StringView<UseChar8T>::equal(QChar other) const noexcept
1260 {
1261 return QtPrivate::equalStrings(*this, QStringView(&other, 1));
1262 }
1263
1264 template <bool UseChar8T>
1265 [[nodiscard]] bool QBasicUtf8StringView<UseChar8T>::equal(QStringView other) const noexcept
1266 {
1267 return QtPrivate::equalStrings(*this, other);
1268 }
1269
1270
1271
1272
1273
1274 template <bool UseChar8T>
1275 QString QBasicUtf8StringView<UseChar8T>::toString() const
1276 {
1277 return QString::fromUtf8(data(), size());
1278 }
1279
1280 template<bool UseChar8T>
1281 [[nodiscard]] int QBasicUtf8StringView<UseChar8T>::compare(QLatin1StringView other,
1282 Qt::CaseSensitivity cs) const noexcept
1283 {
1284 return QtPrivate::compareStrings(*this, other, cs);
1285 }
1286
1287 template<bool UseChar8T>
1288 [[nodiscard]] int QBasicUtf8StringView<UseChar8T>::compare(const QByteArray &other,
1289 Qt::CaseSensitivity cs) const noexcept
1290 {
1291 return QtPrivate::compareStrings(*this,
1292 QBasicUtf8StringView<UseChar8T>(other.data(), other.size()),
1293 cs);
1294 }
1295
1296 template <bool UseChar8T>
1297 [[nodiscard]] bool QBasicUtf8StringView<UseChar8T>::equal(QLatin1StringView other) const noexcept
1298 {
1299 return QtPrivate::equalStrings(*this, other);
1300 }
1301
1302 template <bool UseChar8T>
1303 [[nodiscard]] bool QBasicUtf8StringView<UseChar8T>::equal(const QByteArray &other) const noexcept
1304 {
1305 return size() == other.size()
1306 && QtPrivate::equalStrings(*this, QBasicUtf8StringView<UseChar8T>(other.data(),
1307 other.size()));
1308 }
1309
1310
1311
1312
1313
1314 QAnyStringView::QAnyStringView(const QByteArray &str) noexcept
1315 : QAnyStringView{str.begin(), str.size()} {}
1316 QAnyStringView::QAnyStringView(const QString &str) noexcept
1317 : QAnyStringView{str.begin(), str.size()} {}
1318
1319 QString QAnyStringView::toString() const
1320 { return QtPrivate::convertToQString(*this); }
1321
1322
1323
1324
1325 QString::QString(QLatin1StringView latin1)
1326 : QString{QString::fromLatin1(latin1.data(), latin1.size())} {}
1327 const QChar QString::at(qsizetype i) const
1328 { verify(i, 1); return QChar(d.data()[i]); }
1329 const QChar QString::operator[](qsizetype i) const
1330 { verify(i, 1); return QChar(d.data()[i]); }
1331 const QChar *QString::unicode() const
1332 { return data(); }
1333 const QChar *QString::data() const
1334 {
1335 #if QT5_NULL_STRINGS == 1
1336 return reinterpret_cast<const QChar *>(d.data() ? d.data() : &_empty);
1337 #else
1338 return reinterpret_cast<const QChar *>(d.data());
1339 #endif
1340 }
1341 QChar *QString::data()
1342 {
1343 detach();
1344 Q_ASSERT(d.data());
1345 return reinterpret_cast<QChar *>(d.data());
1346 }
1347 const QChar *QString::constData() const
1348 { return data(); }
1349 void QString::detach()
1350 { if (d.needsDetach()) reallocData(d.size, QArrayData::KeepSize); }
1351 bool QString::isDetached() const
1352 { return !d.isShared(); }
1353 void QString::clear()
1354 { if (!isNull()) *this = QString(); }
1355 QString::QString(const QString &other) noexcept : d(other.d)
1356 { }
1357 qsizetype QString::capacity() const { return qsizetype(d.constAllocatedCapacity()); }
1358 QString &QString::setNum(short n, int base)
1359 { return setNum(qlonglong(n), base); }
1360 QString &QString::setNum(ushort n, int base)
1361 { return setNum(qulonglong(n), base); }
1362 QString &QString::setNum(int n, int base)
1363 { return setNum(qlonglong(n), base); }
1364 QString &QString::setNum(uint n, int base)
1365 { return setNum(qulonglong(n), base); }
1366 QString &QString::setNum(long n, int base)
1367 { return setNum(qlonglong(n), base); }
1368 QString &QString::setNum(ulong n, int base)
1369 { return setNum(qulonglong(n), base); }
1370 QString &QString::setNum(float n, char f, int prec)
1371 { return setNum(double(n),f,prec); }
1372 #if QT_CORE_REMOVED_SINCE(6, 9)
1373 QString QString::arg(int a, int fieldWidth, int base, QChar fillChar) const
1374 { return arg(qlonglong(a), fieldWidth, base, fillChar); }
1375 QString QString::arg(uint a, int fieldWidth, int base, QChar fillChar) const
1376 { return arg(qulonglong(a), fieldWidth, base, fillChar); }
1377 QString QString::arg(long a, int fieldWidth, int base, QChar fillChar) const
1378 { return arg(qlonglong(a), fieldWidth, base, fillChar); }
1379 QString QString::arg(ulong a, int fieldWidth, int base, QChar fillChar) const
1380 { return arg(qulonglong(a), fieldWidth, base, fillChar); }
1381 QString QString::arg(short a, int fieldWidth, int base, QChar fillChar) const
1382 { return arg(qlonglong(a), fieldWidth, base, fillChar); }
1383 QString QString::arg(ushort a, int fieldWidth, int base, QChar fillChar) const
1384 { return arg(qulonglong(a), fieldWidth, base, fillChar); }
1385 #endif
1386
1387 QString QString::section(QChar asep, qsizetype astart, qsizetype aend, SectionFlags aflags) const
1388 { return section(QString(asep), astart, aend, aflags); }
1389
1390 QT_WARNING_PUSH
1391 QT_WARNING_DISABLE_MSVC(4127)
1392 QT_WARNING_DISABLE_INTEL(111)
1393
1394 qsizetype QString::toWCharArray(wchar_t *array) const
1395 {
1396 return qToStringViewIgnoringNull(*this).toWCharArray(array);
1397 }
1398
1399 qsizetype QStringView::toWCharArray(wchar_t *array) const
1400 {
1401 if (sizeof(wchar_t) == sizeof(QChar)) {
1402 if (auto src = data())
1403 memcpy(array, src, sizeof(QChar) * size());
1404 return size();
1405 } else {
1406 return QString::toUcs4_helper(utf16(), size(), reinterpret_cast<char32_t *>(array));
1407 }
1408 }
1409
1410 QT_WARNING_POP
1411
1412 QString QString::fromWCharArray(const wchar_t *string, qsizetype size)
1413 {
1414 if constexpr (sizeof(wchar_t) == sizeof(QChar)) {
1415 return QString(reinterpret_cast<const QChar *>(string), size);
1416 } else {
1417 #ifdef QT_BOOTSTRAPPED
1418 Q_UNREACHABLE_RETURN(QString());
1419 #else
1420 return fromUcs4(reinterpret_cast<const char32_t *>(string), size);
1421 #endif
1422 }
1423 }
1424
1425 constexpr QString::QString() noexcept {}
1426 QString::~QString() {}
1427
1428 void QString::reserve(qsizetype asize)
1429 {
1430 if (d.needsDetach() || asize >= capacity() - d.freeSpaceAtBegin())
1431 reallocData(qMax(asize, size()), QArrayData::KeepSize);
1432 if (d.constAllocatedCapacity())
1433 d.setFlag(Data::CapacityReserved);
1434 }
1435
1436 void QString::squeeze()
1437 {
1438 if (!d.isMutable())
1439 return;
1440 if (d.needsDetach() || size() < capacity())
1441 reallocData(d.size, QArrayData::KeepSize);
1442 if (d.constAllocatedCapacity())
1443 d.clearFlag(Data::CapacityReserved);
1444 }
1445
1446 QChar &QString::operator[](qsizetype i)
1447 { verify(i, 1); return data()[i]; }
1448 QChar &QString::front() { return operator[](0); }
1449 QChar &QString::back() { return operator[](size() - 1); }
1450 QString::iterator QString::begin()
1451 { detach(); return reinterpret_cast<QChar*>(d.data()); }
1452 QString::const_iterator QString::begin() const
1453 { return reinterpret_cast<const QChar*>(d.data()); }
1454 QString::const_iterator QString::cbegin() const
1455 { return reinterpret_cast<const QChar*>(d.data()); }
1456 QString::const_iterator QString::constBegin() const
1457 { return reinterpret_cast<const QChar*>(d.data()); }
1458 QString::iterator QString::end()
1459 { detach(); return reinterpret_cast<QChar*>(d.data() + d.size); }
1460 QString::const_iterator QString::end() const
1461 { return reinterpret_cast<const QChar*>(d.data() + d.size); }
1462 QString::const_iterator QString::cend() const
1463 { return reinterpret_cast<const QChar*>(d.data() + d.size); }
1464 QString::const_iterator QString::constEnd() const
1465 { return reinterpret_cast<const QChar*>(d.data() + d.size); }
1466 bool QString::contains(const QString &s, Qt::CaseSensitivity cs) const
1467 { return indexOf(s, 0, cs) != -1; }
1468 bool QString::contains(QLatin1StringView s, Qt::CaseSensitivity cs) const
1469 { return indexOf(s, 0, cs) != -1; }
1470 bool QString::contains(QChar c, Qt::CaseSensitivity cs) const
1471 { return indexOf(c, 0, cs) != -1; }
1472 bool QString::contains(QStringView s, Qt::CaseSensitivity cs) const noexcept
1473 { return indexOf(s, 0, cs) != -1; }
1474
1475 #if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
1476 #if QT_CORE_REMOVED_SINCE(6, 8)
1477 bool QString::operator==(const char *s) const
1478 { return QString::compare_helper(constData(), size(), s, -1) == 0; }
1479 bool QString::operator!=(const char *s) const
1480 { return QString::compare_helper(constData(), size(), s, -1) != 0; }
1481 bool QString::operator<(const char *s) const
1482 { return QString::compare_helper(constData(), size(), s, -1) < 0; }
1483 bool QString::operator>(const char *s) const
1484 { return QString::compare_helper(constData(), size(), s, -1) > 0; }
1485 bool QString::operator<=(const char *s) const
1486 { return QString::compare_helper(constData(), size(), s, -1) <= 0; }
1487 bool QString::operator>=(const char *s) const
1488 { return QString::compare_helper(constData(), size(), s, -1) >= 0; }
1489
1490 QT_ASCII_CAST_WARN bool QString::operator==(const QByteArray &s) const
1491 { return QString::compare_helper(constData(), size(), s.constData(), s.size()) == 0; }
1492 QT_ASCII_CAST_WARN bool QString::operator!=(const QByteArray &s) const
1493 { return QString::compare_helper(constData(), size(), s.constData(), s.size()) != 0; }
1494 QT_ASCII_CAST_WARN bool QString::operator<(const QByteArray &s) const
1495 { return QString::compare_helper(constData(), size(), s.constData(), s.size()) < 0; }
1496 QT_ASCII_CAST_WARN bool QString::operator>(const QByteArray &s) const
1497 { return QString::compare_helper(constData(), size(), s.constData(), s.size()) > 0; }
1498 QT_ASCII_CAST_WARN bool QString::operator<=(const QByteArray &s) const
1499 { return QString::compare_helper(constData(), size(), s.constData(), s.size()) <= 0; }
1500 QT_ASCII_CAST_WARN bool QString::operator>=(const QByteArray &s) const
1501 { return QString::compare_helper(constData(), size(), s.constData(), s.size()) >= 0; }
1502
1503 bool QByteArray::operator==(const QString &s) const
1504 { return QString::compare_helper(s.constData(), s.size(), constData(), size()) == 0; }
1505 bool QByteArray::operator!=(const QString &s) const
1506 { return QString::compare_helper(s.constData(), s.size(), constData(), size()) != 0; }
1507 bool QByteArray::operator<(const QString &s) const
1508 { return QString::compare_helper(s.constData(), s.size(), constData(), size()) > 0; }
1509 bool QByteArray::operator>(const QString &s) const
1510 { return QString::compare_helper(s.constData(), s.size(), constData(), size()) < 0; }
1511 bool QByteArray::operator<=(const QString &s) const
1512 { return QString::compare_helper(s.constData(), s.size(), constData(), size()) >= 0; }
1513 bool QByteArray::operator>=(const QString &s) const
1514 { return QString::compare_helper(s.constData(), s.size(), constData(), size()) <= 0; }
1515 #endif
1516 #endif
1517
1518 #if !defined(QT_USE_FAST_OPERATOR_PLUS) && !defined(QT_USE_QSTRINGBUILDER)
1519
1520 inline QString operator+(const QString &s1, const QString &s2)
1521 { QString t(s1); t += s2; return t; }
1522 inline QString operator+(QString &&lhs, const QString &rhs)
1523 { return std::move(lhs += rhs); }
1524 inline QString operator+(const QString &s1, QChar s2)
1525 { QString t(s1); t += s2; return t; }
1526 inline QString operator+(QString &&lhs, QChar rhs)
1527 { return std::move(lhs += rhs); }
1528 inline QString operator+(QChar s1, const QString &s2)
1529 { QString t(s1); t += s2; return t; }
1530 inline QString operator+(const QString &lhs, QStringView rhs)
1531 {
1532 QString ret{lhs.size() + rhs.size(), Qt::Uninitialized};
1533 return ret.assign(lhs).append(rhs);
1534 }
1535 inline QString operator+(QStringView lhs, const QString &rhs)
1536 {
1537 QString ret{lhs.size() + rhs.size(), Qt::Uninitialized};
1538 return ret.assign(lhs).append(rhs);
1539 }
1540
1541 # if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
1542 QT_ASCII_CAST_WARN inline QString operator+(const QString &s1, const char *s2)
1543 { QString t(s1); t += QUtf8StringView(s2); return t; }
1544 QT_ASCII_CAST_WARN inline QString operator+(QString &&lhs, const char *rhs)
1545 { QT_IGNORE_DEPRECATIONS(return std::move(lhs += rhs);) }
1546 QT_ASCII_CAST_WARN inline QString operator+(const char *s1, const QString &s2)
1547 { QString t = QString::fromUtf8(s1); t += s2; return t; }
1548 QT_ASCII_CAST_WARN inline QString operator+(const QByteArray &ba, const QString &s)
1549 { QString t = QString::fromUtf8(ba); t += s; return t; }
1550 QT_ASCII_CAST_WARN inline QString operator+(const QString &s, const QByteArray &ba)
1551 { QString t(s); t += QUtf8StringView(ba); return t; }
1552 QT_ASCII_CAST_WARN inline QString operator+(QString &&lhs, const QByteArray &rhs)
1553 { QT_IGNORE_DEPRECATIONS(return std::move(lhs += rhs);) }
1554 # endif
1555 #endif
1556
1557 QString QString::fromStdString(const std::string &s)
1558 { return fromUtf8(s.data(), qsizetype(s.size())); }
1559
1560 std::wstring QString::toStdWString() const
1561 {
1562 std::wstring str;
1563 str.resize(size());
1564 str.resize(toWCharArray(str.data()));
1565 return str;
1566 }
1567
1568 QString QString::fromStdWString(const std::wstring &s)
1569 { return fromWCharArray(s.data(), qsizetype(s.size())); }
1570
1571 QString QString::fromStdU16String(const std::u16string &s)
1572 { return fromUtf16(s.data(), qsizetype(s.size())); }
1573
1574 std::u16string QString::toStdU16String() const
1575 { return std::u16string(reinterpret_cast<const char16_t*>(data()), size()); }
1576
1577 QString QString::fromStdU32String(const std::u32string &s)
1578 { return fromUcs4(s.data(), qsizetype(s.size())); }
1579
1580 std::u32string QString::toStdU32String() const
1581 {
1582 std::u32string u32str(size(), char32_t(0));
1583 const qsizetype len = toUcs4_helper(reinterpret_cast<const char16_t *>(data()),
1584 size(), u32str.data());
1585 u32str.resize(len);
1586 return u32str;
1587 }
1588
1589 QString::operator std::u16string_view() const noexcept
1590 {
1591 return std::u16string_view(d.data(), size_t(d.size));
1592 }
1593
1594 #if !defined(QT_NO_DATASTREAM) || defined(QT_BOOTSTRAPPED)
1595 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QString &);
1596 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QString &);
1597 #endif
1598
1599 Q_DECLARE_SHARED(QString)
1600 Q_DECLARE_OPERATORS_FOR_FLAGS(QString::SectionFlags)
1601
1602 int QString::compare(QStringView s, Qt::CaseSensitivity cs) const noexcept
1603 { return -s.compare(*this, cs); }
1604
1605 int QString::localeAwareCompare(QStringView s) const
1606 { return localeAwareCompare_helper(constData(), size(), s.constData(), s.size()); }
1607 int QString::localeAwareCompare(QStringView s1, QStringView s2)
1608 { return localeAwareCompare_helper(s1.constData(), s1.size(), s2.constData(), s2.size()); }
1609 int QStringView::localeAwareCompare(QStringView other) const
1610 { return QString::localeAwareCompare(*this, other); }
1611
1612 #if QT_CORE_INLINE_IMPL_SINCE(6, 5)
1613 qint64 QString::toLongLong(bool *ok, int base) const
1614 {
1615 return toIntegral_helper<qlonglong>(*this, ok, base);
1616 }
1617
1618 quint64 QString::toULongLong(bool *ok, int base) const
1619 {
1620 return toIntegral_helper<qulonglong>(*this, ok, base);
1621 }
1622 #endif
1623 #if QT_CORE_INLINE_IMPL_SINCE(6, 8)
1624 qsizetype QString::indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const
1625 {
1626 return qToStringViewIgnoringNull(*this).indexOf(ch, from, cs);
1627 }
1628 qsizetype QString::lastIndexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const
1629 {
1630 return qToStringViewIgnoringNull(*this).lastIndexOf(ch, from, cs);
1631 }
1632 #endif
1633 #if QT_CORE_INLINE_IMPL_SINCE(6, 10)
1634 QString QString::fromRawData(const QChar *unicode, qsizetype size)
1635 {
1636 return fromRawData(reinterpret_cast<const char16_t *>(unicode), size);
1637 }
1638 #endif
1639
1640 namespace QtPrivate {
1641
1642 inline const QString &asString(const QString &s) { return s; }
1643 inline QString &&asString(QString &&s) { return std::move(s); }
1644 }
1645
1646 #ifndef qPrintable
1647 # define qPrintable(string) QtPrivate::asString(string).toLocal8Bit().constData()
1648 #endif
1649
1650 #ifndef qUtf8Printable
1651 # define qUtf8Printable(string) QtPrivate::asString(string).toUtf8().constData()
1652 #endif
1653
1654
1655
1656
1657
1658 #ifndef qUtf16Printable
1659 # define qUtf16Printable(string) \
1660 static_cast<const wchar_t *>( \
1661 static_cast<const void *>(QtPrivate::asString(string).nullTerminated().constData()))
1662 #endif
1663
1664
1665
1666
1667
1668 namespace QtPrivate {
1669
1670 struct ArgBase {
1671 enum Tag : uchar { L1, Any, U16 } tag;
1672 };
1673
1674 struct QStringViewArg : ArgBase {
1675 QStringView string;
1676 QStringViewArg() = default;
1677 constexpr explicit QStringViewArg(QStringView v) noexcept : ArgBase{U16}, string{v} {}
1678 };
1679
1680 struct QLatin1StringArg : ArgBase {
1681 QLatin1StringView string;
1682 QLatin1StringArg() = default;
1683 constexpr explicit QLatin1StringArg(QLatin1StringView v) noexcept : ArgBase{L1}, string{v} {}
1684 };
1685
1686 struct QAnyStringArg : ArgBase {
1687 QAnyStringView string;
1688 QAnyStringArg() = default;
1689 constexpr explicit QAnyStringArg(QAnyStringView v) noexcept : ArgBase{Any}, string{v} {}
1690 };
1691
1692 #if QT_CORE_REMOVED_SINCE(6, 9)
1693 [[nodiscard]] Q_CORE_EXPORT QString argToQString(QStringView pattern, size_t n, const ArgBase **args);
1694 [[nodiscard]] Q_CORE_EXPORT QString argToQString(QLatin1StringView pattern, size_t n, const ArgBase **args);
1695 #endif
1696 [[nodiscard]] Q_CORE_EXPORT QString argToQString(QAnyStringView pattern, size_t n, const ArgBase **args);
1697
1698 template <typename...Args>
1699 [[nodiscard]] Q_ALWAYS_INLINE QString argToQStringDispatch(QAnyStringView pattern, const Args &...args)
1700 {
1701 const ArgBase *argBases[] = {&args..., nullptr};
1702 return QtPrivate::argToQString(pattern, sizeof...(Args), argBases);
1703 }
1704
1705 constexpr inline QAnyStringArg qStringLikeToArg(QAnyStringView s) noexcept { return QAnyStringArg{s}; }
1706
1707 }
1708
1709 template <typename...Args>
1710 Q_ALWAYS_INLINE
1711 QString QStringView::arg(Args &&...args) const
1712 {
1713 return QtPrivate::argToQStringDispatch(*this, QtPrivate::qStringLikeToArg(args)...);
1714 }
1715
1716 template <typename...Args>
1717 Q_ALWAYS_INLINE
1718 QString QLatin1StringView::arg(Args &&...args) const
1719 {
1720 return QtPrivate::argToQStringDispatch(*this, QtPrivate::qStringLikeToArg(args)...);
1721 }
1722
1723 template <bool HasChar8T>
1724 template <typename...Args>
1725 QString QBasicUtf8StringView<HasChar8T>::arg(Args &&...args) const
1726 {
1727 return QtPrivate::argToQStringDispatch(*this, QtPrivate::qStringLikeToArg(args)...);
1728 }
1729
1730 template <typename...Args>
1731 QString QAnyStringView::arg(Args &&...args) const
1732 {
1733 return QtPrivate::argToQStringDispatch(*this, QtPrivate::qStringLikeToArg(args)...);
1734 }
1735
1736 template <typename T>
1737 qsizetype erase(QString &s, const T &t)
1738 {
1739 return s.removeIf_helper([&t](const auto &e) { return t == e; });
1740 }
1741
1742 template <typename Predicate>
1743 qsizetype erase_if(QString &s, Predicate pred)
1744 {
1745 return s.removeIf_helper(pred);
1746 }
1747
1748 namespace Qt {
1749 inline namespace Literals {
1750 inline namespace StringLiterals {
1751 inline QString operator""_s(const char16_t *str, size_t size) noexcept
1752 {
1753 return QString(QStringPrivate(nullptr, const_cast<char16_t *>(str), qsizetype(size)));
1754 }
1755
1756 }
1757 }
1758 }
1759
1760 inline namespace QtLiterals {
1761 #if QT_DEPRECATED_SINCE(6, 8)
1762
1763 QT_DEPRECATED_VERSION_X_6_8("Use _s from Qt::StringLiterals namespace instead.")
1764 inline QString operator""_qs(const char16_t *str, size_t size) noexcept
1765 {
1766 return Qt::StringLiterals::operator""_s(str, size);
1767 }
1768
1769 #endif
1770 }
1771
1772
1773
1774
1775
1776
1777 #define QT_UNICODE_LITERAL(str) u"" str
1778
1779 namespace QtPrivate {
1780 template <qsizetype N>
1781 Q_ALWAYS_INLINE static QStringPrivate qMakeStringPrivate(const char16_t (&literal)[N])
1782 {
1783
1784 auto str = const_cast<char16_t *>(literal);
1785 return { nullptr, str, N - 1 };
1786 }
1787 }
1788
1789 #define QStringLiteral(str) (QString(QtPrivate::qMakeStringPrivate(QT_UNICODE_LITERAL(str))))
1790
1791 QT_END_NAMESPACE
1792
1793 #include <QtCore/qstringbuilder.h>
1794 #include <QtCore/qstringconverter.h>
1795
1796 #ifdef Q_L1S_VIEW_IS_PRIMARY
1797 # undef Q_L1S_VIEW_IS_PRIMARY
1798 #endif
1799
1800 #endif