Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qanystringview.h
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com>
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4#ifndef QANYSTRINGVIEW_H
5#define QANYSTRINGVIEW_H
6
7#include <QtCore/qcompare.h>
8#include <QtCore/qcontainerfwd.h>
9#include <QtCore/qlatin1stringview.h>
10#include <QtCore/qstringview.h>
11#include <QtCore/qutf8stringview.h>
12
13#include <QtCore/q20type_traits.h>
14#include <limits>
15
16class tst_QAnyStringView;
17
18QT_BEGIN_NAMESPACE
19
20namespace QtPrivate {
21
22template <typename Tag, typename Result>
23struct wrapped { using type = Result; };
24
25template <typename Tag, typename Result>
26using wrapped_t = typename wrapped<Tag, Result>::type;
27
28} // namespace QtPrivate
29
31{
32public:
35private:
36 static constexpr size_t SizeMask = (std::numeric_limits<size_t>::max)() / 4;
37#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED)
38 static constexpr int SizeShift = 2;
39 static constexpr size_t Latin1Flag = 1;
40#else
41 static constexpr int SizeShift = 0;
42 static constexpr size_t Latin1Flag = SizeMask + 1;
43#endif
44 static constexpr size_t TwoByteCodePointFlag = Latin1Flag << 1;
45 static constexpr size_t TypeMask = ~(SizeMask << SizeShift);
46 static_assert(TypeMask == (Latin1Flag|TwoByteCodePointFlag));
47
48 // Tag bits
49 // 0 0 Utf8
50 // 0 1 Latin1
51 // 1 0 Utf16
52 // 1 1 Unused
53 // ^ ^ latin1
54 // | sizeof code-point == 2
55 enum Tag : size_t {
56 Utf8 = 0,
57 Latin1 = Latin1Flag,
58 Utf16 = TwoByteCodePointFlag,
59 Unused = TypeMask,
60 };
61
62 template <typename Char>
66 >;
67
68 template <typename Char>
70
71 template <typename Pointer>
75 >, bool>;
76
77
78 template <typename T>
82 >, bool>;
83
84 template <typename QStringOrQByteArray, typename T>
86 // need to exclude a bunch of stuff, because we take by universal reference:
89 std::is_same<q20::remove_cvref_t<T>, QAnyStringView>, // don't make a copy/move ctor
90 std::is_pointer<std::decay_t<T>>, // const char*, etc
91 is_compatible_char<T>, // don't create a QString/QByteArray, we have a ctor
94 >>,
95 // this is what we're really after:
97 >, bool>;
98
99 // confirm we don't make an accidental copy constructor:
102
103 template<typename Char>
104 static constexpr bool isAsciiOnlyCharsAtCompileTime(Char *str, qsizetype sz) noexcept
105 {
106 // do not perform check if not at compile time
107 if (!q20::is_constant_evaluated())
108 return false;
109 if constexpr (sizeof(Char) != sizeof(char)) {
110 Q_UNUSED(str);
111 Q_UNUSED(sz);
112 return false;
113 } else {
114 for (qsizetype i = 0; i < sz; ++i) {
115 if (uchar(str[i]) > 0x7f)
116 return false;
117 }
118 return true;
119 }
120 }
121
122 template<typename Char>
123 static constexpr std::size_t encodeType(const Char *str, qsizetype sz) noexcept
124 {
125 // Utf16 if 16 bit, Latin1 if ASCII, else Utf8
126 Q_ASSERT(sz >= 0);
127 Q_ASSERT(sz <= qsizetype(SizeMask));
128 Q_ASSERT(str || !sz);
129 return (std::size_t(sz) << SizeShift)
130 | uint(sizeof(Char) == sizeof(char16_t)) * Tag::Utf16
131 | uint(isAsciiOnlyCharsAtCompileTime(str, sz)) * Tag::Latin1;
132 }
133
134 template <typename Char>
135 static constexpr qsizetype lengthHelperPointer(const Char *str) noexcept
136 {
137 if (q20::is_constant_evaluated())
138 return QtPrivate::lengthHelperPointer(str);
139 if constexpr (sizeof(Char) == sizeof(char16_t))
140 return QtPrivate::qustrlen(reinterpret_cast<const char16_t*>(str));
141 else
142 return qsizetype(strlen(reinterpret_cast<const char*>(str)));
143 }
144
145 static QChar toQChar(char ch) noexcept { return toQChar(QLatin1Char{ch}); } // we don't handle UTF-8 multibytes
146 static QChar toQChar(QChar ch) noexcept { return ch; }
147 static QChar toQChar(QLatin1Char ch) noexcept { return ch; }
148
149 struct QCharContainer { // private, so users can't pass their own
150 explicit QCharContainer() = default;
151 QChar ch;
152 };
153
154 template <typename Char>
155 static constexpr QAnyStringView fromCharInternal(const Char &ch) noexcept
156 {
157 if constexpr (sizeof ch == 1) // even char8_t is Latin-1 as single octet
158 return QAnyStringView{&ch, 1, size_t{Tag::Latin1}};
159 else // sizeof ch == 2
160 return {&ch, 1};
161 }
162
163 explicit constexpr QAnyStringView(const void *d, qsizetype n, std::size_t sizeAndType) noexcept
164 : m_data{d}, m_size{std::size_t(n) | (sizeAndType & TypeMask)} {}
165public:
166 constexpr QAnyStringView() noexcept
167 : m_data{nullptr}, m_size{0} {}
168 constexpr QAnyStringView(std::nullptr_t) noexcept
170
171 template <typename Char, if_compatible_char<Char> = true>
172 constexpr QAnyStringView(const Char *str, qsizetype len)
173 : m_data{str}, m_size{encodeType<Char>(str, len)}
174 {
175 }
176
177 template <typename Char, if_compatible_char<Char> = true>
178 constexpr QAnyStringView(const Char *f, const Char *l)
179 : QAnyStringView(f, l - f) {}
180
181#ifdef Q_QDOC
182 template <typename Char, size_t N>
183 constexpr QAnyStringView(const Char (&array)[N]) noexcept;
184
185 template <typename Char>
186 constexpr QAnyStringView(const Char *str) noexcept;
187#else
188 template <typename Pointer, if_compatible_pointer<Pointer> = true>
189 constexpr QAnyStringView(const Pointer &str) noexcept
190 : QAnyStringView{str, str ? lengthHelperPointer(str) : 0} {}
191
192 template <typename Char, if_compatible_char<Char> = true>
193 constexpr QAnyStringView(const Char (&str)[]) noexcept
194 : QAnyStringView{&*str} {} // decay to pointer
195#endif
196
197 // defined in qstring.h
198 inline QAnyStringView(const QByteArray &str) noexcept; // TODO: Should we have this at all? Remove?
199 inline QAnyStringView(const QString &str) noexcept;
200 inline constexpr QAnyStringView(QLatin1StringView str) noexcept;
201
202 template <typename Container, if_compatible_container<Container> = true>
205
206 template <typename Container, if_convertible_to<QString, Container> = true>
207 constexpr QAnyStringView(Container &&c, QtPrivate::wrapped_t<Container, QString> &&capacity = {})
208 //noexcept(std::is_nothrow_constructible_v<QString, Container>)
210
211 template <typename Container, if_convertible_to<QByteArray, Container> = true>
212 constexpr QAnyStringView(Container &&c, QtPrivate::wrapped_t<Container, QByteArray> &&capacity = {})
213 //noexcept(std::is_nothrow_constructible_v<QByteArray, Container>)
215
216 template <typename Char, if_compatible_char<Char> = true>
217 constexpr QAnyStringView(const Char &c) noexcept
218 : QAnyStringView{fromCharInternal(c)} {}
219 template <typename Char, if_convertible_to<QChar, Char> = true>
220 constexpr QAnyStringView(Char ch, QCharContainer &&capacity = QCharContainer()) noexcept
221 : QAnyStringView{&(capacity.ch = ch), 1} {}
222 template <typename Char, typename Container = decltype(QChar::fromUcs4(U'x')),
223 std::enable_if_t<std::is_same_v<Char, char32_t>, bool> = true>
224 constexpr QAnyStringView(Char c, Container &&capacity = {}) noexcept
226
229
230 template <bool UseChar8T>
233
234 template <typename Char, size_t Size, if_compatible_char<Char> = true>
235 [[nodiscard]] constexpr static QAnyStringView fromArray(const Char (&string)[Size]) noexcept
236 { return QAnyStringView(string, Size); }
237
238 // defined in qstring.h:
239 template <typename Visitor>
240 inline constexpr decltype(auto) visit(Visitor &&v) const;
241
242 [[nodiscard]]
243 constexpr QAnyStringView 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 ? QAnyStringView() : sliced(pos, n);
248 }
249 [[nodiscard]]
250 constexpr QAnyStringView left(qsizetype n) const
251 {
252 if (size_t(n) >= size_t(size()))
253 n = size();
254 return sliced(0, n);
255 }
256 [[nodiscard]]
257 constexpr QAnyStringView right(qsizetype n) const
258 {
259 if (size_t(n) >= size_t(size()))
260 n = size();
261 return sliced(size() - n, n);
262 }
263
264 [[nodiscard]] constexpr QAnyStringView sliced(qsizetype pos) const
265 { verify(pos, 0); auto r = *this; r.advanceData(pos); r.setSize(size() - pos); return r; }
266 [[nodiscard]] constexpr QAnyStringView sliced(qsizetype pos, qsizetype n) const
267 { verify(pos, n); auto r = *this; r.advanceData(pos); r.setSize(n); return r; }
268 [[nodiscard]] constexpr QAnyStringView first(qsizetype n) const
269 { verify(0, n); return sliced(0, n); }
270 [[nodiscard]] constexpr QAnyStringView last(qsizetype n) const
271 { verify(0, n); return sliced(size() - n, n); }
272 [[nodiscard]] constexpr QAnyStringView chopped(qsizetype n) const
273 { verify(0, n); return sliced(0, size() - n); }
274
275 constexpr QAnyStringView &slice(qsizetype pos)
276 { *this = sliced(pos); return *this; }
277 constexpr QAnyStringView &slice(qsizetype pos, qsizetype n)
278 { *this = sliced(pos, n); return *this; }
279
280 constexpr void truncate(qsizetype n)
281 { verify(0, n); setSize(n); }
282 constexpr void chop(qsizetype n)
283 { verify(0, n); setSize(size() - n); }
284
285 template <typename...Args>
286 [[nodiscard]] inline QString arg(Args &&...args) const;
287
288 [[nodiscard]] inline QString toString() const; // defined in qstring.h
289
290 [[nodiscard]] constexpr qsizetype size() const noexcept
291 { return qsizetype((m_size >> SizeShift) & SizeMask); }
292 [[nodiscard]] constexpr const void *data() const noexcept { return m_data; }
293
294 [[nodiscard]] Q_CORE_EXPORT static int compare(QAnyStringView lhs, QAnyStringView rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
295 [[nodiscard]] Q_CORE_EXPORT static bool equal(QAnyStringView lhs, QAnyStringView rhs) noexcept;
296
297 static constexpr inline bool detects_US_ASCII_at_compile_time =
298#ifdef QT_SUPPORTS_IS_CONSTANT_EVALUATED
299 true
300#else
301 false
302#endif
303 ;
304
305 //
306 // STL compatibility API:
307 //
308 [[nodiscard]] constexpr QChar front() const; // NOT noexcept!
309 [[nodiscard]] constexpr QChar back() const; // NOT noexcept!
310 [[nodiscard]] constexpr bool empty() const noexcept { return size() == 0; }
311 [[nodiscard]] constexpr qsizetype size_bytes() const noexcept
312 { return size() * charSize(); }
313
314 [[nodiscard]] constexpr qsizetype max_size() const noexcept
315 {
316 // -1 to deal with the pointer one-past-the-end;
317 return QtPrivate::MaxAllocSize / charSize() - 1;
318 }
319
320 //
321 // Qt compatibility API:
322 //
323 [[nodiscard]] constexpr bool isNull() const noexcept { return !m_data; }
324 [[nodiscard]] constexpr bool isEmpty() const noexcept { return empty(); }
325 [[nodiscard]] constexpr qsizetype length() const noexcept
326 { return size(); }
327
328private:
329 friend bool comparesEqual(const QAnyStringView &lhs, const QAnyStringView &rhs) noexcept
330 { return QAnyStringView::equal(lhs, rhs); }
331 friend Qt::strong_ordering
332 compareThreeWay(const QAnyStringView &lhs, const QAnyStringView &rhs) noexcept
333 {
334 const int res = QAnyStringView::compare(lhs, rhs);
335 return Qt::compareThreeWay(res, 0);
336 }
338
339#ifndef QT_NO_DEBUG_STREAM
341#endif
342
343 [[nodiscard]] constexpr Tag tag() const noexcept { return Tag{m_size & TypeMask}; }
344 [[nodiscard]] constexpr bool isUtf16() const noexcept { return tag() == Tag::Utf16; }
345 [[nodiscard]] constexpr bool isUtf8() const noexcept { return tag() == Tag::Utf8; }
346 [[nodiscard]] constexpr bool isLatin1() const noexcept { return tag() == Tag::Latin1; }
347 [[nodiscard]] constexpr QStringView asStringView() const
348 { return Q_ASSERT(isUtf16()), QStringView{m_data_utf16, size()}; }
349 [[nodiscard]] constexpr q_no_char8_t::QUtf8StringView asUtf8StringView() const
350 { return Q_ASSERT(isUtf8()), q_no_char8_t::QUtf8StringView{m_data_utf8, size()}; }
351 [[nodiscard]] inline constexpr QLatin1StringView asLatin1StringView() const;
352 [[nodiscard]] constexpr size_t charSize() const noexcept { return isUtf16() ? 2 : 1; }
353 constexpr void setSize(qsizetype sz) noexcept { m_size = size_t(sz) | tag(); }
354 constexpr void advanceData(qsizetype delta) noexcept
355 { m_data_utf8 += delta * charSize(); }
356 Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos = 0,
357 [[maybe_unused]] qsizetype n = 1) const
358 {
359 Q_ASSERT(pos >= 0);
360 Q_ASSERT(pos <= size());
361 Q_ASSERT(n >= 0);
362 Q_ASSERT(n <= size() - pos);
363 }
364 union {
365 const void *m_data;
366 const char *m_data_utf8;
367 const char16_t *m_data_utf16;
368 };
369 size_t m_size;
370 friend class ::tst_QAnyStringView;
371};
373
374template <typename QStringLike, std::enable_if_t<std::disjunction_v<
375 std::is_same<QStringLike, QString>,
376 std::is_same<QStringLike, QByteArray>
377 >, bool> = true>
378[[nodiscard]] inline QAnyStringView qToAnyStringViewIgnoringNull(const QStringLike &s) noexcept
379{ return QAnyStringView(s.begin(), s.size()); }
380
381QT_END_NAMESPACE
382
383#endif /* QANYSTRINGVIEW_H */
\inmodule QtCore
constexpr QChar back() const
Returns the last character in the string view.
Definition qstring.h:134
constexpr const void * data() const noexcept
Returns a const pointer to the first character in the string view.
constexpr QAnyStringView sliced(qsizetype pos, qsizetype n) const
static constexpr bool detects_US_ASCII_at_compile_time
constexpr QAnyStringView left(qsizetype n) const
constexpr QAnyStringView(Char ch, QCharContainer &&capacity=QCharContainer()) noexcept
constexpr QAnyStringView & slice(qsizetype pos)
constexpr QAnyStringView last(qsizetype n) const
const char * m_data_utf8
constexpr qsizetype size() const noexcept
Returns the size of this string view, in the encoding's code points.
constexpr QAnyStringView & slice(qsizetype pos, qsizetype n)
constexpr QAnyStringView(const Char *str, qsizetype len)
Constructs a string view on str with length len.
QString toString() const
Returns a deep copy of this string view's data as a QString.
Definition qstring.h:1338
qsizetype size_type
Alias for qsizetype.
constexpr QAnyStringView first(qsizetype n) const
constexpr QAnyStringView right(qsizetype n) const
static Q_CORE_EXPORT int compare(QAnyStringView lhs, QAnyStringView rhs, Qt::CaseSensitivity cs=Qt::CaseSensitive) noexcept
Compares the string view lhs with the string view rhs and returns a negative integer if lhs is less t...
Definition qstring.cpp:1594
constexpr bool isEmpty() const noexcept
Returns whether this string view is empty - that is, whether {size() == 0}.
static Q_CORE_EXPORT bool equal(QAnyStringView lhs, QAnyStringView rhs) noexcept
Definition qstring.cpp:1441
friend bool comparesEqual(const QAnyStringView &lhs, const QAnyStringView &rhs) noexcept
const void * m_data
qptrdiff difference_type
Alias for {std::ptrdiff_t}.
constexpr qsizetype size_bytes() const noexcept
Returns the size of this string view, but in bytes, not code-points.
constexpr QAnyStringView sliced(qsizetype pos) const
constexpr QAnyStringView mid(qsizetype pos, qsizetype n=-1) const
constexpr QAnyStringView(Container &&c, QtPrivate::wrapped_t< Container, QString > &&capacity={})
constexpr QAnyStringView chopped(qsizetype n) const
constexpr bool isNull() const noexcept
Returns whether this string view is null - that is, whether {data() == nullptr}.
constexpr QAnyStringView() noexcept
Constructs a null string view.
const char16_t * m_data_utf16
constexpr QAnyStringView(const Char *f, const Char *l)
Constructs a string view on first with length (last - first).
constexpr QChar front() const
Returns the first character in the string view.
Definition qstring.h:130
QString arg(Args &&...args) const
Definition qstring.h:1749
constexpr QAnyStringView(std::nullptr_t) noexcept
Constructs a null string view.
constexpr void chop(qsizetype n)
constexpr void truncate(qsizetype n)
constexpr QAnyStringView(QLatin1StringView str) noexcept
Definition qstring.h:105
constexpr QAnyStringView(const Char(&str)[]) noexcept
constexpr bool empty() const noexcept
Returns whether this string view is empty - that is, whether {size() == 0}.
constexpr QAnyStringView(const Pointer &str) noexcept
constexpr qsizetype length() const noexcept
Same as size().
constexpr qsizetype max_size() const noexcept
friend Qt::strong_ordering compareThreeWay(const QAnyStringView &lhs, const QAnyStringView &rhs) noexcept
QAnyStringView(const QByteArray &str) noexcept
Constructs a string view on str.
Definition qstring.h:1333
Combined button and popup list for selecting options.
Q_DECLARE_TYPEINFO(QAnyStringView, Q_PRIMITIVE_TYPE)