Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qhashedstring_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant
4
5#ifndef QHASHEDSTRING_P_H
6#define QHASHEDSTRING_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtCore/qglobal.h>
20#include <QtCore/qstring.h>
21#include <private/qv4string_p.h>
22
23#if defined(Q_OS_QNX)
24#include <stdlib.h>
25#endif
26
27QT_BEGIN_NAMESPACE
28
29class QHashedStringRef;
30class Q_QML_EXPORT QHashedString : public QString
31{
32public:
33 inline QHashedString();
34 inline QHashedString(const QString &string);
35 inline QHashedString(const QString &string, quint32);
36 inline QHashedString(const QHashedString &string);
37
38 inline QHashedString &operator=(const QHashedString &string);
39 inline bool operator==(const QHashedString &string) const;
40 inline bool operator==(const QHashedStringRef &string) const;
41
42 inline quint32 hash() const;
43 inline quint32 existingHash() const;
44
45 static inline bool compare(const QChar *lhs, const char *rhs, int length);
46 static inline bool compare(const char *lhs, const char *rhs, int length);
47
48 static inline quint32 stringHash(const QChar* data, int length);
49 static inline quint32 stringHash(const char *data, int length);
50
51private:
52 friend class QHashedStringRef;
53 friend class QStringHashNode;
54
55 inline void computeHash() const;
56 mutable quint32 m_hash = 0;
57};
58
60class Q_QML_EXPORT QHashedStringRef
61{
62public:
63 inline QHashedStringRef();
64 inline QHashedStringRef(const QString &);
65 inline QHashedStringRef(QStringView);
66 inline QHashedStringRef(const QChar *, int);
67 inline QHashedStringRef(const QChar *, int, quint32);
68 inline QHashedStringRef(const QHashedString &);
69 inline QHashedStringRef(const QHashedStringRef &);
70 inline QHashedStringRef &operator=(const QHashedStringRef &);
71
72 inline bool operator==(const QString &string) const;
73 inline bool operator==(const QHashedString &string) const;
74 inline bool operator==(const QHashedStringRef &string) const;
75 inline bool operator==(const QHashedCStringRef &string) const;
76 inline bool operator!=(const QString &string) const;
77 inline bool operator!=(const QHashedString &string) const;
78 inline bool operator!=(const QHashedStringRef &string) const;
79 inline bool operator!=(const QHashedCStringRef &string) const;
80
81 inline quint32 hash() const;
82
83 inline QChar *data();
84 inline const QChar &at(int) const;
85 inline const QChar *constData() const;
86 bool startsWith(const QString &) const;
87 bool endsWith(const QString &) const;
88 int indexOf(const QChar &, int from=0) const;
89 QHashedStringRef mid(int, int) const;
90 QList<QHashedStringRef> split(const QChar sep) const;
91
92 inline bool isEmpty() const;
93 inline int length() const;
94 inline bool startsWithUpper() const;
95
96 QString toString() const;
97
98 inline bool isLatin1() const;
99
100private:
101 friend class QHashedString;
102
103 inline void computeHash() const;
104
105 const QChar *m_data = nullptr;
106 int m_length = 0;
107 mutable quint32 m_hash = 0;
108};
109
111{
112public:
113 inline QHashedCStringRef();
114 inline QHashedCStringRef(const char *, int);
115 inline QHashedCStringRef(const char *, int, quint32);
116 inline QHashedCStringRef(const QHashedCStringRef &);
117
118 inline quint32 hash() const;
119
120 inline const char *constData() const;
121 inline int length() const;
122
123 Q_AUTOTEST_EXPORT QString toUtf16() const;
124 inline int utf16length() const;
125 inline void writeUtf16(QChar *) const;
126 inline void writeUtf16(quint16 *) const;
127private:
128 friend class QHashedStringRef;
129
130 inline void computeHash() const;
131
132 const char *m_data = nullptr;
133 int m_length = 0;
134 mutable quint32 m_hash = 0;
135};
136
137inline size_t qHash(const QHashedString &string)
138{
139 return uint(string.hash());
140}
141
142inline size_t qHash(const QHashedStringRef &string)
143{
144 return uint(string.hash());
145}
146
147QHashedString::QHashedString()
148: QString()
149{
150}
151
152QHashedString::QHashedString(const QString &string)
153: QString(string), m_hash(0)
154{
155}
156
157QHashedString::QHashedString(const QString &string, quint32 hash)
158: QString(string), m_hash(hash)
159{
160}
161
162QHashedString::QHashedString(const QHashedString &string)
163: QString(string), m_hash(string.m_hash)
164{
165}
166
167QHashedString &QHashedString::operator=(const QHashedString &string)
168{
169 static_cast<QString &>(*this) = string;
170 m_hash = string.m_hash;
171 return *this;
172}
173
174bool QHashedString::operator==(const QHashedString &string) const
175{
176 return (string.m_hash == m_hash || !string.m_hash || !m_hash) &&
177 static_cast<const QString &>(*this) == static_cast<const QString &>(string);
178}
179
180bool QHashedString::operator==(const QHashedStringRef &string) const
181{
182 if (m_hash && string.m_hash && m_hash != string.m_hash)
183 return false;
184 QStringView otherView {string.m_data, string.m_length};
185 return static_cast<const QString &>(*this) == otherView;
186}
187
188quint32 QHashedString::hash() const
189{
190 if (!m_hash) computeHash();
191 return m_hash;
192}
193
194quint32 QHashedString::existingHash() const
195{
196 return m_hash;
197}
198
199QHashedStringRef::QHashedStringRef()
200{
201}
202
203// QHashedStringRef is meant for identifiers, property names, etc.
204// Those should alsways be smaller than std::numeric_limits<int>::max())
205QHashedStringRef::QHashedStringRef(const QString &str)
206: m_data(str.constData()), m_length(int(str.size())), m_hash(0)
207{
208 Q_ASSERT(str.size() <= std::numeric_limits<int>::max());
209}
210
211QHashedStringRef::QHashedStringRef(QStringView str)
212: m_data(str.constData()), m_length(int(str.size())), m_hash(0)
213{
214 Q_ASSERT(str.size() <= std::numeric_limits<int>::max());
215}
216
217QHashedStringRef::QHashedStringRef(const QChar *data, int length)
218: m_data(data), m_length(length), m_hash(0)
219{
220}
221
222QHashedStringRef::QHashedStringRef(const QChar *data, int length, quint32 hash)
223: m_data(data), m_length(length), m_hash(hash)
224{
225}
226
227QHashedStringRef::QHashedStringRef(const QHashedString &string)
228: m_data(string.constData()), m_length(int(string.size())), m_hash(string.m_hash)
229{
230 Q_ASSERT(string.size() <= std::numeric_limits<int>::max());
231}
232
233QHashedStringRef::QHashedStringRef(const QHashedStringRef &string)
234: m_data(string.m_data), m_length(string.m_length), m_hash(string.m_hash)
235{
236}
237
238QHashedStringRef &QHashedStringRef::operator=(const QHashedStringRef &o)
239{
240 m_data = o.m_data;
241 m_length = o.m_length;
242 m_hash = o.m_hash;
243 return *this;
244}
245
246bool QHashedStringRef::operator==(const QString &string) const
247{
248 QStringView view {m_data, m_length};
249 return view == string;
250}
251
252bool QHashedStringRef::operator==(const QHashedString &string) const
253{
254 if (m_hash && string.m_hash && m_hash != string.m_hash)
255 return false;
256 QStringView view {m_data, m_length};
257 QStringView otherView {string.constData(), string.size()};
258 return view == otherView;
259}
260
261bool QHashedStringRef::operator==(const QHashedStringRef &string) const
262{
263 if (m_hash && string.m_hash && m_hash != string.m_hash)
264 return false;
265 QStringView view {m_data, m_length};
266 QStringView otherView {string.m_data, string.m_length};
267 return view == otherView;
268}
269
270bool QHashedStringRef::operator==(const QHashedCStringRef &string) const
271{
272 return m_length == string.m_length &&
273 (m_hash == string.m_hash || !m_hash || !string.m_hash) &&
274 QHashedString::compare(m_data, string.m_data, m_length);
275}
276
277bool QHashedStringRef::operator!=(const QString &string) const
278{
279 return !(*this == string);
280}
281
282bool QHashedStringRef::operator!=(const QHashedString &string) const
283{
284 return !(*this == string);
285}
286
287bool QHashedStringRef::operator!=(const QHashedStringRef &string) const
288{
289 return !(*this == string);
290}
291
292bool QHashedStringRef::operator!=(const QHashedCStringRef &string) const
293{
294 return !(*this == string);
295}
296
297QChar *QHashedStringRef::data()
298{
299 return const_cast<QChar *>(m_data);
300}
301
302const QChar &QHashedStringRef::at(int index) const
303{
304 Q_ASSERT(index < m_length);
305 return m_data[index];
306}
307
308const QChar *QHashedStringRef::constData() const
309{
310 return m_data;
311}
312
313bool QHashedStringRef::isEmpty() const
314{
315 return m_length == 0;
316}
317
318int QHashedStringRef::length() const
319{
320 return m_length;
321}
322
323bool QHashedStringRef::isLatin1() const
324{
325 for (int ii = 0; ii < m_length; ++ii)
326 if (m_data[ii].unicode() > 127) return false;
327 return true;
328}
329
330void QHashedStringRef::computeHash() const
331{
332 m_hash = QHashedString::stringHash(m_data, m_length);
333}
334
335bool QHashedStringRef::startsWithUpper() const
336{
337 if (m_length < 1) return false;
338 return m_data[0].isUpper();
339}
340
341quint32 QHashedStringRef::hash() const
342{
343 if (!m_hash) computeHash();
344 return m_hash;
345}
346
350
351QHashedCStringRef::QHashedCStringRef(const char *data, int length)
352: m_data(data), m_length(length), m_hash(0)
353{
354}
355
356QHashedCStringRef::QHashedCStringRef(const char *data, int length, quint32 hash)
357: m_data(data), m_length(length), m_hash(hash)
358{
359}
360
362: m_data(o.m_data), m_length(o.m_length), m_hash(o.m_hash)
363{
364}
365
367{
368 if (!m_hash) computeHash();
369 return m_hash;
370}
371
372const char *QHashedCStringRef::constData() const
373{
374 return m_data;
375}
376
378{
379 return m_length;
380}
381
383{
384 return m_length;
385}
386
387void QHashedCStringRef::writeUtf16(QChar *output) const
388{
389 writeUtf16((quint16 *)output);
390}
391
392void QHashedCStringRef::writeUtf16(quint16 *output) const
393{
394 int l = m_length;
395 const char *d = m_data;
396 while (l--)
397 *output++ = *d++;
398}
399
400void QHashedCStringRef::computeHash() const
401{
402 m_hash = QHashedString::stringHash(m_data, m_length);
403}
404
405bool QHashedString::compare(const QChar *lhs, const char *rhs, int length)
406{
407 Q_ASSERT(lhs && rhs);
408 const quint16 *l = (const quint16*)lhs;
409 while (length--)
410 if (*l++ != *rhs++) return false;
411 return true;
412}
413
414bool QHashedString::compare(const char *lhs, const char *rhs, int length)
415{
416 Q_ASSERT(lhs && rhs);
417 return 0 == ::memcmp(lhs, rhs, length);
418}
419
420
421quint32 QHashedString::stringHash(const QChar *data, int length)
422{
423 return QV4::String::createHashValue(data, length, nullptr);
424}
425
426quint32 QHashedString::stringHash(const char *data, int length)
427{
428 return QV4::String::createHashValue(data, length, nullptr);
429}
430
431void QHashedString::computeHash() const
432{
433 m_hash = stringHash(constData(), int(size()));
434}
435
436namespace QtPrivate {
437inline QString asString(const QHashedCStringRef &ref) { return ref.toUtf16(); }
438inline QString asString(const QHashedStringRef &ref) { return ref.toString(); }
439}
440
441QT_END_NAMESPACE
442
443#endif // QHASHEDSTRING_P_H
quint32 hash() const
QHashedCStringRef(const QHashedCStringRef &)
const char * constData() const
void writeUtf16(QChar *) const
int utf16length() const
QHashedCStringRef(const char *, int, quint32)
QHashedCStringRef(const char *, int)
Combined button and popup list for selecting options.
QString asString(const QHashedStringRef &ref)
QString asString(const QHashedCStringRef &ref)
size_t qHash(const QHashedString &string)
size_t qHash(const QHashedStringRef &string)