From c1be26e5844b71e2e2ce197fa71b88cd36376761 Mon Sep 17 00:00:00 2001 From: Giuseppe Ottaviano Date: Fri, 11 Sep 2015 17:37:20 -0700 Subject: [PATCH] Add support for std::string in folly::Hash MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Reviewed By: @​liviu Differential Revision: D2436705 --- folly/Hash.h | 6 ++++++ folly/test/HashTest.cpp | 8 +++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/folly/Hash.h b/folly/Hash.h index f40f4ec2..07eb5571 100644 --- a/folly/Hash.h +++ b/folly/Hash.h @@ -375,6 +375,12 @@ template<> struct hasher { } }; +template<> struct hasher { + size_t operator()(const std::string& key) const { + return hash::SpookyHashV2::Hash64(key.data(), key.size(), 0); + } +}; + template struct hasher::value, void>::type> { size_t operator()(T key) const { diff --git a/folly/test/HashTest.cpp b/folly/test/HashTest.cpp index bbae4f6f..de12b1e0 100644 --- a/folly/test/HashTest.cpp +++ b/folly/test/HashTest.cpp @@ -303,7 +303,7 @@ TEST(Hash, std_tuple_different_hash) { std::hash()(t3)); } -TEST(Range, Hash) { +TEST(Hash, Strings) { using namespace folly; StringPiece a1 = "10050517", b1 = "51107032", @@ -329,4 +329,10 @@ TEST(Range, Hash) { EXPECT_NE(h2(w1), h2(w2)); EXPECT_NE(h2(w1), h2(w3)); EXPECT_NE(h2(w2), h2(w4)); + + // Check compatibility with std::string. + EXPECT_EQ(h2(a1), h2(a1.str())); + EXPECT_EQ(h2(a2), h2(a2.str())); + EXPECT_EQ(h2(a3), h2(a3.str())); + EXPECT_EQ(h2(a4), h2(a4.str())); } -- 2.34.1