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 #include "lldb/Utility/Stream.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/Support/Format.h"
20 using namespace lldb_private;
22 // Whether to put a separator after count uuid bytes.
23 // For the first 16 bytes we follow the traditional UUID format. After that, we
24 // simply put a dash after every 6 bytes.
25 static inline bool separate(size_t count) {
27 return (count - 10) % 6 == 0;
39 std::string UUID::GetAsString(llvm::StringRef separator) const {
41 llvm::raw_string_ostream os(result);
43 for (auto B : llvm::enumerate(GetBytes())) {
44 if (separate(B.index()))
47 os << llvm::format_hex_no_prefix(B.value(), 2, true);
54 void UUID::Dump(Stream *s) const { s->PutCString(GetAsString()); }
56 static inline int xdigit_to_int(char ch) {
58 if (ch >= 'a' && ch <= 'f')
64 UUID::DecodeUUIDBytesFromString(llvm::StringRef p,
65 llvm::SmallVectorImpl<uint8_t> &uuid_bytes,
66 uint32_t num_uuid_bytes) {
69 if (isxdigit(p[0]) && isxdigit(p[1])) {
70 int hi_nibble = xdigit_to_int(p[0]);
71 int lo_nibble = xdigit_to_int(p[1]);
72 // Translate the two hex nibble characters into a byte
73 uuid_bytes.push_back((hi_nibble << 4) + lo_nibble);
75 // Skip both hex digits
78 // Increment the byte that we are decoding within the UUID value and
79 // break out if we are done
80 if (uuid_bytes.size() == num_uuid_bytes)
82 } else if (p.front() == '-') {
86 // UUID values can only consist of hex characters and '-' chars
93 size_t UUID::SetFromStringRef(llvm::StringRef str, uint32_t num_uuid_bytes) {
94 llvm::StringRef p = str;
96 // Skip leading whitespace characters
99 llvm::SmallVector<uint8_t, 20> bytes;
100 llvm::StringRef rest =
101 UUID::DecodeUUIDBytesFromString(p, bytes, num_uuid_bytes);
103 // If we successfully decoded a UUID, return the amount of characters that
105 if (bytes.size() == num_uuid_bytes) {
106 *this = fromData(bytes);
107 return str.size() - rest.size();
110 // Else return zero to indicate we were not able to parse a UUID value