Commit aa27a00a authored by Daniel Hosseinian's avatar Daniel Hosseinian Committed by Commit Bot

Support hashing of base::GUID

Implement a custom hasher for base::GUID so it may be used in standard
hash containers.

Bug: 1026195
Change-Id: I4a6315b21543e374b7eb6e8863b0afc3ff57f334
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2533644
Commit-Queue: Daniel Hosseinian <dhoss@chromium.org>
Reviewed-by: default avatarGabriel Charette <gab@chromium.org>
Cr-Commit-Position: refs/heads/master@{#827073}
parent 2d4a29a8
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include <string> #include <string>
#include "base/base_export.h" #include "base/base_export.h"
#include "base/hash/hash.h"
#include "base/strings/string_piece.h" #include "base/strings/string_piece.h"
#include "build/build_config.h" #include "build/build_config.h"
...@@ -82,6 +83,16 @@ class BASE_EXPORT GUID { ...@@ -82,6 +83,16 @@ class BASE_EXPORT GUID {
std::string lowercase_; std::string lowercase_;
}; };
// For runtime usage only. Do not store the result of this hash, as it may
// change in future Chromium revisions.
struct BASE_EXPORT GUIDHash {
size_t operator()(const GUID& guid) const {
// TODO(crbug.com/1026195): Avoid converting to string to take the hash when
// the internal type is migrated to a non-string type.
return FastHash(guid.AsLowercaseString());
}
};
// Stream operator so GUID objects can be used in logging statements. // Stream operator so GUID objects can be used in logging statements.
BASE_EXPORT std::ostream& operator<<(std::ostream& out, const GUID& guid); BASE_EXPORT std::ostream& operator<<(std::ostream& out, const GUID& guid);
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <stdint.h> #include <stdint.h>
#include <limits> #include <limits>
#include <unordered_set>
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "build/build_config.h" #include "build/build_config.h"
...@@ -147,4 +148,20 @@ TEST(GUIDTest, Equality) { ...@@ -147,4 +148,20 @@ TEST(GUIDTest, Equality) {
EXPECT_EQ(GUID(), GUID()); EXPECT_EQ(GUID(), GUID());
} }
TEST(GUIDTest, UnorderedSet) {
std::unordered_set<GUID, GUIDHash> guid_set;
static constexpr char kGUID1[] = "01234567-89ab-cdef-fedc-ba9876543210";
guid_set.insert(GUID::ParseCaseInsensitive(ToLowerASCII(kGUID1)));
EXPECT_EQ(1u, guid_set.size());
guid_set.insert(GUID::ParseCaseInsensitive(ToUpperASCII(kGUID1)));
EXPECT_EQ(1u, guid_set.size());
static constexpr char kGUID2[] = "deadbeef-dead-beef-dead-beefdeadbeef";
guid_set.insert(GUID::ParseCaseInsensitive(ToLowerASCII(kGUID2)));
EXPECT_EQ(2u, guid_set.size());
guid_set.insert(GUID::ParseCaseInsensitive(ToUpperASCII(kGUID2)));
EXPECT_EQ(2u, guid_set.size());
}
} // namespace base } // namespace base
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