1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com> |
4 | ** Contact: http://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtCore module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | #ifndef QUTF8STRINGVIEW_H |
40 | #define QUTF8STRINGVIEW_H |
41 | |
42 | #include <QtCore/qstringalgorithms.h> |
43 | #include <QtCore/qarraydata.h> // for QContainerImplHelper |
44 | |
45 | #include <string> |
46 | |
47 | QT_BEGIN_NAMESPACE |
48 | |
49 | template <bool> class QBasicUtf8StringView; |
50 | class QByteArray; |
51 | class QLatin1String; |
52 | |
53 | namespace QtPrivate { |
54 | template <typename Char> |
55 | using IsCompatibleChar8TypeHelper = std::disjunction< |
56 | #ifdef __cpp_char8_t |
57 | std::is_same<Char, char8_t>, |
58 | #endif |
59 | std::is_same<Char, char>, |
60 | std::is_same<Char, uchar>, |
61 | std::is_same<Char, signed char> |
62 | >; |
63 | template <typename Char> |
64 | using IsCompatibleChar8Type |
65 | = IsCompatibleChar8TypeHelper<std::remove_cv_t<std::remove_reference_t<Char>>>; |
66 | |
67 | template <typename Pointer> |
68 | struct IsCompatiblePointer8Helper : std::false_type {}; |
69 | template <typename Char> |
70 | struct IsCompatiblePointer8Helper<Char*> |
71 | : IsCompatibleChar8Type<Char> {}; |
72 | template <typename Pointer> |
73 | using IsCompatiblePointer8 |
74 | = IsCompatiblePointer8Helper<std::remove_cv_t<std::remove_reference_t<Pointer>>>; |
75 | |
76 | template <typename T, typename Enable = void> |
77 | struct IsContainerCompatibleWithQUtf8StringView : std::false_type {}; |
78 | |
79 | template <typename T> |
80 | struct IsContainerCompatibleWithQUtf8StringView<T, std::enable_if_t<std::conjunction_v< |
81 | // lacking concepts and ranges, we accept any T whose std::data yields a suitable pointer ... |
82 | IsCompatiblePointer8<decltype(std::data(std::declval<const T &>()))>, |
83 | // ... and that has a suitable size ... |
84 | std::is_convertible< |
85 | decltype(std::size(std::declval<const T &>())), |
86 | qsizetype |
87 | >, |
88 | // ... and it's a range as it defines an iterator-like API |
89 | IsCompatibleChar8Type<typename std::iterator_traits< |
90 | decltype(std::begin(std::declval<const T &>()))>::value_type |
91 | >, |
92 | std::is_convertible< |
93 | decltype( std::begin(std::declval<const T &>()) != std::end(std::declval<const T &>()) ), |
94 | bool |
95 | >, |
96 | |
97 | // This needs to be treated specially due to the empty vs null distinction |
98 | std::negation<std::is_same<std::decay_t<T>, QByteArray>>, |
99 | |
100 | // This has a compatible value_type, but explicitly a different encoding |
101 | std::negation<std::is_same<std::decay_t<T>, QLatin1String>>, |
102 | |
103 | // Don't make an accidental copy constructor |
104 | std::negation<std::disjunction< |
105 | std::is_same<std::decay_t<T>, QBasicUtf8StringView<true>>, |
106 | std::is_same<std::decay_t<T>, QBasicUtf8StringView<false>> |
107 | >> |
108 | >>> : std::true_type {}; |
109 | |
110 | struct hide_char8_t { |
111 | #ifdef __cpp_char8_t |
112 | using type = char8_t; |
113 | #endif |
114 | }; |
115 | |
116 | struct wrap_char { using type = char; }; |
117 | |
118 | } // namespace QtPrivate |
119 | |
120 | template <bool UseChar8T> |
121 | class QBasicUtf8StringView |
122 | { |
123 | public: |
124 | using storage_type = typename std::conditional<UseChar8T, |
125 | QtPrivate::hide_char8_t, |
126 | QtPrivate::wrap_char |
127 | >::type::type; |
128 | typedef const storage_type value_type; |
129 | typedef qptrdiff difference_type; |
130 | typedef qsizetype size_type; |
131 | typedef value_type &reference; |
132 | typedef value_type &const_reference; |
133 | typedef value_type *pointer; |
134 | typedef value_type *const_pointer; |
135 | |
136 | typedef pointer iterator; |
137 | typedef const_pointer const_iterator; |
138 | typedef std::reverse_iterator<iterator> reverse_iterator; |
139 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
140 | |
141 | private: |
142 | template <typename Char> |
143 | using if_compatible_char = std::enable_if_t<QtPrivate::IsCompatibleChar8Type<Char>::value, bool>; |
144 | |
145 | template <typename Pointer> |
146 | using if_compatible_pointer = std::enable_if_t<QtPrivate::IsCompatiblePointer8<Pointer>::value, bool>; |
147 | |
148 | template <typename T> |
149 | using if_compatible_qstring_like = std::enable_if_t<std::is_same_v<T, QByteArray>, bool>; |
150 | |
151 | template <typename T> |
152 | using if_compatible_container = std::enable_if_t<QtPrivate::IsContainerCompatibleWithQUtf8StringView<T>::value, bool>; |
153 | |
154 | template <typename Container> |
155 | static constexpr qsizetype lengthHelperContainer(const Container &c) noexcept |
156 | { |
157 | return qsizetype(std::size(c)); |
158 | } |
159 | |
160 | // Note: Do not replace with std::size(const Char (&)[N]), cause the result |
161 | // will be of by one. |
162 | template <typename Char, size_t N> |
163 | static constexpr qsizetype lengthHelperContainer(const Char (&str)[N]) noexcept |
164 | { |
165 | const auto it = std::char_traits<Char>::find(str, N, Char(0)); |
166 | const auto end = it ? it : std::next(str, N); |
167 | return qsizetype(std::distance(str, end)); |
168 | } |
169 | |
170 | template <typename Char> |
171 | static const storage_type *castHelper(const Char *str) noexcept |
172 | { return reinterpret_cast<const storage_type*>(str); } |
173 | static constexpr const storage_type *castHelper(const storage_type *str) noexcept |
174 | { return str; } |
175 | |
176 | public: |
177 | constexpr QBasicUtf8StringView() noexcept |
178 | : m_data(nullptr), m_size(0) {} |
179 | constexpr QBasicUtf8StringView(std::nullptr_t) noexcept |
180 | : QBasicUtf8StringView() {} |
181 | |
182 | template <typename Char, if_compatible_char<Char> = true> |
183 | constexpr QBasicUtf8StringView(const Char *str, qsizetype len) |
184 | : m_data(castHelper(str)), |
185 | m_size((Q_ASSERT(len >= 0), Q_ASSERT(str || !len), len)) {} |
186 | |
187 | template <typename Char, if_compatible_char<Char> = true> |
188 | constexpr QBasicUtf8StringView(const Char *f, const Char *l) |
189 | : QBasicUtf8StringView(f, l - f) {} |
190 | |
191 | #ifdef Q_CLANG_QDOC |
192 | template <typename Char, size_t N> |
193 | constexpr QBasicUtf8StringView(const Char (&array)[N]) noexcept; |
194 | |
195 | template <typename Char> |
196 | constexpr QBasicUtf8StringView(const Char *str) noexcept; |
197 | #else |
198 | template <typename Pointer, if_compatible_pointer<Pointer> = true> |
199 | constexpr QBasicUtf8StringView(const Pointer &str) noexcept |
200 | : QBasicUtf8StringView(str, |
201 | str ? std::char_traits<std::remove_cv_t<std::remove_pointer_t<Pointer>>>::length(str) : 0) {} |
202 | #endif |
203 | |
204 | #ifdef Q_CLANG_QDOC |
205 | QBasicUtf8StringView(const QByteArray &str) noexcept; |
206 | #else |
207 | template <typename String, if_compatible_qstring_like<String> = true> |
208 | QBasicUtf8StringView(const String &str) noexcept |
209 | : QBasicUtf8StringView(str.isNull() ? nullptr : str.data(), qsizetype(str.size())) {} |
210 | #endif |
211 | |
212 | template <typename Container, if_compatible_container<Container> = true> |
213 | constexpr QBasicUtf8StringView(const Container &c) noexcept |
214 | : QBasicUtf8StringView(std::data(c), lengthHelperContainer(c)) {} |
215 | |
216 | #ifdef __cpp_char8_t |
217 | constexpr QBasicUtf8StringView(QBasicUtf8StringView<!UseChar8T> other) |
218 | : QBasicUtf8StringView(other.data(), other.size()) {} |
219 | #endif |
220 | |
221 | template <typename Char, size_t Size, if_compatible_char<Char> = true> |
222 | [[nodiscard]] constexpr static QBasicUtf8StringView fromArray(const Char (&string)[Size]) noexcept |
223 | { return QBasicUtf8StringView(string, Size); } |
224 | |
225 | [[nodiscard]] inline QString toString() const; // defined in qstring.h |
226 | |
227 | [[nodiscard]] constexpr qsizetype size() const noexcept { return m_size; } |
228 | [[nodiscard]] const_pointer data() const noexcept { return reinterpret_cast<const_pointer>(m_data); } |
229 | #ifdef __cpp_char8_t |
230 | [[nodiscard]] const char8_t *utf8() const noexcept { return reinterpret_cast<const char8_t*>(m_data); } |
231 | #endif |
232 | |
233 | [[nodiscard]] constexpr storage_type operator[](qsizetype n) const |
234 | { return Q_ASSERT(n >= 0), Q_ASSERT(n < size()), m_data[n]; } |
235 | |
236 | // |
237 | // QString API |
238 | // |
239 | |
240 | [[nodiscard]] constexpr storage_type at(qsizetype n) const { return (*this)[n]; } |
241 | |
242 | [[nodiscard]] |
243 | constexpr QBasicUtf8StringView mid(qsizetype pos, qsizetype n = -1) const |
244 | { |
245 | using namespace QtPrivate; |
246 | auto result = QContainerImplHelper::mid(size(), &pos, &n); |
247 | return result == QContainerImplHelper::Null ? QBasicUtf8StringView() : QBasicUtf8StringView(m_data + pos, n); |
248 | } |
249 | [[nodiscard]] |
250 | constexpr QBasicUtf8StringView left(qsizetype n) const |
251 | { |
252 | if (size_t(n) >= size_t(size())) |
253 | n = size(); |
254 | return QBasicUtf8StringView(m_data, n); |
255 | } |
256 | [[nodiscard]] |
257 | constexpr QBasicUtf8StringView right(qsizetype n) const |
258 | { |
259 | if (size_t(n) >= size_t(size())) |
260 | n = size(); |
261 | return QBasicUtf8StringView(m_data + m_size - n, n); |
262 | } |
263 | |
264 | [[nodiscard]] constexpr QBasicUtf8StringView sliced(qsizetype pos) const |
265 | { verify(pos); return QBasicUtf8StringView{m_data + pos, m_size - pos}; } |
266 | [[nodiscard]] constexpr QBasicUtf8StringView sliced(qsizetype pos, qsizetype n) const |
267 | { verify(pos, n); return QBasicUtf8StringView(m_data + pos, n); } |
268 | [[nodiscard]] constexpr QBasicUtf8StringView first(qsizetype n) const |
269 | { verify(n); return QBasicUtf8StringView(m_data, n); } |
270 | [[nodiscard]] constexpr QBasicUtf8StringView last(qsizetype n) const |
271 | { verify(n); return QBasicUtf8StringView(m_data + m_size - n, n); } |
272 | [[nodiscard]] constexpr QBasicUtf8StringView chopped(qsizetype n) const |
273 | { verify(n); return QBasicUtf8StringView(m_data, m_size - n); } |
274 | |
275 | constexpr void truncate(qsizetype n) |
276 | { verify(n); m_size = n; } |
277 | constexpr void chop(qsizetype n) |
278 | { verify(n); m_size -= n; } |
279 | |
280 | // |
281 | // STL compatibility API: |
282 | // |
283 | [[nodiscard]] const_iterator begin() const noexcept { return data(); } |
284 | [[nodiscard]] const_iterator end() const noexcept { return data() + size(); } |
285 | [[nodiscard]] const_iterator cbegin() const noexcept { return begin(); } |
286 | [[nodiscard]] const_iterator cend() const noexcept { return end(); } |
287 | [[nodiscard]] const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); } |
288 | [[nodiscard]] const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); } |
289 | [[nodiscard]] const_reverse_iterator crbegin() const noexcept { return rbegin(); } |
290 | [[nodiscard]] const_reverse_iterator crend() const noexcept { return rend(); } |
291 | |
292 | [[nodiscard]] constexpr bool empty() const noexcept { return size() == 0; } |
293 | [[nodiscard]] constexpr storage_type front() const { return Q_ASSERT(!empty()), m_data[0]; } |
294 | [[nodiscard]] constexpr storage_type back() const { return Q_ASSERT(!empty()), m_data[m_size - 1]; } |
295 | |
296 | // |
297 | // Qt compatibility API: |
298 | // |
299 | [[nodiscard]] constexpr bool isNull() const noexcept { return !m_data; } |
300 | [[nodiscard]] constexpr bool isEmpty() const noexcept { return empty(); } |
301 | #if QT_DEPRECATED_SINCE(6, 0) |
302 | [[nodiscard]] |
303 | Q_DECL_DEPRECATED_X("Use size() and port callers to qsizetype." ) |
304 | constexpr int length() const /* not nothrow! */ |
305 | { return Q_ASSERT(int(size()) == size()), int(size()); } |
306 | #endif |
307 | |
308 | private: |
309 | [[nodiscard]] static inline int compare(QBasicUtf8StringView lhs, QBasicUtf8StringView rhs) noexcept |
310 | { |
311 | return QtPrivate::compareStrings(QBasicUtf8StringView<false>(lhs.data(), lhs.size()), |
312 | QBasicUtf8StringView<false>(rhs.data(), rhs.size())); |
313 | } |
314 | |
315 | [[nodiscard]] friend inline bool operator==(QBasicUtf8StringView lhs, QBasicUtf8StringView rhs) noexcept |
316 | { |
317 | return QtPrivate::equalStrings(QBasicUtf8StringView<false>(lhs.data(), lhs.size()), |
318 | QBasicUtf8StringView<false>(rhs.data(), rhs.size())); |
319 | } |
320 | [[nodiscard]] friend inline bool operator!=(QBasicUtf8StringView lhs, QBasicUtf8StringView rhs) noexcept |
321 | { return !operator==(lhs, rhs); } |
322 | |
323 | #ifdef __cpp_impl_three_way_comparison |
324 | [[nodiscard]] friend inline auto operator<=>(QBasicUtf8StringView lhs, QBasicUtf8StringView rhs) noexcept |
325 | { return QBasicUtf8StringView::compare(lhs, rhs) <=> 0; } |
326 | #else |
327 | [[nodiscard]] friend inline bool operator<=(QBasicUtf8StringView lhs, QBasicUtf8StringView rhs) noexcept |
328 | { return QBasicUtf8StringView::compare(lhs, rhs) <= 0; } |
329 | [[nodiscard]] friend inline bool operator>=(QBasicUtf8StringView lhs, QBasicUtf8StringView rhs) noexcept |
330 | { return QBasicUtf8StringView::compare(lhs, rhs) >= 0; } |
331 | [[nodiscard]] friend inline bool operator<(QBasicUtf8StringView lhs, QBasicUtf8StringView rhs) noexcept |
332 | { return QBasicUtf8StringView::compare(lhs, rhs) < 0; } |
333 | [[nodiscard]] friend inline bool operator>(QBasicUtf8StringView lhs, QBasicUtf8StringView rhs) noexcept |
334 | { return QBasicUtf8StringView::compare(lhs, rhs) > 0; } |
335 | #endif |
336 | |
337 | Q_ALWAYS_INLINE constexpr void verify(qsizetype pos, qsizetype n = 0) const |
338 | { |
339 | Q_ASSERT(pos >= 0); |
340 | Q_ASSERT(pos <= size()); |
341 | Q_ASSERT(n >= 0); |
342 | Q_ASSERT(n <= size() - pos); |
343 | } |
344 | const storage_type *m_data; |
345 | qsizetype m_size; |
346 | }; |
347 | template <bool UseChar8T> |
348 | Q_DECLARE_TYPEINFO_BODY(QBasicUtf8StringView<UseChar8T>, Q_PRIMITIVE_TYPE); |
349 | |
350 | QT_BEGIN_NO_CHAR8_T_NAMESPACE |
351 | using QUtf8StringView = QBasicUtf8StringView<false>; |
352 | QT_END_NO_CHAR8_T_NAMESPACE |
353 | |
354 | QT_BEGIN_HAS_CHAR8_T_NAMESPACE |
355 | using QUtf8StringView = QBasicUtf8StringView<true>; |
356 | QT_END_HAS_CHAR8_T_NAMESPACE |
357 | |
358 | template <typename QStringLike, std::enable_if_t<std::is_same_v<QStringLike, QByteArray>, bool> = true> |
359 | [[nodiscard]] inline q_no_char8_t::QUtf8StringView qToUtf8StringViewIgnoringNull(const QStringLike &s) noexcept |
360 | { return q_no_char8_t::QUtf8StringView(s.data(), s.size()); } |
361 | |
362 | QT_END_NAMESPACE |
363 | |
364 | #endif /* QUTF8STRINGVIEW_H */ |
365 | |