Add UUID::InitializeFromString().

R=mark@chromium.org
TEST=util_test --gtest_filter=UUID.FromString

Review URL: https://codereview.chromium.org/820783004
This commit is contained in:
Robert Sesek 2015-01-02 18:46:10 -05:00
parent 1cdb7c1d04
commit c75dc46b17
3 changed files with 94 additions and 0 deletions

View File

@ -14,6 +14,8 @@
#include "util/misc/uuid.h"
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include "base/basictypes.h"
@ -52,6 +54,35 @@ void UUID::InitializeFromBytes(const uint8_t* bytes) {
data_3 = base::NetToHost16(data_3);
}
bool UUID::InitializeFromString(const base::StringPiece& string) {
if (string.length() != 36)
return false;
UUID temp;
const char kScanFormat[] =
"%08" SCNx32 "-%04" SCNx16 "-%04" SCNx16
"-%02" SCNx8 "%02" SCNx8
"-%02" SCNx8 "%02" SCNx8 "%02" SCNx8 "%02" SCNx8 "%02" SCNx8 "%02" SCNx8;
int rv = sscanf(string.data(),
kScanFormat,
&temp.data_1,
&temp.data_2,
&temp.data_3,
&temp.data_4[0],
&temp.data_4[1],
&temp.data_5[0],
&temp.data_5[1],
&temp.data_5[2],
&temp.data_5[3],
&temp.data_5[4],
&temp.data_5[5]);
if (rv != 11)
return false;
*this = temp;
return true;
}
std::string UUID::ToString() const {
return base::StringPrintf("%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
data_1,

View File

@ -19,6 +19,8 @@
#include <string>
#include "base/strings/string_piece.h"
namespace crashpad {
//! \brief A universally unique identifier (%UUID).
@ -49,6 +51,16 @@ struct UUID {
//! %UUID.
void InitializeFromBytes(const uint8_t* bytes);
//! \brief Initializes the %UUID from a RFC 4122 §3 formatted string.
//!
//! \param[in] string A string of the form
//! `"00112233-4455-6677-8899-aabbccddeeff"`.
//!
//! \return `true` if the string was formatted correctly and the object has
//! been initialized with the data. `false` if the string could not be
//! parsed, with the object state untouched.
bool InitializeFromString(const base::StringPiece& string);
//! \brief Formats the %UUID per RFC 4122 §3.
//!
//! \return A string of the form `"00112233-4455-6677-8899-aabbccddeeff"`.

View File

@ -19,6 +19,7 @@
#include <string>
#include "base/basictypes.h"
#include "base/strings/stringprintf.h"
#include "gtest/gtest.h"
namespace crashpad {
@ -152,6 +153,56 @@ TEST(UUID, UUID) {
EXPECT_EQ("45454545-4545-4545-4545-454545454545", uuid.ToString());
}
TEST(UUID, FromString) {
const struct TestCase {
const char* uuid_string;
bool success;
} kCases[] = {
// Valid:
{"c6849cb5-fe14-4a79-8978-9ae6034c521d", true},
{"00000000-0000-0000-0000-000000000000", true},
{"ffffffff-ffff-ffff-ffff-ffffffffffff", true},
// Outside HEX range:
{"7318z10b-c453-4cef-9dc8-015655cb4bbc", false},
{"7318a10b-c453-4cef-9dz8-015655cb4bbc", false},
// Incomplete:
{"15655cb4-", false},
{"7318f10b-c453-4cef-9dc8-015655cb4bb", false},
{"318f10b-c453-4cef-9dc8-015655cb4bb2", false},
{"7318f10b-c453-4ef-9dc8-015655cb4bb2", false},
{"", false},
{"abcd", false},
// Trailing data:
{"6d247a34-53d5-40ec-a90d-d8dea9e94cc01", false}
};
const std::string empty_uuid = UUID().ToString();
for (size_t index = 0; index < arraysize(kCases); ++index) {
const TestCase& test_case = kCases[index];
SCOPED_TRACE(
base::StringPrintf("index %zu: %s", index, test_case.uuid_string));
UUID uuid;
EXPECT_EQ(test_case.success,
uuid.InitializeFromString(test_case.uuid_string));
if (test_case.success) {
EXPECT_EQ(test_case.uuid_string, uuid.ToString());
} else {
EXPECT_EQ(empty_uuid, uuid.ToString());
}
}
// Test for case insensitivty.
UUID uuid;
uuid.InitializeFromString("F32E5BDC-2681-4C73-A4E6-911FFD89B846");
EXPECT_EQ("f32e5bdc-2681-4c73-a4e6-911ffd89b846", uuid.ToString());
// Mixed case.
uuid.InitializeFromString("5762C15D-50b5-4171-a2e9-7429C9EC6CAB");
EXPECT_EQ("5762c15d-50b5-4171-a2e9-7429c9ec6cab", uuid.ToString());
}
} // namespace
} // namespace test
} // namespace crashpad