Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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/qlatin1stringview.h>
9#include <QtCore/qstringview.h>
10#include <QtCore/qutf8stringview.h>
11
12#ifdef __cpp_impl_three_way_comparison
13#include <compare>
14#endif
15#include <QtCore/q20type_traits.h>
16#include <limits>
17
18class tst_QAnyStringView;
19
21
22namespace QtPrivate {
23
24template <typename Tag, typename Result>
25struct wrapped { using type = Result; };
26
27template <typename Tag, typename Result>
29
30} // namespace QtPrivate
31
33{
34public:
37private:
38 static constexpr size_t SizeMask = (std::numeric_limits<size_t>::max)() / 4;
39#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED)
40 static constexpr int SizeShift = 2;
41 static constexpr size_t Latin1Flag = 1;
42#else
43 static constexpr int SizeShift = 0;
44 static constexpr size_t Latin1Flag = SizeMask + 1;
45#endif
46 static constexpr size_t TwoByteCodePointFlag = Latin1Flag << 1;
47 static constexpr size_t TypeMask = ~(SizeMask << SizeShift);
48 static_assert(TypeMask == (Latin1Flag|TwoByteCodePointFlag));
49
50 // Tag bits
51 // 0 0 Utf8
52 // 0 1 Latin1
53 // 1 0 Utf16
54 // 1 1 Unused
55 // ^ ^ latin1
56 // | sizeof code-point == 2
57 enum Tag : size_t {
58 Utf8 = 0,
59 Latin1 = Latin1Flag,
60 Utf16 = TwoByteCodePointFlag,
61 Unused = TypeMask,
62 };
63
64 template <typename Char>
65 using if_compatible_char = std::enable_if_t<std::disjunction_v<
68 >, bool>;
69
70 template <typename Pointer>
71 using if_compatible_pointer = std::enable_if_t<std::disjunction_v<
74 >, bool>;
75
76
77 template <typename T>
78 using if_compatible_container = std::enable_if_t<std::disjunction_v<
81 >, bool>;
82
83 template <typename QStringOrQByteArray, typename T>
84 using if_convertible_to = std::enable_if_t<std::conjunction_v<
85 // need to exclude a bunch of stuff, because we take by universal reference:
86 std::negation<std::disjunction<
87 std::is_same<q20::remove_cvref_t<T>, QAnyStringView::Tag>,
88 std::is_same<q20::remove_cvref_t<T>, QAnyStringView>, // don't make a copy/move ctor
89 std::is_pointer<std::decay_t<T>>, // const char*, etc
90 std::is_same<q20::remove_cvref_t<T>, QByteArray>,
91 std::is_same<q20::remove_cvref_t<T>, QString>
92 >>,
93 // this is what we're really after:
94 std::is_convertible<T, QStringOrQByteArray>
95 >, bool>;
96
97 // confirm we don't make an accidental copy constructor:
100
101 template<typename Char>
102 static constexpr bool isAsciiOnlyCharsAtCompileTime(Char *str, qsizetype sz) noexcept
103 {
104 // do not perform check if not at compile time
106 return false;
107 if constexpr (sizeof(Char) != sizeof(char)) {
108 Q_UNUSED(str);
109 Q_UNUSED(sz);
110 return false;
111 } else {
112 for (qsizetype i = 0; i < sz; ++i) {
113 if (uchar(str[i]) > 0x7f)
114 return false;
115 }
116 return true;
117 }
118 }
119
120 template<typename Char>
121 static constexpr std::size_t encodeType(const Char *str, qsizetype sz) noexcept
122 {
123 // Utf16 if 16 bit, Latin1 if ASCII, else Utf8
124 Q_ASSERT(sz >= 0);
125 Q_ASSERT(sz <= qsizetype(SizeMask));
126 Q_ASSERT(str || !sz);
127 return (std::size_t(sz) << SizeShift)
128 | uint(sizeof(Char) == sizeof(char16_t)) * Tag::Utf16
129 | uint(isAsciiOnlyCharsAtCompileTime(str, sz)) * Tag::Latin1;
130 }
131
132 template <typename Char>
133 static constexpr qsizetype lengthHelperPointer(const Char *str) noexcept
134 {
136 return qsizetype(std::char_traits<Char>::length(str));
137 if constexpr (sizeof(Char) == sizeof(char16_t))
138 return QtPrivate::qustrlen(reinterpret_cast<const char16_t*>(str));
139 else
140 return qsizetype(strlen(reinterpret_cast<const char*>(str)));
141 }
142
143 static QChar toQChar(char ch) noexcept { return toQChar(QLatin1Char{ch}); } // we don't handle UTF-8 multibytes
144 static QChar toQChar(QChar ch) noexcept { return ch; }
145 static QChar toQChar(QLatin1Char ch) noexcept { return ch; }
146
147 explicit constexpr QAnyStringView(const void *d, qsizetype n, std::size_t sizeAndType) noexcept
148 : m_data{d}, m_size{std::size_t(n) | (sizeAndType & TypeMask)} {}
149public:
150 constexpr QAnyStringView() noexcept
151 : m_data{nullptr}, m_size{0} {}
152 constexpr QAnyStringView(std::nullptr_t) noexcept
153 : QAnyStringView() {}
154
155 template <typename Char, if_compatible_char<Char> = true>
157 : m_data{str}, m_size{encodeType<Char>(str, len)}
158 {
159 }
160
161 template <typename Char, if_compatible_char<Char> = true>
162 constexpr QAnyStringView(const Char *f, const Char *l)
163 : QAnyStringView(f, l - f) {}
164
165#ifdef Q_QDOC
166 template <typename Char, size_t N>
167 constexpr QAnyStringView(const Char (&array)[N]) noexcept;
168
169 template <typename Char>
170 constexpr QAnyStringView(const Char *str) noexcept;
171#else
172
173 template <typename Pointer, if_compatible_pointer<Pointer> = true>
174 constexpr QAnyStringView(const Pointer &str) noexcept
175 : QAnyStringView{str, str ? lengthHelperPointer(str) : 0} {}
176#endif
177
178 // defined in qstring.h
179 inline QAnyStringView(const QByteArray &str) noexcept; // TODO: Should we have this at all? Remove?
180 inline QAnyStringView(const QString &str) noexcept;
181 inline constexpr QAnyStringView(QLatin1StringView str) noexcept;
182
183 template <typename Container, if_compatible_container<Container> = true>
184 constexpr Q_ALWAYS_INLINE QAnyStringView(const Container &c) noexcept
186
187 template <typename Container, if_convertible_to<QString, Container> = true>
189 //noexcept(std::is_nothrow_constructible_v<QString, Container>)
190 : QAnyStringView(capacity = std::forward<Container>(c)) {}
191
192 template <typename Container, if_convertible_to<QByteArray, Container> = true>
194 //noexcept(std::is_nothrow_constructible_v<QByteArray, Container>)
195 : QAnyStringView(capacity = std::forward<Container>(c)) {}
196
197 template <typename Char, if_compatible_char<Char> = true>
198 constexpr QAnyStringView(const Char &c) noexcept
199 : QAnyStringView{&c, 1} {}
200 constexpr QAnyStringView(const QChar &c) noexcept
201 : QAnyStringView{&c, 1} {}
202
203 template <typename Char, typename Container = decltype(QChar::fromUcs4(U'x')),
204 std::enable_if_t<std::is_same_v<Char, char32_t>, bool> = true>
205 constexpr QAnyStringView(Char c, Container &&capacity = {})
206 : QAnyStringView(capacity = QChar::fromUcs4(c)) {}
207
210
211 template <bool UseChar8T>
212 constexpr QAnyStringView(QBasicUtf8StringView<UseChar8T> v) noexcept
214
215 template <typename Char, size_t Size, if_compatible_char<Char> = true>
216 [[nodiscard]] constexpr static QAnyStringView fromArray(const Char (&string)[Size]) noexcept
217 { return QAnyStringView(string, Size); }
218
219 // defined in qstring.h:
220 template <typename Visitor>
221 inline constexpr decltype(auto) visit(Visitor &&v) const;
222
223 [[nodiscard]]
224 constexpr QAnyStringView mid(qsizetype pos, qsizetype n = -1) const
225 {
226 using namespace QtPrivate;
227 auto result = QContainerImplHelper::mid(size(), &pos, &n);
228 return result == QContainerImplHelper::Null ? QAnyStringView() : sliced(pos, n);
229 }
230 [[nodiscard]]
232 {
233 if (size_t(n) >= size_t(size()))
234 n = size();
235 return sliced(0, n);
236 }
237 [[nodiscard]]
239 {
240 if (size_t(n) >= size_t(size()))
241 n = size();
242 return sliced(size() - n, n);
243 }
244
245 [[nodiscard]] constexpr QAnyStringView sliced(qsizetype pos) const
246 { verify(pos, 0); auto r = *this; r.advanceData(pos); r.setSize(size() - pos); return r; }
247 [[nodiscard]] constexpr QAnyStringView sliced(qsizetype pos, qsizetype n) const
248 { verify(pos, n); auto r = *this; r.advanceData(pos); r.setSize(n); return r; }
249 [[nodiscard]] constexpr QAnyStringView first(qsizetype n) const
250 { verify(0, n); return sliced(0, n); }
251 [[nodiscard]] constexpr QAnyStringView last(qsizetype n) const
252 { verify(0, n); return sliced(size() - n, n); }
253 [[nodiscard]] constexpr QAnyStringView chopped(qsizetype n) const
254 { verify(0, n); return sliced(0, size() - n); }
255
256 constexpr void truncate(qsizetype n)
257 { verify(0, n); setSize(n); }
258 constexpr void chop(qsizetype n)
259 { verify(0, n); setSize(size() - n); }
260
261
262 [[nodiscard]] inline QString toString() const; // defined in qstring.h
263
264 [[nodiscard]] constexpr qsizetype size() const noexcept
265 { return qsizetype((m_size >> SizeShift) & SizeMask); }
266 [[nodiscard]] constexpr const void *data() const noexcept { return m_data; }
267
268 [[nodiscard]] Q_CORE_EXPORT static int compare(QAnyStringView lhs, QAnyStringView rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
269 [[nodiscard]] Q_CORE_EXPORT static bool equal(QAnyStringView lhs, QAnyStringView rhs) noexcept;
270
271 static constexpr inline bool detects_US_ASCII_at_compile_time =
272#ifdef QT_SUPPORTS_IS_CONSTANT_EVALUATED
273 true
274#else
275 false
276#endif
277 ;
278
279 //
280 // STL compatibility API:
281 //
282 [[nodiscard]] constexpr QChar front() const; // NOT noexcept!
283 [[nodiscard]] constexpr QChar back() const; // NOT noexcept!
284 [[nodiscard]] constexpr bool empty() const noexcept { return size() == 0; }
285 [[nodiscard]] constexpr qsizetype size_bytes() const noexcept
286 { return size() * charSize(); }
287
288 //
289 // Qt compatibility API:
290 //
291 [[nodiscard]] constexpr bool isNull() const noexcept { return !m_data; }
292 [[nodiscard]] constexpr bool isEmpty() const noexcept { return empty(); }
293 [[nodiscard]] constexpr qsizetype length() const noexcept
294 { return size(); }
295
296private:
297 friend bool comparesEqual(const QAnyStringView &lhs, const QAnyStringView &rhs) noexcept
298 { return QAnyStringView::equal(lhs, rhs); }
300 compareThreeWay(const QAnyStringView &lhs, const QAnyStringView &rhs) noexcept
301 {
302 const int res = QAnyStringView::compare(lhs, rhs);
303 return Qt::compareThreeWay(res, 0);
304 }
306
307#ifndef QT_NO_DEBUG_STREAM
308 Q_CORE_EXPORT friend QDebug operator<<(QDebug d, QAnyStringView s);
309#endif
310
311 [[nodiscard]] constexpr Tag tag() const noexcept { return Tag{m_size & TypeMask}; }
312 [[nodiscard]] constexpr bool isUtf16() const noexcept { return tag() == Tag::Utf16; }
313 [[nodiscard]] constexpr bool isUtf8() const noexcept { return tag() == Tag::Utf8; }
314 [[nodiscard]] constexpr bool isLatin1() const noexcept { return tag() == Tag::Latin1; }
315 [[nodiscard]] constexpr QStringView asStringView() const
316 { return Q_ASSERT(isUtf16()), QStringView{m_data_utf16, size()}; }
317 [[nodiscard]] constexpr q_no_char8_t::QUtf8StringView asUtf8StringView() const
318 { return Q_ASSERT(isUtf8()), q_no_char8_t::QUtf8StringView{m_data_utf8, size()}; }
319 [[nodiscard]] inline constexpr QLatin1StringView asLatin1StringView() const;
320 [[nodiscard]] constexpr size_t charSize() const noexcept { return isUtf16() ? 2 : 1; }
321 constexpr void setSize(qsizetype sz) noexcept { m_size = size_t(sz) | tag(); }
322 constexpr void advanceData(qsizetype delta) noexcept
323 { m_data_utf8 += delta * charSize(); }
324 Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos = 0,
325 [[maybe_unused]] qsizetype n = 1) const
326 {
327 Q_ASSERT(pos >= 0);
328 Q_ASSERT(pos <= size());
329 Q_ASSERT(n >= 0);
330 Q_ASSERT(n <= size() - pos);
331 }
332 union {
333 const void *m_data;
334 const char *m_data_utf8;
335 const char16_t *m_data_utf16;
336 };
337 size_t m_size;
338 friend class ::tst_QAnyStringView;
339};
341
342template <typename QStringLike, std::enable_if_t<std::disjunction_v<
343 std::is_same<QStringLike, QString>,
344 std::is_same<QStringLike, QByteArray>
345 >, bool> = true>
346[[nodiscard]] inline QAnyStringView qToAnyStringViewIgnoringNull(const QStringLike &s) noexcept
347{ return QAnyStringView(s.begin(), s.size()); }
348
350
351#endif /* QANYSTRINGVIEW_H */
\inmodule QtCore
constexpr QAnyStringView(const QChar &c) noexcept
constexpr QAnyStringView(std::nullptr_t) noexcept
Constructs a null string view.
constexpr QAnyStringView() noexcept
Constructs a null string view.
constexpr QChar back() const
Returns the last character in the string view.
Definition qstring.h:122
constexpr bool isNull() const noexcept
Returns whether this string view is null - that is, whether {data() == nullptr}.
Q_CORE_EXPORT friend QDebug operator<<(QDebug d, QAnyStringView s)
constexpr QAnyStringView sliced(qsizetype pos, qsizetype n) const
constexpr qsizetype size_bytes() const noexcept
Returns the size of this string view, but in bytes, not code-points.
static constexpr bool detects_US_ASCII_at_compile_time
qsizetype size_type
Alias for qsizetype.
constexpr qsizetype size() const noexcept
Returns the size of this string view, in the encoding's code points.
const char * m_data_utf8
QString toString() const
Returns a deep copy of this string view's data as a QString.
Definition qstring.h:1218
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:1599
constexpr bool isEmpty() const noexcept
Returns whether this string view is empty - that is, whether {size() == 0}.
static constexpr QAnyStringView fromArray(const Char(&string)[Size]) noexcept
constexpr QAnyStringView right(qsizetype n) const
constexpr QAnyStringView(const Char &c) noexcept
static Q_CORE_EXPORT bool equal(QAnyStringView lhs, QAnyStringView rhs) noexcept
Definition qstring.cpp:1448
constexpr QAnyStringView(Container &&c, QtPrivate::wrapped_t< Container, QByteArray > &&capacity={})
constexpr QAnyStringView(const Char *f, const Char *l)
Constructs a string view on first with length (last - first).
friend bool comparesEqual(const QAnyStringView &lhs, const QAnyStringView &rhs) noexcept
constexpr qsizetype length() const noexcept
Same as size().
const void * m_data
constexpr QAnyStringView last(qsizetype n) const
constexpr QAnyStringView sliced(qsizetype pos) const
constexpr QAnyStringView(Container &&c, QtPrivate::wrapped_t< Container, QString > &&capacity={})
const char16_t * m_data_utf16
qptrdiff difference_type
Alias for {std::ptrdiff_t}.
constexpr QChar front() const
Returns the first character in the string view.
Definition qstring.h:118
constexpr QAnyStringView left(qsizetype n) const
constexpr QAnyStringView(QStringView v) noexcept
constexpr QAnyStringView first(qsizetype n) const
constexpr QAnyStringView(const Char *str, qsizetype len)
Constructs a string view on str with length len.
constexpr QAnyStringView mid(qsizetype pos, qsizetype n=-1) const
constexpr QAnyStringView chopped(qsizetype n) const
constexpr void chop(qsizetype n)
constexpr const void * data() const noexcept
Returns a const pointer to the first character in the string view.
constexpr void truncate(qsizetype n)
constexpr QAnyStringView(Char c, Container &&capacity={})
constexpr Q_ALWAYS_INLINE QAnyStringView(const Container &c) noexcept
constexpr QAnyStringView(const Pointer &str) noexcept
friend Qt::strong_ordering compareThreeWay(const QAnyStringView &lhs, const QAnyStringView &rhs) noexcept
constexpr QAnyStringView(QBasicUtf8StringView< UseChar8T > v) noexcept
constexpr bool empty() const noexcept
Returns whether this string view is empty - that is, whether {size() == 0}.
constexpr decltype(auto) visit(Visitor &&v) const
Calls v with either a QUtf8StringView, QLatin1String, or QStringView, depending on the encoding of th...
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore
\inmodule QtCore
\inmodule QtCore
Definition qstringview.h:78
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore \title Classes and helpers for defining comparison operators \keyword qtcompare
Definition qcompare.h:400
QString str
[2]
Combined button and popup list for selecting options.
\macro QT_NO_KEYWORDS >
IsCompatiblePointer8Helper< q20::remove_cvref_t< Pointer > > IsCompatiblePointer8
constexpr Q_ALWAYS_INLINE std::enable_if_t< sizeof(Char)==sizeof(char16_t), qsizetype > lengthHelperContainer(const Char(&str)[N])
IsCompatibleChar8TypeHelper< q20::remove_cvref_t< Char > > IsCompatibleChar8Type
typename wrapped< Tag, Result >::type wrapped_t
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype qustrlen(const char16_t *str) noexcept
Definition qstring.cpp:658
constexpr Qt::strong_ordering compareThreeWay(LeftInt lhs, RightInt rhs) noexcept
CaseSensitivity
@ CaseSensitive
constexpr bool is_constant_evaluated() noexcept
QAnyStringView qToAnyStringViewIgnoringNull(const QStringLike &s) noexcept
#define Q_DECLARE_STRONGLY_ORDERED(...)
#define Q_ALWAYS_INLINE
quint32 Tag
NSUInteger capacity
GLsizei const GLfloat * v
[13]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLboolean r
[2]
GLfloat GLfloat f
GLfloat n
GLdouble s
[6]
Definition qopenglext.h:235
GLuint res
const GLubyte * c
GLenum array
GLuint64EXT * result
[6]
GLenum GLsizei len
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
char Char
#define Q_UNUSED(x)
@ Q_PRIMITIVE_TYPE
Definition qtypeinfo.h:157
#define Q_DECLARE_TYPEINFO(TYPE, FLAGS)
Definition qtypeinfo.h:180
unsigned char uchar
Definition qtypes.h:32
ptrdiff_t qptrdiff
Definition qtypes.h:164
ptrdiff_t qsizetype
Definition qtypes.h:165
unsigned int uint
Definition qtypes.h:34
\inmodule QtCore \reentrant
Definition qchar.h:18