1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL21$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
32 ****************************************************************************/
36 #include <QtCore/qstring.h>
37 #include "qv4managed_p.h"
43 struct ExecutionEngine;
49 struct Q_QML_PRIVATE_EXPORT String : Base {
56 String(const QString &text);
57 String(String *l, String *n);
59 if (!largestSubLength && !text->ref.deref())
60 QStringData::deallocate(text);
62 void simplifyString() const;
64 Q_ASSERT((largestSubLength &&
65 (len == left->len + right->len)) ||
66 len == (uint)text->size);
69 void createHashValue() const;
70 inline unsigned hashValue() const {
71 if (subtype == StringType_Unknown)
73 Q_ASSERT(!largestSubLength);
77 inline QString toQString() const {
80 QStringDataPtr ptr = { text };
84 inline bool isEqualTo(const String *other) const {
87 if (hashValue() != other->hashValue())
89 Q_ASSERT(!largestSubLength);
90 if (identifier && identifier == other->identifier)
92 if (subtype == Heap::String::StringType_ArrayIndex && other->subtype == Heap::String::StringType_ArrayIndex)
95 return toQString() == other->toQString();
99 mutable QStringData *text;
100 mutable String *left;
103 mutable Identifier *identifier;
104 mutable String *right;
106 mutable uint subtype;
107 mutable uint stringHash;
108 mutable uint largestSubLength;
111 static void append(const String *data, QChar *ch);
117 struct Q_QML_PRIVATE_EXPORT String : public Managed {
119 V4_MANAGED(String, Managed)
120 Q_MANAGED_TYPE(String)
126 uchar subtype() const { return d()->subtype; }
127 void setSubtype(uchar subtype) const { d()->subtype = subtype; }
129 bool equals(String *other) const {
130 return d()->isEqualTo(other->d());
132 inline bool isEqualTo(const String *other) const {
133 return d()->isEqualTo(other->d());
136 inline bool compare(const String *other) {
137 return toQString() < other->toQString();
140 inline QString toQString() const {
141 return d()->toQString();
144 inline unsigned hashValue() const {
145 return d()->hashValue();
147 uint asArrayIndex() const {
148 if (subtype() == Heap::String::StringType_Unknown)
149 d()->createHashValue();
150 Q_ASSERT(!d()->largestSubLength);
151 if (subtype() == Heap::String::StringType_ArrayIndex)
152 return d()->stringHash;
155 uint toUInt(bool *ok) const;
157 void makeIdentifier(ExecutionEngine *e) const {
160 makeIdentifierImpl(e);
163 void makeIdentifierImpl(ExecutionEngine *e) const;
165 static uint createHashValue(const QChar *ch, int length);
166 static uint createHashValue(const char *ch, int length);
168 bool startsWithUpper() const {
169 const String::Data *l = d();
170 while (l->largestSubLength)
172 return l->text->size && QChar::isUpper(l->text->data()[0]);
175 Identifier *identifier() const { return d()->identifier; }
178 static void markObjects(Heap::Base *that, ExecutionEngine *e);
179 static bool isEqualTo(Managed *that, Managed *o);
180 static uint getLength(const Managed *m);
184 static uint toArrayIndex(const QString &str);
188 inline const String *Value::as() const {
189 return isManaged() && m() && m()->vtable()->isString ? static_cast<const String *>(this) : 0;
194 inline ReturnedValue value_convert<String>(ExecutionEngine *e, const Value &v)
196 return v.toString(e)->asReturnedValue();