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** 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 QSTRINGVIEW_H
40#define QSTRINGVIEW_H
41
42#ifndef QT_STRINGVIEW_LEVEL
43# define QT_STRINGVIEW_LEVEL 1
44#endif
45
46#include <QtCore/qchar.h>
47#include <QtCore/qbytearray.h>
48#include <QtCore/qstringliteral.h>
49#include <QtCore/qstringalgorithms.h>
50
51#include <string>
52
53QT_BEGIN_NAMESPACE
54
55class QString;
56class QStringRef;
57
58namespace QtPrivate {
59template <typename Char>
60struct IsCompatibleCharTypeHelper
61 : std::integral_constant<bool,
62 std::is_same<Char, QChar>::value ||
63 std::is_same<Char, ushort>::value ||
64#if defined(Q_COMPILER_UNICODE_STRINGS)
65 std::is_same<Char, char16_t>::value ||
66#endif
67 (std::is_same<Char, wchar_t>::value && sizeof(wchar_t) == sizeof(QChar))> {};
68template <typename Char>
69struct IsCompatibleCharType
70 : IsCompatibleCharTypeHelper<typename std::remove_cv<typename std::remove_reference<Char>::type>::type> {};
71
72template <typename Array>
73struct IsCompatibleArrayHelper : std::false_type {};
74template <typename Char, size_t N>
75struct IsCompatibleArrayHelper<Char[N]>
76 : IsCompatibleCharType<Char> {};
77template <typename Array>
78struct IsCompatibleArray
79 : IsCompatibleArrayHelper<typename std::remove_cv<typename std::remove_reference<Array>::type>::type> {};
80
81template <typename Pointer>
82struct IsCompatiblePointerHelper : std::false_type {};
83template <typename Char>
84struct IsCompatiblePointerHelper<Char*>
85 : IsCompatibleCharType<Char> {};
86template <typename Pointer>
87struct IsCompatiblePointer
88 : IsCompatiblePointerHelper<typename std::remove_cv<typename std::remove_reference<Pointer>::type>::type> {};
89
90template <typename T>
91struct IsCompatibleStdBasicStringHelper : std::false_type {};
92template <typename Char, typename...Args>
93struct IsCompatibleStdBasicStringHelper<std::basic_string<Char, Args...> >
94 : IsCompatibleCharType<Char> {};
95
96template <typename T>
97struct IsCompatibleStdBasicString
98 : IsCompatibleStdBasicStringHelper<
99 typename std::remove_cv<typename std::remove_reference<T>::type>::type
100 > {};
101
102} // namespace QtPrivate
103
104class QStringView
105{
106public:
107#if defined(Q_OS_WIN) && !defined(Q_COMPILER_UNICODE_STRINGS)
108 typedef wchar_t storage_type;
109#else
110 typedef char16_t storage_type;
111#endif
112 typedef const QChar value_type;
113 typedef std::ptrdiff_t difference_type;
114 typedef qsizetype size_type;
115 typedef value_type &reference;
116 typedef value_type &const_reference;
117 typedef value_type *pointer;
118 typedef value_type *const_pointer;
119
120 typedef pointer iterator;
121 typedef const_pointer const_iterator;
122 typedef std::reverse_iterator<iterator> reverse_iterator;
123 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
124
125private:
126 template <typename Char>
127 using if_compatible_char = typename std::enable_if<QtPrivate::IsCompatibleCharType<Char>::value, bool>::type;
128
129 template <typename Array>
130 using if_compatible_array = typename std::enable_if<QtPrivate::IsCompatibleArray<Array>::value, bool>::type;
131
132 template <typename Pointer>
133 using if_compatible_pointer = typename std::enable_if<QtPrivate::IsCompatiblePointer<Pointer>::value, bool>::type;
134
135 template <typename T>
136 using if_compatible_string = typename std::enable_if<QtPrivate::IsCompatibleStdBasicString<T>::value, bool>::type;
137
138 template <typename T>
139 using if_compatible_qstring_like = typename std::enable_if<std::is_same<T, QString>::value || std::is_same<T, QStringRef>::value, bool>::type;
140
141 template <typename Char, size_t N>
142 static Q_DECL_CONSTEXPR qsizetype lengthHelperArray(const Char (&)[N]) Q_DECL_NOTHROW
143 {
144 return qsizetype(N - 1);
145 }
146
147 template <typename Char>
148 static qsizetype lengthHelperPointer(const Char *str) Q_DECL_NOTHROW
149 {
150#if defined(Q_CC_GNU) && !defined(Q_CC_CLANG) && !defined(Q_CC_INTEL)
151 if (__builtin_constant_p(*str)) {
152 qsizetype result = 0;
153 while (*str++)
154 ++result;
155 return result;
156 }
157#endif
158 return QtPrivate::qustrlen(reinterpret_cast<const ushort *>(str));
159 }
160 static qsizetype lengthHelperPointer(const QChar *str) Q_DECL_NOTHROW
161 {
162 return QtPrivate::qustrlen(reinterpret_cast<const ushort *>(str));
163 }
164
165 template <typename Char>
166 static const storage_type *castHelper(const Char *str) Q_DECL_NOTHROW
167 { return reinterpret_cast<const storage_type*>(str); }
168 static Q_DECL_CONSTEXPR const storage_type *castHelper(const storage_type *str) Q_DECL_NOTHROW
169 { return str; }
170
171public:
172 Q_DECL_CONSTEXPR QStringView() Q_DECL_NOTHROW
173 : m_size(0), m_data(nullptr) {}
174 Q_DECL_CONSTEXPR QStringView(std::nullptr_t) Q_DECL_NOTHROW
175 : QStringView() {}
176
177 template <typename Char, if_compatible_char<Char> = true>
178 Q_DECL_CONSTEXPR QStringView(const Char *str, qsizetype len)
179 : m_size((Q_ASSERT(len >= 0), Q_ASSERT(str || !len), len)),
180 m_data(castHelper(str)) {}
181
182 template <typename Char, if_compatible_char<Char> = true>
183 Q_DECL_CONSTEXPR QStringView(const Char *f, const Char *l)
184 : QStringView(f, l - f) {}
185
186#ifdef Q_CLANG_QDOC
187 template <typename Char, size_t N>
188 Q_DECL_CONSTEXPR QStringView(const Char (&array)[N]) Q_DECL_NOTHROW;
189
190 template <typename Char>
191 Q_DECL_CONSTEXPR QStringView(const Char *str) Q_DECL_NOTHROW;
192#else
193 template <typename Array, if_compatible_array<Array> = true>
194 Q_DECL_CONSTEXPR QStringView(const Array &str) Q_DECL_NOTHROW
195 : QStringView(str, lengthHelperArray(str)) {}
196
197 template <typename Pointer, if_compatible_pointer<Pointer> = true>
198 Q_DECL_CONSTEXPR QStringView(const Pointer &str) Q_DECL_NOTHROW
199 : QStringView(str, str ? lengthHelperPointer(str) : 0) {}
200#endif
201
202#ifdef Q_CLANG_QDOC
203 QStringView(const QString &str) Q_DECL_NOTHROW;
204 QStringView(const QStringRef &str) Q_DECL_NOTHROW;
205#else
206 template <typename String, if_compatible_qstring_like<String> = true>
207 QStringView(const String &str) Q_DECL_NOTHROW
208 : QStringView(str.isNull() ? nullptr : str.data(), qsizetype(str.size())) {}
209#endif
210
211 template <typename StdBasicString, if_compatible_string<StdBasicString> = true>
212 QStringView(const StdBasicString &str) Q_DECL_NOTHROW
213 : QStringView(str.data(), qsizetype(str.size())) {}
214
215 Q_REQUIRED_RESULT inline QString toString() const; // defined in qstring.h
216
217 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR qsizetype size() const Q_DECL_NOTHROW { return m_size; }
218 Q_REQUIRED_RESULT const_pointer data() const Q_DECL_NOTHROW { return reinterpret_cast<const_pointer>(m_data); }
219 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR const storage_type *utf16() const Q_DECL_NOTHROW { return m_data; }
220
221 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QChar operator[](qsizetype n) const
222 { return Q_ASSERT(n >= 0), Q_ASSERT(n < size()), QChar(m_data[n]); }
223
224 //
225 // QString API
226 //
227
228 Q_REQUIRED_RESULT QByteArray toLatin1() const { return QtPrivate::convertToLatin1(*this); }
229 Q_REQUIRED_RESULT QByteArray toUtf8() const { return QtPrivate::convertToUtf8(*this); }
230 Q_REQUIRED_RESULT QByteArray toLocal8Bit() const { return QtPrivate::convertToLocal8Bit(*this); }
231 Q_REQUIRED_RESULT inline QVector<uint> toUcs4() const; // defined in qvector.h
232
233 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QChar at(qsizetype n) const { return (*this)[n]; }
234
235 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QStringView mid(qsizetype pos) const
236 { return Q_ASSERT(pos >= 0), Q_ASSERT(pos <= size()), QStringView(m_data + pos, m_size - pos); }
237 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QStringView mid(qsizetype pos, qsizetype n) const
238 { return Q_ASSERT(pos >= 0), Q_ASSERT(n >= 0), Q_ASSERT(pos + n <= size()), QStringView(m_data + pos, n); }
239 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QStringView left(qsizetype n) const
240 { return Q_ASSERT(n >= 0), Q_ASSERT(n <= size()), QStringView(m_data, n); }
241 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QStringView right(qsizetype n) const
242 { return Q_ASSERT(n >= 0), Q_ASSERT(n <= size()), QStringView(m_data + m_size - n, n); }
243 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QStringView chopped(qsizetype n) const
244 { return Q_ASSERT(n >= 0), Q_ASSERT(n <= size()), QStringView(m_data, m_size - n); }
245
246 Q_DECL_RELAXED_CONSTEXPR void truncate(qsizetype n)
247 { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); m_size = n; }
248 Q_DECL_RELAXED_CONSTEXPR void chop(qsizetype n)
249 { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); m_size -= n; }
250
251 Q_REQUIRED_RESULT QStringView trimmed() const Q_DECL_NOTHROW { return QtPrivate::trimmed(*this); }
252
253 Q_REQUIRED_RESULT int compare(QStringView other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW
254 { return QtPrivate::compareStrings(*this, other, cs); }
255
256 Q_REQUIRED_RESULT bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW
257 { return QtPrivate::startsWith(*this, s, cs); }
258 Q_REQUIRED_RESULT inline bool startsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW;
259 Q_REQUIRED_RESULT bool startsWith(QChar c) const Q_DECL_NOTHROW
260 { return !empty() && front() == c; }
261 Q_REQUIRED_RESULT bool startsWith(QChar c, Qt::CaseSensitivity cs) const Q_DECL_NOTHROW
262 { return QtPrivate::startsWith(*this, QStringView(&c, 1), cs); }
263
264 Q_REQUIRED_RESULT bool endsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW
265 { return QtPrivate::endsWith(*this, s, cs); }
266 Q_REQUIRED_RESULT inline bool endsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW;
267 Q_REQUIRED_RESULT bool endsWith(QChar c) const Q_DECL_NOTHROW
268 { return !empty() && back() == c; }
269 Q_REQUIRED_RESULT bool endsWith(QChar c, Qt::CaseSensitivity cs) const Q_DECL_NOTHROW
270 { return QtPrivate::endsWith(*this, QStringView(&c, 1), cs); }
271
272 Q_REQUIRED_RESULT bool isRightToLeft() const Q_DECL_NOTHROW
273 { return QtPrivate::isRightToLeft(*this); }
274
275 //
276 // STL compatibility API:
277 //
278 Q_REQUIRED_RESULT const_iterator begin() const Q_DECL_NOTHROW { return data(); }
279 Q_REQUIRED_RESULT const_iterator end() const Q_DECL_NOTHROW { return data() + size(); }
280 Q_REQUIRED_RESULT const_iterator cbegin() const Q_DECL_NOTHROW { return begin(); }
281 Q_REQUIRED_RESULT const_iterator cend() const Q_DECL_NOTHROW { return end(); }
282 Q_REQUIRED_RESULT const_reverse_iterator rbegin() const Q_DECL_NOTHROW { return const_reverse_iterator(end()); }
283 Q_REQUIRED_RESULT const_reverse_iterator rend() const Q_DECL_NOTHROW { return const_reverse_iterator(begin()); }
284 Q_REQUIRED_RESULT const_reverse_iterator crbegin() const Q_DECL_NOTHROW { return rbegin(); }
285 Q_REQUIRED_RESULT const_reverse_iterator crend() const Q_DECL_NOTHROW { return rend(); }
286
287 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR bool empty() const Q_DECL_NOTHROW { return size() == 0; }
288 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QChar front() const { return Q_ASSERT(!empty()), QChar(m_data[0]); }
289 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QChar back() const { return Q_ASSERT(!empty()), QChar(m_data[m_size - 1]); }
290
291 //
292 // Qt compatibility API:
293 //
294 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR bool isNull() const Q_DECL_NOTHROW { return !m_data; }
295 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR bool isEmpty() const Q_DECL_NOTHROW { return empty(); }
296 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR int length() const /* not nothrow! */
297 { return Q_ASSERT(int(size()) == size()), int(size()); }
298 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QChar first() const { return front(); }
299 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QChar last() const { return back(); }
300private:
301 qsizetype m_size;
302 const storage_type *m_data;
303};
304Q_DECLARE_TYPEINFO(QStringView, Q_PRIMITIVE_TYPE);
305
306template <typename QStringLike, typename std::enable_if<
307 std::is_same<QStringLike, QString>::value || std::is_same<QStringLike, QStringRef>::value,
308 bool>::type = true>
309inline QStringView qToStringViewIgnoringNull(const QStringLike &s) Q_DECL_NOTHROW
310{ return QStringView(s.data(), s.size()); }
311
312QT_END_NAMESPACE
313
314#endif /* QSTRINGVIEW_H */
315