1// Copyright (C) 2020 The Qt Company Ltd.
2// Copyright (C) 2018 Intel Corporation.
3// Copyright (C) 2019 Mail.ru Group.
4// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
5// Qt-Security score:critical reason:data-parser
6
7#include "qstringref.h"
8
9#include <QtCore/qnumeric.h>
10#include <QtCore/qlist.h>
11#include <QtCore/qvarlengtharray.h>
12
13#ifdef Q_OS_MACOS
14#include <private/qcore_mac_p.h>
15#endif
16
17#include <private/qfunctions_p.h>
18#include <private/qstringalgorithms_p.h>
19#include <private/qstringconverter_p.h>
20#include <private/qstringiterator_p.h>
21#include <private/qunicodetables_p.h>
22
23#include <limits.h>
24#include <string.h>
25#include <stdlib.h>
26#include <stdio.h>
27#include <stdarg.h>
28#include <wchar.h>
29
30#ifdef Q_OS_WIN
31# include <qt_windows.h>
32#endif
33
34#ifdef truncate
35# undef truncate
36#endif
37
38QT_BEGIN_NAMESPACE
39
40// internal
41static inline qsizetype qFindChar(QStringView str, QChar ch, qsizetype from, Qt::CaseSensitivity cs) noexcept;
42
43static inline bool qt_starts_with(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs);
44static inline bool qt_starts_with(QStringView haystack, QLatin1String needle, Qt::CaseSensitivity cs);
45static inline bool qt_starts_with(QStringView haystack, QChar needle, Qt::CaseSensitivity cs);
46static inline bool qt_ends_with(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs);
47static inline bool qt_ends_with(QStringView haystack, QLatin1String needle, Qt::CaseSensitivity cs);
48static inline bool qt_ends_with(QStringView haystack, QChar needle, Qt::CaseSensitivity cs);
49
50// Code imported from QUnicodeTables
51
52namespace QUnicodeTablesPrivate
53{
54 static const unsigned short specialCaseMap[] = {
55 0x0, // placeholder
56 0x1, 0x2c65, 0x1, 0x2c66, 0x1, 0x2c7e, 0x1, 0x2c7f, 0x1, 0x2c6f, 0x1, 0x2c6d,
57 0x1, 0x2c70, 0x1, 0xa7ab, 0x1, 0xa7ac, 0x1, 0xa78d, 0x1, 0xa7aa, 0x1, 0xa7ae,
58 0x1, 0x2c62, 0x1, 0xa7ad, 0x1, 0x2c6e, 0x1, 0x2c64, 0x1, 0xa7c5, 0x1, 0xa7b1,
59 0x1, 0xa7b2, 0x1, 0xa7b0, 0x1, 0xab70, 0x1, 0xab71, 0x1, 0xab72, 0x1, 0xab73,
60 0x1, 0xab74, 0x1, 0xab75, 0x1, 0xab76, 0x1, 0xab77, 0x1, 0xab78, 0x1, 0xab79,
61 0x1, 0xab7a, 0x1, 0xab7b, 0x1, 0xab7c, 0x1, 0xab7d, 0x1, 0xab7e, 0x1, 0xab7f,
62 0x1, 0xab80, 0x1, 0xab81, 0x1, 0xab82, 0x1, 0xab83, 0x1, 0xab84, 0x1, 0xab85,
63 0x1, 0xab86, 0x1, 0xab87, 0x1, 0xab88, 0x1, 0xab89, 0x1, 0xab8a, 0x1, 0xab8b,
64 0x1, 0xab8c, 0x1, 0xab8d, 0x1, 0xab8e, 0x1, 0xab8f, 0x1, 0xab90, 0x1, 0xab91,
65 0x1, 0xab92, 0x1, 0xab93, 0x1, 0xab94, 0x1, 0xab95, 0x1, 0xab96, 0x1, 0xab97,
66 0x1, 0xab98, 0x1, 0xab99, 0x1, 0xab9a, 0x1, 0xab9b, 0x1, 0xab9c, 0x1, 0xab9d,
67 0x1, 0xab9e, 0x1, 0xab9f, 0x1, 0xaba0, 0x1, 0xaba1, 0x1, 0xaba2, 0x1, 0xaba3,
68 0x1, 0xaba4, 0x1, 0xaba5, 0x1, 0xaba6, 0x1, 0xaba7, 0x1, 0xaba8, 0x1, 0xaba9,
69 0x1, 0xabaa, 0x1, 0xabab, 0x1, 0xabac, 0x1, 0xabad, 0x1, 0xabae, 0x1, 0xabaf,
70 0x1, 0xabb0, 0x1, 0xabb1, 0x1, 0xabb2, 0x1, 0xabb3, 0x1, 0xabb4, 0x1, 0xabb5,
71 0x1, 0xabb6, 0x1, 0xabb7, 0x1, 0xabb8, 0x1, 0xabb9, 0x1, 0xabba, 0x1, 0xabbb,
72 0x1, 0xabbc, 0x1, 0xabbd, 0x1, 0xabbe, 0x1, 0xabbf, 0x1, 0xa64a, 0x1, 0xa77d,
73 0x1, 0xa7c6, 0x1, 0x6b, 0x1, 0xe5, 0x1, 0x26b, 0x1, 0x27d, 0x1, 0x23a,
74 0x1, 0x23e, 0x1, 0x251, 0x1, 0x271, 0x1, 0x250, 0x1, 0x252, 0x1, 0x23f,
75 0x1, 0x240, 0x1, 0x1d79, 0x1, 0x265, 0x1, 0x266, 0x1, 0x25c, 0x1, 0x261,
76 0x1, 0x26c, 0x1, 0x26a, 0x1, 0x29e, 0x1, 0x287, 0x1, 0x29d, 0x1, 0x282,
77 0x1, 0x1d8e, 0x1, 0x13a0, 0x1, 0x13a1, 0x1, 0x13a2, 0x1, 0x13a3, 0x1, 0x13a4,
78 0x1, 0x13a5, 0x1, 0x13a6, 0x1, 0x13a7, 0x1, 0x13a8, 0x1, 0x13a9, 0x1, 0x13aa,
79 0x1, 0x13ab, 0x1, 0x13ac, 0x1, 0x13ad, 0x1, 0x13ae, 0x1, 0x13af, 0x1, 0x13b0,
80 0x1, 0x13b1, 0x1, 0x13b2, 0x1, 0x13b3, 0x1, 0x13b4, 0x1, 0x13b5, 0x1, 0x13b6,
81 0x1, 0x13b7, 0x1, 0x13b8, 0x1, 0x13b9, 0x1, 0x13ba, 0x1, 0x13bb, 0x1, 0x13bc,
82 0x1, 0x13bd, 0x1, 0x13be, 0x1, 0x13bf, 0x1, 0x13c0, 0x1, 0x13c1, 0x1, 0x13c2,
83 0x1, 0x13c3, 0x1, 0x13c4, 0x1, 0x13c5, 0x1, 0x13c6, 0x1, 0x13c7, 0x1, 0x13c8,
84 0x1, 0x13c9, 0x1, 0x13ca, 0x1, 0x13cb, 0x1, 0x13cc, 0x1, 0x13cd, 0x1, 0x13ce,
85 0x1, 0x13cf, 0x1, 0x13d0, 0x1, 0x13d1, 0x1, 0x13d2, 0x1, 0x13d3, 0x1, 0x13d4,
86 0x1, 0x13d5, 0x1, 0x13d6, 0x1, 0x13d7, 0x1, 0x13d8, 0x1, 0x13d9, 0x1, 0x13da,
87 0x1, 0x13db, 0x1, 0x13dc, 0x1, 0x13dd, 0x1, 0x13de, 0x1, 0x13df, 0x1, 0x13e0,
88 0x1, 0x13e1, 0x1, 0x13e2, 0x1, 0x13e3, 0x1, 0x13e4, 0x1, 0x13e5, 0x1, 0x13e6,
89 0x1, 0x13e7, 0x1, 0x13e8, 0x1, 0x13e9, 0x1, 0x13ea, 0x1, 0x13eb, 0x1, 0x13ec,
90 0x1, 0x13ed, 0x1, 0x13ee, 0x1, 0x13ef, 0x2, 0x53, 0x73, 0x2, 0x53, 0x53,
91 0x2, 0x69, 0x307, 0x2, 0x46, 0x66, 0x2, 0x46, 0x46, 0x2, 0x46, 0x69,
92 0x2, 0x46, 0x49, 0x2, 0x46, 0x6c, 0x2, 0x46, 0x4c, 0x3, 0x46, 0x66,
93 0x69, 0x3, 0x46, 0x46, 0x49, 0x3, 0x46, 0x66, 0x6c, 0x3, 0x46, 0x46,
94 0x4c, 0x2, 0x53, 0x74, 0x2, 0x53, 0x54, 0x2, 0x535, 0x582, 0x2, 0x535,
95 0x552, 0x2, 0x544, 0x576, 0x2, 0x544, 0x546, 0x2, 0x544, 0x565, 0x2, 0x544,
96 0x535, 0x2, 0x544, 0x56b, 0x2, 0x544, 0x53b, 0x2, 0x54e, 0x576, 0x2, 0x54e,
97 0x546, 0x2, 0x544, 0x56d, 0x2, 0x544, 0x53d, 0x2, 0x2bc, 0x4e, 0x3, 0x399,
98 0x308, 0x301, 0x3, 0x3a5, 0x308, 0x301, 0x2, 0x4a, 0x30c, 0x2, 0x48, 0x331,
99 0x2, 0x54, 0x308, 0x2, 0x57, 0x30a, 0x2, 0x59, 0x30a, 0x2, 0x41, 0x2be,
100 0x2, 0x3a5, 0x313, 0x3, 0x3a5, 0x313, 0x300, 0x3, 0x3a5, 0x313, 0x301, 0x3,
101 0x3a5, 0x313, 0x342, 0x2, 0x391, 0x342, 0x2, 0x397, 0x342, 0x3, 0x399, 0x308,
102 0x300, 0x2, 0x399, 0x342, 0x3, 0x399, 0x308, 0x342, 0x3, 0x3a5, 0x308, 0x300,
103 0x2, 0x3a1, 0x313, 0x2, 0x3a5, 0x342, 0x3, 0x3a5, 0x308, 0x342, 0x2, 0x3a9,
104 0x342, 0x2, 0x1f08, 0x399, 0x2, 0x1f09, 0x399, 0x2, 0x1f0a, 0x399, 0x2, 0x1f0b,
105 0x399, 0x2, 0x1f0c, 0x399, 0x2, 0x1f0d, 0x399, 0x2, 0x1f0e, 0x399, 0x2, 0x1f0f,
106 0x399, 0x2, 0x1f28, 0x399, 0x2, 0x1f29, 0x399, 0x2, 0x1f2a, 0x399, 0x2, 0x1f2b,
107 0x399, 0x2, 0x1f2c, 0x399, 0x2, 0x1f2d, 0x399, 0x2, 0x1f2e, 0x399, 0x2, 0x1f2f,
108 0x399, 0x2, 0x1f68, 0x399, 0x2, 0x1f69, 0x399, 0x2, 0x1f6a, 0x399, 0x2, 0x1f6b,
109 0x399, 0x2, 0x1f6c, 0x399, 0x2, 0x1f6d, 0x399, 0x2, 0x1f6e, 0x399, 0x2, 0x1f6f,
110 0x399, 0x2, 0x391, 0x399, 0x2, 0x397, 0x399, 0x2, 0x3a9, 0x399, 0x2, 0x1fba,
111 0x345, 0x2, 0x1fba, 0x399, 0x2, 0x386, 0x345, 0x2, 0x386, 0x399, 0x2, 0x1fca,
112 0x345, 0x2, 0x1fca, 0x399, 0x2, 0x389, 0x345, 0x2, 0x389, 0x399, 0x2, 0x1ffa,
113 0x345, 0x2, 0x1ffa, 0x399, 0x2, 0x38f, 0x345, 0x2, 0x38f, 0x399, 0x3, 0x391,
114 0x342, 0x345, 0x3, 0x391, 0x342, 0x399, 0x3, 0x397, 0x342, 0x345, 0x3, 0x397,
115 0x342, 0x399, 0x3, 0x3a9, 0x342, 0x345, 0x3, 0x3a9, 0x342, 0x399, 0x1, 0xa64b
116 };
117}
118
119// Code imported from QChar
120
121namespace QCharPrivate
122{
123 template<typename T>
124 Q_DECL_CONST_FUNCTION static inline T convertCase_helper(T uc, QUnicodeTables::Case which) noexcept
125 {
126 const auto fold = QUnicodeTables::properties(ucs4: uc)->cases[which];
127
128 if (Q_UNLIKELY(fold.special)) {
129 const ushort *specialCase = QUnicodeTablesPrivate::specialCaseMap + fold.diff;
130 // so far, there are no special cases beyond BMP (guaranteed by the qunicodetables
131 // generator)
132 return *specialCase == 1 ? specialCase[1] : uc;
133 }
134
135 return uc + fold.diff;
136 }
137
138 [[maybe_unused]] static inline char32_t foldCase(char32_t ch, char32_t &last) noexcept
139 {
140 char32_t ucs4 = ch;
141 if (QChar::isLowSurrogate(ucs4) && QChar::isHighSurrogate(ucs4: last))
142 ucs4 = QChar::surrogateToUcs4(high: last, low: ucs4);
143 last = ch;
144 return convertCase_helper(uc: ucs4, which: QUnicodeTables::CaseFold);
145 }
146
147 static inline char16_t foldCase(char16_t ch) noexcept
148 {
149 return convertCase_helper(uc: ch, which: QUnicodeTables::CaseFold);
150 }
151
152 static inline QChar foldCase(QChar ch) noexcept
153 {
154 return QChar(foldCase(ch: ch.unicode()));
155 }
156}
157
158static int qt_compare_strings(QStringView lhs, QStringView rhs, Qt::CaseSensitivity cs) noexcept
159{
160 return lhs.compare(other: rhs, cs);
161}
162
163static int qt_compare_strings(QStringView lhs, QLatin1String rhs, Qt::CaseSensitivity cs) noexcept
164{
165 return lhs.compare(s: rhs, cs);
166}
167
168static QByteArray qt_convert_to_local_8bit(QStringView string)
169{
170 if (string.isNull())
171 return QByteArray();
172 QStringEncoder fromUtf16(QStringEncoder::System, QStringEncoder::Flag::Stateless);
173 return fromUtf16(string);
174}
175
176static QByteArray qt_convert_to_utf8(QStringView str)
177{
178 if (str.isNull())
179 return QByteArray();
180
181 return QUtf8::convertFromUnicode(in: str);
182}
183
184static QList<uint> qt_convert_to_ucs4(QStringView string)
185{
186 QList<uint> v(string.size());
187 uint *a = const_cast<uint*>(v.constData());
188 QStringIterator it(string);
189 while (it.hasNext())
190 *a++ = it.next();
191 v.resize(size: a - v.constData());
192 return v;
193}
194
195/*!
196 \internal
197 \since 4.5
198*/
199int QStringRef::compare_helper(const QChar *data1, qsizetype length1, const QChar *data2, qsizetype length2,
200 Qt::CaseSensitivity cs) noexcept
201{
202 Q_ASSERT(length1 >= 0);
203 Q_ASSERT(length2 >= 0);
204 Q_ASSERT(data1 || length1 == 0);
205 Q_ASSERT(data2 || length2 == 0);
206 return qt_compare_strings(lhs: QStringView(data1, length1), rhs: QStringView(data2, length2), cs);
207}
208
209/*!
210 \internal
211 \since 5.0
212*/
213int QStringRef::compare_helper(const QChar *data1, qsizetype length1, const char *data2, qsizetype length2,
214 Qt::CaseSensitivity cs)
215{
216 Q_ASSERT(length1 >= 0);
217 Q_ASSERT(data1 || length1 == 0);
218 if (!data2)
219 return length1;
220 if (Q_UNLIKELY(length2 < 0))
221 length2 = qsizetype(strlen(s: data2));
222 // ### make me nothrow in all cases
223 QVarLengthArray<ushort> s2(length2);
224 const auto beg = reinterpret_cast<QChar *>(s2.data());
225 const auto end = QUtf8::convertToUnicode(buffer: beg, in: QByteArrayView(data2, length2));
226 return qt_compare_strings(lhs: QStringView(data1, length1), rhs: QStringView(beg, end - beg), cs);
227}
228
229/*!
230 \internal
231 \since 4.5
232*/
233int QStringRef::compare_helper(const QChar *data1, qsizetype length1, QLatin1String s2,
234 Qt::CaseSensitivity cs) noexcept
235{
236 Q_ASSERT(length1 >= 0);
237 Q_ASSERT(data1 || length1 == 0);
238 return qt_compare_strings(lhs: QStringView(data1, length1), rhs: s2, cs);
239}
240
241namespace {
242template<class ResultList, class StringSource>
243static ResultList splitString(const StringSource &source, QStringView sep,
244 Qt::SplitBehavior behavior, Qt::CaseSensitivity cs)
245{
246 ResultList list;
247 typename StringSource::size_type start = 0;
248 typename StringSource::size_type end;
249 typename StringSource::size_type extra = 0;
250 while ((end = QtPrivate::findString(QStringView(source.constData(), source.size()), start + extra, sep, cs)) != -1) {
251 if (start != end || behavior == Qt::KeepEmptyParts)
252 list.append(source.mid(start, end - start));
253 start = end + sep.size();
254 extra = (sep.size() == 0 ? 1 : 0);
255 }
256 if (start != source.size() || behavior == Qt::KeepEmptyParts)
257 list.append(source.mid(start));
258 return list;
259}
260
261} // namespace
262
263/*!
264 Splits the string into substrings references wherever \a sep occurs, and
265 returns the list of those strings.
266
267 See QString::split() for how \a sep, \a behavior and \a cs interact to form
268 the result.
269
270 \note All references are valid as long this string is alive. Destroying this
271 string will cause all references to be dangling pointers.
272
273 \since 5.14
274*/
275QList<QStringRef> QStringRef::split(const QString &sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const
276{
277 return splitString<QList<QStringRef>>(source: *this, sep, behavior, cs);
278}
279
280/*!
281 \overload
282 \since 5.14
283*/
284QList<QStringRef> QStringRef::split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const
285{
286 return splitString<QList<QStringRef>>(source: *this, sep: QStringView(&sep, 1), behavior, cs);
287}
288
289/*!
290 \class QStringRef
291 \inmodule QtCore5Compat
292 \since 4.3
293 \brief The QStringRef class provides a thin wrapper around QString substrings.
294 \reentrant
295 \ingroup tools
296 \ingroup string-processing
297
298 QStringRef provides a read-only subset of the QString API.
299
300 A string reference explicitly references a portion of a string()
301 with a given size(), starting at a specific position(). Calling
302 toString() returns a copy of the data as a real QString instance.
303
304 This class is designed to improve the performance of substring
305 handling when manipulating substrings obtained from existing QString
306 instances. QStringRef avoids the memory allocation and reference
307 counting overhead of a standard QString by simply referencing a
308 part of the original string. This can prove to be advantageous in
309 low level code, such as that used in a parser, at the expense of
310 potentially more complex code.
311
312 For most users, there are no semantic benefits to using QStringRef
313 instead of QString since QStringRef requires attention to be paid
314 to memory management issues, potentially making code more complex
315 to write and maintain.
316
317 \warning A QStringRef is only valid as long as the referenced
318 string exists. If the original string is deleted, the string
319 reference points to an invalid memory location.
320
321 We suggest that you only use this class in stable code where profiling
322 has clearly identified that performance improvements can be made by
323 replacing standard string operations with the optimized substring
324 handling provided by this class.
325
326 \sa {Implicitly Shared Classes}
327*/
328
329/*!
330 \typedef QStringRef::size_type
331 \internal
332*/
333
334/*!
335 \typedef QStringRef::value_type
336 \internal
337*/
338
339/*!
340 \typedef QStringRef::const_pointer
341 \internal
342*/
343
344/*!
345 \typedef QStringRef::const_reference
346 \internal
347*/
348
349/*!
350 \typedef QStringRef::const_iterator
351 \since 5.4
352
353 \sa QStringRef::const_reverse_iterator
354*/
355
356/*!
357 \typedef QStringRef::const_reverse_iterator
358 \since 5.7
359
360 \sa QStringRef::const_iterator
361*/
362
363/*!
364 \fn QStringRef::QStringRef()
365
366 Constructs an empty string reference.
367*/
368
369/*! \fn QStringRef::QStringRef(const QString *string, int position, int length)
370
371Constructs a string reference to the range of characters in the given
372\a string specified by the starting \a position and \a length in characters.
373
374\warning This function exists to improve performance as much as possible,
375and performs no bounds checking. For program correctness, \a position and
376\a length must describe a valid substring of \a string.
377
378This means that the starting \a position must be positive or 0 and smaller
379than \a string's length, and \a length must be positive or 0 but smaller than
380the string's length minus the starting \a position;
381i.e, 0 <= position < string->length() and
3820 <= length <= string->length() - position must both be satisfied.
383*/
384
385/*! \fn QStringRef::QStringRef(const QString *string)
386
387Constructs a string reference to the given \a string.
388*/
389
390/*! \fn QStringRef::QStringRef(const QStringRef &other)
391
392Constructs a copy of the \a other string reference.
393 */
394/*!
395\fn QStringRef::~QStringRef()
396
397Destroys the string reference.
398
399Since this class is only used to refer to string data, and does not take
400ownership of it, no memory is freed when instances are destroyed.
401*/
402
403/*!
404 \fn int QStringRef::position() const
405
406 Returns the starting position in the referenced string that is referred to
407 by the string reference.
408
409 \sa size(), string()
410*/
411
412/*!
413 \fn int QStringRef::size() const
414
415 Returns the number of characters referred to by the string reference.
416 Equivalent to length() and count().
417
418 \sa position(), string()
419*/
420/*!
421 \fn int QStringRef::count() const
422 Returns the number of characters referred to by the string reference.
423 Equivalent to size() and length().
424
425 \sa position(), string()
426*/
427/*!
428 \fn int QStringRef::length() const
429 Returns the number of characters referred to by the string reference.
430 Equivalent to size() and count().
431
432 \sa position(), string()
433*/
434
435
436/*!
437 \fn bool QStringRef::isEmpty() const
438
439 Returns \c true if the string reference has no characters; otherwise returns
440 \c false.
441
442 A string reference is empty if its size is zero.
443
444 \sa size()
445*/
446
447/*!
448 \fn bool QStringRef::isNull() const
449
450 Returns \c true if this string reference does not reference a string or if
451 the string it references is null (i.e. QString::isNull() is true).
452
453 \sa size()
454*/
455
456/*!
457 \fn const QString *QStringRef::string() const
458
459 Returns a pointer to the string referred to by the string reference, or
460 0 if it does not reference a string.
461
462 \sa unicode()
463*/
464
465
466/*!
467 \fn const QChar *QStringRef::unicode() const
468
469 Returns a Unicode representation of the string reference. Since
470 the data stems directly from the referenced string, it is not
471 \\0'-terminated unless the string reference includes the string's
472 null terminator.
473
474 \sa string()
475*/
476
477/*!
478 \fn const QChar *QStringRef::data() const
479
480 Same as unicode().
481*/
482
483/*!
484 \fn const QChar *QStringRef::constData() const
485
486 Same as unicode().
487*/
488
489/*!
490 \fn QStringRef::const_iterator QStringRef::begin() const
491 \since 5.4
492
493 Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first character in
494 the string.
495
496 \sa cbegin(), constBegin(), end(), constEnd(), rbegin(), rend()
497*/
498
499/*!
500 \fn QStringRef::const_iterator QStringRef::cbegin() const
501 \since 5.4
502
503 Same as begin().
504
505 \sa begin(), constBegin(), cend(), constEnd(), rbegin(), rend()
506*/
507
508/*!
509 \fn QStringRef::const_iterator QStringRef::constBegin() const
510 \since 5.9
511
512 Same as begin().
513
514 \sa begin(), cend(), constEnd(), rbegin(), rend()
515*/
516
517/*!
518 \fn QStringRef::const_iterator QStringRef::end() const
519 \since 5.4
520
521 Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary
522 character after the last character in the list.
523
524 \sa cbegin(), constBegin(), end(), constEnd(), rbegin(), rend()
525*/
526
527/*! \fn QStringRef::const_iterator QStringRef::cend() const
528 \since 5.4
529
530 Same as end().
531
532 \sa end(), constEnd(), cbegin(), constBegin(), rbegin(), rend()
533*/
534
535/*! \fn QStringRef::const_iterator QStringRef::constEnd() const
536 \since 5.9
537
538 Same as end().
539
540 \sa end(), cend(), cbegin(), constBegin(), rbegin(), rend()
541*/
542
543/*!
544 \fn QStringRef::const_reverse_iterator QStringRef::rbegin() const
545 \since 5.7
546
547 Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing to the first
548 character in the string, in reverse order.
549
550 \sa begin(), crbegin(), rend()
551*/
552
553/*!
554 \fn QStringRef::const_reverse_iterator QStringRef::crbegin() const
555 \since 5.7
556
557 Same as rbegin().
558
559 \sa begin(), rbegin(), rend()
560*/
561
562/*!
563 \fn QStringRef::const_reverse_iterator QStringRef::rend() const
564 \since 5.7
565
566 Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to one past
567 the last character in the string, in reverse order.
568
569 \sa end(), crend(), rbegin()
570*/
571
572
573/*!
574 \fn QStringRef::const_reverse_iterator QStringRef::crend() const
575 \since 5.7
576
577 Same as rend().
578
579 \sa end(), rend(), rbegin()
580*/
581
582/*!
583 Returns a copy of the string reference as a QString object.
584
585 If the string reference is not a complete reference of the string
586 (meaning that position() is 0 and size() equals string()->size()),
587 this function will allocate a new string to return.
588
589 \sa string()
590*/
591
592QString QStringRef::toString() const {
593 if (isNull())
594 return QString();
595 if (m_size && m_position == 0 && m_size == m_string->size())
596 return *m_string;
597 return QString(m_string->unicode() + m_position, m_size);
598}
599
600
601/*! \relates QStringRef
602
603 Returns \c true if string reference \a s1 is lexically equal to string reference \a s2; otherwise
604 returns \c false.
605*/
606bool operator==(const QStringRef &s1,const QStringRef &s2) noexcept
607{
608 return s1.size() == s2.size() && qt_compare_strings(lhs: s1, rhs: s2, cs: Qt::CaseSensitive) == 0;
609}
610
611/*! \relates QStringRef
612
613 Returns \c true if string \a s1 is lexically equal to string reference \a s2; otherwise
614 returns \c false.
615*/
616bool operator==(const QString &s1,const QStringRef &s2) noexcept
617{
618 return s1.size() == s2.size() && qt_compare_strings(lhs: s1, rhs: s2, cs: Qt::CaseSensitive) == 0;
619}
620
621/*! \relates QStringRef
622
623 Returns \c true if string \a s1 is lexically equal to string reference \a s2; otherwise
624 returns \c false.
625*/
626bool operator==(QLatin1String s1, const QStringRef &s2) noexcept
627{
628 if (s1.size() != s2.size())
629 return false;
630
631 return qt_compare_strings(lhs: s2, rhs: s1, cs: Qt::CaseSensitive) == 0;
632}
633
634/*!
635 \relates QStringRef
636
637 Returns \c true if string reference \a s1 is lexically less than
638 string reference \a s2; otherwise returns \c false.
639
640 \sa {Comparing Strings}
641*/
642bool operator<(const QStringRef &s1,const QStringRef &s2) noexcept
643{
644 return qt_compare_strings(lhs: s1, rhs: s2, cs: Qt::CaseSensitive) < 0;
645}
646
647/*!\fn bool operator<=(const QStringRef &s1,const QStringRef &s2)
648
649 \relates QStringRef
650
651 Returns \c true if string reference \a s1 is lexically less than
652 or equal to string reference \a s2; otherwise returns \c false.
653
654 \sa {Comparing Strings}
655*/
656
657/*!\fn bool operator>=(const QStringRef &s1,const QStringRef &s2)
658
659 \relates QStringRef
660
661 Returns \c true if string reference \a s1 is lexically greater than
662 or equal to string reference \a s2; otherwise returns \c false.
663
664 \sa {Comparing Strings}
665*/
666
667/*!\fn bool operator>(const QStringRef &s1,const QStringRef &s2)
668
669 \relates QStringRef
670
671 Returns \c true if string reference \a s1 is lexically greater than
672 string reference \a s2; otherwise returns \c false.
673
674 \sa {Comparing Strings}
675*/
676
677
678/*!
679 \fn const QChar QStringRef::at(int position) const
680
681 Returns the character at the given index \a position in the
682 string reference.
683
684 The \a position must be a valid index position in the string
685 (i.e., 0 <= \a position < size()).
686*/
687
688/*!
689 \fn QChar QStringRef::operator[](int position) const
690 \since 5.7
691
692 Returns the character at the given index \a position in the
693 string reference.
694
695 The \a position must be a valid index position in the string
696 reference (i.e., 0 <= \a position < size()).
697
698 \sa at()
699*/
700
701/*!
702 \fn QChar QStringRef::front() const
703 \since 5.10
704
705 Returns the first character in the string.
706 Same as \c{at(0)}.
707
708 This function is provided for STL compatibility.
709
710 \warning Calling this function on an empty string constitutes
711 undefined behavior.
712
713 \sa back(), at(), operator[]()
714*/
715
716/*!
717 \fn QChar QStringRef::back() const
718 \since 5.10
719
720 Returns the last character in the string.
721 Same as \c{at(size() - 1)}.
722
723 This function is provided for STL compatibility.
724
725 \warning Calling this function on an empty string constitutes
726 undefined behavior.
727
728 \sa front(), at(), operator[]()
729*/
730
731/*!
732 \fn void QStringRef::clear()
733
734 Clears the contents of the string reference by making it null and empty.
735
736 \sa isEmpty(), isNull()
737*/
738
739/*!
740 \fn QStringRef &QStringRef::operator=(const QStringRef &other)
741
742 Assigns the \a other string reference to this string reference, and
743 returns the result.
744*/
745
746/*!
747 \fn QStringRef &QStringRef::operator=(const QString *string)
748
749 Constructs a string reference to the given \a string and assigns it to
750 this string reference, returning the result.
751*/
752
753/*!
754 \fn bool QStringRef::operator==(const char * s) const
755
756 \overload operator==()
757
758 The \a s byte array is converted to a QStringRef using the
759 fromUtf8() function. This function stops conversion at the
760 first NUL character found, or the end of the byte array.
761
762 You can disable this operator by defining \c
763 QT_NO_CAST_FROM_ASCII when you compile your applications. This
764 can be useful if you want to ensure that all user-visible strings
765 go through QObject::tr(), for example.
766
767 Returns \c true if this string is lexically equal to the parameter
768 string \a s. Otherwise returns \c false.
769
770 \sa QT_NO_CAST_FROM_ASCII
771*/
772
773/*!
774 \fn bool QStringRef::operator!=(const char * s) const
775
776 \overload operator!=()
777
778 The \a s const char pointer is converted to a QStringRef using
779 the fromUtf8() function.
780
781 You can disable this operator by defining \c
782 QT_NO_CAST_FROM_ASCII when you compile your applications. This
783 can be useful if you want to ensure that all user-visible strings
784 go through QObject::tr(), for example.
785
786 Returns \c true if this string is not lexically equal to the parameter
787 string \a s. Otherwise returns \c false.
788
789 \sa QT_NO_CAST_FROM_ASCII
790*/
791
792/*!
793 \fn bool QStringRef::operator<(const char * s) const
794
795 \overload operator<()
796
797 The \a s const char pointer is converted to a QStringRef using
798 the fromUtf8() function.
799
800 You can disable this operator by defining \c
801 QT_NO_CAST_FROM_ASCII when you compile your applications. This
802 can be useful if you want to ensure that all user-visible strings
803 go through QObject::tr(), for example.
804
805 Returns \c true if this string is lexically smaller than the parameter
806 string \a s. Otherwise returns \c false.
807
808 \sa QT_NO_CAST_FROM_ASCII
809*/
810
811/*!
812 \fn bool QStringRef::operator<=(const char * s) const
813
814 \overload operator<=()
815
816 The \a s const char pointer is converted to a QStringRef using
817 the fromUtf8() function.
818
819 You can disable this operator by defining \c
820 QT_NO_CAST_FROM_ASCII when you compile your applications. This
821 can be useful if you want to ensure that all user-visible strings
822 go through QObject::tr(), for example.
823
824 Returns \c true if this string is lexically smaller than or equal to the parameter
825 string \a s. Otherwise returns \c false.
826
827 \sa QT_NO_CAST_FROM_ASCII
828*/
829
830/*!
831 \fn bool QStringRef::operator>(const char * s) const
832
833
834 \overload operator>()
835
836 The \a s const char pointer is converted to a QStringRef using
837 the fromUtf8() function.
838
839 You can disable this operator by defining \c
840 QT_NO_CAST_FROM_ASCII when you compile your applications. This
841 can be useful if you want to ensure that all user-visible strings
842 go through QObject::tr(), for example.
843
844 Returns \c true if this string is lexically greater than the parameter
845 string \a s. Otherwise returns \c false.
846
847 \sa QT_NO_CAST_FROM_ASCII
848*/
849
850/*!
851 \fn bool QStringRef::operator>= (const char * s) const
852
853 \overload operator>=()
854
855 The \a s const char pointer is converted to a QStringRef using
856 the fromUtf8() function.
857
858 You can disable this operator by defining \c
859 QT_NO_CAST_FROM_ASCII when you compile your applications. This
860 can be useful if you want to ensure that all user-visible strings
861 go through QObject::tr(), for example.
862
863 Returns \c true if this string is lexically greater than or equal to the
864 parameter string \a s. Otherwise returns \c false.
865
866 \sa QT_NO_CAST_FROM_ASCII
867*/
868
869/*! Appends the string reference to \a string, and returns a new
870reference to the combined string data.
871 */
872QStringRef QStringRef::appendTo(QString *string) const
873{
874 if (!string)
875 return QStringRef();
876 int pos = string->size();
877 string->insert(i: pos, uc: unicode(), len: size());
878 return QStringRef(string, pos, size());
879}
880
881/*!
882 \fn int QStringRef::compare(const QStringRef &s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive)
883 \since 4.5
884
885 Compares \a s1 with \a s2 and returns a negative
886 integer if \a s1 is less than \a s2, a positive integer if it is greater
887 than \a s2, and zero if they are equal.
888
889 If \a cs is Qt::CaseSensitive, the comparison is case sensitive;
890 otherwise the comparison is case insensitive.
891*/
892
893/*!
894 \fn int QStringRef::compare(const QStringRef &s1, const QStringRef &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive)
895 \since 4.5
896 \overload
897
898 Compares \a s1 with \a s2 and returns a negative
899 integer if \a s1 is less than \a s2, a positive integer if it is greater
900 than \a s2, and zero if they are equal.
901
902 If \a cs is Qt::CaseSensitive, the comparison is case sensitive;
903 otherwise the comparison is case insensitive.
904*/
905
906/*!
907 \fn int QStringRef::compare(const QStringRef &s1, QLatin1String s2, Qt::CaseSensitivity cs = Qt::CaseSensitive)
908 \since 4.5
909 \overload
910
911 Compares \a s1 with \a s2 and returns a negative
912 integer if \a s1 is less than \a s2, a positive integer if it is greater
913 than \a s2, and zero if they are equal.
914
915 If \a cs is Qt::CaseSensitive, the comparison is case sensitive;
916 otherwise the comparison is case insensitive.
917*/
918
919/*!
920 \overload
921 \fn int QStringRef::compare(const QString &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
922 \since 4.5
923
924 Compares \c {*this} with \a other and returns a negative integer if
925 \c {*this} is less than \a other, a positive integer if it is greater
926 than \a other, and zero if they are equal.
927
928 If \a cs is Qt::CaseSensitive, the comparison is case sensitive;
929 otherwise the comparison is case insensitive.
930
931 Equivalent to \c {compare(*this, other, cs)}.
932*/
933
934/*!
935 \overload
936 \fn int QStringRef::compare(const QStringRef &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
937 \since 4.5
938
939 Compares \c {*this} with \a other and returns a negative integer if
940 \c {*this} is less than \a other, a positive integer if it is greater
941 than \a other, and zero if they are equal.
942
943 If \a cs is Qt::CaseSensitive, the comparison is case sensitive;
944 otherwise the comparison is case insensitive.
945
946 Equivalent to \c {compare(*this, other, cs)}.
947*/
948
949/*!
950 \overload
951 \fn int QStringRef::compare(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
952 \since 5.14
953
954 Compares \c {*this} with \a ch and returns a negative integer if \c {*this}
955 is less than \a ch, a positive integer if it is greater than \a ch, and zero
956 if they are equal. Here, \a ch interpreted as a string of length one.
957
958 If \a cs is Qt::CaseSensitive, the comparison is case sensitive;
959 otherwise the comparison is case insensitive.
960*/
961
962/*!
963 \overload
964 \fn int QStringRef::compare(QLatin1String other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
965 \since 4.5
966
967 Compares \c {*this} with \a other and returns a negative integer if \c {*this}
968 is less than \a other, a positive integer if it is greater than \a other, and
969 zero if they are equal.
970
971 If \a cs is Qt::CaseSensitive, the comparison is case sensitive;
972 otherwise the comparison is case insensitive.
973
974 Equivalent to \c {compare(*this, other, cs)}.
975*/
976
977/*!
978 \overload
979 \fn int QStringRef::compare(const QByteArray &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
980 \since 5.8
981
982 Compares \c {*this} with \a other and returns a negative integer if \c {*this}
983 is less than \a other, a positive integer if it is greater than \a other, and
984 zero if they are equal. The contents of \a other is interpreted as UTF-8.
985
986 If \a cs is Qt::CaseSensitive, the comparison is case sensitive;
987 otherwise the comparison is case insensitive.
988
989 Equivalent to \c {compare(*this, other, cs)}.
990*/
991
992/*!
993 \fn int QStringRef::localeAwareCompare(const QStringRef &s1, const QString & s2)
994 \since 4.5
995
996 Compares \a s1 with \a s2 and returns a negative
997 integer if \a s1 is less than \a s2, a positive integer if it is greater
998 than \a s2, and zero if they are equal.
999
1000 The comparison is performed in a locale- and also
1001 platform-dependent manner. Use this function to present sorted
1002 lists of strings to the user.
1003
1004 \sa compare(), QLocale, {Comparing Strings}
1005*/
1006
1007/*!
1008 \fn int QStringRef::localeAwareCompare(const QStringRef &s1, const QStringRef & s2)
1009 \since 4.5
1010 \overload
1011
1012 Compares \a s1 with \a s2 and returns a negative
1013 integer if \a s1 is less than \a s2, a positive integer if it is greater
1014 than \a s2, and zero if they are equal.
1015
1016 The comparison is performed in a locale- and also
1017 platform-dependent manner. Use this function to present sorted
1018 lists of strings to the user.
1019
1020 \sa {Comparing Strings}
1021*/
1022
1023/*!
1024 \fn int QStringRef::localeAwareCompare(const QString &other) const
1025 \since 4.5
1026 \overload
1027
1028 Compares \c {*this} with \a other and returns a negative integer if \c {*this}
1029 is less than \a other, a positive integer if it is greater than \a other, and
1030 zero if they are equal.
1031
1032 The comparison is performed in a locale- and also
1033 platform-dependent manner. Use this function to present sorted
1034 lists of strings to the user.
1035
1036 \sa {Comparing Strings}
1037*/
1038
1039/*!
1040 \fn int QStringRef::localeAwareCompare(const QStringRef &other) const
1041 \since 4.5
1042 \overload
1043
1044 Compares \c {*this} with \a other and returns a negative integer if \c {*this}
1045 is less than \a other, a positive integer if it is greater than \a other, and
1046 zero if they are equal.
1047
1048 The comparison is performed in a locale- and also
1049 platform-dependent manner. Use this function to present sorted
1050 lists of strings to the user.
1051
1052 \sa {Comparing Strings}
1053*/
1054
1055/*!
1056 \fn QStringRef::left(int n) const
1057 \since 5.2
1058
1059 Returns a substring reference to the \a n leftmost characters
1060 of the string.
1061
1062 If \a n is greater than or equal to size(), or less than zero,
1063 a reference to the entire string is returned.
1064
1065 \sa right(), mid(), startsWith(), chopped(), chop(), truncate()
1066*/
1067QStringRef QStringRef::left(int n) const
1068{
1069 if (size_t(n) >= size_t(m_size))
1070 return *this;
1071 return QStringRef(m_string, m_position, n);
1072}
1073
1074/*!
1075 \fn QStringRef::right(int n) const
1076 \since 5.2
1077
1078 Returns a substring reference to the \a n rightmost characters
1079 of the string.
1080
1081 If \a n is greater than or equal to size(), or less than zero,
1082 a reference to the entire string is returned.
1083
1084 \sa left(), mid(), endsWith(), chopped(), chop(), truncate()
1085*/
1086QStringRef QStringRef::right(int n) const
1087{
1088 if (size_t(n) >= size_t(m_size))
1089 return *this;
1090 return QStringRef(m_string, m_size - n + m_position, n);
1091}
1092
1093/*!
1094 \fn QStringRef QStringRef::mid(int position, int n = -1) const
1095 \since 5.2
1096
1097 Returns a substring reference to \a n characters of this string,
1098 starting at the specified \a position.
1099
1100 If the \a position exceeds the length of the string, a null
1101 reference is returned.
1102
1103 If there are less than \a n characters available in the string,
1104 starting at the given \a position, or if \a n is -1 (default), the
1105 function returns all characters from the specified \a position
1106 onwards.
1107
1108 \sa left(), right(), chopped(), chop(), truncate()
1109*/
1110QStringRef QStringRef::mid(int pos, int n) const
1111{
1112 qsizetype p = pos;
1113 qsizetype l = n;
1114 using namespace QtPrivate;
1115 switch (QContainerImplHelper::mid(originalLength: m_size, position: &p, length: &l)) {
1116 case QContainerImplHelper::Null:
1117 return QStringRef();
1118 case QContainerImplHelper::Empty:
1119 return QStringRef(m_string, 0, 0);
1120 case QContainerImplHelper::Full:
1121 return *this;
1122 case QContainerImplHelper::Subset:
1123 return QStringRef(m_string, p + m_position, l);
1124 }
1125 Q_UNREACHABLE_RETURN(QStringRef());
1126}
1127
1128/*!
1129 \fn QStringRef QStringRef::chopped(int len) const
1130 \since 5.10
1131
1132 Returns a substring reference to the size() - \a len leftmost characters
1133 of this string.
1134
1135 \note The behavior is undefined if \a len is negative or greater than size().
1136
1137 \sa endsWith(), left(), right(), mid(), chop(), truncate()
1138*/
1139
1140/*!
1141 \fn void QStringRef::truncate(int position)
1142 \since 5.6
1143
1144 Truncates the string at the given \a position index.
1145
1146 If the specified \a position index is beyond the end of the
1147 string, nothing happens.
1148
1149 If \a position is negative, it is equivalent to passing zero.
1150
1151 \sa QString::truncate()
1152*/
1153
1154/*!
1155 \fn void QStringRef::chop(int n)
1156 \since 5.8
1157
1158 Removes \a n characters from the end of the string.
1159
1160 If \a n is greater than or equal to size(), the result is an
1161 empty string; if \a n is negative, it is equivalent to passing zero.
1162
1163 \sa QString::chop(), truncate()
1164*/
1165
1166/*!
1167 \since 4.8
1168
1169 Returns the index position of the first occurrence of the string \a
1170 str in this string reference, searching forward from index position
1171 \a from. Returns -1 if \a str is not found.
1172
1173 If \a cs is Qt::CaseSensitive (default), the search is case
1174 sensitive; otherwise the search is case insensitive.
1175
1176 If \a from is -1, the search starts at the last character; if it is
1177 -2, at the next to last character and so on.
1178
1179 \sa QString::indexOf(), lastIndexOf(), contains(), count()
1180*/
1181int QStringRef::indexOf(const QString &str, int from, Qt::CaseSensitivity cs) const
1182{
1183 // ### Qt6: qsizetype
1184 return int(QtPrivate::findString(haystack: QStringView(unicode(), length()), from, needle: QStringView(str.unicode(), str.size()), cs));
1185}
1186
1187/*!
1188 \fn int QStringRef::indexOf(QStringView str, int from, Qt::CaseSensitivity cs) const
1189 \since 5.14
1190 \overload indexOf()
1191
1192 Returns the index position of the first occurrence of the string view \a str
1193 in this string reference, searching forward from index position \a from.
1194 Returns -1 if \a str is not found.
1195
1196 If \a cs is Qt::CaseSensitive (default), the search is case
1197 sensitive; otherwise the search is case insensitive.
1198
1199 If \a from is -1, the search starts at the last character; if it is
1200 -2, at the next to last character and so on.
1201
1202 \sa QString::indexOf(), QStringView::indexOf(), lastIndexOf(), contains(), count()
1203*/
1204
1205/*!
1206 \since 4.8
1207 \overload indexOf()
1208
1209 Returns the index position of the first occurrence of the
1210 character \a ch in the string reference, searching forward from
1211 index position \a from. Returns -1 if \a ch could not be found.
1212
1213 \sa QString::indexOf(), lastIndexOf(), contains(), count()
1214*/
1215int QStringRef::indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
1216{
1217 // ### Qt6: qsizetype
1218 return int(qFindChar(str: QStringView(unicode(), length()), ch, from, cs));
1219}
1220
1221/*!
1222 \since 4.8
1223
1224 Returns the index position of the first occurrence of the string \a
1225 str in this string reference, searching forward from index position
1226 \a from. Returns -1 if \a str is not found.
1227
1228 If \a cs is Qt::CaseSensitive (default), the search is case
1229 sensitive; otherwise the search is case insensitive.
1230
1231 If \a from is -1, the search starts at the last character; if it is
1232 -2, at the next to last character and so on.
1233
1234 \sa QString::indexOf(), lastIndexOf(), contains(), count()
1235*/
1236int QStringRef::indexOf(QLatin1String str, int from, Qt::CaseSensitivity cs) const
1237{
1238 // ### Qt6: qsizetype
1239 return int(QtPrivate::findString(haystack: QStringView(unicode(), size()), from, needle: str, cs));
1240}
1241
1242/*!
1243 \since 4.8
1244
1245 \overload indexOf()
1246
1247 Returns the index position of the first occurrence of the string
1248 reference \a str in this string reference, searching forward from
1249 index position \a from. Returns -1 if \a str is not found.
1250
1251 If \a cs is Qt::CaseSensitive (default), the search is case
1252 sensitive; otherwise the search is case insensitive.
1253
1254 \sa QString::indexOf(), lastIndexOf(), contains(), count()
1255*/
1256int QStringRef::indexOf(const QStringRef &str, int from, Qt::CaseSensitivity cs) const
1257{
1258 // ### Qt6: qsizetype
1259 return int(QtPrivate::findString(haystack: QStringView(unicode(), size()), from, needle: QStringView(str.unicode(), str.size()), cs));
1260}
1261
1262/*!
1263 \since 4.8
1264
1265 Returns the index position of the last occurrence of the string \a
1266 str in this string reference, searching backward from index position
1267 \a from. If \a from is -1 (default), the search starts at the last
1268 character; if \a from is -2, at the next to last character and so
1269 on. Returns -1 if \a str is not found.
1270
1271 If \a cs is Qt::CaseSensitive (default), the search is case
1272 sensitive; otherwise the search is case insensitive.
1273
1274 \sa QString::lastIndexOf(), indexOf(), contains(), count()
1275*/
1276int QStringRef::lastIndexOf(const QString &str, int from, Qt::CaseSensitivity cs) const
1277{
1278 // ### Qt6: qsizetype
1279 return int(QtPrivate::lastIndexOf(haystack: *this, from, needle: str, cs));
1280}
1281
1282/*!
1283 \since 4.8
1284 \overload lastIndexOf()
1285
1286 Returns the index position of the last occurrence of the character
1287 \a ch, searching backward from position \a from.
1288
1289 \sa QString::lastIndexOf(), indexOf(), contains(), count()
1290*/
1291int QStringRef::lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
1292{
1293 // ### Qt6: qsizetype
1294 return int(QStringView{ *this }.lastIndexOf(c: ch, from, cs));
1295}
1296
1297/*!
1298 \since 4.8
1299 \overload lastIndexOf()
1300
1301 Returns the index position of the last occurrence of the string \a
1302 str in this string reference, searching backward from index position
1303 \a from. If \a from is -1 (default), the search starts at the last
1304 character; if \a from is -2, at the next to last character and so
1305 on. Returns -1 if \a str is not found.
1306
1307 If \a cs is Qt::CaseSensitive (default), the search is case
1308 sensitive; otherwise the search is case insensitive.
1309
1310 \sa QString::lastIndexOf(), indexOf(), contains(), count()
1311*/
1312int QStringRef::lastIndexOf(QLatin1String str, int from, Qt::CaseSensitivity cs) const
1313{
1314 // ### Qt6: qsizetype
1315 return int(QtPrivate::lastIndexOf(haystack: *this, from, needle: str, cs));
1316}
1317
1318/*!
1319 \since 4.8
1320 \overload lastIndexOf()
1321
1322 Returns the index position of the last occurrence of the string
1323 reference \a str in this string reference, searching backward from
1324 index position \a from. If \a from is -1 (default), the search
1325 starts at the last character; if \a from is -2, at the next to last
1326 character and so on. Returns -1 if \a str is not found.
1327
1328 If \a cs is Qt::CaseSensitive (default), the search is case
1329 sensitive; otherwise the search is case insensitive.
1330
1331 \sa QString::lastIndexOf(), indexOf(), contains(), count()
1332*/
1333int QStringRef::lastIndexOf(const QStringRef &str, int from, Qt::CaseSensitivity cs) const
1334{
1335 // ### Qt6: qsizetype
1336 return int(QtPrivate::lastIndexOf(haystack: *this, from, needle: str, cs));
1337}
1338
1339/*!
1340 \fn int QStringRef::lastIndexOf(QStringView str, int from, Qt::CaseSensitivity cs) const
1341 \since 5.14
1342 \overload lastIndexOf()
1343
1344 Returns the index position of the last occurrence of the string view \a
1345 str in this string, searching backward from index position \a
1346 from. If \a from is -1 (default), the search starts at the last
1347 character; if \a from is -2, at the next to last character and so
1348 on. Returns -1 if \a str is not found.
1349
1350 If \a cs is Qt::CaseSensitive (default), the search is case
1351 sensitive; otherwise the search is case insensitive.
1352
1353 \sa indexOf(), contains(), count()
1354*/
1355
1356/*!
1357 \since 4.8
1358 Returns the number of (potentially overlapping) occurrences of
1359 the string \a str in this string reference.
1360
1361 If \a cs is Qt::CaseSensitive (default), the search is
1362 case sensitive; otherwise the search is case insensitive.
1363
1364 \sa QString::count(), contains(), indexOf()
1365*/
1366int QStringRef::count(const QString &str, Qt::CaseSensitivity cs) const
1367{
1368 // ### Qt6: qsizetype
1369 return int(QtPrivate::count(haystack: QStringView(unicode(), size()), needle: QStringView(str.unicode(), str.size()), cs));
1370}
1371
1372/*!
1373 \since 4.8
1374 \overload count()
1375
1376 Returns the number of occurrences of the character \a ch in the
1377 string reference.
1378
1379 If \a cs is Qt::CaseSensitive (default), the search is
1380 case sensitive; otherwise the search is case insensitive.
1381
1382 \sa QString::count(), contains(), indexOf()
1383*/
1384int QStringRef::count(QChar ch, Qt::CaseSensitivity cs) const
1385{
1386 // ### Qt6: qsizetype
1387 return int(QtPrivate::count(haystack: QStringView(unicode(), size()), needle: ch, cs));
1388}
1389
1390/*!
1391 \since 4.8
1392 \overload count()
1393
1394 Returns the number of (potentially overlapping) occurrences of the
1395 string reference \a str in this string reference.
1396
1397 If \a cs is Qt::CaseSensitive (default), the search is
1398 case sensitive; otherwise the search is case insensitive.
1399
1400 \sa QString::count(), contains(), indexOf()
1401*/
1402int QStringRef::count(const QStringRef &str, Qt::CaseSensitivity cs) const
1403{
1404 // ### Qt6: qsizetype
1405 return int(QtPrivate::count(haystack: QStringView(unicode(), size()), needle: QStringView(str.unicode(), str.size()), cs));
1406}
1407
1408/*!
1409 \since 5.9
1410
1411 Returns \c true if the string is read right to left.
1412
1413 \sa QString::isRightToLeft()
1414*/
1415bool QStringRef::isRightToLeft() const
1416{
1417 return QtPrivate::isRightToLeft(string: QStringView(unicode(), size()));
1418}
1419
1420/*!
1421 \since 4.8
1422
1423 Returns \c true if the string reference starts with \a str; otherwise
1424 returns \c false.
1425
1426 If \a cs is Qt::CaseSensitive (default), the search is
1427 case sensitive; otherwise the search is case insensitive.
1428
1429 \sa QString::startsWith(), endsWith()
1430*/
1431bool QStringRef::startsWith(const QString &str, Qt::CaseSensitivity cs) const
1432{
1433 return qt_starts_with(haystack: *this, needle: str, cs);
1434}
1435
1436/*!
1437 \since 4.8
1438 \overload startsWith()
1439 \sa QString::startsWith(), endsWith()
1440*/
1441bool QStringRef::startsWith(QLatin1String str, Qt::CaseSensitivity cs) const
1442{
1443 return qt_starts_with(haystack: *this, needle: str, cs);
1444}
1445
1446/*!
1447 \fn bool QStringRef::startsWith(QStringView str, Qt::CaseSensitivity cs) const
1448 \since 5.10
1449 \overload startsWith()
1450 \sa QString::startsWith(), endsWith()
1451*/
1452
1453/*!
1454 \since 4.8
1455 \overload startsWith()
1456 \sa QString::startsWith(), endsWith()
1457*/
1458bool QStringRef::startsWith(const QStringRef &str, Qt::CaseSensitivity cs) const
1459{
1460 return qt_starts_with(haystack: *this, needle: str, cs);
1461}
1462
1463/*!
1464 \since 4.8
1465 \overload startsWith()
1466
1467 Returns \c true if the string reference starts with \a ch; otherwise
1468 returns \c false.
1469
1470 If \a cs is Qt::CaseSensitive (default), the search is case
1471 sensitive; otherwise the search is case insensitive.
1472
1473 \sa QString::startsWith(), endsWith()
1474*/
1475bool QStringRef::startsWith(QChar ch, Qt::CaseSensitivity cs) const
1476{
1477 return qt_starts_with(haystack: *this, needle: ch, cs);
1478}
1479
1480/*!
1481 \since 4.8
1482 Returns \c true if the string reference ends with \a str; otherwise
1483 returns \c false.
1484
1485 If \a cs is Qt::CaseSensitive (default), the search is case
1486 sensitive; otherwise the search is case insensitive.
1487
1488 \sa QString::endsWith(), startsWith()
1489*/
1490bool QStringRef::endsWith(const QString &str, Qt::CaseSensitivity cs) const
1491{
1492 return qt_ends_with(haystack: *this, needle: str, cs);
1493}
1494
1495/*!
1496 \since 4.8
1497 \overload endsWith()
1498
1499 Returns \c true if the string reference ends with \a ch; otherwise
1500 returns \c false.
1501
1502 If \a cs is Qt::CaseSensitive (default), the search is case
1503 sensitive; otherwise the search is case insensitive.
1504
1505 \sa QString::endsWith(), endsWith()
1506*/
1507bool QStringRef::endsWith(QChar ch, Qt::CaseSensitivity cs) const
1508{
1509 return qt_ends_with(haystack: *this, needle: ch, cs);
1510}
1511
1512/*!
1513 \since 4.8
1514 \overload endsWith()
1515 \sa QString::endsWith(), endsWith()
1516*/
1517bool QStringRef::endsWith(QLatin1String str, Qt::CaseSensitivity cs) const
1518{
1519 return qt_ends_with(haystack: *this, needle: str, cs);
1520}
1521
1522/*!
1523 \fn bool QStringRef::endsWith(QStringView str, Qt::CaseSensitivity cs) const
1524 \since 5.10
1525 \overload endsWith()
1526 \sa QString::endsWith(), startsWith()
1527*/
1528
1529/*!
1530 \since 4.8
1531 \overload endsWith()
1532 \sa QString::endsWith(), endsWith()
1533*/
1534bool QStringRef::endsWith(const QStringRef &str, Qt::CaseSensitivity cs) const
1535{
1536 return qt_ends_with(haystack: *this, needle: str, cs);
1537}
1538
1539/*! \fn bool QStringRef::contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
1540
1541 \since 4.8
1542 Returns \c true if this string reference contains an occurrence of
1543 the string \a str; otherwise returns \c false.
1544
1545 If \a cs is Qt::CaseSensitive (default), the search is
1546 case sensitive; otherwise the search is case insensitive.
1547
1548 \sa indexOf(), count()
1549*/
1550
1551/*! \fn bool QStringRef::contains(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
1552
1553 \overload contains()
1554 \since 4.8
1555
1556 Returns \c true if this string contains an occurrence of the
1557 character \a ch; otherwise returns \c false.
1558
1559 If \a cs is Qt::CaseSensitive (default), the search is
1560 case sensitive; otherwise the search is case insensitive.
1561
1562*/
1563
1564/*! \fn bool QStringRef::contains(const QStringRef &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
1565 \overload contains()
1566 \since 4.8
1567
1568 Returns \c true if this string reference contains an occurrence of
1569 the string reference \a str; otherwise returns \c false.
1570
1571 If \a cs is Qt::CaseSensitive (default), the search is
1572 case sensitive; otherwise the search is case insensitive.
1573
1574 \sa indexOf(), count()
1575*/
1576
1577/*! \fn bool QStringRef::contains(QLatin1String str, Qt::CaseSensitivity cs) const
1578 \since 4.8
1579 \overload contains()
1580
1581 Returns \c true if this string reference contains an occurrence of
1582 the string \a str; otherwise returns \c false.
1583
1584 If \a cs is Qt::CaseSensitive (default), the search is
1585 case sensitive; otherwise the search is case insensitive.
1586
1587 \sa indexOf(), count()
1588*/
1589
1590/*! \fn bool QStringRef::contains(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
1591 \since 5.14
1592 \overload contains()
1593
1594 Returns \c true if this string reference contains an occurrence of
1595 the string view \a str; otherwise returns \c false.
1596
1597 If \a cs is Qt::CaseSensitive (default), the search is
1598 case sensitive; otherwise the search is case insensitive.
1599
1600 \sa indexOf(), count()
1601*/
1602
1603template <typename Haystack, typename Needle>
1604bool qt_starts_with_impl(Haystack haystack, Needle needle, Qt::CaseSensitivity cs) noexcept
1605{
1606 if (haystack.isNull())
1607 return needle.isNull(); // historical behavior, consider changing in ### Qt 6.
1608 const auto haystackLen = haystack.size();
1609 const auto needleLen = needle.size();
1610 if (haystackLen == 0)
1611 return needleLen == 0;
1612 if (needleLen > haystackLen)
1613 return false;
1614
1615 return qt_compare_strings(haystack.left(needleLen), needle, cs) == 0;
1616}
1617
1618static inline bool qt_starts_with(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs)
1619{
1620 return qt_starts_with_impl(haystack, needle, cs);
1621}
1622
1623static inline bool qt_starts_with(QStringView haystack, QLatin1String needle, Qt::CaseSensitivity cs)
1624{
1625 return qt_starts_with_impl(haystack, needle, cs);
1626}
1627
1628static inline bool qt_starts_with(QStringView haystack, QChar needle, Qt::CaseSensitivity cs)
1629{
1630 return haystack.size()
1631 && (cs == Qt::CaseSensitive ? haystack.front() == needle
1632 : QCharPrivate::foldCase(ch: haystack.front()) == QCharPrivate::foldCase(ch: needle));
1633}
1634
1635template <typename Haystack, typename Needle>
1636bool qt_ends_with_impl(Haystack haystack, Needle needle, Qt::CaseSensitivity cs) noexcept
1637{
1638 if (haystack.isNull())
1639 return needle.isNull(); // historical behavior, consider changing in ### Qt 6.
1640 const auto haystackLen = haystack.size();
1641 const auto needleLen = needle.size();
1642 if (haystackLen == 0)
1643 return needleLen == 0;
1644 if (haystackLen < needleLen)
1645 return false;
1646
1647 return qt_compare_strings(haystack.right(needleLen), needle, cs) == 0;
1648}
1649
1650static inline bool qt_ends_with(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs)
1651{
1652 return qt_ends_with_impl(haystack, needle, cs);
1653}
1654
1655static inline bool qt_ends_with(QStringView haystack, QLatin1String needle, Qt::CaseSensitivity cs)
1656{
1657 return qt_ends_with_impl(haystack, needle, cs);
1658}
1659
1660static inline bool qt_ends_with(QStringView haystack, QChar needle, Qt::CaseSensitivity cs)
1661{
1662 return haystack.size()
1663 && (cs == Qt::CaseSensitive ? haystack.back() == needle
1664 : QCharPrivate::foldCase(ch: haystack.back()) == QCharPrivate::foldCase(ch: needle));
1665}
1666
1667/*!
1668 \internal
1669
1670 Returns the index position of the first occurrence of the
1671 character \a ch in the string given by \a str and \a len,
1672 searching forward from index
1673 position \a from. Returns -1 if \a ch could not be found.
1674*/
1675
1676static inline qsizetype qFindChar(QStringView str, QChar ch, qsizetype from, Qt::CaseSensitivity cs) noexcept
1677{
1678 if (from < 0)
1679 from = qMax(a: from + str.size(), b: qsizetype(0));
1680 if (from < str.size()) {
1681 const char16_t *s = str.utf16();
1682 char16_t c = ch.unicode();
1683 const char16_t *n = s + from;
1684 const char16_t *e = s + str.size();
1685 if (cs == Qt::CaseSensitive) {
1686 n = QtPrivate::qustrchr(str: QStringView(n, e), ch: c);
1687 if (n != e)
1688 return n - s;
1689 } else {
1690 c = QCharPrivate::foldCase(ch: c);
1691 --n;
1692 while (++n != e)
1693 if (QCharPrivate::foldCase(ch: *n) == c)
1694 return n - s;
1695 }
1696 }
1697 return -1;
1698}
1699
1700/*!
1701 \since 4.8
1702
1703 Returns a Latin-1 representation of the string as a QByteArray.
1704
1705 The returned byte array is undefined if the string contains non-Latin1
1706 characters. Those characters may be suppressed or replaced with a
1707 question mark.
1708
1709 \sa toUtf8(), toLocal8Bit(), QStringEncoder
1710*/
1711QByteArray QStringRef::toLatin1() const
1712{
1713 return QStringView{ *this }.toLatin1();
1714}
1715
1716/*!
1717 \since 4.8
1718
1719 Returns the local 8-bit representation of the string as a
1720 QByteArray. The returned byte array is undefined if the string
1721 contains characters not supported by the local 8-bit encoding.
1722
1723 On Unix systems this is equivalent to toUtf8(), on Windows the systems
1724 current code page is being used.
1725
1726 If this string contains any characters that cannot be encoded in the
1727 locale, the returned byte array is undefined. Those characters may be
1728 suppressed or replaced by another.
1729
1730 \sa toLatin1(), toUtf8(), QStringEncoder
1731*/
1732QByteArray QStringRef::toLocal8Bit() const
1733{
1734 return qt_convert_to_local_8bit(string: *this);
1735}
1736
1737/*!
1738 \since 4.8
1739
1740 Returns a UTF-8 representation of the string as a QByteArray.
1741
1742 UTF-8 is a Unicode codec and can represent all characters in a Unicode
1743 string like QString.
1744
1745 \sa toLatin1(), toLocal8Bit(), QStringEncoder
1746*/
1747QByteArray QStringRef::toUtf8() const
1748{
1749 return qt_convert_to_utf8(str: *this);
1750}
1751
1752/*!
1753 \since 4.8
1754
1755 Returns a UCS-4/UTF-32 representation of the string as a QList<uint>.
1756
1757 UCS-4 is a Unicode codec and therefore it is lossless. All characters from
1758 this string will be encoded in UCS-4. Any invalid sequence of code units in
1759 this string is replaced by the Unicode's replacement character
1760 (QChar::ReplacementCharacter, which corresponds to \c{U+FFFD}).
1761
1762 The returned list is not \\0'-terminated.
1763
1764 \sa toUtf8(), toLatin1(), toLocal8Bit(), QStringEncoder
1765*/
1766QList<uint> QStringRef::toUcs4() const
1767{
1768 return qt_convert_to_ucs4(string: *this);
1769}
1770
1771/*!
1772 Returns a string that has whitespace removed from the start and
1773 the end.
1774
1775 Whitespace means any character for which QChar::isSpace() returns
1776 \c true. This includes the ASCII characters '\\t', '\\n', '\\v',
1777 '\\f', '\\r', and ' '.
1778
1779 Unlike QString::simplified(), trimmed() leaves internal whitespace alone.
1780
1781 \since 5.1
1782
1783 \sa QString::trimmed()
1784*/
1785QStringRef QStringRef::trimmed() const
1786{
1787 auto trimmed = QStringView(*this).trimmed();
1788 auto begin = trimmed.cbegin();
1789 auto end = trimmed.cend();
1790 if (begin == cbegin() && end == cend())
1791 return *this;
1792 int position = m_position + (begin - cbegin());
1793 return QStringRef(m_string, position, end - begin);
1794}
1795
1796/*!
1797 Returns the string converted to a \c{long long} using base \a
1798 base, which is 10 by default and must be between 2 and 36, or 0.
1799 Returns 0 if the conversion fails.
1800
1801 If \a ok is not \nullptr, failure is reported by setting *\a{ok}
1802 to \c false, and success by setting *\a{ok} to \c true.
1803
1804 If \a base is 0, the C language convention is used: If the string
1805 begins with "0x", base 16 is used; if the string begins with "0",
1806 base 8 is used; otherwise, base 10 is used.
1807
1808 The string conversion will always happen in the 'C' locale. For locale
1809 dependent conversion use QLocale::toLongLong()
1810
1811 \sa QString::toLongLong()
1812
1813 \since 5.1
1814*/
1815qint64 QStringRef::toLongLong(bool *ok, int base) const
1816{
1817 return QStringView{ *this }.toLongLong(ok, base);
1818}
1819
1820/*!
1821 Returns the string converted to an \c{unsigned long long} using base \a
1822 base, which is 10 by default and must be between 2 and 36, or 0.
1823 Returns 0 if the conversion fails.
1824
1825 If \a ok is not \nullptr, failure is reported by setting *\a{ok}
1826 to \c false, and success by setting *\a{ok} to \c true.
1827
1828 If \a base is 0, the C language convention is used: If the string
1829 begins with "0x", base 16 is used; if the string begins with "0",
1830 base 8 is used; otherwise, base 10 is used.
1831
1832 The string conversion will always happen in the 'C' locale. For locale
1833 dependent conversion use QLocale::toULongLong()
1834
1835 \sa QString::toULongLong()
1836
1837 \since 5.1
1838*/
1839quint64 QStringRef::toULongLong(bool *ok, int base) const
1840{
1841 return QStringView{ *this }.toULongLong(ok, base);
1842}
1843
1844/*!
1845 \fn long QStringRef::toLong(bool *ok, int base) const
1846
1847 Returns the string converted to a \c long using base \a
1848 base, which is 10 by default and must be between 2 and 36, or 0.
1849 Returns 0 if the conversion fails.
1850
1851 If \a ok is not \nullptr, failure is reported by setting *\a{ok}
1852 to \c false, and success by setting *\a{ok} to \c true.
1853
1854 If \a base is 0, the C language convention is used: If the string
1855 begins with "0x", base 16 is used; if the string begins with "0",
1856 base 8 is used; otherwise, base 10 is used.
1857
1858 The string conversion will always happen in the 'C' locale. For locale
1859 dependent conversion use QLocale::toLong()
1860
1861 \sa QString::toLong()
1862
1863 \since 5.1
1864*/
1865long QStringRef::toLong(bool *ok, int base) const
1866{
1867 return QStringView{ *this }.toLong(ok, base);
1868}
1869
1870/*!
1871 \fn ulong QStringRef::toULong(bool *ok, int base) const
1872
1873 Returns the string converted to an \c{unsigned long} using base \a
1874 base, which is 10 by default and must be between 2 and 36, or 0.
1875 Returns 0 if the conversion fails.
1876
1877 If \a ok is not \nullptr, failure is reported by setting *\a{ok}
1878 to \c false, and success by setting *\a{ok} to \c true.
1879
1880 If \a base is 0, the C language convention is used: If the string
1881 begins with "0x", base 16 is used; if the string begins with "0",
1882 base 8 is used; otherwise, base 10 is used.
1883
1884 The string conversion will always happen in the 'C' locale. For locale
1885 dependent conversion use QLocale::toULongLong()
1886
1887 \sa QString::toULong()
1888
1889 \since 5.1
1890*/
1891ulong QStringRef::toULong(bool *ok, int base) const
1892{
1893 return QStringView{ *this }.toULong(ok, base);
1894}
1895
1896/*!
1897 Returns the string converted to an \c int using base \a
1898 base, which is 10 by default and must be between 2 and 36, or 0.
1899 Returns 0 if the conversion fails.
1900
1901 If \a ok is not \nullptr, failure is reported by setting *\a{ok}
1902 to \c false, and success by setting *\a{ok} to \c true.
1903
1904 If \a base is 0, the C language convention is used: If the string
1905 begins with "0x", base 16 is used; if the string begins with "0",
1906 base 8 is used; otherwise, base 10 is used.
1907
1908 The string conversion will always happen in the 'C' locale. For locale
1909 dependent conversion use QLocale::toInt()
1910
1911 \sa QString::toInt()
1912
1913 \since 5.1
1914*/
1915int QStringRef::toInt(bool *ok, int base) const
1916{
1917 return QStringView{ *this }.toInt(ok, base);
1918}
1919
1920/*!
1921 Returns the string converted to an \c{unsigned int} using base \a
1922 base, which is 10 by default and must be between 2 and 36, or 0.
1923 Returns 0 if the conversion fails.
1924
1925 If \a ok is not \nullptr, failure is reported by setting *\a{ok}
1926 to \c false, and success by setting *\a{ok} to \c true.
1927
1928 If \a base is 0, the C language convention is used: If the string
1929 begins with "0x", base 16 is used; if the string begins with "0",
1930 base 8 is used; otherwise, base 10 is used.
1931
1932 The string conversion will always happen in the 'C' locale. For locale
1933 dependent conversion use QLocale::toUInt()
1934
1935 \sa QString::toUInt()
1936
1937 \since 5.1
1938*/
1939uint QStringRef::toUInt(bool *ok, int base) const
1940{
1941 return QStringView{ *this }.toUInt(ok, base);
1942}
1943
1944/*!
1945 Returns the string converted to a \c short using base \a
1946 base, which is 10 by default and must be between 2 and 36, or 0.
1947 Returns 0 if the conversion fails.
1948
1949 If \a ok is not \nullptr, failure is reported by setting *\a{ok}
1950 to \c false, and success by setting *\a{ok} to \c true.
1951
1952 If \a base is 0, the C language convention is used: If the string
1953 begins with "0x", base 16 is used; if the string begins with "0",
1954 base 8 is used; otherwise, base 10 is used.
1955
1956 The string conversion will always happen in the 'C' locale. For locale
1957 dependent conversion use QLocale::toShort()
1958
1959 \sa QString::toShort()
1960
1961 \since 5.1
1962*/
1963short QStringRef::toShort(bool *ok, int base) const
1964{
1965 return QStringView{ *this }.toShort(ok, base);
1966}
1967
1968/*!
1969 Returns the string converted to an \c{unsigned short} using base \a
1970 base, which is 10 by default and must be between 2 and 36, or 0.
1971 Returns 0 if the conversion fails.
1972
1973 If \a ok is not \nullptr, failure is reported by setting *\a{ok}
1974 to \c false, and success by setting *\a{ok} to \c true.
1975
1976 If \a base is 0, the C language convention is used: If the string
1977 begins with "0x", base 16 is used; if the string begins with "0",
1978 base 8 is used; otherwise, base 10 is used.
1979
1980 The string conversion will always happen in the 'C' locale. For locale
1981 dependent conversion use QLocale::toUShort()
1982
1983 \sa QString::toUShort()
1984
1985 \since 5.1
1986*/
1987ushort QStringRef::toUShort(bool *ok, int base) const
1988{
1989 return QStringView{ *this }.toUShort(ok, base);
1990}
1991
1992/*!
1993 Returns the string converted to a \c double value.
1994
1995 Returns an infinity if the conversion overflows or 0.0 if the
1996 conversion fails for other reasons (e.g. underflow).
1997
1998 If \a ok is not \nullptr, failure is reported by setting *\a{ok}
1999 to \c false, and success by setting *\a{ok} to \c true.
2000
2001 The string conversion will always happen in the 'C' locale. For locale
2002 dependent conversion use QLocale::toDouble()
2003
2004 For historic reasons, this function does not handle
2005 thousands group separators. If you need to convert such numbers,
2006 use QLocale::toDouble().
2007
2008 \sa QString::toDouble()
2009
2010 \since 5.1
2011*/
2012double QStringRef::toDouble(bool *ok) const
2013{
2014 return QStringView{ *this }.toDouble(ok);
2015}
2016
2017/*!
2018 Returns the string converted to a \c float value.
2019
2020 Returns an infinity if the conversion overflows or 0.0 if the
2021 conversion fails for other reasons (e.g. underflow).
2022
2023 If \a ok is not \nullptr, failure is reported by setting *\a{ok}
2024 to \c false, and success by setting *\a{ok} to \c true.
2025
2026 The string conversion will always happen in the 'C' locale. For locale
2027 dependent conversion use QLocale::toFloat()
2028
2029 \sa QString::toFloat()
2030
2031 \since 5.1
2032*/
2033float QStringRef::toFloat(bool *ok) const
2034{
2035 return QStringView{ *this }.toFloat(ok);
2036}
2037
2038/*! \fn size_t qHash(const QStringRef &key, size_t seed = 0)
2039 \relates QHash
2040 \since 5.0
2041
2042 Returns the hash value for the \a key, using \a seed to seed the calculation.
2043*/
2044
2045QT_END_NAMESPACE
2046

source code of qt5compat/src/core5/text/qstringref.cpp