Index: util/misc/uuid.cc |
diff --git a/util/misc/uuid.cc b/util/misc/uuid.cc |
index 939c0b8e985daa24e0fab5c4f1c1d1100e218bd2..5f6bf14856191fdb50f84ede3f8fe1fb054214ee 100644 |
--- a/util/misc/uuid.cc |
+++ b/util/misc/uuid.cc |
@@ -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, |