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
|
// # 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
// # }}
#include "array.hpp"
#include "date_time.hpp"
#include "print_to_stream.hpp"
#include "source_region.hpp"
#include "table.hpp"
#include "toml_formatter.hpp"
#include "value.hpp"
TOML_DISABLE_WARNINGS;
#include <ostream>
#if TOML_INT_CHARCONV || TOML_FLOAT_CHARCONV
#include <charconv>
#endif
#if !TOML_INT_CHARCONV || !TOML_FLOAT_CHARCONV
#include <sstream>
#endif
#if !TOML_INT_CHARCONV
#include <iomanip>
#endif
TOML_ENABLE_WARNINGS;
#include "header_start.hpp"
TOML_ANON_NAMESPACE_START {
template <typename T>
inline constexpr size_t charconv_buffer_length = 0;
template <>
inline constexpr size_t charconv_buffer_length<int8_t> = 4; // strlen("-128")
template <>
inline constexpr size_t charconv_buffer_length<int16_t> = 6; // strlen("-32768")
template <>
inline constexpr size_t charconv_buffer_length<int32_t> = 11; // strlen("-2147483648")
template <>
inline constexpr size_t charconv_buffer_length<int64_t> = 20; // strlen("-9223372036854775808")
template <>
inline constexpr size_t charconv_buffer_length<uint8_t> = 3; // strlen("255")
template <>
inline constexpr size_t charconv_buffer_length<uint16_t> = 5; // strlen("65535")
template <>
inline constexpr size_t charconv_buffer_length<uint32_t> = 10; // strlen("4294967295")
template <>
inline constexpr size_t charconv_buffer_length<uint64_t> = 20; // strlen("18446744073709551615")
template <>
inline constexpr size_t charconv_buffer_length<float> = 64;
template <>
inline constexpr size_t charconv_buffer_length<double> = 64;
template <typename T>
TOML_INTERNAL_LINKAGE void print_integer_to_stream(
std::ostream & stream, T val, value_flags format = {}, size_t min_digits = 0) {
if (!val) {
if (!min_digits) min_digits = 1;
for (size_t i = 0; i < min_digits; i++) stream.put('0');
return;
}
static constexpr auto value_flags_mask = value_flags::format_as_binary |
value_flags::format_as_octal |
value_flags::format_as_hexadecimal;
format &= value_flags_mask;
int base = 10;
if (format != value_flags::none && val > T{}) {
switch (format) {
case value_flags::format_as_binary:
base = 2;
break;
case value_flags::format_as_octal:
base = 8;
break;
case value_flags::format_as_hexadecimal:
base = 16;
break;
default:
break;
}
}
#if TOML_INT_CHARCONV
char buf[(sizeof(T) * CHAR_BIT)];
const auto res = std::to_chars(buf, buf + sizeof(buf), val, base);
const auto len = static_cast<size_t>(res.ptr - buf);
for (size_t i = len; i < min_digits; i++) stream.put('0');
if (base == 16) {
for (size_t i = 0; i < len; i++)
if (buf[i] >= 'a') buf[i] -= 32;
}
impl::print_to_stream(stream, buf, len);
#else
using unsigned_type =
std::conditional_t<(sizeof(T) > sizeof(unsigned)), std::make_unsigned_t<T>, unsigned>;
using cast_type =
std::conditional_t<std::is_signed_v<T>, std::make_signed_t<unsigned_type>, unsigned_type>;
if (base == 2) {
const auto len = sizeof(T) * CHAR_BIT;
for (size_t i = len; i < min_digits; i++) stream.put('0');
bool found_one = false;
const auto v = static_cast<unsigned_type>(val);
unsigned_type mask = unsigned_type{1} << (len - 1u);
for (size_t i = 0; i < len; i++) {
if ((v & mask)) {
stream.put('1');
found_one = true;
} else if (found_one)
stream.put('0');
mask >>= 1;
}
} else {
std::ostringstream ss;
ss.imbue(std::locale::classic());
ss << std::uppercase << std::setbase(base);
if (min_digits) ss << std::setfill('0') << std::setw(static_cast<int>(min_digits));
ss << static_cast<cast_type>(val);
const auto str = std::move(ss).str();
impl::print_to_stream(stream, str);
}
#endif
}
template <typename T>
TOML_INTERNAL_LINKAGE void print_floating_point_to_stream(
std::ostream & stream, T val, value_flags format, [[maybe_unused]] bool relaxed_precision) {
switch (impl::fpclassify(val)) {
case impl::fp_class::neg_inf:
impl::print_to_stream(stream, "-inf"sv);
break;
case impl::fp_class::pos_inf:
impl::print_to_stream(stream, "inf"sv);
break;
case impl::fp_class::nan:
impl::print_to_stream(stream, "nan"sv);
break;
case impl::fp_class::ok: {
static constexpr auto needs_decimal_point = [](auto&& s) noexcept {
for (auto c : s)
if (c == '.' || c == 'E' || c == 'e') return false;
return true;
};
#if TOML_FLOAT_CHARCONV
const auto hex = !!(format & value_flags::format_as_hexadecimal);
char buf[charconv_buffer_length<T>];
auto res = hex ? std::to_chars(buf, buf + sizeof(buf), val, std::chars_format::hex)
: std::to_chars(buf, buf + sizeof(buf), val);
auto str = std::string_view{buf, static_cast<size_t>(res.ptr - buf)};
char buf2[charconv_buffer_length<T>];
if (!hex && relaxed_precision) {
res = std::to_chars(buf2, buf2 + sizeof(buf2), val, std::chars_format::general, 6);
const auto str2 = std::string_view{buf2, static_cast<size_t>(res.ptr - buf2)};
if (str2.length() < str.length()) str = str2;
}
impl::print_to_stream(stream, str);
if (!hex && needs_decimal_point(str)) toml::impl::print_to_stream(stream, ".0"sv);
#else
std::ostringstream ss;
ss.imbue(std::locale::classic());
if (!relaxed_precision) ss.precision(std::numeric_limits<T>::max_digits10);
if (!!(format & value_flags::format_as_hexadecimal)) ss << std::hexfloat;
ss << val;
const auto str = std::move(ss).str();
impl::print_to_stream(stream, str);
if (!(format & value_flags::format_as_hexadecimal) && needs_decimal_point(str))
impl::print_to_stream(stream, ".0"sv);
#endif
} break;
default:
TOML_UNREACHABLE;
}
}
}
TOML_ANON_NAMESPACE_END;
TOML_IMPL_NAMESPACE_START {
TOML_EXTERNAL_LINKAGE
TOML_ATTR(nonnull)
void TOML_CALLCONV print_to_stream(std::ostream & stream, const char* val, size_t len) {
stream.write(val, static_cast<std::streamsize>(len));
}
TOML_EXTERNAL_LINKAGE
void TOML_CALLCONV print_to_stream(std::ostream & stream, std::string_view val) {
stream.write(val.data(), static_cast<std::streamsize>(val.length()));
}
TOML_EXTERNAL_LINKAGE
void TOML_CALLCONV print_to_stream(std::ostream & stream, const std::string& val) {
stream.write(val.data(), static_cast<std::streamsize>(val.length()));
}
TOML_EXTERNAL_LINKAGE
void TOML_CALLCONV print_to_stream(std::ostream & stream, char val) {
stream.put(val);
}
TOML_EXTERNAL_LINKAGE
void TOML_CALLCONV print_to_stream(std::ostream & stream, signed char val, value_flags format,
size_t min_digits) {
TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
}
TOML_EXTERNAL_LINKAGE
void TOML_CALLCONV print_to_stream(std::ostream & stream, signed short val, value_flags format,
size_t min_digits) {
TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
}
TOML_EXTERNAL_LINKAGE
void TOML_CALLCONV print_to_stream(std::ostream & stream, signed int val, value_flags format,
size_t min_digits) {
TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
}
TOML_EXTERNAL_LINKAGE
void TOML_CALLCONV print_to_stream(std::ostream & stream, signed long val, value_flags format,
size_t min_digits) {
TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
}
TOML_EXTERNAL_LINKAGE
void TOML_CALLCONV print_to_stream(std::ostream & stream, signed long long val,
value_flags format, size_t min_digits) {
TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
}
TOML_EXTERNAL_LINKAGE
void TOML_CALLCONV print_to_stream(std::ostream & stream, unsigned char val, value_flags format,
size_t min_digits) {
TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
}
TOML_EXTERNAL_LINKAGE
void TOML_CALLCONV print_to_stream(std::ostream & stream, unsigned short val, value_flags format,
size_t min_digits) {
TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
}
TOML_EXTERNAL_LINKAGE
void TOML_CALLCONV print_to_stream(std::ostream & stream, unsigned int val, value_flags format,
size_t min_digits) {
TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
}
TOML_EXTERNAL_LINKAGE
void TOML_CALLCONV print_to_stream(std::ostream & stream, unsigned long val, value_flags format,
size_t min_digits) {
TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
}
TOML_EXTERNAL_LINKAGE
void TOML_CALLCONV print_to_stream(std::ostream & stream, unsigned long long val,
value_flags format, size_t min_digits) {
TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
}
TOML_EXTERNAL_LINKAGE
void TOML_CALLCONV print_to_stream(std::ostream & stream, float val, value_flags format,
bool relaxed_precision) {
TOML_ANON_NAMESPACE::print_floating_point_to_stream(stream, val, format, relaxed_precision);
}
TOML_EXTERNAL_LINKAGE
void TOML_CALLCONV print_to_stream(std::ostream & stream, double val, value_flags format,
bool relaxed_precision) {
TOML_ANON_NAMESPACE::print_floating_point_to_stream(stream, val, format, relaxed_precision);
}
TOML_EXTERNAL_LINKAGE
void TOML_CALLCONV print_to_stream(std::ostream & stream, bool val) {
print_to_stream(stream, val ? "true"sv : "false"sv);
}
TOML_EXTERNAL_LINKAGE
void TOML_CALLCONV print_to_stream(std::ostream & stream, const toml::date& val) {
print_to_stream(stream, val.year, {}, 4);
stream.put('-');
print_to_stream(stream, val.month, {}, 2);
stream.put('-');
print_to_stream(stream, val.day, {}, 2);
}
TOML_EXTERNAL_LINKAGE
void TOML_CALLCONV print_to_stream(std::ostream & stream, const toml::time& val) {
print_to_stream(stream, val.hour, {}, 2);
stream.put(':');
print_to_stream(stream, val.minute, {}, 2);
stream.put(':');
print_to_stream(stream, val.second, {}, 2);
if (val.nanosecond && val.nanosecond <= 999999999u) {
stream.put('.');
auto ns = val.nanosecond;
size_t digits = 9u;
while (ns % 10u == 0u) {
ns /= 10u;
digits--;
}
print_to_stream(stream, ns, {}, digits);
}
}
TOML_EXTERNAL_LINKAGE
void TOML_CALLCONV print_to_stream(std::ostream & stream, const toml::time_offset& val) {
if (!val.minutes) {
stream.put('Z');
return;
}
auto mins = static_cast<int>(val.minutes);
if (mins < 0) {
stream.put('-');
mins = -mins;
} else
stream.put('+');
const auto hours = mins / 60;
if (hours) {
print_to_stream(stream, static_cast<unsigned int>(hours), {}, 2);
mins -= hours * 60;
} else
print_to_stream(stream, "00"sv);
stream.put(':');
print_to_stream(stream, static_cast<unsigned int>(mins), {}, 2);
}
TOML_EXTERNAL_LINKAGE
void TOML_CALLCONV print_to_stream(std::ostream & stream, const toml::date_time& val) {
print_to_stream(stream, val.date);
stream.put('T');
print_to_stream(stream, val.time);
if (val.offset) print_to_stream(stream, *val.offset);
}
TOML_EXTERNAL_LINKAGE
void TOML_CALLCONV print_to_stream(std::ostream & stream, const source_position& val) {
print_to_stream(stream, "line "sv);
print_to_stream(stream, val.line);
print_to_stream(stream, ", column "sv);
print_to_stream(stream, val.column);
}
TOML_EXTERNAL_LINKAGE
void TOML_CALLCONV print_to_stream(std::ostream & stream, const source_region& val) {
print_to_stream(stream, val.begin);
if (val.path) {
print_to_stream(stream, " of '"sv);
print_to_stream(stream, *val.path);
stream.put('\'');
}
}
#if TOML_ENABLE_FORMATTERS
TOML_EXTERNAL_LINKAGE
void TOML_CALLCONV print_to_stream(std::ostream & stream, const array& arr) {
stream << toml_formatter{arr};
}
TOML_EXTERNAL_LINKAGE
void TOML_CALLCONV print_to_stream(std::ostream & stream, const table& tbl) {
stream << toml_formatter{tbl};
}
TOML_EXTERNAL_LINKAGE
void TOML_CALLCONV print_to_stream(std::ostream & stream, const value<std::string>& val) {
stream << toml_formatter{val};
}
TOML_EXTERNAL_LINKAGE
void TOML_CALLCONV print_to_stream(std::ostream & stream, const value<int64_t>& val) {
stream << toml_formatter{val};
}
TOML_EXTERNAL_LINKAGE
void TOML_CALLCONV print_to_stream(std::ostream & stream, const value<double>& val) {
stream << toml_formatter{val};
}
TOML_EXTERNAL_LINKAGE
void TOML_CALLCONV print_to_stream(std::ostream & stream, const value<bool>& val) {
stream << toml_formatter{val};
}
TOML_EXTERNAL_LINKAGE
void TOML_CALLCONV print_to_stream(std::ostream & stream, const value<date>& val) {
stream << toml_formatter{val};
}
TOML_EXTERNAL_LINKAGE
void TOML_CALLCONV print_to_stream(std::ostream & stream, const value<time>& val) {
stream << toml_formatter{val};
}
TOML_EXTERNAL_LINKAGE
void TOML_CALLCONV print_to_stream(std::ostream & stream, const value<date_time>& val) {
stream << toml_formatter{val};
}
#endif
}
TOML_IMPL_NAMESPACE_END;
#include "header_end.hpp"
|