| 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 | |
| 23 | QT_BEGIN_NAMESPACE |
| 24 | |
| 25 | namespace QV4 { |
| 26 | |
| 27 | struct ExecutionEngine; |
| 28 | struct PropertyKey; |
| 29 | |
| 30 | namespace Heap { |
| 31 | |
| 32 | struct Q_QML_EXPORT StringOrSymbol : Base |
| 33 | { |
| 34 | enum StringType { |
| 35 | StringType_Symbol, |
| 36 | StringType_Regular, |
| 37 | StringType_ArrayIndex, |
| 38 | StringType_Unknown, |
| 39 | StringType_AddedString, |
| 40 | StringType_SubString, |
| 41 | StringType_Complex = StringType_AddedString |
| 42 | }; |
| 43 | |
| 44 | void init() { |
| 45 | Base::init(); |
| 46 | new (&textStorage) QStringPrivate; |
| 47 | } |
| 48 | |
| 49 | void init(QStringPrivate text) |
| 50 | { |
| 51 | Base::init(); |
| 52 | new (&textStorage) QStringPrivate(std::move(text)); |
| 53 | } |
| 54 | |
| 55 | mutable struct { alignas(QStringPrivate) unsigned char data[sizeof(QStringPrivate)]; } textStorage; |
| 56 | mutable PropertyKey identifier; |
| 57 | mutable uint subtype; |
| 58 | mutable uint stringHash; |
| 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 | |
| 79 | struct 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 | |
| 113 | private: |
| 114 | static void append(const String *data, QChar *ch); |
| 115 | }; |
| 116 | Q_STATIC_ASSERT(std::is_trivial_v<String>); |
| 117 | |
| 118 | struct ComplexString : String { |
| 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 | }; |
| 129 | Q_STATIC_ASSERT(std::is_trivial_v<ComplexString>); |
| 130 | |
| 131 | inline |
| 132 | int 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 | |
| 139 | struct Q_QML_EXPORT StringOrSymbol : public Managed { |
| 140 | V4_MANAGED(StringOrSymbol, Managed) |
| 141 | V4_NEEDS_DESTROY |
| 142 | enum { |
| 143 | IsStringOrSymbol = true |
| 144 | }; |
| 145 | |
| 146 | private: |
| 147 | inline void createPropertyKey() const; |
| 148 | public: |
| 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 | |
| 158 | struct Q_QML_EXPORT String : public StringOrSymbol { |
| 159 | V4_MANAGED(String, StringOrSymbol) |
| 160 | Q_MANAGED_TYPE(String) |
| 161 | V4_INTERNALCLASS(String) |
| 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: other->d()); |
| 171 | } |
| 172 | inline bool isEqualTo(const String *other) const { |
| 173 | return d()->isEqualTo(other: 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 createHashValueDisallowingArrayIndex(const QChar *ch, int length, uint *subtype) |
| 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 | |
| 212 | protected: |
| 213 | static bool virtualIsEqualTo(Managed *that, Managed *o); |
| 214 | static qint64 virtualGetLength(const Managed *m); |
| 215 | |
| 216 | public: |
| 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) { |
| 224 | h = stringToArrayIndex(ch, end); |
| 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 | |
| 243 | struct ComplexString : String { |
| 244 | typedef QV4::Heap::ComplexString Data; |
| 245 | QV4::Heap::ComplexString *d_unchecked() const { return static_cast<QV4::Heap::ComplexString *>(m()); } |
| 246 | QV4::Heap::ComplexString *d() const { |
| 247 | QV4::Heap::ComplexString *dptr = d_unchecked(); |
| 248 | dptr->_checkIsInitialized(); |
| 249 | return dptr; |
| 250 | } |
| 251 | }; |
| 252 | |
| 253 | inline |
| 254 | void StringOrSymbol::createPropertyKey() const { |
| 255 | Q_ASSERT(!d()->identifier.isValid()); |
| 256 | Q_ASSERT(isString()); |
| 257 | static_cast<const String *>(this)->createPropertyKeyImpl(); |
| 258 | } |
| 259 | |
| 260 | inline PropertyKey StringOrSymbol::toPropertyKey() const { |
| 261 | if (!d()->identifier.isValid()) |
| 262 | createPropertyKey(); |
| 263 | return d()->identifier; |
| 264 | } |
| 265 | |
| 266 | template<> |
| 267 | inline const StringOrSymbol *Value::as() const { |
| 268 | return isManaged() && m()->internalClass->vtable->isStringOrSymbol ? static_cast<const String *>(this) : nullptr; |
| 269 | } |
| 270 | |
| 271 | template<> |
| 272 | inline const String *Value::as() const { |
| 273 | return isManaged() && m()->internalClass->vtable->isString ? static_cast<const String *>(this) : nullptr; |
| 274 | } |
| 275 | |
| 276 | template<> |
| 277 | inline ReturnedValue value_convert<String>(ExecutionEngine *e, const Value &v) |
| 278 | { |
| 279 | return v.toString(e)->asReturnedValue(); |
| 280 | } |
| 281 | |
| 282 | } |
| 283 | |
| 284 | QT_END_NAMESPACE |
| 285 | |
| 286 | #endif |
| 287 | |