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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
#include "string.h"
#include "format.h"
#include <library/cpp/yt/assert/assert.h>
#include <library/cpp/yt/exception/exception.h>
#include <util/generic/hash.h>
#include <util/string/ascii.h>
namespace NYT {
////////////////////////////////////////////////////////////////////////////////
void UnderscoreCaseToCamelCase(TStringBuilderBase* builder, TStringBuf str)
{
bool first = true;
bool upper = true;
for (char c : str) {
if (c == '_') {
upper = true;
} else {
if (upper) {
if (!std::isalpha(c) && !first) {
builder->AppendChar('_');
}
c = std::toupper(c);
}
builder->AppendChar(c);
upper = false;
}
first = false;
}
}
TString UnderscoreCaseToCamelCase(TStringBuf str)
{
TStringBuilder builder;
UnderscoreCaseToCamelCase(&builder, str);
return builder.Flush();
}
void CamelCaseToUnderscoreCase(TStringBuilderBase* builder, TStringBuf str)
{
bool first = true;
for (char c : str) {
if (std::isupper(c) && std::isalpha(c)) {
if (!first) {
builder->AppendChar('_');
}
c = std::tolower(c);
}
builder->AppendChar(c);
first = false;
}
}
TString CamelCaseToUnderscoreCase(TStringBuf str)
{
TStringBuilder builder;
CamelCaseToUnderscoreCase(&builder, str);
return builder.Flush();
}
////////////////////////////////////////////////////////////////////////////////
TString TrimLeadingWhitespaces(const TString& str)
{
for (int i = 0; i < static_cast<int>(str.size()); ++i) {
if (str[i] != ' ') {
return str.substr(i);
}
}
return "";
}
TString Trim(const TString& str, const TString& whitespaces)
{
size_t end = str.size();
while (end > 0) {
size_t i = end - 1;
bool isWhitespace = false;
for (auto c : whitespaces) {
if (str[i] == c) {
isWhitespace = true;
break;
}
}
if (!isWhitespace) {
break;
}
--end;
}
if (end == 0) {
return "";
}
size_t begin = str.find_first_not_of(whitespaces);
YT_VERIFY(begin != TString::npos);
YT_VERIFY(begin < end);
return str.substr(begin, end - begin);
}
////////////////////////////////////////////////////////////////////////////////
namespace {
const ui16 DecimalDigits2[100] = {
12336, 12592, 12848, 13104, 13360, 13616, 13872, 14128, 14384, 14640,
12337, 12593, 12849, 13105, 13361, 13617, 13873, 14129, 14385, 14641,
12338, 12594, 12850, 13106, 13362, 13618, 13874, 14130, 14386, 14642,
12339, 12595, 12851, 13107, 13363, 13619, 13875, 14131, 14387, 14643,
12340, 12596, 12852, 13108, 13364, 13620, 13876, 14132, 14388, 14644,
12341, 12597, 12853, 13109, 13365, 13621, 13877, 14133, 14389, 14645,
12342, 12598, 12854, 13110, 13366, 13622, 13878, 14134, 14390, 14646,
12343, 12599, 12855, 13111, 13367, 13623, 13879, 14135, 14391, 14647,
12344, 12600, 12856, 13112, 13368, 13624, 13880, 14136, 14392, 14648,
12345, 12601, 12857, 13113, 13369, 13625, 13881, 14137, 14393, 14649
};
template <class T>
char* WriteSignedDecIntToBufferBackwardsImpl(char* ptr, T value, TStringBuf min)
{
if (value == 0) {
--ptr;
*ptr = '0';
return ptr;
}
// The negative value handling code below works incorrectly for min values.
if (value == std::numeric_limits<T>::min()) {
ptr -= min.length();
::memcpy(ptr, min.begin(), min.length());
return ptr;
}
bool negative = false;
if (value < 0) {
negative = true;
value = -value;
}
while (value >= 10) {
auto rem = value % 100;
auto quot = value / 100;
ptr -= 2;
::memcpy(ptr, &DecimalDigits2[rem], 2);
value = quot;
}
if (value > 0) {
--ptr;
*ptr = ('0' + value);
}
if (negative) {
--ptr;
*ptr = '-';
}
return ptr;
}
template <class T>
char* WriteUnsignedDecIntToBufferBackwardsImpl(char* ptr, T value)
{
if (value == 0) {
--ptr;
*ptr = '0';
return ptr;
}
while (value >= 10) {
auto rem = value % 100;
auto quot = value / 100;
ptr -= 2;
::memcpy(ptr, &DecimalDigits2[rem], 2);
value = quot;
}
if (value > 0) {
--ptr;
*ptr = ('0' + value);
}
return ptr;
}
} // namespace
template <>
char* WriteDecIntToBufferBackwards(char* ptr, i32 value)
{
return WriteSignedDecIntToBufferBackwardsImpl(ptr, value, TStringBuf("-2147483647"));
}
template <>
char* WriteDecIntToBufferBackwards(char* ptr, i64 value)
{
return WriteSignedDecIntToBufferBackwardsImpl(ptr, value, TStringBuf("-9223372036854775808"));
}
template <>
char* WriteDecIntToBufferBackwards(char* ptr, ui32 value)
{
return WriteUnsignedDecIntToBufferBackwardsImpl(ptr, value);
}
template <>
char* WriteDecIntToBufferBackwards(char* ptr, ui64 value)
{
return WriteUnsignedDecIntToBufferBackwardsImpl(ptr, value);
}
////////////////////////////////////////////////////////////////////////////////
namespace {
template <class T>
char* WriteSignedHexIntToBufferBackwardsImpl(char* ptr, T value, bool uppercase, TStringBuf min)
{
if (value == 0) {
--ptr;
*ptr = '0';
return ptr;
}
// The negative value handling code below works incorrectly for min values.
if (value == std::numeric_limits<T>::min()) {
ptr -= min.length();
::memcpy(ptr, min.begin(), min.length());
return ptr;
}
bool negative = false;
if (value < 0) {
negative = true;
value = -value;
}
while (value != 0) {
auto rem = value & 0xf;
auto quot = value >> 4;
--ptr;
*ptr = uppercase ? IntToHexUppercase[rem] : IntToHexLowercase[rem];
value = quot;
}
if (negative) {
--ptr;
*ptr = '-';
}
return ptr;
}
template <class T>
char* WriteUnsignedHexIntToBufferBackwardsImpl(char* ptr, T value, bool uppercase)
{
if (value == 0) {
--ptr;
*ptr = '0';
return ptr;
}
while (value != 0) {
auto rem = value & 0xf;
auto quot = value >> 4;
--ptr;
*ptr = uppercase ? IntToHexUppercase[rem] : IntToHexLowercase[rem];
value = quot;
}
return ptr;
}
} // namespace
template <>
char* WriteHexIntToBufferBackwards(char* ptr, i32 value, bool uppercase)
{
return WriteSignedHexIntToBufferBackwardsImpl(ptr, value, uppercase, TStringBuf("-80000000"));
}
template <>
char* WriteHexIntToBufferBackwards(char* ptr, i64 value, bool uppercase)
{
return WriteSignedHexIntToBufferBackwardsImpl(ptr, value, uppercase, TStringBuf("-8000000000000000"));
}
template <>
char* WriteHexIntToBufferBackwards(char* ptr, ui32 value, bool uppercase)
{
return WriteUnsignedHexIntToBufferBackwardsImpl(ptr, value, uppercase);
}
template <>
char* WriteHexIntToBufferBackwards(char* ptr, ui64 value, bool uppercase)
{
return WriteUnsignedHexIntToBufferBackwardsImpl(ptr, value, uppercase);
}
////////////////////////////////////////////////////////////////////////////////
size_t TCaseInsensitiveStringHasher::operator()(TStringBuf arg) const
{
auto compute = [&] (char* buffer) {
for (size_t index = 0; index < arg.length(); ++index) {
buffer[index] = AsciiToLower(arg[index]);
}
return ComputeHash(TStringBuf(buffer, arg.length()));
};
const size_t SmallSize = 256;
if (arg.length() <= SmallSize) {
std::array<char, SmallSize> stackBuffer;
return compute(stackBuffer.data());
} else {
std::unique_ptr<char[]> heapBuffer(new char[arg.length()]);
return compute(heapBuffer.get());
}
}
bool TCaseInsensitiveStringEqualityComparer::operator()(TStringBuf lhs, TStringBuf rhs) const
{
return AsciiEqualsIgnoreCase(lhs, rhs);
}
////////////////////////////////////////////////////////////////////////////////
bool TryParseBool(TStringBuf value, bool* result)
{
if (value == "true" || value == "1") {
*result = true;
return true;
} else if (value == "false" || value == "0") {
*result = false;
return true;
} else {
return false;
}
}
bool ParseBool(TStringBuf value)
{
bool result;
if (!TryParseBool(value, &result)) {
throw TSimpleException(Format("Error parsing boolean value %Qv",
value));
}
return result;
}
TStringBuf FormatBool(bool value)
{
return value ? TStringBuf("true") : TStringBuf("false");
}
////////////////////////////////////////////////////////////////////////////////
} // namespace NYT
|