1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #ifndef QHASHEDSTRING_P_H
43 #define QHASHEDSTRING_P_H
49 // This file is not part of the Qt API. It exists purely as an
50 // implementation detail. This header file may change from version to
51 // version without notice, or even be removed.
56 #include <QtCore/qglobal.h>
57 #include <QtCore/qstring.h>
58 #include <private/qv8_p.h>
62 class QHashedStringRef;
63 class Q_AUTOTEST_EXPORT QHashedString : public QString
66 inline QHashedString();
67 inline QHashedString(const QString &string);
68 inline QHashedString(const QString &string, quint32);
69 inline QHashedString(const QHashedString &string);
71 inline QHashedString &operator=(const QHashedString &string);
72 inline bool operator==(const QHashedString &string) const;
73 inline bool operator==(const QHashedStringRef &string) const;
75 inline quint32 hash() const;
76 inline quint32 existingHash() const;
78 static inline bool isUpper(const QChar &);
80 static bool compare(const QChar *lhs, const QChar *rhs, int length);
81 static inline bool compare(const QChar *lhs, const char *rhs, int length);
82 static inline bool compare(const char *lhs, const char *rhs, int length);
84 friend class QHashedStringRef;
85 friend class QStringHashNode;
87 void computeHash() const;
88 mutable quint32 m_hash;
91 class Q_AUTOTEST_EXPORT QHashedV8String
94 inline QHashedV8String();
95 explicit inline QHashedV8String(v8::Handle<v8::String>);
96 inline QHashedV8String(const QHashedV8String &string);
97 inline QHashedV8String &operator=(const QHashedV8String &other);
99 inline bool operator==(const QHashedV8String &string);
101 inline quint32 hash() const;
102 inline int length() const;
103 inline quint32 symbolId() const;
105 inline v8::Handle<v8::String> string() const;
108 v8::String::CompleteHashData m_hash;
109 v8::Handle<v8::String> m_string;
112 class QHashedCStringRef;
113 class Q_AUTOTEST_EXPORT QHashedStringRef
116 inline QHashedStringRef();
117 inline QHashedStringRef(const QString &);
118 inline QHashedStringRef(const QStringRef &);
119 inline QHashedStringRef(const QChar *, int);
120 inline QHashedStringRef(const QChar *, int, quint32);
121 inline QHashedStringRef(const QHashedString &);
122 inline QHashedStringRef(const QHashedStringRef &);
123 inline QHashedStringRef &operator=(const QHashedStringRef &);
125 inline bool operator==(const QString &string) const;
126 inline bool operator==(const QHashedString &string) const;
127 inline bool operator==(const QHashedStringRef &string) const;
128 inline bool operator==(const QHashedCStringRef &string) const;
129 inline bool operator!=(const QString &string) const;
130 inline bool operator!=(const QHashedString &string) const;
131 inline bool operator!=(const QHashedStringRef &string) const;
132 inline bool operator!=(const QHashedCStringRef &string) const;
134 inline quint32 hash() const;
136 inline const QChar &at(int) const;
137 inline const QChar *constData() const;
138 bool startsWith(const QString &) const;
139 bool endsWith(const QString &) const;
140 QHashedStringRef mid(int, int) const;
142 inline bool isEmpty() const;
143 inline int length() const;
144 inline bool startsWithUpper() const;
146 QString toString() const;
148 inline int utf8length() const;
149 QByteArray toUtf8() const;
150 void writeUtf8(char *) const;
152 friend class QHashedString;
154 void computeHash() const;
155 void computeUtf8Length() const;
159 mutable int m_utf8length;
160 mutable quint32 m_hash;
163 class Q_AUTOTEST_EXPORT QHashedCStringRef
166 inline QHashedCStringRef();
167 inline QHashedCStringRef(const char *, int);
168 inline QHashedCStringRef(const char *, int, quint32);
169 inline QHashedCStringRef(const QHashedCStringRef &);
171 inline quint32 hash() const;
173 inline const char *constData() const;
174 inline int length() const;
176 QString toUtf16() const;
177 inline int utf16length() const;
178 inline void writeUtf16(QChar *) const;
179 inline void writeUtf16(uint16_t *) const;
181 friend class QHashedStringRef;
183 void computeHash() const;
187 mutable quint32 m_hash;
190 class QStringHashData;
191 class Q_AUTOTEST_EXPORT QStringHashNode
195 : nlist(0), next(0), length(0), hash(0), pooled(0), ckey(0), ukey(0), symbolId()
199 QStringHashNode(const QHashedString &key)
200 : nlist(0), next(0), length(key.length()), hash(key.hash()), pooled(0), ckey(0), key(key),
201 ukey(key.constData()), symbolId(0) {
204 QStringHashNode(const QHashedCStringRef &key)
205 : nlist(0), next(0), length(key.length()), hash(key.hash()), pooled(0), ckey(key.constData()),
206 ukey(0), symbolId(0) {
209 QStringHashNode(const QStringHashNode &o)
210 : nlist(0), next(0), length(o.length), hash(o.hash), pooled(0), ckey(o.ckey), key(o.key),
211 ukey(o.ukey), symbolId(o.symbolId) {
214 QStringHashNode *nlist;
215 QStringHashNode *next;
226 inline bool equals(v8::Handle<v8::String> string) {
227 return ckey?string->Equals((char*)ckey, length):
228 string->Equals((uint16_t*)ukey, length);
231 inline bool symbolEquals(const QHashedV8String &string) {
232 Q_ASSERT(string.symbolId() != 0);
233 return length == string.length() && hash == string.hash() &&
234 (string.symbolId() == symbolId || equals(string.string()));
237 inline bool equals(const QHashedV8String &string) {
238 return length == string.length() && hash == string.hash() &&
239 equals(string.string());
242 inline bool equals(const QHashedStringRef &string) {
243 return length == string.length() &&
244 hash == string.hash() &&
245 (ckey?(QHashedString::compare(string.constData(), ckey, length)):
246 (QHashedString::compare(string.constData(), ukey, length)));
249 inline bool equals(const QHashedCStringRef &string) {
250 return length == string.length() &&
251 hash == string.hash() &&
252 (ckey?(QHashedString::compare(string.constData(), ckey, length)):
253 (QHashedString::compare(ukey, string.constData(), length)));
257 struct Q_AUTOTEST_EXPORT QStringHashData
261 : nodes(0), buckets(0), numBuckets(0), size(0), numBits(0) {}
263 QStringHashNode *nodes;
264 QStringHashNode **buckets;
269 void rehashToBits(short);
270 void rehashToSize(int);
273 QStringHashData(const QStringHashData &);
274 QStringHashData &operator=(const QStringHashData &);
277 template<class T, int SmallThreshold = 0>
278 class Q_AUTOTEST_EXPORT QStringHash
281 struct Node : public QStringHashNode {
282 Node(const QHashedString &key, const T &value) : QStringHashNode(key), value(value) {}
283 Node(const QHashedCStringRef &key, const T &value) : QStringHashNode(key), value(value) {}
284 Node(const Node &o) : QStringHashNode(o), value(o.value) {}
288 struct ReservedNodePool
290 ReservedNodePool() : count(0), used(0), nodes(0) {}
291 ~ReservedNodePool() { delete [] nodes; }
297 QStringHashData data;
298 ReservedNodePool *nodePool;
300 inline Node *findNode(const QString &) const;
301 inline Node *findNode(const QHashedString &) const;
302 inline Node *findNode(const QHashedStringRef &) const;
303 inline Node *findNode(const QHashedCStringRef &) const;
304 inline Node *findNode(const QHashedV8String &) const;
305 inline Node *findSymbolNode(const QHashedV8String &) const;
306 inline Node *createNode(const QHashedString &, const T &);
307 inline Node *createNode(const QHashedCStringRef &, const T &);
309 inline Node *takeNode(const QHashedString &key, const T &value);
310 inline Node *takeNode(const QHashedCStringRef &key, const T &value);
311 inline Node *takeNode(const Node &o);
313 inline void copy(const QStringHash<T,SmallThreshold> &);
315 inline QStringHash();
316 inline QStringHash(const QStringHash &);
317 inline ~QStringHash();
319 QStringHash &operator=(const QStringHash<T,SmallThreshold> &);
321 void copyAndReserve(const QStringHash<T,SmallThreshold> &other, int additionalReserve);
323 inline bool isEmpty() const;
325 inline int count() const;
327 inline void insert(const QString &, const T &);
328 inline void insert(const QHashedString &, const T &);
329 inline void insert(const QHashedStringRef &, const T &);
330 inline void insert(const QHashedCStringRef &, const T &);
332 inline T *value(const QString &) const;
333 inline T *value(const QHashedString &) const;
334 inline T *value(const QHashedStringRef &) const;
335 inline T *value(const QHashedV8String &) const;
336 inline T *value(const QHashedCStringRef &) const;
338 inline bool contains(const QString &) const;
339 inline bool contains(const QHashedString &) const;
340 inline bool contains(const QHashedStringRef &) const;
341 inline bool contains(const QHashedCStringRef &) const;
343 T &operator[](const QString &);
344 T &operator[](const QHashedString &);
345 T &operator[](const QHashedStringRef &);
346 T &operator[](const QHashedCStringRef &);
348 class ConstIterator {
350 ConstIterator() : n(0) {}
351 ConstIterator(Node *n) : n(n) {}
353 ConstIterator &operator++() { n = (Node *)n->nlist; return *this; }
354 bool operator==(const ConstIterator &o) const { return n == o.n; }
355 bool operator!=(const ConstIterator &o) const { return n != o.n; }
357 QHashedString key() const {
359 return QHashedString(QString::fromLatin1(n->ckey, n->length), n->hash);
361 return QHashedString(n->key, n->hash);
364 const T &value() const { return n->value; }
365 const T &operator*() const { return n->value; }
370 ConstIterator begin() const { return ConstIterator((Node *)data.nodes); }
371 ConstIterator end() const { return ConstIterator(); }
373 inline void reserve(int);
376 template<class T, int SmallThreshold>
377 QStringHash<T,SmallThreshold>::QStringHash()
382 template<class T, int SmallThreshold>
383 QStringHash<T,SmallThreshold>::QStringHash(const QStringHash<T,SmallThreshold> &other)
386 data.numBits = other.data.numBits;
387 data.size = other.data.size;
388 reserve(other.count());
392 template<class T, int SmallThreshold>
393 QStringHash<T,SmallThreshold> &QStringHash<T,SmallThreshold>::operator=(const QStringHash<T,SmallThreshold> &other)
400 data.numBits = other.data.numBits;
401 data.size = other.data.size;
402 reserve(other.count());
408 template<class T, int SmallThreshold>
409 void QStringHash<T,SmallThreshold>::copyAndReserve(const QStringHash<T,SmallThreshold> &other, int additionalReserve)
413 data.numBits = other.data.numBits;
414 data.size = other.data.size;;
415 reserve(other.count() + additionalReserve);
419 template<class T, int SmallThreshold>
420 QStringHash<T,SmallThreshold>::~QStringHash()
425 template<class T, int SmallThreshold>
426 void QStringHash<T,SmallThreshold>::clear()
428 // If all the nodes were allocated from the node pool, we
429 // don't need to clean them individually
430 if (!nodePool || data.size != nodePool->used) {
431 QStringHashNode *n = data.nodes;
435 if (!o->pooled) delete o;
438 if (nodePool) delete nodePool;
439 delete [] data.buckets;
450 template<class T, int SmallThreshold>
451 bool QStringHash<T,SmallThreshold>::isEmpty() const
453 return data.nodes == 0;
456 template<class T, int SmallThreshold>
457 int QStringHash<T,SmallThreshold>::count() const
462 template<class T, int SmallThreshold>
463 typename QStringHash<T,SmallThreshold>::Node *QStringHash<T,SmallThreshold>::takeNode(const QHashedString &key, const T &value)
465 if (nodePool && nodePool->used != nodePool->count) {
466 Node *rv = nodePool->nodes + nodePool->used++;
467 rv->length = key.length();
468 rv->hash = key.hash();
470 rv->ukey = rv->key.constData();
475 return new Node(key, value);
479 template<class T, int SmallThreshold>
480 typename QStringHash<T,SmallThreshold>::Node *QStringHash<T,SmallThreshold>::takeNode(const QHashedCStringRef &key, const T &value)
482 if (nodePool && nodePool->used != nodePool->count) {
483 Node *rv = nodePool->nodes + nodePool->used++;
484 rv->length = key.length();
485 rv->hash = key.hash();
486 rv->ckey = key.constData();
491 return new Node(key, value);
495 template<class T, int SmallThreshold>
496 typename QStringHash<T,SmallThreshold>::Node *QStringHash<T,SmallThreshold>::takeNode(const Node &o)
498 if (nodePool && nodePool->used != nodePool->count) {
499 Node *rv = nodePool->nodes + nodePool->used++;
500 rv->length = o.length;
506 rv->symbolId = o.symbolId;
514 template<class T, int SmallThreshold>
515 void QStringHash<T,SmallThreshold>::copy(const QStringHash<T,SmallThreshold> &other)
517 Q_ASSERT(data.nodes == 0);
519 if (other.data.size <= SmallThreshold) {
520 QStringHashNode *n = other.data.nodes;
523 Node *mynode = takeNode(*o);
524 mynode->nlist = data.nodes;
525 mynode->next = data.nodes;
531 // Ensure buckets array is created
532 data.rehashToBits(data.numBits);
534 QStringHashNode *n = other.data.nodes;
537 Node *mynode = takeNode(*o);
538 mynode->nlist = data.nodes;
541 int bucket = mynode->hash % data.numBuckets;
542 mynode->next = data.buckets[bucket];
543 data.buckets[bucket] = mynode;
550 template<class T, int SmallThreshold>
551 typename QStringHash<T,SmallThreshold>::Node *QStringHash<T,SmallThreshold>::createNode(const QHashedString &key, const T &value)
553 Node *n = takeNode(key, value);
555 if (data.size < SmallThreshold) {
557 n->nlist = data.nodes;
558 n->next = data.nodes;
562 if (data.size >= data.numBuckets)
563 data.rehashToBits(data.numBits + 1);
565 n->nlist = data.nodes;
568 int bucket = key.hash() % data.numBuckets;
569 n->next = data.buckets[bucket];
570 data.buckets[bucket] = n;
578 template<class T, int SmallThreshold>
579 typename QStringHash<T,SmallThreshold>::Node *QStringHash<T,SmallThreshold>::createNode(const QHashedCStringRef &key, const T &value)
581 Node *n = takeNode(key, value);
583 if (data.size < SmallThreshold) {
585 n->nlist = data.nodes;
586 n->next = data.nodes;
590 if (data.size >= data.numBuckets)
591 data.rehashToBits(data.numBits + 1);
593 n->nlist = data.nodes;
596 int bucket = key.hash() % data.numBuckets;
597 n->next = data.buckets[bucket];
598 data.buckets[bucket] = n;
606 template<class T, int SmallThreshold>
607 void QStringHash<T,SmallThreshold>::insert(const QString &key, const T &value)
609 QHashedStringRef ch(key);
610 Node *n = findNode(key);
611 if (n) n->value = value;
612 else createNode(QHashedString(key, ch.hash()), value);
615 template<class T, int SmallThreshold>
616 void QStringHash<T,SmallThreshold>::insert(const QHashedString &key, const T &value)
618 Node *n = findNode(key);
619 if (n) n->value = value;
620 else createNode(key, value);
623 template<class T, int SmallThreshold>
624 void QStringHash<T,SmallThreshold>::insert(const QHashedStringRef &key, const T &value)
626 Node *n = findNode(key);
627 if (n) n->value = value;
628 else createNode(key, value);
631 template<class T, int SmallThreshold>
632 void QStringHash<T,SmallThreshold>::insert(const QHashedCStringRef &key, const T &value)
634 Node *n = findNode(key);
635 if (n) n->value = value;
636 else createNode(key, value);
639 template<class T, int SmallThreshold>
640 typename QStringHash<T,SmallThreshold>::Node *QStringHash<T,SmallThreshold>::findNode(const QString &string) const
642 return findNode(QHashedStringRef(string));
645 template<class T, int SmallThreshold>
646 typename QStringHash<T,SmallThreshold>::Node *QStringHash<T,SmallThreshold>::findNode(const QHashedString &string) const
648 return findNode(QHashedStringRef(string.constData(), string.length(), string.hash()));
651 template<class T, int SmallThreshold>
652 typename QStringHash<T,SmallThreshold>::Node *QStringHash<T,SmallThreshold>::findNode(const QHashedStringRef &string) const
654 QStringHashNode *node = (data.size <= SmallThreshold)?data.nodes:data.buckets[string.hash() % data.numBuckets];
655 while (node && !node->equals(string))
661 template<class T, int SmallThreshold>
662 typename QStringHash<T,SmallThreshold>::Node *QStringHash<T,SmallThreshold>::findNode(const QHashedCStringRef &string) const
664 QStringHashNode *node = (data.size <= SmallThreshold)?data.nodes:data.buckets[string.hash() % data.numBuckets];
665 while (node && !node->equals(string))
671 template<class T, int SmallThreshold>
672 typename QStringHash<T,SmallThreshold>::Node *QStringHash<T,SmallThreshold>::findNode(const QHashedV8String &string) const
674 QStringHashNode *node = (data.size <= SmallThreshold)?data.nodes:data.buckets[string.hash() % data.numBuckets];
675 while (node && !node->equals(string))
681 template<class T, int SmallThreshold>
682 typename QStringHash<T,SmallThreshold>::Node *QStringHash<T,SmallThreshold>::findSymbolNode(const QHashedV8String &string) const
684 Q_ASSERT(string.symbolId() != 0);
686 QStringHashNode *node = (data.size <= SmallThreshold)?data.nodes:data.buckets[string.hash() % data.numBuckets];
687 while (node && !node->symbolEquals(string))
691 node->symbolId = string.symbolId();
696 template<class T, int SmallThreshold>
697 T *QStringHash<T,SmallThreshold>::value(const QString &key) const
699 Node *n = findNode(key);
700 return n?&n->value:0;
703 template<class T, int SmallThreshold>
704 T *QStringHash<T,SmallThreshold>::value(const QHashedString &key) const
706 Node *n = findNode(key);
707 return n?&n->value:0;
710 template<class T, int SmallThreshold>
711 T *QStringHash<T,SmallThreshold>::value(const QHashedStringRef &key) const
713 Node *n = findNode(key);
714 return n?&n->value:0;
717 template<class T, int SmallThreshold>
718 T *QStringHash<T,SmallThreshold>::value(const QHashedCStringRef &key) const
720 Node *n = findNode(key);
721 return n?&n->value:0;
724 template<class T, int SmallThreshold>
725 T *QStringHash<T,SmallThreshold>::value(const QHashedV8String &string) const
727 Node *n = string.symbolId()?findSymbolNode(string):findNode(string);
728 return n?&n->value:0;
731 template<class T, int SmallThreshold>
732 bool QStringHash<T,SmallThreshold>::contains(const QString &s) const
734 return 0 != value(s);
737 template<class T, int SmallThreshold>
738 bool QStringHash<T,SmallThreshold>::contains(const QHashedString &s) const
740 return 0 != value(s);
743 template<class T, int SmallThreshold>
744 bool QStringHash<T,SmallThreshold>::contains(const QHashedStringRef &s) const
746 return 0 != value(s);
749 template<class T, int SmallThreshold>
750 bool QStringHash<T,SmallThreshold>::contains(const QHashedCStringRef &s) const
752 return 0 != value(s);
755 template<class T, int SmallThreshold>
756 T &QStringHash<T,SmallThreshold>::operator[](const QString &key)
758 QHashedStringRef cs(key);
759 Node *n = findNode(cs);
760 if (n) return n->value;
761 else return createNode(QHashedString(key, cs.hash()), T())->value;
764 template<class T, int SmallThreshold>
765 T &QStringHash<T,SmallThreshold>::operator[](const QHashedString &key)
767 Node *n = findNode(key);
768 if (n) return n->value;
769 else return createNode(key, T())->value;
772 template<class T, int SmallThreshold>
773 T &QStringHash<T,SmallThreshold>::operator[](const QHashedStringRef &key)
775 Node *n = findNode(key);
776 if (n) return n->value;
777 else return createNode(key, T())->value;
780 template<class T, int SmallThreshold>
781 T &QStringHash<T,SmallThreshold>::operator[](const QHashedCStringRef &key)
783 Node *n = findNode(key);
784 if (n) return n->value;
785 else return createNode(key, T())->value;
788 template<class T, int SmallThreshold>
789 void QStringHash<T,SmallThreshold>::reserve(int n)
791 if (nodePool || 0 == n)
793 nodePool = new ReservedNodePool;
796 nodePool->nodes = new Node[n];
798 if (n > SmallThreshold)
799 data.rehashToSize(n);
802 inline uint qHash(const QHashedString &string)
804 return uint(string.hash());
807 inline uint qHash(const QHashedStringRef &string)
809 return uint(string.hash());
812 QHashedString::QHashedString()
813 : QString(), m_hash(0)
817 QHashedString::QHashedString(const QString &string)
818 : QString(string), m_hash(0)
822 QHashedString::QHashedString(const QString &string, quint32 hash)
823 : QString(string), m_hash(hash)
827 QHashedString::QHashedString(const QHashedString &string)
828 : QString(string), m_hash(string.m_hash)
832 QHashedString &QHashedString::operator=(const QHashedString &string)
834 static_cast<QString &>(*this) = string;
835 m_hash = string.m_hash;
839 bool QHashedString::operator==(const QHashedString &string) const
841 return (string.m_hash == m_hash || !string.m_hash || !m_hash) &&
842 static_cast<const QString &>(*this) == static_cast<const QString &>(string);
845 bool QHashedString::operator==(const QHashedStringRef &string) const
847 return length() == string.m_length &&
848 (string.m_hash == m_hash || !string.m_hash || !m_hash) &&
849 QHashedString::compare(constData(), string.m_data, string.m_length);
852 quint32 QHashedString::hash() const
854 if (!m_hash) computeHash();
858 quint32 QHashedString::existingHash() const
863 bool QHashedString::isUpper(const QChar &qc)
865 ushort c = qc.unicode();
866 // Optimize for _, a-z and A-Z.
867 return ((c != '_' ) && (!(c >= 'a' && c <= 'z')) &&
868 ((c >= 'A' && c <= 'Z') || QChar::category(c) == QChar::Letter_Uppercase));
871 QHashedV8String::QHashedV8String()
875 QHashedV8String::QHashedV8String(v8::Handle<v8::String> string)
876 : m_hash(string->CompleteHash()), m_string(string)
878 Q_ASSERT(!m_string.IsEmpty());
881 QHashedV8String::QHashedV8String(const QHashedV8String &string)
882 : m_hash(string.m_hash), m_string(string.m_string)
886 QHashedV8String &QHashedV8String::operator=(const QHashedV8String &other)
888 m_hash = other.m_hash;
889 m_string = other.m_string;
893 bool QHashedV8String::operator==(const QHashedV8String &string)
895 return m_hash.hash == string.m_hash.hash && m_hash.length == string.m_hash.length &&
896 m_string.IsEmpty() == m_string.IsEmpty() &&
897 (m_string.IsEmpty() || m_string->StrictEquals(string.m_string));
900 quint32 QHashedV8String::hash() const
905 int QHashedV8String::length() const
907 return m_hash.length;
910 quint32 QHashedV8String::symbolId() const
912 return m_hash.symbol_id;
915 v8::Handle<v8::String> QHashedV8String::string() const
920 QHashedStringRef::QHashedStringRef()
921 : m_data(0), m_length(0), m_utf8length(-1), m_hash(0)
925 QHashedStringRef::QHashedStringRef(const QString &str)
926 : m_data(str.constData()), m_length(str.length()), m_utf8length(0), m_hash(0)
930 QHashedStringRef::QHashedStringRef(const QStringRef &str)
931 : m_data(str.constData()), m_length(str.length()), m_utf8length(0), m_hash(0)
935 QHashedStringRef::QHashedStringRef(const QChar *data, int length)
936 : m_data(data), m_length(length), m_utf8length(0), m_hash(0)
940 QHashedStringRef::QHashedStringRef(const QChar *data, int length, quint32 hash)
941 : m_data(data), m_length(length), m_utf8length(0), m_hash(hash)
945 QHashedStringRef::QHashedStringRef(const QHashedString &string)
946 : m_data(string.constData()), m_length(string.length()), m_utf8length(0), m_hash(string.m_hash)
950 QHashedStringRef::QHashedStringRef(const QHashedStringRef &string)
951 : m_data(string.m_data), m_length(string.m_length), m_utf8length(string.m_utf8length),
952 m_hash(string.m_hash)
956 QHashedStringRef &QHashedStringRef::operator=(const QHashedStringRef &o)
959 m_length = o.m_length;
960 m_utf8length = o.m_utf8length;
965 bool QHashedStringRef::operator==(const QString &string) const
967 return m_length == string.length() &&
968 QHashedString::compare(string.constData(), m_data, m_length);
971 bool QHashedStringRef::operator==(const QHashedString &string) const
973 return m_length == string.length() &&
974 (m_hash == string.m_hash || !m_hash || !string.m_hash) &&
975 QHashedString::compare(string.constData(), m_data, m_length);
978 bool QHashedStringRef::operator==(const QHashedStringRef &string) const
980 return m_length == string.m_length &&
981 (m_hash == string.m_hash || !m_hash || !string.m_hash) &&
982 QHashedString::compare(string.m_data, m_data, m_length);
985 bool QHashedStringRef::operator==(const QHashedCStringRef &string) const
987 return m_length == string.m_length &&
988 (m_hash == string.m_hash || !m_hash || !string.m_hash) &&
989 QHashedString::compare(m_data, string.m_data, m_length);
992 bool QHashedStringRef::operator!=(const QString &string) const
994 return m_length != string.length() ||
995 !QHashedString::compare(string.constData(), m_data, m_length);
998 bool QHashedStringRef::operator!=(const QHashedString &string) const
1000 return m_length != string.length() ||
1001 (m_hash != string.m_hash && m_hash && string.m_hash) ||
1002 !QHashedString::compare(string.constData(), m_data, m_length);
1005 bool QHashedStringRef::operator!=(const QHashedStringRef &string) const
1007 return m_length != string.m_length ||
1008 (m_hash != string.m_hash && m_hash && string.m_hash) ||
1009 QHashedString::compare(string.m_data, m_data, m_length);
1012 bool QHashedStringRef::operator!=(const QHashedCStringRef &string) const
1014 return m_length != string.m_length ||
1015 (m_hash != string.m_hash && m_hash && string.m_hash) ||
1016 QHashedString::compare(m_data, string.m_data, m_length);
1019 const QChar &QHashedStringRef::at(int index) const
1021 Q_ASSERT(index < m_length);
1022 return m_data[index];
1025 const QChar *QHashedStringRef::constData() const
1030 bool QHashedStringRef::isEmpty() const
1032 return m_length == 0;
1035 int QHashedStringRef::length() const
1040 int QHashedStringRef::utf8length() const
1042 if (m_utf8length < m_length)
1043 computeUtf8Length();
1044 return m_utf8length;
1047 bool QHashedStringRef::startsWithUpper() const
1049 if (m_length < 1) return false;
1050 return QHashedString::isUpper(m_data[0]);
1053 quint32 QHashedStringRef::hash() const
1055 if (!m_hash) computeHash();
1059 QHashedCStringRef::QHashedCStringRef()
1060 : m_data(0), m_length(0), m_hash(0)
1064 QHashedCStringRef::QHashedCStringRef(const char *data, int length)
1065 : m_data(data), m_length(length), m_hash(0)
1069 QHashedCStringRef::QHashedCStringRef(const char *data, int length, quint32 hash)
1070 : m_data(data), m_length(length), m_hash(hash)
1074 QHashedCStringRef::QHashedCStringRef(const QHashedCStringRef &o)
1075 : m_data(o.m_data), m_length(o.m_length), m_hash(o.m_hash)
1079 quint32 QHashedCStringRef::hash() const
1081 if (!m_hash) computeHash();
1085 const char *QHashedCStringRef::constData() const
1090 int QHashedCStringRef::length() const
1095 int QHashedCStringRef::utf16length() const
1100 void QHashedCStringRef::writeUtf16(QChar *output) const
1102 writeUtf16((uint16_t *)output);
1105 void QHashedCStringRef::writeUtf16(uint16_t *output) const
1108 const char *d = m_data;
1113 bool QHashedString::compare(const QChar *lhs, const char *rhs, int length)
1115 Q_ASSERT(lhs && rhs);
1116 const quint16 *l = (const quint16*)lhs;
1118 if (*l++ != *rhs++) return false;
1122 bool QHashedString::compare(const char *lhs, const char *rhs, int length)
1124 Q_ASSERT(lhs && rhs);
1125 return 0 == ::memcmp(lhs, rhs, length);
1130 #endif // QHASHEDSTRING_P_H