1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
#include "guid.h"
#include <library/cpp/yt/exception/exception.h>
#include <util/random/random.h>
#include <util/string/printf.h>
namespace NYT {
////////////////////////////////////////////////////////////////////////////////
namespace {
const ui8 HexDigits[16] = {
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
};
} // anonymous namespace
////////////////////////////////////////////////////////////////////////////////
TGuid TGuid::Create()
{
return TGuid(RandomNumber<ui64>(), RandomNumber<ui64>());
}
TGuid TGuid::FromString(TStringBuf str)
{
TGuid guid;
if (!FromString(str, &guid)) {
throw TSimpleException(Sprintf("Error parsing GUID \"%s\"",
std::string(str).c_str()));
}
return guid;
}
bool TGuid::FromString(TStringBuf str, TGuid* result)
{
size_t partId = 3;
ui64 partValue = 0;
bool isEmptyPart = true;
for (size_t i = 0; i != str.size(); ++i) {
const char c = str[i];
if (c == '-') {
if (isEmptyPart || partId == 0) { // x-y--z, -x-y-z or x-y-z-m-...
return false;
}
result->Parts32[partId] = static_cast<ui32>(partValue);
--partId;
partValue = 0;
isEmptyPart = true;
continue;
}
ui32 digit = 0;
if ('0' <= c && c <= '9') {
digit = c - '0';
} else if ('a' <= c && c <= 'f') {
digit = c - 'a' + 10;
} else if ('A' <= c && c <= 'F') {
digit = c - 'A' + 10;
} else {
return false; // non-hex character
}
partValue = partValue * 16 + digit;
isEmptyPart = false;
// overflow check
if (partValue > Max<ui32>()) {
return false;
}
}
if (partId != 0 || isEmptyPart) { // x-y or x-y-z-
return false;
}
result->Parts32[partId] = static_cast<ui32>(partValue);
return true;
}
TGuid TGuid::FromStringHex32(TStringBuf str)
{
TGuid guid;
if (!FromStringHex32(str, &guid)) {
throw TSimpleException(Sprintf("Error parsing Hex32 GUID \"%s\"",
str.data()));
}
return guid;
}
bool TGuid::FromStringHex32(TStringBuf str, TGuid* result)
{
if (str.size() != 32) {
return false;
}
bool ok = true;
int i = 0;
auto parseChar = [&] {
const char c = str[i++];
ui32 digit = 0;
if ('0' <= c && c <= '9') {
digit = c - '0';
} else if ('a' <= c && c <= 'f') {
digit = c - 'a' + 10;
} else if ('A' <= c && c <= 'F') {
digit = c - 'A' + 10;
} else {
ok = false;
}
return digit;
};
for (size_t j = 0; j < 16; ++j) {
result->ReversedParts8[15 - j] = parseChar() * 16 + parseChar();
}
return ok;
}
char* WriteGuidToBuffer(char* ptr, TGuid value)
{
// Each 32-bit component is emitted as lowercase hex with leading zeros
// stripped (so 1..8 digits). We derive the exact digit count from the
// position of the highest set bit and fill the digits back-to-front; this
// avoids the long branch cascade of the naive per-magnitude approach and
// writes exactly as many bytes as the component requires.
auto writeComponent = [&] (ui32 x) {
int digits = x == 0 ? 1 : (35 - __builtin_clz(x)) >> 2;
char* start = ptr;
char* cursor = ptr + digits;
ptr = cursor;
do {
*--cursor = HexDigits[x & 0xf];
x >>= 4;
} while (cursor != start);
};
writeComponent(value.Parts32[3]);
*ptr++ = '-';
writeComponent(value.Parts32[2]);
*ptr++ = '-';
writeComponent(value.Parts32[1]);
*ptr++ = '-';
writeComponent(value.Parts32[0]);
return ptr;
}
////////////////////////////////////////////////////////////////////////////////
TFormattableGuid::TFormattableGuid(TGuid guid)
: End_(WriteGuidToBuffer(Buffer_.data(), guid))
{ }
TStringBuf TFormattableGuid::ToStringBuf() const
{
return {Buffer_.data(), End_};
}
////////////////////////////////////////////////////////////////////////////////
} // namespace NYT
|