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

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