1 //===-- UUID.cpp ----------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Utility/UUID.h"
11 #include "lldb/Utility/Stream.h"
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/Support/Format.h"
19 using namespace lldb_private
;
21 // Whether to put a separator after count uuid bytes.
22 // For the first 16 bytes we follow the traditional UUID format. After that, we
23 // simply put a dash after every 6 bytes.
24 static inline bool separate(size_t count
) {
26 return (count
- 10) % 6 == 0;
38 UUID::UUID(UUID::CvRecordPdb70 debug_info
) {
39 llvm::sys::swapByteOrder(debug_info
.Uuid
.Data1
);
40 llvm::sys::swapByteOrder(debug_info
.Uuid
.Data2
);
41 llvm::sys::swapByteOrder(debug_info
.Uuid
.Data3
);
42 llvm::sys::swapByteOrder(debug_info
.Age
);
44 *this = UUID(&debug_info
, sizeof(debug_info
));
46 *this = UUID(&debug_info
.Uuid
, sizeof(debug_info
.Uuid
));
49 std::string
UUID::GetAsString(llvm::StringRef separator
) const {
51 llvm::raw_string_ostream
os(result
);
53 for (auto B
: llvm::enumerate(GetBytes())) {
54 if (separate(B
.index()))
57 os
<< llvm::format_hex_no_prefix(B
.value(), 2, true);
63 void UUID::Dump(Stream
&s
) const { s
.PutCString(GetAsString()); }
65 static inline int xdigit_to_int(char ch
) {
67 if (ch
>= 'a' && ch
<= 'f')
73 UUID::DecodeUUIDBytesFromString(llvm::StringRef p
,
74 llvm::SmallVectorImpl
<uint8_t> &uuid_bytes
) {
76 while (p
.size() >= 2) {
77 if (isxdigit(p
[0]) && isxdigit(p
[1])) {
78 int hi_nibble
= xdigit_to_int(p
[0]);
79 int lo_nibble
= xdigit_to_int(p
[1]);
80 // Translate the two hex nibble characters into a byte
81 uuid_bytes
.push_back((hi_nibble
<< 4) + lo_nibble
);
83 // Skip both hex digits
85 } else if (p
.front() == '-') {
89 // UUID values can only consist of hex characters and '-' chars
96 bool UUID::SetFromStringRef(llvm::StringRef str
) {
97 llvm::StringRef p
= str
;
99 // Skip leading whitespace characters
102 llvm::SmallVector
<uint8_t, 20> bytes
;
103 llvm::StringRef rest
= UUID::DecodeUUIDBytesFromString(p
, bytes
);
105 // Return false if we could not consume the entire string or if the parsed
107 if (!rest
.empty() || bytes
.empty())