1/****************************************************************************
2**
3** Copyright (C) 2017 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com>
4** Copyright (C) 2019 Mail.ru Group.
5** Contact: http://www.qt.io/licensing/
6**
7** This file is part of the QtCore module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see https://www.qt.io/terms-conditions. For further
16** information use the contact form at https://www.qt.io/contact-us.
17**
18** GNU Lesser General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU Lesser
20** General Public License version 3 as published by the Free Software
21** Foundation and appearing in the file LICENSE.LGPL3 included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU Lesser General Public License version 3 requirements
24** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25**
26** GNU General Public License Usage
27** Alternatively, this file may be used under the terms of the GNU
28** General Public License version 2.0 or (at your option) the GNU General
29** Public license version 3 or any later version approved by the KDE Free
30** Qt Foundation. The licenses are as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32** included in the packaging of this file. Please review the following
33** information to ensure the GNU General Public License requirements will
34** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35** https://www.gnu.org/licenses/gpl-3.0.html.
36**
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40#ifndef QSTRINGVIEW_H
41#define QSTRINGVIEW_H
42
43#ifndef QT_STRINGVIEW_LEVEL
44# define QT_STRINGVIEW_LEVEL 1
45#endif
46
47#include <QtCore/qchar.h>
48#include <QtCore/qbytearray.h>
49#include <QtCore/qstringliteral.h>
50#include <QtCore/qstringalgorithms.h>
51
52#include <string>
53
54QT_BEGIN_NAMESPACE
55
56class QString;
57class QStringRef;
58
59namespace QtPrivate {
60template <typename Char>
61struct IsCompatibleCharTypeHelper
62 : std::integral_constant<bool,
63 std::is_same<Char, QChar>::value ||
64 std::is_same<Char, ushort>::value ||
65 std::is_same<Char, char16_t>::value ||
66 (std::is_same<Char, wchar_t>::value && sizeof(wchar_t) == sizeof(QChar))> {};
67template <typename Char>
68struct IsCompatibleCharType
69 : IsCompatibleCharTypeHelper<typename std::remove_cv<typename std::remove_reference<Char>::type>::type> {};
70
71template <typename Array>
72struct IsCompatibleArrayHelper : std::false_type {};
73template <typename Char, size_t N>
74struct IsCompatibleArrayHelper<Char[N]>
75 : IsCompatibleCharType<Char> {};
76template <typename Array>
77struct IsCompatibleArray
78 : IsCompatibleArrayHelper<typename std::remove_cv<typename std::remove_reference<Array>::type>::type> {};
79
80template <typename Pointer>
81struct IsCompatiblePointerHelper : std::false_type {};
82template <typename Char>
83struct IsCompatiblePointerHelper<Char*>
84 : IsCompatibleCharType<Char> {};
85template <typename Pointer>
86struct IsCompatiblePointer
87 : IsCompatiblePointerHelper<typename std::remove_cv<typename std::remove_reference<Pointer>::type>::type> {};
88
89template <typename T>
90struct IsCompatibleStdBasicStringHelper : std::false_type {};
91template <typename Char, typename...Args>
92struct IsCompatibleStdBasicStringHelper<std::basic_string<Char, Args...> >
93 : IsCompatibleCharType<Char> {};
94
95template <typename T>
96struct IsCompatibleStdBasicString
97 : IsCompatibleStdBasicStringHelper<
98 typename std::remove_cv<typename std::remove_reference<T>::type>::type
99 > {};
100
101} // namespace QtPrivate
102
103class QStringView
104{
105public:
106 typedef char16_t storage_type;
107 typedef const QChar value_type;
108 typedef std::ptrdiff_t difference_type;
109 typedef qsizetype size_type;
110 typedef value_type &reference;
111 typedef value_type &const_reference;
112 typedef value_type *pointer;
113 typedef value_type *const_pointer;
114
115 typedef pointer iterator;
116 typedef const_pointer const_iterator;
117 typedef std::reverse_iterator<iterator> reverse_iterator;
118 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
119
120private:
121 template <typename Char>
122 using if_compatible_char = typename std::enable_if<QtPrivate::IsCompatibleCharType<Char>::value, bool>::type;
123
124 template <typename Array>
125 using if_compatible_array = typename std::enable_if<QtPrivate::IsCompatibleArray<Array>::value, bool>::type;
126
127 template <typename Pointer>
128 using if_compatible_pointer = typename std::enable_if<QtPrivate::IsCompatiblePointer<Pointer>::value, bool>::type;
129
130 template <typename T>
131 using if_compatible_string = typename std::enable_if<QtPrivate::IsCompatibleStdBasicString<T>::value, bool>::type;
132
133 template <typename T>
134 using if_compatible_qstring_like = typename std::enable_if<std::is_same<T, QString>::value || std::is_same<T, QStringRef>::value, bool>::type;
135
136 template <typename Char, size_t N>
137 static Q_DECL_CONSTEXPR qsizetype lengthHelperArray(const Char (&)[N]) noexcept
138 {
139 return qsizetype(N - 1);
140 }
141
142 template <typename Char>
143 static qsizetype lengthHelperPointer(const Char *str) noexcept
144 {
145#if defined(Q_CC_GNU) && !defined(Q_CC_CLANG) && !defined(Q_CC_INTEL)
146 if (__builtin_constant_p(*str)) {
147 qsizetype result = 0;
148 while (*str++)
149 ++result;
150 return result;
151 }
152#endif
153 return QtPrivate::qustrlen(reinterpret_cast<const ushort *>(str));
154 }
155 static qsizetype lengthHelperPointer(const QChar *str) noexcept
156 {
157 return QtPrivate::qustrlen(reinterpret_cast<const ushort *>(str));
158 }
159
160 template <typename Char>
161 static const storage_type *castHelper(const Char *str) noexcept
162 { return reinterpret_cast<const storage_type*>(str); }
163 static Q_DECL_CONSTEXPR const storage_type *castHelper(const storage_type *str) noexcept
164 { return str; }
165
166public:
167 Q_DECL_CONSTEXPR QStringView() noexcept
168 : m_size(0), m_data(nullptr) {}
169 Q_DECL_CONSTEXPR QStringView(std::nullptr_t) noexcept
170 : QStringView() {}
171
172 template <typename Char, if_compatible_char<Char> = true>
173 Q_DECL_CONSTEXPR QStringView(const Char *str, qsizetype len)
174 : m_size((Q_ASSERT(len >= 0), Q_ASSERT(str || !len), len)),
175 m_data(castHelper(str)) {}
176
177 template <typename Char, if_compatible_char<Char> = true>
178 Q_DECL_CONSTEXPR QStringView(const Char *f, const Char *l)
179 : QStringView(f, l - f) {}
180
181#ifdef Q_CLANG_QDOC
182 template <typename Char, size_t N>
183 Q_DECL_CONSTEXPR QStringView(const Char (&array)[N]) noexcept;
184
185 template <typename Char>
186 Q_DECL_CONSTEXPR QStringView(const Char *str) noexcept;
187#else
188 template <typename Array, if_compatible_array<Array> = true>
189 Q_DECL_CONSTEXPR QStringView(const Array &str) noexcept
190 : QStringView(str, lengthHelperArray(str)) {}
191
192 template <typename Pointer, if_compatible_pointer<Pointer> = true>
193 Q_DECL_CONSTEXPR QStringView(const Pointer &str) noexcept
194 : QStringView(str, str ? lengthHelperPointer(str) : 0) {}
195#endif
196
197#ifdef Q_CLANG_QDOC
198 QStringView(const QString &str) noexcept;
199 QStringView(const QStringRef &str) noexcept;
200#else
201 template <typename String, if_compatible_qstring_like<String> = true>
202 QStringView(const String &str) noexcept
203 : QStringView(str.isNull() ? nullptr : str.data(), qsizetype(str.size())) {}
204#endif
205
206 template <typename StdBasicString, if_compatible_string<StdBasicString> = true>
207 QStringView(const StdBasicString &str) noexcept
208 : QStringView(str.data(), qsizetype(str.size())) {}
209
210 Q_REQUIRED_RESULT inline QString toString() const; // defined in qstring.h
211
212 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR qsizetype size() const noexcept { return m_size; }
213 Q_REQUIRED_RESULT const_pointer data() const noexcept { return reinterpret_cast<const_pointer>(m_data); }
214 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR const storage_type *utf16() const noexcept { return m_data; }
215
216 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QChar operator[](qsizetype n) const
217 { return Q_ASSERT(n >= 0), Q_ASSERT(n < size()), QChar(m_data[n]); }
218
219 //
220 // QString API
221 //
222
223 template <typename...Args>
224 Q_REQUIRED_RESULT inline QString arg(Args &&...args) const; // defined in qstring.h
225
226 Q_REQUIRED_RESULT QByteArray toLatin1() const { return QtPrivate::convertToLatin1(*this); }
227 Q_REQUIRED_RESULT QByteArray toUtf8() const { return QtPrivate::convertToUtf8(*this); }
228 Q_REQUIRED_RESULT QByteArray toLocal8Bit() const { return QtPrivate::convertToLocal8Bit(*this); }
229 Q_REQUIRED_RESULT inline QVector<uint> toUcs4() const; // defined in qvector.h
230
231 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QChar at(qsizetype n) const { return (*this)[n]; }
232
233 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QStringView mid(qsizetype pos) const
234 { return Q_ASSERT(pos >= 0), Q_ASSERT(pos <= size()), QStringView(m_data + pos, m_size - pos); }
235 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QStringView mid(qsizetype pos, qsizetype n) const
236 { return Q_ASSERT(pos >= 0), Q_ASSERT(n >= 0), Q_ASSERT(pos + n <= size()), QStringView(m_data + pos, n); }
237 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QStringView left(qsizetype n) const
238 { return Q_ASSERT(n >= 0), Q_ASSERT(n <= size()), QStringView(m_data, n); }
239 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QStringView right(qsizetype n) const
240 { return Q_ASSERT(n >= 0), Q_ASSERT(n <= size()), QStringView(m_data + m_size - n, n); }
241 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QStringView chopped(qsizetype n) const
242 { return Q_ASSERT(n >= 0), Q_ASSERT(n <= size()), QStringView(m_data, m_size - n); }
243
244 Q_DECL_RELAXED_CONSTEXPR void truncate(qsizetype n)
245 { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); m_size = n; }
246 Q_DECL_RELAXED_CONSTEXPR void chop(qsizetype n)
247 { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); m_size -= n; }
248
249 Q_REQUIRED_RESULT QStringView trimmed() const noexcept { return QtPrivate::trimmed(*this); }
250
251 Q_REQUIRED_RESULT int compare(QStringView other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
252 { return QtPrivate::compareStrings(*this, other, cs); }
253
254 Q_REQUIRED_RESULT bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
255 { return QtPrivate::startsWith(*this, s, cs); }
256 Q_REQUIRED_RESULT inline bool startsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
257 Q_REQUIRED_RESULT bool startsWith(QChar c) const noexcept
258 { return !empty() && front() == c; }
259 Q_REQUIRED_RESULT bool startsWith(QChar c, Qt::CaseSensitivity cs) const noexcept
260 { return QtPrivate::startsWith(*this, QStringView(&c, 1), cs); }
261
262 Q_REQUIRED_RESULT bool endsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
263 { return QtPrivate::endsWith(*this, s, cs); }
264 Q_REQUIRED_RESULT inline bool endsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
265 Q_REQUIRED_RESULT bool endsWith(QChar c) const noexcept
266 { return !empty() && back() == c; }
267 Q_REQUIRED_RESULT bool endsWith(QChar c, Qt::CaseSensitivity cs) const noexcept
268 { return QtPrivate::endsWith(*this, QStringView(&c, 1), cs); }
269
270 Q_REQUIRED_RESULT qsizetype indexOf(QChar c, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
271 { return QtPrivate::findString(*this, from, QStringView(&c, 1), cs); }
272 Q_REQUIRED_RESULT qsizetype indexOf(QStringView s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
273 { return QtPrivate::findString(*this, from, s, cs); }
274 Q_REQUIRED_RESULT inline qsizetype indexOf(QLatin1String s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
275
276 Q_REQUIRED_RESULT bool contains(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
277 { return indexOf(QStringView(&c, 1), 0, cs) != qsizetype(-1); }
278 Q_REQUIRED_RESULT bool contains(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
279 { return indexOf(s, 0, cs) != qsizetype(-1); }
280 Q_REQUIRED_RESULT inline bool contains(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
281
282 Q_REQUIRED_RESULT qsizetype lastIndexOf(QChar c, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
283 { return QtPrivate::lastIndexOf(*this, from, QStringView(&c, 1), cs); }
284 Q_REQUIRED_RESULT qsizetype lastIndexOf(QStringView s, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
285 { return QtPrivate::lastIndexOf(*this, from, s, cs); }
286 Q_REQUIRED_RESULT inline qsizetype lastIndexOf(QLatin1String s, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
287
288 Q_REQUIRED_RESULT bool isRightToLeft() const noexcept
289 { return QtPrivate::isRightToLeft(*this); }
290
291 Q_REQUIRED_RESULT inline int toWCharArray(wchar_t *array) const; // defined in qstring.h
292
293 //
294 // STL compatibility API:
295 //
296 Q_REQUIRED_RESULT const_iterator begin() const noexcept { return data(); }
297 Q_REQUIRED_RESULT const_iterator end() const noexcept { return data() + size(); }
298 Q_REQUIRED_RESULT const_iterator cbegin() const noexcept { return begin(); }
299 Q_REQUIRED_RESULT const_iterator cend() const noexcept { return end(); }
300 Q_REQUIRED_RESULT const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
301 Q_REQUIRED_RESULT const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); }
302 Q_REQUIRED_RESULT const_reverse_iterator crbegin() const noexcept { return rbegin(); }
303 Q_REQUIRED_RESULT const_reverse_iterator crend() const noexcept { return rend(); }
304
305 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR bool empty() const noexcept { return size() == 0; }
306 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QChar front() const { return Q_ASSERT(!empty()), QChar(m_data[0]); }
307 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QChar back() const { return Q_ASSERT(!empty()), QChar(m_data[m_size - 1]); }
308
309 //
310 // Qt compatibility API:
311 //
312 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR bool isNull() const noexcept { return !m_data; }
313 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR bool isEmpty() const noexcept { return empty(); }
314 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR int length() const /* not nothrow! */
315 { return Q_ASSERT(int(size()) == size()), int(size()); }
316 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QChar first() const { return front(); }
317 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QChar last() const { return back(); }
318private:
319 qsizetype m_size;
320 const storage_type *m_data;
321};
322Q_DECLARE_TYPEINFO(QStringView, Q_PRIMITIVE_TYPE);
323
324template <typename QStringLike, typename std::enable_if<
325 std::is_same<QStringLike, QString>::value || std::is_same<QStringLike, QStringRef>::value,
326 bool>::type = true>
327inline QStringView qToStringViewIgnoringNull(const QStringLike &s) noexcept
328{ return QStringView(s.data(), s.size()); }
329
330QT_END_NAMESPACE
331
332#endif /* QSTRINGVIEW_H */
333