summaryrefslogtreecommitdiffstats
path: root/base/strings
diff options
context:
space:
mode:
authordavidben <davidben@chromium.org>2016-01-21 17:41:41 -0800
committerCommit bot <commit-bot@chromium.org>2016-01-22 01:43:27 +0000
commit411d3f7a3a18335713a46d9022ce25e018a19e47 (patch)
tree72cd74ea04f27cc8b01f7153f43e24ae8d48b512 /base/strings
parent58be7eea6963f63c4395e97a8a8099916f738a61 (diff)
downloadchromium_src-411d3f7a3a18335713a46d9022ce25e018a19e47.zip
chromium_src-411d3f7a3a18335713a46d9022ce25e018a19e47.tar.gz
chromium_src-411d3f7a3a18335713a46d9022ce25e018a19e47.tar.bz2
Allow std::unordered_*.
This is a reland of https://codereview.chromium.org/1502373009 with some fixes for components/metrics/leak_detector allocator type mismatches. Original issue's description: > Allow std::unordered_*. > > base::hash_* is, as a transition step, implemented in terms of > std::unordered_*. Later commits will convert existing uses. > > Also fix a host of IWYU problems that arose from this CL. > > (NOPRESUBMIT because the wstring presubmit check is overzealous > and complains about the reference to wstring in the comment.) > > Committed: https://crrev.com/3f37f7f1459e7b5a452c0e433493e0a6e9649ca7 > Cr-Commit-Position: refs/heads/master@{#370553} BUG=576864 TBR=derat@chromium.org,danakj@chromium.org,dalecurtis@chromium.org,jbauman@chromium.org,blundell@chromium.org NOPRESUBMIT=true CQ_INCLUDE_TRYBOTS=tryserver.blink:linux_blink_rel Review URL: https://codereview.chromium.org/1615713003 Cr-Commit-Position: refs/heads/master@{#370867}
Diffstat (limited to 'base/strings')
-rw-r--r--base/strings/string16.h17
-rw-r--r--base/strings/string16_unittest.cc14
-rw-r--r--base/strings/string_piece.h6
3 files changed, 31 insertions, 6 deletions
diff --git a/base/strings/string16.h b/base/strings/string16.h
index e47669c..82dd0fa 100644
--- a/base/strings/string16.h
+++ b/base/strings/string16.h
@@ -29,6 +29,8 @@
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
+
+#include <functional>
#include <string>
#include "base/base_export.h"
@@ -182,6 +184,21 @@ BASE_EXPORT extern void PrintTo(const string16& str, std::ostream* out);
extern template
class BASE_EXPORT std::basic_string<base::char16, base::string16_char_traits>;
+// Specialize std::hash for base::string16. Although the style guide forbids
+// this in general, it is necessary for consistency with WCHAR_T_IS_UTF16
+// platforms, where base::string16 is a type alias for std::wstring.
+namespace std {
+template <>
+struct hash<base::string16> {
+ std::size_t operator()(const base::string16& s) const {
+ std::size_t result = 0;
+ for (base::char16 c : s)
+ result = (result * 131) + c;
+ return result;
+ }
+};
+} // namespace std
+
#endif // WCHAR_T_IS_UTF32
#endif // BASE_STRINGS_STRING16_H_
diff --git a/base/strings/string16_unittest.cc b/base/strings/string16_unittest.cc
index 4e58218..0d2ca80 100644
--- a/base/strings/string16_unittest.cc
+++ b/base/strings/string16_unittest.cc
@@ -3,6 +3,7 @@
// found in the LICENSE file.
#include <sstream>
+#include <unordered_set>
#include "base/strings/string16.h"
@@ -11,8 +12,6 @@
namespace base {
-#if defined(WCHAR_T_IS_UTF32)
-
// We define a custom operator<< for string16 so we can use it with logging.
// This tests that conversion.
TEST(String16Test, OutputStream) {
@@ -53,6 +52,15 @@ TEST(String16Test, OutputStream) {
}
}
-#endif
+TEST(String16Test, Hash) {
+ string16 str1 = ASCIIToUTF16("hello");
+ string16 str2 = ASCIIToUTF16("world");
+
+ std::unordered_set<string16> set;
+
+ set.insert(str1);
+ EXPECT_EQ(1u, set.count(str1));
+ EXPECT_EQ(0u, set.count(str2));
+}
} // namespace base
diff --git a/base/strings/string_piece.h b/base/strings/string_piece.h
index 31e7596..92634b9 100644
--- a/base/strings/string_piece.h
+++ b/base/strings/string_piece.h
@@ -439,9 +439,9 @@ BASE_EXPORT std::ostream& operator<<(std::ostream& o,
// We provide appropriate hash functions so StringPiece and StringPiece16 can
// be used as keys in hash sets and maps.
-// This hash function is copied from base/containers/hash_tables.h. We don't
-// use the ones already defined for string and string16 directly because it
-// would require the string constructors to be called, which we don't want.
+// This hash function is copied from base/strings/string16.h. We don't use the
+// ones already defined for string and string16 directly because it would
+// require the string constructors to be called, which we don't want.
#define HASH_STRING_PIECE(StringPieceType, string_piece) \
std::size_t result = 0; \
for (StringPieceType::const_iterator i = string_piece.begin(); \