1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2019 Intel Corporation.
3// Copyright (C) 2019 Mail.ru Group.
4// Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com>
5// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
6// Qt-Security score:critical reason:data-parser
7// ### TODO: de-inline non-trivial stuff
8
9#ifndef QSTRINGREF_H
10#define QSTRINGREF_H
11
12#if defined(QT_NO_CAST_FROM_ASCII) && defined(QT_RESTRICTED_CAST_FROM_ASCII)
13#error QT_NO_CAST_FROM_ASCII and QT_RESTRICTED_CAST_FROM_ASCII must not be defined at the same time
14#endif
15
16#include <QtCore/qchar.h>
17#include <QtCore/qbytearray.h>
18#include <QtCore/qarraydata.h>
19#include <QtCore/qnamespace.h>
20#include <QtCore/qhashfunctions.h>
21#include <QtCore/qstringliteral.h>
22#include <QtCore/qstringalgorithms.h>
23#include <QtCore/qstringview.h>
24#include <QtCore/qstringtokenizer.h>
25
26#include <QtCore5Compat/qcore5global.h>
27
28#include <iterator>
29
30#ifdef truncate
31#error qstringref.h must be included before any header file that defines truncate
32#endif
33
34QT_BEGIN_NAMESPACE
35
36class Q_CORE5COMPAT_EXPORT QStringRef
37{
38 const QString *m_string;
39 int m_position;
40 int m_size;
41public:
42 typedef QString::size_type size_type;
43 typedef QString::value_type value_type;
44 typedef const QChar *const_iterator;
45 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
46 typedef QString::const_pointer const_pointer;
47 typedef QString::const_reference const_reference;
48
49 constexpr QStringRef() noexcept
50 : m_string(nullptr), m_position(0), m_size(0) { }
51 inline QStringRef(const QString *string, int position, int size);
52 inline QStringRef(const QString *string);
53
54 inline const QString *string() const { return m_string; }
55 inline int position() const { return m_position; }
56 inline int size() const { return m_size; }
57 inline int count() const { return m_size; }
58 inline int length() const { return m_size; }
59
60 int indexOf(const QString &str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
61 int indexOf(const QStringRef &str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
62 Q_REQUIRED_RESULT int indexOf(QStringView s, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
63 { return int(QtPrivate::findString(haystack: *this, from, needle: s, cs)); } // ### Qt6: qsizetype
64 int indexOf(QChar ch, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
65 int indexOf(QLatin1String str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
66 int lastIndexOf(const QStringRef &str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
67 int lastIndexOf(const QString &str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
68 int lastIndexOf(QChar ch, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
69 int lastIndexOf(QLatin1String str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
70 Q_REQUIRED_RESULT int lastIndexOf(QStringView s, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
71 { return int(QtPrivate::lastIndexOf(haystack: *this, from, needle: s, cs)); } // ### Qt6: qsizetype
72
73 inline bool contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
74 inline bool contains(const QStringRef &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
75 inline bool contains(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
76 inline bool contains(QLatin1String str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
77 inline bool contains(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
78
79 int count(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
80 int count(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
81 int count(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
82
83 Q_REQUIRED_RESULT
84 QList<QStringRef> split(const QString &sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts,
85 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
86 Q_REQUIRED_RESULT
87 QList<QStringRef> split(QChar sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts,
88 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
89
90 Q_REQUIRED_RESULT QStringRef left(int n) const;
91 Q_REQUIRED_RESULT QStringRef right(int n) const;
92 Q_REQUIRED_RESULT QStringRef mid(int pos, int n = -1) const;
93 Q_REQUIRED_RESULT QStringRef chopped(int n) const
94 { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return left(n: size() - n); }
95
96 void truncate(int pos) noexcept { m_size = qBound(min: 0, val: pos, max: m_size); }
97 void chop(int n) noexcept
98 {
99 if (n >= m_size)
100 m_size = 0;
101 else if (n > 0)
102 m_size -= n;
103 }
104
105 bool isRightToLeft() const;
106
107 Q_REQUIRED_RESULT bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
108 { return QtPrivate::startsWith(haystack: *this, needle: s, cs); }
109 bool startsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
110 bool startsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
111 bool startsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
112 bool startsWith(const QStringRef &c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
113
114 Q_REQUIRED_RESULT bool endsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
115 { return QtPrivate::endsWith(haystack: *this, needle: s, cs); }
116 bool endsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
117 bool endsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
118 bool endsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
119 bool endsWith(const QStringRef &c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
120
121 inline operator QStringView() const {
122 if (isNull())
123 return {};
124 return QStringView(m_string->data() + m_position, m_size);
125 }
126
127#ifdef QSTRINGVIEW_REFUSES_QSTRINGREF
128 operator QAnyStringView() const noexcept { return QStringView{*this}; }
129#else
130 operator QAnyStringView() const noexcept { return operator QStringView(); }
131#endif
132
133 inline QStringRef &operator=(const QString *string);
134
135 inline const QChar *unicode() const
136 {
137 static const char16_t _empty = 0;
138 if (!m_string)
139 return reinterpret_cast<const QChar *>(&_empty);
140 return m_string->unicode() + m_position;
141 }
142 inline const QChar *data() const { return unicode(); }
143 inline const QChar *constData() const { return unicode(); }
144
145 inline const_iterator begin() const { return unicode(); }
146 inline const_iterator cbegin() const { return unicode(); }
147 inline const_iterator constBegin() const { return unicode(); }
148 inline const_iterator end() const { return unicode() + size(); }
149 inline const_iterator cend() const { return unicode() + size(); }
150 inline const_iterator constEnd() const { return unicode() + size(); }
151 inline const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
152 inline const_reverse_iterator crbegin() const { return rbegin(); }
153 inline const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
154 inline const_reverse_iterator crend() const { return rend(); }
155
156 Q_REQUIRED_RESULT QByteArray toLatin1() const;
157 Q_REQUIRED_RESULT QByteArray toUtf8() const;
158 Q_REQUIRED_RESULT QByteArray toLocal8Bit() const;
159 Q_REQUIRED_RESULT QList<uint> toUcs4() const;
160
161 inline void clear() { m_string = nullptr; m_position = m_size = 0; }
162 QString toString() const;
163 inline bool isEmpty() const { return m_size == 0; }
164 inline bool isNull() const { return m_string == nullptr || m_string->isNull(); }
165
166 QStringRef appendTo(QString *string) const;
167
168 inline const QChar at(int i) const
169 { Q_ASSERT(uint(i) < uint(size())); return m_string->at(i: i + m_position); }
170 QChar operator[](int i) const { return at(i); }
171 Q_REQUIRED_RESULT QChar front() const { return at(i: 0); }
172 Q_REQUIRED_RESULT QChar back() const { return at(i: size() - 1); }
173
174#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
175 // ASCII compatibility
176 QT_ASCII_CAST_WARN inline bool operator==(const char *s) const;
177 QT_ASCII_CAST_WARN inline bool operator!=(const char *s) const;
178 QT_ASCII_CAST_WARN inline bool operator<(const char *s) const;
179 QT_ASCII_CAST_WARN inline bool operator<=(const char *s) const;
180 QT_ASCII_CAST_WARN inline bool operator>(const char *s) const;
181 QT_ASCII_CAST_WARN inline bool operator>=(const char *s) const;
182#endif
183
184 int compare(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
185 int compare(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
186 int compare(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
187 { return QtPrivate::compareStrings(lhs: *this, rhs: QStringView(&c, 1), cs); }
188 int compare(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
189#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
190 int compare(const QByteArray &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
191 { return QStringRef::compare_helper(data1: unicode(), length1: size(), data2: s.data(), length2: qstrnlen(str: s.data(), maxlen: s.size()), cs); }
192#endif
193 static int compare(const QStringRef &s1, const QString &s2,
194 Qt::CaseSensitivity = Qt::CaseSensitive) noexcept;
195 static int compare(const QStringRef &s1, const QStringRef &s2,
196 Qt::CaseSensitivity = Qt::CaseSensitive) noexcept;
197 static int compare(const QStringRef &s1, QLatin1String s2,
198 Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
199
200 int localeAwareCompare(const QString &s) const;
201 int localeAwareCompare(const QStringRef &s) const;
202 static int localeAwareCompare(const QStringRef &s1, const QString &s2);
203 static int localeAwareCompare(const QStringRef &s1, const QStringRef &s2);
204
205 Q_REQUIRED_RESULT QStringRef trimmed() const;
206 short toShort(bool *ok = nullptr, int base = 10) const;
207 ushort toUShort(bool *ok = nullptr, int base = 10) const;
208 int toInt(bool *ok = nullptr, int base = 10) const;
209 uint toUInt(bool *ok = nullptr, int base = 10) const;
210 long toLong(bool *ok = nullptr, int base = 10) const;
211 ulong toULong(bool *ok = nullptr, int base = 10) const;
212 qlonglong toLongLong(bool *ok = nullptr, int base = 10) const;
213 qulonglong toULongLong(bool *ok = nullptr, int base = 10) const;
214 float toFloat(bool *ok = nullptr) const;
215 double toDouble(bool *ok = nullptr) const;
216
217 friend inline bool operator==(QChar, const QStringRef &) noexcept;
218 friend inline bool operator<(QChar, const QStringRef &) noexcept;
219 friend inline bool operator>(QChar, const QStringRef &) noexcept;
220
221#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
222 friend inline bool operator==(const char *s1, const QStringRef &s2);
223 friend inline bool operator!=(const char *s1, const QStringRef &s2);
224 friend inline bool operator<(const char *s1, const QStringRef &s2);
225 friend inline bool operator>(const char *s1, const QStringRef &s2);
226 friend inline bool operator<=(const char *s1, const QStringRef &s2);
227 friend inline bool operator>=(const char *s1, const QStringRef &s2);
228#endif
229
230private:
231 static int compare_helper(const QChar *data1, qsizetype length1,
232 const QChar *data2, qsizetype length2,
233 Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
234 static int compare_helper(const QChar *data1, qsizetype length1,
235 const char *data2, qsizetype length2,
236 Qt::CaseSensitivity cs = Qt::CaseSensitive);
237 static int compare_helper(const QChar *data1, qsizetype length1,
238 QLatin1String s2,
239 Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
240};
241Q_DECLARE_TYPEINFO(QStringRef, Q_PRIMITIVE_TYPE);
242
243namespace QtPrivate {
244namespace Tok {
245 template <> struct ViewForImpl<QStringRef> : ViewForImpl<QStringView> {};
246 } // namespace Tok
247} // namespace QtPrivate
248
249inline Q_DECL_PURE_FUNCTION size_t qHash(const QStringRef &key, size_t seed = 0) noexcept
250{
251 return qHash(key: QStringView { key }, seed);
252}
253QT_SPECIALIZE_STD_HASH_TO_CALL_QHASH_BY_CREF(QStringRef)
254
255inline QStringRef &QStringRef::operator=(const QString *aString)
256{ m_string = aString; m_position = 0; m_size = aString ? int(aString->size()) : 0; return *this; }
257
258inline QStringRef::QStringRef(const QString *aString, int aPosition, int aSize)
259 :m_string(aString), m_position(aPosition), m_size(aSize){}
260
261inline QStringRef::QStringRef(const QString *aString)
262 :m_string(aString), m_position(0), m_size(aString ? int(aString->size()) : 0){}
263
264// QStringRef <> QStringRef
265Q_CORE5COMPAT_EXPORT bool operator==(const QStringRef &s1, const QStringRef &s2) noexcept;
266inline bool operator!=(const QStringRef &s1, const QStringRef &s2) noexcept
267{ return !(s1 == s2); }
268Q_CORE5COMPAT_EXPORT bool operator<(const QStringRef &s1, const QStringRef &s2) noexcept;
269inline bool operator>(const QStringRef &s1, const QStringRef &s2) noexcept
270{ return s2 < s1; }
271inline bool operator<=(const QStringRef &s1, const QStringRef &s2) noexcept
272{ return !(s1 > s2); }
273inline bool operator>=(const QStringRef &s1, const QStringRef &s2) noexcept
274{ return !(s1 < s2); }
275
276// QString <> QStringRef
277Q_CORE5COMPAT_EXPORT bool operator==(const QString &lhs, const QStringRef &rhs) noexcept;
278inline bool operator!=(const QString &lhs, const QStringRef &rhs) noexcept { return lhs.compare(s: rhs) != 0; }
279inline bool operator< (const QString &lhs, const QStringRef &rhs) noexcept { return lhs.compare(s: rhs) < 0; }
280inline bool operator> (const QString &lhs, const QStringRef &rhs) noexcept { return lhs.compare(s: rhs) > 0; }
281inline bool operator<=(const QString &lhs, const QStringRef &rhs) noexcept { return lhs.compare(s: rhs) <= 0; }
282inline bool operator>=(const QString &lhs, const QStringRef &rhs) noexcept { return lhs.compare(s: rhs) >= 0; }
283
284inline bool operator==(const QStringRef &lhs, const QString &rhs) noexcept { return rhs == lhs; }
285inline bool operator!=(const QStringRef &lhs, const QString &rhs) noexcept { return rhs != lhs; }
286inline bool operator< (const QStringRef &lhs, const QString &rhs) noexcept { return rhs > lhs; }
287inline bool operator> (const QStringRef &lhs, const QString &rhs) noexcept { return rhs < lhs; }
288inline bool operator<=(const QStringRef &lhs, const QString &rhs) noexcept { return rhs >= lhs; }
289inline bool operator>=(const QStringRef &lhs, const QString &rhs) noexcept { return rhs <= lhs; }
290
291inline int QStringRef::compare(const QString &s, Qt::CaseSensitivity cs) const noexcept
292{ return QStringRef::compare_helper(data1: constData(), length1: length(), data2: s.constData(), length2: s.size(), cs); }
293inline int QStringRef::compare(const QStringRef &s, Qt::CaseSensitivity cs) const noexcept
294{ return QStringRef::compare_helper(data1: constData(), length1: length(), data2: s.constData(), length2: s.length(), cs); }
295inline int QStringRef::compare(QLatin1String s, Qt::CaseSensitivity cs) const noexcept
296{ return QStringRef::compare_helper(data1: constData(), length1: length(), s2: s, cs); }
297inline int QStringRef::compare(const QStringRef &s1, const QString &s2, Qt::CaseSensitivity cs) noexcept
298{ return QStringRef::compare_helper(data1: s1.constData(), length1: s1.length(), data2: s2.constData(), length2: s2.size(), cs); }
299inline int QStringRef::compare(const QStringRef &s1, const QStringRef &s2, Qt::CaseSensitivity cs) noexcept
300{ return QStringRef::compare_helper(data1: s1.constData(), length1: s1.length(), data2: s2.constData(), length2: s2.length(), cs); }
301inline int QStringRef::compare(const QStringRef &s1, QLatin1String s2, Qt::CaseSensitivity cs) noexcept
302{ return QStringRef::compare_helper(data1: s1.constData(), length1: s1.length(), s2, cs); }
303
304// QLatin1String <> QStringRef
305Q_CORE5COMPAT_EXPORT bool operator==(QLatin1String lhs, const QStringRef &rhs) noexcept;
306inline bool operator!=(QLatin1String lhs, const QStringRef &rhs) noexcept { return rhs.compare(s: lhs) != 0; }
307inline bool operator< (QLatin1String lhs, const QStringRef &rhs) noexcept { return rhs.compare(s: lhs) > 0; }
308inline bool operator> (QLatin1String lhs, const QStringRef &rhs) noexcept { return rhs.compare(s: lhs) < 0; }
309inline bool operator<=(QLatin1String lhs, const QStringRef &rhs) noexcept { return rhs.compare(s: lhs) >= 0; }
310inline bool operator>=(QLatin1String lhs, const QStringRef &rhs) noexcept { return rhs.compare(s: lhs) <= 0; }
311
312inline bool operator==(const QStringRef &lhs, QLatin1String rhs) noexcept { return rhs == lhs; }
313inline bool operator!=(const QStringRef &lhs, QLatin1String rhs) noexcept { return rhs != lhs; }
314inline bool operator< (const QStringRef &lhs, QLatin1String rhs) noexcept { return rhs > lhs; }
315inline bool operator> (const QStringRef &lhs, QLatin1String rhs) noexcept { return rhs < lhs; }
316inline bool operator<=(const QStringRef &lhs, QLatin1String rhs) noexcept { return rhs >= lhs; }
317inline bool operator>=(const QStringRef &lhs, QLatin1String rhs) noexcept { return rhs <= lhs; }
318
319// QChar <> QStringRef
320inline bool operator==(QChar lhs, const QStringRef &rhs) noexcept
321{ return rhs.size() == 1 && lhs == rhs.front(); }
322inline bool operator< (QChar lhs, const QStringRef &rhs) noexcept
323{ return QStringRef::compare_helper(data1: &lhs, length1: 1, data2: rhs.data(), length2: rhs.size()) < 0; }
324inline bool operator> (QChar lhs, const QStringRef &rhs) noexcept
325{ return QStringRef::compare_helper(data1: &lhs, length1: 1, data2: rhs.data(), length2: rhs.size()) > 0; }
326
327inline bool operator!=(QChar lhs, const QStringRef &rhs) noexcept { return !(lhs == rhs); }
328inline bool operator<=(QChar lhs, const QStringRef &rhs) noexcept { return !(lhs > rhs); }
329inline bool operator>=(QChar lhs, const QStringRef &rhs) noexcept { return !(lhs < rhs); }
330
331inline bool operator==(const QStringRef &lhs, QChar rhs) noexcept { return rhs == lhs; }
332inline bool operator!=(const QStringRef &lhs, QChar rhs) noexcept { return !(rhs == lhs); }
333inline bool operator< (const QStringRef &lhs, QChar rhs) noexcept { return rhs > lhs; }
334inline bool operator> (const QStringRef &lhs, QChar rhs) noexcept { return rhs < lhs; }
335inline bool operator<=(const QStringRef &lhs, QChar rhs) noexcept { return !(rhs < lhs); }
336inline bool operator>=(const QStringRef &lhs, QChar rhs) noexcept { return !(rhs > lhs); }
337
338#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
339// QStringRef <> QByteArray
340QT_ASCII_CAST_WARN inline bool operator==(const QStringRef &lhs, const QByteArray &rhs) { return lhs.compare(s: rhs) == 0; }
341QT_ASCII_CAST_WARN inline bool operator!=(const QStringRef &lhs, const QByteArray &rhs) { return lhs.compare(s: rhs) != 0; }
342QT_ASCII_CAST_WARN inline bool operator< (const QStringRef &lhs, const QByteArray &rhs) { return lhs.compare(s: rhs) < 0; }
343QT_ASCII_CAST_WARN inline bool operator> (const QStringRef &lhs, const QByteArray &rhs) { return lhs.compare(s: rhs) > 0; }
344QT_ASCII_CAST_WARN inline bool operator<=(const QStringRef &lhs, const QByteArray &rhs) { return lhs.compare(s: rhs) <= 0; }
345QT_ASCII_CAST_WARN inline bool operator>=(const QStringRef &lhs, const QByteArray &rhs) { return lhs.compare(s: rhs) >= 0; }
346
347QT_ASCII_CAST_WARN inline bool operator==(const QByteArray &lhs, const QStringRef &rhs) { return rhs.compare(s: lhs) == 0; }
348QT_ASCII_CAST_WARN inline bool operator!=(const QByteArray &lhs, const QStringRef &rhs) { return rhs.compare(s: lhs) != 0; }
349QT_ASCII_CAST_WARN inline bool operator< (const QByteArray &lhs, const QStringRef &rhs) { return rhs.compare(s: lhs) > 0; }
350QT_ASCII_CAST_WARN inline bool operator> (const QByteArray &lhs, const QStringRef &rhs) { return rhs.compare(s: lhs) < 0; }
351QT_ASCII_CAST_WARN inline bool operator<=(const QByteArray &lhs, const QStringRef &rhs) { return rhs.compare(s: lhs) >= 0; }
352QT_ASCII_CAST_WARN inline bool operator>=(const QByteArray &lhs, const QStringRef &rhs) { return rhs.compare(s: lhs) <= 0; }
353
354// QStringRef <> const char *
355QT_ASCII_CAST_WARN inline bool QStringRef::operator==(const char *s) const
356{ return QStringRef::compare_helper(data1: constData(), length1: size(), data2: s, length2: -1) == 0; }
357QT_ASCII_CAST_WARN inline bool QStringRef::operator!=(const char *s) const
358{ return QStringRef::compare_helper(data1: constData(), length1: size(), data2: s, length2: -1) != 0; }
359QT_ASCII_CAST_WARN inline bool QStringRef::operator<(const char *s) const
360{ return QStringRef::compare_helper(data1: constData(), length1: size(), data2: s, length2: -1) < 0; }
361QT_ASCII_CAST_WARN inline bool QStringRef::operator<=(const char *s) const
362{ return QStringRef::compare_helper(data1: constData(), length1: size(), data2: s, length2: -1) <= 0; }
363QT_ASCII_CAST_WARN inline bool QStringRef::operator>(const char *s) const
364{ return QStringRef::compare_helper(data1: constData(), length1: size(), data2: s, length2: -1) > 0; }
365QT_ASCII_CAST_WARN inline bool QStringRef::operator>=(const char *s) const
366{ return QStringRef::compare_helper(data1: constData(), length1: size(), data2: s, length2: -1) >= 0; }
367
368QT_ASCII_CAST_WARN inline bool operator==(const char *s1, const QStringRef &s2)
369{ return QStringRef::compare_helper(data1: s2.constData(), length1: s2.size(), data2: s1, length2: -1) == 0; }
370QT_ASCII_CAST_WARN inline bool operator!=(const char *s1, const QStringRef &s2)
371{ return QStringRef::compare_helper(data1: s2.constData(), length1: s2.size(), data2: s1, length2: -1) != 0; }
372QT_ASCII_CAST_WARN inline bool operator<(const char *s1, const QStringRef &s2)
373{ return QStringRef::compare_helper(data1: s2.constData(), length1: s2.size(), data2: s1, length2: -1) > 0; }
374QT_ASCII_CAST_WARN inline bool operator<=(const char *s1, const QStringRef &s2)
375{ return QStringRef::compare_helper(data1: s2.constData(), length1: s2.size(), data2: s1, length2: -1) >= 0; }
376QT_ASCII_CAST_WARN inline bool operator>(const char *s1, const QStringRef &s2)
377{ return QStringRef::compare_helper(data1: s2.constData(), length1: s2.size(), data2: s1, length2: -1) < 0; }
378QT_ASCII_CAST_WARN inline bool operator>=(const char *s1, const QStringRef &s2)
379{ return QStringRef::compare_helper(data1: s2.constData(), length1: s2.size(), data2: s1, length2: -1) <= 0; }
380#endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
381
382inline int QStringRef::localeAwareCompare(const QString &s) const
383{ return QString::localeAwareCompare(s1: QStringView{ *this }, s2: QStringView{ s }); }
384inline int QStringRef::localeAwareCompare(const QStringRef &s) const
385{ return QString::localeAwareCompare(s1: QStringView{ *this }, s2: QStringView{ s }); }
386inline int QStringRef::localeAwareCompare(const QStringRef &s1, const QString &s2)
387{ return QString::localeAwareCompare(s1: QStringView{ s1 }, s2: QStringView{ s2 }); }
388inline int QStringRef::localeAwareCompare(const QStringRef &s1, const QStringRef &s2)
389{ return QString::localeAwareCompare(s1: QStringView{ s1 }, s2: QStringView{ s2 }); }
390
391inline bool QStringRef::contains(const QString &s, Qt::CaseSensitivity cs) const
392{ return indexOf(str: s, from: 0, cs) != -1; }
393inline bool QStringRef::contains(const QStringRef &s, Qt::CaseSensitivity cs) const
394{ return indexOf(str: s, from: 0, cs) != -1; }
395inline bool QStringRef::contains(QLatin1String s, Qt::CaseSensitivity cs) const
396{ return indexOf(str: s, from: 0, cs) != -1; }
397inline bool QStringRef::contains(QChar c, Qt::CaseSensitivity cs) const
398{ return indexOf(ch: c, from: 0, cs) != -1; }
399inline bool QStringRef::contains(QStringView s, Qt::CaseSensitivity cs) const noexcept
400{ return indexOf(s, from: 0, cs) != -1; }
401
402#if !defined(QT_USE_FAST_OPERATOR_PLUS) && !defined(QT_USE_QSTRINGBUILDER)
403inline QString operator+(const QString &s1, const QStringRef &s2)
404{ QString t; t.reserve(s1.size() + s2.size()); t += s1; t += s2; return t; }
405inline QString operator+(const QStringRef &s1, const QString &s2)
406{ QString t; t.reserve(s1.size() + s2.size()); t += s1; t += s2; return t; }
407inline QString operator+(const QStringRef &s1, QLatin1String s2)
408{ QString t; t.reserve(s1.size() + s2.size()); t += s1; t += s2; return t; }
409inline QString operator+(QLatin1String s1, const QStringRef &s2)
410{ QString t; t.reserve(s1.size() + s2.size()); t += s1; t += s2; return t; }
411inline QString operator+(const QStringRef &s1, const QStringRef &s2)
412{ QString t; t.reserve(s1.size() + s2.size()); t += s1; t += s2; return t; }
413inline QString operator+(const QStringRef &s1, QChar s2)
414{ QString t; t.reserve(s1.size() + 1); t += s1; t += s2; return t; }
415inline QString operator+(QChar s1, const QStringRef &s2)
416{ QString t; t.reserve(1 + s2.size()); t += s1; t += s2; return t; }
417#endif // !(QT_USE_FAST_OPERATOR_PLUS || QT_USE_QSTRINGBUILDER)
418
419QT_END_NAMESPACE
420
421#if defined(QT_USE_FAST_OPERATOR_PLUS) || defined(QT_USE_QSTRINGBUILDER)
422
423#include <QtCore/qstringbuilder.h>
424
425QT_BEGIN_NAMESPACE
426
427template <> struct QConcatenable<QStringRef>
428{
429 typedef QStringRef type;
430 typedef QString ConvertTo;
431 enum { ExactSize = true };
432 static int size(const QStringRef &a) { return a.size(); }
433 static inline void appendTo(const QStringRef &a, QChar *&out)
434 {
435 const int n = a.size();
436 if (n)
437 memcpy(dest: out, src: reinterpret_cast<const char*>(a.constData()), n: sizeof(QChar) * n);
438 out += n;
439 }
440};
441
442QT_END_NAMESPACE
443
444#endif
445
446#endif // QSTRINGREF_H
447

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