-
Notifications
You must be signed in to change notification settings - Fork 3
/
integral.hh
463 lines (401 loc) · 16.9 KB
/
integral.hh
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
// Copyright (c) 2022 Mikael Simonsson <https://mikaelsimonsson.com>.
// SPDX-License-Identifier: BSL-1.0
// # Format integral
#pragma once
#include "snn-core/formatter.hh"
#include "snn-core/strcore.hh"
#include "snn-core/fmt/context.hh"
#include "snn-core/fmt/error.hh"
#include "snn-core/hex/table/common.hh"
#include "snn-core/math/common.hh"
#include "snn-core/mem/raw/copy.hh"
#include "snn-core/num/safe.hh"
#include "snn-core/range/contiguous.hh"
namespace snn::fmt
{
namespace detail
{
SNN_DIAGNOSTIC_PUSH
SNN_DIAGNOSTIC_IGNORE_UNSAFE_BUFFER_USAGE
template <math::base Base, strict_unsigned_integral UInt, typename Buf>
constexpr void integral(UInt num, const cstrview group_separator,
const usize digits_per_group, const usize min_digits,
const array<char, 16>& hex_table, strcore<Buf>& append_to,
promise::no_overlap_t)
{
const usize digit_count = math::max(math::count_digits<Base>(num).get(), min_digits);
usize combined_size = digit_count;
if (digits_per_group > 0)
{
const usize separator_count = (digit_count - 1) / digits_per_group;
combined_size = num::safe{separator_count}
.multiply(group_separator.size())
.add(digit_count)
.value();
}
strview dest = append_to.append_uninitialized(combined_size);
char* const first = dest.begin();
char* cur = dest.end();
// Don't insert any separators if digits per group is zero.
// A starting group count of one will never be equal to zero digits per group.
usize group_count = (digits_per_group == 0) ? 1 : 0;
while (cur > first)
{
if (group_count == digits_per_group)
{
if (group_separator.size() == 1) [[likely]]
{
*(--cur) = group_separator.front(promise::not_empty);
}
else
{
cur -= group_separator.size();
mem::raw::copy(group_separator.data(), not_null{cur},
group_separator.byte_size(), promise::no_overlap);
}
group_count = 0;
}
++group_count;
if constexpr (Base == math::base::decimal)
{
const u64 digit = num % 10;
num /= 10;
*(--cur) = static_cast<char>(digit + '0');
}
else if constexpr (Base == math::base::hex)
{
*(--cur) = hex_table.at(num, bounds::mask);
num >>= 4;
}
else if constexpr (Base == math::base::binary)
{
*(--cur) = static_cast<char>((num & 0b1) + '0');
num >>= 1;
}
else if constexpr (Base == math::base::octal)
{
*(--cur) = static_cast<char>((num & 0b111) + '0');
num >>= 3;
}
else
{
static_assert(meta::always_false<decltype(Base)>, "Unsupported base.");
}
}
snn_should(cur == first);
}
SNN_DIAGNOSTIC_POP
template <math::base Base, strict_signed_integral SInt, typename Buf>
constexpr void integral(const SInt num, const cstrview group_separator,
const usize digits_per_group, const usize min_digits,
const array<char, 16>& hex_table, strcore<Buf>& append_to,
promise::no_overlap_t)
{
auto u = force_unsigned(num);
if (num < 0)
{
append_to.append('-');
u = math::negate_with_overflow(u);
}
integral<Base>(u, group_separator, digits_per_group, min_digits, hex_table, append_to,
promise::no_overlap);
}
}
// ## integral
// Format decimal number with `char` thousands separator.
template <strict_integral Int, typename Buf>
constexpr void integral(const Int num, const same_as<char> auto thousands_separator,
strcore<Buf>& append_to)
{
const cstrview group_separator{as_ref(thousands_separator)};
constexpr usize digits_per_group = 3;
constexpr usize min_digits = 0;
// Promote to limit template instantiations.
detail::integral<math::base::decimal>(promote<64>(num), group_separator, digits_per_group,
min_digits, hex::table::lower, append_to,
promise::no_overlap);
}
template <any_strcore Str = str, strict_integral Int>
[[nodiscard]] constexpr Str integral(const Int num,
const same_as<char> auto thousands_separator)
{
Str append_to;
// Promote to limit template instantiations.
integral(promote<64>(num), thousands_separator, append_to);
return append_to;
}
// ## integral
// Format decimal number with `cstrview` thousands separator.
template <strict_integral Int, typename Buf>
constexpr void integral(const Int num, const transient<cstrview> thousands_separator,
strcore<Buf>& append_to, promise::no_overlap_t)
{
snn_should(std::is_constant_evaluated() || !thousands_separator.get().overlaps(append_to));
constexpr usize digits_per_group = 3;
constexpr usize min_digits = 0;
// Promote to limit template instantiations.
detail::integral<math::base::decimal>(promote<64>(num), thousands_separator.get(),
digits_per_group, min_digits, hex::table::lower,
append_to, promise::no_overlap);
}
template <any_strcore Str = str, strict_integral Int>
[[nodiscard]] constexpr Str integral(const Int num,
const transient<cstrview> thousands_separator)
{
Str append_to;
constexpr usize digits_per_group = 3;
constexpr usize min_digits = 0;
// Promote to limit template instantiations.
detail::integral<math::base::decimal>(promote<64>(num), thousands_separator.get(),
digits_per_group, min_digits, hex::table::lower,
append_to, promise::no_overlap);
return append_to;
}
// ## integral
// Extended integral formatting.
template <math::base Base = math::base::decimal, strict_integral Int, typename Buf>
constexpr void integral(const Int num, const transient<cstrview> group_separator,
const usize digits_per_group, const usize min_digits,
const array<char, 16>& hex_table, strcore<Buf>& append_to,
promise::no_overlap_t)
{
snn_should(std::is_constant_evaluated() || !group_separator.get().overlaps(append_to));
// Promote to limit template instantiations.
detail::integral<Base>(promote<64>(num), group_separator.get(), digits_per_group,
min_digits, hex_table, append_to, promise::no_overlap);
}
template <any_strcore Str = str, math::base Base = math::base::decimal, strict_integral Int>
[[nodiscard]] constexpr Str integral(const Int num, const transient<cstrview> group_separator,
const usize digits_per_group, const usize min_digits = 0,
const array<char, 16>& hex_table = hex::table::lower)
{
Str append_to;
// Promote to limit template instantiations.
detail::integral<Base>(promote<64>(num), group_separator.get(), digits_per_group,
min_digits, hex_table, append_to, promise::no_overlap);
return append_to;
}
}
namespace snn
{
// Default formatters, can be overridden with formatter_override<T>.
template <>
struct formatter<u64>
{
using type = u64;
template <strict_unsigned_integral UInt, typename Buf>
requires(sizeof(UInt) >= 8)
constexpr void format(const UInt i, const cstrview format_string, const fmt::context&,
strcore<Buf>& append_to, promise::no_overlap_t)
{
if (format_string.is_empty()) [[likely]]
{
append_to.append_integral(i);
}
else
{
snn_should(std::is_constant_evaluated() || !format_string.overlaps(append_to));
auto rng = format_string.range();
// Sign
if (rng.drop_front(' '))
{
append_to.append(' ');
}
else if (rng.drop_front('+'))
{
append_to.append('+');
}
// Show prefix.
const bool show_prefix = rng.drop_front('#');
// Minimum digits.
const auto min_digits_value_count =
rng.pop_front_integral<usize, math::base::decimal, 4>();
const usize min_digits = min_digits_value_count.value;
if (rng.has_front_if(chr::is_digit))
{
throw_or_abort(fmt::error::invalid_minimum_digits_in_format_string);
}
// Base & prefix.
enum class format_base : u8
{
decimal = 0,
hex_lower = 1,
hex_upper = 2,
binary = 3,
octal = 4,
};
cstrview prefix;
format_base base = format_base::decimal;
if (rng.drop_front('d'))
{
// Nothing changes.
}
else if (rng.drop_front('x'))
{
prefix = "0x";
base = format_base::hex_lower;
}
else if (rng.drop_front('X'))
{
prefix = "0x";
base = format_base::hex_upper;
}
else if (rng.drop_front('b'))
{
prefix = "0b";
base = format_base::binary;
}
else if (rng.drop_front('o'))
{
prefix = "0o";
base = format_base::octal;
}
// Group separator.
const auto group_separator = cstrview{rng.pop_front_while(fn::_not{chr::is_digit})};
// Digits per group.
const auto digits_per_group_value_count =
rng.pop_front_integral<usize, math::base::decimal, 2>();
usize digits_per_group = digits_per_group_value_count.value;
if (!digits_per_group_value_count.count)
{
if (group_separator)
{
digits_per_group = 3;
}
}
else if (rng.has_front_if(chr::is_digit))
{
throw_or_abort(fmt::error::invalid_digits_per_group_in_format_string);
}
if (rng)
{
throw_or_abort(fmt::error::unexpected_character_in_format_string);
}
if (show_prefix && prefix)
{
append_to.append(prefix);
}
switch (base)
{
case format_base::decimal:
fmt::detail::integral<math::base::decimal>(i, group_separator,
digits_per_group, min_digits,
hex::table::lower, append_to,
promise::no_overlap);
break;
case format_base::hex_lower:
fmt::detail::integral<math::base::hex>(i, group_separator, digits_per_group,
min_digits, hex::table::lower,
append_to, promise::no_overlap);
break;
case format_base::hex_upper:
fmt::detail::integral<math::base::hex>(i, group_separator, digits_per_group,
min_digits, hex::table::upper,
append_to, promise::no_overlap);
break;
case format_base::binary:
fmt::detail::integral<math::base::binary>(i, group_separator,
digits_per_group, min_digits,
hex::table::lower, append_to,
promise::no_overlap);
break;
case format_base::octal:
fmt::detail::integral<math::base::octal>(i, group_separator,
digits_per_group, min_digits,
hex::table::lower, append_to,
promise::no_overlap);
break;
}
}
}
template <strict_unsigned_integral UInt, typename Buf>
requires(sizeof(UInt) < 8)
constexpr void format(const UInt i, const cstrview format_string, const fmt::context& ctx,
strcore<Buf>& append_to, promise::no_overlap_t)
{
// Promote to limit template instantiations.
format(promote<64>(i), format_string, ctx, append_to, promise::no_overlap);
}
};
template <>
struct formatter_format_as<u32>
{
using type = u64;
};
template <>
struct formatter_format_as<u16>
{
using type = u64;
};
template <>
struct formatter_format_as<u8>
{
using type = u64;
};
template <>
struct formatter<i64>
{
using type = i64;
template <strict_signed_integral SInt, typename Buf>
requires(sizeof(SInt) >= 8)
constexpr void format(const SInt i, cstrview format_string, const fmt::context& ctx,
strcore<Buf>& append_to, promise::no_overlap_t)
{
if (format_string.is_empty()) [[likely]]
{
append_to.append_integral(i);
}
else
{
snn_should(std::is_constant_evaluated() || !format_string.overlaps(append_to));
auto u = force_unsigned(i);
if (i < 0)
{
append_to.append('-');
u = math::negate_with_overflow(u);
if (format_string.has_front(' ') || format_string.has_front('+'))
{
format_string.drop_front_n(1);
}
}
formatter<u64> f;
f.format(u, format_string, ctx, append_to, promise::no_overlap);
}
}
template <strict_signed_integral SInt, typename Buf>
requires(sizeof(SInt) < 8)
constexpr void format(const SInt i, const cstrview format_string, const fmt::context& ctx,
strcore<Buf>& append_to, promise::no_overlap_t)
{
// Promote to limit template instantiations.
format(promote<64>(i), format_string, ctx, append_to, promise::no_overlap);
}
};
template <>
struct formatter_format_as<i32>
{
using type = i64;
};
template <>
struct formatter_format_as<i16>
{
using type = i64;
};
template <>
struct formatter_format_as<i8>
{
using type = i64;
};
#if SNN_INT128_BOOL
template <>
struct formatter_format_as<u128>
{
using type = u64;
};
template <>
struct formatter_format_as<i128>
{
using type = i64;
};
#endif
}