Line data Source code
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 :
53 : QT_BEGIN_NAMESPACE
54 :
55 : class QString;
56 : class QStringRef;
57 :
58 : namespace QtPrivate {
59 : template <typename Char>
60 : struct 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))> {};
68 : template <typename Char>
69 : struct IsCompatibleCharType
70 : : IsCompatibleCharTypeHelper<typename std::remove_cv<typename std::remove_reference<Char>::type>::type> {};
71 :
72 : template <typename Array>
73 : struct IsCompatibleArrayHelper : std::false_type {};
74 : template <typename Char, size_t N>
75 : struct IsCompatibleArrayHelper<Char[N]>
76 : : IsCompatibleCharType<Char> {};
77 : template <typename Array>
78 : struct IsCompatibleArray
79 : : IsCompatibleArrayHelper<typename std::remove_cv<typename std::remove_reference<Array>::type>::type> {};
80 :
81 : template <typename Pointer>
82 : struct IsCompatiblePointerHelper : std::false_type {};
83 : template <typename Char>
84 : struct IsCompatiblePointerHelper<Char*>
85 : : IsCompatibleCharType<Char> {};
86 : template <typename Pointer>
87 : struct IsCompatiblePointer
88 : : IsCompatiblePointerHelper<typename std::remove_cv<typename std::remove_reference<Pointer>::type>::type> {};
89 :
90 : template <typename T>
91 : struct IsCompatibleStdBasicStringHelper : std::false_type {};
92 : template <typename Char, typename...Args>
93 : struct IsCompatibleStdBasicStringHelper<std::basic_string<Char, Args...> >
94 : : IsCompatibleCharType<Char> {};
95 :
96 : template <typename T>
97 : struct IsCompatibleStdBasicString
98 : : IsCompatibleStdBasicStringHelper<
99 : typename std::remove_cv<typename std::remove_reference<T>::type>::type
100 : > {};
101 :
102 : } // namespace QtPrivate
103 :
104 : class QStringView
105 : {
106 : public:
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 :
125 : private:
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 44 : static const storage_type *castHelper(const Char *str) Q_DECL_NOTHROW
167 44 : { 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 :
171 : public:
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 44 : Q_DECL_CONSTEXPR QStringView(const Char *str, qsizetype len)
179 0 : : m_size((Q_ASSERT(len >= 0), Q_ASSERT(str || !len), len)),
180 88 : 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_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_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 44 : QStringView(const String &str) Q_DECL_NOTHROW
208 44 : : 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 bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW
254 : { return QtPrivate::startsWith(*this, s, cs); }
255 : Q_REQUIRED_RESULT inline bool startsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW;
256 : Q_REQUIRED_RESULT bool startsWith(QChar c) const Q_DECL_NOTHROW
257 : { return !empty() && front() == c; }
258 : Q_REQUIRED_RESULT bool startsWith(QChar c, Qt::CaseSensitivity cs) const Q_DECL_NOTHROW
259 : { return QtPrivate::startsWith(*this, QStringView(&c, 1), cs); }
260 :
261 : Q_REQUIRED_RESULT bool endsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW
262 : { return QtPrivate::endsWith(*this, s, cs); }
263 : Q_REQUIRED_RESULT inline bool endsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW;
264 : Q_REQUIRED_RESULT bool endsWith(QChar c) const Q_DECL_NOTHROW
265 : { return !empty() && back() == c; }
266 : Q_REQUIRED_RESULT bool endsWith(QChar c, Qt::CaseSensitivity cs) const Q_DECL_NOTHROW
267 : { return QtPrivate::endsWith(*this, QStringView(&c, 1), cs); }
268 :
269 : //
270 : // STL compatibility API:
271 : //
272 : Q_REQUIRED_RESULT const_iterator begin() const Q_DECL_NOTHROW { return data(); }
273 : Q_REQUIRED_RESULT const_iterator end() const Q_DECL_NOTHROW { return data() + size(); }
274 : Q_REQUIRED_RESULT const_iterator cbegin() const Q_DECL_NOTHROW { return begin(); }
275 : Q_REQUIRED_RESULT const_iterator cend() const Q_DECL_NOTHROW { return end(); }
276 : Q_REQUIRED_RESULT const_reverse_iterator rbegin() const Q_DECL_NOTHROW { return const_reverse_iterator(end()); }
277 : Q_REQUIRED_RESULT const_reverse_iterator rend() const Q_DECL_NOTHROW { return const_reverse_iterator(begin()); }
278 : Q_REQUIRED_RESULT const_reverse_iterator crbegin() const Q_DECL_NOTHROW { return rbegin(); }
279 : Q_REQUIRED_RESULT const_reverse_iterator crend() const Q_DECL_NOTHROW { return rend(); }
280 :
281 : Q_REQUIRED_RESULT Q_DECL_CONSTEXPR bool empty() const Q_DECL_NOTHROW { return size() == 0; }
282 : Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QChar front() const { return Q_ASSERT(!empty()), QChar(m_data[0]); }
283 : Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QChar back() const { return Q_ASSERT(!empty()), QChar(m_data[m_size - 1]); }
284 :
285 : //
286 : // Qt compatibility API:
287 : //
288 : Q_REQUIRED_RESULT Q_DECL_CONSTEXPR bool isNull() const Q_DECL_NOTHROW { return !m_data; }
289 : Q_REQUIRED_RESULT Q_DECL_CONSTEXPR bool isEmpty() const Q_DECL_NOTHROW { return empty(); }
290 : Q_REQUIRED_RESULT Q_DECL_CONSTEXPR int length() const /* not nothrow! */
291 : { return Q_ASSERT(int(size()) == size()), int(size()); }
292 : Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QChar first() const { return front(); }
293 : Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QChar last() const { return back(); }
294 : private:
295 : qsizetype m_size;
296 : const storage_type *m_data;
297 : };
298 : Q_DECLARE_TYPEINFO(QStringView, Q_PRIMITIVE_TYPE);
299 :
300 : template <typename QStringLike, typename std::enable_if<
301 : std::is_same<QStringLike, QString>::value || std::is_same<QStringLike, QStringRef>::value,
302 : bool>::type = true>
303 : inline QStringView qToStringViewIgnoringNull(const QStringLike &s) Q_DECL_NOTHROW
304 : { return QStringView(s.data(), s.size()); }
305 :
306 : QT_END_NAMESPACE
307 :
308 : #endif /* QSTRINGVIEW_H */
|