Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qv4string_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#ifndef QV4STRING_H
4#define QV4STRING_H
5
6//
7// W A R N I N G
8// -------------
9//
10// This file is not part of the Qt API. It exists purely as an
11// implementation detail. This header file may change from version to
12// version without notice, or even be removed.
13//
14// We mean it.
15//
16
17#include <QtCore/qstring.h>
18#include "qv4managed_p.h"
19#include <QtCore/private/qnumeric_p.h>
20#include "qv4enginebase_p.h"
21#include <private/qv4stringtoarrayindex_p.h>
22
24
25namespace QV4 {
26
27struct ExecutionEngine;
28struct PropertyKey;
29
30namespace Heap {
31
32struct Q_QML_EXPORT StringOrSymbol : Base
33{
43
44 void init() {
45 Base::init();
46 new (&textStorage) QStringPrivate;
47 }
48
50 {
51 Base::init();
52 new (&textStorage) QStringPrivate(std::move(text));
53 }
54
55 mutable struct { alignas(QStringPrivate) unsigned char data[sizeof(QStringPrivate)]; } textStorage;
57 mutable uint subtype;
59
60 static void markObjects(Heap::Base *that, MarkStack *markStack);
61 void destroy();
62
63 QStringPrivate &text() const { return *reinterpret_cast<QStringPrivate *>(&textStorage); }
64
65 inline QString toQString() const {
66 QStringPrivate dd = text();
67 return QString(std::move(dd));
68 }
69 void createHashValue() const;
70 inline unsigned hashValue() const {
71 if (subtype >= StringType_Unknown)
72 createHashValue();
73 Q_ASSERT(subtype < StringType_Complex);
74
75 return stringHash;
76 }
77};
78
79struct Q_QML_EXPORT String : StringOrSymbol {
80 static void markObjects(Heap::Base *that, MarkStack *markStack);
81
82 const VTable *vtable() const {
83 return internalClass->vtable;
84 }
85
86 void init(const QString &text);
87 void simplifyString() const;
88 int length() const;
89 std::size_t retainedTextSize() const {
90 return subtype >= StringType_Complex ? 0 : (std::size_t(text().size) * sizeof(QChar));
91 }
92 inline QString toQString() const {
93 if (subtype >= StringType_Complex)
94 simplifyString();
95 return StringOrSymbol::toQString();
96 }
97 inline bool isEqualTo(const String *other) const {
98 if (this == other)
99 return true;
100 if (hashValue() != other->hashValue())
101 return false;
102 Q_ASSERT(subtype < StringType_Complex);
103 if (identifier.isValid() && identifier == other->identifier)
104 return true;
105 if (subtype == Heap::String::StringType_ArrayIndex && other->subtype == Heap::String::StringType_ArrayIndex)
106 return true;
107
108 return toQString() == other->toQString();
109 }
110
111 bool startsWithUpper() const;
112
113private:
114 static void append(const String *data, QChar *ch);
115};
116Q_STATIC_ASSERT(std::is_trivial_v<String>);
117
119 void init(String *l, String *n);
120 void init(String *ref, int from, int len);
121 mutable String *left;
122 mutable String *right;
123 union {
124 mutable int largestSubLength;
125 int from;
126 };
127 int len;
128};
129Q_STATIC_ASSERT(std::is_trivial_v<ComplexString>);
130
131inline
132int String::length() const {
133 // TODO: ensure that our strings never actually grow larger than INT_MAX
134 return subtype < StringType_AddedString ? int(text().size) : static_cast<const ComplexString *>(this)->len;
135}
136
137}
138
139struct Q_QML_EXPORT StringOrSymbol : public Managed {
142 enum {
143 IsStringOrSymbol = true
144 };
145
146private:
147 inline void createPropertyKey() const;
148public:
149 PropertyKey propertyKey() const { Q_ASSERT(d()->identifier.isValid()); return d()->identifier; }
150 PropertyKey toPropertyKey() const;
151
152
153 inline QString toQString() const {
154 return d()->toQString();
155 }
156};
157
158struct Q_QML_EXPORT String : public StringOrSymbol {
162 enum {
163 IsString = true
164 };
165
166 uchar subtype() const { return d()->subtype; }
167 void setSubtype(uchar subtype) const { d()->subtype = subtype; }
168
169 bool equals(String *other) const {
170 return d()->isEqualTo(other->d());
171 }
172 inline bool isEqualTo(const String *other) const {
173 return d()->isEqualTo(other->d());
174 }
175
176 inline bool lessThan(const String *other) {
177 return toQString() < other->toQString();
178 }
179
180 inline QString toQString() const {
181 return d()->toQString();
182 }
183
184 inline unsigned hashValue() const {
185 return d()->hashValue();
186 }
187 uint toUInt(bool *ok) const;
188
189 // slow path
190 Q_NEVER_INLINE void createPropertyKeyImpl() const;
191
192 static uint createHashValue(const QChar *ch, int length, uint *subtype)
193 {
194 const QChar *end = ch + length;
195 return calculateHashValue(ch, end, subtype);
196 }
197
199 {
200 const QChar *end = ch + length;
201 return calculateHashValue<String::DisallowArrayIndex>(ch, end, subtype);
202 }
203
204 static uint createHashValue(const char *ch, int length, uint *subtype)
205 {
206 const char *end = ch + length;
207 return calculateHashValue(ch, end, subtype);
208 }
209
210 bool startsWithUpper() const { return d()->startsWithUpper(); }
211
212protected:
213 static bool virtualIsEqualTo(Managed *that, Managed *o);
214 static qint64 virtualGetLength(const Managed *m);
215
216public:
217 enum IndicesBehavior {Default, DisallowArrayIndex};
218 template <IndicesBehavior Behavior = Default, typename T>
219 static inline uint calculateHashValue(const T *ch, const T* end, uint *subtype)
220 {
221 // array indices get their number as hash value
222 uint h = UINT_MAX;
223 if constexpr (Behavior != DisallowArrayIndex) {
225 if (h != UINT_MAX) {
226 if (subtype)
227 *subtype = Heap::StringOrSymbol::StringType_ArrayIndex;
228 return h;
229 }
230 }
231
232 while (ch < end) {
233 h = 31 * h + charToUInt(ch);
234 ++ch;
235 }
236
237 if (subtype)
238 *subtype = (ch != end && charToUInt(ch) == '@') ? Heap::StringOrSymbol::StringType_Symbol : Heap::StringOrSymbol::StringType_Regular;
239 return h;
240 }
241};
242
245 QV4::Heap::ComplexString *d_unchecked() const { return static_cast<QV4::Heap::ComplexString *>(m()); }
248 dptr->_checkIsInitialized();
249 return dptr;
250 }
251};
252
253inline
254void StringOrSymbol::createPropertyKey() const {
255 Q_ASSERT(!d()->identifier.isValid());
257 static_cast<const String *>(this)->createPropertyKeyImpl();
258}
259
261 if (!d()->identifier.isValid())
262 createPropertyKey();
263 return d()->identifier;
264}
265
266template<>
267inline const StringOrSymbol *Value::as() const {
268 return isManaged() && m()->internalClass->vtable->isStringOrSymbol ? static_cast<const String *>(this) : nullptr;
269}
270
271template<>
272inline const String *Value::as() const {
273 return isManaged() && m()->internalClass->vtable->isString ? static_cast<const String *>(this) : nullptr;
274}
275
276template<>
278{
279 return v.toString(e)->asReturnedValue();
280}
281
282}
283
285
286#endif
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QString text
list append(new Employee("Blackpool", "Stephen"))
Combined button and popup list for selecting options.
quint64 ReturnedValue
ReturnedValue value_convert< String >(ExecutionEngine *e, const Value &v)
uint stringToArrayIndex(const T *ch, const T *end)
uint charToUInt(const QChar *ch)
#define Q_STATIC_ASSERT(Condition)
Definition qassert.h:108
#define Q_NEVER_INLINE
GLsizei const GLfloat * v
[13]
const GLfloat * m
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint GLuint end
GLenum GLuint GLenum GLsizei length
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLint ref
GLfloat n
GLfloat GLfloat GLfloat GLfloat h
GLenum GLsizei len
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QArrayDataPointer< char16_t > QStringPrivate
static QT_BEGIN_NAMESPACE void init(QTextBoundaryFinder::BoundaryType type, QStringView str, QCharAttributes *attributes)
unsigned char uchar
Definition qtypes.h:32
unsigned int uint
Definition qtypes.h:34
long long qint64
Definition qtypes.h:60
#define V4_MANAGED(DataClass, superClass)
#define V4_NEEDS_DESTROY
#define Q_MANAGED_TYPE(type)
#define V4_INTERNALCLASS(c)
QSharedPointer< T > other(t)
[5]
QV4::Heap::ComplexString * d_unchecked() const
QV4::Heap::ComplexString Data
QV4::Heap::ComplexString * d() const
Q_ALWAYS_INLINE void _checkIsInitialized()
Definition qv4heap_p.h:123
QStringPrivate & text() const
Definition qv4string_p.h:63
unsigned hashValue() const
Definition qv4string_p.h:70
QString toQString() const
Definition qv4string_p.h:65
void init(QStringPrivate text)
Definition qv4string_p.h:49
QString toQString() const
Definition qv4string_p.h:92
int length() const
bool isEqualTo(const String *other) const
Definition qv4string_p.h:97
const VTable * vtable() const
Definition qv4string_p.h:82
std::size_t retainedTextSize() const
Definition qv4string_p.h:89
bool isManaged() const
PropertyKey propertyKey() const
QString toQString() const
PropertyKey toPropertyKey() const
uchar subtype() const
bool isEqualTo(const String *other) const
static uint createHashValueDisallowingArrayIndex(const QChar *ch, int length, uint *subtype)
static uint createHashValue(const char *ch, int length, uint *subtype)
void setSubtype(uchar subtype) const
bool equals(String *other) const
bool lessThan(const String *other)
bool startsWithUpper() const
static uint calculateHashValue(const T *ch, const T *end, uint *subtype)
QString toQString() const
unsigned hashValue() const
static uint createHashValue(const QChar *ch, int length, uint *subtype)
bool isString() const
Definition qv4value_p.h:284
const T * as() const
Definition qv4value_p.h:132