Qt 6.x
The Qt SDK
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_PRIVATE_EXPORT StringOrSymbol : Base
33{
41 StringType_Complex = StringType_AddedString
42 };
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_PRIVATE_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_PRIVATE_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_PRIVATE_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
198 static uint createHashValue(const char *ch, int length, uint *subtype)
199 {
200 const char *end = ch + length;
201 return calculateHashValue(ch, end, subtype);
202 }
203
204 bool startsWithUpper() const { return d()->startsWithUpper(); }
205
206protected:
207 static bool virtualIsEqualTo(Managed *that, Managed *o);
208 static qint64 virtualGetLength(const Managed *m);
209
210public:
211 template <typename T>
212 static inline uint calculateHashValue(const T *ch, const T* end, uint *subtype)
213 {
214 // array indices get their number as hash value
216 if (h != UINT_MAX) {
217 if (subtype)
218 *subtype = Heap::StringOrSymbol::StringType_ArrayIndex;
219 return h;
220 }
221
222 while (ch < end) {
223 h = 31 * h + charToUInt(ch);
224 ++ch;
225 }
226
227 if (subtype)
228 *subtype = (ch != end && charToUInt(ch) == '@') ? Heap::StringOrSymbol::StringType_Symbol : Heap::StringOrSymbol::StringType_Regular;
229 return h;
230 }
231};
232
235 QV4::Heap::ComplexString *d_unchecked() const { return static_cast<QV4::Heap::ComplexString *>(m()); }
238 dptr->_checkIsInitialized();
239 return dptr;
240 }
241};
242
243inline
244void StringOrSymbol::createPropertyKey() const {
245 Q_ASSERT(!d()->identifier.isValid());
247 static_cast<const String *>(this)->createPropertyKeyImpl();
248}
249
251 if (!d()->identifier.isValid())
252 createPropertyKey();
253 return d()->identifier;
254}
255
256template<>
257inline const StringOrSymbol *Value::as() const {
258 return isManaged() && m()->internalClass->vtable->isStringOrSymbol ? static_cast<const String *>(this) : nullptr;
259}
260
261template<>
262inline const String *Value::as() const {
263 return isManaged() && m()->internalClass->vtable->isString ? static_cast<const String *>(this) : nullptr;
264}
265
266template<>
268{
269 return v.toString(e)->asReturnedValue();
270}
271
272}
273
275
276#endif
\inmodule QtCore
Definition qchar.h:48
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
QString text
list append(new Employee("Blackpool", "Stephen"))
double e
Combined button and popup list for selecting options.
\qmltype Particle \inqmlmodule QtQuick.Particles
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:105
#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:27
unsigned int uint
Definition qtypes.h:29
long long qint64
Definition qtypes.h:55
#define V4_MANAGED(DataClass, superClass)
#define V4_NEEDS_DESTROY
#define Q_MANAGED_TYPE(type)
#define V4_INTERNALCLASS(c)
QObject::connect nullptr
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 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
QString toQString() const
static uint calculateHashValue(const T *ch, const T *end, uint *subtype)
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