-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathStringFormatter.h
More file actions
416 lines (347 loc) · 13.7 KB
/
StringFormatter.h
File metadata and controls
416 lines (347 loc) · 13.7 KB
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// ------------------------------------------------------------
// Copyright(c) 2018-2022 Jesse Yurkovich
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// See the LICENSE file in the repo root for full license information.
// ------------------------------------------------------------
#pragma once
#include <algorithm>
#include <cmath>
#include <ios>
#include <iosfwd>
#include <limits>
#include <memory>
#include <string>
#include <string_view>
#include <type_traits>
namespace fx::common
{
namespace detail
{
// clang-format off
template <typename T>
using FormatType =
typename std::conditional_t<std::is_same_v<bool, T>, bool,
typename std::conditional_t<std::is_same_v<short, T> || std::is_same_v<int, T> || std::is_same_v<long, T>, long,
typename std::conditional_t<std::is_same_v<unsigned short, T> || std::is_same_v<unsigned int, T> || std::is_same_v<unsigned long, T>, unsigned long,
typename std::conditional_t<std::is_same_v<long long, T>, long long,
typename std::conditional_t<std::is_same_v<unsigned long long, T>, unsigned long long,
typename std::conditional_t<std::is_same_v<float, T> || std::is_same_v<double, T>, double,
typename std::conditional_t<std::is_same_v<long double, T>, long double, T>>>>>>>;
// clang-format on
} // namespace detail
// Custom formatting for user-defined types
template <typename Type, typename T>
struct Formatter final : std::false_type
{
};
// Formats all placeholders of the form: {index[,alignment][:formatString]}
template <typename T>
class BasicStringFormatter final
{
public:
using StringType = std::basic_string<T>;
using StringViewType = std::basic_string_view<T>;
template <typename... Args>
static StringType Format(StringViewType format, Args &&... args)
{
static thread_local StringType buffer;
buffer.clear();
BasicStringFormatter::FormatInto(buffer, format, std::forward<Args>(args)...);
return buffer;
}
template <typename... Args>
static void FormatInto(StringType & buffer, StringViewType format, Args &&... args)
{
// Provide space for the format string and the args...
buffer.reserve(buffer.length() + format.length() + sizeof...(args) * 4);
FormatState formatState(buffer);
T ch = '\x0';
std::size_t pos = 0;
std::size_t lastPos = 0;
const std::size_t len = format.length();
while (pos < len)
{
while (pos < len)
{
ch = format[pos];
pos++;
if (ch == '}')
{
if (pos < len && format[pos] == '}') // Treat as escape character for }}
{
buffer.append(&format[lastPos], pos - lastPos);
pos++;
lastPos = pos;
}
else
{
FormatErrorIf(true);
}
}
if (ch == '{')
{
if (pos < len && format[pos] == '{') // Treat as escape character for {{
{
buffer.append(&format[lastPos], pos - lastPos);
pos++;
lastPos = pos;
}
else
{
pos--;
buffer.append(&format[lastPos], pos - lastPos);
break;
}
}
}
if (pos == len)
break;
pos++;
int index = 0;
FormatErrorIf(!TryExtractInteger(format, pos, index));
FormatErrorIf(index >= sizeof...(args), "Format_IndexOutOfRange");
while (pos < len && (ch = format[pos]) == ' ')
pos++;
// Alignment specification...
int width = 0;
bool leftJustify = false;
if (ch == ',')
{
pos++;
while (pos < len && format[pos] == ' ')
pos++;
FormatErrorIf(pos == len);
ch = format[pos];
if (ch == '-')
{
leftJustify = true;
pos++;
}
FormatErrorIf(!TryExtractInteger(format, pos, width));
}
while (pos < len && (ch = format[pos]) == ' ')
pos++;
// Format specification...
StringViewType formatSpec;
if (ch == ':')
{
pos++;
const std::size_t startPos = pos;
while (true)
{
FormatErrorIf(pos == len);
ch = format[pos];
pos++;
if (ch == '}' || ch == '{')
{
if (ch == '{')
{
FormatErrorIf(true); // escaped braces not supported
}
else
{
FormatErrorIf(pos < len && format[pos] == '}'); // escaped braces not supported
formatSpec = format.substr(startPos, pos - startPos - 1);
pos--;
break;
}
}
}
}
FormatErrorIf(ch != '}');
pos++;
lastPos = pos;
// Perform the actual substitution...
formatState.Prepare(index, width, leftJustify, formatSpec);
ArgSearch(formatState, 0, std::forward<Args>(args)...);
}
if (lastPos != len)
{
buffer.append(&format[lastPos], pos - lastPos);
}
}
private:
class FormatState final
{
public:
explicit FormatState(StringType & buffer) noexcept
: ios_(), buffer_(buffer), index_(0), width_(0), leftJustify_(false)
{
}
int Index() const noexcept
{
return index_;
}
void Prepare(int index, std::size_t width, bool leftJustify, StringViewType formatSpec)
{
SetDefaults();
index_ = index;
width_ = width;
leftJustify_ = leftJustify;
formatSpec_ = formatSpec;
}
void Format(StringViewType arg)
{
Append(arg);
}
template <typename Arg, typename = std::enable_if_t<Formatter<std::remove_cv_t<Arg>, T>::value>>
void Format(Arg & arg)
{
Formatter<std::remove_cv_t<Arg>, T>::Format(arg, formatSpec_, [this](StringViewType view) { Append(view); });
}
template <typename Arg, typename = std::enable_if_t<std::is_arithmetic_v<Arg>>>
void Format(Arg arg)
{
static const auto & facet = std::use_facet<std::num_put<T, T *>>(std::locale());
T buf[64];
T * begin = std::begin(buf);
T * end = begin;
std::unique_ptr<T[]> scratch;
BasicStringFormatter<T>::FormatErrorIf(!ApplySpecForNumbers());
#pragma warning(push)
#pragma warning(disable : 4127) // conditional expression is constant
if constexpr (std::is_floating_point_v<Arg>)
{
ios_.precision(std::min(static_cast<int>(ios_.precision()), std::numeric_limits<Arg>::max_digits10));
const auto argAbs = std::fabs(arg);
if (argAbs > 1e46)
{
// Large numbers may exceed the size of our stack buffer...
const std::size_t num = static_cast<std::size_t>(std::ceil(std::log10(argAbs))) + ios_.precision() + 2;
if (num > 64)
{
scratch = std::make_unique<T[]>(num);
begin = std::addressof(scratch[0]);
end = begin;
}
}
}
#pragma warning(pop)
end = facet.put(begin, ios_, ' ', static_cast<typename detail::FormatType<Arg>>(arg));
Append(StringViewType{ begin, static_cast<std::size_t>(end - begin) });
}
private:
class IosBase final : public std::basic_ios<T>
{
public:
IosBase() noexcept
: std::basic_ios<T>()
{
std::basic_ios<T>::init(nullptr);
std::basic_ios<T>::setf(std::ios_base::boolalpha);
std::basic_ios<T>::setf(std::ios_base::fixed, std::ios_base::floatfield);
}
};
IosBase ios_;
StringType & buffer_;
int index_;
std::size_t width_;
bool leftJustify_;
StringViewType formatSpec_;
bool ApplySpecForNumbers()
{
bool requiresFill = false;
bool isFloating = false;
if (formatSpec_.empty())
return true;
std::size_t index = 0;
switch (formatSpec_[index])
{
case 'N':
case 'n':
requiresFill = true;
break;
case 'X':
ios_.setf(std::ios_base::uppercase);
case 'x':
requiresFill = true;
ios_.setf(std::ios_base::hex, std::ios_base::basefield);
break;
case 'F':
case 'f':
isFloating = true;
break;
}
index++;
int number = 0;
if (!TryExtractInteger(formatSpec_, index, number))
{
return false;
}
if (requiresFill)
{
ios_.fill('0');
width_ = number;
}
else if (isFloating)
{
ios_.precision(number);
}
return true;
}
void Append(StringViewType arg)
{
const bool shouldPad = width_ > arg.length();
const std::size_t pad = width_ - arg.length();
if (shouldPad && !leftJustify_)
buffer_.append(pad, ios_.fill());
buffer_.append(arg);
if (shouldPad && leftJustify_)
buffer_.append(pad, ios_.fill());
}
void SetDefaults()
{
ios_.unsetf(std::ios_base::uppercase);
ios_.setf(std::ios_base::dec, std::ios_base::basefield);
ios_.precision(2);
ios_.fill(' ');
}
};
static bool TryExtractInteger(StringViewType view, std::size_t & pos, int & out) noexcept
{
if (pos == view.length())
return false;
out = 0;
const std::size_t start = pos;
T ch = view[pos];
while (ch >= '0' && ch <= '9' && out < 1000000)
{
out = out * 10 + ch - '0';
pos++;
if (pos == view.length())
break;
ch = view[pos];
}
return pos != start;
}
static void FormatErrorIf(bool isError, char const * const message = "Format_InvalidString")
{
if (isError)
{
throw std::runtime_error(message);
}
}
template <typename Arg>
static void ArgSearch(FormatState & formatState, int currentIndex, Arg && arg)
{
if (currentIndex == formatState.Index())
{
formatState.Format(arg);
}
}
template <typename Arg, typename... Args>
static void ArgSearch(FormatState & formatState, int currentIndex, Arg && arg, Args &&... args)
{
if (currentIndex == formatState.Index())
{
formatState.Format(arg);
}
else
{
ArgSearch(formatState, currentIndex + 1, std::forward<Args>(args)...);
}
}
};
using StringFormatter = BasicStringFormatter<char>;
using WStringFormatter = BasicStringFormatter<wchar_t>;
} // namespace fx::common