StringHash.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (C) 2006, 2007, 2008, 2012, 2013 Apple Inc. All rights reserved
  3. * Copyright (C) Research In Motion Limited 2009. All rights reserved.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public License
  16. * along with this library; see the file COPYING.LIB. If not, write to
  17. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18. * Boston, MA 02110-1301, USA.
  19. *
  20. */
  21. #ifndef StringHash_h
  22. #define StringHash_h
  23. #include <wtf/text/AtomicString.h>
  24. #include <wtf/HashTraits.h>
  25. #include <wtf/StringHasher.h>
  26. namespace WTF {
  27. inline bool HashTraits<String>::isEmptyValue(const String& value)
  28. {
  29. return value.isNull();
  30. }
  31. // The hash() functions on StringHash and CaseFoldingHash do not support
  32. // null strings. get(), contains(), and add() on HashMap<String,..., StringHash>
  33. // cause a null-pointer dereference when passed null strings.
  34. // FIXME: We should really figure out a way to put the computeHash function that's
  35. // currently a member function of StringImpl into this file so we can be a little
  36. // closer to having all the nearly-identical hash functions in one place.
  37. struct StringHash {
  38. static unsigned hash(StringImpl* key) { return key->hash(); }
  39. static inline bool equal(const StringImpl* a, const StringImpl* b)
  40. {
  41. return equalNonNull(a, b);
  42. }
  43. static unsigned hash(const RefPtr<StringImpl>& key) { return key->hash(); }
  44. static bool equal(const RefPtr<StringImpl>& a, const RefPtr<StringImpl>& b)
  45. {
  46. return equal(a.get(), b.get());
  47. }
  48. static unsigned hash(const String& key) { return key.impl()->hash(); }
  49. static bool equal(const String& a, const String& b)
  50. {
  51. return equal(a.impl(), b.impl());
  52. }
  53. static const bool safeToCompareToEmptyOrDeleted = false;
  54. };
  55. class CaseFoldingHash {
  56. public:
  57. template<typename T> static inline UChar foldCase(T ch)
  58. {
  59. return WTF::Unicode::foldCase(ch);
  60. }
  61. static unsigned hash(const UChar* data, unsigned length)
  62. {
  63. return StringHasher::computeHashAndMaskTop8Bits<UChar, foldCase<UChar> >(data, length);
  64. }
  65. static unsigned hash(StringImpl* str)
  66. {
  67. if (str->is8Bit())
  68. return hash(str->characters8(), str->length());
  69. return hash(str->characters16(), str->length());
  70. }
  71. static unsigned hash(const LChar* data, unsigned length)
  72. {
  73. return StringHasher::computeHashAndMaskTop8Bits<LChar, foldCase<LChar> >(data, length);
  74. }
  75. static inline unsigned hash(const char* data, unsigned length)
  76. {
  77. return CaseFoldingHash::hash(reinterpret_cast<const LChar*>(data), length);
  78. }
  79. static inline bool equal(const StringImpl* a, const StringImpl* b)
  80. {
  81. return equalIgnoringCaseNonNull(a, b);
  82. }
  83. static unsigned hash(const RefPtr<StringImpl>& key)
  84. {
  85. return hash(key.get());
  86. }
  87. static bool equal(const RefPtr<StringImpl>& a, const RefPtr<StringImpl>& b)
  88. {
  89. return equal(a.get(), b.get());
  90. }
  91. static unsigned hash(const String& key)
  92. {
  93. return hash(key.impl());
  94. }
  95. static unsigned hash(const AtomicString& key)
  96. {
  97. return hash(key.impl());
  98. }
  99. static bool equal(const String& a, const String& b)
  100. {
  101. return equal(a.impl(), b.impl());
  102. }
  103. static bool equal(const AtomicString& a, const AtomicString& b)
  104. {
  105. return (a == b) || equal(a.impl(), b.impl());
  106. }
  107. static const bool safeToCompareToEmptyOrDeleted = false;
  108. };
  109. // This hash can be used in cases where the key is a hash of a string, but we don't
  110. // want to store the string. It's not really specific to string hashing, but all our
  111. // current uses of it are for strings.
  112. struct AlreadyHashed : IntHash<unsigned> {
  113. static unsigned hash(unsigned key) { return key; }
  114. // To use a hash value as a key for a hash table, we need to eliminate the
  115. // "deleted" value, which is negative one. That could be done by changing
  116. // the string hash function to never generate negative one, but this works
  117. // and is still relatively efficient.
  118. static unsigned avoidDeletedValue(unsigned hash)
  119. {
  120. ASSERT(hash);
  121. unsigned newHash = hash | (!(hash + 1) << 31);
  122. ASSERT(newHash);
  123. ASSERT(newHash != 0xFFFFFFFF);
  124. return newHash;
  125. }
  126. };
  127. }
  128. using WTF::AlreadyHashed;
  129. using WTF::CaseFoldingHash;
  130. using WTF::StringHash;
  131. #endif