/*
- * 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
#include "SourceCode.h"
#include <wtf/SHA1.h>
+#include <wtf/SixCharacterHash.h>
namespace JSC {
-#define TABLE ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
-
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;
- 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);
+
+ // Ensure that 0 corresponds to the hash not having been computed.
+ if (!m_hash)
+ m_hash = 1;
}
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
- CodeBlockHash recompute(buffer);
+ CodeBlockHash recompute(buffer.data());
ASSERT(recompute == *this);
#endif // !ASSERT_DISABLED
- out.print(buffer);
+ out.print(buffer.data());
}
} // namespace JSC