Title: [136695] trunk/Source/WTF
Revision
136695
Author
[email protected]
Date
2012-12-05 09:06:26 -0800 (Wed, 05 Dec 2012)

Log Message

Extend StringHasher to take a stream of characters
https://bugs.webkit.org/show_bug.cgi?id=104076
<rdar://problem/12811887>

Reviewed by Darin Adler.

We essentially move computeHash() to a member function of StringHasher. This allows clients of the
hasher to append strings to the general hash, without having to first concatenate the strings.

* wtf/StringHasher.h:
(WTF::StringHasher::addCharacters): Same as computeHash(), except also take into account the possibility
that there is a pending character to hash. If this is the case, we align the word boundary by consuming the
first character in data, adjust the length, move the data pointer forward one, and update the hasher's
pending character state. Also add an early return so that we are guaranteed at least one character in case
we run into this situation.
(WTF::StringHasher::computeHash): Refactor into addCharacters().

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (136694 => 136695)


--- trunk/Source/WTF/ChangeLog	2012-12-05 16:48:22 UTC (rev 136694)
+++ trunk/Source/WTF/ChangeLog	2012-12-05 17:06:26 UTC (rev 136695)
@@ -1,3 +1,22 @@
+2012-12-04  Jon Lee  <[email protected]>
+
+        Extend StringHasher to take a stream of characters
+        https://bugs.webkit.org/show_bug.cgi?id=104076
+        <rdar://problem/12811887>
+
+        Reviewed by Darin Adler.
+
+        We essentially move computeHash() to a member function of StringHasher. This allows clients of the
+        hasher to append strings to the general hash, without having to first concatenate the strings.
+
+        * wtf/StringHasher.h:
+        (WTF::StringHasher::addCharacters): Same as computeHash(), except also take into account the possibility
+        that there is a pending character to hash. If this is the case, we align the word boundary by consuming the
+        first character in data, adjust the length, move the data pointer forward one, and update the hasher's
+        pending character state. Also add an early return so that we are guaranteed at least one character in case
+        we run into this situation.
+        (WTF::StringHasher::computeHash): Refactor into addCharacters().
+
 2012-12-04  Yury Semikhatsky  <[email protected]>
 
         Web Inspector: simplify reportMemoryUsage signature

Modified: trunk/Source/WTF/wtf/StringHasher.h (136694 => 136695)


--- trunk/Source/WTF/wtf/StringHasher.h	2012-12-05 16:48:22 UTC (rev 136694)
+++ trunk/Source/WTF/wtf/StringHasher.h	2012-12-05 17:06:26 UTC (rev 136695)
@@ -63,6 +63,29 @@
         m_hasPendingCharacter = true;
     }
 
+    template<typename T, UChar Converter(T)> inline void addCharacters(const T* data, unsigned length)
+    {
+        if (!length)
+            return;
+
+        if (m_hasPendingCharacter) {
+            addCharactersToHash(m_pendingCharacter, Converter(*data++));
+            m_hasPendingCharacter = false;
+            --length;
+        }
+
+        bool rem = length & 1;
+        length >>= 1;
+
+        while (length--) {
+            addCharactersToHash(Converter(data[0]), Converter(data[1]));
+            data += 2;
+        }
+
+        if (rem)
+            addCharacter(Converter(*data));
+    }
+
     inline unsigned hashWithTop8BitsMasked() const
     {
         unsigned result = avalancheBits();
@@ -145,17 +168,7 @@
     template<typename T, UChar Converter(T)> static inline unsigned computeHash(const T* data, unsigned length)
     {
         StringHasher hasher;
-        bool rem = length & 1;
-        length >>= 1;
-
-        while (length--) {
-            hasher.addCharacters(Converter(data[0]), Converter(data[1]));
-            data += 2;
-        }
-
-        if (rem)
-            hasher.addCharacter(Converter(*data));
-
+        hasher.addCharacters<T, Converter>(data, length);
         return hasher.hash();
     }
 
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to