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 | |
6 | #include "qstringref.h" |
7 | |
8 | #include <QtCore/qnumeric.h> |
9 | #include <QtCore/qlist.h> |
10 | #include <QtCore/qvarlengtharray.h> |
11 | |
12 | #ifdef Q_OS_MACOS |
13 | #include <private/qcore_mac_p.h> |
14 | #endif |
15 | |
16 | #include <private/qfunctions_p.h> |
17 | #include <private/qstringalgorithms_p.h> |
18 | #include <private/qstringconverter_p.h> |
19 | #include <private/qstringiterator_p.h> |
20 | #include <private/qunicodetables_p.h> |
21 | |
22 | #include <limits.h> |
23 | #include <string.h> |
24 | #include <stdlib.h> |
25 | #include <stdio.h> |
26 | #include <stdarg.h> |
27 | #include <wchar.h> |
28 | |
29 | #ifdef Q_OS_WIN |
30 | # include <qt_windows.h> |
31 | #endif |
32 | |
33 | #ifdef truncate |
34 | # undef truncate |
35 | #endif |
36 | |
37 | QT_BEGIN_NAMESPACE |
38 | |
39 | // internal |
40 | static inline qsizetype qFindChar(QStringView str, QChar ch, qsizetype from, Qt::CaseSensitivity cs) noexcept; |
41 | |
42 | static inline bool qt_starts_with(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs); |
43 | static inline bool qt_starts_with(QStringView haystack, QLatin1String needle, Qt::CaseSensitivity cs); |
44 | static inline bool qt_starts_with(QStringView haystack, QChar needle, Qt::CaseSensitivity cs); |
45 | static inline bool qt_ends_with(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs); |
46 | static inline bool qt_ends_with(QStringView haystack, QLatin1String needle, Qt::CaseSensitivity cs); |
47 | static inline bool qt_ends_with(QStringView haystack, QChar needle, Qt::CaseSensitivity cs); |
48 | |
49 | // Code imported from QUnicodeTables |
50 | |
51 | namespace QUnicodeTablesPrivate |
52 | { |
53 | static const unsigned short specialCaseMap[] = { |
54 | 0x0, // placeholder |
55 | 0x1, 0x2c65, 0x1, 0x2c66, 0x1, 0x2c7e, 0x1, 0x2c7f, 0x1, 0x2c6f, 0x1, 0x2c6d, |
56 | 0x1, 0x2c70, 0x1, 0xa7ab, 0x1, 0xa7ac, 0x1, 0xa78d, 0x1, 0xa7aa, 0x1, 0xa7ae, |
57 | 0x1, 0x2c62, 0x1, 0xa7ad, 0x1, 0x2c6e, 0x1, 0x2c64, 0x1, 0xa7c5, 0x1, 0xa7b1, |
58 | 0x1, 0xa7b2, 0x1, 0xa7b0, 0x1, 0xab70, 0x1, 0xab71, 0x1, 0xab72, 0x1, 0xab73, |
59 | 0x1, 0xab74, 0x1, 0xab75, 0x1, 0xab76, 0x1, 0xab77, 0x1, 0xab78, 0x1, 0xab79, |
60 | 0x1, 0xab7a, 0x1, 0xab7b, 0x1, 0xab7c, 0x1, 0xab7d, 0x1, 0xab7e, 0x1, 0xab7f, |
61 | 0x1, 0xab80, 0x1, 0xab81, 0x1, 0xab82, 0x1, 0xab83, 0x1, 0xab84, 0x1, 0xab85, |
62 | 0x1, 0xab86, 0x1, 0xab87, 0x1, 0xab88, 0x1, 0xab89, 0x1, 0xab8a, 0x1, 0xab8b, |
63 | 0x1, 0xab8c, 0x1, 0xab8d, 0x1, 0xab8e, 0x1, 0xab8f, 0x1, 0xab90, 0x1, 0xab91, |
64 | 0x1, 0xab92, 0x1, 0xab93, 0x1, 0xab94, 0x1, 0xab95, 0x1, 0xab96, 0x1, 0xab97, |
65 | 0x1, 0xab98, 0x1, 0xab99, 0x1, 0xab9a, 0x1, 0xab9b, 0x1, 0xab9c, 0x1, 0xab9d, |
66 | 0x1, 0xab9e, 0x1, 0xab9f, 0x1, 0xaba0, 0x1, 0xaba1, 0x1, 0xaba2, 0x1, 0xaba3, |
67 | 0x1, 0xaba4, 0x1, 0xaba5, 0x1, 0xaba6, 0x1, 0xaba7, 0x1, 0xaba8, 0x1, 0xaba9, |
68 | 0x1, 0xabaa, 0x1, 0xabab, 0x1, 0xabac, 0x1, 0xabad, 0x1, 0xabae, 0x1, 0xabaf, |
69 | 0x1, 0xabb0, 0x1, 0xabb1, 0x1, 0xabb2, 0x1, 0xabb3, 0x1, 0xabb4, 0x1, 0xabb5, |
70 | 0x1, 0xabb6, 0x1, 0xabb7, 0x1, 0xabb8, 0x1, 0xabb9, 0x1, 0xabba, 0x1, 0xabbb, |
71 | 0x1, 0xabbc, 0x1, 0xabbd, 0x1, 0xabbe, 0x1, 0xabbf, 0x1, 0xa64a, 0x1, 0xa77d, |
72 | 0x1, 0xa7c6, 0x1, 0x6b, 0x1, 0xe5, 0x1, 0x26b, 0x1, 0x27d, 0x1, 0x23a, |
73 | 0x1, 0x23e, 0x1, 0x251, 0x1, 0x271, 0x1, 0x250, 0x1, 0x252, 0x1, 0x23f, |
74 | 0x1, 0x240, 0x1, 0x1d79, 0x1, 0x265, 0x1, 0x266, 0x1, 0x25c, 0x1, 0x261, |
75 | 0x1, 0x26c, 0x1, 0x26a, 0x1, 0x29e, 0x1, 0x287, 0x1, 0x29d, 0x1, 0x282, |
76 | 0x1, 0x1d8e, 0x1, 0x13a0, 0x1, 0x13a1, 0x1, 0x13a2, 0x1, 0x13a3, 0x1, 0x13a4, |
77 | 0x1, 0x13a5, 0x1, 0x13a6, 0x1, 0x13a7, 0x1, 0x13a8, 0x1, 0x13a9, 0x1, 0x13aa, |
78 | 0x1, 0x13ab, 0x1, 0x13ac, 0x1, 0x13ad, 0x1, 0x13ae, 0x1, 0x13af, 0x1, 0x13b0, |
79 | 0x1, 0x13b1, 0x1, 0x13b2, 0x1, 0x13b3, 0x1, 0x13b4, 0x1, 0x13b5, 0x1, 0x13b6, |
80 | 0x1, 0x13b7, 0x1, 0x13b8, 0x1, 0x13b9, 0x1, 0x13ba, 0x1, 0x13bb, 0x1, 0x13bc, |
81 | 0x1, 0x13bd, 0x1, 0x13be, 0x1, 0x13bf, 0x1, 0x13c0, 0x1, 0x13c1, 0x1, 0x13c2, |
82 | 0x1, 0x13c3, 0x1, 0x13c4, 0x1, 0x13c5, 0x1, 0x13c6, 0x1, 0x13c7, 0x1, 0x13c8, |
83 | 0x1, 0x13c9, 0x1, 0x13ca, 0x1, 0x13cb, 0x1, 0x13cc, 0x1, 0x13cd, 0x1, 0x13ce, |
84 | 0x1, 0x13cf, 0x1, 0x13d0, 0x1, 0x13d1, 0x1, 0x13d2, 0x1, 0x13d3, 0x1, 0x13d4, |
85 | 0x1, 0x13d5, 0x1, 0x13d6, 0x1, 0x13d7, 0x1, 0x13d8, 0x1, 0x13d9, 0x1, 0x13da, |
86 | 0x1, 0x13db, 0x1, 0x13dc, 0x1, 0x13dd, 0x1, 0x13de, 0x1, 0x13df, 0x1, 0x13e0, |
87 | 0x1, 0x13e1, 0x1, 0x13e2, 0x1, 0x13e3, 0x1, 0x13e4, 0x1, 0x13e5, 0x1, 0x13e6, |
88 | 0x1, 0x13e7, 0x1, 0x13e8, 0x1, 0x13e9, 0x1, 0x13ea, 0x1, 0x13eb, 0x1, 0x13ec, |
89 | 0x1, 0x13ed, 0x1, 0x13ee, 0x1, 0x13ef, 0x2, 0x53, 0x73, 0x2, 0x53, 0x53, |
90 | 0x2, 0x69, 0x307, 0x2, 0x46, 0x66, 0x2, 0x46, 0x46, 0x2, 0x46, 0x69, |
91 | 0x2, 0x46, 0x49, 0x2, 0x46, 0x6c, 0x2, 0x46, 0x4c, 0x3, 0x46, 0x66, |
92 | 0x69, 0x3, 0x46, 0x46, 0x49, 0x3, 0x46, 0x66, 0x6c, 0x3, 0x46, 0x46, |
93 | 0x4c, 0x2, 0x53, 0x74, 0x2, 0x53, 0x54, 0x2, 0x535, 0x582, 0x2, 0x535, |
94 | 0x552, 0x2, 0x544, 0x576, 0x2, 0x544, 0x546, 0x2, 0x544, 0x565, 0x2, 0x544, |
95 | 0x535, 0x2, 0x544, 0x56b, 0x2, 0x544, 0x53b, 0x2, 0x54e, 0x576, 0x2, 0x54e, |
96 | 0x546, 0x2, 0x544, 0x56d, 0x2, 0x544, 0x53d, 0x2, 0x2bc, 0x4e, 0x3, 0x399, |
97 | 0x308, 0x301, 0x3, 0x3a5, 0x308, 0x301, 0x2, 0x4a, 0x30c, 0x2, 0x48, 0x331, |
98 | 0x2, 0x54, 0x308, 0x2, 0x57, 0x30a, 0x2, 0x59, 0x30a, 0x2, 0x41, 0x2be, |
99 | 0x2, 0x3a5, 0x313, 0x3, 0x3a5, 0x313, 0x300, 0x3, 0x3a5, 0x313, 0x301, 0x3, |
100 | 0x3a5, 0x313, 0x342, 0x2, 0x391, 0x342, 0x2, 0x397, 0x342, 0x3, 0x399, 0x308, |
101 | 0x300, 0x2, 0x399, 0x342, 0x3, 0x399, 0x308, 0x342, 0x3, 0x3a5, 0x308, 0x300, |
102 | 0x2, 0x3a1, 0x313, 0x2, 0x3a5, 0x342, 0x3, 0x3a5, 0x308, 0x342, 0x2, 0x3a9, |
103 | 0x342, 0x2, 0x1f08, 0x399, 0x2, 0x1f09, 0x399, 0x2, 0x1f0a, 0x399, 0x2, 0x1f0b, |
104 | 0x399, 0x2, 0x1f0c, 0x399, 0x2, 0x1f0d, 0x399, 0x2, 0x1f0e, 0x399, 0x2, 0x1f0f, |
105 | 0x399, 0x2, 0x1f28, 0x399, 0x2, 0x1f29, 0x399, 0x2, 0x1f2a, 0x399, 0x2, 0x1f2b, |
106 | 0x399, 0x2, 0x1f2c, 0x399, 0x2, 0x1f2d, 0x399, 0x2, 0x1f2e, 0x399, 0x2, 0x1f2f, |
107 | 0x399, 0x2, 0x1f68, 0x399, 0x2, 0x1f69, 0x399, 0x2, 0x1f6a, 0x399, 0x2, 0x1f6b, |
108 | 0x399, 0x2, 0x1f6c, 0x399, 0x2, 0x1f6d, 0x399, 0x2, 0x1f6e, 0x399, 0x2, 0x1f6f, |
109 | 0x399, 0x2, 0x391, 0x399, 0x2, 0x397, 0x399, 0x2, 0x3a9, 0x399, 0x2, 0x1fba, |
110 | 0x345, 0x2, 0x1fba, 0x399, 0x2, 0x386, 0x345, 0x2, 0x386, 0x399, 0x2, 0x1fca, |
111 | 0x345, 0x2, 0x1fca, 0x399, 0x2, 0x389, 0x345, 0x2, 0x389, 0x399, 0x2, 0x1ffa, |
112 | 0x345, 0x2, 0x1ffa, 0x399, 0x2, 0x38f, 0x345, 0x2, 0x38f, 0x399, 0x3, 0x391, |
113 | 0x342, 0x345, 0x3, 0x391, 0x342, 0x399, 0x3, 0x397, 0x342, 0x345, 0x3, 0x397, |
114 | 0x342, 0x399, 0x3, 0x3a9, 0x342, 0x345, 0x3, 0x3a9, 0x342, 0x399, 0x1, 0xa64b |
115 | }; |
116 | } |
117 | |
118 | // Code imported from QChar |
119 | |
120 | namespace QCharPrivate |
121 | { |
122 | template<typename T> |
123 | Q_DECL_CONST_FUNCTION static inline T convertCase_helper(T uc, QUnicodeTables::Case which) noexcept |
124 | { |
125 | const auto fold = QUnicodeTables::properties(uc)->cases[which]; |
126 | |
127 | if (Q_UNLIKELY(fold.special)) { |
128 | const ushort *specialCase = QUnicodeTablesPrivate::specialCaseMap + fold.diff; |
129 | // so far, there are no special cases beyond BMP (guaranteed by the qunicodetables |
130 | // generator) |
131 | return *specialCase == 1 ? specialCase[1] : uc; |
132 | } |
133 | |
134 | return uc + fold.diff; |
135 | } |
136 | |
137 | [[maybe_unused]] static inline char32_t foldCase(char32_t ch, char32_t &last) noexcept |
138 | { |
139 | char32_t ucs4 = ch; |
140 | if (QChar::isLowSurrogate(ucs4) && QChar::isHighSurrogate(ucs4: last)) |
141 | ucs4 = QChar::surrogateToUcs4(high: last, low: ucs4); |
142 | last = ch; |
143 | return convertCase_helper(uc: ucs4, which: QUnicodeTables::CaseFold); |
144 | } |
145 | |
146 | static inline char16_t foldCase(char16_t ch) noexcept |
147 | { |
148 | return convertCase_helper(uc: ch, which: QUnicodeTables::CaseFold); |
149 | } |
150 | |
151 | static inline QChar foldCase(QChar ch) noexcept |
152 | { |
153 | return QChar(foldCase(ch: ch.unicode())); |
154 | } |
155 | } |
156 | |
157 | static int qt_compare_strings(QStringView lhs, QStringView rhs, Qt::CaseSensitivity cs) noexcept |
158 | { |
159 | return lhs.compare(other: rhs, cs); |
160 | } |
161 | |
162 | static int qt_compare_strings(QStringView lhs, QLatin1String rhs, Qt::CaseSensitivity cs) noexcept |
163 | { |
164 | return lhs.compare(s: rhs, cs); |
165 | } |
166 | |
167 | static QByteArray qt_convert_to_local_8bit(QStringView string) |
168 | { |
169 | if (string.isNull()) |
170 | return QByteArray(); |
171 | QStringEncoder fromUtf16(QStringEncoder::System, QStringEncoder::Flag::Stateless); |
172 | return fromUtf16(string); |
173 | } |
174 | |
175 | static QByteArray qt_convert_to_utf8(QStringView str) |
176 | { |
177 | if (str.isNull()) |
178 | return QByteArray(); |
179 | |
180 | return QUtf8::convertFromUnicode(in: str); |
181 | } |
182 | |
183 | static QList<uint> qt_convert_to_ucs4(QStringView string) |
184 | { |
185 | QList<uint> v(string.size()); |
186 | uint *a = const_cast<uint*>(v.constData()); |
187 | QStringIterator it(string); |
188 | while (it.hasNext()) |
189 | *a++ = it.next(); |
190 | v.resize(size: a - v.constData()); |
191 | return v; |
192 | } |
193 | |
194 | /*! |
195 | \internal |
196 | \since 4.5 |
197 | */ |
198 | int QStringRef::compare_helper(const QChar *data1, qsizetype length1, const QChar *data2, qsizetype length2, |
199 | Qt::CaseSensitivity cs) noexcept |
200 | { |
201 | Q_ASSERT(length1 >= 0); |
202 | Q_ASSERT(length2 >= 0); |
203 | Q_ASSERT(data1 || length1 == 0); |
204 | Q_ASSERT(data2 || length2 == 0); |
205 | return qt_compare_strings(lhs: QStringView(data1, length1), rhs: QStringView(data2, length2), cs); |
206 | } |
207 | |
208 | /*! |
209 | \internal |
210 | \since 5.0 |
211 | */ |
212 | int QStringRef::compare_helper(const QChar *data1, qsizetype length1, const char *data2, qsizetype length2, |
213 | Qt::CaseSensitivity cs) |
214 | { |
215 | Q_ASSERT(length1 >= 0); |
216 | Q_ASSERT(data1 || length1 == 0); |
217 | if (!data2) |
218 | return length1; |
219 | if (Q_UNLIKELY(length2 < 0)) |
220 | length2 = qsizetype(strlen(s: data2)); |
221 | // ### make me nothrow in all cases |
222 | QVarLengthArray<ushort> s2(length2); |
223 | const auto beg = reinterpret_cast<QChar *>(s2.data()); |
224 | const auto end = QUtf8::convertToUnicode(buffer: beg, in: QByteArrayView(data2, length2)); |
225 | return qt_compare_strings(lhs: QStringView(data1, length1), rhs: QStringView(beg, end - beg), cs); |
226 | } |
227 | |
228 | /*! |
229 | \internal |
230 | \since 4.5 |
231 | */ |
232 | int QStringRef::compare_helper(const QChar *data1, qsizetype length1, QLatin1String s2, |
233 | Qt::CaseSensitivity cs) noexcept |
234 | { |
235 | Q_ASSERT(length1 >= 0); |
236 | Q_ASSERT(data1 || length1 == 0); |
237 | return qt_compare_strings(lhs: QStringView(data1, length1), rhs: s2, cs); |
238 | } |
239 | |
240 | namespace { |
241 | template<class ResultList, class StringSource> |
242 | static ResultList splitString(const StringSource &source, QStringView sep, |
243 | Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) |
244 | { |
245 | ResultList list; |
246 | typename StringSource::size_type start = 0; |
247 | typename StringSource::size_type end; |
248 | typename StringSource::size_type = 0; |
249 | while ((end = QtPrivate::findString(QStringView(source.constData(), source.size()), start + extra, sep, cs)) != -1) { |
250 | if (start != end || behavior == Qt::KeepEmptyParts) |
251 | list.append(source.mid(start, end - start)); |
252 | start = end + sep.size(); |
253 | extra = (sep.size() == 0 ? 1 : 0); |
254 | } |
255 | if (start != source.size() || behavior == Qt::KeepEmptyParts) |
256 | list.append(source.mid(start)); |
257 | return list; |
258 | } |
259 | |
260 | } // namespace |
261 | |
262 | /*! |
263 | Splits the string into substrings references wherever \a sep occurs, and |
264 | returns the list of those strings. |
265 | |
266 | See QString::split() for how \a sep, \a behavior and \a cs interact to form |
267 | the result. |
268 | |
269 | \note All references are valid as long this string is alive. Destroying this |
270 | string will cause all references to be dangling pointers. |
271 | |
272 | \since 5.14 |
273 | */ |
274 | QList<QStringRef> QStringRef::split(const QString &sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const |
275 | { |
276 | return splitString<QList<QStringRef>>(source: *this, sep, behavior, cs); |
277 | } |
278 | |
279 | /*! |
280 | \overload |
281 | \since 5.14 |
282 | */ |
283 | QList<QStringRef> QStringRef::split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const |
284 | { |
285 | return splitString<QList<QStringRef>>(source: *this, sep: QStringView(&sep, 1), behavior, cs); |
286 | } |
287 | |
288 | /*! |
289 | \class QStringRef |
290 | \inmodule QtCore5Compat |
291 | \since 4.3 |
292 | \brief The QStringRef class provides a thin wrapper around QString substrings. |
293 | \reentrant |
294 | \ingroup tools |
295 | \ingroup string-processing |
296 | |
297 | QStringRef provides a read-only subset of the QString API. |
298 | |
299 | A string reference explicitly references a portion of a string() |
300 | with a given size(), starting at a specific position(). Calling |
301 | toString() returns a copy of the data as a real QString instance. |
302 | |
303 | This class is designed to improve the performance of substring |
304 | handling when manipulating substrings obtained from existing QString |
305 | instances. QStringRef avoids the memory allocation and reference |
306 | counting overhead of a standard QString by simply referencing a |
307 | part of the original string. This can prove to be advantageous in |
308 | low level code, such as that used in a parser, at the expense of |
309 | potentially more complex code. |
310 | |
311 | For most users, there are no semantic benefits to using QStringRef |
312 | instead of QString since QStringRef requires attention to be paid |
313 | to memory management issues, potentially making code more complex |
314 | to write and maintain. |
315 | |
316 | \warning A QStringRef is only valid as long as the referenced |
317 | string exists. If the original string is deleted, the string |
318 | reference points to an invalid memory location. |
319 | |
320 | We suggest that you only use this class in stable code where profiling |
321 | has clearly identified that performance improvements can be made by |
322 | replacing standard string operations with the optimized substring |
323 | handling provided by this class. |
324 | |
325 | \sa {Implicitly Shared Classes} |
326 | */ |
327 | |
328 | /*! |
329 | \typedef QStringRef::size_type |
330 | \internal |
331 | */ |
332 | |
333 | /*! |
334 | \typedef QStringRef::value_type |
335 | \internal |
336 | */ |
337 | |
338 | /*! |
339 | \typedef QStringRef::const_pointer |
340 | \internal |
341 | */ |
342 | |
343 | /*! |
344 | \typedef QStringRef::const_reference |
345 | \internal |
346 | */ |
347 | |
348 | /*! |
349 | \typedef QStringRef::const_iterator |
350 | \since 5.4 |
351 | |
352 | \sa QStringRef::const_reverse_iterator |
353 | */ |
354 | |
355 | /*! |
356 | \typedef QStringRef::const_reverse_iterator |
357 | \since 5.7 |
358 | |
359 | \sa QStringRef::const_iterator |
360 | */ |
361 | |
362 | /*! |
363 | \fn QStringRef::QStringRef() |
364 | |
365 | Constructs an empty string reference. |
366 | */ |
367 | |
368 | /*! \fn QStringRef::QStringRef(const QString *string, int position, int length) |
369 | |
370 | Constructs a string reference to the range of characters in the given |
371 | \a string specified by the starting \a position and \a length in characters. |
372 | |
373 | \warning This function exists to improve performance as much as possible, |
374 | and performs no bounds checking. For program correctness, \a position and |
375 | \a length must describe a valid substring of \a string. |
376 | |
377 | This means that the starting \a position must be positive or 0 and smaller |
378 | than \a string's length, and \a length must be positive or 0 but smaller than |
379 | the string's length minus the starting \a position; |
380 | i.e, 0 <= position < string->length() and |
381 | 0 <= length <= string->length() - position must both be satisfied. |
382 | */ |
383 | |
384 | /*! \fn QStringRef::QStringRef(const QString *string) |
385 | |
386 | Constructs a string reference to the given \a string. |
387 | */ |
388 | |
389 | /*! \fn QStringRef::QStringRef(const QStringRef &other) |
390 | |
391 | Constructs a copy of the \a other string reference. |
392 | */ |
393 | /*! |
394 | \fn QStringRef::~QStringRef() |
395 | |
396 | Destroys the string reference. |
397 | |
398 | Since this class is only used to refer to string data, and does not take |
399 | ownership of it, no memory is freed when instances are destroyed. |
400 | */ |
401 | |
402 | /*! |
403 | \fn int QStringRef::position() const |
404 | |
405 | Returns the starting position in the referenced string that is referred to |
406 | by the string reference. |
407 | |
408 | \sa size(), string() |
409 | */ |
410 | |
411 | /*! |
412 | \fn int QStringRef::size() const |
413 | |
414 | Returns the number of characters referred to by the string reference. |
415 | Equivalent to length() and count(). |
416 | |
417 | \sa position(), string() |
418 | */ |
419 | /*! |
420 | \fn int QStringRef::count() const |
421 | Returns the number of characters referred to by the string reference. |
422 | Equivalent to size() and length(). |
423 | |
424 | \sa position(), string() |
425 | */ |
426 | /*! |
427 | \fn int QStringRef::length() const |
428 | Returns the number of characters referred to by the string reference. |
429 | Equivalent to size() and count(). |
430 | |
431 | \sa position(), string() |
432 | */ |
433 | |
434 | |
435 | /*! |
436 | \fn bool QStringRef::isEmpty() const |
437 | |
438 | Returns \c true if the string reference has no characters; otherwise returns |
439 | \c false. |
440 | |
441 | A string reference is empty if its size is zero. |
442 | |
443 | \sa size() |
444 | */ |
445 | |
446 | /*! |
447 | \fn bool QStringRef::isNull() const |
448 | |
449 | Returns \c true if this string reference does not reference a string or if |
450 | the string it references is null (i.e. QString::isNull() is true). |
451 | |
452 | \sa size() |
453 | */ |
454 | |
455 | /*! |
456 | \fn const QString *QStringRef::string() const |
457 | |
458 | Returns a pointer to the string referred to by the string reference, or |
459 | 0 if it does not reference a string. |
460 | |
461 | \sa unicode() |
462 | */ |
463 | |
464 | |
465 | /*! |
466 | \fn const QChar *QStringRef::unicode() const |
467 | |
468 | Returns a Unicode representation of the string reference. Since |
469 | the data stems directly from the referenced string, it is not |
470 | \\0'-terminated unless the string reference includes the string's |
471 | null terminator. |
472 | |
473 | \sa string() |
474 | */ |
475 | |
476 | /*! |
477 | \fn const QChar *QStringRef::data() const |
478 | |
479 | Same as unicode(). |
480 | */ |
481 | |
482 | /*! |
483 | \fn const QChar *QStringRef::constData() const |
484 | |
485 | Same as unicode(). |
486 | */ |
487 | |
488 | /*! |
489 | \fn QStringRef::const_iterator QStringRef::begin() const |
490 | \since 5.4 |
491 | |
492 | Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first character in |
493 | the string. |
494 | |
495 | \sa cbegin(), constBegin(), end(), constEnd(), rbegin(), rend() |
496 | */ |
497 | |
498 | /*! |
499 | \fn QStringRef::const_iterator QStringRef::cbegin() const |
500 | \since 5.4 |
501 | |
502 | Same as begin(). |
503 | |
504 | \sa begin(), constBegin(), cend(), constEnd(), rbegin(), rend() |
505 | */ |
506 | |
507 | /*! |
508 | \fn QStringRef::const_iterator QStringRef::constBegin() const |
509 | \since 5.9 |
510 | |
511 | Same as begin(). |
512 | |
513 | \sa begin(), cend(), constEnd(), rbegin(), rend() |
514 | */ |
515 | |
516 | /*! |
517 | \fn QStringRef::const_iterator QStringRef::end() const |
518 | \since 5.4 |
519 | |
520 | Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary |
521 | character after the last character in the list. |
522 | |
523 | \sa cbegin(), constBegin(), end(), constEnd(), rbegin(), rend() |
524 | */ |
525 | |
526 | /*! \fn QStringRef::const_iterator QStringRef::cend() const |
527 | \since 5.4 |
528 | |
529 | Same as end(). |
530 | |
531 | \sa end(), constEnd(), cbegin(), constBegin(), rbegin(), rend() |
532 | */ |
533 | |
534 | /*! \fn QStringRef::const_iterator QStringRef::constEnd() const |
535 | \since 5.9 |
536 | |
537 | Same as end(). |
538 | |
539 | \sa end(), cend(), cbegin(), constBegin(), rbegin(), rend() |
540 | */ |
541 | |
542 | /*! |
543 | \fn QStringRef::const_reverse_iterator QStringRef::rbegin() const |
544 | \since 5.7 |
545 | |
546 | Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing to the first |
547 | character in the string, in reverse order. |
548 | |
549 | \sa begin(), crbegin(), rend() |
550 | */ |
551 | |
552 | /*! |
553 | \fn QStringRef::const_reverse_iterator QStringRef::crbegin() const |
554 | \since 5.7 |
555 | |
556 | Same as rbegin(). |
557 | |
558 | \sa begin(), rbegin(), rend() |
559 | */ |
560 | |
561 | /*! |
562 | \fn QStringRef::const_reverse_iterator QStringRef::rend() const |
563 | \since 5.7 |
564 | |
565 | Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to one past |
566 | the last character in the string, in reverse order. |
567 | |
568 | \sa end(), crend(), rbegin() |
569 | */ |
570 | |
571 | |
572 | /*! |
573 | \fn QStringRef::const_reverse_iterator QStringRef::crend() const |
574 | \since 5.7 |
575 | |
576 | Same as rend(). |
577 | |
578 | \sa end(), rend(), rbegin() |
579 | */ |
580 | |
581 | /*! |
582 | Returns a copy of the string reference as a QString object. |
583 | |
584 | If the string reference is not a complete reference of the string |
585 | (meaning that position() is 0 and size() equals string()->size()), |
586 | this function will allocate a new string to return. |
587 | |
588 | \sa string() |
589 | */ |
590 | |
591 | QString QStringRef::toString() const { |
592 | if (isNull()) |
593 | return QString(); |
594 | if (m_size && m_position == 0 && m_size == m_string->size()) |
595 | return *m_string; |
596 | return QString(m_string->unicode() + m_position, m_size); |
597 | } |
598 | |
599 | |
600 | /*! \relates QStringRef |
601 | |
602 | Returns \c true if string reference \a s1 is lexically equal to string reference \a s2; otherwise |
603 | returns \c false. |
604 | */ |
605 | bool operator==(const QStringRef &s1,const QStringRef &s2) noexcept |
606 | { |
607 | return s1.size() == s2.size() && qt_compare_strings(lhs: s1, rhs: s2, cs: Qt::CaseSensitive) == 0; |
608 | } |
609 | |
610 | /*! \relates QStringRef |
611 | |
612 | Returns \c true if string \a s1 is lexically equal to string reference \a s2; otherwise |
613 | returns \c false. |
614 | */ |
615 | bool operator==(const QString &s1,const QStringRef &s2) noexcept |
616 | { |
617 | return s1.size() == s2.size() && qt_compare_strings(lhs: s1, rhs: s2, cs: Qt::CaseSensitive) == 0; |
618 | } |
619 | |
620 | /*! \relates QStringRef |
621 | |
622 | Returns \c true if string \a s1 is lexically equal to string reference \a s2; otherwise |
623 | returns \c false. |
624 | */ |
625 | bool operator==(QLatin1String s1, const QStringRef &s2) noexcept |
626 | { |
627 | if (s1.size() != s2.size()) |
628 | return false; |
629 | |
630 | return qt_compare_strings(lhs: s2, rhs: s1, cs: Qt::CaseSensitive) == 0; |
631 | } |
632 | |
633 | /*! |
634 | \relates QStringRef |
635 | |
636 | Returns \c true if string reference \a s1 is lexically less than |
637 | string reference \a s2; otherwise returns \c false. |
638 | |
639 | \sa {Comparing Strings} |
640 | */ |
641 | bool operator<(const QStringRef &s1,const QStringRef &s2) noexcept |
642 | { |
643 | return qt_compare_strings(lhs: s1, rhs: s2, cs: Qt::CaseSensitive) < 0; |
644 | } |
645 | |
646 | /*!\fn bool operator<=(const QStringRef &s1,const QStringRef &s2) |
647 | |
648 | \relates QStringRef |
649 | |
650 | Returns \c true if string reference \a s1 is lexically less than |
651 | or equal to string reference \a s2; otherwise returns \c false. |
652 | |
653 | \sa {Comparing Strings} |
654 | */ |
655 | |
656 | /*!\fn bool operator>=(const QStringRef &s1,const QStringRef &s2) |
657 | |
658 | \relates QStringRef |
659 | |
660 | Returns \c true if string reference \a s1 is lexically greater than |
661 | or equal to string reference \a s2; otherwise returns \c false. |
662 | |
663 | \sa {Comparing Strings} |
664 | */ |
665 | |
666 | /*!\fn bool operator>(const QStringRef &s1,const QStringRef &s2) |
667 | |
668 | \relates QStringRef |
669 | |
670 | Returns \c true if string reference \a s1 is lexically greater than |
671 | string reference \a s2; otherwise returns \c false. |
672 | |
673 | \sa {Comparing Strings} |
674 | */ |
675 | |
676 | |
677 | /*! |
678 | \fn const QChar QStringRef::at(int position) const |
679 | |
680 | Returns the character at the given index \a position in the |
681 | string reference. |
682 | |
683 | The \a position must be a valid index position in the string |
684 | (i.e., 0 <= \a position < size()). |
685 | */ |
686 | |
687 | /*! |
688 | \fn QChar QStringRef::operator[](int position) const |
689 | \since 5.7 |
690 | |
691 | Returns the character at the given index \a position in the |
692 | string reference. |
693 | |
694 | The \a position must be a valid index position in the string |
695 | reference (i.e., 0 <= \a position < size()). |
696 | |
697 | \sa at() |
698 | */ |
699 | |
700 | /*! |
701 | \fn QChar QStringRef::front() const |
702 | \since 5.10 |
703 | |
704 | Returns the first character in the string. |
705 | Same as \c{at(0)}. |
706 | |
707 | This function is provided for STL compatibility. |
708 | |
709 | \warning Calling this function on an empty string constitutes |
710 | undefined behavior. |
711 | |
712 | \sa back(), at(), operator[]() |
713 | */ |
714 | |
715 | /*! |
716 | \fn QChar QStringRef::back() const |
717 | \since 5.10 |
718 | |
719 | Returns the last character in the string. |
720 | Same as \c{at(size() - 1)}. |
721 | |
722 | This function is provided for STL compatibility. |
723 | |
724 | \warning Calling this function on an empty string constitutes |
725 | undefined behavior. |
726 | |
727 | \sa front(), at(), operator[]() |
728 | */ |
729 | |
730 | /*! |
731 | \fn void QStringRef::clear() |
732 | |
733 | Clears the contents of the string reference by making it null and empty. |
734 | |
735 | \sa isEmpty(), isNull() |
736 | */ |
737 | |
738 | /*! |
739 | \fn QStringRef &QStringRef::operator=(const QStringRef &other) |
740 | |
741 | Assigns the \a other string reference to this string reference, and |
742 | returns the result. |
743 | */ |
744 | |
745 | /*! |
746 | \fn QStringRef &QStringRef::operator=(const QString *string) |
747 | |
748 | Constructs a string reference to the given \a string and assigns it to |
749 | this string reference, returning the result. |
750 | */ |
751 | |
752 | /*! |
753 | \fn bool QStringRef::operator==(const char * s) const |
754 | |
755 | \overload operator==() |
756 | |
757 | The \a s byte array is converted to a QStringRef using the |
758 | fromUtf8() function. This function stops conversion at the |
759 | first NUL character found, or the end of the byte array. |
760 | |
761 | You can disable this operator by defining \c |
762 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
763 | can be useful if you want to ensure that all user-visible strings |
764 | go through QObject::tr(), for example. |
765 | |
766 | Returns \c true if this string is lexically equal to the parameter |
767 | string \a s. Otherwise returns \c false. |
768 | |
769 | \sa QT_NO_CAST_FROM_ASCII |
770 | */ |
771 | |
772 | /*! |
773 | \fn bool QStringRef::operator!=(const char * s) const |
774 | |
775 | \overload operator!=() |
776 | |
777 | The \a s const char pointer is converted to a QStringRef using |
778 | the fromUtf8() function. |
779 | |
780 | You can disable this operator by defining \c |
781 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
782 | can be useful if you want to ensure that all user-visible strings |
783 | go through QObject::tr(), for example. |
784 | |
785 | Returns \c true if this string is not lexically equal to the parameter |
786 | string \a s. Otherwise returns \c false. |
787 | |
788 | \sa QT_NO_CAST_FROM_ASCII |
789 | */ |
790 | |
791 | /*! |
792 | \fn bool QStringRef::operator<(const char * s) const |
793 | |
794 | \overload operator<() |
795 | |
796 | The \a s const char pointer is converted to a QStringRef using |
797 | the fromUtf8() function. |
798 | |
799 | You can disable this operator by defining \c |
800 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
801 | can be useful if you want to ensure that all user-visible strings |
802 | go through QObject::tr(), for example. |
803 | |
804 | Returns \c true if this string is lexically smaller than the parameter |
805 | string \a s. Otherwise returns \c false. |
806 | |
807 | \sa QT_NO_CAST_FROM_ASCII |
808 | */ |
809 | |
810 | /*! |
811 | \fn bool QStringRef::operator<=(const char * s) const |
812 | |
813 | \overload operator<=() |
814 | |
815 | The \a s const char pointer is converted to a QStringRef using |
816 | the fromUtf8() function. |
817 | |
818 | You can disable this operator by defining \c |
819 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
820 | can be useful if you want to ensure that all user-visible strings |
821 | go through QObject::tr(), for example. |
822 | |
823 | Returns \c true if this string is lexically smaller than or equal to the parameter |
824 | string \a s. Otherwise returns \c false. |
825 | |
826 | \sa QT_NO_CAST_FROM_ASCII |
827 | */ |
828 | |
829 | /*! |
830 | \fn bool QStringRef::operator>(const char * s) const |
831 | |
832 | |
833 | \overload operator>() |
834 | |
835 | The \a s const char pointer is converted to a QStringRef using |
836 | the fromUtf8() function. |
837 | |
838 | You can disable this operator by defining \c |
839 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
840 | can be useful if you want to ensure that all user-visible strings |
841 | go through QObject::tr(), for example. |
842 | |
843 | Returns \c true if this string is lexically greater than the parameter |
844 | string \a s. Otherwise returns \c false. |
845 | |
846 | \sa QT_NO_CAST_FROM_ASCII |
847 | */ |
848 | |
849 | /*! |
850 | \fn bool QStringRef::operator>= (const char * s) const |
851 | |
852 | \overload operator>=() |
853 | |
854 | The \a s const char pointer is converted to a QStringRef using |
855 | the fromUtf8() function. |
856 | |
857 | You can disable this operator by defining \c |
858 | QT_NO_CAST_FROM_ASCII when you compile your applications. This |
859 | can be useful if you want to ensure that all user-visible strings |
860 | go through QObject::tr(), for example. |
861 | |
862 | Returns \c true if this string is lexically greater than or equal to the |
863 | parameter string \a s. Otherwise returns \c false. |
864 | |
865 | \sa QT_NO_CAST_FROM_ASCII |
866 | */ |
867 | |
868 | /*! Appends the string reference to \a string, and returns a new |
869 | reference to the combined string data. |
870 | */ |
871 | QStringRef QStringRef::appendTo(QString *string) const |
872 | { |
873 | if (!string) |
874 | return QStringRef(); |
875 | int pos = string->size(); |
876 | string->insert(i: pos, uc: unicode(), len: size()); |
877 | return QStringRef(string, pos, size()); |
878 | } |
879 | |
880 | /*! |
881 | \fn int QStringRef::compare(const QStringRef &s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) |
882 | \since 4.5 |
883 | |
884 | Compares the string \a s1 with the string \a s2 and returns an |
885 | integer less than, equal to, or greater than zero if \a s1 |
886 | is less than, equal to, or greater than \a s2. |
887 | |
888 | If \a cs is Qt::CaseSensitive, the comparison is case sensitive; |
889 | otherwise the comparison is case insensitive. |
890 | */ |
891 | |
892 | /*! |
893 | \fn int QStringRef::compare(const QStringRef &s1, const QStringRef &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) |
894 | \since 4.5 |
895 | \overload |
896 | |
897 | Compares the string \a s1 with the string \a s2 and returns an |
898 | integer less than, equal to, or greater than zero if \a s1 |
899 | is less than, equal to, or greater than \a s2. |
900 | |
901 | If \a cs is Qt::CaseSensitive, the comparison is case sensitive; |
902 | otherwise the comparison is case insensitive. |
903 | */ |
904 | |
905 | /*! |
906 | \fn int QStringRef::compare(const QStringRef &s1, QLatin1String s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) |
907 | \since 4.5 |
908 | \overload |
909 | |
910 | Compares the string \a s1 with the string \a s2 and returns an |
911 | integer less than, equal to, or greater than zero if \a s1 |
912 | is less than, equal to, or greater than \a s2. |
913 | |
914 | If \a cs is Qt::CaseSensitive, the comparison is case sensitive; |
915 | otherwise the comparison is case insensitive. |
916 | */ |
917 | |
918 | /*! |
919 | \overload |
920 | \fn int QStringRef::compare(const QString &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
921 | \since 4.5 |
922 | |
923 | Compares this string with the \a other string and returns an |
924 | integer less than, equal to, or greater than zero if this string |
925 | is less than, equal to, or greater than the \a other string. |
926 | |
927 | If \a cs is Qt::CaseSensitive, the comparison is case sensitive; |
928 | otherwise the comparison is case insensitive. |
929 | |
930 | Equivalent to \c {compare(*this, other, cs)}. |
931 | */ |
932 | |
933 | /*! |
934 | \overload |
935 | \fn int QStringRef::compare(const QStringRef &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
936 | \since 4.5 |
937 | |
938 | Compares this string with the \a other string and returns an |
939 | integer less than, equal to, or greater than zero if this string |
940 | is less than, equal to, or greater than the \a other string. |
941 | |
942 | If \a cs is Qt::CaseSensitive, the comparison is case sensitive; |
943 | otherwise the comparison is case insensitive. |
944 | |
945 | Equivalent to \c {compare(*this, other, cs)}. |
946 | */ |
947 | |
948 | /*! |
949 | \overload |
950 | \fn int QStringRef::compare(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
951 | \since 5.14 |
952 | |
953 | Compares this string with \a ch and returns an |
954 | integer less than, equal to, or greater than zero if this string |
955 | is less than, equal to, or greater than \a ch, interpreted as a string of length one. |
956 | |
957 | If \a cs is Qt::CaseSensitive, the comparison is case sensitive; |
958 | otherwise the comparison is case insensitive. |
959 | */ |
960 | |
961 | /*! |
962 | \overload |
963 | \fn int QStringRef::compare(QLatin1String other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
964 | \since 4.5 |
965 | |
966 | Compares this string with the \a other string and returns an |
967 | integer less than, equal to, or greater than zero if this string |
968 | is less than, equal to, or greater than the \a other string. |
969 | |
970 | If \a cs is Qt::CaseSensitive, the comparison is case sensitive; |
971 | otherwise the comparison is case insensitive. |
972 | |
973 | Equivalent to \c {compare(*this, other, cs)}. |
974 | */ |
975 | |
976 | /*! |
977 | \overload |
978 | \fn int QStringRef::compare(const QByteArray &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
979 | \since 5.8 |
980 | |
981 | Compares this string with \a other and returns an |
982 | integer less than, equal to, or greater than zero if this string |
983 | is less than, equal to, or greater than the \a other byte array, |
984 | interpreted as a UTF-8 sequence. |
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 an integer less than, equal |
997 | to, or greater than zero if \a s1 is less than, equal to, or |
998 | greater than \a s2. |
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 an integer less than, equal |
1013 | to, or greater than zero if \a s1 is less than, equal to, or |
1014 | greater than \a s2. |
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 this string with the \a other string and returns an |
1029 | integer less than, equal to, or greater than zero if this string |
1030 | is less than, equal to, or greater than the \a other string. |
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 this string with the \a other string and returns an |
1045 | integer less than, equal to, or greater than zero if this string |
1046 | is less than, equal to, or greater than the \a other string. |
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 | */ |
1067 | QStringRef 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 | */ |
1086 | QStringRef 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 | */ |
1110 | QStringRef 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 | */ |
1181 | int 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 | */ |
1215 | int 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 | */ |
1236 | int 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 | */ |
1256 | int 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 | */ |
1276 | int 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 | */ |
1291 | int 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 | */ |
1312 | int 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 | */ |
1333 | int 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 | */ |
1366 | int 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 | */ |
1384 | int 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 | */ |
1402 | int 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 | */ |
1415 | bool 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 | */ |
1431 | bool 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 | */ |
1441 | bool 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 | */ |
1458 | bool 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 | */ |
1475 | bool 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 | */ |
1490 | bool 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 | */ |
1507 | bool 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 | */ |
1517 | bool 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 | */ |
1534 | bool 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 | |
1603 | template <typename Haystack, typename Needle> |
1604 | bool 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 | |
1618 | static inline bool qt_starts_with(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs) |
1619 | { |
1620 | return qt_starts_with_impl(haystack, needle, cs); |
1621 | } |
1622 | |
1623 | static inline bool qt_starts_with(QStringView haystack, QLatin1String needle, Qt::CaseSensitivity cs) |
1624 | { |
1625 | return qt_starts_with_impl(haystack, needle, cs); |
1626 | } |
1627 | |
1628 | static 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 | |
1635 | template <typename Haystack, typename Needle> |
1636 | bool 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 | |
1650 | static inline bool qt_ends_with(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs) |
1651 | { |
1652 | return qt_ends_with_impl(haystack, needle, cs); |
1653 | } |
1654 | |
1655 | static inline bool qt_ends_with(QStringView haystack, QLatin1String needle, Qt::CaseSensitivity cs) |
1656 | { |
1657 | return qt_ends_with_impl(haystack, needle, cs); |
1658 | } |
1659 | |
1660 | static 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 | |
1676 | static 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 | */ |
1711 | QByteArray 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 | */ |
1732 | QByteArray 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 | */ |
1747 | QByteArray 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 | */ |
1766 | QList<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 | */ |
1785 | QStringRef QStringRef::trimmed() const |
1786 | { |
1787 | const QChar *begin = cbegin(); |
1788 | const QChar *end = cend(); |
1789 | QStringAlgorithms<const QStringRef>::trimmed_helper_positions(begin, end); |
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 | */ |
1815 | qint64 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 | */ |
1839 | quint64 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 | */ |
1865 | long 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 | */ |
1891 | ulong 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 | */ |
1915 | int 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 | */ |
1939 | uint 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 | */ |
1963 | short 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 | */ |
1987 | ushort 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 | */ |
2012 | double 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 | */ |
2033 | float 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 | |
2045 | QT_END_NAMESPACE |
2046 | |