This is an automated email from the ASF dual-hosted git repository.

dragon pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new 3d45648  Fix string_view hash function
3d45648 is described below

commit 3d4564835a21772c2c0202edee4fbe02f709525d
Author: Jason Kenny <dragon...@live.com>
AuthorDate: Wed Feb 28 11:36:27 2018 -0600

    Fix string_view hash function
    
    Add a unit test
---
 lib/ts/string_view.h                  | 8 +++++++-
 lib/ts/unit-tests/test_string_view.cc | 9 +++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/lib/ts/string_view.h b/lib/ts/string_view.h
index 89e3ff6..9c88b30 100644
--- a/lib/ts/string_view.h
+++ b/lib/ts/string_view.h
@@ -1220,7 +1220,13 @@ template <class _Type, class _Traits> struct 
hash<ts::basic_string_view<_Type, _
   size_t
   operator()(string_type const &x) const
   {
-    return hash<typename string_type::const_pointer>()(x.data());
+// not what I would normally do.. but better than making a custom hash 
function at the moment.
+// This should also mean we have some consistent behavior with std code
+#if defined(__linux__)
+    return std::_Hash_impl::hash(x.data(), x.length() * sizeof(typename 
string_type::value_type));
+#elif defined(__FreeBSD__)
+    return __do_string_hash(x.data(), x.data() + x.size());
+#endif
   }
 };
 }
diff --git a/lib/ts/unit-tests/test_string_view.cc 
b/lib/ts/unit-tests/test_string_view.cc
index c5f9619..f5b240c 100644
--- a/lib/ts/unit-tests/test_string_view.cc
+++ b/lib/ts/unit-tests/test_string_view.cc
@@ -19,6 +19,7 @@
 #include "catch.hpp"
 
 #include "string_view.h"
+#include <functional>
 #include <iostream>
 #include <string>
 #include <vector>
@@ -538,4 +539,12 @@ TEST_CASE("Find", "[string_view] [find]")
     REQUIRE(sv.find_last_not_of("abcdxyz", 1, 5) == npos);
     REQUIRE(sv.find_last_not_of("aaaaaaaa", 1, 5) == 1);
   }
+
+  SECTION("hash")
+  {
+    ts::string_view sv1("hello");
+    ts::string_view sv2("hello world", 5);
+    std::hash<ts::string_view> h;
+    REQUIRE(h(sv1) == h(sv2));
+  }
 }

-- 
To stop receiving notification emails like this one, please contact
dra...@apache.org.

Reply via email to