Commit 700e786e by Abseil Team Committed by Copybara-Service

Hash support for std::wstring_view/u16string_view/u32string_view

PiperOrigin-RevId: 519200954
Change-Id: I349023cacab0ac4cbefb8505efd29a5eda1e9067
parent 7b9f660f
...@@ -113,7 +113,9 @@ ABSL_NAMESPACE_BEGIN ...@@ -113,7 +113,9 @@ ABSL_NAMESPACE_BEGIN
// * std::string (as well as any instance of std::basic_string that // * std::string (as well as any instance of std::basic_string that
// uses one of {char, wchar_t, char16_t, char32_t} and its associated // uses one of {char, wchar_t, char16_t, char32_t} and its associated
// std::char_traits) // std::char_traits)
// * std::string_view // * std::string_view (as well as any instance of std::basic_string_view
// that uses one of {char, wchar_t, char16_t, char32_t} and its associated
// std::char_traits)
// * All the standard sequence containers (provided the elements are hashable) // * All the standard sequence containers (provided the elements are hashable)
// * All the standard associative containers (provided the elements are // * All the standard associative containers (provided the elements are
// hashable) // hashable)
......
...@@ -53,6 +53,10 @@ ...@@ -53,6 +53,10 @@
#include "absl/numeric/int128.h" #include "absl/numeric/int128.h"
#include "absl/strings/cord_test_helpers.h" #include "absl/strings/cord_test_helpers.h"
#ifdef ABSL_HAVE_STD_STRING_VIEW
#include <string_view>
#endif
namespace { namespace {
// Utility wrapper of T for the purposes of testing the `AbslHash` type erasure // Utility wrapper of T for the purposes of testing the `AbslHash` type erasure
...@@ -488,6 +492,47 @@ TEST(HashValueTest, U32String) { ...@@ -488,6 +492,47 @@ TEST(HashValueTest, U32String) {
std::u32string(U"Iñtërnâtiônàlizætiøn")))); std::u32string(U"Iñtërnâtiônàlizætiøn"))));
} }
TEST(HashValueTest, WStringView) {
#ifndef ABSL_HAVE_STD_STRING_VIEW
GTEST_SKIP();
#else
EXPECT_TRUE((is_hashable<std::wstring_view>::value));
EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
std::wstring_view(), std::wstring_view(L"ABC"), std::wstring_view(L"ABC"),
std::wstring_view(L"Some other different string_view"),
std::wstring_view(L"Iñtërnâtiônàlizætiøn"))));
#endif
}
TEST(HashValueTest, U16StringView) {
#ifndef ABSL_HAVE_STD_STRING_VIEW
GTEST_SKIP();
#else
EXPECT_TRUE((is_hashable<std::u16string_view>::value));
EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
std::make_tuple(std::u16string_view(), std::u16string_view(u"ABC"),
std::u16string_view(u"ABC"),
std::u16string_view(u"Some other different string_view"),
std::u16string_view(u"Iñtërnâtiônàlizætiøn"))));
#endif
}
TEST(HashValueTest, U32StringView) {
#ifndef ABSL_HAVE_STD_STRING_VIEW
GTEST_SKIP();
#else
EXPECT_TRUE((is_hashable<std::u32string_view>::value));
EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
std::make_tuple(std::u32string_view(), std::u32string_view(U"ABC"),
std::u32string_view(U"ABC"),
std::u32string_view(U"Some other different string_view"),
std::u32string_view(U"Iñtërnâtiônàlizætiøn"))));
#endif
}
TEST(HashValueTest, StdArray) { TEST(HashValueTest, StdArray) {
EXPECT_TRUE((is_hashable<std::array<int, 3>>::value)); EXPECT_TRUE((is_hashable<std::array<int, 3>>::value));
......
...@@ -56,6 +56,10 @@ ...@@ -56,6 +56,10 @@
#include "absl/types/variant.h" #include "absl/types/variant.h"
#include "absl/utility/utility.h" #include "absl/utility/utility.h"
#ifdef ABSL_HAVE_STD_STRING_VIEW
#include <string_view>
#endif
namespace absl { namespace absl {
ABSL_NAMESPACE_BEGIN ABSL_NAMESPACE_BEGIN
...@@ -518,7 +522,8 @@ H AbslHashValue(H hash_state, const std::shared_ptr<T>& ptr) { ...@@ -518,7 +522,8 @@ H AbslHashValue(H hash_state, const std::shared_ptr<T>& ptr) {
// - `absl::Cord` // - `absl::Cord`
// - `std::string` (and std::basic_string<T, std::char_traits<T>, A> for // - `std::string` (and std::basic_string<T, std::char_traits<T>, A> for
// any allocator A and any T in {char, wchar_t, char16_t, char32_t}) // any allocator A and any T in {char, wchar_t, char16_t, char32_t})
// - `absl::string_view` and `std::string_view` // - `absl::string_view`, `std::string_view`, `std::wstring_view`,
// `std::u16string_view`, and `std::u32_string_view`.
// //
// For simplicity, we currently support only strings built on `char`, `wchar_t`, // For simplicity, we currently support only strings built on `char`, `wchar_t`,
// `char16_t`, or `char32_t`. This support may be broadened, if necessary, but // `char16_t`, or `char32_t`. This support may be broadened, if necessary, but
...@@ -544,6 +549,21 @@ H AbslHashValue( ...@@ -544,6 +549,21 @@ H AbslHashValue(
str.size()); str.size());
} }
#ifdef ABSL_HAVE_STD_STRING_VIEW
// Support std::wstring_view, std::u16string_view and std::u32string_view.
template <typename Char, typename H,
typename = absl::enable_if_t<std::is_same<Char, wchar_t>::value ||
std::is_same<Char, char16_t>::value ||
std::is_same<Char, char32_t>::value>>
H AbslHashValue(H hash_state, std::basic_string_view<Char> str) {
return H::combine(
H::combine_contiguous(std::move(hash_state), str.data(), str.size()),
str.size());
}
#endif // ABSL_HAVE_STD_STRING_VIEW
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// AbslHashValue for Sequence Containers // AbslHashValue for Sequence Containers
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
......
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