Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-18 08:32:39

0001 // Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com>
0002 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0003 // Qt-Security score:critical reason:data-parser
0004 #ifndef QUTF8STRINGVIEW_H
0005 #define QUTF8STRINGVIEW_H
0006 
0007 #if 0
0008 #pragma qt_class(QUtf8StringView)
0009 #endif
0010 
0011 #include <QtCore/qstringalgorithms.h>
0012 #include <QtCore/qstringfwd.h>
0013 #include <QtCore/qarraydata.h> // for QContainerImplHelper
0014 #include <QtCore/qbytearrayview.h>
0015 #include <QtCore/qcompare.h>
0016 #include <QtCore/qcontainerfwd.h>
0017 
0018 #include <string>
0019 #include <string_view>
0020 #include <QtCore/q20type_traits.h>
0021 
0022 QT_BEGIN_NAMESPACE
0023 
0024 namespace QtPrivate {
0025 template <typename Char>
0026 using IsCompatibleChar8TypeHelper = std::disjunction<
0027 #ifdef __cpp_char8_t
0028         std::is_same<Char, char8_t>,
0029 #endif
0030         std::is_same<Char, char>,
0031         std::is_same<Char, uchar>,
0032         std::is_same<Char, signed char>
0033     >;
0034 template <typename Char>
0035 using IsCompatibleChar8Type
0036     = IsCompatibleChar8TypeHelper<q20::remove_cvref_t<Char>>;
0037 
0038 template <typename Pointer>
0039 struct IsCompatiblePointer8Helper : std::false_type {};
0040 template <typename Char>
0041 struct IsCompatiblePointer8Helper<Char*>
0042     : IsCompatibleChar8Type<Char> {};
0043 template <typename Pointer>
0044 using IsCompatiblePointer8
0045     = IsCompatiblePointer8Helper<q20::remove_cvref_t<Pointer>>;
0046 
0047 template <typename T, typename Enable = void>
0048 struct IsContainerCompatibleWithQUtf8StringView : std::false_type {};
0049 
0050 template <typename T>
0051 struct IsContainerCompatibleWithQUtf8StringView<T, std::enable_if_t<std::conjunction_v<
0052         // lacking concepts and ranges, we accept any T whose std::data yields a suitable pointer ...
0053         IsCompatiblePointer8<decltype(std::data(std::declval<const T &>()))>,
0054         // ... and that has a suitable size ...
0055         std::is_convertible<
0056             decltype(std::size(std::declval<const T &>())),
0057             qsizetype
0058         >,
0059         // ... and it's a range as it defines an iterator-like API
0060         IsCompatibleChar8Type<typename std::iterator_traits<
0061             decltype(std::begin(std::declval<const T &>()))>::value_type
0062         >,
0063         std::is_convertible<
0064             decltype( std::begin(std::declval<const T &>()) != std::end(std::declval<const T &>()) ),
0065             bool
0066         >,
0067 
0068         // This needs to be treated specially due to the empty vs null distinction
0069         std::negation<std::is_same<std::decay_t<T>, QByteArray>>,
0070 
0071         // This has a compatible value_type, but explicitly a different encoding
0072         std::negation<std::is_same<std::decay_t<T>, QLatin1StringView>>,
0073 
0074         // Don't make an accidental copy constructor
0075         std::negation<std::disjunction<
0076             std::is_same<std::decay_t<T>, QBasicUtf8StringView<true>>,
0077             std::is_same<std::decay_t<T>, QBasicUtf8StringView<false>>
0078         >>
0079     >>> : std::true_type {};
0080 
0081 struct hide_char8_t {
0082 #ifdef __cpp_char8_t
0083     using type = char8_t;
0084 #endif
0085 };
0086 
0087 struct wrap_char { using type = char; };
0088 
0089 } // namespace QtPrivate
0090 
0091 #ifdef Q_QDOC
0092 #define QBasicUtf8StringView QUtf8StringView
0093 #else
0094 template <bool UseChar8T>
0095 #endif
0096 class QBasicUtf8StringView
0097 {
0098 public:
0099 #ifndef Q_QDOC
0100     using storage_type = typename std::conditional<UseChar8T,
0101             QtPrivate::hide_char8_t,
0102             QtPrivate::wrap_char
0103         >::type::type;
0104 #else
0105     using storage_type = typename QtPrivate::hide_char8_t;
0106 #endif
0107     typedef const storage_type value_type;
0108     typedef qptrdiff difference_type;
0109     typedef qsizetype size_type;
0110     typedef value_type &reference;
0111     typedef value_type &const_reference;
0112     typedef value_type *pointer;
0113     typedef value_type *const_pointer;
0114 
0115     typedef pointer iterator;
0116     typedef const_pointer const_iterator;
0117     typedef std::reverse_iterator<iterator> reverse_iterator;
0118     typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
0119 
0120 private:
0121     template <typename Char>
0122     using if_compatible_char = std::enable_if_t<QtPrivate::IsCompatibleChar8Type<Char>::value, bool>;
0123 
0124     template <typename Pointer>
0125     using if_compatible_pointer = std::enable_if_t<QtPrivate::IsCompatiblePointer8<Pointer>::value, bool>;
0126 
0127     template <typename T>
0128     using if_compatible_qstring_like = std::enable_if_t<std::is_same_v<T, QByteArray>, bool>;
0129 
0130     template <typename T>
0131     using if_compatible_container = std::enable_if_t<QtPrivate::IsContainerCompatibleWithQUtf8StringView<T>::value, bool>;
0132 
0133     template <typename Container>
0134     static constexpr qsizetype lengthHelperContainer(const Container &c) noexcept
0135     {
0136         return qsizetype(std::size(c));
0137     }
0138 
0139     // Note: Do not replace with std::size(const Char (&)[N]), because the result
0140     // will be of by one.
0141     template <typename Char, size_t N>
0142     static constexpr qsizetype lengthHelperContainer(const Char (&str)[N]) noexcept
0143     {
0144         return QtPrivate::lengthHelperContainer(str);
0145     }
0146 
0147     template <typename Char>
0148     static const storage_type *castHelper(const Char *str) noexcept
0149     { return reinterpret_cast<const storage_type*>(str); }
0150     static constexpr const storage_type *castHelper(const storage_type *str) noexcept
0151     { return str; }
0152 
0153 public:
0154     constexpr QBasicUtf8StringView() noexcept
0155         : m_data(nullptr), m_size(0) {}
0156     constexpr QBasicUtf8StringView(std::nullptr_t) noexcept
0157         : QBasicUtf8StringView() {}
0158 
0159     template <typename Char, if_compatible_char<Char> = true>
0160     constexpr QBasicUtf8StringView(const Char *str, qsizetype len)
0161         : m_data(castHelper(str)),
0162           m_size((Q_ASSERT(len >= 0), Q_ASSERT(str || !len), len)) {}
0163 
0164     template <typename Char, if_compatible_char<Char> = true>
0165     constexpr QBasicUtf8StringView(const Char *f, const Char *l)
0166         : QBasicUtf8StringView(f, l - f) {}
0167 
0168 #ifdef Q_QDOC
0169     template <typename Char, size_t N>
0170     constexpr QBasicUtf8StringView(const Char (&array)[N]) noexcept;
0171 
0172     template <typename Char>
0173     constexpr QBasicUtf8StringView(const Char *str) noexcept;
0174 #else
0175     template <typename Pointer, if_compatible_pointer<Pointer> = true>
0176     constexpr QBasicUtf8StringView(const Pointer &str) noexcept
0177         : QBasicUtf8StringView(str, QtPrivate::lengthHelperPointer(str)) {}
0178 
0179     template <typename Char, if_compatible_char<Char> = true>
0180     constexpr QBasicUtf8StringView(const Char (&str)[]) noexcept
0181         : QBasicUtf8StringView(&*str) {} // decay to pointer
0182 #endif
0183 
0184 #ifdef Q_QDOC
0185     QBasicUtf8StringView(const QByteArray &str) noexcept;
0186     constexpr QBasicUtf8StringView(const storage_type *d, qsizetype n) noexcept {};
0187 #else
0188     template <typename String, if_compatible_qstring_like<String> = true>
0189     QBasicUtf8StringView(const String &str) noexcept
0190         : QBasicUtf8StringView{str.begin(), str.size()} {}
0191 #endif
0192 
0193     template <typename Container, if_compatible_container<Container> = true>
0194     constexpr QBasicUtf8StringView(const Container &c) noexcept
0195         : QBasicUtf8StringView(std::data(c), lengthHelperContainer(c)) {}
0196 
0197 #if defined(__cpp_char8_t) && !defined(Q_QDOC)
0198     constexpr QBasicUtf8StringView(QBasicUtf8StringView<!UseChar8T> other)
0199         : QBasicUtf8StringView(other.data(), other.size()) {}
0200 #endif
0201 
0202     template <typename Char, size_t Size, if_compatible_char<Char> = true>
0203     [[nodiscard]] constexpr static QBasicUtf8StringView fromArray(const Char (&string)[Size]) noexcept
0204     { return QBasicUtf8StringView(string, Size); }
0205 
0206     [[nodiscard]] inline QString toString() const; // defined in qstring.h
0207 
0208     [[nodiscard]] constexpr qsizetype size() const noexcept { return m_size; }
0209     [[nodiscard]] constexpr const_pointer data() const noexcept { return m_data; }
0210 #ifdef __cpp_char8_t
0211     [[nodiscard]] const char8_t *utf8() const noexcept { return reinterpret_cast<const char8_t*>(m_data); }
0212 #endif
0213 
0214     [[nodiscard]] constexpr storage_type operator[](qsizetype n) const
0215     { verify(n, 1); return m_data[n]; }
0216 
0217     //
0218     // QString API
0219     //
0220 
0221     [[nodiscard]] constexpr storage_type at(qsizetype n) const { return (*this)[n]; }
0222 
0223     template <typename...Args>
0224     [[nodiscard]] inline QString arg(Args &&...args) const;
0225 
0226     [[nodiscard]]
0227     constexpr QBasicUtf8StringView mid(qsizetype pos, qsizetype n = -1) const
0228     {
0229         using namespace QtPrivate;
0230         auto result = QContainerImplHelper::mid(size(), &pos, &n);
0231         return result == QContainerImplHelper::Null ? QBasicUtf8StringView() : QBasicUtf8StringView(m_data + pos, n);
0232     }
0233     [[nodiscard]]
0234     constexpr QBasicUtf8StringView left(qsizetype n) const
0235     {
0236         if (size_t(n) >= size_t(size()))
0237             n = size();
0238         return QBasicUtf8StringView(m_data, n);
0239     }
0240     [[nodiscard]]
0241     constexpr QBasicUtf8StringView right(qsizetype n) const
0242     {
0243         if (size_t(n) >= size_t(size()))
0244             n = size();
0245         return QBasicUtf8StringView(m_data + m_size - n, n);
0246     }
0247 
0248     [[nodiscard]] constexpr QBasicUtf8StringView sliced(qsizetype pos) const
0249     { verify(pos, 0); return QBasicUtf8StringView{m_data + pos, m_size - pos}; }
0250     [[nodiscard]] constexpr QBasicUtf8StringView sliced(qsizetype pos, qsizetype n) const
0251     { verify(pos, n); return QBasicUtf8StringView(m_data + pos, n); }
0252     [[nodiscard]] constexpr QBasicUtf8StringView first(qsizetype n) const
0253     { verify(0, n); return sliced(0, n); }
0254     [[nodiscard]] constexpr QBasicUtf8StringView last(qsizetype n) const
0255     { verify(0, n); return sliced(m_size - n, n); }
0256     [[nodiscard]] constexpr QBasicUtf8StringView chopped(qsizetype n) const
0257     { verify(0, n); return sliced(0, m_size - n); }
0258 
0259     constexpr QBasicUtf8StringView &slice(qsizetype pos)
0260     { *this = sliced(pos); return *this; }
0261     constexpr QBasicUtf8StringView &slice(qsizetype pos, qsizetype n)
0262     { *this = sliced(pos, n); return *this; }
0263 
0264     constexpr void truncate(qsizetype n)
0265     { verify(0, n); m_size = n; }
0266     constexpr void chop(qsizetype n)
0267     { verify(0, n); m_size -= n; }
0268 
0269     [[nodiscard]] inline bool isValidUtf8() const noexcept
0270     {
0271         return QByteArrayView(reinterpret_cast<const char *>(data()), size()).isValidUtf8();
0272     }
0273 
0274     //
0275     // STL compatibility API:
0276     //
0277     [[nodiscard]] const_iterator begin()   const noexcept { return data(); }
0278     [[nodiscard]] const_iterator end()     const noexcept { return data() + size(); }
0279     [[nodiscard]] const_iterator cbegin()  const noexcept { return begin(); }
0280     [[nodiscard]] const_iterator cend()    const noexcept { return end(); }
0281     [[nodiscard]] const_reverse_iterator rbegin()  const noexcept { return const_reverse_iterator(end()); }
0282     [[nodiscard]] const_reverse_iterator rend()    const noexcept { return const_reverse_iterator(begin()); }
0283     [[nodiscard]] const_reverse_iterator crbegin() const noexcept { return rbegin(); }
0284     [[nodiscard]] const_reverse_iterator crend()   const noexcept { return rend(); }
0285 
0286     [[nodiscard]] constexpr bool empty() const noexcept { return size() == 0; }
0287     [[nodiscard]] constexpr storage_type front() const { return Q_ASSERT(!empty()), m_data[0]; }
0288     [[nodiscard]] constexpr storage_type back()  const { return Q_ASSERT(!empty()), m_data[m_size - 1]; }
0289 
0290     [[nodiscard]] Q_IMPLICIT operator std::string_view() const noexcept
0291     { return std::string_view{reinterpret_cast<const char*>(data()), size_t(size())}; }
0292 
0293 #ifdef __cpp_lib_char8_t
0294     [[nodiscard]] Q_IMPLICIT operator std::u8string_view() const noexcept
0295     { return std::u8string_view{utf8(), size_t(size())}; }
0296 #endif
0297 
0298     [[nodiscard]] constexpr qsizetype max_size() const noexcept { return maxSize(); }
0299 
0300     //
0301     // Qt compatibility API:
0302     //
0303     [[nodiscard]] constexpr bool isNull() const noexcept { return !m_data; }
0304     [[nodiscard]] constexpr bool isEmpty() const noexcept { return empty(); }
0305     [[nodiscard]] constexpr qsizetype length() const noexcept
0306     { return size(); }
0307 
0308     [[nodiscard]] int compare(QBasicUtf8StringView other,
0309                               Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
0310     {
0311         return QtPrivate::compareStrings(*this, other, cs);
0312     }
0313 
0314     // all defined in qstring.h
0315     [[nodiscard]] inline int compare(QChar other,
0316                                      Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
0317     [[nodiscard]] inline int compare(QStringView other,
0318                                      Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
0319     [[nodiscard]] inline int compare(QLatin1StringView other,
0320                                      Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
0321     [[nodiscard]] inline int compare(const QByteArray &other,
0322                                      Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
0323 
0324     [[nodiscard]] inline bool equal(QChar other) const noexcept;
0325     [[nodiscard]] inline bool equal(QStringView other) const noexcept;
0326     [[nodiscard]] inline bool equal(QLatin1StringView other) const noexcept;
0327     [[nodiscard]] inline bool equal(const QByteArray &other) const noexcept;
0328     // end defined in qstring.h
0329 
0330     [[nodiscard]] static constexpr qsizetype maxSize() noexcept
0331     {
0332         // -1 to deal with the pointer one-past-the-end;
0333         return QtPrivate::MaxAllocSize - 1;
0334     }
0335 
0336 private:
0337     [[nodiscard]] static inline int compare(QBasicUtf8StringView lhs, QBasicUtf8StringView rhs) noexcept
0338     {
0339         return QtPrivate::compareStrings(QBasicUtf8StringView<false>(lhs.data(), lhs.size()),
0340                                          QBasicUtf8StringView<false>(rhs.data(), rhs.size()));
0341     }
0342 
0343     friend bool
0344     comparesEqual(const QBasicUtf8StringView &lhs, const QBasicUtf8StringView &rhs) noexcept
0345     {
0346         return lhs.size() == rhs.size()
0347                 && QtPrivate::equalStrings(QBasicUtf8StringView<false>(lhs.data(), lhs.size()),
0348                                            QBasicUtf8StringView<false>(rhs.data(), rhs.size()));
0349     }
0350     friend Qt::strong_ordering
0351     compareThreeWay(const QBasicUtf8StringView &lhs, const QBasicUtf8StringView &rhs) noexcept
0352     {
0353         const int res = QBasicUtf8StringView::compare(lhs, rhs);
0354         return Qt::compareThreeWay(res, 0);
0355     }
0356     Q_DECLARE_STRONGLY_ORDERED(QBasicUtf8StringView)
0357 
0358     friend bool
0359     comparesEqual(const QBasicUtf8StringView &lhs, const QLatin1StringView &rhs) noexcept
0360     {
0361         return lhs.equal(rhs);
0362     }
0363     friend Qt::strong_ordering
0364     compareThreeWay(const QBasicUtf8StringView &lhs, const QLatin1StringView &rhs) noexcept
0365     {
0366         const int res = lhs.compare(rhs);
0367         return Qt::compareThreeWay(res, 0);
0368     }
0369     Q_DECLARE_STRONGLY_ORDERED(QBasicUtf8StringView, QLatin1StringView)
0370 
0371     friend bool
0372     comparesEqual(const QBasicUtf8StringView &lhs, const QStringView &rhs) noexcept
0373     { return lhs.equal(rhs); }
0374     friend Qt::strong_ordering
0375     compareThreeWay(const QBasicUtf8StringView &lhs, const QStringView &rhs) noexcept
0376     {
0377         const int res = lhs.compare(rhs);
0378         return Qt::compareThreeWay(res, 0);
0379     }
0380     Q_DECLARE_STRONGLY_ORDERED(QBasicUtf8StringView, QStringView)
0381 
0382     friend bool comparesEqual(const QBasicUtf8StringView &lhs, const QChar &rhs) noexcept
0383     { return lhs.equal(rhs); }
0384     friend Qt::strong_ordering
0385     compareThreeWay(const QBasicUtf8StringView &lhs, const QChar &rhs) noexcept
0386     {
0387         const int res = lhs.compare(rhs);
0388         return Qt::compareThreeWay(res, 0);
0389     }
0390     Q_DECLARE_STRONGLY_ORDERED(QBasicUtf8StringView, QChar)
0391     Q_DECLARE_STRONGLY_ORDERED(QBasicUtf8StringView, char16_t)
0392 
0393 #if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
0394     friend bool
0395     comparesEqual(const QBasicUtf8StringView &lhs, const QByteArrayView &rhs) noexcept
0396     {
0397         return lhs.size() == rhs.size()
0398                 && QtPrivate::equalStrings(QBasicUtf8StringView<false>(lhs.data(), lhs.size()),
0399                                            QBasicUtf8StringView<false>(rhs.data(), rhs.size()));
0400     }
0401     friend Qt::strong_ordering
0402     compareThreeWay(const QBasicUtf8StringView &lhs, const QByteArrayView &rhs) noexcept
0403     {
0404         const int res = QtPrivate::compareStrings(QBasicUtf8StringView<false>(lhs.data(), lhs.size()),
0405                                                   QBasicUtf8StringView<false>(rhs.data(), rhs.size()));
0406         return Qt::compareThreeWay(res, 0);
0407     }
0408     Q_DECLARE_STRONGLY_ORDERED(QBasicUtf8StringView, QByteArrayView, QT_ASCII_CAST_WARN)
0409 
0410     friend bool
0411     comparesEqual(const QBasicUtf8StringView &lhs, const QByteArray &rhs) noexcept
0412     {
0413         return lhs.equal(rhs);
0414     }
0415     friend Qt::strong_ordering
0416     compareThreeWay(const QBasicUtf8StringView &lhs, const QByteArray &rhs) noexcept
0417     {
0418         const int res = lhs.compare(rhs);
0419         return Qt::compareThreeWay(res, 0);
0420     }
0421     Q_DECLARE_STRONGLY_ORDERED(QBasicUtf8StringView, QByteArray, QT_ASCII_CAST_WARN)
0422 
0423     friend bool comparesEqual(const QBasicUtf8StringView &lhs, const char *rhs) noexcept
0424     { return comparesEqual(lhs, QByteArrayView(rhs)); }
0425     friend Qt::strong_ordering
0426     compareThreeWay(const QBasicUtf8StringView &lhs, const char *rhs) noexcept
0427     { return compareThreeWay(lhs, QByteArrayView(rhs)); }
0428     Q_DECLARE_STRONGLY_ORDERED(QBasicUtf8StringView, const char *, QT_ASCII_CAST_WARN)
0429 #endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
0430 
0431     Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos = 0,
0432                                           [[maybe_unused]] qsizetype n = 1) const
0433     {
0434         Q_ASSERT(pos >= 0);
0435         Q_ASSERT(pos <= size());
0436         Q_ASSERT(n >= 0);
0437         Q_ASSERT(n <= size() - pos);
0438     }
0439     const storage_type *m_data;
0440     qsizetype m_size;
0441 };
0442 
0443 #ifdef Q_QDOC
0444 #undef QBasicUtf8StringView
0445 #else
0446 template <bool UseChar8T>
0447 Q_DECLARE_TYPEINFO_BODY(QBasicUtf8StringView<UseChar8T>, Q_PRIMITIVE_TYPE);
0448 
0449 template <typename QStringLike, std::enable_if_t<std::is_same_v<QStringLike, QByteArray>, bool> = true>
0450 [[nodiscard]] inline q_no_char8_t::QUtf8StringView qToUtf8StringViewIgnoringNull(const QStringLike &s) noexcept
0451 { return q_no_char8_t::QUtf8StringView(s.begin(), s.size()); }
0452 #endif // Q_QDOC
0453 
0454 QT_END_NAMESPACE
0455 
0456 #endif /* QUTF8STRINGVIEW_H */