diff options
author | Erik Verbruggen <erik.verbruggen@qt.io> | 2016-06-29 15:06:08 +0200 |
---|---|---|
committer | Erik Verbruggen <erik.verbruggen@qt.io> | 2016-07-13 08:16:39 +0000 |
commit | 9109d15398f8c869e401aa9aacc578c96200e217 (patch) | |
tree | a304ea0c5aabb46f3f306e7a5f444d224c918b59 /src/qml/jsruntime/qv4string_p.h | |
parent | b55485bd4ad6d9084e15e982e457a835aeda831d (diff) |
V4: allow for String::createHashValue to be inlined
Notably into QHashedString(Ref)::computeHash.
Change-Id: Icf8487ed3da0f117cb0911f20c9b88498f61510a
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src/qml/jsruntime/qv4string_p.h')
-rw-r--r-- | src/qml/jsruntime/qv4string_p.h | 66 |
1 files changed, 62 insertions, 4 deletions
diff --git a/src/qml/jsruntime/qv4string_p.h b/src/qml/jsruntime/qv4string_p.h index 60e2655536..ff42ab6471 100644 --- a/src/qml/jsruntime/qv4string_p.h +++ b/src/qml/jsruntime/qv4string_p.h @@ -52,6 +52,7 @@ #include <QtCore/qstring.h> #include "qv4managed_p.h" +#include <QtCore/private/qnumeric_p.h> QT_BEGIN_NAMESPACE @@ -62,7 +63,6 @@ struct Identifier; namespace Heap { -#ifndef V4_BOOTSTRAP struct Q_QML_PRIVATE_EXPORT String : Base { enum StringType { StringType_Unknown, @@ -70,6 +70,7 @@ struct Q_QML_PRIVATE_EXPORT String : Base { StringType_ArrayIndex }; +#ifndef V4_BOOTSTRAP String(MemoryManager *mm, const QString &text); String(MemoryManager *mm, String *l, String *n); ~String() { @@ -130,8 +131,8 @@ struct Q_QML_PRIVATE_EXPORT String : Base { MemoryManager *mm; private: static void append(const String *data, QChar *ch); -}; #endif +}; } @@ -183,8 +184,17 @@ struct Q_QML_PRIVATE_EXPORT String : public Managed { void makeIdentifierImpl(ExecutionEngine *e) const; - static uint createHashValue(const QChar *ch, int length, uint *subtype); - static uint createHashValue(const char *ch, int length, uint *subtype); + static uint createHashValue(const QChar *ch, int length, uint *subtype) + { + const QChar *end = ch + length; + return calculateHashValue(ch, end, subtype); + } + + static uint createHashValue(const char *ch, int length, uint *subtype) + { + const char *end = ch + length; + return calculateHashValue(ch, end, subtype); + } bool startsWithUpper() const { const String::Data *l = d(); @@ -203,6 +213,54 @@ protected: public: static uint toArrayIndex(const QString &str); + +private: + static inline uint toUInt(const QChar *ch) { return ch->unicode(); } + static inline uint toUInt(const char *ch) { return *ch; } + + template <typename T> + static inline uint toArrayIndex(const T *ch, const T *end) + { + uint i = toUInt(ch) - '0'; + if (i > 9) + return UINT_MAX; + ++ch; + // reject "01", "001", ... + if (i == 0 && ch != end) + return UINT_MAX; + + while (ch < end) { + uint x = toUInt(ch) - '0'; + if (x > 9) + return UINT_MAX; + if (mul_overflow(i, uint(10), &i) || add_overflow(i, x, &i)) // i = i * 10 + x + return UINT_MAX; + ++ch; + } + return i; + } + +public: + template <typename T> + static inline uint calculateHashValue(const T *ch, const T* end, uint *subtype) + { + // array indices get their number as hash value + uint h = toArrayIndex(ch, end); + if (h != UINT_MAX) { + if (subtype) + *subtype = Heap::String::StringType_ArrayIndex; + return h; + } + + while (ch < end) { + h = 31 * h + toUInt(ch); + ++ch; + } + + if (subtype) + *subtype = Heap::String::StringType_Regular; + return h; + } }; template<> |