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
|
#pragma once
#include <util/system/types.h>
#include <util/system/yassert.h>
#include <cctype>
#include <cstring>
#include <utility>
class IOutputStream;
namespace NKikimr {
namespace NUuid {
static constexpr ui32 UUID_LEN = 16;
TString UuidBytesToString(const TString& in);
void UuidBytesToString(const TString& in, IOutputStream& out);
void UuidHalfsToString(ui64 low, ui64 hi, IOutputStream& out);
void UuidToString(ui16 dw[8], IOutputStream& out);
void UuidHalfsToByteString(ui64 low, ui64 hi, IOutputStream& out);
inline bool GetDigit(char c, ui32& digit) {
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
}
return true;
}
template<typename T>
inline bool IsValidUuid(const T& buf) {
if (buf.Size() != 36) {
return false;
}
for (size_t i = 0; i < buf.Size(); ++i) {
const char c = buf.Data()[i];
if (c == '-') {
if (i != 8 && i != 13 && i != 18 && i != 23) {
return false;
}
} else if (!std::isxdigit(c)) {
return false;
}
}
return true;
}
template<typename T>
bool ParseUuidToArray(const T& buf, ui16* dw, bool shortForm) {
if (buf.size() != (shortForm ? 32 : 36)) {
return false;
}
size_t partId = 0;
ui64 partValue = 0;
size_t digitCount = 0;
for (size_t i = 0; i < buf.size(); ++i) {
const char c = buf.data()[i];
if (!shortForm && (i == 8 || i == 13 || i == 18 || i == 23)) {
if (c == '-') {
continue;
} else {
return false;
}
}
ui32 digit = 0;
if (!GetDigit(c, digit)) {
return false;
}
partValue = partValue * 16 + digit;
if (++digitCount == 4) {
dw[partId++] = partValue;
digitCount = 0;
}
}
std::swap(dw[0], dw[1]);
for (ui32 i = 4; i < 8; ++i) {
dw[i] = ((dw[i] >> 8) & 0xff) | ((dw[i] & 0xff) << 8);
}
return true;
}
inline void UuidHalfsToBytes(char *dst, size_t dstSize, ui64 hi, ui64 low) {
union {
char bytes[UUID_LEN];
ui64 half[2];
} buf;
Y_ABORT_UNLESS(UUID_LEN == dstSize);
buf.half[0] = low;
buf.half[1] = hi;
memcpy(dst, buf.bytes, sizeof(buf));
}
inline void UuidBytesToHalfs(const char *str, size_t sz, ui64 &high, ui64 &low) {
union {
char bytes[UUID_LEN];
ui64 half[2];
} buf;
Y_ABORT_UNLESS(UUID_LEN == sz);
memcpy(buf.bytes, str, sizeof(buf));
low = buf.half[0];
high = buf.half[1];
}
}
}
|