Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 09:26:52

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