1 //===-- UUID.cpp ------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Utility/UUID.h"
12 // Other libraries and framework includes
14 #include "lldb/Utility/Stream.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Support/Format.h"
23 using namespace lldb_private;
25 // Whether to put a separator after count uuid bytes.
26 // For the first 16 bytes we follow the traditional UUID format. After that, we
27 // simply put a dash after every 6 bytes.
28 static inline bool separate(size_t count) {
30 return (count - 10) % 6 == 0;
42 std::string UUID::GetAsString(llvm::StringRef separator) const {
44 llvm::raw_string_ostream os(result);
46 for (auto B : llvm::enumerate(GetBytes())) {
47 if (separate(B.index()))
50 os << llvm::format_hex_no_prefix(B.value(), 2, true);
57 void UUID::Dump(Stream *s) const {
58 s->PutCString(GetAsString().c_str());
61 static inline int xdigit_to_int(char ch) {
63 if (ch >= 'a' && ch <= 'f')
69 UUID::DecodeUUIDBytesFromString(llvm::StringRef p,
70 llvm::SmallVectorImpl<uint8_t> &uuid_bytes,
71 uint32_t num_uuid_bytes) {
74 if (isxdigit(p[0]) && isxdigit(p[1])) {
75 int hi_nibble = xdigit_to_int(p[0]);
76 int lo_nibble = xdigit_to_int(p[1]);
77 // Translate the two hex nibble characters into a byte
78 uuid_bytes.push_back((hi_nibble << 4) + lo_nibble);
80 // Skip both hex digits
83 // Increment the byte that we are decoding within the UUID value and
84 // break out if we are done
85 if (uuid_bytes.size() == num_uuid_bytes)
87 } else if (p.front() == '-') {
91 // UUID values can only consist of hex characters and '-' chars
98 size_t UUID::SetFromStringRef(llvm::StringRef str, uint32_t num_uuid_bytes) {
99 llvm::StringRef p = str;
101 // Skip leading whitespace characters
104 llvm::SmallVector<uint8_t, 20> bytes;
105 llvm::StringRef rest =
106 UUID::DecodeUUIDBytesFromString(p, bytes, num_uuid_bytes);
108 // If we successfully decoded a UUID, return the amount of characters that
110 if (bytes.size() == num_uuid_bytes) {
111 *this = fromData(bytes);
112 return str.size() - rest.size();
115 // Else return zero to indicate we were not able to parse a UUID value