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
|
// # This file is a part of toml++ and is subject to the the terms of the MIT license.
// # Copyright (c) Mark Gillard <mark.gillard@outlook.com.au>
// # See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
// SPDX-License-Identifier: MIT
#pragma once
#include "preprocessor.hpp"
// # {{
#if !TOML_IMPLEMENTATION
#error This is an implementation-only header.
#endif
// # }}
#if TOML_ENABLE_FORMATTERS
#include "array.hpp"
#include "formatter.hpp"
#include "header_start.hpp"
#include "parse_result.hpp"
#include "print_to_stream.hpp"
#include "table.hpp"
#include "unicode.hpp"
#include "value.hpp"
TOML_IMPL_NAMESPACE_START {
enum class TOML_CLOSED_FLAGS_ENUM formatted_string_traits : unsigned {
none,
line_breaks = 1u << 0, // \n
tabs = 1u << 1, // \t
control_chars = 1u << 2, // also includes non-ascii vertical whitespace
single_quotes = 1u << 3,
non_bare = 1u << 4, // anything not satisfying "is bare key character"
non_ascii = 1u << 5, // any codepoint >= 128
all = (non_ascii << 1u) - 1u
};
TOML_MAKE_FLAGS(formatted_string_traits);
TOML_EXTERNAL_LINKAGE
formatter::formatter(const node* source_node, const parse_result* source_pr,
const formatter_constants& constants,
const formatter_config& config) noexcept //
#if TOML_ENABLE_PARSER && !TOML_EXCEPTIONS
: source_{source_pr && *source_pr ? &source_pr->table() : source_node},
result_{source_pr},
#else
: source_{source_pr ? source_pr : source_node},
#endif
constants_{&constants},
config_{config} {
TOML_ASSERT_ASSUME(source_);
config_.flags = (config_.flags | constants_->mandatory_flags) & ~constants_->ignored_flags;
indent_columns_ = {};
for (auto c : config_.indent) indent_columns_ += c == '\t' ? 4u : 1u;
int_format_mask_ =
config_.flags & (format_flags::allow_binary_integers | format_flags::allow_octal_integers |
format_flags::allow_hexadecimal_integers);
}
TOML_EXTERNAL_LINKAGE
void formatter::attach(std::ostream & stream) noexcept {
indent_ = {};
naked_newline_ = true;
stream_ = &stream;
}
TOML_EXTERNAL_LINKAGE
void formatter::detach() noexcept {
stream_ = nullptr;
}
TOML_EXTERNAL_LINKAGE
void formatter::print_newline(bool force) {
if (!naked_newline_ || force) {
print_to_stream(*stream_, '\n');
naked_newline_ = true;
}
}
TOML_EXTERNAL_LINKAGE
void formatter::print_indent() {
for (int i = 0; i < indent_; i++) {
print_to_stream(*stream_, config_.indent);
naked_newline_ = false;
}
}
TOML_EXTERNAL_LINKAGE
void formatter::print_unformatted(char c) {
print_to_stream(*stream_, c);
naked_newline_ = false;
}
TOML_EXTERNAL_LINKAGE
void formatter::print_unformatted(std::string_view str) {
print_to_stream(*stream_, str);
naked_newline_ = false;
}
TOML_EXTERNAL_LINKAGE
void formatter::print_string(std::string_view str, bool allow_multi_line, bool allow_bare,
bool allow_literal_whitespace) {
if (str.empty()) {
print_unformatted(literal_strings_allowed() ? "''"sv : "\"\""sv);
return;
}
// pre-scan the string to determine how we should output it
formatted_string_traits traits = {};
if (!allow_bare) traits |= formatted_string_traits::non_bare;
bool unicode_allowed = unicode_strings_allowed();
// ascii fast path
if (is_ascii(str.data(), str.length())) {
for (auto c : str) {
switch (c) {
case '\n':
traits |= formatted_string_traits::line_breaks;
break;
case '\t':
traits |= formatted_string_traits::tabs;
break;
case '\'':
traits |= formatted_string_traits::single_quotes;
break;
default: {
if TOML_UNLIKELY (is_control_character(c))
traits |= formatted_string_traits::control_chars;
if (!is_ascii_bare_key_character(static_cast<char32_t>(c)))
traits |= formatted_string_traits::non_bare;
break;
}
}
static constexpr auto all_ascii_traits =
formatted_string_traits::all & ~formatted_string_traits::non_ascii;
if (traits == all_ascii_traits) break;
}
}
// unicode slow path
else {
traits |= formatted_string_traits::non_ascii;
utf8_decoder decoder;
// if the unicode is malformed just treat the string as a single-line non-literal and
// escape all non-ascii characters (to ensure round-tripping and help with diagnostics)
const auto bad_unicode = [&]() noexcept {
traits &= ~formatted_string_traits::line_breaks;
traits |= formatted_string_traits::control_chars | formatted_string_traits::non_bare;
unicode_allowed = false;
};
for (auto c : str) {
decoder(c);
if TOML_UNLIKELY (decoder.error()) {
bad_unicode();
break;
}
if (!decoder.has_code_point()) continue;
switch (decoder.codepoint) {
case U'\n':
traits |= formatted_string_traits::line_breaks;
break;
case U'\t':
traits |= formatted_string_traits::tabs;
break;
case U'\'':
traits |= formatted_string_traits::single_quotes;
break;
default: {
if TOML_UNLIKELY (is_control_character(decoder.codepoint) ||
is_non_ascii_vertical_whitespace(decoder.codepoint))
traits |= formatted_string_traits::control_chars;
if (!is_bare_key_character(decoder.codepoint))
traits |= formatted_string_traits::non_bare;
break;
}
}
}
if (decoder.needs_more_input()) bad_unicode();
}
// strings with line breaks, tabs, and single-quotes can't be bare
if (!!(traits & (formatted_string_traits::line_breaks | formatted_string_traits::tabs |
formatted_string_traits::single_quotes)))
traits |= formatted_string_traits::non_bare;
// if the string meets the requirements of being 'bare' we can emit a bare string
// (bare strings are composed of letters and numbers; no whitespace, control chars, quotes, etc)
if (!(traits & formatted_string_traits::non_bare) &&
(!(traits & formatted_string_traits::non_ascii) || unicode_allowed)) {
print_unformatted(str);
return;
}
const auto real_tabs_allowed = allow_literal_whitespace && real_tabs_in_strings_allowed();
// determine if this should be a multi-line string (triple-quotes)
const auto multi_line = allow_literal_whitespace //
&& allow_multi_line //
&& multi_line_strings_allowed() //
&& !!(traits & formatted_string_traits::line_breaks);
// determine if this should be a literal string (single-quotes with no escaping)
const auto literal = literal_strings_allowed() //
&& !(traits & formatted_string_traits::control_chars) //
&& (!(traits & formatted_string_traits::single_quotes) || multi_line) //
&& (!(traits & formatted_string_traits::tabs) || real_tabs_allowed) //
&& (!(traits & formatted_string_traits::line_breaks) || multi_line) //
&& (!(traits & formatted_string_traits::non_ascii) || unicode_allowed);
// literal strings (single quotes, no escape codes)
if (literal) {
const auto quot = multi_line ? R"(''')"sv : R"(')"sv;
print_unformatted(quot);
print_unformatted(str);
print_unformatted(quot);
return;
}
// anything from here down is a non-literal string, so requires iteration and escaping.
print_unformatted(multi_line ? R"(""")"sv : R"(")"sv);
// ascii fast path
if (!(traits & formatted_string_traits::non_ascii)) {
for (auto c : str) {
switch (c) {
case '"':
print_to_stream(*stream_, R"(\")"sv);
break;
case '\\':
print_to_stream(*stream_, R"(\\)"sv);
break;
case '\x7F':
print_to_stream(*stream_, R"(\u007F)"sv);
break;
case '\t':
print_to_stream(*stream_, real_tabs_allowed ? "\t"sv : R"(\t)"sv);
break;
case '\n':
print_to_stream(*stream_, multi_line ? "\n"sv : R"(\n)"sv);
break;
default: {
// control characters from lookup table
if TOML_UNLIKELY (c >= '\x00' && c <= '\x1F')
print_to_stream(*stream_, control_char_escapes[c]);
// regular characters
else
print_to_stream(*stream_, c);
}
}
}
}
// unicode slow path
else {
utf8_decoder decoder;
const char* cp_start = str.data();
const char* cp_end = cp_start;
for (auto c : str) {
decoder(c);
cp_end++;
// if the decoder encounters malformed unicode just emit raw bytes and
if (decoder.error()) {
while (cp_start != cp_end) {
print_to_stream(*stream_, R"(\u00)"sv);
print_to_stream(*stream_, static_cast<uint8_t>(*cp_start),
value_flags::format_as_hexadecimal, 2);
cp_start++;
}
decoder.reset();
continue;
}
if (!decoder.has_code_point()) continue;
switch (decoder.codepoint) {
case U'"':
print_to_stream(*stream_, R"(\")"sv);
break;
case U'\\':
print_to_stream(*stream_, R"(\\)"sv);
break;
case U'\x7F':
print_to_stream(*stream_, R"(\u007F)"sv);
break;
case U'\t':
print_to_stream(*stream_, real_tabs_allowed ? "\t"sv : R"(\t)"sv);
break;
case U'\n':
print_to_stream(*stream_, multi_line ? "\n"sv : R"(\n)"sv);
break;
default: {
// control characters from lookup table
if TOML_UNLIKELY (decoder.codepoint <= U'\x1F')
print_to_stream(*stream_,
control_char_escapes[static_cast<uint_least32_t>(decoder.codepoint)]);
// escaped unicode characters
else if (decoder.codepoint > U'\x7F' &&
(!unicode_allowed || is_non_ascii_vertical_whitespace(decoder.codepoint))) {
if (static_cast<uint_least32_t>(decoder.codepoint) > 0xFFFFu) {
print_to_stream(*stream_, R"(\U)"sv);
print_to_stream(*stream_, static_cast<uint_least32_t>(decoder.codepoint),
value_flags::format_as_hexadecimal, 8);
} else {
print_to_stream(*stream_, R"(\u)"sv);
print_to_stream(*stream_, static_cast<uint_least32_t>(decoder.codepoint),
value_flags::format_as_hexadecimal, 4);
}
}
// regular characters
else
print_to_stream(*stream_, cp_start, static_cast<size_t>(cp_end - cp_start));
}
}
cp_start = cp_end;
}
}
print_unformatted(multi_line ? R"(""")"sv : R"(")"sv);
}
TOML_EXTERNAL_LINKAGE
void formatter::print(const value<std::string>& val) {
print_string(val.get());
}
TOML_EXTERNAL_LINKAGE
void formatter::print(const value<int64_t>& val) {
naked_newline_ = false;
if (*val >= 0 && !!int_format_mask_) {
static constexpr auto value_flags_mask = value_flags::format_as_binary |
value_flags::format_as_octal |
value_flags::format_as_hexadecimal;
const auto fmt = val.flags() & value_flags_mask;
switch (fmt) {
case value_flags::format_as_binary:
if (!!(int_format_mask_ & format_flags::allow_binary_integers)) {
print_to_stream(*stream_, "0b"sv);
print_to_stream(*stream_, *val, fmt);
return;
}
break;
case value_flags::format_as_octal:
if (!!(int_format_mask_ & format_flags::allow_octal_integers)) {
print_to_stream(*stream_, "0o"sv);
print_to_stream(*stream_, *val, fmt);
return;
}
break;
case value_flags::format_as_hexadecimal:
if (!!(int_format_mask_ & format_flags::allow_hexadecimal_integers)) {
print_to_stream(*stream_, "0x"sv);
print_to_stream(*stream_, *val, fmt);
return;
}
break;
default:
break;
}
}
// fallback to decimal
print_to_stream(*stream_, *val);
}
TOML_EXTERNAL_LINKAGE
void formatter::print(const value<double>& val) {
const std::string_view* inf_nan = nullptr;
switch (fpclassify(*val)) {
case fp_class::neg_inf:
inf_nan = &constants_->float_neg_inf;
break;
case fp_class::pos_inf:
inf_nan = &constants_->float_pos_inf;
break;
case fp_class::nan:
inf_nan = &constants_->float_nan;
break;
case fp_class::ok:
print_to_stream(*stream_, *val, value_flags::none,
!!(config_.flags & format_flags::relaxed_float_precision));
break;
default:
TOML_UNREACHABLE;
}
if (inf_nan) {
if (!!(config_.flags & format_flags::quote_infinities_and_nans))
print_to_stream_bookended(*stream_, *inf_nan, '"');
else
print_to_stream(*stream_, *inf_nan);
}
naked_newline_ = false;
}
TOML_EXTERNAL_LINKAGE
void formatter::print(const value<bool>& val) {
print_unformatted(*val ? constants_->bool_true : constants_->bool_false);
}
TOML_EXTERNAL_LINKAGE
void formatter::print(const value<date>& val) {
if (!!(config_.flags & format_flags::quote_dates_and_times))
print_to_stream_bookended(*stream_, *val, literal_strings_allowed() ? '\'' : '"');
else
print_to_stream(*stream_, *val);
naked_newline_ = false;
}
TOML_EXTERNAL_LINKAGE
void formatter::print(const value<time>& val) {
if (!!(config_.flags & format_flags::quote_dates_and_times))
print_to_stream_bookended(*stream_, *val, literal_strings_allowed() ? '\'' : '"');
else
print_to_stream(*stream_, *val);
naked_newline_ = false;
}
TOML_EXTERNAL_LINKAGE
void formatter::print(const value<date_time>& val) {
if (!!(config_.flags & format_flags::quote_dates_and_times))
print_to_stream_bookended(*stream_, *val, literal_strings_allowed() ? '\'' : '"');
else
print_to_stream(*stream_, *val);
naked_newline_ = false;
}
TOML_EXTERNAL_LINKAGE
void formatter::print_value(const node& val_node, node_type type) {
TOML_ASSUME(type > node_type::array);
switch (type) {
case node_type::string:
print(*reinterpret_cast<const value<std::string>*>(&val_node));
break;
case node_type::integer:
print(*reinterpret_cast<const value<int64_t>*>(&val_node));
break;
case node_type::floating_point:
print(*reinterpret_cast<const value<double>*>(&val_node));
break;
case node_type::boolean:
print(*reinterpret_cast<const value<bool>*>(&val_node));
break;
case node_type::date:
print(*reinterpret_cast<const value<date>*>(&val_node));
break;
case node_type::time:
print(*reinterpret_cast<const value<time>*>(&val_node));
break;
case node_type::date_time:
print(*reinterpret_cast<const value<date_time>*>(&val_node));
break;
default:
TOML_UNREACHABLE;
}
}
#if TOML_ENABLE_PARSER && !TOML_EXCEPTIONS
TOML_EXTERNAL_LINKAGE
bool formatter::dump_failed_parse_result() {
if (result_ && !(*result_)) {
stream() << result_->error();
return true;
}
return false;
}
#else
TOML_EXTERNAL_LINKAGE
TOML_ATTR(const)
bool formatter::dump_failed_parse_result() {
return false;
}
#endif
}
TOML_IMPL_NAMESPACE_END;
#include "header_end.hpp"
#endif // TOML_ENABLE_FORMATTERS
|