]> git.saurik.com Git - apple/javascriptcore.git/blobdiff - bytecode/CodeBlockHash.cpp
JavaScriptCore-7600.1.4.11.8.tar.gz
[apple/javascriptcore.git] / bytecode / CodeBlockHash.cpp
index 7c890cc88092c3f59c7d58ee7179d90ca40aefd5..87c092f64a891364261180e652a947dfe3de96e5 100644 (file)
@@ -1,5 +1,5 @@
 /*
 /*
- * Copyright (C) 2012 Apple Inc. All rights reserved.
+ * Copyright (C) 2012, 2013 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
 
 #include "SourceCode.h"
 #include <wtf/SHA1.h>
 
 #include "SourceCode.h"
 #include <wtf/SHA1.h>
+#include <wtf/SixCharacterHash.h>
 
 namespace JSC {
 
 
 namespace JSC {
 
-#define TABLE ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
-
 CodeBlockHash::CodeBlockHash(const char* string)
 CodeBlockHash::CodeBlockHash(const char* string)
-    : m_hash(0)
+    : m_hash(sixCharacterHashStringToInteger(string))
 {
 {
-    RELEASE_ASSERT(strlen(string) == 6);
-    
-    for (unsigned i = 0; i < 6; ++i) {
-        m_hash *= 62;
-        unsigned c = string[i];
-        if (c >= 'A' && c <= 'Z') {
-            m_hash += c - 'A';
-            continue;
-        }
-        if (c >= 'a' && c <= 'z') {
-            m_hash += c - 'a' + 26;
-            continue;
-        }
-        ASSERT(c >= '0' && c <= '9');
-        m_hash += c - '0' + 26 * 2;
-    }
 }
 
 CodeBlockHash::CodeBlockHash(const SourceCode& sourceCode, CodeSpecializationKind kind)
     : m_hash(0)
 {
     SHA1 sha1;
 }
 
 CodeBlockHash::CodeBlockHash(const SourceCode& sourceCode, CodeSpecializationKind kind)
     : m_hash(0)
 {
     SHA1 sha1;
-    sha1.addBytes(sourceCode.toString().utf8());
-    Vector<uint8_t, 20> digest;
+    sha1.addBytes(sourceCode.toUTF8());
+    SHA1::Digest digest;
     sha1.computeHash(digest);
     m_hash += digest[0] | (digest[1] << 8) | (digest[2] << 16) | (digest[3] << 24);
     m_hash ^= static_cast<unsigned>(kind);
     sha1.computeHash(digest);
     m_hash += digest[0] | (digest[1] << 8) | (digest[2] << 16) | (digest[3] << 24);
     m_hash ^= static_cast<unsigned>(kind);
+    
+    // Ensure that 0 corresponds to the hash not having been computed.
+    if (!m_hash)
+        m_hash = 1;
 }
 
 void CodeBlockHash::dump(PrintStream& out) const
 {
 }
 
 void CodeBlockHash::dump(PrintStream& out) const
 {
-    ASSERT(strlen(TABLE) == 62);
-    
-    char buffer[7];
-    unsigned accumulator = m_hash;
-    for (unsigned i = 6; i--;) {
-        buffer[i] = TABLE[accumulator % 62];
-        accumulator /= 62;
-    }
-    buffer[6] = 0;
+    std::array<char, 7> buffer = integerToSixCharacterHashString(m_hash);
     
 #if !ASSERT_DISABLED
     
 #if !ASSERT_DISABLED
-    CodeBlockHash recompute(buffer);
+    CodeBlockHash recompute(buffer.data());
     ASSERT(recompute == *this);
 #endif // !ASSERT_DISABLED
     
     ASSERT(recompute == *this);
 #endif // !ASSERT_DISABLED
     
-    out.print(buffer);
+    out.print(buffer.data());
 }
 
 } // namespace JSC
 }
 
 } // namespace JSC