Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qv4string.cpp
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
4#include "qv4string_p.h"
5#include "qv4value_p.h"
7#include "qv4runtime_p.h"
8#include <QtQml/private/qv4mm_p.h>
9#include <QtCore/QHash>
10#include <QtCore/private/qnumeric_p.h>
11
12using namespace QV4;
13
15{
16 StringOrSymbol *s = static_cast<StringOrSymbol *>(that);
18 if (id)
19 id->mark(markStack);
20}
21
23{
24 StringOrSymbol::markObjects(that, markStack);
25 String *s = static_cast<String *>(that);
26 if (s->subtype < StringType_Complex)
27 return;
28
29 ComplexString *cs = static_cast<ComplexString *>(s);
30 if (cs->subtype == StringType_AddedString) {
31 cs->left->mark(markStack);
32 cs->right->mark(markStack);
33 } else {
34 Q_ASSERT(cs->subtype == StringType_SubString);
35 cs->left->mark(markStack);
36 }
37}
38
41
42
44{
45 if (t == o)
46 return true;
47
48 if (!o->vtable()->isString)
49 return false;
50
51 return static_cast<String *>(t)->isEqualTo(static_cast<String *>(o));
52}
53
54
56{
57 QString mutableText(t);
58 StringOrSymbol::init(mutableText.data_ptr());
60}
61
63{
66
67 left = l;
68 right = r;
69 len = left->length() + right->length();
70 if (left->subtype >= StringType_Complex)
71 largestSubLength = static_cast<ComplexString *>(left)->largestSubLength;
72 else
73 largestSubLength = left->length();
74 if (right->subtype >= StringType_Complex)
75 largestSubLength = qMax(largestSubLength, static_cast<ComplexString *>(right)->largestSubLength);
76 else
77 largestSubLength = qMax(largestSubLength, right->length());
78
79 // make sure we don't get excessive depth in our strings
80 if (len > 256 && len >= 2*largestSubLength)
81 simplifyString();
82}
83
85{
86 Q_ASSERT(ref->length() >= from + len);
88
90
91 left = ref;
92 this->from = from;
93 this->len = len;
94}
95
97{
99 internalClass->engine->memoryManager->changeUnmanagedHeapSizeUsage(
100 qptrdiff(-text()->size) * qptrdiff(sizeof(QChar)));
101 }
102 text().~QStringPrivate();
104}
105
107{
108 *ok = true;
109
111 d()->createHashValue();
113 return d()->stringHash;
114
115 // required for UINT_MAX or numbers starting with a leading 0
117 uint l = (uint)d;
118 if (d == l)
119 return l;
120 *ok = false;
121 return UINT_MAX;
122}
123
125{
127 d()->simplifyString();
129 engine()->identifierTable->asPropertyKey(this);
130}
131
133{
134 Q_ASSERT(subtype >= StringType_AddedString);
135
136 int l = length();
138 QChar *ch = const_cast<QChar *>(result.constData());
139 append(this, ch);
140 text() = result.data_ptr();
141 const ComplexString *cs = static_cast<const ComplexString *>(this);
142 identifier = PropertyKey::invalid();
143 cs->left = cs->right = nullptr;
144
145 internalClass->engine->memoryManager->changeUnmanagedHeapSizeUsage(
146 qptrdiff(text().size) * qptrdiff(sizeof(QChar)));
147 subtype = StringType_Unknown;
148}
149
151{
152 if (subtype == StringType_AddedString)
153 return static_cast<const Heap::ComplexString *>(this)->left->startsWithUpper();
154
155 const Heap::String *str = this;
156 int offset = 0;
157 if (subtype == StringType_SubString) {
158 const ComplexString *cs = static_cast<const Heap::ComplexString *>(this);
159 if (!cs->len)
160 return false;
161 // simplification here is not ideal, but hopefully not a common case.
163 cs->left->simplifyString();
164 str = cs->left;
165 offset = cs->from;
166 }
168 return str->text().size > offset && QChar::isUpper(str->text().data()[offset]);
169}
170
171void Heap::String::append(const String *data, QChar *ch)
172{
173 std::vector<const String *> worklist;
174 worklist.reserve(32);
175 worklist.push_back(data);
176
177 while (!worklist.empty()) {
178 const String *item = worklist.back();
179 worklist.pop_back();
180
181 if (item->subtype == StringType_AddedString) {
182 const ComplexString *cs = static_cast<const ComplexString *>(item);
183 worklist.push_back(cs->right);
184 worklist.push_back(cs->left);
185 } else if (item->subtype == StringType_SubString) {
186 const ComplexString *cs = static_cast<const ComplexString *>(item);
187 memcpy(ch, cs->left->toQString().constData() + cs->from, cs->len*sizeof(QChar));
188 ch += cs->len;
189 } else {
190 memcpy(static_cast<void *>(ch), item->text().data(), item->text().size * sizeof(QChar));
191 ch += item->text().size;
192 }
193 }
194}
195
197{
198 if (subtype >= StringType_AddedString) {
199 Q_ASSERT(internalClass->vtable->isString);
200 static_cast<const Heap::String *>(this)->simplifyString();
201 }
202 Q_ASSERT(subtype < StringType_AddedString);
203 const QChar *ch = reinterpret_cast<const QChar *>(text().data());
204 const QChar *end = ch + text().size;
205 stringHash = QV4::String::calculateHashValue(ch, end, &subtype);
206}
207
209{
210 return static_cast<const String *>(m)->d()->length();
211}
\inmodule QtCore
Definition qchar.h:48
constexpr bool isUpper() const noexcept
Returns true if the character is an uppercase letter, for example category() is Letter_Uppercase.
Definition qchar.h:475
QVariant data(int key) const
Returns this item's custom data for the key key as a QVariant.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
const QChar * constData() const
Returns a pointer to the data stored in the QString.
Definition qstring.h:1101
qsizetype size() const
Returns the number of characters in this string.
Definition qstring.h:182
DataPointer & data_ptr()
Definition qstring.h:986
QChar * data()
Returns a pointer to the data stored in the QString.
Definition qstring.h:1095
QString str
[2]
QString text
list append(new Employee("Blackpool", "Stephen"))
\qmltype Particle \inqmlmodule QtQuick.Particles
constexpr Initialization Uninitialized
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
const GLfloat * m
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLboolean r
[2]
GLuint GLuint end
GLenum GLuint GLenum GLsizei length
GLdouble GLdouble right
GLint left
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum GLuint GLintptr offset
GLint ref
GLenum GLsizei len
GLdouble GLdouble t
Definition qopenglext.h:243
GLuint64EXT * result
[6]
GLdouble s
[6]
Definition qopenglext.h:235
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
ptrdiff_t qptrdiff
Definition qtypes.h:69
unsigned int uint
Definition qtypes.h:29
long long qint64
Definition qtypes.h:55
#define DEFINE_MANAGED_VTABLE(classname)
QGraphicsItem * item
QJSEngine engine
[0]
void mark(QV4::MarkStack *markStack)
Definition qv4heap_p.h:138
static void markObjects(Heap::Base *that, MarkStack *markStack)
Definition qv4string.cpp:14
static void markObjects(Heap::Base *that, MarkStack *markStack)
Definition qv4string.cpp:22
bool startsWithUpper() const
QString toQString() const
Definition qv4string_p.h:92
int length() const
void simplifyString() const
bool isEqualTo(const String *other) const
Definition qv4string_p.h:97
void mark(MarkStack *markStack)
StringOrSymbol * asStringOrSymbol() const
static PropertyKey invalid()
static double stringToNumber(const QString &s)
Q_NEVER_INLINE void createPropertyKeyImpl() const
uint toUInt(bool *ok) const
QString toQString() const
static uint calculateHashValue(const T *ch, const T *end, uint *subtype)
static constexpr VTable::GetLength virtualGetLength
static constexpr VTable::IsEqualTo virtualIsEqualTo