Commit 74300ac7 authored by miloyip's avatar miloyip

Add Hasher::IsValid()

parent c040d26c
...@@ -81,7 +81,7 @@ class Hasher { ...@@ -81,7 +81,7 @@ class Hasher {
public: public:
typedef typename ValueType::Ch Ch; typedef typename ValueType::Ch Ch;
Hasher() : stack_(0, kDefaultSize) {} Hasher(Allocator* allocator = 0) : stack_(allocator, kDefaultSize) {}
bool Null() { return WriteType(kNullType); } bool Null() { return WriteType(kNullType); }
bool Bool(bool b) { return WriteType(b ? kTrueType : kFalseType); } bool Bool(bool b) { return WriteType(b ? kTrueType : kFalseType); }
...@@ -96,10 +96,12 @@ public: ...@@ -96,10 +96,12 @@ public:
n.d = d; n.d = d;
return WriteNumber(n); return WriteNumber(n);
} }
bool String(const Ch* str, SizeType len, bool) { bool String(const Ch* str, SizeType len, bool) {
WriteBuffer(kStringType, str, len * sizeof(Ch)); WriteBuffer(kStringType, str, len * sizeof(Ch));
return true; return true;
} }
bool StartObject() { return true; } bool StartObject() { return true; }
bool Key(const Ch* str, SizeType len, bool copy) { return String(str, len, copy); } bool Key(const Ch* str, SizeType len, bool copy) { return String(str, len, copy); }
bool EndObject(SizeType memberCount) { bool EndObject(SizeType memberCount) {
...@@ -110,6 +112,7 @@ public: ...@@ -110,6 +112,7 @@ public:
*stack_.template Push<uint64_t>() = h; *stack_.template Push<uint64_t>() = h;
return true; return true;
} }
bool StartArray() { return true; } bool StartArray() { return true; }
bool EndArray(SizeType elementCount) { bool EndArray(SizeType elementCount) {
uint64_t h = Hash(0, kArrayType); uint64_t h = Hash(0, kArrayType);
...@@ -120,8 +123,10 @@ public: ...@@ -120,8 +123,10 @@ public:
return true; return true;
} }
bool IsValid() const { return stack_.GetSize() == sizeof(uint64_t); }
uint64_t GetHashCode() const { uint64_t GetHashCode() const {
RAPIDJSON_ASSERT(stack_.GetSize() == sizeof(uint64_t)); RAPIDJSON_ASSERT(IsValid());
return *stack_.template Top<uint64_t>(); return *stack_.template Top<uint64_t>();
} }
...@@ -135,9 +140,11 @@ private: ...@@ -135,9 +140,11 @@ private:
double d; double d;
}; };
bool WriteType(unsigned char type) { WriteBuffer(type, 0, 0); return true; } bool WriteType(Type type) { return WriteBuffer(type, 0, 0); }
bool WriteNumber(const Number& n) { WriteBuffer(kNumberType, &n, sizeof(n)); return true; }
bool WriteBuffer(unsigned char type, const void* data, size_t len) { bool WriteNumber(const Number& n) { return WriteBuffer(kNumberType, &n, sizeof(n)); }
bool WriteBuffer(Type type, const void* data, size_t len) {
// FNV-1a from http://isthe.com/chongo/tech/comp/fnv/ // FNV-1a from http://isthe.com/chongo/tech/comp/fnv/
uint64_t h = Hash(RAPIDJSON_UINT64_C2(0x84222325, 0xcbf29ce4), type); uint64_t h = Hash(RAPIDJSON_UINT64_C2(0x84222325, 0xcbf29ce4), type);
const unsigned char* d = static_cast<const unsigned char*>(data); const unsigned char* d = static_cast<const unsigned char*>(data);
......
...@@ -27,6 +27,8 @@ using namespace rapidjson; ...@@ -27,6 +27,8 @@ using namespace rapidjson;
internal::Hasher<Value, CrtAllocator> h1, h2;\ internal::Hasher<Value, CrtAllocator> h1, h2;\
d1.Accept(h1);\ d1.Accept(h1);\
d2.Accept(h2);\ d2.Accept(h2);\
ASSERT_TRUE(h1.IsValid());\
ASSERT_TRUE(h2.IsValid());\
/*printf("%s: 0x%016llx\n%s: 0x%016llx\n\n", json1, h1.GetHashCode(), json2, h2.GetHashCode());*/\ /*printf("%s: 0x%016llx\n%s: 0x%016llx\n\n", json1, h1.GetHashCode(), json2, h2.GetHashCode());*/\
EXPECT_TRUE(expected == (h1.GetHashCode() == h2.GetHashCode()));\ EXPECT_TRUE(expected == (h1.GetHashCode() == h2.GetHashCode()));\
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment