-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsafmat.hpp
798 lines (675 loc) · 23.8 KB
/
safmat.hpp
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
/* Standalone Text Formatting Library.
Copyright (C) 2022 Benjamin Stürz <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef FILE_SAFMAT_HPP
#define FILE_SAFMAT_HPP
#include <string_view>
#include <type_traits>
#include <functional>
#include <exception>
#include <optional>
#include <concepts>
#include <charconv>
#include <variant>
#include <utility>
#include <cstring>
#include <version>
#include <string>
#include <memory>
#include <limits>
#include <cctype>
#include <cmath>
#include <array>
#include <span>
#if __cpp_lib_source_location >= 201907L
# include <source_location>
#endif
// Enable support for std::ostream Output (default=disabled).
#ifndef SAFMAT_OUT_OSTREAM
# define SAFMAT_OUT_OSTREAM 0
#endif
#if SAFMAT_OUT_OSTREAM
# include <ostream>
#endif
// Enable support for std::FILE* Output (default=enabled).
#ifndef SAFMAT_OUT_FILE
# define SAFMAT_OUT_FILE 1
#endif
#if SAFMAT_OUT_FILE
# include <cstdio>
#endif
namespace safmat {
template<class T>
struct Formatter;
struct FormatContext;
class format_error : std::exception {
private:
std::string msg;
public:
format_error(std::string msg) : msg(std::move(msg)) {}
const char *what() const noexcept override { return msg.c_str(); }
};
}
namespace safmat::io {
template<class T>
struct OutputAdapter;
template<class T>
concept OutputConcept = requires (T *out, std::string_view s) {
OutputAdapter<T>::write(out, s);
};
class Output {
private:
struct OutputBase {
virtual ~OutputBase() = default;
virtual void write(std::string_view s) const = 0;
};
template<OutputConcept T>
struct OutputImpl : OutputBase {
T *out;
OutputImpl(T *out) noexcept : out(out) {}
void write(std::string_view s) const override {
OutputAdapter<T>::write(out, s);
}
};
std::shared_ptr<const OutputBase> ptr;
public:
Output(const Output &) = default;
Output(Output &&) = default;
template<OutputConcept T>
Output(T *out) : ptr(std::make_unique<const OutputImpl<T>>(out)) {}
template<OutputConcept T>
Output(T &out) : ptr(std::make_unique<const OutputImpl<T>>(&out)) {}
Output &operator=(const Output &) = default;
Output &operator=(Output &&) = default;
void write(std::string_view s) const { ptr->write(s); }
void write(char ch) const { ptr->write({ &ch, &ch + 1 }); }
};
}
namespace safmat {
using io::Output;
using InputIterator = decltype(std::string_view{}.begin());
template<class T>
concept Formattable = requires (const std::remove_cvref_t<T> &x,
Formatter<std::remove_cvref_t<T>> &fmt,
InputIterator &in,
FormatContext &ctx) {
fmt.parse(in);
fmt.format_to(ctx, x);
};
class FormatArg {
private:
struct FormatArgBase {
virtual ~FormatArgBase() = default;
virtual void parse(InputIterator &) = 0;
virtual void format_to(FormatContext &) = 0;
virtual void reset_fmt() = 0;
virtual std::optional<std::size_t> to_size_t() const noexcept = 0;
};
template<Formattable T>
struct FormatArgImpl : FormatArgBase {
Formatter<T> fmt{};
T value;
FormatArgImpl(T value) : value(std::move(value)) {}
void parse(InputIterator &in) override { fmt.parse(in); }
void format_to(FormatContext &ctx) override { fmt.format_to(ctx, value); }
void reset_fmt() override { fmt = Formatter<T>{}; }
std::optional<std::size_t> to_size_t() const noexcept override {
if constexpr (std::integral<T>) {
return std::size_t(value);
} else {
return {};
}
}
};
std::unique_ptr<FormatArgBase> ptr;
public:
template<Formattable T>
FormatArg(T x) : ptr(std::make_unique<FormatArgImpl<T>>(std::move(x))) {}
FormatArg(FormatArg &&) = default;
FormatArg &operator=(FormatArg &&) = default;
template<Formattable T>
FormatArg &operator=(T x) {
ptr = std::make_unique<FormatArgImpl<T>>(std::move(x));
return *this;
}
void parse(InputIterator &in) const { ptr->parse(in); }
void format_to(FormatContext &ctx) const { ptr->format_to(ctx); }
void reset_fmt() const { ptr->reset_fmt(); }
std::optional<std::size_t> to_size_t() const noexcept { return ptr->to_size_t(); }
std::size_t expect_size_t() const {
const auto opt = to_size_t();
return opt.has_value() ? opt.value() : throw format_error{"Expected size as the nested argument."};
}
};
struct FormatContext {
Output out;
std::span<FormatArg> args{};
std::size_t carg{0};
const FormatArg &operator[](std::size_t idx) const {
return idx < args.size() ? args[idx] : throw format_error{"Not enough format arguments."};
}
const FormatArg &next() {
return (*this)[carg++];
}
};
inline void xformat_to(FormatContext &ctx, std::string_view fmt) {
auto &out = ctx.out;
auto it = begin(fmt);
while (it != end(fmt)) {
if (*it == '{') {
++it;
if (*it == '{') {
++it;
out.write('{');
continue;
}
std::size_t idx;
if (std::isdigit(*it)) {
idx = 0;
while (std::isdigit(*it))
idx = idx * 10 + (*it++ - '0');
} else {
idx = ctx.carg++;
}
auto &arg = ctx[idx];
arg.reset_fmt();
if (*it == ':') {
++it;
arg.parse(it);
}
if (*it != '}')
throw format_error("Expected '}'.");
++it;
arg.format_to(ctx);
} else if (*it == '}') {
++it;
if (*it == '}') {
++it;
out.write('}');
} else {
throw format_error("'}' must be escaped with '}'.");
}
} else {
out.write(*it++);
}
}
}
template<class... Args>
void format_to(Output out, std::string_view fmt, Args&&... args) {
std::array<FormatArg, sizeof...(args)> argv{ FormatArg{ std::forward<Args>(args) }... };
auto ctx = FormatContext{ out, argv, 0 };
xformat_to(ctx, fmt);
}
template<class... Args>
std::string format(std::string_view fmt, Args&&... args) {
std::string str{};
Output out{str};
format_to(out, fmt, std::forward<Args>(args)...);
return str;
}
template<class... Args>
void print(Output out, std::string_view fmt, Args&&... args) {
format_to(out, fmt, std::forward<Args>(args)...);
}
template<class... Args>
void print(std::string_view fmt, Args&&... args) {
print(stdout, fmt, std::forward<Args>(args)...);
}
template<class... Args>
void println(Output out, std::string_view fmt, Args&&... args) {
format_to(out, fmt, std::forward<Args>(args)...);
out.write('\n');
}
template<class... Args>
void println(std::string_view fmt, Args&&... args) {
println(stdout, fmt, std::forward<Args>(args)...);
}
}
// OutputAdapter<T> specializations.
namespace safmat::io {
template<>
struct OutputAdapter<std::string> {
inline static void write(std::string *out, std::string_view s) {
*out += s;
}
};
#if SAFMAT_OUT_OSTREAM
template<>
struct OutputAdapter<std::ostream> {
inline static void write(std::ostream *out, std::string_view s) {
*out << s;
}
};
#endif // SAFMAT_OUT_OSTREAM
#if SAFMAT_OUT_FILE
template<>
struct OutputAdapter<FILE> {
inline static void write(FILE *out, std::string_view s) {
std::fwrite(s.data(), 1, s.size(), out);
}
};
#endif // SAFMAT_OUT_FILE
}
// Concepts used by Formatter<>'s.
namespace safmat::concepts {
template<class T>
concept StringLike = requires (T x) {
std::string_view{x};
};
template<class C>
concept FormattableContainer = !StringLike<C> && requires (const C &c) {
{ *begin(c) } -> Formattable;
{ *end(c) } -> Formattable;
};
template<FormattableContainer C>
using elem_type_t = std::remove_cvref_t<decltype(*begin(*(C *)0))>;
}
// Formatter<> helpers.
namespace safmat::internal {
class NestedSizeArgFormatter {
private:
// std::monostate => unspecified,
// std::size_t => direct,
// std::optional => indirect,
std::variant<std::monostate, std::size_t, std::optional<std::size_t>> arg_rep;
public:
std::optional<std::size_t> arg;
bool parse(InputIterator &in) {
const auto parse_number = [&in] {
std::size_t n{};
while (std::isdigit(*in))
n = n * 10 + (*in++ - '0');
return n;
};
if (std::isdigit(*in)) {
arg_rep = parse_number();
return true;
} else if (*in == '{') {
std::optional<std::size_t> idx{};
++in;
if (std::isdigit(*in))
idx = parse_number();
if (*in != '}')
throw format_error{"Expected '}' for nested argument."};
++in;
arg_rep = idx;
return true;
} else {
return false;
}
}
void read(FormatContext &ctx) {
if (arg.has_value())
return;
if (auto n = std::get_if<std::size_t>(&arg_rep)) {
arg = *n;
} else if (auto idx = std::get_if<std::optional<std::size_t>>(&arg_rep)) {
arg = (idx->has_value() ? ctx[idx->value()] : ctx.next()).expect_size_t();
}
}
};
struct PaddedFormatter : private NestedSizeArgFormatter {
char fill;
char padding;
PaddedFormatter(char fill = '<', char padding = ' ') : fill{fill}, padding{padding} {}
void parse_fill(InputIterator &in) {
auto is_fill = [](char ch) {
return ch == '<' || ch == '>' || ch == '^';
};
if (is_fill(*in)) {
padding = ' ';
fill = *in++;
} else if (*in && is_fill(in[1])) {
padding = *in++;
fill = *in++;
}
}
void parse_width(InputIterator &in) {
NestedSizeArgFormatter::parse(in);
}
void parse(InputIterator &in) {
parse_fill(in);
parse_width(in);
}
void print_padding(Output out, std::size_t len, std::size_t add) {
const std::string pad(fill == '^' ? (len + add) / 2 : len, padding);
out.write(pad);
}
void read_width(FormatContext &ctx) { NestedSizeArgFormatter::read(ctx); }
std::size_t width() const { return NestedSizeArgFormatter::arg.value_or(0); }
void pre_format(Output out, std::size_t len) {
if (len < width() && (fill == '>' || fill == '^')) {
print_padding(out, width() - len, 0);
}
}
void post_format(Output out, std::size_t len) {
if (len < width() && (fill == '<' || fill == '^')) {
print_padding(out, width() - len, 1);
}
}
};
struct PrecisionFormatter : private NestedSizeArgFormatter {
void read_prec(FormatContext &ctx) { NestedSizeArgFormatter::read(ctx); }
auto prec() const { return NestedSizeArgFormatter::arg; }
void set_prec(std::size_t n) { arg = n; }
void parse_prec(InputIterator &in) {
if (*in == '.') {
++in;
if (!NestedSizeArgFormatter::parse(in))
throw format_error{"Expected precision."};
}
}
};
struct NumericFormatter : PaddedFormatter {
char sign{'-'};
char alternate{false};
char pad_zero{false};
NumericFormatter() : PaddedFormatter{'>', '\0'} {}
void parse(InputIterator &in) {
PaddedFormatter::parse_fill(in);
// Parse sign.
if (*in == '+' || *in == '-' || *in == ' ') {
sign = *in++;
}
// Parse alternate form.
if (*in == '#') {
alternate = true;
++in;
}
// Parse '0'.
if (*in == '0') {
pad_zero = padding == '\0';
++in;
}
PaddedFormatter::parse_width(in);
}
void format(FormatContext &ctx, std::string_view number, bool negative) {
const std::string_view sign_str = negative ? "-" : (sign != '-' ? std::string_view{&sign, &sign + 1} : "");
auto &out = ctx.out;
PaddedFormatter::read_width(ctx);
if (pad_zero) {
out.write(sign_str);
if (const auto len = number.size(); len < width()) {
const std::string pad(width() - len, '0');
out.write(pad);
}
out.write(number);
} else {
const auto len = sign_str.size() + number.size();
PaddedFormatter::pre_format(out, len);
out.write(sign_str);
out.write(number);
PaddedFormatter::post_format(out, len);
}
}
};
struct IntegralFormatter : NumericFormatter {
char rep;
IntegralFormatter(char rep) : rep{rep} {}
void parse(InputIterator &in, bool is_bool) {
NumericFormatter::parse(in);
// Parse 'L'.
if (*in == 'L')
throw format_error{"Locale-specific formatting is not implemented/supported."};
// Parse rep.
switch (*in) {
case 'b':
case 'B':
case 'c':
case 'd':
case 'o':
case 'x':
case 'X':
rep = *in++;
break;
case '}':
break;
case 's':
if (is_bool) {
rep = *in++;
break;
}
// fallthrough
default:
throw format_error("Expected '}'.");
}
}
void format(FormatContext &ctx, std::function<std::string(int)> f, bool negative) {
std::string number{};
PaddedFormatter::read_width(ctx);
switch (rep) {
case 'b':
case 'B':
case 'x':
case 'X':
if (alternate) {
number += '0';
number += rep;
}
number += f(std::tolower(rep) == 'b' ? 2 : 16);
break;
case 'c':
case 'd':
case '\0':
number = f(rep == 'c' ? 0 : 10);
break;
case 'o':
if (alternate)
number += '0';
number += f(8);
break;
case 's':
number = f(1);
break;
default:
throw format_error{"Unimplemented operation."};
}
NumericFormatter::format(ctx, number, negative);
}
};
struct FloatingPointFormatter : NumericFormatter, PrecisionFormatter {
char rep{'\0'};
void parse(InputIterator &in) {
NumericFormatter::parse(in);
PrecisionFormatter::parse_prec(in);
// Parse 'L'.
if (*in == 'L')
throw format_error{"Locale-specific formatting is not implemented/supported."};
// Parse rep.
switch (*in) {
case 'a':
case 'A':
case 'e':
case 'E':
case 'f':
case 'F':
case 'g':
case 'G':
rep = *in++;
break;
case '}':
break;
default:
throw format_error("Expected '}'.");
}
}
void format(FormatContext &ctx, std::function<std::string(std::optional<std::chars_format>)> f, bool negative) {
std::optional<std::chars_format> fmt{};
PaddedFormatter::read_width(ctx);
PrecisionFormatter::read_prec(ctx);
switch (rep) {
case 'a':
case 'A':
fmt = std::chars_format::hex;
break;
case 'e':
case 'E':
fmt = std::chars_format::scientific;
break;
case 'f':
case 'F':
fmt = std::chars_format::fixed;
break;
case 'g':
case 'G':
fmt = std::chars_format::general;
break;
case '\0':
if (prec().has_value())
fmt = std::chars_format::general;
break;
default:
throw format_error{"Unimplemented operation."};
}
if (rep != '\0' && !prec().has_value()) {
set_prec(6);
}
auto number = f(fmt);
if (std::isupper(rep)) {
std::for_each(begin(number), end(number), [](char &ch){ ch = std::toupper(static_cast<unsigned char>(ch)); });
}
NumericFormatter::format(ctx, number, negative);
}
};
struct StringFormatter : PaddedFormatter, PrecisionFormatter {
void parse(InputIterator &in) {
PaddedFormatter::parse(in);
PrecisionFormatter::parse_prec(in);
if (*in == 's')
++in;
}
void format_to(FormatContext &ctx, std::string_view s) {
PaddedFormatter::read_width(ctx);
PrecisionFormatter::read_prec(ctx);
const auto len = prec().has_value() ? std::min(prec().value(), s.length()) : s.length();
auto &out = ctx.out;
PaddedFormatter::pre_format(out, len);
out.write(s.substr(0, len));
PaddedFormatter::post_format(out, len);
}
};
}
// Formatter<T> specializations.
namespace safmat {
template<std::integral T>
struct Formatter<T> : internal::IntegralFormatter {
Formatter(char rep = 'd') : IntegralFormatter{rep} {}
void parse(InputIterator &in) {
IntegralFormatter::parse(in, std::is_same_v<T, bool>);
}
void format_to(FormatContext &ctx, T x) {
bool is_negative;
if constexpr (std::is_signed_v<T>) {
is_negative = x < T{};
if (is_negative)
x = -x;
} else {
is_negative = false;
}
const auto f = [x](int base) -> std::string {
if (base == 0) {
return std::string(1, static_cast<char>(x));
} else if (base == 1) {
return x ? "true" : "false";
}
char buffer[sizeof (T) * 8 + 2];
const auto result = std::to_chars(buffer, buffer + sizeof buffer, x, base);
if (result.ec == std::errc{}) {
*result.ptr = '\0';
return buffer;
} else {
throw format_error{"Number too big."};
}
};
IntegralFormatter::format(ctx, f, is_negative);
}
};
template<>
struct Formatter<bool> : Formatter<unsigned> {
Formatter() : Formatter<unsigned>('s') {}
};
template<>
struct Formatter<char> : Formatter<int> {
Formatter() : Formatter<int>('c') {}
};
template<std::floating_point T>
struct Formatter<T> : internal::FloatingPointFormatter {
void format_to(FormatContext &ctx, T x) {
const auto f = [this, x](std::optional<std::chars_format> fmt) -> std::string {
const auto v = std::abs(x);
const auto ilen = std::max(width(), (v != T{}) ? static_cast<std::size_t>(std::ceil(std::log10(v))) : 1);
const auto flen = prec().value_or(std::numeric_limits<T>::digits10 * 2);
const auto len = ilen + flen + 3;
std::unique_ptr<char []> buffer(new char[len]);
std::to_chars_result r;
if (!fmt.has_value()) {
r = std::to_chars(buffer.get(), buffer.get() + len, v);
} else if (prec().has_value()) {
r = std::to_chars(buffer.get(), buffer.get() + len, v, fmt.value(), prec().value());
} else {
r = std::to_chars(buffer.get(), buffer.get() + len, v, fmt.value());
}
if (r.ec == std::errc{}) {
*r.ptr = '\0';
return buffer.get();
} else {
throw format_error{"Number too long."};
}
};
format(ctx, f, x < T{});
}
};
template<concepts::StringLike T>
struct Formatter<T> : internal::StringFormatter {};
template<Formattable A, Formattable B>
struct Formatter<std::pair<A, B>> : internal::PaddedFormatter {
using T = std::pair<A, B>;
void format_to(FormatContext &ctx, const T &p) {
const auto &[a, b] = p;
internal::PaddedFormatter::read_width(ctx);
const auto f = format("({}, {})", a, b);
internal::PaddedFormatter::pre_format(ctx.out, f.length());
ctx.out.write(f);
internal::PaddedFormatter::post_format(ctx.out, f.length());
}
};
template<concepts::FormattableContainer C>
struct Formatter<C> : Formatter<concepts::elem_type_t<C>> {
using T = concepts::elem_type_t<C>;
using F = Formatter<T>;
void format_to(FormatContext &ctx, const C &c) {
auto &out = ctx.out;
auto it = begin(c);
const auto e = end(c);
out.write('[');
if (it != e) {
F::format_to(ctx, *it++);
while (it != e) {
out.write(", ");
F::format_to(ctx, *it++);
}
}
out.write(']');
}
};
#if __cpp_lib_source_location >= 201907L
template<>
struct Formatter<std::source_location> : internal::PaddedFormatter {
void format_to(FormatContext &ctx, const std::source_location &loc) {
auto &out = ctx.out;
safmat::format_to(out, "{}:{}:{}", loc.file_name(), loc.line(), loc.column());
}
};
#endif
}
#endif // FILE_SAFMAT_HPP