mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 07:36:10 -04:00
Added a new Uuid constructor from UUID string
PiperOrigin-RevId: 628288959
This commit is contained in:
committed by
Copybara-Service
parent
f72ceedfd2
commit
c9c8544669
@@ -14,11 +14,20 @@
|
||||
|
||||
#include "internal/platform/uuid.h"
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <iomanip>
|
||||
#include <ios>
|
||||
#include <optional>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/strings/escaping.h"
|
||||
#include "absl/strings/numbers.h"
|
||||
#include "absl/strings/str_cat.h"
|
||||
#include "absl/strings/str_split.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "internal/platform/implementation/crypto.h"
|
||||
|
||||
namespace nearby {
|
||||
@@ -32,6 +41,49 @@ std::ostream& write_hex(std::ostream& os, absl::string_view data) {
|
||||
}
|
||||
} // namespace
|
||||
|
||||
// Based on the Java implementation
|
||||
// http://androidxref.com/8.0.0_r4/xref/libcore/ojluni/src/main/java/java/util/UUID.java#191
|
||||
std::optional<Uuid> Uuid::FromString(absl::string_view data) {
|
||||
std::vector<std::string> components = absl::StrSplit(data, '-');
|
||||
if (components.size() != 5) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
for (std::string& component : components) {
|
||||
component = absl::StrCat("0x", component);
|
||||
}
|
||||
|
||||
int64_t most_sig_bits = 0L;
|
||||
int64_t least_sig_bits = 0L;
|
||||
int64_t temp;
|
||||
if (!absl::SimpleHexAtoi(components[0], &temp)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
most_sig_bits = temp;
|
||||
most_sig_bits <<= 16;
|
||||
if (!absl::SimpleHexAtoi(components[1], &temp)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
most_sig_bits |= temp;
|
||||
most_sig_bits <<= 16;
|
||||
if (!absl::SimpleHexAtoi(components[2], &temp)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
most_sig_bits |= temp;
|
||||
|
||||
if (!absl::SimpleHexAtoi(components[3], &temp)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
least_sig_bits = temp;
|
||||
least_sig_bits <<= 48;
|
||||
if (!absl::SimpleHexAtoi(components[4], &temp)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
least_sig_bits |= temp;
|
||||
|
||||
return Uuid(most_sig_bits, least_sig_bits);
|
||||
}
|
||||
|
||||
Uuid::Uuid(absl::string_view data) {
|
||||
// Based on the Java counterpart at
|
||||
// http://androidxref.com/8.0.0_r4/xref/libcore/ojluni/src/main/java/java/util/UUID.java#162.
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#include "absl/strings/string_view.h"
|
||||
@@ -32,6 +33,10 @@ class Uuid final {
|
||||
public:
|
||||
Uuid() = default;
|
||||
|
||||
// Constructs a UUID from the canonical string format as
|
||||
// xxxxxABCD-xxxx-xxxx-xxxxxxxxxxxxxx
|
||||
static std::optional<Uuid> FromString(absl::string_view data);
|
||||
|
||||
// Constructs a type 3 (name based) UUID based on the input string.
|
||||
explicit Uuid(absl::string_view data);
|
||||
// Constructs a new UUID using the specified most_sig_bits for the most
|
||||
|
||||
@@ -14,11 +14,15 @@
|
||||
|
||||
#include "internal/platform/uuid.h"
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "protobuf-matchers/protocol-buffer-matchers.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "internal/platform/crypto.h"
|
||||
#include "internal/platform/logging.h"
|
||||
|
||||
@@ -131,5 +135,13 @@ TEST(UuidTest, OperatorGreaterThan) {
|
||||
EXPECT_TRUE(a < b);
|
||||
}
|
||||
|
||||
TEST(UuidTest, ConstructUuidFromString) {
|
||||
std::optional<Uuid> a =
|
||||
Uuid::FromString("12345678-1234-1234-1234-123456789012");
|
||||
|
||||
ASSERT_TRUE(a.has_value());
|
||||
EXPECT_EQ(std::string(*a), "12345678-1234-1234-1234-123456789012");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace nearby
|
||||
|
||||
Reference in New Issue
Block a user