Skip to content

Commit d091b8a

Browse files
committed
address review comments
1 parent b2e52cc commit d091b8a

File tree

3 files changed

+21
-25
lines changed

3 files changed

+21
-25
lines changed

google/cloud/spanner/uuid.cc

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,12 @@ namespace spanner {
2424
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
2525
namespace {
2626

27-
constexpr int kMaxUuidNumberOfHexDigits = 32;
28-
2927
// Helper function to parse a single hexadecimal block of a UUID.
3028
// A hexadecimal block is a 16-digit hexadecimal number, which is represented
3129
// as 8 bytes.
3230
StatusOr<std::uint64_t> ParseHexBlock(absl::string_view& str,
3331
absl::string_view original_str) {
32+
constexpr int kMaxUuidNumberOfHexDigits = 32;
3433
constexpr int kMaxUuidBlockLength = 16;
3534
static auto const* char_to_hex = new absl::flat_hash_map<char, std::uint8_t>(
3635
{{'0', 0x00}, {'1', 0x01}, {'2', 0x02}, {'3', 0x03}, {'4', 0x04},
@@ -73,14 +72,6 @@ Uuid::Uuid(absl::uint128 value) : uuid_(value) {}
7372
Uuid::Uuid(std::uint64_t high_bits, std::uint64_t low_bits)
7473
: Uuid(absl::MakeUint128(high_bits, low_bits)) {}
7574

76-
bool operator==(Uuid const& lhs, Uuid const& rhs) {
77-
return lhs.uuid_ == rhs.uuid_;
78-
}
79-
80-
bool operator<(Uuid const& lhs, Uuid const& rhs) {
81-
return lhs.uuid_ < rhs.uuid_;
82-
}
83-
8475
Uuid::operator std::string() const {
8576
constexpr int kUuidStringLen = 36;
8677
constexpr int kChunkLength[] = {8, 4, 4, 4, 12};
@@ -95,7 +86,7 @@ Uuid::operator std::string() const {
9586

9687
std::string output;
9788
output.resize(kUuidStringLen);
98-
char* target = &((output)[output.size() - kUuidStringLen]);
89+
char* target = const_cast<char*>(output.data());
9990
char* const last = &((output)[output.size()]);
10091
auto bits = Uint128High64(uuid_);
10192
int start = 16;
@@ -131,7 +122,7 @@ StatusOr<Uuid> MakeUuid(absl::string_view str) {
131122
}
132123

133124
// Check for leading hyphen after stripping any surrounding braces.
134-
if (str[0] == '-') {
125+
if (absl::StartsWith(str, "-")) {
135126
return internal::InvalidArgumentError(
136127
absl::StrFormat("UUID cannot begin with '-': %s", original_str),
137128
GCP_ERROR_INFO());
@@ -144,8 +135,7 @@ StatusOr<Uuid> MakeUuid(absl::string_view str) {
144135

145136
if (!str.empty()) {
146137
return internal::InvalidArgumentError(
147-
absl::StrFormat("Extra characters (%d) found after parsing UUID: %s",
148-
str.size(), original_str),
138+
absl::StrFormat("Extra characters found after parsing UUID: %s", str),
149139
GCP_ERROR_INFO());
150140
}
151141

google/cloud/spanner/uuid.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ class Uuid {
3838
/// Default construction yields a zero value UUID.
3939
Uuid() = default;
4040

41-
/// Construct a UUID from a packed integer.
41+
/// Construct a UUID from one 128 bit unsigned integer.
4242
explicit Uuid(absl::uint128 value);
4343

44-
/// Construct a UUID from two 64 bit pieces.
44+
/// Construct a UUID from two unsigned 64 bit pieces.
4545
Uuid(std::uint64_t high_bits, std::uint64_t low_bits);
4646

4747
/// @name Regular value type, supporting copy, assign, move.
@@ -55,11 +55,15 @@ class Uuid {
5555
/// @name Relational operators
5656
///
5757
///@{
58-
friend bool operator==(Uuid const& lhs, Uuid const& rhs);
58+
friend bool operator==(Uuid const& lhs, Uuid const& rhs) {
59+
return lhs.uuid_ == rhs.uuid_;
60+
}
5961
friend bool operator!=(Uuid const& lhs, Uuid const& rhs) {
6062
return !(lhs == rhs);
6163
}
62-
friend bool operator<(Uuid const& lhs, Uuid const& rhs);
64+
friend bool operator<(Uuid const& lhs, Uuid const& rhs) {
65+
return lhs.uuid_ < rhs.uuid_;
66+
}
6367
friend bool operator<=(Uuid const& lhs, Uuid const& rhs) {
6468
return !(rhs < lhs);
6569
}
@@ -91,10 +95,9 @@ class Uuid {
9195
* Parses a textual representation a `Uuid` from a string of hexadecimal digits.
9296
* Returns an error if unable to parse the given input.
9397
*
94-
* Acceptable input strings must consist of [0-f] digits with formatting such:
95-
* - Upper, lower, or mixed case.
96-
* - Optional curly bracers around the entire UUID string.
97-
* - Hyphens between any pair of hexadecimal digits are allowed.
98+
* Acceptable input strings must consist of 32 hexadecimal digits: [0-9a-fA-F].
99+
* Optional curly braces are allowed around the entire sequence of digits as are
100+
* hyphens between any pair of hexadecimal digits.
98101
*
99102
* Example acceptable inputs:
100103
* - {0b6ed04c-a16d-fc46-5281-7f9978c13738}

google/cloud/spanner/uuid_test.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,14 @@ INSTANTIATE_TEST_SUITE_P(
118118
MakeUuidTestParam{
119119
"MissingClosingCurlyBrace", "{0b6ed04ca16dfc4652817f9978c13738",
120120
0x0, 0x0,
121-
Status{StatusCode::kInvalidArgument, "UUID missing closing '}':"}},
121+
Status{
122+
StatusCode::kInvalidArgument,
123+
"UUID missing closing '}': {0b6ed04ca16dfc4652817f9978c13738"}},
122124
MakeUuidTestParam{
123125
"MissingOpeningCurlyBrace", "0b6ed04ca16dfc4652817f9978c13738}",
124126
0x0, 0x0,
125127
Status{StatusCode::kInvalidArgument,
126-
"Extra characters (1) found after parsing UUID:"}},
128+
"Extra characters found after parsing UUID: }"}},
127129
MakeUuidTestParam{"StartsWithInvalidHyphen",
128130
"-0b6ed04ca16dfc4652817f9978c13738", 0x0, 0x0,
129131
Status{StatusCode::kInvalidArgument,
@@ -135,7 +137,8 @@ INSTANTIATE_TEST_SUITE_P(
135137
MakeUuidTestParam{"ContainsConsecutiveHyphens",
136138
"0b--6ed04ca16dfc4652817f9978c13738", 0x0, 0x0,
137139
Status{StatusCode::kInvalidArgument,
138-
"UUID cannot contain consecutive hyphens:"}},
140+
"UUID cannot contain consecutive hyphens: "
141+
"0b--6ed04ca16dfc4652817f9978c13738"}},
139142
MakeUuidTestParam{"InsufficientDigits",
140143
"00-00-00-00-00-00-00-00-00-00-00-00-00-00-00", 0x0,
141144
0x0,

0 commit comments

Comments
 (0)