Debian Codegraph @

thirdparty/toml++/toml.hpp

raw symbols SCIP
1 //----------------------------------------------------------------------------------------------------------------------
2 //
3 // toml++ v3.4.0
4 // https://github.com/marzer/tomlplusplus
5 // SPDX-License-Identifier: MIT
6 //
7 //----------------------------------------------------------------------------------------------------------------------
8 //
9 // - THIS FILE WAS ASSEMBLED FROM MULTIPLE HEADER FILES BY A SCRIPT - PLEASE DON'T EDIT IT DIRECTLY -
10 //
11 // If you wish to submit a contribution to toml++, hooray and thanks! Before you crack on, please be aware that this
12 // file was assembled from a number of smaller files by a python script, and code contributions should not be made
13 // against it directly. You should instead make your changes in the relevant source file(s). The file names of the files
14 // that contributed to this header can be found at the beginnings and ends of the corresponding sections of this file.
15 //
16 //----------------------------------------------------------------------------------------------------------------------
17 //
18 // TOML Language Specifications:
19 // latest: https://github.com/toml-lang/toml/blob/master/README.md
20 // v1.0.0: https://toml.io/en/v1.0.0
21 // v0.5.0: https://toml.io/en/v0.5.0
22 // changelog: https://github.com/toml-lang/toml/blob/master/CHANGELOG.md
23 //
24 //----------------------------------------------------------------------------------------------------------------------
25 //
26 // MIT License
27 //
28 // Copyright (c) Mark Gillard <[email protected]>
29 //
30 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
31 // documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
32 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
33 // permit persons to whom the Software is furnished to do so, subject to the following conditions:
34 //
35 // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
36 // Software.
37 //
38 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
39 // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
40 // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
41 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
42 //
43 //----------------------------------------------------------------------------------------------------------------------
44 #ifndef TOMLPLUSPLUS_HPP
45 #define TOMLPLUSPLUS_HPP
46
47 #define INCLUDE_TOMLPLUSPLUS_H // old guard name used pre-v3
48 #define TOMLPLUSPLUS_H // guard name used in the legacy toml.h
49
50 //******** impl/preprocessor.hpp *************************************************************************************
51
52 #ifndef __cplusplus
53 #error toml++ is a C++ library.
54 #endif
55
56 #ifndef TOML_CPP
57 #ifdef _MSVC_LANG
58 #if _MSVC_LANG > __cplusplus
59 #define TOML_CPP _MSVC_LANG
60 #endif
61 #endif
62 #ifndef TOML_CPP
63 #define TOML_CPP __cplusplus
64 #endif
65 #if TOML_CPP >= 202900L
66 #undef TOML_CPP
67 #define TOML_CPP 29
68 #elif TOML_CPP >= 202600L
69 #undef TOML_CPP
70 #define TOML_CPP 26
71 #elif TOML_CPP >= 202302L
72 #undef TOML_CPP
73 #define TOML_CPP 23
74 #elif TOML_CPP >= 202002L
75 #undef TOML_CPP
76 #define TOML_CPP 20
77 #elif TOML_CPP >= 201703L
78 #undef TOML_CPP
79 #define TOML_CPP 17
80 #elif TOML_CPP >= 201402L
81 #undef TOML_CPP
82 #define TOML_CPP 14
83 #elif TOML_CPP >= 201103L
84 #undef TOML_CPP
85 #define TOML_CPP 11
86 #else
87 #undef TOML_CPP
88 #define TOML_CPP 0
89 #endif
90 #endif
91
92 #if !TOML_CPP
93 #error toml++ requires C++17 or higher. For a pre-C++11 TOML library see https://github.com/ToruNiina/Boost.toml
94 #elif TOML_CPP < 17
95 #error toml++ requires C++17 or higher. For a C++11 TOML library see https://github.com/ToruNiina/toml11
96 #endif
97
98 #ifndef TOML_MAKE_VERSION
99 #define TOML_MAKE_VERSION(major, minor, patch) (((major)*10000) + ((minor)*100) + ((patch)))
100 #endif
101
102 #ifndef TOML_INTELLISENSE
103 #ifdef __INTELLISENSE__
104 #define TOML_INTELLISENSE 1
105 #else
106 #define TOML_INTELLISENSE 0
107 #endif
108 #endif
109
110 #ifndef TOML_DOXYGEN
111 #if defined(DOXYGEN) || defined(__DOXYGEN) || defined(__DOXYGEN__) || defined(__doxygen__) || defined(__POXY__) \
112 || defined(__poxy__)
113 #define TOML_DOXYGEN 1
114 #else
115 #define TOML_DOXYGEN 0
116 #endif
117 #endif
118
119 #ifndef TOML_CLANG
120 #ifdef __clang__
121 #define TOML_CLANG __clang_major__
122 #else
123 #define TOML_CLANG 0
124 #endif
125
126 // special handling for apple clang; see:
127 // - https://github.com/marzer/tomlplusplus/issues/189
128 // - https://en.wikipedia.org/wiki/Xcode
129 // -
130 // https://stackoverflow.com/questions/19387043/how-can-i-reliably-detect-the-version-of-clang-at-preprocessing-time
131 #if TOML_CLANG && defined(__apple_build_version__)
132 #undef TOML_CLANG
133 #define TOML_CLANG_VERSION TOML_MAKE_VERSION(__clang_major__, __clang_minor__, __clang_patchlevel__)
134 #if TOML_CLANG_VERSION >= TOML_MAKE_VERSION(15, 0, 0)
135 #define TOML_CLANG 16
136 #elif TOML_CLANG_VERSION >= TOML_MAKE_VERSION(14, 3, 0)
137 #define TOML_CLANG 15
138 #elif TOML_CLANG_VERSION >= TOML_MAKE_VERSION(14, 0, 0)
139 #define TOML_CLANG 14
140 #elif TOML_CLANG_VERSION >= TOML_MAKE_VERSION(13, 1, 6)
141 #define TOML_CLANG 13
142 #elif TOML_CLANG_VERSION >= TOML_MAKE_VERSION(13, 0, 0)
143 #define TOML_CLANG 12
144 #elif TOML_CLANG_VERSION >= TOML_MAKE_VERSION(12, 0, 5)
145 #define TOML_CLANG 11
146 #elif TOML_CLANG_VERSION >= TOML_MAKE_VERSION(12, 0, 0)
147 #define TOML_CLANG 10
148 #elif TOML_CLANG_VERSION >= TOML_MAKE_VERSION(11, 0, 3)
149 #define TOML_CLANG 9
150 #elif TOML_CLANG_VERSION >= TOML_MAKE_VERSION(11, 0, 0)
151 #define TOML_CLANG 8
152 #elif TOML_CLANG_VERSION >= TOML_MAKE_VERSION(10, 0, 1)
153 #define TOML_CLANG 7
154 #else
155 #define TOML_CLANG 6 // not strictly correct but doesn't matter below this
156 #endif
157 #undef TOML_CLANG_VERSION
158 #endif
159 #endif
160
161 #ifndef TOML_ICC
162 #ifdef __INTEL_COMPILER
163 #define TOML_ICC __INTEL_COMPILER
164 #ifdef __ICL
165 #define TOML_ICC_CL TOML_ICC
166 #else
167 #define TOML_ICC_CL 0
168 #endif
169 #else
170 #define TOML_ICC 0
171 #define TOML_ICC_CL 0
172 #endif
173 #endif
174
175 #ifndef TOML_MSVC_LIKE
176 #ifdef _MSC_VER
177 #define TOML_MSVC_LIKE _MSC_VER
178 #else
179 #define TOML_MSVC_LIKE 0
180 #endif
181 #endif
182
183 #ifndef TOML_MSVC
184 #if TOML_MSVC_LIKE && !TOML_CLANG && !TOML_ICC
185 #define TOML_MSVC TOML_MSVC_LIKE
186 #else
187 #define TOML_MSVC 0
188 #endif
189 #endif
190
191 #ifndef TOML_GCC_LIKE
192 #ifdef __GNUC__
193 #define TOML_GCC_LIKE __GNUC__
194 #else
195 #define TOML_GCC_LIKE 0
196 #endif
197 #endif
198
199 #ifndef TOML_GCC
200 #if TOML_GCC_LIKE && !TOML_CLANG && !TOML_ICC
201 #define TOML_GCC TOML_GCC_LIKE
202 #else
203 #define TOML_GCC 0
204 #endif
205 #endif
206
207 #ifndef TOML_CUDA
208 #if defined(__CUDACC__) || defined(__CUDA_ARCH__) || defined(__CUDA_LIBDEVICE__)
209 #define TOML_CUDA 1
210 #else
211 #define TOML_CUDA 0
212 #endif
213 #endif
214
215 #ifndef TOML_NVCC
216 #ifdef __NVCOMPILER_MAJOR__
217 #define TOML_NVCC __NVCOMPILER_MAJOR__
218 #else
219 #define TOML_NVCC 0
220 #endif
221 #endif
222
223 #ifndef TOML_ARCH_ITANIUM
224 #if defined(__ia64__) || defined(__ia64) || defined(_IA64) || defined(__IA64__) || defined(_M_IA64)
225 #define TOML_ARCH_ITANIUM 1
226 #define TOML_ARCH_BITNESS 64
227 #else
228 #define TOML_ARCH_ITANIUM 0
229 #endif
230 #endif
231
232 #ifndef TOML_ARCH_AMD64
233 #if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_AMD64)
234 #define TOML_ARCH_AMD64 1
235 #define TOML_ARCH_BITNESS 64
236 #else
237 #define TOML_ARCH_AMD64 0
238 #endif
239 #endif
240
241 #ifndef TOML_ARCH_X86
242 #if defined(__i386__) || defined(_M_IX86)
243 #define TOML_ARCH_X86 1
244 #define TOML_ARCH_BITNESS 32
245 #else
246 #define TOML_ARCH_X86 0
247 #endif
248 #endif
249
250 #ifndef TOML_ARCH_ARM
251 #if defined(__aarch64__) || defined(__ARM_ARCH_ISA_A64) || defined(_M_ARM64) || defined(__ARM_64BIT_STATE) \
252 || defined(_M_ARM64EC)
253 #define TOML_ARCH_ARM32 0
254 #define TOML_ARCH_ARM64 1
255 #define TOML_ARCH_ARM 1
256 #define TOML_ARCH_BITNESS 64
257 #elif defined(__arm__) || defined(_M_ARM) || defined(__ARM_32BIT_STATE)
258 #define TOML_ARCH_ARM32 1
259 #define TOML_ARCH_ARM64 0
260 #define TOML_ARCH_ARM 1
261 #define TOML_ARCH_BITNESS 32
262 #else
263 #define TOML_ARCH_ARM32 0
264 #define TOML_ARCH_ARM64 0
265 #define TOML_ARCH_ARM 0
266 #endif
267 #endif
268
269 #ifndef TOML_ARCH_BITNESS
270 #define TOML_ARCH_BITNESS 0
271 #endif
272
273 #ifndef TOML_ARCH_X64
274 #if TOML_ARCH_BITNESS == 64
275 #define TOML_ARCH_X64 1
276 #else
277 #define TOML_ARCH_X64 0
278 #endif
279 #endif
280
281 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) || defined(__CYGWIN__)
282 #define TOML_WINDOWS 1
283 #else
284 #define TOML_WINDOWS 0
285 #endif
286
287 #ifdef __unix__
288 #define TOML_UNIX 1
289 #else
290 #define TOML_UNIX 0
291 #endif
292
293 #ifdef __linux__
294 #define TOML_LINUX 1
295 #else
296 #define TOML_LINUX 0
297 #endif
298
299 // TOML_HAS_INCLUDE
300 #ifndef TOML_HAS_INCLUDE
301 #ifdef __has_include
302 #define TOML_HAS_INCLUDE(header) __has_include(header)
303 #else
304 #define TOML_HAS_INCLUDE(header) 0
305 #endif
306 #endif
307
308 // TOML_HAS_BUILTIN
309 #ifndef TOML_HAS_BUILTIN
310 #ifdef __has_builtin
311 #define TOML_HAS_BUILTIN(name) __has_builtin(name)
312 #else
313 #define TOML_HAS_BUILTIN(name) 0
314 #endif
315 #endif
316
317 // TOML_HAS_FEATURE
318 #ifndef TOML_HAS_FEATURE
319 #ifdef __has_feature
320 #define TOML_HAS_FEATURE(name) __has_feature(name)
321 #else
322 #define TOML_HAS_FEATURE(name) 0
323 #endif
324 #endif
325
326 // TOML_HAS_ATTR
327 #ifndef TOML_HAS_ATTR
328 #ifdef __has_attribute
329 #define TOML_HAS_ATTR(attr) __has_attribute(attr)
330 #else
331 #define TOML_HAS_ATTR(attr) 0
332 #endif
333 #endif
334
335 // TOML_HAS_CPP_ATTR
336 #ifndef TOML_HAS_CPP_ATTR
337 #ifdef __has_cpp_attribute
338 #define TOML_HAS_CPP_ATTR(attr) __has_cpp_attribute(attr)
339 #else
340 #define TOML_HAS_CPP_ATTR(attr) 0
341 #endif
342 #endif
343
344 // TOML_ATTR (gnu attributes)
345 #ifndef TOML_ATTR
346 #if TOML_CLANG || TOML_GCC_LIKE
347 #define TOML_ATTR(...) __attribute__((__VA_ARGS__))
348 #else
349 #define TOML_ATTR(...)
350 #endif
351 #endif
352
353 // TOML_DECLSPEC (msvc attributes)
354 #ifndef TOML_DECLSPEC
355 #if TOML_MSVC_LIKE
356 #define TOML_DECLSPEC(...) __declspec(__VA_ARGS__)
357 #else
358 #define TOML_DECLSPEC(...)
359 #endif
360 #endif
361
362 // TOML_COMPILER_HAS_EXCEPTIONS
363 #ifndef TOML_COMPILER_HAS_EXCEPTIONS
364 #if defined(__EXCEPTIONS) || defined(_CPPUNWIND) || defined(__cpp_exceptions)
365 #define TOML_COMPILER_HAS_EXCEPTIONS 1
366 #else
367 #define TOML_COMPILER_HAS_EXCEPTIONS 0
368 #endif
369 #endif
370
371 // TOML_COMPILER_HAS_RTTI
372 #ifndef TOML_COMPILER_HAS_RTTI
373 #if defined(_CPPRTTI) || defined(__GXX_RTTI) || TOML_HAS_FEATURE(cxx_rtti)
374 #define TOML_COMPILER_HAS_RTTI 1
375 #else
376 #define TOML_COMPILER_HAS_RTTI 0
377 #endif
378 #endif
379
380 // TOML_CONCAT
381 #define TOML_CONCAT_1(x, y) x##y
382 #define TOML_CONCAT(x, y) TOML_CONCAT_1(x, y)
383
384 // TOML_MAKE_STRING
385 #define TOML_MAKE_STRING_1(s) #s
386 #define TOML_MAKE_STRING(s) TOML_MAKE_STRING_1(s)
387
388 // TOML_PRAGMA_XXXX (compiler-specific pragmas)
389 #if TOML_CLANG
390 #define TOML_PRAGMA_CLANG(decl) _Pragma(TOML_MAKE_STRING(clang decl))
391 #else
392 #define TOML_PRAGMA_CLANG(decl)
393 #endif
394 #if TOML_CLANG >= 8
395 #define TOML_PRAGMA_CLANG_GE_8(decl) TOML_PRAGMA_CLANG(decl)
396 #else
397 #define TOML_PRAGMA_CLANG_GE_8(decl)
398 #endif
399 #if TOML_CLANG >= 9
400 #define TOML_PRAGMA_CLANG_GE_9(decl) TOML_PRAGMA_CLANG(decl)
401 #else
402 #define TOML_PRAGMA_CLANG_GE_9(decl)
403 #endif
404 #if TOML_CLANG >= 10
405 #define TOML_PRAGMA_CLANG_GE_10(decl) TOML_PRAGMA_CLANG(decl)
406 #else
407 #define TOML_PRAGMA_CLANG_GE_10(decl)
408 #endif
409 #if TOML_CLANG >= 11
410 #define TOML_PRAGMA_CLANG_GE_11(decl) TOML_PRAGMA_CLANG(decl)
411 #else
412 #define TOML_PRAGMA_CLANG_GE_11(decl)
413 #endif
414 #if TOML_GCC
415 #define TOML_PRAGMA_GCC(decl) _Pragma(TOML_MAKE_STRING(GCC decl))
416 #else
417 #define TOML_PRAGMA_GCC(decl)
418 #endif
419 #if TOML_MSVC
420 #define TOML_PRAGMA_MSVC(...) __pragma(__VA_ARGS__)
421 #else
422 #define TOML_PRAGMA_MSVC(...)
423 #endif
424 #if TOML_ICC
425 #define TOML_PRAGMA_ICC(...) __pragma(__VA_ARGS__)
426 #else
427 #define TOML_PRAGMA_ICC(...)
428 #endif
429
430 // TOML_ALWAYS_INLINE
431 #ifndef TOML_ALWAYS_INLINE
432 #ifdef _MSC_VER
433 #define TOML_ALWAYS_INLINE __forceinline
434 #elif TOML_GCC || TOML_CLANG || TOML_HAS_ATTR(__always_inline__)
435 #define TOML_ALWAYS_INLINE \
436 TOML_ATTR(__always_inline__) \
437 inline
438 #else
439 #define TOML_ALWAYS_INLINE inline
440 #endif
441 #endif
442
443 // TOML_NEVER_INLINE
444 #ifndef TOML_NEVER_INLINE
445 #ifdef _MSC_VER
446 #define TOML_NEVER_INLINE TOML_DECLSPEC(noinline)
447 #elif TOML_CUDA // https://gitlab.gnome.org/GNOME/glib/-/issues/2555
448 #define TOML_NEVER_INLINE TOML_ATTR(noinline)
449 #else
450 #if TOML_GCC || TOML_CLANG || TOML_HAS_ATTR(__noinline__)
451 #define TOML_NEVER_INLINE TOML_ATTR(__noinline__)
452 #endif
453 #endif
454 #ifndef TOML_NEVER_INLINE
455 #define TOML_NEVER_INLINE
456 #endif
457 #endif
458
459 // MSVC attributes
460 #ifndef TOML_ABSTRACT_INTERFACE
461 #define TOML_ABSTRACT_INTERFACE TOML_DECLSPEC(novtable)
462 #endif
463 #ifndef TOML_EMPTY_BASES
464 #define TOML_EMPTY_BASES TOML_DECLSPEC(empty_bases)
465 #endif
466
467 // TOML_TRIVIAL_ABI
468 #ifndef TOML_TRIVIAL_ABI
469 #if TOML_CLANG || TOML_HAS_ATTR(__trivial_abi__)
470 #define TOML_TRIVIAL_ABI TOML_ATTR(__trivial_abi__)
471 #else
472 #define TOML_TRIVIAL_ABI
473 #endif
474 #endif
475
476 // TOML_NODISCARD
477 #ifndef TOML_NODISCARD
478 #if TOML_CPP >= 17 && TOML_HAS_CPP_ATTR(nodiscard) >= 201603
479 #define TOML_NODISCARD [[nodiscard]]
480 #elif TOML_CLANG || TOML_GCC || TOML_HAS_ATTR(__warn_unused_result__)
481 #define TOML_NODISCARD TOML_ATTR(__warn_unused_result__)
482 #else
483 #define TOML_NODISCARD
484 #endif
485 #endif
486
487 // TOML_NODISCARD_CTOR
488 #ifndef TOML_NODISCARD_CTOR
489 #if TOML_CPP >= 17 && TOML_HAS_CPP_ATTR(nodiscard) >= 201907
490 #define TOML_NODISCARD_CTOR [[nodiscard]]
491 #else
492 #define TOML_NODISCARD_CTOR
493 #endif
494 #endif
495
496 // pure + const
497 #ifndef TOML_PURE
498 #ifdef NDEBUG
499 #define TOML_PURE \
500 TOML_DECLSPEC(noalias) \
501 TOML_ATTR(pure)
502 #else
503 #define TOML_PURE
504 #endif
505 #endif
506 #ifndef TOML_CONST
507 #ifdef NDEBUG
508 #define TOML_CONST \
509 TOML_DECLSPEC(noalias) \
510 TOML_ATTR(const)
511 #else
512 #define TOML_CONST
513 #endif
514 #endif
515 #ifndef TOML_INLINE_GETTER
516 #define TOML_INLINE_GETTER \
517 TOML_NODISCARD \
518 TOML_ALWAYS_INLINE
519 #endif
520 #ifndef TOML_PURE_GETTER
521 #define TOML_PURE_GETTER \
522 TOML_NODISCARD \
523 TOML_PURE
524 #endif
525 #ifndef TOML_PURE_INLINE_GETTER
526 #define TOML_PURE_INLINE_GETTER \
527 TOML_NODISCARD \
528 TOML_ALWAYS_INLINE \
529 TOML_PURE
530 #endif
531 #ifndef TOML_CONST_GETTER
532 #define TOML_CONST_GETTER \
533 TOML_NODISCARD \
534 TOML_CONST
535 #endif
536 #ifndef TOML_CONST_INLINE_GETTER
537 #define TOML_CONST_INLINE_GETTER \
538 TOML_NODISCARD \
539 TOML_ALWAYS_INLINE \
540 TOML_CONST
541 #endif
542
543 // TOML_ASSUME
544 #ifndef TOML_ASSUME
545 #ifdef _MSC_VER
546 #define TOML_ASSUME(expr) __assume(expr)
547 #elif TOML_ICC || TOML_CLANG || TOML_HAS_BUILTIN(__builtin_assume)
548 #define TOML_ASSUME(expr) __builtin_assume(expr)
549 #elif TOML_HAS_CPP_ATTR(assume) >= 202207
550 #define TOML_ASSUME(expr) [[assume(expr)]]
551 #elif TOML_HAS_ATTR(__assume__)
552 #define TOML_ASSUME(expr) __attribute__((__assume__(expr)))
553 #else
554 #define TOML_ASSUME(expr) static_cast<void>(0)
555 #endif
556 #endif
557
558 // TOML_UNREACHABLE
559 #ifndef TOML_UNREACHABLE
560 #ifdef _MSC_VER
561 #define TOML_UNREACHABLE __assume(0)
562 #elif TOML_ICC || TOML_CLANG || TOML_GCC || TOML_HAS_BUILTIN(__builtin_unreachable)
563 #define TOML_UNREACHABLE __builtin_unreachable()
564 #else
565 #define TOML_UNREACHABLE static_cast<void>(0)
566 #endif
567 #endif
568
569 // TOML_LIKELY
570 #if TOML_CPP >= 20 && TOML_HAS_CPP_ATTR(likely) >= 201803
571 #define TOML_LIKELY(...) (__VA_ARGS__) [[likely]]
572 #define TOML_LIKELY_CASE [[likely]]
573 #elif TOML_GCC || TOML_CLANG || TOML_HAS_BUILTIN(__builtin_expect)
574 #define TOML_LIKELY(...) (__builtin_expect(!!(__VA_ARGS__), 1))
575 #else
576 #define TOML_LIKELY(...) (__VA_ARGS__)
577 #endif
578 #ifndef TOML_LIKELY_CASE
579 #define TOML_LIKELY_CASE
580 #endif
581
582 // TOML_UNLIKELY
583 #if TOML_CPP >= 20 && TOML_HAS_CPP_ATTR(unlikely) >= 201803
584 #define TOML_UNLIKELY(...) (__VA_ARGS__) [[unlikely]]
585 #define TOML_UNLIKELY_CASE [[unlikely]]
586 #elif TOML_GCC || TOML_CLANG || TOML_HAS_BUILTIN(__builtin_expect)
587 #define TOML_UNLIKELY(...) (__builtin_expect(!!(__VA_ARGS__), 0))
588 #else
589 #define TOML_UNLIKELY(...) (__VA_ARGS__)
590 #endif
591 #ifndef TOML_UNLIKELY_CASE
592 #define TOML_UNLIKELY_CASE
593 #endif
594
595 // TOML_FLAGS_ENUM
596 #if TOML_CLANG || TOML_HAS_ATTR(flag_enum)
597 #define TOML_FLAGS_ENUM __attribute__((flag_enum))
598 #else
599 #define TOML_FLAGS_ENUM
600 #endif
601
602 // TOML_OPEN_ENUM + TOML_CLOSED_ENUM
603 #if TOML_CLANG || TOML_HAS_ATTR(enum_extensibility)
604 #define TOML_OPEN_ENUM __attribute__((enum_extensibility(open)))
605 #define TOML_CLOSED_ENUM __attribute__((enum_extensibility(closed)))
606 #else
607 #define TOML_OPEN_ENUM
608 #define TOML_CLOSED_ENUM
609 #endif
610
611 // TOML_OPEN_FLAGS_ENUM + TOML_CLOSED_FLAGS_ENUM
612 #define TOML_OPEN_FLAGS_ENUM TOML_OPEN_ENUM TOML_FLAGS_ENUM
613 #define TOML_CLOSED_FLAGS_ENUM TOML_CLOSED_ENUM TOML_FLAGS_ENUM
614
615 // TOML_MAKE_FLAGS
616 #define TOML_MAKE_FLAGS_2(T, op, linkage) \
617 TOML_CONST_INLINE_GETTER \
618 linkage constexpr T operator op(T lhs, T rhs) noexcept \
619 { \
620 using under = std::underlying_type_t<T>; \
621 return static_cast<T>(static_cast<under>(lhs) op static_cast<under>(rhs)); \
622 } \
623 \
624 linkage constexpr T& operator TOML_CONCAT(op, =)(T & lhs, T rhs) noexcept \
625 { \
626 return lhs = (lhs op rhs); \
627 } \
628 \
629 static_assert(true)
630 #define TOML_MAKE_FLAGS_1(T, linkage) \
631 static_assert(std::is_enum_v<T>); \
632 \
633 TOML_MAKE_FLAGS_2(T, &, linkage); \
634 TOML_MAKE_FLAGS_2(T, |, linkage); \
635 TOML_MAKE_FLAGS_2(T, ^, linkage); \
636 \
637 TOML_CONST_INLINE_GETTER \
638 linkage constexpr T operator~(T val) noexcept \
639 { \
640 using under = std::underlying_type_t<T>; \
641 return static_cast<T>(~static_cast<under>(val)); \
642 } \
643 \
644 TOML_CONST_INLINE_GETTER \
645 linkage constexpr bool operator!(T val) noexcept \
646 { \
647 using under = std::underlying_type_t<T>; \
648 return !static_cast<under>(val); \
649 } \
650 \
651 static_assert(true)
652 #define TOML_MAKE_FLAGS(T) TOML_MAKE_FLAGS_1(T, )
653
654 #define TOML_UNUSED(...) static_cast<void>(__VA_ARGS__)
655
656 #define TOML_DELETE_DEFAULTS(T) \
657 T(const T&) = delete; \
658 T(T&&) = delete; \
659 T& operator=(const T&) = delete; \
660 T& operator=(T&&) = delete
661
662 #define TOML_ASYMMETRICAL_EQUALITY_OPS(LHS, RHS, ...) \
663 __VA_ARGS__ TOML_NODISCARD \
664 friend bool operator==(RHS rhs, LHS lhs) noexcept \
665 { \
666 return lhs == rhs; \
667 } \
668 __VA_ARGS__ TOML_NODISCARD \
669 friend bool operator!=(LHS lhs, RHS rhs) noexcept \
670 { \
671 return !(lhs == rhs); \
672 } \
673 __VA_ARGS__ TOML_NODISCARD \
674 friend bool operator!=(RHS rhs, LHS lhs) noexcept \
675 { \
676 return !(lhs == rhs); \
677 } \
678 static_assert(true)
679
680 #define TOML_EVAL_BOOL_1(T, F) T
681 #define TOML_EVAL_BOOL_0(T, F) F
682
683 #if !defined(__POXY__) && !defined(POXY_IMPLEMENTATION_DETAIL)
684 #define POXY_IMPLEMENTATION_DETAIL(...) __VA_ARGS__
685 #endif
686
687 // COMPILER-SPECIFIC WARNING MANAGEMENT
688
689 #if TOML_CLANG
690
691 #define TOML_PUSH_WARNINGS \
692 TOML_PRAGMA_CLANG(diagnostic push) \
693 TOML_PRAGMA_CLANG(diagnostic ignored "-Wunknown-warning-option") \
694 static_assert(true)
695
696 #define TOML_DISABLE_SWITCH_WARNINGS \
697 TOML_PRAGMA_CLANG(diagnostic ignored "-Wswitch") \
698 static_assert(true)
699
700 #define TOML_DISABLE_ARITHMETIC_WARNINGS \
701 TOML_PRAGMA_CLANG_GE_10(diagnostic ignored "-Wimplicit-int-float-conversion") \
702 TOML_PRAGMA_CLANG(diagnostic ignored "-Wfloat-equal") \
703 TOML_PRAGMA_CLANG(diagnostic ignored "-Wdouble-promotion") \
704 TOML_PRAGMA_CLANG(diagnostic ignored "-Wchar-subscripts") \
705 TOML_PRAGMA_CLANG(diagnostic ignored "-Wshift-sign-overflow") \
706 static_assert(true)
707
708 #define TOML_DISABLE_SPAM_WARNINGS \
709 TOML_PRAGMA_CLANG_GE_8(diagnostic ignored "-Wdefaulted-function-deleted") \
710 TOML_PRAGMA_CLANG_GE_9(diagnostic ignored "-Wctad-maybe-unsupported") \
711 TOML_PRAGMA_CLANG_GE_10(diagnostic ignored "-Wzero-as-null-pointer-constant") \
712 TOML_PRAGMA_CLANG_GE_11(diagnostic ignored "-Wsuggest-destructor-override") \
713 TOML_PRAGMA_CLANG(diagnostic ignored "-Wweak-vtables") \
714 TOML_PRAGMA_CLANG(diagnostic ignored "-Wweak-template-vtables") \
715 TOML_PRAGMA_CLANG(diagnostic ignored "-Wdouble-promotion") \
716 TOML_PRAGMA_CLANG(diagnostic ignored "-Wchar-subscripts") \
717 TOML_PRAGMA_CLANG(diagnostic ignored "-Wmissing-field-initializers") \
718 TOML_PRAGMA_CLANG(diagnostic ignored "-Wpadded") \
719 static_assert(true)
720
721 #define TOML_POP_WARNINGS \
722 TOML_PRAGMA_CLANG(diagnostic pop) \
723 static_assert(true)
724
725 #define TOML_DISABLE_WARNINGS \
726 TOML_PRAGMA_CLANG(diagnostic push) \
727 TOML_PRAGMA_CLANG(diagnostic ignored "-Weverything") \
728 static_assert(true, "")
729
730 #define TOML_ENABLE_WARNINGS \
731 TOML_PRAGMA_CLANG(diagnostic pop) \
732 static_assert(true)
733
734 #define TOML_SIMPLE_STATIC_ASSERT_MESSAGES 1
735
736 #elif TOML_MSVC
737
738 #define TOML_PUSH_WARNINGS \
739 __pragma(warning(push)) \
740 static_assert(true)
741
742 #if TOML_HAS_INCLUDE(<CodeAnalysis/Warnings.h>)
743 #pragma warning(push, 0)
744 #include <CodeAnalysis/Warnings.h>
745 #pragma warning(pop)
746 #define TOML_DISABLE_CODE_ANALYSIS_WARNINGS \
747 __pragma(warning(disable : ALL_CODE_ANALYSIS_WARNINGS)) \
748 static_assert(true)
749 #else
750 #define TOML_DISABLE_CODE_ANALYSIS_WARNINGS static_assert(true)
751 #endif
752
753 #define TOML_DISABLE_SWITCH_WARNINGS \
754 __pragma(warning(disable : 4061)) \
755 __pragma(warning(disable : 4062)) \
756 __pragma(warning(disable : 4063)) \
757 __pragma(warning(disable : 5262)) /* switch-case implicit fallthrough (false-positive) */ \
758 __pragma(warning(disable : 26819)) /* cg: unannotated fallthrough */ \
759 static_assert(true)
760
761 #define TOML_DISABLE_SPAM_WARNINGS \
762 __pragma(warning(disable : 4127)) /* conditional expr is constant */ \
763 __pragma(warning(disable : 4324)) /* structure was padded due to alignment specifier */ \
764 __pragma(warning(disable : 4348)) \
765 __pragma(warning(disable : 4464)) /* relative include path contains '..' */ \
766 __pragma(warning(disable : 4505)) /* unreferenced local function removed */ \
767 __pragma(warning(disable : 4514)) /* unreferenced inline function has been removed */ \
768 __pragma(warning(disable : 4582)) /* constructor is not implicitly called */ \
769 __pragma(warning(disable : 4619)) /* there is no warning number 'XXXX' */ \
770 __pragma(warning(disable : 4623)) /* default constructor was implicitly defined as deleted */ \
771 __pragma(warning(disable : 4625)) /* copy constructor was implicitly defined as deleted */ \
772 __pragma(warning(disable : 4626)) /* assignment operator was implicitly defined as deleted */ \
773 __pragma(warning(disable : 4710)) /* function not inlined */ \
774 __pragma(warning(disable : 4711)) /* function selected for automatic expansion */ \
775 __pragma(warning(disable : 4820)) /* N bytes padding added */ \
776 __pragma(warning(disable : 4946)) /* reinterpret_cast used between related classes */ \
777 __pragma(warning(disable : 5026)) /* move constructor was implicitly defined as deleted */ \
778 __pragma(warning(disable : 5027)) /* move assignment operator was implicitly defined as deleted */ \
779 __pragma(warning(disable : 5039)) /* potentially throwing function passed to 'extern "C"' function */ \
780 __pragma(warning(disable : 5045)) /* Compiler will insert Spectre mitigation */ \
781 __pragma(warning(disable : 5264)) /* const variable is not used (false-positive) */ \
782 __pragma(warning(disable : 26451)) \
783 __pragma(warning(disable : 26490)) \
784 __pragma(warning(disable : 26495)) \
785 __pragma(warning(disable : 26812)) \
786 __pragma(warning(disable : 26819)) \
787 static_assert(true)
788
789 #define TOML_DISABLE_ARITHMETIC_WARNINGS \
790 __pragma(warning(disable : 4365)) /* argument signed/unsigned mismatch */ \
791 __pragma(warning(disable : 4738)) /* storing 32-bit float result in memory */ \
792 __pragma(warning(disable : 5219)) /* implicit conversion from integral to float */ \
793 static_assert(true)
794
795 #define TOML_POP_WARNINGS \
796 __pragma(warning(pop)) \
797 static_assert(true)
798
799 #define TOML_DISABLE_WARNINGS \
800 __pragma(warning(push, 0)) \
801 __pragma(warning(disable : 4348)) \
802 __pragma(warning(disable : 4668)) \
803 __pragma(warning(disable : 5105)) \
804 __pragma(warning(disable : 5264)) \
805 TOML_DISABLE_CODE_ANALYSIS_WARNINGS; \
806 TOML_DISABLE_SWITCH_WARNINGS; \
807 TOML_DISABLE_SPAM_WARNINGS; \
808 TOML_DISABLE_ARITHMETIC_WARNINGS; \
809 static_assert(true)
810
811 #define TOML_ENABLE_WARNINGS TOML_POP_WARNINGS
812
813 #elif TOML_ICC
814
815 #define TOML_PUSH_WARNINGS \
816 __pragma(warning(push)) \
817 static_assert(true)
818
819 #define TOML_DISABLE_SPAM_WARNINGS \
820 __pragma(warning(disable : 82)) /* storage class is not first */ \
821 __pragma(warning(disable : 111)) /* statement unreachable (false-positive) */ \
822 __pragma(warning(disable : 869)) /* unreferenced parameter */ \
823 __pragma(warning(disable : 1011)) /* missing return (false-positive) */ \
824 __pragma(warning(disable : 2261)) /* assume expr side-effects discarded */ \
825 static_assert(true)
826
827 #define TOML_POP_WARNINGS \
828 __pragma(warning(pop)) \
829 static_assert(true)
830
831 #define TOML_DISABLE_WARNINGS \
832 __pragma(warning(push, 0)) \
833 TOML_DISABLE_SPAM_WARNINGS
834
835 #define TOML_ENABLE_WARNINGS \
836 __pragma(warning(pop)) \
837 static_assert(true)
838
839 #elif TOML_GCC
840
841 #define TOML_PUSH_WARNINGS \
842 TOML_PRAGMA_GCC(diagnostic push) \
843 static_assert(true)
844
845 #define TOML_DISABLE_SWITCH_WARNINGS \
846 TOML_PRAGMA_GCC(diagnostic ignored "-Wswitch") \
847 TOML_PRAGMA_GCC(diagnostic ignored "-Wswitch-enum") \
848 TOML_PRAGMA_GCC(diagnostic ignored "-Wswitch-default") \
849 static_assert(true)
850
851 #define TOML_DISABLE_ARITHMETIC_WARNINGS \
852 TOML_PRAGMA_GCC(diagnostic ignored "-Wfloat-equal") \
853 TOML_PRAGMA_GCC(diagnostic ignored "-Wsign-conversion") \
854 TOML_PRAGMA_GCC(diagnostic ignored "-Wchar-subscripts") \
855 static_assert(true)
856
857 #define TOML_DISABLE_SUGGEST_ATTR_WARNINGS \
858 TOML_PRAGMA_GCC(diagnostic ignored "-Wsuggest-attribute=const") \
859 TOML_PRAGMA_GCC(diagnostic ignored "-Wsuggest-attribute=pure") \
860 static_assert(true)
861
862 #define TOML_DISABLE_SPAM_WARNINGS \
863 TOML_PRAGMA_GCC(diagnostic ignored "-Wpadded") \
864 TOML_PRAGMA_GCC(diagnostic ignored "-Wcast-align") \
865 TOML_PRAGMA_GCC(diagnostic ignored "-Wcomment") \
866 TOML_PRAGMA_GCC(diagnostic ignored "-Wtype-limits") \
867 TOML_PRAGMA_GCC(diagnostic ignored "-Wuseless-cast") \
868 TOML_PRAGMA_GCC(diagnostic ignored "-Wchar-subscripts") \
869 TOML_PRAGMA_GCC(diagnostic ignored "-Wsubobject-linkage") \
870 TOML_PRAGMA_GCC(diagnostic ignored "-Wmissing-field-initializers") \
871 TOML_PRAGMA_GCC(diagnostic ignored "-Wmaybe-uninitialized") \
872 TOML_PRAGMA_GCC(diagnostic ignored "-Wnoexcept") \
873 TOML_PRAGMA_GCC(diagnostic ignored "-Wnull-dereference") \
874 TOML_PRAGMA_GCC(diagnostic ignored "-Wduplicated-branches") \
875 static_assert(true)
876
877 #define TOML_POP_WARNINGS \
878 TOML_PRAGMA_GCC(diagnostic pop) \
879 static_assert(true)
880
881 #define TOML_DISABLE_WARNINGS \
882 TOML_PRAGMA_GCC(diagnostic push) \
883 TOML_PRAGMA_GCC(diagnostic ignored "-Wall") \
884 TOML_PRAGMA_GCC(diagnostic ignored "-Wextra") \
885 TOML_PRAGMA_GCC(diagnostic ignored "-Wpedantic") \
886 TOML_DISABLE_SWITCH_WARNINGS; \
887 TOML_DISABLE_ARITHMETIC_WARNINGS; \
888 TOML_DISABLE_SUGGEST_ATTR_WARNINGS; \
889 TOML_DISABLE_SPAM_WARNINGS; \
890 static_assert(true)
891
892 #define TOML_ENABLE_WARNINGS \
893 TOML_PRAGMA_GCC(diagnostic pop) \
894 static_assert(true)
895
896 #endif
897
898 #ifndef TOML_PUSH_WARNINGS
899 #define TOML_PUSH_WARNINGS static_assert(true)
900 #endif
901 #ifndef TOML_DISABLE_CODE_ANALYSIS_WARNINGS
902 #define TOML_DISABLE_CODE_ANALYSIS_WARNINGS static_assert(true)
903 #endif
904 #ifndef TOML_DISABLE_SWITCH_WARNINGS
905 #define TOML_DISABLE_SWITCH_WARNINGS static_assert(true)
906 #endif
907 #ifndef TOML_DISABLE_SUGGEST_ATTR_WARNINGS
908 #define TOML_DISABLE_SUGGEST_ATTR_WARNINGS static_assert(true)
909 #endif
910 #ifndef TOML_DISABLE_SPAM_WARNINGS
911 #define TOML_DISABLE_SPAM_WARNINGS static_assert(true)
912 #endif
913 #ifndef TOML_DISABLE_ARITHMETIC_WARNINGS
914 #define TOML_DISABLE_ARITHMETIC_WARNINGS static_assert(true)
915 #endif
916 #ifndef TOML_POP_WARNINGS
917 #define TOML_POP_WARNINGS static_assert(true)
918 #endif
919 #ifndef TOML_DISABLE_WARNINGS
920 #define TOML_DISABLE_WARNINGS static_assert(true)
921 #endif
922 #ifndef TOML_ENABLE_WARNINGS
923 #define TOML_ENABLE_WARNINGS static_assert(true)
924 #endif
925 #ifndef TOML_SIMPLE_STATIC_ASSERT_MESSAGES
926 #define TOML_SIMPLE_STATIC_ASSERT_MESSAGES 0
927 #endif
928
929 #ifdef TOML_CONFIG_HEADER
930 #include TOML_CONFIG_HEADER
931 #endif
932
933 // is the library being built as a shared lib/dll using meson and friends?
934 #ifndef TOML_SHARED_LIB
935 #define TOML_SHARED_LIB 0
936 #endif
937
938 // header-only mode
939 #if !defined(TOML_HEADER_ONLY) && defined(TOML_ALL_INLINE) // was TOML_ALL_INLINE pre-2.0
940 #define TOML_HEADER_ONLY TOML_ALL_INLINE
941 #endif
942 #if !defined(TOML_HEADER_ONLY) || (defined(TOML_HEADER_ONLY) && TOML_HEADER_ONLY) || TOML_INTELLISENSE
943 #undef TOML_HEADER_ONLY
944 #define TOML_HEADER_ONLY 1
945 #endif
946 #if TOML_DOXYGEN || TOML_SHARED_LIB
947 #undef TOML_HEADER_ONLY
948 #define TOML_HEADER_ONLY 0
949 #endif
950
951 // internal implementation switch
952 #if defined(TOML_IMPLEMENTATION) || TOML_HEADER_ONLY
953 #undef TOML_IMPLEMENTATION
954 #define TOML_IMPLEMENTATION 1
955 #else
956 #define TOML_IMPLEMENTATION 0
957 #endif
958
959 // dll/shared lib function exports (legacy - TOML_API was the old name for this setting)
960 #if !defined(TOML_EXPORTED_MEMBER_FUNCTION) && !defined(TOML_EXPORTED_STATIC_FUNCTION) \
961 && !defined(TOML_EXPORTED_FREE_FUNCTION) && !defined(TOML_EXPORTED_CLASS) && defined(TOML_API)
962 #define TOML_EXPORTED_MEMBER_FUNCTION TOML_API
963 #define TOML_EXPORTED_STATIC_FUNCTION TOML_API
964 #define TOML_EXPORTED_FREE_FUNCTION TOML_API
965 #endif
966
967 // dll/shared lib exports
968 #if TOML_SHARED_LIB
969 #undef TOML_API
970 #undef TOML_EXPORTED_CLASS
971 #undef TOML_EXPORTED_MEMBER_FUNCTION
972 #undef TOML_EXPORTED_STATIC_FUNCTION
973 #undef TOML_EXPORTED_FREE_FUNCTION
974 #if TOML_WINDOWS
975 #if TOML_IMPLEMENTATION
976 #define TOML_EXPORTED_CLASS __declspec(dllexport)
977 #define TOML_EXPORTED_FREE_FUNCTION __declspec(dllexport)
978 #else
979 #define TOML_EXPORTED_CLASS __declspec(dllimport)
980 #define TOML_EXPORTED_FREE_FUNCTION __declspec(dllimport)
981 #endif
982 #ifndef TOML_CALLCONV
983 #define TOML_CALLCONV __cdecl
984 #endif
985 #elif defined(__GNUC__) && __GNUC__ >= 4
986 #define TOML_EXPORTED_CLASS __attribute__((visibility("default")))
987 #define TOML_EXPORTED_MEMBER_FUNCTION __attribute__((visibility("default")))
988 #define TOML_EXPORTED_STATIC_FUNCTION __attribute__((visibility("default")))
989 #define TOML_EXPORTED_FREE_FUNCTION __attribute__((visibility("default")))
990 #endif
991 #endif
992 #ifndef TOML_EXPORTED_CLASS
993 #define TOML_EXPORTED_CLASS
994 #endif
995 #ifndef TOML_EXPORTED_MEMBER_FUNCTION
996 #define TOML_EXPORTED_MEMBER_FUNCTION
997 #endif
998 #ifndef TOML_EXPORTED_STATIC_FUNCTION
999 #define TOML_EXPORTED_STATIC_FUNCTION
1000 #endif
1001 #ifndef TOML_EXPORTED_FREE_FUNCTION
1002 #define TOML_EXPORTED_FREE_FUNCTION
1003 #endif
1004
1005 // experimental language features
1006 #if !defined(TOML_ENABLE_UNRELEASED_FEATURES) && defined(TOML_UNRELEASED_FEATURES) // was TOML_UNRELEASED_FEATURES
1007 // pre-3.0
1008 #define TOML_ENABLE_UNRELEASED_FEATURES TOML_UNRELEASED_FEATURES
1009 #endif
1010 #if (defined(TOML_ENABLE_UNRELEASED_FEATURES) && TOML_ENABLE_UNRELEASED_FEATURES) || TOML_INTELLISENSE
1011 #undef TOML_ENABLE_UNRELEASED_FEATURES
1012 #define TOML_ENABLE_UNRELEASED_FEATURES 1
1013 #endif
1014 #ifndef TOML_ENABLE_UNRELEASED_FEATURES
1015 #define TOML_ENABLE_UNRELEASED_FEATURES 0
1016 #endif
1017
1018 // parser
1019 #if !defined(TOML_ENABLE_PARSER) && defined(TOML_PARSER) // was TOML_PARSER pre-3.0
1020 #define TOML_ENABLE_PARSER TOML_PARSER
1021 #endif
1022 #if !defined(TOML_ENABLE_PARSER) || (defined(TOML_ENABLE_PARSER) && TOML_ENABLE_PARSER) || TOML_INTELLISENSE
1023 #undef TOML_ENABLE_PARSER
1024 #define TOML_ENABLE_PARSER 1
1025 #endif
1026
1027 // formatters
1028 #if !defined(TOML_ENABLE_FORMATTERS) || (defined(TOML_ENABLE_FORMATTERS) && TOML_ENABLE_FORMATTERS) || TOML_INTELLISENSE
1029 #undef TOML_ENABLE_FORMATTERS
1030 #define TOML_ENABLE_FORMATTERS 1
1031 #endif
1032
1033 // SIMD
1034 #if !defined(TOML_ENABLE_SIMD) || (defined(TOML_ENABLE_SIMD) && TOML_ENABLE_SIMD) || TOML_INTELLISENSE
1035 #undef TOML_ENABLE_SIMD
1036 #define TOML_ENABLE_SIMD 1
1037 #endif
1038
1039 // windows compat
1040 #if !defined(TOML_ENABLE_WINDOWS_COMPAT) && defined(TOML_WINDOWS_COMPAT) // was TOML_WINDOWS_COMPAT pre-3.0
1041 #define TOML_ENABLE_WINDOWS_COMPAT TOML_WINDOWS_COMPAT
1042 #endif
1043 #if !defined(TOML_ENABLE_WINDOWS_COMPAT) || (defined(TOML_ENABLE_WINDOWS_COMPAT) && TOML_ENABLE_WINDOWS_COMPAT) \
1044 || TOML_INTELLISENSE
1045 #undef TOML_ENABLE_WINDOWS_COMPAT
1046 #define TOML_ENABLE_WINDOWS_COMPAT 1
1047 #endif
1048
1049 #if !TOML_WINDOWS
1050 #undef TOML_ENABLE_WINDOWS_COMPAT
1051 #define TOML_ENABLE_WINDOWS_COMPAT 0
1052 #endif
1053
1054 #ifndef TOML_INCLUDE_WINDOWS_H
1055 #define TOML_INCLUDE_WINDOWS_H 0
1056 #endif
1057
1058 // custom optional
1059 #ifdef TOML_OPTIONAL_TYPE
1060 #define TOML_HAS_CUSTOM_OPTIONAL_TYPE 1
1061 #else
1062 #define TOML_HAS_CUSTOM_OPTIONAL_TYPE 0
1063 #endif
1064
1065 // exceptions (library use)
1066 #if TOML_COMPILER_HAS_EXCEPTIONS
1067 #if !defined(TOML_EXCEPTIONS) || (defined(TOML_EXCEPTIONS) && TOML_EXCEPTIONS)
1068 #undef TOML_EXCEPTIONS
1069 #define TOML_EXCEPTIONS 1
1070 #endif
1071 #else
1072 #if defined(TOML_EXCEPTIONS) && TOML_EXCEPTIONS
1073 #error TOML_EXCEPTIONS was explicitly enabled but exceptions are disabled/unsupported by the compiler.
1074 #endif
1075 #undef TOML_EXCEPTIONS
1076 #define TOML_EXCEPTIONS 0
1077 #endif
1078
1079 // calling convention for static/free/friend functions
1080 #ifndef TOML_CALLCONV
1081 #define TOML_CALLCONV
1082 #endif
1083
1084 #ifndef TOML_UNDEF_MACROS
1085 #define TOML_UNDEF_MACROS 1
1086 #endif
1087
1088 #ifndef TOML_MAX_NESTED_VALUES
1089 #define TOML_MAX_NESTED_VALUES 256
1090 // this refers to the depth of nested values, e.g. inline tables and arrays.
1091 // 256 is crazy high! if you're hitting this limit with real input, TOML is probably the wrong tool for the job...
1092 #endif
1093
1094 #ifndef TOML_MAX_DOTTED_KEYS_DEPTH
1095 #define TOML_MAX_DOTTED_KEYS_DEPTH 1024
1096 #endif
1097
1098 #ifdef TOML_CHAR_8_STRINGS
1099 #if TOML_CHAR_8_STRINGS
1100 #error TOML_CHAR_8_STRINGS was removed in toml++ 2.0.0; all value setters and getters now work with char8_t strings implicitly.
1101 #endif
1102 #endif
1103
1104 #ifdef TOML_LARGE_FILES
1105 #if !TOML_LARGE_FILES
1106 #error Support for !TOML_LARGE_FILES (i.e. 'small files') was removed in toml++ 3.0.0.
1107 #endif
1108 #endif
1109
1110 #ifndef TOML_LIFETIME_HOOKS
1111 #define TOML_LIFETIME_HOOKS 0
1112 #endif
1113
1114 #ifdef NDEBUG
1115 #undef TOML_ASSERT
1116 #define TOML_ASSERT(expr) static_assert(true)
1117 #endif
1118 #ifndef TOML_ASSERT
1119 #ifndef assert
1120 TOML_DISABLE_WARNINGS;
1121 #include <cassert>
1122 TOML_ENABLE_WARNINGS;
1123 #endif
1124 #define TOML_ASSERT(expr) assert(expr)
1125 #endif
1126 #ifdef NDEBUG
1127 #define TOML_ASSERT_ASSUME(expr) TOML_ASSUME(expr)
1128 #else
1129 #define TOML_ASSERT_ASSUME(expr) TOML_ASSERT(expr)
1130 #endif
1131
1132 #ifndef TOML_ENABLE_FLOAT16
1133 #define TOML_ENABLE_FLOAT16 0
1134 #endif
1135
1136 #ifndef TOML_DISABLE_CONDITIONAL_NOEXCEPT_LAMBDA
1137 #define TOML_DISABLE_CONDITIONAL_NOEXCEPT_LAMBDA 0
1138 #endif
1139
1140 #ifndef TOML_DISABLE_NOEXCEPT_NOEXCEPT
1141 #define TOML_DISABLE_NOEXCEPT_NOEXCEPT 0
1142 #ifdef _MSC_VER
1143 #if _MSC_VER <= 1943 // Up to Visual Studio 2022 Version 17.13.6
1144 #undef TOML_DISABLE_NOEXCEPT_NOEXCEPT
1145 #define TOML_DISABLE_NOEXCEPT_NOEXCEPT 1
1146 #endif
1147 #endif
1148 #endif
1149
1150 #if !defined(TOML_FLOAT_CHARCONV) && (TOML_GCC || TOML_CLANG || (TOML_ICC && !TOML_ICC_CL))
1151 // not supported by any version of GCC or Clang as of 26/11/2020
1152 // not supported by any version of ICC on Linux as of 11/01/2021
1153 #define TOML_FLOAT_CHARCONV 0
1154 #endif
1155 #if !defined(TOML_INT_CHARCONV) && (defined(__EMSCRIPTEN__) || defined(__APPLE__))
1156 // causes link errors on emscripten
1157 // causes Mac OS SDK version errors on some versions of Apple Clang
1158 #define TOML_INT_CHARCONV 0
1159 #endif
1160 #ifndef TOML_INT_CHARCONV
1161 #define TOML_INT_CHARCONV 1
1162 #endif
1163 #ifndef TOML_FLOAT_CHARCONV
1164 #define TOML_FLOAT_CHARCONV 1
1165 #endif
1166 #if (TOML_INT_CHARCONV || TOML_FLOAT_CHARCONV) && !TOML_HAS_INCLUDE(<charconv>)
1167 #undef TOML_INT_CHARCONV
1168 #undef TOML_FLOAT_CHARCONV
1169 #define TOML_INT_CHARCONV 0
1170 #define TOML_FLOAT_CHARCONV 0
1171 #endif
1172
1173 #if defined(__cpp_concepts) && __cpp_concepts >= 201907
1174 #define TOML_REQUIRES(...) requires(__VA_ARGS__)
1175 #else
1176 #define TOML_REQUIRES(...)
1177 #endif
1178 #define TOML_ENABLE_IF(...) , typename std::enable_if<(__VA_ARGS__), int>::type = 0
1179 #define TOML_CONSTRAINED_TEMPLATE(condition, ...) \
1180 template <__VA_ARGS__ TOML_ENABLE_IF(condition)> \
1181 TOML_REQUIRES(condition)
1182 #define TOML_HIDDEN_CONSTRAINT(condition, ...) TOML_CONSTRAINED_TEMPLATE(condition, __VA_ARGS__)
1183
1184 #if defined(__SIZEOF_FLOAT128__) && defined(__FLT128_MANT_DIG__) && defined(__LDBL_MANT_DIG__) \
1185 && __FLT128_MANT_DIG__ > __LDBL_MANT_DIG__
1186 #define TOML_FLOAT128 __float128
1187 #endif
1188
1189 #ifdef __SIZEOF_INT128__
1190 #define TOML_INT128 __int128_t
1191 #define TOML_UINT128 __uint128_t
1192 #endif
1193
1194 // clang-format off
1195
1196 //******** impl/version.hpp ******************************************************************************************
1197
1198 #define TOML_LIB_MAJOR 3
1199 #define TOML_LIB_MINOR 4
1200 #define TOML_LIB_PATCH 0
1201
1202 #define TOML_LANG_MAJOR 1
1203 #define TOML_LANG_MINOR 0
1204 #define TOML_LANG_PATCH 0
1205
1206 //******** impl/preprocessor.hpp *************************************************************************************
1207
1208 #define TOML_LIB_SINGLE_HEADER 1
1209
1210 #if TOML_ENABLE_UNRELEASED_FEATURES
1211 #define TOML_LANG_EFFECTIVE_VERSION \
1212 TOML_MAKE_VERSION(TOML_LANG_MAJOR, TOML_LANG_MINOR, TOML_LANG_PATCH+1)
1213 #else
1214 #define TOML_LANG_EFFECTIVE_VERSION \
1215 TOML_MAKE_VERSION(TOML_LANG_MAJOR, TOML_LANG_MINOR, TOML_LANG_PATCH)
1216 #endif
1217
1218 #define TOML_LANG_HIGHER_THAN(major, minor, patch) \
1219 (TOML_LANG_EFFECTIVE_VERSION > TOML_MAKE_VERSION(major, minor, patch))
1220
1221 #define TOML_LANG_AT_LEAST(major, minor, patch) \
1222 (TOML_LANG_EFFECTIVE_VERSION >= TOML_MAKE_VERSION(major, minor, patch))
1223
1224 #define TOML_LANG_UNRELEASED \
1225 TOML_LANG_HIGHER_THAN(TOML_LANG_MAJOR, TOML_LANG_MINOR, TOML_LANG_PATCH)
1226
1227 #ifndef TOML_ABI_NAMESPACES
1228 #if TOML_DOXYGEN
1229 #define TOML_ABI_NAMESPACES 0
1230 #else
1231 #define TOML_ABI_NAMESPACES 1
1232 #endif
1233 #endif
1234 #if TOML_ABI_NAMESPACES
1235 #define TOML_NAMESPACE_START namespace toml { inline namespace TOML_CONCAT(v, TOML_LIB_MAJOR)
1236 #define TOML_NAMESPACE_END } static_assert(true)
1237 #define TOML_NAMESPACE ::toml::TOML_CONCAT(v, TOML_LIB_MAJOR)
1238 #define TOML_ABI_NAMESPACE_START(name) inline namespace name { static_assert(true)
1239 #define TOML_ABI_NAMESPACE_BOOL(cond, T, F) TOML_ABI_NAMESPACE_START(TOML_CONCAT(TOML_EVAL_BOOL_, cond)(T, F))
1240 #define TOML_ABI_NAMESPACE_END } static_assert(true)
1241 #else
1242 #define TOML_NAMESPACE_START namespace toml
1243 #define TOML_NAMESPACE_END static_assert(true)
1244 #define TOML_NAMESPACE toml
1245 #define TOML_ABI_NAMESPACE_START(...) static_assert(true)
1246 #define TOML_ABI_NAMESPACE_BOOL(...) static_assert(true)
1247 #define TOML_ABI_NAMESPACE_END static_assert(true)
1248 #endif
1249 #define TOML_IMPL_NAMESPACE_START TOML_NAMESPACE_START { namespace impl
1250 #define TOML_IMPL_NAMESPACE_END } TOML_NAMESPACE_END
1251 #if TOML_HEADER_ONLY
1252 #define TOML_ANON_NAMESPACE_START static_assert(TOML_IMPLEMENTATION); TOML_IMPL_NAMESPACE_START
1253 #define TOML_ANON_NAMESPACE_END TOML_IMPL_NAMESPACE_END
1254 #define TOML_ANON_NAMESPACE TOML_NAMESPACE::impl
1255 #define TOML_EXTERNAL_LINKAGE inline
1256 #define TOML_INTERNAL_LINKAGE inline
1257 #else
1258 #define TOML_ANON_NAMESPACE_START static_assert(TOML_IMPLEMENTATION); \
1259 using namespace toml; \
1260 namespace
1261 #define TOML_ANON_NAMESPACE_END static_assert(true)
1262 #define TOML_ANON_NAMESPACE
1263 #define TOML_EXTERNAL_LINKAGE
1264 #define TOML_INTERNAL_LINKAGE static
1265 #endif
1266
1267 // clang-format on
1268
1269 // clang-format off
1270
1271 #if TOML_SIMPLE_STATIC_ASSERT_MESSAGES
1272
1273 #define TOML_SA_NEWLINE " "
1274 #define TOML_SA_LIST_SEP ", "
1275 #define TOML_SA_LIST_BEG " ("
1276 #define TOML_SA_LIST_END ")"
1277 #define TOML_SA_LIST_NEW " "
1278 #define TOML_SA_LIST_NXT ", "
1279
1280 #else
1281
1282 #define TOML_SA_NEWLINE "\n| "
1283 #define TOML_SA_LIST_SEP TOML_SA_NEWLINE " - "
1284 #define TOML_SA_LIST_BEG TOML_SA_LIST_SEP
1285 #define TOML_SA_LIST_END
1286 #define TOML_SA_LIST_NEW TOML_SA_NEWLINE TOML_SA_NEWLINE
1287 #define TOML_SA_LIST_NXT TOML_SA_LIST_NEW
1288
1289 #endif
1290
1291 #define TOML_SA_NATIVE_VALUE_TYPE_LIST \
1292 TOML_SA_LIST_BEG "std::string" \
1293 TOML_SA_LIST_SEP "int64_t" \
1294 TOML_SA_LIST_SEP "double" \
1295 TOML_SA_LIST_SEP "bool" \
1296 TOML_SA_LIST_SEP "toml::date" \
1297 TOML_SA_LIST_SEP "toml::time" \
1298 TOML_SA_LIST_SEP "toml::date_time" \
1299 TOML_SA_LIST_END
1300
1301 #define TOML_SA_NODE_TYPE_LIST \
1302 TOML_SA_LIST_BEG "toml::table" \
1303 TOML_SA_LIST_SEP "toml::array" \
1304 TOML_SA_LIST_SEP "toml::value<std::string>" \
1305 TOML_SA_LIST_SEP "toml::value<int64_t>" \
1306 TOML_SA_LIST_SEP "toml::value<double>" \
1307 TOML_SA_LIST_SEP "toml::value<bool>" \
1308 TOML_SA_LIST_SEP "toml::value<toml::date>" \
1309 TOML_SA_LIST_SEP "toml::value<toml::time>" \
1310 TOML_SA_LIST_SEP "toml::value<toml::date_time>" \
1311 TOML_SA_LIST_END
1312
1313 #define TOML_SA_UNWRAPPED_NODE_TYPE_LIST \
1314 TOML_SA_LIST_NEW "A native TOML value type" \
1315 TOML_SA_NATIVE_VALUE_TYPE_LIST \
1316 \
1317 TOML_SA_LIST_NXT "A TOML node type" \
1318 TOML_SA_NODE_TYPE_LIST
1319
1320 // clang-format on
1321
1322 TOML_PUSH_WARNINGS;
1323 TOML_DISABLE_SPAM_WARNINGS;
1324 TOML_DISABLE_SWITCH_WARNINGS;
1325 TOML_DISABLE_SUGGEST_ATTR_WARNINGS;
1326
1327 // misc warning false-positives
1328 #if TOML_MSVC
1329 #pragma warning(disable : 5031) // #pragma warning(pop): likely mismatch
1330 #if TOML_SHARED_LIB
1331 #pragma warning(disable : 4251) // dll exports for std lib types
1332 #endif
1333 #elif TOML_CLANG
1334 TOML_PRAGMA_CLANG(diagnostic ignored "-Wheader-hygiene")
1335 #if TOML_CLANG >= 12
1336 TOML_PRAGMA_CLANG(diagnostic ignored "-Wc++20-extensions")
1337 #endif
1338 #if TOML_CLANG == 13
1339 TOML_PRAGMA_CLANG(diagnostic ignored "-Wreserved-identifier")
1340 #endif
1341 #endif
1342
1343 //******** impl/std_new.hpp ******************************************************************************************
1344
1345 TOML_DISABLE_WARNINGS;
1346 #include <new>
1347 TOML_ENABLE_WARNINGS;
1348
1349 #if (!defined(__apple_build_version__) && TOML_CLANG >= 8) || TOML_GCC >= 7 || TOML_ICC >= 1910 || TOML_MSVC >= 1914
1350 #define TOML_LAUNDER(x) __builtin_launder(x)
1351 #elif defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606
1352 #define TOML_LAUNDER(x) std::launder(x)
1353 #else
1354 #define TOML_LAUNDER(x) x
1355 #endif
1356
1357 //******** impl/std_string.hpp ***************************************************************************************
1358
1359 TOML_DISABLE_WARNINGS;
1360 #include <string_view>
1361 #include <string>
1362 TOML_ENABLE_WARNINGS;
1363
1364 #if TOML_DOXYGEN \
1365 || (defined(__cpp_char8_t) && __cpp_char8_t >= 201811 && defined(__cpp_lib_char8_t) \
1366 && __cpp_lib_char8_t >= 201907)
1367 #define TOML_HAS_CHAR8 1
1368 #else
1369 #define TOML_HAS_CHAR8 0
1370 #endif
1371
1372 namespace toml // non-abi namespace; this is not an error
1373 {
1374 using namespace std::string_literals;
1375 using namespace std::string_view_literals;
1376 }
1377
1378 #if TOML_ENABLE_WINDOWS_COMPAT
1379
1380 TOML_IMPL_NAMESPACE_START
1381 {
1382 TOML_NODISCARD
1383 TOML_EXPORTED_FREE_FUNCTION
1384 std::string narrow(std::wstring_view);
1385
1386 TOML_NODISCARD
1387 TOML_EXPORTED_FREE_FUNCTION
1388 std::wstring widen(std::string_view);
1389
1390 #if TOML_HAS_CHAR8
1391
1392 TOML_NODISCARD
1393 TOML_EXPORTED_FREE_FUNCTION
1394 std::wstring widen(std::u8string_view);
1395
1396 #endif
1397 }
1398 TOML_IMPL_NAMESPACE_END;
1399
1400 #endif // TOML_ENABLE_WINDOWS_COMPAT
1401
1402 //******** impl/std_optional.hpp *************************************************************************************
1403
1404 TOML_DISABLE_WARNINGS;
1405 #if !TOML_HAS_CUSTOM_OPTIONAL_TYPE
1406 #include <optional>
1407 #endif
1408 TOML_ENABLE_WARNINGS;
1409
1410 TOML_NAMESPACE_START
1411 {
1412 #if TOML_HAS_CUSTOM_OPTIONAL_TYPE
1413
1414 template <typename T>
1415 using optional = TOML_OPTIONAL_TYPE<T>;
1416
1417 #else
1418
1419 template <typename T>
1420 using optional = std::optional<T>;
1421
1422 #endif
1423 }
1424 TOML_NAMESPACE_END;
1425
1426 //******** impl/forward_declarations.hpp *****************************************************************************
1427
1428 TOML_DISABLE_WARNINGS;
1429 #include <cstdint>
1430 #include <cstddef>
1431 #include <cstring>
1432 #include <cfloat>
1433 #include <climits>
1434 #include <cmath>
1435 #include <limits>
1436 #include <memory>
1437 #include <iosfwd>
1438 #include <type_traits>
1439 TOML_ENABLE_WARNINGS;
1440 TOML_PUSH_WARNINGS;
1441 #ifdef _MSC_VER
1442 #ifndef __clang__
1443 #pragma inline_recursion(on)
1444 #endif
1445 #pragma push_macro("min")
1446 #pragma push_macro("max")
1447 #undef min
1448 #undef max
1449 #endif
1450
1451 #ifndef TOML_DISABLE_ENVIRONMENT_CHECKS
1452 #define TOML_ENV_MESSAGE \
1453 "If you're seeing this error it's because you're building toml++ for an environment that doesn't conform to " \
1454 "one of the 'ground truths' assumed by the library. Essentially this just means that I don't have the " \
1455 "resources to test on more platforms, but I wish I did! You can try disabling the checks by defining " \
1456 "TOML_DISABLE_ENVIRONMENT_CHECKS, but your mileage may vary. Please consider filing an issue at " \
1457 "https://github.com/marzer/tomlplusplus/issues to help me improve support for your target environment. " \
1458 "Thanks!"
1459
1460 static_assert(CHAR_BIT == 8, TOML_ENV_MESSAGE);
1461 #ifdef FLT_RADIX
1462 static_assert(FLT_RADIX == 2, TOML_ENV_MESSAGE);
1463 #endif
1464 static_assert('A' == 65, TOML_ENV_MESSAGE);
1465 static_assert(sizeof(double) == 8, TOML_ENV_MESSAGE);
1466 static_assert(std::numeric_limits<double>::is_iec559, TOML_ENV_MESSAGE);
1467 static_assert(std::numeric_limits<double>::digits == 53, TOML_ENV_MESSAGE);
1468 static_assert(std::numeric_limits<double>::digits10 == 15, TOML_ENV_MESSAGE);
1469 static_assert(std::numeric_limits<double>::radix == 2, TOML_ENV_MESSAGE);
1470
1471 #undef TOML_ENV_MESSAGE
1472 #endif // !TOML_DISABLE_ENVIRONMENT_CHECKS
1473
1474 // undocumented forward declarations are hidden from doxygen because they fuck it up =/
1475
1476 namespace toml // non-abi namespace; this is not an error
1477 {
1478 using ::std::size_t;
1479 using ::std::intptr_t;
1480 using ::std::uintptr_t;
1481 using ::std::ptrdiff_t;
1482 using ::std::nullptr_t;
1483 using ::std::int8_t;
1484 using ::std::int16_t;
1485 using ::std::int32_t;
1486 using ::std::int64_t;
1487 using ::std::uint8_t;
1488 using ::std::uint16_t;
1489 using ::std::uint32_t;
1490 using ::std::uint64_t;
1491 using ::std::uint_least32_t;
1492 using ::std::uint_least64_t;
1493 }
1494
1495 TOML_NAMESPACE_START
1496 {
1497 struct date;
1498 struct time;
1499 struct time_offset;
1500
1501 TOML_ABI_NAMESPACE_BOOL(TOML_HAS_CUSTOM_OPTIONAL_TYPE, custopt, stdopt);
1502 struct date_time;
1503 TOML_ABI_NAMESPACE_END;
1504
1505 struct source_position;
1506 struct source_region;
1507
1508 class node;
1509 template <typename>
1510 class node_view;
1511
1512 class key;
1513 class array;
1514 class table;
1515 template <typename>
1516 class value;
1517
1518 class path;
1519
1520 class toml_formatter;
1521 class json_formatter;
1522 class yaml_formatter;
1523
1524 TOML_ABI_NAMESPACE_BOOL(TOML_EXCEPTIONS, ex, noex);
1525 #if TOML_EXCEPTIONS
1526 using parse_result = table;
1527 #else
1528 class parse_result;
1529 #endif
1530 TOML_ABI_NAMESPACE_END; // TOML_EXCEPTIONS
1531 }
1532 TOML_NAMESPACE_END;
1533
1534 TOML_IMPL_NAMESPACE_START
1535 {
1536 using node_ptr = std::unique_ptr<node>;
1537
1538 TOML_ABI_NAMESPACE_BOOL(TOML_EXCEPTIONS, impl_ex, impl_noex);
1539 class parser;
1540 TOML_ABI_NAMESPACE_END; // TOML_EXCEPTIONS
1541
1542 // clang-format off
1543
1544 inline constexpr std::string_view control_char_escapes[] =
1545 {
1546 "\\u0000"sv,
1547 "\\u0001"sv,
1548 "\\u0002"sv,
1549 "\\u0003"sv,
1550 "\\u0004"sv,
1551 "\\u0005"sv,
1552 "\\u0006"sv,
1553 "\\u0007"sv,
1554 "\\b"sv,
1555 "\\t"sv,
1556 "\\n"sv,
1557 "\\u000B"sv,
1558 "\\f"sv,
1559 "\\r"sv,
1560 "\\u000E"sv,
1561 "\\u000F"sv,
1562 "\\u0010"sv,
1563 "\\u0011"sv,
1564 "\\u0012"sv,
1565 "\\u0013"sv,
1566 "\\u0014"sv,
1567 "\\u0015"sv,
1568 "\\u0016"sv,
1569 "\\u0017"sv,
1570 "\\u0018"sv,
1571 "\\u0019"sv,
1572 "\\u001A"sv,
1573 "\\u001B"sv,
1574 "\\u001C"sv,
1575 "\\u001D"sv,
1576 "\\u001E"sv,
1577 "\\u001F"sv,
1578 };
1579
1580 inline constexpr std::string_view node_type_friendly_names[] =
1581 {
1582 "none"sv,
1583 "table"sv,
1584 "array"sv,
1585 "string"sv,
1586 "integer"sv,
1587 "floating-point"sv,
1588 "boolean"sv,
1589 "date"sv,
1590 "time"sv,
1591 "date-time"sv
1592 };
1593
1594 // clang-format on
1595 }
1596 TOML_IMPL_NAMESPACE_END;
1597
1598 #if TOML_ABI_NAMESPACES
1599 #if TOML_EXCEPTIONS
1600 #define TOML_PARSER_TYPENAME TOML_NAMESPACE::impl::impl_ex::parser
1601 #else
1602 #define TOML_PARSER_TYPENAME TOML_NAMESPACE::impl::impl_noex::parser
1603 #endif
1604 #else
1605 #define TOML_PARSER_TYPENAME TOML_NAMESPACE::impl::parser
1606 #endif
1607
1608 namespace toml
1609 {
1610 }
1611
1612 TOML_NAMESPACE_START // abi namespace
1613 {
1614 inline namespace literals
1615 {
1616 }
1617
1618 enum class TOML_CLOSED_ENUM node_type : uint8_t
1619 {
1620 none,
1621 table,
1622 array,
1623 string,
1624 integer,
1625 floating_point,
1626 boolean,
1627 date,
1628 time,
1629 date_time
1630 };
1631
1632 template <typename Char>
1633 inline std::basic_ostream<Char>& operator<<(std::basic_ostream<Char>& lhs, node_type rhs)
1634 {
1635 const auto str = impl::node_type_friendly_names[static_cast<std::underlying_type_t<node_type>>(rhs)];
1636 using str_char_t = decltype(str)::value_type;
1637 if constexpr (std::is_same_v<Char, str_char_t>)
1638 return lhs << str;
1639 else
1640 {
1641 if constexpr (sizeof(Char) == sizeof(str_char_t))
1642 return lhs << std::basic_string_view<Char>{ reinterpret_cast<const Char*>(str.data()), str.length() };
1643 else
1644 return lhs << str.data();
1645 }
1646 }
1647
1648 enum class TOML_OPEN_FLAGS_ENUM value_flags : uint16_t // being an "OPEN" flags enum is not an error
1649 {
1650 none,
1651 format_as_binary = 1,
1652 format_as_octal = 2,
1653 format_as_hexadecimal = 3,
1654 };
1655 TOML_MAKE_FLAGS(value_flags);
1656
1657 inline constexpr value_flags preserve_source_value_flags =
1658 POXY_IMPLEMENTATION_DETAIL(value_flags{ static_cast<std::underlying_type_t<value_flags>>(-1) });
1659
1660 enum class TOML_CLOSED_FLAGS_ENUM format_flags : uint64_t
1661 {
1662 none,
1663 quote_dates_and_times = (1ull << 0),
1664 quote_infinities_and_nans = (1ull << 1),
1665 allow_literal_strings = (1ull << 2),
1666 allow_multi_line_strings = (1ull << 3),
1667 allow_real_tabs_in_strings = (1ull << 4),
1668 allow_unicode_strings = (1ull << 5),
1669 allow_binary_integers = (1ull << 6),
1670 allow_octal_integers = (1ull << 7),
1671 allow_hexadecimal_integers = (1ull << 8),
1672 indent_sub_tables = (1ull << 9),
1673 indent_array_elements = (1ull << 10),
1674 indentation = indent_sub_tables | indent_array_elements,
1675 relaxed_float_precision = (1ull << 11),
1676 terse_key_value_pairs = (1ull << 12),
1677 force_multiline_arrays = (1ull << 13),
1678 };
1679 TOML_MAKE_FLAGS(format_flags);
1680
1681 template <typename T>
1682 struct TOML_TRIVIAL_ABI inserter
1683 {
1684 static_assert(std::is_reference_v<T>);
1685
1686 T value;
1687 };
1688 template <typename T>
1689 inserter(T&&) -> inserter<T&&>;
1690 template <typename T>
1691 inserter(T&) -> inserter<T&>;
1692
1693 using default_formatter = toml_formatter;
1694 }
1695 TOML_NAMESPACE_END;
1696
1697 TOML_IMPL_NAMESPACE_START
1698 {
1699 template <typename T>
1700 using remove_cvref = std::remove_cv_t<std::remove_reference_t<T>>;
1701
1702 template <typename... T>
1703 using common_signed_type = std::common_type_t<std::make_signed_t<T>...>;
1704
1705 template <typename T, typename... U>
1706 inline constexpr bool is_one_of = (false || ... || std::is_same_v<T, U>);
1707
1708 template <typename... T>
1709 inline constexpr bool all_integral = (std::is_integral_v<T> && ...);
1710
1711 template <typename T>
1712 inline constexpr bool is_cvref = std::is_reference_v<T> || std::is_const_v<T> || std::is_volatile_v<T>;
1713
1714 template <typename T>
1715 inline constexpr bool is_wide_string =
1716 is_one_of<std::decay_t<T>, const wchar_t*, wchar_t*, std::wstring_view, std::wstring>;
1717
1718 template <typename T>
1719 inline constexpr bool value_retrieval_is_nothrow = !std::is_same_v<remove_cvref<T>, std::string>
1720 #if TOML_HAS_CHAR8
1721 && !std::is_same_v<remove_cvref<T>, std::u8string>
1722 #endif
1723
1724 && !is_wide_string<T>;
1725
1726 template <typename, typename>
1727 struct copy_ref_;
1728 template <typename Dest, typename Src>
1729 using copy_ref = typename copy_ref_<Dest, Src>::type;
1730
1731 template <typename Dest, typename Src>
1732 struct copy_ref_
1733 {
1734 using type = Dest;
1735 };
1736
1737 template <typename Dest, typename Src>
1738 struct copy_ref_<Dest, Src&>
1739 {
1740 using type = std::add_lvalue_reference_t<Dest>;
1741 };
1742
1743 template <typename Dest, typename Src>
1744 struct copy_ref_<Dest, Src&&>
1745 {
1746 using type = std::add_rvalue_reference_t<Dest>;
1747 };
1748
1749 template <typename, typename>
1750 struct copy_cv_;
1751 template <typename Dest, typename Src>
1752 using copy_cv = typename copy_cv_<Dest, Src>::type;
1753
1754 template <typename Dest, typename Src>
1755 struct copy_cv_
1756 {
1757 using type = Dest;
1758 };
1759
1760 template <typename Dest, typename Src>
1761 struct copy_cv_<Dest, const Src>
1762 {
1763 using type = std::add_const_t<Dest>;
1764 };
1765
1766 template <typename Dest, typename Src>
1767 struct copy_cv_<Dest, volatile Src>
1768 {
1769 using type = std::add_volatile_t<Dest>;
1770 };
1771
1772 template <typename Dest, typename Src>
1773 struct copy_cv_<Dest, const volatile Src>
1774 {
1775 using type = std::add_cv_t<Dest>;
1776 };
1777
1778 template <typename Dest, typename Src>
1779 using copy_cvref =
1780 copy_ref<copy_ref<copy_cv<std::remove_reference_t<Dest>, std::remove_reference_t<Src>>, Dest>, Src>;
1781
1782 template <typename...>
1783 inline constexpr bool always_false = false;
1784
1785 template <typename T, typename... U>
1786 inline constexpr bool first_is_same = false;
1787 template <typename T, typename... U>
1788 inline constexpr bool first_is_same<T, T, U...> = true;
1789
1790 template <typename T, bool = std::is_enum_v<T>>
1791 struct underlying_type_
1792 {
1793 using type = std::underlying_type_t<T>;
1794 };
1795 template <typename T>
1796 struct underlying_type_<T, false>
1797 {
1798 using type = T;
1799 };
1800 template <typename T>
1801 using underlying_type = typename underlying_type_<T>::type;
1802
1803 // general value traits
1804 // (as they relate to their equivalent native TOML type)
1805 struct default_value_traits
1806 {
1807 using native_type = void;
1808 static constexpr bool is_native = false;
1809 static constexpr bool is_losslessly_convertible_to_native = false;
1810 static constexpr bool can_represent_native = false;
1811 static constexpr bool can_partially_represent_native = false;
1812 static constexpr auto type = node_type::none;
1813 };
1814
1815 template <typename T>
1816 struct value_traits;
1817
1818 template <typename T, bool = std::is_enum_v<T>>
1819 struct value_traits_base_selector
1820 {
1821 static_assert(!is_cvref<T>);
1822
1823 using type = default_value_traits;
1824 };
1825 template <typename T>
1826 struct value_traits_base_selector<T, true>
1827 {
1828 static_assert(!is_cvref<T>);
1829
1830 using type = value_traits<underlying_type<T>>;
1831 };
1832
1833 template <typename T>
1834 struct value_traits : value_traits_base_selector<T>::type
1835 {};
1836 template <typename T>
1837 struct value_traits<const T> : value_traits<T>
1838 {};
1839 template <typename T>
1840 struct value_traits<volatile T> : value_traits<T>
1841 {};
1842 template <typename T>
1843 struct value_traits<const volatile T> : value_traits<T>
1844 {};
1845 template <typename T>
1846 struct value_traits<T&> : value_traits<T>
1847 {};
1848 template <typename T>
1849 struct value_traits<T&&> : value_traits<T>
1850 {};
1851
1852 // integer value_traits specializations - standard types
1853 template <typename T>
1854 struct integer_limits
1855 {
1856 static constexpr T min = T{ (std::numeric_limits<underlying_type<T>>::min)() };
1857 static constexpr T max = T{ (std::numeric_limits<underlying_type<T>>::max)() };
1858 };
1859 template <typename T>
1860 struct integer_traits_base : integer_limits<T>
1861 {
1862 using native_type = int64_t;
1863 static constexpr bool is_native = std::is_same_v<underlying_type<T>, native_type>;
1864 static constexpr bool is_signed = static_cast<underlying_type<T>>(-1) < underlying_type<T>{};
1865 static constexpr auto type = node_type::integer;
1866 static constexpr bool can_partially_represent_native = true;
1867 };
1868 template <typename T>
1869 struct unsigned_integer_traits : integer_traits_base<T>
1870 {
1871 static constexpr bool is_losslessly_convertible_to_native =
1872 integer_limits<underlying_type<T>>::max <= 9223372036854775807ULL;
1873 static constexpr bool can_represent_native = false;
1874 };
1875 template <typename T>
1876 struct signed_integer_traits : integer_traits_base<T>
1877 {
1878 using native_type = int64_t;
1879 static constexpr bool is_losslessly_convertible_to_native =
1880 integer_limits<underlying_type<T>>::min >= (-9223372036854775807LL - 1LL)
1881 && integer_limits<underlying_type<T>>::max <= 9223372036854775807LL;
1882 static constexpr bool can_represent_native =
1883 integer_limits<underlying_type<T>>::min <= (-9223372036854775807LL - 1LL)
1884 && integer_limits<underlying_type<T>>::max >= 9223372036854775807LL;
1885 };
1886 template <typename T, bool S = integer_traits_base<T>::is_signed>
1887 struct integer_traits : signed_integer_traits<T>
1888 {};
1889 template <typename T>
1890 struct integer_traits<T, false> : unsigned_integer_traits<T>
1891 {};
1892 template <>
1893 struct value_traits<signed char> : integer_traits<signed char>
1894 {};
1895 template <>
1896 struct value_traits<unsigned char> : integer_traits<unsigned char>
1897 {};
1898 template <>
1899 struct value_traits<signed short> : integer_traits<signed short>
1900 {};
1901 template <>
1902 struct value_traits<unsigned short> : integer_traits<unsigned short>
1903 {};
1904 template <>
1905 struct value_traits<signed int> : integer_traits<signed int>
1906 {};
1907 template <>
1908 struct value_traits<unsigned int> : integer_traits<unsigned int>
1909 {};
1910 template <>
1911 struct value_traits<signed long> : integer_traits<signed long>
1912 {};
1913 template <>
1914 struct value_traits<unsigned long> : integer_traits<unsigned long>
1915 {};
1916 template <>
1917 struct value_traits<signed long long> : integer_traits<signed long long>
1918 {};
1919 template <>
1920 struct value_traits<unsigned long long> : integer_traits<unsigned long long>
1921 {};
1922 static_assert(value_traits<int64_t>::is_native);
1923 static_assert(value_traits<int64_t>::is_signed);
1924 static_assert(value_traits<int64_t>::is_losslessly_convertible_to_native);
1925 static_assert(value_traits<int64_t>::can_represent_native);
1926 static_assert(value_traits<int64_t>::can_partially_represent_native);
1927
1928 // integer value_traits specializations - non-standard types
1929 #ifdef TOML_INT128
1930 template <>
1931 struct integer_limits<TOML_INT128>
1932 {
1933 static constexpr TOML_INT128 max =
1934 static_cast<TOML_INT128>((TOML_UINT128{ 1u } << ((__SIZEOF_INT128__ * CHAR_BIT) - 1)) - 1);
1935 static constexpr TOML_INT128 min = -max - TOML_INT128{ 1 };
1936 };
1937 template <>
1938 struct integer_limits<TOML_UINT128>
1939 {
1940 static constexpr TOML_UINT128 min = TOML_UINT128{};
1941 static constexpr TOML_UINT128 max = (2u * static_cast<TOML_UINT128>(integer_limits<TOML_INT128>::max)) + 1u;
1942 };
1943 template <>
1944 struct value_traits<TOML_INT128> : integer_traits<TOML_INT128>
1945 {};
1946 template <>
1947 struct value_traits<TOML_UINT128> : integer_traits<TOML_UINT128>
1948 {};
1949 #endif
1950 #ifdef TOML_SMALL_INT_TYPE
1951 template <>
1952 struct value_traits<TOML_SMALL_INT_TYPE> : signed_integer_traits<TOML_SMALL_INT_TYPE>
1953 {};
1954 #endif
1955
1956 // floating-point traits base
1957 template <typename T, int MantissaDigits, int DecimalDigits>
1958 struct float_traits_base
1959 {
1960 static constexpr auto type = node_type::floating_point;
1961 using native_type = double;
1962 static constexpr bool is_native = std::is_same_v<T, native_type>;
1963 static constexpr bool is_signed = true;
1964
1965 static constexpr int bits = static_cast<int>(sizeof(T) * CHAR_BIT);
1966 static constexpr int digits = MantissaDigits;
1967 static constexpr int digits10 = DecimalDigits;
1968
1969 static constexpr bool is_losslessly_convertible_to_native = bits <= 64 //
1970 && digits <= 53 // DBL_MANT_DIG
1971 && digits10 <= 15; // DBL_DIG
1972
1973 static constexpr bool can_represent_native = digits >= 53 // DBL_MANT_DIG
1974 && digits10 >= 15; // DBL_DIG
1975
1976 static constexpr bool can_partially_represent_native = digits > 0 && digits10 > 0;
1977 };
1978 template <typename T>
1979 struct float_traits : float_traits_base<T, std::numeric_limits<T>::digits, std::numeric_limits<T>::digits10>
1980 {};
1981 #if TOML_ENABLE_FLOAT16
1982 template <>
1983 struct float_traits<_Float16> : float_traits_base<_Float16, __FLT16_MANT_DIG__, __FLT16_DIG__>
1984 {};
1985 #endif
1986 #ifdef TOML_FLOAT128
1987 template <>
1988 struct float_traits<TOML_FLOAT128> : float_traits_base<TOML_FLOAT128, __FLT128_MANT_DIG__, __FLT128_DIG__>
1989 {};
1990 #endif
1991
1992 // floating-point traits
1993 template <>
1994 struct value_traits<float> : float_traits<float>
1995 {};
1996 template <>
1997 struct value_traits<double> : float_traits<double>
1998 {};
1999 template <>
2000 struct value_traits<long double> : float_traits<long double>
2001 {};
2002 #if TOML_ENABLE_FLOAT16
2003 template <>
2004 struct value_traits<_Float16> : float_traits<_Float16>
2005 {};
2006 #endif
2007 #ifdef TOML_FLOAT128
2008 template <>
2009 struct value_traits<TOML_FLOAT128> : float_traits<TOML_FLOAT128>
2010 {};
2011 #endif
2012 #ifdef TOML_SMALL_FLOAT_TYPE
2013 template <>
2014 struct value_traits<TOML_SMALL_FLOAT_TYPE> : float_traits<TOML_SMALL_FLOAT_TYPE>
2015 {};
2016 #endif
2017 static_assert(value_traits<double>::is_native);
2018 static_assert(value_traits<double>::is_losslessly_convertible_to_native);
2019 static_assert(value_traits<double>::can_represent_native);
2020 static_assert(value_traits<double>::can_partially_represent_native);
2021
2022 // string value_traits specializations - char-based strings
2023 template <typename T>
2024 struct string_traits
2025 {
2026 using native_type = std::string;
2027 static constexpr bool is_native = std::is_same_v<T, native_type>;
2028 static constexpr bool is_losslessly_convertible_to_native = true;
2029 static constexpr bool can_represent_native =
2030 !std::is_array_v<T> && (!std::is_pointer_v<T> || std::is_const_v<std::remove_pointer_t<T>>);
2031 static constexpr bool can_partially_represent_native = can_represent_native;
2032 static constexpr auto type = node_type::string;
2033 };
2034 template <>
2035 struct value_traits<std::string> : string_traits<std::string>
2036 {};
2037 template <>
2038 struct value_traits<std::string_view> : string_traits<std::string_view>
2039 {};
2040 template <>
2041 struct value_traits<const char*> : string_traits<const char*>
2042 {};
2043 template <size_t N>
2044 struct value_traits<const char[N]> : string_traits<const char[N]>
2045 {};
2046 template <>
2047 struct value_traits<char*> : string_traits<char*>
2048 {};
2049 template <size_t N>
2050 struct value_traits<char[N]> : string_traits<char[N]>
2051 {};
2052
2053 // string value_traits specializations - char8_t-based strings
2054 #if TOML_HAS_CHAR8
2055 template <>
2056 struct value_traits<std::u8string> : string_traits<std::u8string>
2057 {};
2058 template <>
2059 struct value_traits<std::u8string_view> : string_traits<std::u8string_view>
2060 {};
2061 template <>
2062 struct value_traits<const char8_t*> : string_traits<const char8_t*>
2063 {};
2064 template <size_t N>
2065 struct value_traits<const char8_t[N]> : string_traits<const char8_t[N]>
2066 {};
2067 template <>
2068 struct value_traits<char8_t*> : string_traits<char8_t*>
2069 {};
2070 template <size_t N>
2071 struct value_traits<char8_t[N]> : string_traits<char8_t[N]>
2072 {};
2073 #endif
2074
2075 // string value_traits specializations - wchar_t-based strings on Windows
2076 #if TOML_ENABLE_WINDOWS_COMPAT
2077 template <typename T>
2078 struct wstring_traits
2079 {
2080 using native_type = std::string;
2081 static constexpr bool is_native = false;
2082 static constexpr bool is_losslessly_convertible_to_native = true; // narrow
2083 static constexpr bool can_represent_native = std::is_same_v<T, std::wstring>; // widen
2084 static constexpr bool can_partially_represent_native = can_represent_native;
2085 static constexpr auto type = node_type::string;
2086 };
2087 template <>
2088 struct value_traits<std::wstring> : wstring_traits<std::wstring>
2089 {};
2090 template <>
2091 struct value_traits<std::wstring_view> : wstring_traits<std::wstring_view>
2092 {};
2093 template <>
2094 struct value_traits<const wchar_t*> : wstring_traits<const wchar_t*>
2095 {};
2096 template <size_t N>
2097 struct value_traits<const wchar_t[N]> : wstring_traits<const wchar_t[N]>
2098 {};
2099 template <>
2100 struct value_traits<wchar_t*> : wstring_traits<wchar_t*>
2101 {};
2102 template <size_t N>
2103 struct value_traits<wchar_t[N]> : wstring_traits<wchar_t[N]>
2104 {};
2105 #endif
2106
2107 // other 'native' value_traits specializations
2108 template <typename T, node_type NodeType>
2109 struct native_value_traits
2110 {
2111 using native_type = T;
2112 static constexpr bool is_native = true;
2113 static constexpr bool is_losslessly_convertible_to_native = true;
2114 static constexpr bool can_represent_native = true;
2115 static constexpr bool can_partially_represent_native = true;
2116 static constexpr auto type = NodeType;
2117 };
2118 template <>
2119 struct value_traits<bool> : native_value_traits<bool, node_type::boolean>
2120 {};
2121 template <>
2122 struct value_traits<date> : native_value_traits<date, node_type::date>
2123 {};
2124 template <>
2125 struct value_traits<time> : native_value_traits<time, node_type::time>
2126 {};
2127 template <>
2128 struct value_traits<date_time> : native_value_traits<date_time, node_type::date_time>
2129 {};
2130
2131 // native value category queries
2132 template <typename T>
2133 using native_type_of = typename value_traits<T>::native_type;
2134 template <typename T>
2135 inline constexpr bool is_native = value_traits<T>::is_native;
2136 template <typename T>
2137 inline constexpr bool can_represent_native = value_traits<T>::can_represent_native;
2138 template <typename T>
2139 inline constexpr bool can_partially_represent_native = value_traits<T>::can_partially_represent_native;
2140 template <typename T>
2141 inline constexpr bool is_losslessly_convertible_to_native = value_traits<T>::is_losslessly_convertible_to_native;
2142 template <typename T, typename... U>
2143 inline constexpr bool is_natively_one_of = is_one_of<native_type_of<T>, U...>;
2144
2145 // native value types => nodes
2146 template <typename T>
2147 struct node_wrapper
2148 {
2149 using type = T;
2150 };
2151 template <typename T>
2152 struct node_wrapper<const T>
2153 {
2154 using type = std::add_const_t<typename node_wrapper<T>::type>;
2155 };
2156 template <typename T>
2157 struct node_wrapper<volatile T>
2158 {
2159 using type = std::add_volatile_t<typename node_wrapper<T>::type>;
2160 };
2161 template <typename T>
2162 struct node_wrapper<const volatile T>
2163 {
2164 using type = std::add_const_t<std::add_volatile_t<typename node_wrapper<T>::type>>;
2165 };
2166 template <>
2167 struct node_wrapper<std::string>
2168 {
2169 using type = value<std::string>;
2170 };
2171 template <>
2172 struct node_wrapper<int64_t>
2173 {
2174 using type = value<int64_t>;
2175 };
2176 template <>
2177 struct node_wrapper<double>
2178 {
2179 using type = value<double>;
2180 };
2181 template <>
2182 struct node_wrapper<bool>
2183 {
2184 using type = value<bool>;
2185 };
2186 template <>
2187 struct node_wrapper<date>
2188 {
2189 using type = value<date>;
2190 };
2191 template <>
2192 struct node_wrapper<time>
2193 {
2194 using type = value<time>;
2195 };
2196 template <>
2197 struct node_wrapper<date_time>
2198 {
2199 using type = value<date_time>;
2200 };
2201 template <typename T>
2202 using wrap_node = typename node_wrapper<std::remove_reference_t<T>>::type;
2203
2204 // nodes => native value types
2205 template <typename T>
2206 struct node_unwrapper
2207 {
2208 using type = T;
2209 };
2210 template <typename T>
2211 struct node_unwrapper<value<T>>
2212 {
2213 using type = T;
2214 };
2215 template <typename T>
2216 struct node_unwrapper<const value<T>>
2217 {
2218 using type = std::add_const_t<T>;
2219 };
2220 template <typename T>
2221 struct node_unwrapper<volatile value<T>>
2222 {
2223 using type = std::add_volatile_t<T>;
2224 };
2225 template <typename T>
2226 struct node_unwrapper<const volatile value<T>>
2227 {
2228 using type = std::add_volatile_t<std::add_const_t<T>>;
2229 };
2230 template <typename T>
2231 using unwrap_node = typename node_unwrapper<std::remove_reference_t<T>>::type;
2232
2233 template <typename T>
2234 struct node_type_getter
2235 {
2236 static constexpr auto value = value_traits<T>::type;
2237 };
2238 template <>
2239 struct node_type_getter<table>
2240 {
2241 static constexpr auto value = node_type::table;
2242 };
2243 template <>
2244 struct node_type_getter<array>
2245 {
2246 static constexpr auto value = node_type::array;
2247 };
2248 template <>
2249 struct node_type_getter<void>
2250 {
2251 static constexpr auto value = node_type::none;
2252 };
2253 template <typename T>
2254 inline constexpr node_type node_type_of = node_type_getter<unwrap_node<remove_cvref<T>>>::value;
2255
2256 template <typename T, typename ConvertFrom>
2257 inline constexpr bool is_constructible_or_convertible = std::is_constructible_v<T, ConvertFrom> //
2258 || std::is_convertible_v<ConvertFrom, T>;
2259 }
2260 TOML_IMPL_NAMESPACE_END;
2261
2262 TOML_NAMESPACE_START
2263 {
2264 template <typename T>
2265 inline constexpr bool is_table = std::is_same_v<impl::remove_cvref<T>, table>;
2266
2267 template <typename T>
2268 inline constexpr bool is_array = std::is_same_v<impl::remove_cvref<T>, array>;
2269
2270 template <typename T>
2271 inline constexpr bool is_container = is_table<T> || is_array<T>;
2272
2273 template <typename T>
2274 inline constexpr bool is_string = std::is_same_v< //
2275 impl::remove_cvref<impl::wrap_node<impl::remove_cvref<T>>>, //
2276 value<std::string>>;
2277
2278 template <typename T>
2279 inline constexpr bool is_integer = std::is_same_v< //
2280 impl::remove_cvref<impl::wrap_node<impl::remove_cvref<T>>>, //
2281 value<int64_t>>;
2282
2283 template <typename T>
2284 inline constexpr bool is_floating_point = std::is_same_v< //
2285 impl::remove_cvref<impl::wrap_node<impl::remove_cvref<T>>>, //
2286 value<double>>;
2287
2288 template <typename T>
2289 inline constexpr bool is_number = is_integer<T> || is_floating_point<T>;
2290
2291 template <typename T>
2292 inline constexpr bool is_boolean = std::is_same_v< //
2293 impl::remove_cvref<impl::wrap_node<impl::remove_cvref<T>>>, //
2294 value<bool>>;
2295
2296 template <typename T>
2297 inline constexpr bool is_date = std::is_same_v< //
2298 impl::remove_cvref<impl::wrap_node<impl::remove_cvref<T>>>, //
2299 value<date>>;
2300
2301 template <typename T>
2302 inline constexpr bool is_time = std::is_same_v< //
2303 impl::remove_cvref<impl::wrap_node<impl::remove_cvref<T>>>, //
2304 value<time>>;
2305
2306 template <typename T>
2307 inline constexpr bool is_date_time = std::is_same_v< //
2308 impl::remove_cvref<impl::wrap_node<impl::remove_cvref<T>>>, //
2309 value<date_time>>;
2310
2311 template <typename T>
2312 inline constexpr bool is_chronological = is_date<T> || is_time<T> || is_date_time<T>;
2313
2314 template <typename T>
2315 inline constexpr bool is_value = is_string<T> || is_number<T> || is_boolean<T> || is_chronological<T>;
2316
2317 template <typename T>
2318 inline constexpr bool is_node = std::is_same_v<toml::node, impl::remove_cvref<T>> //
2319 || std::is_base_of_v<toml::node, impl::remove_cvref<T>>;
2320
2321 template <typename T>
2322 inline constexpr bool is_node_view = impl::is_one_of<impl::remove_cvref<T>, node_view<node>, node_view<const node>>;
2323 }
2324 TOML_NAMESPACE_END;
2325
2326 TOML_IMPL_NAMESPACE_START
2327 {
2328 template <typename T>
2329 TOML_CONST_INLINE_GETTER
2330 constexpr std::underlying_type_t<T> unwrap_enum(T val) noexcept
2331 {
2332 return static_cast<std::underlying_type_t<T>>(val);
2333 }
2334
2335 // Q: "why not use std::fpclassify?"
2336 // A: Because it gets broken by -ffast-math and friends
2337 enum class TOML_CLOSED_ENUM fp_class : unsigned
2338 {
2339 ok,
2340 neg_inf,
2341 pos_inf,
2342 nan
2343 };
2344
2345 TOML_PURE_GETTER
2346 inline fp_class fpclassify(const double& val) noexcept
2347 {
2348 static_assert(sizeof(uint64_t) == sizeof(double));
2349
2350 static constexpr uint64_t sign = 0b1000000000000000000000000000000000000000000000000000000000000000ull;
2351 static constexpr uint64_t exponent = 0b0111111111110000000000000000000000000000000000000000000000000000ull;
2352 static constexpr uint64_t mantissa = 0b0000000000001111111111111111111111111111111111111111111111111111ull;
2353
2354 uint64_t val_bits;
2355 std::memcpy(&val_bits, &val, sizeof(val));
2356 if ((val_bits & exponent) != exponent)
2357 return fp_class::ok;
2358 if ((val_bits & mantissa))
2359 return fp_class::nan;
2360 return (val_bits & sign) ? fp_class::neg_inf : fp_class::pos_inf;
2361 }
2362
2363 // Q: "why not use std::find and std::min?"
2364 // A: Because <algorithm> is _huge_ and these would be the only things I used from it.
2365 // I don't want to impose such a heavy compile-time burden on users.
2366
2367 template <typename Iterator, typename T>
2368 TOML_PURE_GETTER
2369 inline auto find(Iterator start, Iterator end, const T& needle) noexcept //
2370 ->decltype(&(*start))
2371 {
2372 for (; start != end; start++)
2373 if (*start == needle)
2374 return &(*start);
2375 return nullptr;
2376 }
2377
2378 template <typename T>
2379 TOML_PURE_INLINE_GETTER
2380 constexpr const T& min(const T& a, const T& b) noexcept //
2381 {
2382 return a < b ? a : b;
2383 }
2384 }
2385 TOML_IMPL_NAMESPACE_END;
2386
2387 #ifdef _MSC_VER
2388 #pragma pop_macro("min")
2389 #pragma pop_macro("max")
2390 #ifndef __clang__
2391 #pragma inline_recursion(off)
2392 #endif
2393 #endif
2394 TOML_POP_WARNINGS;
2395
2396 //******** impl/print_to_stream.hpp **********************************************************************************
2397
2398 TOML_PUSH_WARNINGS;
2399 #ifdef _MSC_VER
2400 #ifndef __clang__
2401 #pragma inline_recursion(on)
2402 #endif
2403 #pragma push_macro("min")
2404 #pragma push_macro("max")
2405 #undef min
2406 #undef max
2407 #endif
2408
2409 TOML_IMPL_NAMESPACE_START
2410 {
2411 // Q: "why does print_to_stream() exist? why not just use ostream::write(), ostream::put() etc?"
2412 // A: - I'm using <charconv> to format numerics. Faster and locale-independent.
2413 // - I can (potentially) avoid forcing users to drag in <sstream> and <iomanip>.
2414 // - Strings in C++. Honestly.
2415
2416 TOML_EXPORTED_FREE_FUNCTION
2417 TOML_ATTR(nonnull)
2418 void TOML_CALLCONV print_to_stream(std::ostream&, const char*, size_t);
2419
2420 TOML_EXPORTED_FREE_FUNCTION
2421 void TOML_CALLCONV print_to_stream(std::ostream&, std::string_view);
2422
2423 TOML_EXPORTED_FREE_FUNCTION
2424 void TOML_CALLCONV print_to_stream(std::ostream&, const std::string&);
2425
2426 TOML_EXPORTED_FREE_FUNCTION
2427 void TOML_CALLCONV print_to_stream(std::ostream&, char);
2428
2429 TOML_EXPORTED_FREE_FUNCTION
2430 void TOML_CALLCONV print_to_stream(std::ostream&, signed char, value_flags = {}, size_t min_digits = 0);
2431
2432 TOML_EXPORTED_FREE_FUNCTION
2433 void TOML_CALLCONV print_to_stream(std::ostream&, signed short, value_flags = {}, size_t min_digits = 0);
2434
2435 TOML_EXPORTED_FREE_FUNCTION
2436 void TOML_CALLCONV print_to_stream(std::ostream&, signed int, value_flags = {}, size_t min_digits = 0);
2437
2438 TOML_EXPORTED_FREE_FUNCTION
2439 void TOML_CALLCONV print_to_stream(std::ostream&, signed long, value_flags = {}, size_t min_digits = 0);
2440
2441 TOML_EXPORTED_FREE_FUNCTION
2442 void TOML_CALLCONV print_to_stream(std::ostream&, signed long long, value_flags = {}, size_t min_digits = 0);
2443
2444 TOML_EXPORTED_FREE_FUNCTION
2445 void TOML_CALLCONV print_to_stream(std::ostream&, unsigned char, value_flags = {}, size_t min_digits = 0);
2446
2447 TOML_EXPORTED_FREE_FUNCTION
2448 void TOML_CALLCONV print_to_stream(std::ostream&, unsigned short, value_flags = {}, size_t min_digits = 0);
2449
2450 TOML_EXPORTED_FREE_FUNCTION
2451 void TOML_CALLCONV print_to_stream(std::ostream&, unsigned int, value_flags = {}, size_t min_digits = 0);
2452
2453 TOML_EXPORTED_FREE_FUNCTION
2454 void TOML_CALLCONV print_to_stream(std::ostream&, unsigned long, value_flags = {}, size_t min_digits = 0);
2455
2456 TOML_EXPORTED_FREE_FUNCTION
2457 void TOML_CALLCONV print_to_stream(std::ostream&, unsigned long long, value_flags = {}, size_t min_digits = 0);
2458
2459 TOML_EXPORTED_FREE_FUNCTION
2460 void TOML_CALLCONV print_to_stream(std::ostream&, float, value_flags = {}, bool relaxed_precision = false);
2461
2462 TOML_EXPORTED_FREE_FUNCTION
2463 void TOML_CALLCONV print_to_stream(std::ostream&, double, value_flags = {}, bool relaxed_precision = false);
2464
2465 TOML_EXPORTED_FREE_FUNCTION
2466 void TOML_CALLCONV print_to_stream(std::ostream&, bool);
2467
2468 TOML_EXPORTED_FREE_FUNCTION
2469 void TOML_CALLCONV print_to_stream(std::ostream&, const toml::date&);
2470
2471 TOML_EXPORTED_FREE_FUNCTION
2472 void TOML_CALLCONV print_to_stream(std::ostream&, const toml::time&);
2473
2474 TOML_EXPORTED_FREE_FUNCTION
2475 void TOML_CALLCONV print_to_stream(std::ostream&, const toml::time_offset&);
2476
2477 TOML_EXPORTED_FREE_FUNCTION
2478 void TOML_CALLCONV print_to_stream(std::ostream&, const toml::date_time&);
2479
2480 TOML_EXPORTED_FREE_FUNCTION
2481 void TOML_CALLCONV print_to_stream(std::ostream&, const source_position&);
2482
2483 TOML_EXPORTED_FREE_FUNCTION
2484 void TOML_CALLCONV print_to_stream(std::ostream&, const source_region&);
2485
2486 #if TOML_ENABLE_FORMATTERS
2487
2488 TOML_EXPORTED_FREE_FUNCTION
2489 void TOML_CALLCONV print_to_stream(std::ostream&, const array&);
2490
2491 TOML_EXPORTED_FREE_FUNCTION
2492 void TOML_CALLCONV print_to_stream(std::ostream&, const table&);
2493
2494 TOML_EXPORTED_FREE_FUNCTION
2495 void TOML_CALLCONV print_to_stream(std::ostream&, const value<std::string>&);
2496
2497 TOML_EXPORTED_FREE_FUNCTION
2498 void TOML_CALLCONV print_to_stream(std::ostream&, const value<int64_t>&);
2499
2500 TOML_EXPORTED_FREE_FUNCTION
2501 void TOML_CALLCONV print_to_stream(std::ostream&, const value<double>&);
2502
2503 TOML_EXPORTED_FREE_FUNCTION
2504 void TOML_CALLCONV print_to_stream(std::ostream&, const value<bool>&);
2505
2506 TOML_EXPORTED_FREE_FUNCTION
2507 void TOML_CALLCONV print_to_stream(std::ostream&, const value<date>&);
2508
2509 TOML_EXPORTED_FREE_FUNCTION
2510 void TOML_CALLCONV print_to_stream(std::ostream&, const value<time>&);
2511
2512 TOML_EXPORTED_FREE_FUNCTION
2513 void TOML_CALLCONV print_to_stream(std::ostream&, const value<date_time>&);
2514
2515 #endif
2516
2517 template <typename T, typename U>
2518 inline void print_to_stream_bookended(std::ostream & stream, const T& val, const U& bookend)
2519 {
2520 print_to_stream(stream, bookend);
2521 print_to_stream(stream, val);
2522 print_to_stream(stream, bookend);
2523 }
2524 }
2525 TOML_IMPL_NAMESPACE_END;
2526
2527 #ifdef _MSC_VER
2528 #pragma pop_macro("min")
2529 #pragma pop_macro("max")
2530 #ifndef __clang__
2531 #pragma inline_recursion(off)
2532 #endif
2533 #endif
2534 TOML_POP_WARNINGS;
2535
2536 //******** impl/source_region.hpp ************************************************************************************
2537
2538 TOML_PUSH_WARNINGS;
2539 #ifdef _MSC_VER
2540 #ifndef __clang__
2541 #pragma inline_recursion(on)
2542 #endif
2543 #pragma push_macro("min")
2544 #pragma push_macro("max")
2545 #undef min
2546 #undef max
2547 #endif
2548
2549 TOML_NAMESPACE_START
2550 {
2551 using source_index = uint32_t;
2552
2553 using source_path_ptr = std::shared_ptr<const std::string>;
2554
2555 struct TOML_TRIVIAL_ABI source_position
2556 {
2557 source_index line;
2558
2559 source_index column;
2560
2561 TOML_PURE_GETTER
2562 explicit constexpr operator bool() const noexcept
2563 {
2564 return line > source_index{} //
2565 && column > source_index{};
2566 }
2567
2568 TOML_PURE_GETTER
2569 friend constexpr bool operator==(const source_position& lhs, const source_position& rhs) noexcept
2570 {
2571 return lhs.line == rhs.line //
2572 && lhs.column == rhs.column;
2573 }
2574
2575 TOML_PURE_INLINE_GETTER
2576 friend constexpr bool operator!=(const source_position& lhs, const source_position& rhs) noexcept
2577 {
2578 return !(lhs == rhs);
2579 }
2580
2581 private:
2582
2583 TOML_PURE_GETTER
2584 static constexpr uint64_t pack(const source_position& pos) noexcept
2585 {
2586 return static_cast<uint64_t>(pos.line) << 32 | static_cast<uint64_t>(pos.column);
2587 }
2588
2589 public:
2590
2591 TOML_PURE_GETTER
2592 friend constexpr bool operator<(const source_position& lhs, const source_position& rhs) noexcept
2593 {
2594 return pack(lhs) < pack(rhs);
2595 }
2596
2597 TOML_PURE_GETTER
2598 friend constexpr bool operator<=(const source_position& lhs, const source_position& rhs) noexcept
2599 {
2600 return pack(lhs) <= pack(rhs);
2601 }
2602
2603 TOML_PURE_GETTER
2604 friend constexpr bool operator>(const source_position& lhs, const source_position& rhs) noexcept
2605 {
2606 return pack(lhs) > pack(rhs);
2607 }
2608
2609 TOML_PURE_GETTER
2610 friend constexpr bool operator>=(const source_position& lhs, const source_position& rhs) noexcept
2611 {
2612 return pack(lhs) >= pack(rhs);
2613 }
2614
2615 friend std::ostream& operator<<(std::ostream& lhs, const source_position& rhs)
2616 {
2617 impl::print_to_stream(lhs, rhs);
2618 return lhs;
2619 }
2620 };
2621
2622 struct source_region
2623 {
2624 source_position begin;
2625
2626 source_position end;
2627
2628 source_path_ptr path;
2629
2630 #if TOML_ENABLE_WINDOWS_COMPAT
2631
2632 TOML_NODISCARD
2633 optional<std::wstring> wide_path() const
2634 {
2635 if (!path || path->empty())
2636 return {};
2637 return { impl::widen(*path) };
2638 }
2639
2640 #endif
2641
2642 friend std::ostream& operator<<(std::ostream& lhs, const source_region& rhs)
2643 {
2644 impl::print_to_stream(lhs, rhs);
2645 return lhs;
2646 }
2647 };
2648
2649 TOML_NODISCARD
2650 constexpr optional<std::string_view> get_line(std::string_view doc, source_index line_num) noexcept
2651 {
2652 if (line_num == 0)
2653 {
2654 // Invalid line number. Should be greater than zero.
2655 return {};
2656 }
2657
2658 // The position of the first character of the specified line.
2659 const auto begin_of_line = [doc, line_num]() -> std::size_t
2660 {
2661 if (line_num == 1)
2662 {
2663 return 0;
2664 }
2665
2666 const auto num_chars_of_doc = doc.size();
2667 std::size_t current_line_num{ 1 };
2668
2669 for (std::size_t i{}; i < num_chars_of_doc; ++i)
2670 {
2671 if (doc[i] == '\n')
2672 {
2673 ++current_line_num;
2674
2675 if (current_line_num == line_num)
2676 {
2677 return i + 1;
2678 }
2679 }
2680 }
2681 return std::string_view::npos;
2682 }();
2683
2684 if (begin_of_line >= doc.size())
2685 {
2686 return {};
2687 }
2688
2689 if (const auto end_of_line = doc.find('\n', begin_of_line); end_of_line != std::string_view::npos)
2690 {
2691 const auto num_chars_of_line = end_of_line - begin_of_line;
2692
2693 // Trim an optional trailing carriage return.
2694 return doc.substr(begin_of_line,
2695 ((num_chars_of_line > 0) && (doc[end_of_line - 1] == '\r')) ? num_chars_of_line - 1
2696 : num_chars_of_line);
2697 }
2698
2699 // Return the last line. Apparently this doc has no trailing line break character at the end.
2700 return doc.substr(begin_of_line);
2701 }
2702 }
2703 TOML_NAMESPACE_END;
2704
2705 #ifdef _MSC_VER
2706 #pragma pop_macro("min")
2707 #pragma pop_macro("max")
2708 #ifndef __clang__
2709 #pragma inline_recursion(off)
2710 #endif
2711 #endif
2712 TOML_POP_WARNINGS;
2713
2714 //******** impl/date_time.hpp ****************************************************************************************
2715
2716 TOML_PUSH_WARNINGS;
2717 #ifdef _MSC_VER
2718 #ifndef __clang__
2719 #pragma inline_recursion(on)
2720 #endif
2721 #pragma push_macro("min")
2722 #pragma push_macro("max")
2723 #undef min
2724 #undef max
2725 #endif
2726
2727 TOML_NAMESPACE_START
2728 {
2729 struct TOML_TRIVIAL_ABI date
2730 {
2731 uint16_t year;
2732
2733 uint8_t month;
2734
2735 uint8_t day;
2736
2737 TOML_NODISCARD_CTOR
2738 date() noexcept = default;
2739
2740 TOML_CONSTRAINED_TEMPLATE((impl::all_integral<Y, M, D>), typename Y, typename M, typename D)
2741 TOML_NODISCARD_CTOR
2742 constexpr date(Y y, M m, D d) noexcept //
2743 : year{ static_cast<uint16_t>(y) },
2744 month{ static_cast<uint8_t>(m) },
2745 day{ static_cast<uint8_t>(d) }
2746 {}
2747
2748 TOML_PURE_GETTER
2749 friend constexpr bool operator==(const date& lhs, const date& rhs) noexcept
2750 {
2751 return lhs.year == rhs.year //
2752 && lhs.month == rhs.month //
2753 && lhs.day == rhs.day;
2754 }
2755
2756 TOML_PURE_INLINE_GETTER
2757 friend constexpr bool operator!=(const date& lhs, const date& rhs) noexcept
2758 {
2759 return !(lhs == rhs);
2760 }
2761
2762 private:
2763
2764 TOML_PURE_GETTER
2765 static constexpr uint32_t pack(const date& d) noexcept
2766 {
2767 return (static_cast<uint32_t>(d.year) << 16) | (static_cast<uint32_t>(d.month) << 8)
2768 | static_cast<uint32_t>(d.day);
2769 }
2770
2771 public:
2772
2773 TOML_PURE_GETTER
2774 friend constexpr bool operator<(const date& lhs, const date& rhs) noexcept
2775 {
2776 return pack(lhs) < pack(rhs);
2777 }
2778
2779 TOML_PURE_GETTER
2780 friend constexpr bool operator<=(const date& lhs, const date& rhs) noexcept
2781 {
2782 return pack(lhs) <= pack(rhs);
2783 }
2784
2785 TOML_PURE_GETTER
2786 friend constexpr bool operator>(const date& lhs, const date& rhs) noexcept
2787 {
2788 return pack(lhs) > pack(rhs);
2789 }
2790
2791 TOML_PURE_GETTER
2792 friend constexpr bool operator>=(const date& lhs, const date& rhs) noexcept
2793 {
2794 return pack(lhs) >= pack(rhs);
2795 }
2796
2797 friend std::ostream& operator<<(std::ostream& lhs, const date& rhs)
2798 {
2799 impl::print_to_stream(lhs, rhs);
2800 return lhs;
2801 }
2802 };
2803
2804 struct TOML_TRIVIAL_ABI time
2805 {
2806 uint8_t hour;
2807
2808 uint8_t minute;
2809
2810 uint8_t second;
2811
2812 uint32_t nanosecond;
2813
2814 TOML_NODISCARD_CTOR
2815 time() noexcept = default;
2816
2817 TOML_CONSTRAINED_TEMPLATE((impl::all_integral<H, M, S, NS>),
2818 typename H,
2819 typename M,
2820 typename S = uint8_t,
2821 typename NS = uint32_t)
2822 TOML_NODISCARD_CTOR
2823 constexpr time(H h, M m, S s = S{}, NS ns = NS{}) noexcept //
2824 : hour{ static_cast<uint8_t>(h) },
2825 minute{ static_cast<uint8_t>(m) },
2826 second{ static_cast<uint8_t>(s) },
2827 nanosecond{ static_cast<uint32_t>(ns) }
2828 {}
2829
2830 TOML_PURE_GETTER
2831 friend constexpr bool operator==(const time& lhs, const time& rhs) noexcept
2832 {
2833 return lhs.hour == rhs.hour //
2834 && lhs.minute == rhs.minute //
2835 && lhs.second == rhs.second //
2836 && lhs.nanosecond == rhs.nanosecond;
2837 }
2838
2839 TOML_PURE_INLINE_GETTER
2840 friend constexpr bool operator!=(const time& lhs, const time& rhs) noexcept
2841 {
2842 return !(lhs == rhs);
2843 }
2844
2845 private:
2846
2847 TOML_PURE_GETTER
2848 static constexpr uint64_t pack(const time& t) noexcept
2849 {
2850 return static_cast<uint64_t>(t.hour) << 48 | static_cast<uint64_t>(t.minute) << 40
2851 | static_cast<uint64_t>(t.second) << 32 | static_cast<uint64_t>(t.nanosecond);
2852 }
2853
2854 public:
2855
2856 TOML_PURE_GETTER
2857 friend constexpr bool operator<(const time& lhs, const time& rhs) noexcept
2858 {
2859 return pack(lhs) < pack(rhs);
2860 }
2861
2862 TOML_PURE_GETTER
2863 friend constexpr bool operator<=(const time& lhs, const time& rhs) noexcept
2864 {
2865 return pack(lhs) <= pack(rhs);
2866 }
2867
2868 TOML_PURE_GETTER
2869 friend constexpr bool operator>(const time& lhs, const time& rhs) noexcept
2870 {
2871 return pack(lhs) > pack(rhs);
2872 }
2873
2874 TOML_PURE_GETTER
2875 friend constexpr bool operator>=(const time& lhs, const time& rhs) noexcept
2876 {
2877 return pack(lhs) >= pack(rhs);
2878 }
2879
2880 friend std::ostream& operator<<(std::ostream& lhs, const time& rhs)
2881 {
2882 impl::print_to_stream(lhs, rhs);
2883 return lhs;
2884 }
2885 };
2886
2887 struct TOML_TRIVIAL_ABI time_offset
2888 {
2889 int16_t minutes;
2890
2891 TOML_NODISCARD_CTOR
2892 time_offset() noexcept = default;
2893
2894 TOML_CONSTRAINED_TEMPLATE((impl::all_integral<H, M>), typename H, typename M)
2895 TOML_NODISCARD_CTOR
2896 constexpr time_offset(H h, M m) noexcept //
2897 : minutes{ static_cast<int16_t>(static_cast<impl::common_signed_type<H, M>>(h)
2898 * impl::common_signed_type<H, M>{ 60 }
2899 + static_cast<impl::common_signed_type<H, M>>(m)) }
2900 {}
2901
2902 TOML_PURE_INLINE_GETTER
2903 friend constexpr bool operator==(time_offset lhs, time_offset rhs) noexcept
2904 {
2905 return lhs.minutes == rhs.minutes;
2906 }
2907
2908 TOML_PURE_INLINE_GETTER
2909 friend constexpr bool operator!=(time_offset lhs, time_offset rhs) noexcept
2910 {
2911 return lhs.minutes != rhs.minutes;
2912 }
2913
2914 TOML_PURE_INLINE_GETTER
2915 friend constexpr bool operator<(time_offset lhs, time_offset rhs) noexcept
2916 {
2917 return lhs.minutes < rhs.minutes;
2918 }
2919
2920 TOML_PURE_INLINE_GETTER
2921 friend constexpr bool operator<=(time_offset lhs, time_offset rhs) noexcept
2922 {
2923 return lhs.minutes <= rhs.minutes;
2924 }
2925
2926 TOML_PURE_INLINE_GETTER
2927 friend constexpr bool operator>(time_offset lhs, time_offset rhs) noexcept
2928 {
2929 return lhs.minutes > rhs.minutes;
2930 }
2931
2932 TOML_PURE_INLINE_GETTER
2933 friend constexpr bool operator>=(time_offset lhs, time_offset rhs) noexcept
2934 {
2935 return lhs.minutes >= rhs.minutes;
2936 }
2937
2938 friend std::ostream& operator<<(std::ostream& lhs, const time_offset& rhs)
2939 {
2940 impl::print_to_stream(lhs, rhs);
2941 return lhs;
2942 }
2943 };
2944
2945 TOML_ABI_NAMESPACE_BOOL(TOML_HAS_CUSTOM_OPTIONAL_TYPE, custopt, stdopt);
2946
2947 struct date_time
2948 {
2949 toml::date date;
2950
2951 toml::time time;
2952
2953 optional<toml::time_offset> offset;
2954
2955 TOML_NODISCARD_CTOR
2956 date_time() noexcept = default;
2957
2958 TOML_NODISCARD_CTOR
2959 constexpr date_time(const toml::date& d, const toml::time& t) noexcept //
2960 : date{ d },
2961 time{ t },
2962 offset{} // TINAE - icc bugfix
2963 {}
2964
2965 TOML_NODISCARD_CTOR
2966 explicit constexpr date_time(const toml::date& d) noexcept //
2967 : date{ d },
2968 time{},
2969 offset{} // TINAE - icc bugfix
2970 {}
2971
2972 TOML_NODISCARD_CTOR
2973 explicit constexpr date_time(const toml::time& t) noexcept //
2974 : date{},
2975 time{ t },
2976 offset{} // TINAE - icc bugfix
2977 {}
2978
2979 TOML_NODISCARD_CTOR
2980 constexpr date_time(const toml::date& d, const toml::time& t, const toml::time_offset& off) noexcept
2981 : date{ d },
2982 time{ t },
2983 offset{ off }
2984 {}
2985
2986 TOML_PURE_INLINE_GETTER
2987 constexpr bool is_local() const noexcept
2988 {
2989 return !offset.has_value();
2990 }
2991
2992 TOML_PURE_GETTER
2993 friend constexpr bool operator==(const date_time& lhs, const date_time& rhs) noexcept
2994 {
2995 return lhs.date == rhs.date //
2996 && lhs.time == rhs.time //
2997 && lhs.offset == rhs.offset;
2998 }
2999
3000 TOML_PURE_INLINE_GETTER
3001 friend constexpr bool operator!=(const date_time& lhs, const date_time& rhs) noexcept
3002 {
3003 return !(lhs == rhs);
3004 }
3005
3006 TOML_PURE_GETTER
3007 friend constexpr bool operator<(const date_time& lhs, const date_time& rhs) noexcept
3008 {
3009 if (lhs.date != rhs.date)
3010 return lhs.date < rhs.date;
3011 if (lhs.time != rhs.time)
3012 return lhs.time < rhs.time;
3013 return lhs.offset < rhs.offset;
3014 }
3015
3016 TOML_PURE_GETTER
3017 friend constexpr bool operator<=(const date_time& lhs, const date_time& rhs) noexcept
3018 {
3019 if (lhs.date != rhs.date)
3020 return lhs.date < rhs.date;
3021 if (lhs.time != rhs.time)
3022 return lhs.time < rhs.time;
3023 return lhs.offset <= rhs.offset;
3024 }
3025
3026 TOML_PURE_INLINE_GETTER
3027 friend constexpr bool operator>(const date_time& lhs, const date_time& rhs) noexcept
3028 {
3029 return !(lhs <= rhs);
3030 }
3031
3032 TOML_PURE_INLINE_GETTER
3033 friend constexpr bool operator>=(const date_time& lhs, const date_time& rhs) noexcept
3034 {
3035 return !(lhs < rhs);
3036 }
3037
3038 friend std::ostream& operator<<(std::ostream& lhs, const date_time& rhs)
3039 {
3040 impl::print_to_stream(lhs, rhs);
3041 return lhs;
3042 }
3043 };
3044
3045 TOML_ABI_NAMESPACE_END; // TOML_HAS_CUSTOM_OPTIONAL_TYPE
3046 }
3047 TOML_NAMESPACE_END;
3048
3049 #ifdef _MSC_VER
3050 #pragma pop_macro("min")
3051 #pragma pop_macro("max")
3052 #ifndef __clang__
3053 #pragma inline_recursion(off)
3054 #endif
3055 #endif
3056 TOML_POP_WARNINGS;
3057
3058 //******** impl/at_path.hpp ******************************************************************************************
3059
3060 TOML_IMPL_NAMESPACE_START
3061 {
3062 template <typename T>
3063 using parse_path_callback = bool(TOML_CALLCONV*)(void*, T);
3064
3065 TOML_NODISCARD
3066 bool TOML_CALLCONV parse_path(std::string_view,
3067 void*,
3068 parse_path_callback<std::string_view>,
3069 parse_path_callback<size_t>);
3070 }
3071 TOML_IMPL_NAMESPACE_END;
3072
3073 TOML_NAMESPACE_START
3074 {
3075 TOML_NODISCARD
3076 TOML_EXPORTED_FREE_FUNCTION
3077 node_view<node> TOML_CALLCONV at_path(node & root, std::string_view path) noexcept;
3078
3079 TOML_NODISCARD
3080 TOML_EXPORTED_FREE_FUNCTION
3081 node_view<const node> TOML_CALLCONV at_path(const node& root, std::string_view path) noexcept;
3082
3083 #if TOML_ENABLE_WINDOWS_COMPAT
3084
3085 TOML_NODISCARD
3086 TOML_EXPORTED_FREE_FUNCTION
3087 node_view<node> TOML_CALLCONV at_path(node & root, std::wstring_view path);
3088
3089 TOML_NODISCARD
3090 TOML_EXPORTED_FREE_FUNCTION
3091 node_view<const node> TOML_CALLCONV at_path(const node& root, std::wstring_view path);
3092
3093 #endif
3094 }
3095 TOML_NAMESPACE_END;
3096
3097 //******** impl/std_vector.hpp ***************************************************************************************
3098
3099 TOML_DISABLE_WARNINGS;
3100 #include <vector>
3101 #include <iterator>
3102 TOML_ENABLE_WARNINGS;
3103
3104 //******** impl/path.hpp *********************************************************************************************
3105
3106 TOML_PUSH_WARNINGS;
3107 #ifdef _MSC_VER
3108 #ifndef __clang__
3109 #pragma inline_recursion(on)
3110 #endif
3111 #pragma push_macro("min")
3112 #pragma push_macro("max")
3113 #undef min
3114 #undef max
3115 #endif
3116
3117 TOML_NAMESPACE_START
3118 {
3119 enum class TOML_CLOSED_ENUM path_component_type : uint8_t
3120 {
3121 key = 0x1,
3122 array_index = 0x2
3123 };
3124
3125 class TOML_EXPORTED_CLASS path_component
3126 {
3127 struct storage_t
3128 {
3129 static constexpr size_t size =
3130 (sizeof(size_t) < sizeof(std::string) ? sizeof(std::string) : sizeof(size_t));
3131 static constexpr size_t align =
3132 (alignof(size_t) < alignof(std::string) ? alignof(std::string) : alignof(size_t));
3133
3134 alignas(align) unsigned char bytes[size];
3135 };
3136 alignas(storage_t::align) mutable storage_t value_storage_;
3137
3138 path_component_type type_;
3139
3140 TOML_PURE_GETTER
3141 TOML_EXPORTED_STATIC_FUNCTION
3142 static bool TOML_CALLCONV equal(const path_component&, const path_component&) noexcept;
3143
3144 template <typename Type>
3145 TOML_PURE_INLINE_GETTER
3146 static Type* get_as(storage_t& s) noexcept
3147 {
3148 return TOML_LAUNDER(reinterpret_cast<Type*>(s.bytes));
3149 }
3150
3151 static void store_key(std::string_view key, storage_t& storage_)
3152 {
3153 ::new (static_cast<void*>(storage_.bytes)) std::string{ key };
3154 }
3155
3156 static void store_index(size_t index, storage_t& storage_) noexcept
3157 {
3158 ::new (static_cast<void*>(storage_.bytes)) std::size_t{ index };
3159 }
3160
3161 void destroy() noexcept
3162 {
3163 if (type_ == path_component_type::key)
3164 get_as<std::string>(value_storage_)->~basic_string();
3165 }
3166
3167 TOML_NODISCARD
3168 size_t& index_ref() noexcept
3169 {
3170 TOML_ASSERT_ASSUME(type_ == path_component_type::array_index);
3171 return *get_as<size_t>(value_storage_);
3172 }
3173
3174 TOML_NODISCARD
3175 std::string& key_ref() noexcept
3176 {
3177 TOML_ASSERT_ASSUME(type_ == path_component_type::key);
3178 return *get_as<std::string>(value_storage_);
3179 }
3180
3181 public:
3182
3183 TOML_NODISCARD_CTOR
3184 TOML_EXPORTED_MEMBER_FUNCTION
3185 path_component();
3186
3187 TOML_NODISCARD_CTOR
3188 TOML_EXPORTED_MEMBER_FUNCTION
3189 path_component(size_t index) noexcept;
3190
3191 TOML_NODISCARD_CTOR
3192 TOML_EXPORTED_MEMBER_FUNCTION
3193 path_component(std::string_view key);
3194
3195 #if TOML_ENABLE_WINDOWS_COMPAT
3196
3197 TOML_NODISCARD_CTOR
3198 TOML_EXPORTED_MEMBER_FUNCTION
3199 path_component(std::wstring_view key);
3200
3201 #endif
3202
3203 TOML_NODISCARD_CTOR
3204 TOML_EXPORTED_MEMBER_FUNCTION
3205 path_component(const path_component& pc);
3206
3207 TOML_NODISCARD_CTOR
3208 TOML_EXPORTED_MEMBER_FUNCTION
3209 path_component(path_component&& pc) noexcept;
3210
3211 TOML_EXPORTED_MEMBER_FUNCTION
3212 path_component& operator=(const path_component& rhs);
3213
3214 TOML_EXPORTED_MEMBER_FUNCTION
3215 path_component& operator=(path_component&& rhs) noexcept;
3216
3217 TOML_EXPORTED_MEMBER_FUNCTION
3218 path_component& operator=(size_t new_index) noexcept;
3219
3220 TOML_EXPORTED_MEMBER_FUNCTION
3221 path_component& operator=(std::string_view new_key);
3222
3223 #if TOML_ENABLE_WINDOWS_COMPAT
3224
3225 TOML_EXPORTED_MEMBER_FUNCTION
3226 path_component& operator=(std::wstring_view new_key);
3227
3228 #endif
3229
3230 ~path_component() noexcept
3231 {
3232 destroy();
3233 }
3234
3235 TOML_PURE_GETTER
3236 size_t index() const noexcept
3237 {
3238 TOML_ASSERT_ASSUME(type_ == path_component_type::array_index);
3239 return *get_as<const size_t>(value_storage_);
3240 }
3241
3242 TOML_PURE_INLINE_GETTER
3243 explicit operator size_t() const noexcept
3244 {
3245 return index();
3246 }
3247
3248 TOML_PURE_GETTER
3249 const std::string& key() const noexcept
3250 {
3251 TOML_ASSERT_ASSUME(type_ == path_component_type::key);
3252 return *get_as<const std::string>(value_storage_);
3253 }
3254
3255 TOML_PURE_INLINE_GETTER
3256 explicit operator const std::string&() const noexcept
3257 {
3258 return key();
3259 }
3260
3261 TOML_PURE_INLINE_GETTER
3262 path_component_type type() const noexcept
3263 {
3264 return type_;
3265 }
3266
3267 TOML_PURE_INLINE_GETTER
3268 friend bool operator==(const path_component& lhs, const path_component& rhs) noexcept
3269 {
3270 return equal(lhs, rhs);
3271 }
3272
3273 TOML_PURE_INLINE_GETTER
3274 friend bool operator!=(const path_component& lhs, const path_component& rhs) noexcept
3275 {
3276 return !equal(lhs, rhs);
3277 }
3278 };
3279
3280 class TOML_EXPORTED_CLASS path
3281 {
3282 private:
3283
3284 std::vector<path_component> components_;
3285
3286 TOML_EXPORTED_MEMBER_FUNCTION
3287 void print_to(std::ostream&) const;
3288
3289 TOML_PURE_GETTER
3290 TOML_EXPORTED_STATIC_FUNCTION
3291 static bool TOML_CALLCONV equal(const path&, const path&) noexcept;
3292
3293 public:
3294
3295 TOML_NODISCARD_CTOR
3296 path() noexcept = default;
3297
3298 TOML_NODISCARD_CTOR
3299 TOML_EXPORTED_MEMBER_FUNCTION
3300 explicit path(std::string_view);
3301
3302 #if TOML_ENABLE_WINDOWS_COMPAT
3303
3304 TOML_NODISCARD_CTOR
3305 TOML_EXPORTED_MEMBER_FUNCTION
3306 explicit path(std::wstring_view);
3307
3308 #endif
3309
3310 ~path() noexcept = default;
3311
3312 TOML_NODISCARD_CTOR
3313 path(const path&) = default;
3314
3315 TOML_NODISCARD_CTOR
3316 path(path&&) noexcept = default;
3317
3318 TOML_PURE_INLINE_GETTER
3319 size_t size() const noexcept
3320 {
3321 return components_.size();
3322 }
3323
3324 TOML_PURE_INLINE_GETTER
3325 explicit operator bool() const noexcept
3326 {
3327 return !components_.empty();
3328 }
3329
3330 TOML_PURE_INLINE_GETTER
3331 bool empty() const noexcept
3332 {
3333 return components_.empty();
3334 }
3335
3336 TOML_PURE_INLINE_GETTER
3337 path_component& operator[](size_t index) noexcept
3338 {
3339 TOML_ASSERT(index < size());
3340 return components_[index];
3341 }
3342
3343 TOML_PURE_INLINE_GETTER
3344 const path_component& operator[](size_t index) const noexcept
3345 {
3346 TOML_ASSERT(index < size());
3347 return components_[index];
3348 }
3349
3350 path& operator=(const path&) = default;
3351
3352 path& operator=(path&&) noexcept = default;
3353
3354 TOML_EXPORTED_MEMBER_FUNCTION
3355 path& operator=(std::string_view);
3356
3357 #if TOML_ENABLE_WINDOWS_COMPAT
3358
3359 TOML_EXPORTED_MEMBER_FUNCTION
3360 path& operator=(std::wstring_view);
3361
3362 #endif
3363
3364 TOML_ALWAYS_INLINE
3365 path& assign(const path& p)
3366 {
3367 return *this = p;
3368 }
3369
3370 TOML_ALWAYS_INLINE
3371 path& assign(path&& p) noexcept
3372 {
3373 return *this = std::move(p);
3374 }
3375
3376 TOML_ALWAYS_INLINE
3377 path& assign(std::string_view str)
3378 {
3379 return *this = str;
3380 }
3381
3382 #if TOML_ENABLE_WINDOWS_COMPAT
3383
3384 TOML_ALWAYS_INLINE
3385 path& assign(std::wstring_view str)
3386 {
3387 return *this = str;
3388 }
3389
3390 #endif
3391
3392 TOML_EXPORTED_MEMBER_FUNCTION
3393 path& operator+=(const path&);
3394
3395 TOML_EXPORTED_MEMBER_FUNCTION
3396 path& operator+=(path&&);
3397
3398 TOML_EXPORTED_MEMBER_FUNCTION
3399 path& operator+=(std::string_view);
3400
3401 #if TOML_ENABLE_WINDOWS_COMPAT
3402
3403 TOML_EXPORTED_MEMBER_FUNCTION
3404 path& operator+=(std::wstring_view);
3405
3406 #endif
3407
3408 TOML_ALWAYS_INLINE
3409 path& append(const path& p)
3410 {
3411 return *this += p;
3412 }
3413
3414 TOML_ALWAYS_INLINE
3415 path& append(path&& p)
3416 {
3417 return *this += std::move(p);
3418 }
3419
3420 TOML_ALWAYS_INLINE
3421 path& append(std::string_view str)
3422 {
3423 return *this += str;
3424 }
3425
3426 #if TOML_ENABLE_WINDOWS_COMPAT
3427
3428 TOML_ALWAYS_INLINE
3429 path& append(std::wstring_view str)
3430 {
3431 return *this += str;
3432 }
3433
3434 #endif
3435
3436 TOML_EXPORTED_MEMBER_FUNCTION
3437 path& prepend(const path&);
3438
3439 TOML_EXPORTED_MEMBER_FUNCTION
3440 path& prepend(path&&);
3441
3442 TOML_EXPORTED_MEMBER_FUNCTION
3443 path& prepend(std::string_view);
3444
3445 #if TOML_ENABLE_WINDOWS_COMPAT
3446
3447 TOML_EXPORTED_MEMBER_FUNCTION
3448 path& prepend(std::wstring_view);
3449
3450 #endif
3451
3452 TOML_NODISCARD
3453 friend path operator+(const path& lhs, const path& rhs)
3454 {
3455 path result = lhs;
3456 result += rhs;
3457 return result;
3458 }
3459
3460 TOML_NODISCARD
3461 friend path operator+(const path& lhs, std::string_view rhs)
3462 {
3463 path result = lhs;
3464 result += rhs;
3465 return result;
3466 }
3467
3468 TOML_NODISCARD
3469 friend path operator+(std::string_view lhs, const path& rhs)
3470 {
3471 path result = rhs;
3472 result.prepend(lhs);
3473 return result;
3474 }
3475
3476 #if TOML_ENABLE_WINDOWS_COMPAT
3477
3478 TOML_NODISCARD
3479 friend path operator+(const path& lhs, std::wstring_view rhs)
3480 {
3481 path result = lhs;
3482 result += rhs;
3483 return result;
3484 }
3485
3486 TOML_NODISCARD
3487 friend path operator+(std::wstring_view lhs, const path& rhs)
3488 {
3489 path result = rhs;
3490 result.prepend(lhs);
3491 return result;
3492 }
3493
3494 #endif
3495
3496 TOML_ALWAYS_INLINE
3497 friend std::ostream& operator<<(std::ostream& os, const path& rhs)
3498 {
3499 rhs.print_to(os);
3500 return os;
3501 }
3502
3503 TOML_NODISCARD
3504 TOML_EXPORTED_MEMBER_FUNCTION
3505 std::string str() const;
3506
3507 TOML_NODISCARD
3508 TOML_ALWAYS_INLINE
3509 explicit operator std::string() const
3510 {
3511 return str();
3512 }
3513
3514 #if TOML_ENABLE_WINDOWS_COMPAT
3515
3516 TOML_NODISCARD
3517 TOML_EXPORTED_MEMBER_FUNCTION
3518 std::wstring wide_str() const;
3519
3520 TOML_NODISCARD
3521 TOML_ALWAYS_INLINE
3522 explicit operator std::wstring() const
3523 {
3524 return wide_str();
3525 }
3526
3527 #endif
3528
3529 TOML_PURE_INLINE_GETTER
3530 friend bool operator==(const path& lhs, const path& rhs) noexcept
3531 {
3532 return equal(lhs, rhs);
3533 }
3534
3535 TOML_PURE_INLINE_GETTER
3536 friend bool operator!=(const path& lhs, const path& rhs) noexcept
3537 {
3538 return !equal(lhs, rhs);
3539 }
3540
3541 TOML_NODISCARD
3542 TOML_ALWAYS_INLINE
3543 friend bool operator==(const path& lhs, std::string_view rhs)
3544 {
3545 return lhs == path{ rhs };
3546 }
3547
3548 TOML_NODISCARD
3549 TOML_ALWAYS_INLINE
3550 friend bool operator==(std::string_view lhs, const path& rhs)
3551 {
3552 return rhs == lhs;
3553 }
3554
3555 TOML_NODISCARD
3556 TOML_ALWAYS_INLINE
3557 friend bool operator!=(const path& lhs, std::string_view rhs)
3558 {
3559 return lhs != path{ rhs };
3560 }
3561
3562 TOML_NODISCARD
3563 TOML_ALWAYS_INLINE
3564 friend bool operator!=(std::string_view lhs, const path& rhs)
3565 {
3566 return rhs != lhs;
3567 }
3568
3569 #if TOML_ENABLE_WINDOWS_COMPAT
3570
3571 TOML_NODISCARD
3572 TOML_ALWAYS_INLINE
3573 friend bool operator==(const path& lhs, std::wstring_view rhs)
3574 {
3575 return lhs == path{ rhs };
3576 }
3577
3578 TOML_NODISCARD
3579 TOML_ALWAYS_INLINE
3580 friend bool operator==(std::wstring_view lhs, const path& rhs)
3581 {
3582 return rhs == lhs;
3583 }
3584
3585 TOML_NODISCARD
3586 TOML_ALWAYS_INLINE
3587 friend bool operator!=(const path& lhs, std::wstring_view rhs)
3588 {
3589 return lhs != path{ rhs };
3590 }
3591
3592 TOML_NODISCARD
3593 TOML_ALWAYS_INLINE
3594 friend bool operator!=(std::wstring_view lhs, const path& rhs)
3595 {
3596 return rhs != lhs;
3597 }
3598
3599 #endif // TOML_ENABLE_WINDOWS_COMPAT
3600
3601 using iterator = std::vector<path_component>::iterator;
3602
3603 using const_iterator = std::vector<path_component>::const_iterator;
3604
3605 TOML_PURE_INLINE_GETTER
3606 iterator begin() noexcept
3607 {
3608 return components_.begin();
3609 }
3610
3611 TOML_PURE_INLINE_GETTER
3612 iterator end() noexcept
3613 {
3614 return components_.end();
3615 }
3616
3617 TOML_PURE_INLINE_GETTER
3618 const_iterator begin() const noexcept
3619 {
3620 return components_.begin();
3621 }
3622
3623 TOML_PURE_INLINE_GETTER
3624 const_iterator end() const noexcept
3625 {
3626 return components_.end();
3627 }
3628
3629 TOML_PURE_INLINE_GETTER
3630 const_iterator cbegin() const noexcept
3631 {
3632 return components_.begin();
3633 }
3634
3635 TOML_PURE_INLINE_GETTER
3636 const_iterator cend() const noexcept
3637 {
3638 return components_.end();
3639 }
3640
3641 TOML_EXPORTED_MEMBER_FUNCTION
3642 void clear() noexcept;
3643
3644 TOML_EXPORTED_MEMBER_FUNCTION
3645 path& truncate(size_t n);
3646
3647 TOML_NODISCARD
3648 TOML_EXPORTED_MEMBER_FUNCTION
3649 path truncated(size_t n) const;
3650
3651 TOML_NODISCARD
3652 TOML_EXPORTED_MEMBER_FUNCTION
3653 path parent() const;
3654
3655 TOML_NODISCARD
3656 TOML_EXPORTED_MEMBER_FUNCTION
3657 path leaf(size_t n = 1) const;
3658
3659 TOML_NODISCARD
3660 TOML_EXPORTED_MEMBER_FUNCTION
3661 path subpath(const_iterator start, const_iterator end) const;
3662
3663 TOML_NODISCARD
3664 TOML_EXPORTED_MEMBER_FUNCTION
3665 path subpath(size_t start, size_t length) const;
3666 };
3667
3668 inline namespace literals
3669 {
3670 TOML_NODISCARD
3671 TOML_ALWAYS_INLINE
3672 path operator""_tpath(const char* str, size_t len)
3673 {
3674 return path(std::string_view{ str, len });
3675 }
3676 }
3677
3678 TOML_NODISCARD
3679 TOML_EXPORTED_FREE_FUNCTION
3680 node_view<node> TOML_CALLCONV at_path(node & root, const toml::path& path) noexcept;
3681
3682 TOML_NODISCARD
3683 TOML_EXPORTED_FREE_FUNCTION
3684 node_view<const node> TOML_CALLCONV at_path(const node& root, const toml::path& path) noexcept;
3685 }
3686 TOML_NAMESPACE_END;
3687
3688 #ifdef _MSC_VER
3689 #pragma pop_macro("min")
3690 #pragma pop_macro("max")
3691 #ifndef __clang__
3692 #pragma inline_recursion(off)
3693 #endif
3694 #endif
3695 TOML_POP_WARNINGS;
3696
3697 //******** impl/std_utility.hpp **************************************************************************************
3698
3699 TOML_DISABLE_WARNINGS;
3700 #include <utility>
3701 TOML_ENABLE_WARNINGS;
3702
3703 //******** impl/node.hpp *********************************************************************************************
3704
3705 TOML_PUSH_WARNINGS;
3706 #ifdef _MSC_VER
3707 #ifndef __clang__
3708 #pragma inline_recursion(on)
3709 #endif
3710 #pragma push_macro("min")
3711 #pragma push_macro("max")
3712 #undef min
3713 #undef max
3714 #endif
3715
3716 // workaround for this: https://github.com/marzer/tomlplusplus/issues/220
3717 #if TOML_NVCC
3718 #define TOML_NVCC_WORKAROUND \
3719 { \
3720 return {}; \
3721 }
3722 #else
3723 #define TOML_NVCC_WORKAROUND = 0
3724 #endif
3725
3726 TOML_NAMESPACE_START
3727 {
3728 class TOML_ABSTRACT_INTERFACE TOML_EXPORTED_CLASS node
3729 {
3730 private:
3731
3732 friend class TOML_PARSER_TYPENAME;
3733 source_region source_{};
3734
3735 template <typename T>
3736 TOML_NODISCARD
3737 decltype(auto) get_value_exact() const noexcept(impl::value_retrieval_is_nothrow<T>);
3738
3739 template <typename T, typename N>
3740 using ref_type_ = std::conditional_t< //
3741 std::is_reference_v<T>, //
3742 impl::copy_ref<impl::copy_cv<impl::unwrap_node<T>, std::remove_reference_t<N>>, T>, //
3743 impl::copy_cvref<impl::unwrap_node<T>, N> //
3744 >;
3745
3746 template <typename T, typename N>
3747 using ref_type = std::conditional_t< //
3748 std::is_reference_v<N>, //
3749 ref_type_<T, N>, //
3750 ref_type_<T, std::add_lvalue_reference_t<N>> //
3751 >;
3752
3753 template <typename T, typename N>
3754 TOML_PURE_GETTER
3755 static ref_type<T, N&&> do_ref(N&& n) noexcept
3756 {
3757 using unwrapped_type = impl::unwrap_node<T>;
3758 static_assert(toml::is_value<unwrapped_type> || toml::is_container<unwrapped_type>,
3759 "The template type argument of node::ref() must be one of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
3760
3761 TOML_ASSERT_ASSUME(
3762 n.template is<unwrapped_type>()
3763 && "template type argument provided to toml::node::ref() didn't match the node's actual type");
3764
3765 using node_ref = std::remove_volatile_t<std::remove_reference_t<N>>&;
3766 using val_type = std::remove_volatile_t<unwrapped_type>;
3767 using out_ref = ref_type<T, N&&>;
3768 static_assert(std::is_reference_v<out_ref>);
3769
3770 if constexpr (toml::is_value<unwrapped_type>)
3771 return static_cast<out_ref>(const_cast<node_ref>(n).template ref_cast<val_type>().get());
3772 else
3773 return static_cast<out_ref>(const_cast<node_ref>(n).template ref_cast<val_type>());
3774 }
3775
3776 protected:
3777 TOML_EXPORTED_MEMBER_FUNCTION
3778 node() noexcept;
3779
3780 TOML_EXPORTED_MEMBER_FUNCTION
3781 node(const node&) noexcept;
3782
3783 TOML_EXPORTED_MEMBER_FUNCTION
3784 node(node&&) noexcept;
3785
3786 TOML_EXPORTED_MEMBER_FUNCTION
3787 node& operator=(const node&) noexcept;
3788
3789 TOML_EXPORTED_MEMBER_FUNCTION
3790 node& operator=(node&&) noexcept;
3791
3792 template <typename T, typename N>
3793 using ref_cast_type_ = std::conditional_t< //
3794 std::is_reference_v<T>, //
3795 impl::copy_ref<impl::copy_cv<impl::wrap_node<T>, std::remove_reference_t<N>>, T>, //
3796 impl::copy_cvref<impl::wrap_node<T>, N> //
3797 >;
3798
3799 template <typename T, typename N>
3800 using ref_cast_type = std::conditional_t< //
3801 std::is_reference_v<N>, //
3802 ref_cast_type_<T, N>, //
3803 ref_cast_type_<T, std::add_lvalue_reference_t<N>> //
3804 >;
3805
3806 template <typename T>
3807 TOML_PURE_INLINE_GETTER
3808 ref_cast_type<T, node&> ref_cast() & noexcept
3809 {
3810 using out_ref = ref_cast_type<T, node&>;
3811 using out_type = std::remove_reference_t<out_ref>;
3812 return static_cast<out_ref>(*reinterpret_cast<out_type*>(this));
3813 }
3814
3815 template <typename T>
3816 TOML_PURE_INLINE_GETTER
3817 ref_cast_type<T, node&&> ref_cast() && noexcept
3818 {
3819 using out_ref = ref_cast_type<T, node&&>;
3820 using out_type = std::remove_reference_t<out_ref>;
3821 return static_cast<out_ref>(*reinterpret_cast<out_type*>(this));
3822 }
3823
3824 template <typename T>
3825 TOML_PURE_INLINE_GETTER
3826 ref_cast_type<T, const node&> ref_cast() const& noexcept
3827 {
3828 using out_ref = ref_cast_type<T, const node&>;
3829 using out_type = std::remove_reference_t<out_ref>;
3830 return static_cast<out_ref>(*reinterpret_cast<out_type*>(this));
3831 }
3832
3833 template <typename T>
3834 TOML_PURE_INLINE_GETTER
3835 ref_cast_type<T, const node&&> ref_cast() const&& noexcept
3836 {
3837 using out_ref = ref_cast_type<T, const node&&>;
3838 using out_type = std::remove_reference_t<out_ref>;
3839 return static_cast<out_ref>(*reinterpret_cast<out_type*>(this));
3840 }
3841
3842 public:
3843 TOML_EXPORTED_MEMBER_FUNCTION
3844 virtual ~node() noexcept;
3845
3846 TOML_NODISCARD
3847 virtual bool is_homogeneous(node_type ntype, node*& first_nonmatch) noexcept = 0;
3848
3849 TOML_NODISCARD
3850 virtual bool is_homogeneous(node_type ntype, const node*& first_nonmatch) const noexcept = 0;
3851
3852 TOML_PURE_GETTER
3853 virtual bool is_homogeneous(node_type ntype) const noexcept = 0;
3854
3855 template <typename ElemType = void>
3856 TOML_PURE_GETTER
3857 bool is_homogeneous() const noexcept
3858 {
3859 using type = impl::remove_cvref<impl::unwrap_node<ElemType>>;
3860 static_assert(std::is_void_v<type> || toml::is_value<type> || toml::is_container<type>,
3861 "The template type argument of node::is_homogeneous() must be void or one "
3862 "of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
3863
3864 return is_homogeneous(impl::node_type_of<type>);
3865 }
3866
3867 TOML_PURE_GETTER
3868 virtual node_type type() const noexcept TOML_NVCC_WORKAROUND;
3869
3870 TOML_PURE_GETTER
3871 virtual bool is_table() const noexcept TOML_NVCC_WORKAROUND;
3872
3873 TOML_PURE_GETTER
3874 virtual bool is_array() const noexcept = 0;
3875
3876 TOML_PURE_GETTER
3877 virtual bool is_array_of_tables() const noexcept TOML_NVCC_WORKAROUND;
3878
3879 TOML_PURE_GETTER
3880 virtual bool is_value() const noexcept = 0;
3881
3882 TOML_PURE_GETTER
3883 virtual bool is_string() const noexcept = 0;
3884
3885 TOML_PURE_GETTER
3886 virtual bool is_integer() const noexcept = 0;
3887
3888 TOML_PURE_GETTER
3889 virtual bool is_floating_point() const noexcept = 0;
3890
3891 TOML_PURE_GETTER
3892 virtual bool is_number() const noexcept = 0;
3893
3894 TOML_PURE_GETTER
3895 virtual bool is_boolean() const noexcept = 0;
3896
3897 TOML_PURE_GETTER
3898 virtual bool is_date() const noexcept = 0;
3899
3900 TOML_PURE_GETTER
3901 virtual bool is_time() const noexcept = 0;
3902
3903 TOML_PURE_GETTER
3904 virtual bool is_date_time() const noexcept = 0;
3905
3906 template <typename T>
3907 TOML_PURE_INLINE_GETTER
3908 bool is() const noexcept
3909 {
3910 using type = impl::remove_cvref<impl::unwrap_node<T>>;
3911 static_assert(toml::is_value<type> || toml::is_container<type>,
3912 "The template type argument of node::is() must be one of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
3913
3914 if constexpr (std::is_same_v<type, table>)
3915 return is_table();
3916 else if constexpr (std::is_same_v<type, array>)
3917 return is_array();
3918 else if constexpr (std::is_same_v<type, std::string>)
3919 return is_string();
3920 else if constexpr (std::is_same_v<type, int64_t>)
3921 return is_integer();
3922 else if constexpr (std::is_same_v<type, double>)
3923 return is_floating_point();
3924 else if constexpr (std::is_same_v<type, bool>)
3925 return is_boolean();
3926 else if constexpr (std::is_same_v<type, date>)
3927 return is_date();
3928 else if constexpr (std::is_same_v<type, time>)
3929 return is_time();
3930 else if constexpr (std::is_same_v<type, date_time>)
3931 return is_date_time();
3932
3933 TOML_UNREACHABLE;
3934 }
3935
3936 TOML_PURE_GETTER
3937 virtual table* as_table() noexcept = 0;
3938
3939 TOML_PURE_GETTER
3940 virtual array* as_array() noexcept = 0;
3941
3942 TOML_PURE_GETTER
3943 virtual toml::value<std::string>* as_string() noexcept = 0;
3944
3945 TOML_PURE_GETTER
3946 virtual toml::value<int64_t>* as_integer() noexcept = 0;
3947
3948 TOML_PURE_GETTER
3949 virtual toml::value<double>* as_floating_point() noexcept = 0;
3950
3951 TOML_PURE_GETTER
3952 virtual toml::value<bool>* as_boolean() noexcept = 0;
3953
3954 TOML_PURE_GETTER
3955 virtual toml::value<date>* as_date() noexcept = 0;
3956
3957 TOML_PURE_GETTER
3958 virtual toml::value<time>* as_time() noexcept = 0;
3959
3960 TOML_PURE_GETTER
3961 virtual toml::value<date_time>* as_date_time() noexcept = 0;
3962
3963 TOML_PURE_GETTER
3964 virtual const table* as_table() const noexcept = 0;
3965
3966 TOML_PURE_GETTER
3967 virtual const array* as_array() const noexcept = 0;
3968
3969 TOML_PURE_GETTER
3970 virtual const toml::value<std::string>* as_string() const noexcept = 0;
3971
3972 TOML_PURE_GETTER
3973 virtual const toml::value<int64_t>* as_integer() const noexcept = 0;
3974
3975 TOML_PURE_GETTER
3976 virtual const toml::value<double>* as_floating_point() const noexcept = 0;
3977
3978 TOML_PURE_GETTER
3979 virtual const toml::value<bool>* as_boolean() const noexcept = 0;
3980
3981 TOML_PURE_GETTER
3982 virtual const toml::value<date>* as_date() const noexcept = 0;
3983
3984 TOML_PURE_GETTER
3985 virtual const toml::value<time>* as_time() const noexcept = 0;
3986
3987 TOML_PURE_GETTER
3988 virtual const toml::value<date_time>* as_date_time() const noexcept = 0;
3989
3990 template <typename T>
3991 TOML_PURE_INLINE_GETTER
3992 impl::wrap_node<T>* as() noexcept
3993 {
3994 using unwrapped_type = impl::unwrap_node<impl::remove_cvref<T>>;
3995 static_assert(toml::is_value<unwrapped_type> || toml::is_container<unwrapped_type>,
3996 "The template type argument of node::as() must be one of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
3997
3998 if constexpr (std::is_same_v<unwrapped_type, table>)
3999 return as_table();
4000 else if constexpr (std::is_same_v<unwrapped_type, array>)
4001 return as_array();
4002 else if constexpr (std::is_same_v<unwrapped_type, std::string>)
4003 return as_string();
4004 else if constexpr (std::is_same_v<unwrapped_type, int64_t>)
4005 return as_integer();
4006 else if constexpr (std::is_same_v<unwrapped_type, double>)
4007 return as_floating_point();
4008 else if constexpr (std::is_same_v<unwrapped_type, bool>)
4009 return as_boolean();
4010 else if constexpr (std::is_same_v<unwrapped_type, date>)
4011 return as_date();
4012 else if constexpr (std::is_same_v<unwrapped_type, time>)
4013 return as_time();
4014 else if constexpr (std::is_same_v<unwrapped_type, date_time>)
4015 return as_date_time();
4016
4017 TOML_UNREACHABLE;
4018 }
4019
4020 template <typename T>
4021 TOML_PURE_INLINE_GETTER
4022 const impl::wrap_node<T>* as() const noexcept
4023 {
4024 using unwrapped_type = impl::unwrap_node<impl::remove_cvref<T>>;
4025 static_assert(toml::is_value<unwrapped_type> || toml::is_container<unwrapped_type>,
4026 "The template type argument of node::as() must be one of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
4027
4028 if constexpr (std::is_same_v<unwrapped_type, table>)
4029 return as_table();
4030 else if constexpr (std::is_same_v<unwrapped_type, array>)
4031 return as_array();
4032 else if constexpr (std::is_same_v<unwrapped_type, std::string>)
4033 return as_string();
4034 else if constexpr (std::is_same_v<unwrapped_type, int64_t>)
4035 return as_integer();
4036 else if constexpr (std::is_same_v<unwrapped_type, double>)
4037 return as_floating_point();
4038 else if constexpr (std::is_same_v<unwrapped_type, bool>)
4039 return as_boolean();
4040 else if constexpr (std::is_same_v<unwrapped_type, date>)
4041 return as_date();
4042 else if constexpr (std::is_same_v<unwrapped_type, time>)
4043 return as_time();
4044 else if constexpr (std::is_same_v<unwrapped_type, date_time>)
4045 return as_date_time();
4046
4047 TOML_UNREACHABLE;
4048 }
4049
4050 template <typename T>
4051 TOML_NODISCARD
4052 optional<T> value_exact() const noexcept(impl::value_retrieval_is_nothrow<T>);
4053
4054 template <typename T>
4055 TOML_NODISCARD
4056 optional<T> value() const noexcept(impl::value_retrieval_is_nothrow<T>);
4057
4058 template <typename T>
4059 TOML_NODISCARD
4060 auto value_or(T&& default_value) const noexcept(impl::value_retrieval_is_nothrow<T>);
4061
4062 template <typename T>
4063 TOML_PURE_GETTER
4064 decltype(auto) ref() & noexcept
4065 {
4066 return do_ref<T>(*this);
4067 }
4068
4069 template <typename T>
4070 TOML_PURE_GETTER
4071 decltype(auto) ref() && noexcept
4072 {
4073 return do_ref<T>(std::move(*this));
4074 }
4075
4076 template <typename T>
4077 TOML_PURE_GETTER
4078 decltype(auto) ref() const& noexcept
4079 {
4080 return do_ref<T>(*this);
4081 }
4082
4083 template <typename T>
4084 TOML_PURE_GETTER
4085 decltype(auto) ref() const&& noexcept
4086 {
4087 return do_ref<T>(std::move(*this));
4088 }
4089
4090 TOML_PURE_INLINE_GETTER
4091 const source_region& source() const noexcept
4092 {
4093 return source_;
4094 }
4095
4096 private:
4097
4098 template <typename Func, typename Node, typename T>
4099 static constexpr bool can_visit = std::is_invocable_v<Func, ref_cast_type<T, Node>>;
4100
4101 template <typename Func, typename Node, typename T>
4102 static constexpr bool can_visit_nothrow = std::is_nothrow_invocable_v<Func, ref_cast_type<T, Node>>;
4103
4104 template <typename Func, typename Node>
4105 static constexpr bool can_visit_any = can_visit<Func, Node, table> //
4106 || can_visit<Func, Node, array> //
4107 || can_visit<Func, Node, std::string> //
4108 || can_visit<Func, Node, int64_t> //
4109 || can_visit<Func, Node, double> //
4110 || can_visit<Func, Node, bool> //
4111 || can_visit<Func, Node, date> //
4112 || can_visit<Func, Node, time> //
4113 || can_visit<Func, Node, date_time>;
4114
4115 // clang-format off
4116
4117 template <typename Func, typename Node>
4118 static constexpr bool can_visit_all = can_visit<Func, Node, table> //
4119 && can_visit<Func, Node, array> //
4120 && can_visit<Func, Node, std::string> //
4121 && can_visit<Func, Node, int64_t> //
4122 && can_visit<Func, Node, double> //
4123 && can_visit<Func, Node, bool> //
4124 && can_visit<Func, Node, date> //
4125 && can_visit<Func, Node, time> //
4126 && can_visit<Func, Node, date_time>;
4127
4128 template <typename Func, typename Node, typename T>
4129 static constexpr bool visit_is_nothrow_one = !can_visit<Func, Node, T> || can_visit_nothrow<Func, Node, T>;
4130
4131 template <typename Func, typename Node>
4132 static constexpr bool visit_is_nothrow = visit_is_nothrow_one<Func, Node, table> //
4133 && visit_is_nothrow_one<Func, Node, array> //
4134 && visit_is_nothrow_one<Func, Node, std::string> //
4135 && visit_is_nothrow_one<Func, Node, int64_t> //
4136 && visit_is_nothrow_one<Func, Node, double> //
4137 && visit_is_nothrow_one<Func, Node, bool> //
4138 && visit_is_nothrow_one<Func, Node, date> //
4139 && visit_is_nothrow_one<Func, Node, time> //
4140 && visit_is_nothrow_one<Func, Node, date_time>;
4141
4142 // clang-format on
4143
4144 template <typename Func, typename Node, typename T, bool = can_visit<Func, Node, T>>
4145 struct visit_return_type_
4146 {
4147 using type = decltype(std::declval<Func>()(std::declval<ref_cast_type<T, Node>>()));
4148 };
4149 template <typename Func, typename Node, typename T>
4150 struct visit_return_type_<Func, Node, T, false>
4151 {
4152 using type = void;
4153 };
4154
4155 template <typename Func, typename Node, typename T>
4156 using visit_return_type = typename visit_return_type_<Func, Node, T>::type;
4157
4158 template <typename A, typename B>
4159 using nonvoid = std::conditional_t<std::is_void_v<A>, B, A>;
4160
4161 template <typename Func, typename Node>
4162 static decltype(auto) do_visit(Func&& visitor, Node&& n) noexcept(visit_is_nothrow<Func&&, Node&&>)
4163 {
4164 static_assert(can_visit_any<Func&&, Node&&>,
4165 "TOML node visitors must be invocable for at least one of the toml::node "
4166 "specializations:" TOML_SA_NODE_TYPE_LIST);
4167
4168 switch (n.type())
4169 {
4170 case node_type::table:
4171 if constexpr (can_visit<Func&&, Node&&, table>)
4172 return static_cast<Func&&>(visitor)(static_cast<Node&&>(n).template ref_cast<table>());
4173 break;
4174
4175 case node_type::array:
4176 if constexpr (can_visit<Func&&, Node&&, array>)
4177 return static_cast<Func&&>(visitor)(static_cast<Node&&>(n).template ref_cast<array>());
4178 break;
4179
4180 case node_type::string:
4181 if constexpr (can_visit<Func&&, Node&&, std::string>)
4182 return static_cast<Func&&>(visitor)(static_cast<Node&&>(n).template ref_cast<std::string>());
4183 break;
4184
4185 case node_type::integer:
4186 if constexpr (can_visit<Func&&, Node&&, int64_t>)
4187 return static_cast<Func&&>(visitor)(static_cast<Node&&>(n).template ref_cast<int64_t>());
4188 break;
4189
4190 case node_type::floating_point:
4191 if constexpr (can_visit<Func&&, Node&&, double>)
4192 return static_cast<Func&&>(visitor)(static_cast<Node&&>(n).template ref_cast<double>());
4193 break;
4194
4195 case node_type::boolean:
4196 if constexpr (can_visit<Func&&, Node&&, bool>)
4197 return static_cast<Func&&>(visitor)(static_cast<Node&&>(n).template ref_cast<bool>());
4198 break;
4199
4200 case node_type::date:
4201 if constexpr (can_visit<Func&&, Node&&, date>)
4202 return static_cast<Func&&>(visitor)(static_cast<Node&&>(n).template ref_cast<date>());
4203 break;
4204
4205 case node_type::time:
4206 if constexpr (can_visit<Func&&, Node&&, time>)
4207 return static_cast<Func&&>(visitor)(static_cast<Node&&>(n).template ref_cast<time>());
4208 break;
4209
4210 case node_type::date_time:
4211 if constexpr (can_visit<Func&&, Node&&, date_time>)
4212 return static_cast<Func&&>(visitor)(static_cast<Node&&>(n).template ref_cast<date_time>());
4213 break;
4214
4215 case node_type::none: TOML_UNREACHABLE;
4216 default: TOML_UNREACHABLE;
4217 }
4218
4219 if constexpr (!can_visit_all<Func&&, Node&&>)
4220 {
4221 // clang-format off
4222
4223 using return_type =
4224 nonvoid<visit_return_type<Func&&, Node&&, table>,
4225 nonvoid<visit_return_type<Func&&, Node&&, array>,
4226 nonvoid<visit_return_type<Func&&, Node&&, std::string>,
4227 nonvoid<visit_return_type<Func&&, Node&&, int64_t>,
4228 nonvoid<visit_return_type<Func&&, Node&&, double>,
4229 nonvoid<visit_return_type<Func&&, Node&&, bool>,
4230 nonvoid<visit_return_type<Func&&, Node&&, date>,
4231 nonvoid<visit_return_type<Func&&, Node&&, time>,
4232 visit_return_type<Func&&, Node&&, date_time>
4233 >>>>>>>>;
4234
4235 // clang-format on
4236
4237 if constexpr (!std::is_void_v<return_type>)
4238 {
4239 static_assert(std::is_default_constructible_v<return_type>,
4240 "Non-exhaustive visitors must return a default-constructible type, or void");
4241 return return_type{};
4242 }
4243 }
4244 }
4245
4246 public:
4247
4248 template <typename Func>
4249 decltype(auto) visit(Func&& visitor) & noexcept(visit_is_nothrow<Func&&, node&>)
4250 {
4251 return do_visit(static_cast<Func&&>(visitor), *this);
4252 }
4253
4254 template <typename Func>
4255 decltype(auto) visit(Func&& visitor) && noexcept(visit_is_nothrow<Func&&, node&&>)
4256 {
4257 return do_visit(static_cast<Func&&>(visitor), static_cast<node&&>(*this));
4258 }
4259
4260 template <typename Func>
4261 decltype(auto) visit(Func&& visitor) const& noexcept(visit_is_nothrow<Func&&, const node&>)
4262 {
4263 return do_visit(static_cast<Func&&>(visitor), *this);
4264 }
4265
4266 template <typename Func>
4267 decltype(auto) visit(Func&& visitor) const&& noexcept(visit_is_nothrow<Func&&, const node&&>)
4268 {
4269 return do_visit(static_cast<Func&&>(visitor), static_cast<const node&&>(*this));
4270 }
4271
4272 TOML_NODISCARD
4273 explicit operator node_view<node>() noexcept;
4274
4275 TOML_NODISCARD
4276 explicit operator node_view<const node>() const noexcept;
4277
4278 TOML_NODISCARD
4279 TOML_EXPORTED_MEMBER_FUNCTION
4280 node_view<node> at_path(std::string_view path) noexcept;
4281
4282 TOML_NODISCARD
4283 TOML_EXPORTED_MEMBER_FUNCTION
4284 node_view<const node> at_path(std::string_view path) const noexcept;
4285
4286 TOML_NODISCARD
4287 TOML_EXPORTED_MEMBER_FUNCTION
4288 node_view<node> at_path(const toml::path& path) noexcept;
4289
4290 TOML_NODISCARD
4291 TOML_EXPORTED_MEMBER_FUNCTION
4292 node_view<const node> at_path(const toml::path& path) const noexcept;
4293
4294 #if TOML_ENABLE_WINDOWS_COMPAT
4295
4296 TOML_NODISCARD
4297 TOML_EXPORTED_MEMBER_FUNCTION
4298 node_view<node> at_path(std::wstring_view path);
4299
4300 TOML_NODISCARD
4301 TOML_EXPORTED_MEMBER_FUNCTION
4302 node_view<const node> at_path(std::wstring_view path) const;
4303
4304 #endif // TOML_ENABLE_WINDOWS_COMPAT
4305
4306 TOML_NODISCARD
4307 TOML_EXPORTED_MEMBER_FUNCTION
4308 node_view<node> operator[](const toml::path& path) noexcept;
4309
4310 TOML_NODISCARD
4311 TOML_EXPORTED_MEMBER_FUNCTION
4312 node_view<const node> operator[](const toml::path& path) const noexcept;
4313 };
4314 }
4315 TOML_NAMESPACE_END;
4316
4317 TOML_IMPL_NAMESPACE_START
4318 {
4319 TOML_PURE_GETTER
4320 TOML_EXPORTED_FREE_FUNCTION
4321 bool TOML_CALLCONV node_deep_equality(const node*, const node*) noexcept;
4322 }
4323 TOML_IMPL_NAMESPACE_END;
4324
4325 #undef TOML_NVCC_WORKAROUND
4326
4327 #ifdef _MSC_VER
4328 #pragma pop_macro("min")
4329 #pragma pop_macro("max")
4330 #ifndef __clang__
4331 #pragma inline_recursion(off)
4332 #endif
4333 #endif
4334 TOML_POP_WARNINGS;
4335
4336 //******** impl/std_initializer_list.hpp *****************************************************************************
4337
4338 TOML_DISABLE_WARNINGS;
4339 #include <initializer_list>
4340 TOML_ENABLE_WARNINGS;
4341
4342 //******** impl/node_view.hpp ****************************************************************************************
4343
4344 TOML_PUSH_WARNINGS;
4345 #ifdef _MSC_VER
4346 #ifndef __clang__
4347 #pragma inline_recursion(on)
4348 #endif
4349 #pragma push_macro("min")
4350 #pragma push_macro("max")
4351 #undef min
4352 #undef max
4353 #endif
4354
4355 TOML_DISABLE_ARITHMETIC_WARNINGS;
4356
4357 TOML_NAMESPACE_START
4358 {
4359 template <typename ViewedType>
4360 class TOML_TRIVIAL_ABI node_view
4361 {
4362 static_assert(impl::is_one_of<ViewedType, toml::node, const toml::node>,
4363 "A toml::node_view<> must wrap toml::node or const toml::node.");
4364
4365 public:
4366
4367 using viewed_type = ViewedType;
4368
4369 private:
4370 template <typename T>
4371 friend class node_view;
4372
4373 mutable viewed_type* node_ = nullptr;
4374
4375 public:
4376
4377 TOML_NODISCARD_CTOR
4378 node_view() noexcept = default;
4379
4380 TOML_NODISCARD_CTOR
4381 explicit node_view(viewed_type* node) noexcept //
4382 : node_{ node }
4383 {}
4384
4385 TOML_NODISCARD_CTOR
4386 explicit node_view(viewed_type& node) noexcept //
4387 : node_{ &node }
4388 {}
4389
4390 TOML_NODISCARD_CTOR
4391 node_view(const node_view&) noexcept = default;
4392
4393 TOML_NODISCARD_CTOR
4394 node_view(node_view&&) noexcept = default;
4395
4396 node_view& operator=(const node_view&) & noexcept = default;
4397
4398 node_view& operator=(node_view&&) & noexcept = default;
4399
4400 TOML_PURE_INLINE_GETTER
4401 explicit operator bool() const noexcept
4402 {
4403 return node_ != nullptr;
4404 }
4405
4406 TOML_PURE_INLINE_GETTER
4407 viewed_type* node() const noexcept
4408 {
4409 return node_;
4410 }
4411
4412 TOML_PURE_GETTER
4413 node_type type() const noexcept
4414 {
4415 return node_ ? node_->type() : node_type::none;
4416 }
4417
4418 TOML_PURE_GETTER
4419 bool is_table() const noexcept
4420 {
4421 return node_ && node_->is_table();
4422 }
4423
4424 TOML_PURE_GETTER
4425 bool is_array() const noexcept
4426 {
4427 return node_ && node_->is_array();
4428 }
4429
4430 TOML_PURE_GETTER
4431 bool is_value() const noexcept
4432 {
4433 return node_ && node_->is_value();
4434 }
4435
4436 TOML_PURE_GETTER
4437 bool is_string() const noexcept
4438 {
4439 return node_ && node_->is_string();
4440 }
4441
4442 TOML_PURE_GETTER
4443 bool is_integer() const noexcept
4444 {
4445 return node_ && node_->is_integer();
4446 }
4447
4448 TOML_PURE_GETTER
4449 bool is_floating_point() const noexcept
4450 {
4451 return node_ && node_->is_floating_point();
4452 }
4453
4454 TOML_PURE_GETTER
4455 bool is_number() const noexcept
4456 {
4457 return node_ && node_->is_number();
4458 }
4459
4460 TOML_PURE_GETTER
4461 bool is_boolean() const noexcept
4462 {
4463 return node_ && node_->is_boolean();
4464 }
4465
4466 TOML_PURE_GETTER
4467 bool is_date() const noexcept
4468 {
4469 return node_ && node_->is_date();
4470 }
4471
4472 TOML_PURE_GETTER
4473 bool is_time() const noexcept
4474 {
4475 return node_ && node_->is_time();
4476 }
4477
4478 TOML_PURE_GETTER
4479 bool is_date_time() const noexcept
4480 {
4481 return node_ && node_->is_date_time();
4482 }
4483
4484 TOML_PURE_GETTER
4485 bool is_array_of_tables() const noexcept
4486 {
4487 return node_ && node_->is_array_of_tables();
4488 }
4489
4490 template <typename T>
4491 TOML_PURE_GETTER
4492 bool is() const noexcept
4493 {
4494 return node_ ? node_->template is<impl::unwrap_node<impl::remove_cvref<T>>>() : false;
4495 }
4496
4497 TOML_NODISCARD
4498 bool is_homogeneous(node_type ntype, viewed_type*& first_nonmatch) const noexcept
4499 {
4500 if (!node_)
4501 {
4502 first_nonmatch = {};
4503 return false;
4504 }
4505 return node_->is_homogeneous(ntype, first_nonmatch);
4506 }
4507
4508 TOML_PURE_GETTER
4509 bool is_homogeneous(node_type ntype) const noexcept
4510 {
4511 return node_ ? node_->is_homogeneous(ntype) : false;
4512 }
4513
4514 template <typename ElemType = void>
4515 TOML_PURE_GETTER
4516 bool is_homogeneous() const noexcept
4517 {
4518 return node_ ? node_->template is_homogeneous<impl::unwrap_node<impl::remove_cvref<ElemType>>>() : false;
4519 }
4520
4521 template <typename T>
4522 TOML_PURE_GETTER
4523 auto* as() const noexcept
4524 {
4525 return node_ ? node_->template as<T>() : nullptr;
4526 }
4527
4528 TOML_PURE_GETTER
4529 auto* as_table() const noexcept
4530 {
4531 return as<table>();
4532 }
4533
4534 TOML_PURE_GETTER
4535 auto* as_array() const noexcept
4536 {
4537 return as<array>();
4538 }
4539
4540 TOML_PURE_GETTER
4541 auto* as_string() const noexcept
4542 {
4543 return as<std::string>();
4544 }
4545
4546 TOML_PURE_GETTER
4547 auto* as_integer() const noexcept
4548 {
4549 return as<int64_t>();
4550 }
4551
4552 TOML_PURE_GETTER
4553 auto* as_floating_point() const noexcept
4554 {
4555 return as<double>();
4556 }
4557
4558 TOML_PURE_GETTER
4559 auto* as_boolean() const noexcept
4560 {
4561 return as<bool>();
4562 }
4563
4564 TOML_PURE_GETTER
4565 auto* as_date() const noexcept
4566 {
4567 return as<date>();
4568 }
4569
4570 TOML_PURE_GETTER
4571 auto* as_time() const noexcept
4572 {
4573 return as<time>();
4574 }
4575
4576 TOML_PURE_GETTER
4577 auto* as_date_time() const noexcept
4578 {
4579 return as<date_time>();
4580 }
4581
4582 template <typename T>
4583 TOML_NODISCARD
4584 optional<T> value_exact() const noexcept(impl::value_retrieval_is_nothrow<T>)
4585 {
4586 if (node_)
4587 return node_->template value_exact<T>();
4588 return {};
4589 }
4590
4591 template <typename T>
4592 TOML_NODISCARD
4593 optional<T> value() const noexcept(impl::value_retrieval_is_nothrow<T>)
4594 {
4595 if (node_)
4596 return node_->template value<T>();
4597 return {};
4598 }
4599
4600 template <typename T>
4601 TOML_NODISCARD
4602 auto value_or(T&& default_value) const noexcept(impl::value_retrieval_is_nothrow<T>)
4603 {
4604 using namespace ::toml::impl;
4605
4606 static_assert(!is_wide_string<T> || TOML_ENABLE_WINDOWS_COMPAT,
4607 "Retrieving values as wide-character strings is only "
4608 "supported on Windows with TOML_ENABLE_WINDOWS_COMPAT enabled.");
4609
4610 if constexpr (is_wide_string<T>)
4611 {
4612 #if TOML_ENABLE_WINDOWS_COMPAT
4613
4614 if (node_)
4615 return node_->value_or(static_cast<T&&>(default_value));
4616 return std::wstring{ static_cast<T&&>(default_value) };
4617
4618 #else
4619
4620 static_assert(impl::always_false<T>, "Evaluated unreachable branch!");
4621
4622 #endif
4623 }
4624 else
4625 {
4626 using value_type =
4627 std::conditional_t<std::is_pointer_v<std::decay_t<T>>,
4628 std::add_pointer_t<std::add_const_t<std::remove_pointer_t<std::decay_t<T>>>>,
4629 std::decay_t<T>>;
4630
4631 if (node_)
4632 return node_->value_or(static_cast<T&&>(default_value));
4633 if constexpr (std::is_pointer_v<value_type>)
4634 return value_type{ default_value };
4635 else
4636 return static_cast<T&&>(default_value);
4637 }
4638 }
4639
4640 template <typename T>
4641 TOML_PURE_INLINE_GETTER
4642 decltype(auto) ref() const noexcept
4643 {
4644 TOML_ASSERT_ASSUME(node_ && "toml::node_view::ref() called on a node_view that did not reference a node");
4645 return node_->template ref<T>();
4646 }
4647
4648 private:
4649
4650 template <typename Func>
4651 static constexpr bool visit_is_nothrow = noexcept(std::declval<viewed_type*>()->visit(std::declval<Func>()));
4652
4653 public:
4654
4655 template <typename Func>
4656 decltype(auto) visit(Func&& visitor) const noexcept(visit_is_nothrow<Func&&>)
4657 {
4658 using return_type = decltype(node_->visit(static_cast<Func&&>(visitor)));
4659 if (node_)
4660 return node_->visit(static_cast<Func&&>(visitor));
4661 if constexpr (!std::is_void_v<return_type>)
4662 return return_type{};
4663 }
4664
4665 public:
4666
4667 template <typename T>
4668 TOML_PURE_GETTER
4669 friend bool operator==(const node_view& lhs, const node_view<T>& rhs) noexcept
4670 {
4671 return impl::node_deep_equality(lhs.node_, rhs.node_);
4672 }
4673
4674 template <typename T>
4675 TOML_PURE_GETTER
4676 friend bool operator!=(const node_view& lhs, const node_view<T>& rhs) noexcept
4677 {
4678 return !impl::node_deep_equality(lhs.node_, rhs.node_);
4679 }
4680
4681 TOML_NODISCARD
4682 friend bool operator==(const node_view& lhs, const table& rhs) noexcept
4683 {
4684 if (lhs.node_ == &rhs)
4685 return true;
4686 const auto tbl = lhs.as<table>();
4687 return tbl && *tbl == rhs;
4688 }
4689 TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&, const table&, );
4690
4691 TOML_NODISCARD
4692 friend bool operator==(const node_view& lhs, const array& rhs) noexcept
4693 {
4694 if (lhs.node_ == &rhs)
4695 return true;
4696 const auto arr = lhs.as<array>();
4697 return arr && *arr == rhs;
4698 }
4699 TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&, const array&, );
4700
4701 template <typename T>
4702 TOML_NODISCARD
4703 friend bool operator==(const node_view& lhs, const toml::value<T>& rhs) noexcept
4704 {
4705 if (lhs.node_ == &rhs)
4706 return true;
4707 const auto val = lhs.as<T>();
4708 return val && *val == rhs;
4709 }
4710 TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&, const toml::value<T>&, template <typename T>);
4711
4712 TOML_CONSTRAINED_TEMPLATE(impl::is_losslessly_convertible_to_native<T>, typename T)
4713 TOML_NODISCARD
4714 friend bool operator==(const node_view& lhs, const T& rhs) noexcept(!impl::is_wide_string<T>)
4715 {
4716 static_assert(!impl::is_wide_string<T> || TOML_ENABLE_WINDOWS_COMPAT,
4717 "Comparison with wide-character strings is only "
4718 "supported on Windows with TOML_ENABLE_WINDOWS_COMPAT enabled.");
4719
4720 if constexpr (impl::is_wide_string<T>)
4721 {
4722 #if TOML_ENABLE_WINDOWS_COMPAT
4723 return lhs == impl::narrow(rhs);
4724 #else
4725 static_assert(impl::always_false<T>, "Evaluated unreachable branch!");
4726 #endif
4727 }
4728 else
4729 {
4730 const auto val = lhs.as<impl::native_type_of<T>>();
4731 return val && *val == rhs;
4732 }
4733 }
4734 TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&,
4735 const T&,
4736 TOML_CONSTRAINED_TEMPLATE(impl::is_losslessly_convertible_to_native<T>,
4737 typename T));
4738
4739 template <typename T>
4740 TOML_NODISCARD
4741 friend bool operator==(const node_view& lhs,
4742 const std::initializer_list<T>& rhs) noexcept(!impl::is_wide_string<T>)
4743 {
4744 const auto arr = lhs.as<array>();
4745 return arr && *arr == rhs;
4746 }
4747 TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&, const std::initializer_list<T>&, template <typename T>);
4748
4749 template <typename T>
4750 TOML_NODISCARD
4751 friend bool operator==(const node_view& lhs, const std::vector<T>& rhs) noexcept(!impl::is_wide_string<T>)
4752 {
4753 const auto arr = lhs.as<array>();
4754 return arr && *arr == rhs;
4755 }
4756 TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&, const std::vector<T>&, template <typename T>);
4757
4758 TOML_NODISCARD
4759 node_view operator[](std::string_view key) const noexcept
4760 {
4761 if (auto tbl = this->as_table())
4762 return node_view{ tbl->get(key) };
4763 return {};
4764 }
4765
4766 TOML_NODISCARD
4767 node_view operator[](const toml::path& path) const noexcept
4768 {
4769 return node_ ? node_->at_path(path) : node_view{};
4770 }
4771
4772 TOML_NODISCARD
4773 node_view at_path(std::string_view path) const noexcept
4774 {
4775 return node_ ? node_->at_path(path) : node_view{};
4776 }
4777
4778 TOML_NODISCARD
4779 node_view at_path(const toml::path& path) const noexcept
4780 {
4781 return node_ ? node_->at_path(path) : node_view{};
4782 }
4783
4784 #if TOML_ENABLE_WINDOWS_COMPAT
4785
4786 TOML_NODISCARD
4787 node_view operator[](std::wstring_view key) const
4788 {
4789 if (auto tbl = this->as_table())
4790 return node_view{ tbl->get(key) };
4791 return {};
4792 }
4793
4794 TOML_NODISCARD
4795 node_view at_path(std::wstring_view path) const
4796 {
4797 return node_ ? node_->at_path(path) : node_view{};
4798 }
4799
4800 #endif // TOML_ENABLE_WINDOWS_COMPAT
4801
4802 TOML_NODISCARD
4803 node_view operator[](size_t index) const noexcept
4804 {
4805 if (auto arr = this->as_array())
4806 return node_view{ arr->get(index) };
4807 return {};
4808 }
4809
4810 #if TOML_ENABLE_FORMATTERS
4811
4812 friend std::ostream& operator<<(std::ostream& os, const node_view& nv)
4813 {
4814 if (nv.node_)
4815 nv.node_->visit([&os](const auto& n) { os << n; });
4816 return os;
4817 }
4818
4819 #endif
4820 };
4821
4822 template <typename T>
4823 node_view(const T&) -> node_view<const node>;
4824
4825 template <typename T>
4826 node_view(const T*) -> node_view<const node>;
4827
4828 template <typename T>
4829 node_view(T&) -> node_view<node>;
4830
4831 template <typename T>
4832 node_view(T*) -> node_view<node>;
4833 }
4834 TOML_NAMESPACE_END;
4835
4836 TOML_NAMESPACE_START
4837 {
4838 inline node::operator node_view<node>() noexcept
4839 {
4840 return node_view<node>{ this };
4841 }
4842
4843 inline node::operator node_view<const node>() const noexcept
4844 {
4845 return node_view<const node>{ this };
4846 }
4847 }
4848 TOML_NAMESPACE_END;
4849
4850 #ifdef _MSC_VER
4851 #pragma pop_macro("min")
4852 #pragma pop_macro("max")
4853 #ifndef __clang__
4854 #pragma inline_recursion(off)
4855 #endif
4856 #endif
4857 TOML_POP_WARNINGS;
4858
4859 //******** impl/value.hpp ********************************************************************************************
4860
4861 TOML_PUSH_WARNINGS;
4862 #ifdef _MSC_VER
4863 #ifndef __clang__
4864 #pragma inline_recursion(on)
4865 #endif
4866 #pragma push_macro("min")
4867 #pragma push_macro("max")
4868 #undef min
4869 #undef max
4870 #endif
4871
4872 TOML_DISABLE_ARITHMETIC_WARNINGS;
4873
4874 // clang-format off
4875
4876 #if TOML_ENABLE_WINDOWS_COMPAT
4877 #define TOML_SA_VALUE_MESSAGE_WSTRING TOML_SA_LIST_SEP "std::wstring"
4878 #else
4879 #define TOML_SA_VALUE_MESSAGE_WSTRING
4880 #endif
4881 #if TOML_HAS_CHAR8
4882 #define TOML_SA_VALUE_MESSAGE_U8STRING_VIEW TOML_SA_LIST_SEP "std::u8string_view"
4883 #define TOML_SA_VALUE_MESSAGE_CONST_CHAR8 TOML_SA_LIST_SEP "const char8_t*"
4884 #else
4885 #define TOML_SA_VALUE_MESSAGE_U8STRING_VIEW
4886 #define TOML_SA_VALUE_MESSAGE_CONST_CHAR8
4887 #endif
4888
4889 #define TOML_SA_VALUE_EXACT_FUNC_MESSAGE(type_arg) \
4890 "The " type_arg " must be one of:" \
4891 TOML_SA_LIST_NEW "A native TOML value type" \
4892 TOML_SA_NATIVE_VALUE_TYPE_LIST \
4893 \
4894 TOML_SA_LIST_NXT "A non-view type capable of losslessly representing a native TOML value type" \
4895 TOML_SA_LIST_BEG "std::string" \
4896 TOML_SA_VALUE_MESSAGE_WSTRING \
4897 TOML_SA_LIST_SEP "any signed integer type >= 64 bits" \
4898 TOML_SA_LIST_SEP "any floating-point type >= 64 bits" \
4899 TOML_SA_LIST_END \
4900 \
4901 TOML_SA_LIST_NXT "An immutable view type not requiring additional temporary storage" \
4902 TOML_SA_LIST_BEG "std::string_view" \
4903 TOML_SA_VALUE_MESSAGE_U8STRING_VIEW \
4904 TOML_SA_LIST_SEP "const char*" \
4905 TOML_SA_VALUE_MESSAGE_CONST_CHAR8 \
4906 TOML_SA_LIST_END
4907
4908 #define TOML_SA_VALUE_FUNC_MESSAGE(type_arg) \
4909 "The " type_arg " must be one of:" \
4910 TOML_SA_LIST_NEW "A native TOML value type" \
4911 TOML_SA_NATIVE_VALUE_TYPE_LIST \
4912 \
4913 TOML_SA_LIST_NXT "A non-view type capable of losslessly representing a native TOML value type" \
4914 TOML_SA_LIST_BEG "std::string" \
4915 TOML_SA_VALUE_MESSAGE_WSTRING \
4916 TOML_SA_LIST_SEP "any signed integer type >= 64 bits" \
4917 TOML_SA_LIST_SEP "any floating-point type >= 64 bits" \
4918 TOML_SA_LIST_END \
4919 \
4920 TOML_SA_LIST_NXT "A non-view type capable of (reasonably) representing a native TOML value type" \
4921 TOML_SA_LIST_BEG "any other integral type" \
4922 TOML_SA_LIST_SEP "any floating-point type" \
4923 TOML_SA_LIST_END \
4924 \
4925 TOML_SA_LIST_NXT "An immutable view type not requiring additional temporary storage" \
4926 TOML_SA_LIST_BEG "std::string_view" \
4927 TOML_SA_VALUE_MESSAGE_U8STRING_VIEW \
4928 TOML_SA_LIST_SEP "const char*" \
4929 TOML_SA_VALUE_MESSAGE_CONST_CHAR8 \
4930 TOML_SA_LIST_END
4931
4932 // clang-format on
4933 TOML_IMPL_NAMESPACE_START
4934 {
4935 template <typename T, typename...>
4936 struct native_value_maker
4937 {
4938 template <typename... Args>
4939 TOML_NODISCARD
4940 static T make(Args&&... args) noexcept(std::is_nothrow_constructible_v<T, Args&&...>)
4941 {
4942 if constexpr (std::is_aggregate_v<T>)
4943 return T{ static_cast<Args&&>(args)... };
4944 else
4945 return T(static_cast<Args&&>(args)...);
4946 }
4947 };
4948
4949 template <typename T>
4950 struct native_value_maker<T, T>
4951 {
4952 template <typename U>
4953 TOML_NODISCARD
4954 TOML_ALWAYS_INLINE
4955 static U&& make(U&& val) noexcept
4956 {
4957 return static_cast<U&&>(val);
4958 }
4959 };
4960
4961 #if TOML_HAS_CHAR8 || TOML_ENABLE_WINDOWS_COMPAT
4962
4963 struct string_maker
4964 {
4965 template <typename T>
4966 TOML_NODISCARD
4967 static std::string make(T&& arg)
4968 {
4969 using arg_type = std::decay_t<T>;
4970 #if TOML_HAS_CHAR8
4971 if constexpr (is_one_of<arg_type, char8_t*, const char8_t*>)
4972 {
4973 return std::string(reinterpret_cast<const char*>(static_cast<const char8_t*>(arg)));
4974 }
4975 if constexpr (is_one_of<arg_type, std::u8string, std::u8string_view>)
4976 {
4977 return std::string(reinterpret_cast<const char*>(static_cast<const char8_t*>(arg.data())),
4978 arg.length());
4979 }
4980 #endif
4981
4982 #if TOML_ENABLE_WINDOWS_COMPAT
4983 if constexpr (is_wide_string<arg_type>)
4984 {
4985 return narrow(static_cast<T&&>(arg));
4986 }
4987 #endif
4988 }
4989 };
4990
4991 #if TOML_HAS_CHAR8
4992 template <>
4993 struct native_value_maker<std::string, char8_t*> : string_maker
4994 {};
4995 template <>
4996 struct native_value_maker<std::string, const char8_t*> : string_maker
4997 {};
4998 template <>
4999 struct native_value_maker<std::string, std::u8string> : string_maker
5000 {};
5001 template <>
5002 struct native_value_maker<std::string, std::u8string_view> : string_maker
5003 {};
5004 #endif // TOML_HAS_CHAR8
5005
5006 #if TOML_ENABLE_WINDOWS_COMPAT
5007 template <>
5008 struct native_value_maker<std::string, wchar_t*> : string_maker
5009 {};
5010 template <>
5011 struct native_value_maker<std::string, const wchar_t*> : string_maker
5012 {};
5013 template <>
5014 struct native_value_maker<std::string, std::wstring> : string_maker
5015 {};
5016 template <>
5017 struct native_value_maker<std::string, std::wstring_view> : string_maker
5018 {};
5019 #endif // TOML_ENABLE_WINDOWS_COMPAT
5020
5021 #endif // TOML_HAS_CHAR8 || TOML_ENABLE_WINDOWS_COMPAT
5022
5023 template <typename T>
5024 TOML_CONST_GETTER
5025 inline optional<T> node_integer_cast(int64_t val) noexcept
5026 {
5027 static_assert(node_type_of<T> == node_type::integer);
5028 static_assert(!is_cvref<T>);
5029
5030 using traits = value_traits<T>;
5031 if constexpr (!traits::is_signed)
5032 {
5033 if constexpr ((sizeof(T) * CHAR_BIT) < 63) // 63 bits == int64_max
5034 {
5035 using common_t = decltype(int64_t{} + T{});
5036 if (val < int64_t{} || static_cast<common_t>(val) > static_cast<common_t>(traits::max))
5037 return {};
5038 }
5039 else
5040 {
5041 if (val < int64_t{})
5042 return {};
5043 }
5044 }
5045 else
5046 {
5047 if (val < traits::min || val > traits::max)
5048 return {};
5049 }
5050 return { static_cast<T>(val) };
5051 }
5052
5053 template <typename...>
5054 struct value_variadic_ctor_allowed : std::true_type
5055 {};
5056
5057 template <typename T, typename... Args>
5058 struct value_variadic_ctor_allowed<value<T>, value<T>, Args...> : std::false_type
5059 {};
5060 }
5061 TOML_IMPL_NAMESPACE_END;
5062
5063 TOML_NAMESPACE_START
5064 {
5065 template <typename ValueType>
5066 class value : public node
5067 {
5068 static_assert(impl::is_native<ValueType> && !impl::is_cvref<ValueType>,
5069 "A toml::value<> must model one of the native TOML value types:" TOML_SA_NATIVE_VALUE_TYPE_LIST);
5070
5071 private:
5072
5073 friend class TOML_PARSER_TYPENAME;
5074
5075 template <typename T, typename U>
5076 TOML_CONST_INLINE_GETTER
5077 static auto as_value([[maybe_unused]] U* ptr) noexcept
5078 {
5079 if constexpr (std::is_same_v<value_type, T>)
5080 return ptr;
5081 else
5082 return nullptr;
5083 }
5084
5085 ValueType val_;
5086 value_flags flags_ = value_flags::none;
5087
5088 public:
5089
5090 using value_type = ValueType;
5091
5092 using value_arg = POXY_IMPLEMENTATION_DETAIL(
5093 std::conditional_t<
5094 std::is_same_v<value_type, std::string>,
5095 std::string_view,
5096 std::conditional_t<impl::is_one_of<value_type, double, int64_t, bool>, value_type, const value_type&>>);
5097
5098 TOML_HIDDEN_CONSTRAINT(
5099 (impl::value_variadic_ctor_allowed<value<ValueType>, impl::remove_cvref<Args>...>::value),
5100 typename... Args)
5101 TOML_NODISCARD_CTOR
5102 explicit value(Args&&... args)
5103 #if !TOML_DISABLE_NOEXCEPT_NOEXCEPT
5104 noexcept(noexcept(value_type(
5105 impl::native_value_maker<value_type, std::decay_t<Args>...>::make(static_cast<Args&&>(args)...))))
5106 #endif
5107 : val_(impl::native_value_maker<value_type, std::decay_t<Args>...>::make(static_cast<Args&&>(args)...))
5108 {
5109 #if TOML_LIFETIME_HOOKS
5110 TOML_VALUE_CREATED;
5111 #endif
5112 }
5113
5114 TOML_NODISCARD_CTOR
5115 value(const value& other) noexcept //
5116 : node(other),
5117 val_{ other.val_ },
5118 flags_{ other.flags_ }
5119 {
5120 #if TOML_LIFETIME_HOOKS
5121 TOML_VALUE_CREATED;
5122 #endif
5123 }
5124
5125 TOML_NODISCARD_CTOR
5126 value(const value& other, value_flags flags) noexcept //
5127 : node(other),
5128 val_{ other.val_ },
5129 flags_{ flags == preserve_source_value_flags ? other.flags_ : flags }
5130 {
5131 #if TOML_LIFETIME_HOOKS
5132 TOML_VALUE_CREATED;
5133 #endif
5134 }
5135
5136 TOML_NODISCARD_CTOR
5137 value(value&& other) noexcept //
5138 : node(std::move(other)),
5139 val_{ std::move(other.val_) },
5140 flags_{ std::exchange(other.flags_, value_flags{}) }
5141 {
5142 #if TOML_LIFETIME_HOOKS
5143 TOML_VALUE_CREATED;
5144 #endif
5145 }
5146
5147 TOML_NODISCARD_CTOR
5148 value(value&& other, value_flags flags) noexcept //
5149 : node(std::move(other)),
5150 val_{ std::move(other.val_) },
5151 flags_{ flags == preserve_source_value_flags ? other.flags_ : flags }
5152 {
5153 #if TOML_LIFETIME_HOOKS
5154 TOML_VALUE_CREATED;
5155 #endif
5156 other.flags_ = {};
5157 }
5158
5159 value& operator=(const value& rhs) noexcept
5160 {
5161 node::operator=(rhs);
5162 val_ = rhs.val_;
5163 flags_ = rhs.flags_;
5164 return *this;
5165 }
5166
5167 value& operator=(value&& rhs) noexcept
5168 {
5169 if (&rhs != this)
5170 {
5171 node::operator=(std::move(rhs));
5172 val_ = std::move(rhs.val_);
5173 flags_ = std::exchange(rhs.flags_, value_flags{});
5174 }
5175 return *this;
5176 }
5177
5178 #if TOML_LIFETIME_HOOKS
5179 ~value() noexcept
5180 {
5181 TOML_VALUE_DESTROYED;
5182 }
5183 #endif
5184
5185 TOML_CONST_INLINE_GETTER
5186 node_type type() const noexcept final
5187 {
5188 return impl::node_type_of<value_type>;
5189 }
5190
5191 TOML_PURE_GETTER
5192 bool is_homogeneous(node_type ntype) const noexcept final
5193 {
5194 return ntype == node_type::none || ntype == impl::node_type_of<value_type>;
5195 }
5196
5197 TOML_NODISCARD
5198 bool is_homogeneous(node_type ntype, node*& first_nonmatch) noexcept final
5199 {
5200 if (ntype != node_type::none && ntype != impl::node_type_of<value_type>)
5201 {
5202 first_nonmatch = this;
5203 return false;
5204 }
5205 return true;
5206 }
5207
5208 TOML_NODISCARD
5209 bool is_homogeneous(node_type ntype, const node*& first_nonmatch) const noexcept final
5210 {
5211 if (ntype != node_type::none && ntype != impl::node_type_of<value_type>)
5212 {
5213 first_nonmatch = this;
5214 return false;
5215 }
5216 return true;
5217 }
5218
5219 template <typename ElemType = void>
5220 TOML_PURE_GETTER
5221 bool is_homogeneous() const noexcept
5222 {
5223 using type = impl::remove_cvref<impl::unwrap_node<ElemType>>;
5224 static_assert(std::is_void_v<type> || toml::is_value<type> || toml::is_container<type>,
5225 "The template type argument of value::is_homogeneous() must be void or one "
5226 "of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
5227
5228 if constexpr (std::is_void_v<type>)
5229 return true;
5230 else
5231 return impl::node_type_of<type> == impl::node_type_of<value_type>;
5232 }
5233 TOML_CONST_INLINE_GETTER
5234 bool is_table() const noexcept final
5235 {
5236 return false;
5237 }
5238
5239 TOML_CONST_INLINE_GETTER
5240 bool is_array() const noexcept final
5241 {
5242 return false;
5243 }
5244
5245 TOML_CONST_INLINE_GETTER
5246 bool is_array_of_tables() const noexcept final
5247 {
5248 return false;
5249 }
5250
5251 TOML_CONST_INLINE_GETTER
5252 bool is_value() const noexcept final
5253 {
5254 return true;
5255 }
5256
5257 TOML_CONST_INLINE_GETTER
5258 bool is_string() const noexcept final
5259 {
5260 return std::is_same_v<value_type, std::string>;
5261 }
5262
5263 TOML_CONST_INLINE_GETTER
5264 bool is_integer() const noexcept final
5265 {
5266 return std::is_same_v<value_type, int64_t>;
5267 }
5268
5269 TOML_CONST_INLINE_GETTER
5270 bool is_floating_point() const noexcept final
5271 {
5272 return std::is_same_v<value_type, double>;
5273 }
5274
5275 TOML_CONST_INLINE_GETTER
5276 bool is_number() const noexcept final
5277 {
5278 return impl::is_one_of<value_type, int64_t, double>;
5279 }
5280
5281 TOML_CONST_INLINE_GETTER
5282 bool is_boolean() const noexcept final
5283 {
5284 return std::is_same_v<value_type, bool>;
5285 }
5286
5287 TOML_CONST_INLINE_GETTER
5288 bool is_date() const noexcept final
5289 {
5290 return std::is_same_v<value_type, date>;
5291 }
5292
5293 TOML_CONST_INLINE_GETTER
5294 bool is_time() const noexcept final
5295 {
5296 return std::is_same_v<value_type, time>;
5297 }
5298
5299 TOML_CONST_INLINE_GETTER
5300 bool is_date_time() const noexcept final
5301 {
5302 return std::is_same_v<value_type, date_time>;
5303 }
5304
5305 TOML_CONST_INLINE_GETTER
5306 table* as_table() noexcept final
5307 {
5308 return nullptr;
5309 }
5310
5311 TOML_CONST_INLINE_GETTER
5312 array* as_array() noexcept final
5313 {
5314 return nullptr;
5315 }
5316
5317 TOML_CONST_INLINE_GETTER
5318 value<std::string>* as_string() noexcept final
5319 {
5320 return as_value<std::string>(this);
5321 }
5322
5323 TOML_CONST_INLINE_GETTER
5324 value<int64_t>* as_integer() noexcept final
5325 {
5326 return as_value<int64_t>(this);
5327 }
5328
5329 TOML_CONST_INLINE_GETTER
5330 value<double>* as_floating_point() noexcept final
5331 {
5332 return as_value<double>(this);
5333 }
5334
5335 TOML_CONST_INLINE_GETTER
5336 value<bool>* as_boolean() noexcept final
5337 {
5338 return as_value<bool>(this);
5339 }
5340
5341 TOML_CONST_INLINE_GETTER
5342 value<date>* as_date() noexcept final
5343 {
5344 return as_value<date>(this);
5345 }
5346
5347 TOML_CONST_INLINE_GETTER
5348 value<time>* as_time() noexcept final
5349 {
5350 return as_value<time>(this);
5351 }
5352
5353 TOML_CONST_INLINE_GETTER
5354 value<date_time>* as_date_time() noexcept final
5355 {
5356 return as_value<date_time>(this);
5357 }
5358
5359 TOML_CONST_INLINE_GETTER
5360 const table* as_table() const noexcept final
5361 {
5362 return nullptr;
5363 }
5364
5365 TOML_CONST_INLINE_GETTER
5366 const array* as_array() const noexcept final
5367 {
5368 return nullptr;
5369 }
5370
5371 TOML_CONST_INLINE_GETTER
5372 const value<std::string>* as_string() const noexcept final
5373 {
5374 return as_value<std::string>(this);
5375 }
5376
5377 TOML_CONST_INLINE_GETTER
5378 const value<int64_t>* as_integer() const noexcept final
5379 {
5380 return as_value<int64_t>(this);
5381 }
5382
5383 TOML_CONST_INLINE_GETTER
5384 const value<double>* as_floating_point() const noexcept final
5385 {
5386 return as_value<double>(this);
5387 }
5388
5389 TOML_CONST_INLINE_GETTER
5390 const value<bool>* as_boolean() const noexcept final
5391 {
5392 return as_value<bool>(this);
5393 }
5394
5395 TOML_CONST_INLINE_GETTER
5396 const value<date>* as_date() const noexcept final
5397 {
5398 return as_value<date>(this);
5399 }
5400
5401 TOML_CONST_INLINE_GETTER
5402 const value<time>* as_time() const noexcept final
5403 {
5404 return as_value<time>(this);
5405 }
5406
5407 TOML_CONST_INLINE_GETTER
5408 const value<date_time>* as_date_time() const noexcept final
5409 {
5410 return as_value<date_time>(this);
5411 }
5412
5413 TOML_PURE_INLINE_GETTER
5414 value_type& get() & noexcept
5415 {
5416 return val_;
5417 }
5418
5419 TOML_PURE_INLINE_GETTER
5420 value_type&& get() && noexcept
5421 {
5422 return static_cast<value_type&&>(val_);
5423 }
5424
5425 TOML_PURE_INLINE_GETTER
5426 const value_type& get() const& noexcept
5427 {
5428 return val_;
5429 }
5430
5431 TOML_PURE_INLINE_GETTER
5432 const value_type&& get() const&& noexcept
5433 {
5434 return static_cast<const value_type&&>(val_);
5435 }
5436
5437 TOML_PURE_INLINE_GETTER
5438 value_type& operator*() & noexcept
5439 {
5440 return val_;
5441 }
5442
5443 TOML_PURE_INLINE_GETTER
5444 value_type&& operator*() && noexcept
5445 {
5446 return static_cast<value_type&&>(val_);
5447 }
5448
5449 TOML_PURE_INLINE_GETTER
5450 const value_type& operator*() const& noexcept
5451 {
5452 return val_;
5453 }
5454
5455 TOML_PURE_INLINE_GETTER
5456 const value_type&& operator*() const&& noexcept
5457 {
5458 return static_cast<const value_type&&>(val_);
5459 }
5460
5461 TOML_PURE_INLINE_GETTER
5462 explicit operator value_type&() & noexcept
5463 {
5464 return val_;
5465 }
5466
5467 TOML_PURE_INLINE_GETTER
5468 explicit operator value_type&&() && noexcept
5469 {
5470 return static_cast<value_type&&>(val_);
5471 }
5472
5473 TOML_PURE_INLINE_GETTER
5474 explicit operator const value_type&() const& noexcept
5475 {
5476 return val_;
5477 }
5478
5479 TOML_PURE_INLINE_GETTER
5480 explicit operator const value_type&&() && noexcept
5481 {
5482 return static_cast<const value_type&&>(val_);
5483 }
5484
5485 TOML_HIDDEN_CONSTRAINT(std::is_class_v<T>, typename T = value_type)
5486 TOML_PURE_INLINE_GETTER
5487 value_type* operator->() noexcept
5488 {
5489 return &val_;
5490 }
5491
5492 TOML_HIDDEN_CONSTRAINT(std::is_class_v<T>, typename T = value_type)
5493 TOML_PURE_INLINE_GETTER
5494 const value_type* operator->() const noexcept
5495 {
5496 return &val_;
5497 }
5498
5499 TOML_NODISCARD
5500 value_flags flags() const noexcept
5501 {
5502 return flags_;
5503 }
5504
5505 value& flags(value_flags new_flags) noexcept
5506 {
5507 flags_ = new_flags;
5508 return *this;
5509 }
5510
5511 value& operator=(value_arg rhs) noexcept
5512 {
5513 if constexpr (std::is_same_v<value_type, std::string>)
5514 val_.assign(rhs);
5515 else
5516 val_ = rhs;
5517 return *this;
5518 }
5519
5520 TOML_CONSTRAINED_TEMPLATE((std::is_same_v<T, std::string>), typename T = value_type)
5521 value& operator=(std::string&& rhs) noexcept
5522 {
5523 val_ = std::move(rhs);
5524 return *this;
5525 }
5526
5527 TOML_PURE_GETTER
5528 friend bool operator==(const value& lhs, value_arg rhs) noexcept
5529 {
5530 if constexpr (std::is_same_v<value_type, double>)
5531 {
5532 const auto lhs_nan = impl::fpclassify(lhs.val_) == impl::fp_class::nan;
5533 const auto rhs_nan = impl::fpclassify(rhs) == impl::fp_class::nan;
5534 if (lhs_nan != rhs_nan)
5535 return false;
5536 if (lhs_nan)
5537 return true;
5538 }
5539 return lhs.val_ == rhs;
5540 }
5541 TOML_ASYMMETRICAL_EQUALITY_OPS(const value&, value_arg, );
5542
5543 TOML_PURE_GETTER
5544 friend bool operator<(const value& lhs, value_arg rhs) noexcept
5545 {
5546 return lhs.val_ < rhs;
5547 }
5548
5549 TOML_PURE_GETTER
5550 friend bool operator<(value_arg lhs, const value& rhs) noexcept
5551 {
5552 return lhs < rhs.val_;
5553 }
5554
5555 TOML_PURE_GETTER
5556 friend bool operator<=(const value& lhs, value_arg rhs) noexcept
5557 {
5558 return lhs.val_ <= rhs;
5559 }
5560
5561 TOML_PURE_GETTER
5562 friend bool operator<=(value_arg lhs, const value& rhs) noexcept
5563 {
5564 return lhs <= rhs.val_;
5565 }
5566
5567 TOML_PURE_GETTER
5568 friend bool operator>(const value& lhs, value_arg rhs) noexcept
5569 {
5570 return lhs.val_ > rhs;
5571 }
5572
5573 TOML_PURE_GETTER
5574 friend bool operator>(value_arg lhs, const value& rhs) noexcept
5575 {
5576 return lhs > rhs.val_;
5577 }
5578
5579 TOML_PURE_GETTER
5580 friend bool operator>=(const value& lhs, value_arg rhs) noexcept
5581 {
5582 return lhs.val_ >= rhs;
5583 }
5584
5585 TOML_PURE_GETTER
5586 friend bool operator>=(value_arg lhs, const value& rhs) noexcept
5587 {
5588 return lhs >= rhs.val_;
5589 }
5590
5591 template <typename T>
5592 TOML_PURE_GETTER
5593 friend bool operator==(const value& lhs, const value<T>& rhs) noexcept
5594 {
5595 if constexpr (std::is_same_v<value_type, T>)
5596 return lhs == rhs.val_; // calls asymmetrical value-equality operator defined above
5597 else
5598 return false;
5599 }
5600
5601 template <typename T>
5602 TOML_PURE_INLINE_GETTER
5603 friend bool operator!=(const value& lhs, const value<T>& rhs) noexcept
5604 {
5605 return !(lhs == rhs);
5606 }
5607
5608 template <typename T>
5609 TOML_PURE_GETTER
5610 friend bool operator<(const value& lhs, const value<T>& rhs) noexcept
5611 {
5612 if constexpr (std::is_same_v<value_type, T>)
5613 return lhs.val_ < rhs.val_;
5614 else
5615 return impl::node_type_of<value_type> < impl::node_type_of<T>;
5616 }
5617
5618 template <typename T>
5619 TOML_PURE_GETTER
5620 friend bool operator<=(const value& lhs, const value<T>& rhs) noexcept
5621 {
5622 if constexpr (std::is_same_v<value_type, T>)
5623 return lhs.val_ <= rhs.val_;
5624 else
5625 return impl::node_type_of<value_type> <= impl::node_type_of<T>;
5626 }
5627
5628 template <typename T>
5629 TOML_PURE_GETTER
5630 friend bool operator>(const value& lhs, const value<T>& rhs) noexcept
5631 {
5632 if constexpr (std::is_same_v<value_type, T>)
5633 return lhs.val_ > rhs.val_;
5634 else
5635 return impl::node_type_of<value_type> > impl::node_type_of<T>;
5636 }
5637
5638 template <typename T>
5639 TOML_PURE_GETTER
5640 friend bool operator>=(const value& lhs, const value<T>& rhs) noexcept
5641 {
5642 if constexpr (std::is_same_v<value_type, T>)
5643 return lhs.val_ >= rhs.val_;
5644 else
5645 return impl::node_type_of<value_type> >= impl::node_type_of<T>;
5646 }
5647
5648 #if TOML_ENABLE_FORMATTERS
5649
5650 friend std::ostream& operator<<(std::ostream& lhs, const value& rhs)
5651 {
5652 impl::print_to_stream(lhs, rhs);
5653 return lhs;
5654 }
5655
5656 #endif
5657 };
5658
5659 template <typename T>
5660 value(T) -> value<impl::native_type_of<impl::remove_cvref<T>>>;
5661 template <typename T>
5662 value(T, value_flags) -> value<impl::native_type_of<impl::remove_cvref<T>>>;
5663
5664 template <typename T>
5665 TOML_NODISCARD
5666 inline decltype(auto) node::get_value_exact() const noexcept(impl::value_retrieval_is_nothrow<T>)
5667 {
5668 using namespace impl;
5669
5670 static_assert(node_type_of<T> != node_type::none);
5671 static_assert(node_type_of<T> != node_type::table);
5672 static_assert(node_type_of<T> != node_type::array);
5673 static_assert(is_native<T> || can_represent_native<T>);
5674 static_assert(!is_cvref<T>);
5675 TOML_ASSERT(this->type() == node_type_of<T>);
5676
5677 if constexpr (node_type_of<T> == node_type::string)
5678 {
5679 const auto& str = *ref_cast<std::string>();
5680 if constexpr (std::is_same_v<T, std::string>)
5681 return str;
5682 else if constexpr (std::is_same_v<T, std::string_view>)
5683 return T{ str };
5684 else if constexpr (std::is_same_v<T, const char*>)
5685 return str.c_str();
5686
5687 else if constexpr (std::is_same_v<T, std::wstring>)
5688 {
5689 #if TOML_ENABLE_WINDOWS_COMPAT
5690 return widen(str);
5691 #else
5692 static_assert(always_false<T>, "Evaluated unreachable branch!");
5693 #endif
5694 }
5695
5696 #if TOML_HAS_CHAR8
5697
5698 // char -> char8_t (potentially unsafe - the feature is 'experimental'!)
5699 else if constexpr (is_one_of<T, std::u8string, std::u8string_view>)
5700 return T(reinterpret_cast<const char8_t*>(str.c_str()), str.length());
5701 else if constexpr (std::is_same_v<T, const char8_t*>)
5702 return reinterpret_cast<const char8_t*>(str.c_str());
5703 else
5704 static_assert(always_false<T>, "Evaluated unreachable branch!");
5705
5706 #endif
5707 }
5708 else
5709 return static_cast<T>(*ref_cast<native_type_of<T>>());
5710 }
5711
5712 template <typename T>
5713 TOML_NODISCARD
5714 inline optional<T> node::value_exact() const noexcept(impl::value_retrieval_is_nothrow<T>)
5715 {
5716 using namespace impl;
5717
5718 static_assert(!is_wide_string<T> || TOML_ENABLE_WINDOWS_COMPAT,
5719 "Retrieving values as wide-character strings with node::value_exact() is only "
5720 "supported on Windows with TOML_ENABLE_WINDOWS_COMPAT enabled.");
5721
5722 static_assert((is_native<T> || can_represent_native<T>) && !is_cvref<T>,
5723 TOML_SA_VALUE_EXACT_FUNC_MESSAGE("return type of node::value_exact()"));
5724
5725 // prevent additional compiler error spam when the static_assert fails by gating behind if constexpr
5726 if constexpr ((is_native<T> || can_represent_native<T>) && !is_cvref<T>)
5727 {
5728 if (type() == node_type_of<T>)
5729 return { this->get_value_exact<T>() };
5730 else
5731 return {};
5732 }
5733 }
5734
5735 template <typename T>
5736 TOML_NODISCARD
5737 inline optional<T> node::value() const noexcept(impl::value_retrieval_is_nothrow<T>)
5738 {
5739 using namespace impl;
5740
5741 static_assert(!is_wide_string<T> || TOML_ENABLE_WINDOWS_COMPAT,
5742 "Retrieving values as wide-character strings with node::value() is only "
5743 "supported on Windows with TOML_ENABLE_WINDOWS_COMPAT enabled.");
5744 static_assert((is_native<T> || can_represent_native<T> || can_partially_represent_native<T>) && !is_cvref<T>,
5745 TOML_SA_VALUE_FUNC_MESSAGE("return type of node::value()"));
5746
5747 // when asking for strings, dates, times and date_times there's no 'fuzzy' conversion
5748 // semantics to be mindful of so the exact retrieval is enough.
5749 if constexpr (is_natively_one_of<T, std::string, time, date, date_time>)
5750 {
5751 if (type() == node_type_of<T>)
5752 return { this->get_value_exact<T>() };
5753 else
5754 return {};
5755 }
5756
5757 // everything else requires a bit of logicking.
5758 else
5759 {
5760 switch (type())
5761 {
5762 // int -> *
5763 case node_type::integer:
5764 {
5765 // int -> int
5766 if constexpr (is_natively_one_of<T, int64_t>)
5767 {
5768 if constexpr (is_native<T> || can_represent_native<T>)
5769 return static_cast<T>(*ref_cast<int64_t>());
5770 else
5771 return node_integer_cast<T>(*ref_cast<int64_t>());
5772 }
5773
5774 // int -> float
5775 else if constexpr (is_natively_one_of<T, double>)
5776 {
5777 const int64_t val = *ref_cast<int64_t>();
5778 if constexpr (std::numeric_limits<T>::digits < 64)
5779 {
5780 constexpr auto largest_whole_float = (int64_t{ 1 } << std::numeric_limits<T>::digits);
5781 if (val < -largest_whole_float || val > largest_whole_float)
5782 return {};
5783 }
5784 return static_cast<T>(val);
5785 }
5786
5787 // int -> bool
5788 else if constexpr (is_natively_one_of<T, bool>)
5789 return static_cast<bool>(*ref_cast<int64_t>());
5790
5791 // int -> anything else
5792 else
5793 return {};
5794 }
5795
5796 // float -> *
5797 case node_type::floating_point:
5798 {
5799 // float -> float
5800 if constexpr (is_natively_one_of<T, double>)
5801 {
5802 if constexpr (is_native<T> || can_represent_native<T>)
5803 return { static_cast<T>(*ref_cast<double>()) };
5804 else
5805 {
5806 const double val = *ref_cast<double>();
5807 if (impl::fpclassify(val) == fp_class::ok
5808 && (val < (std::numeric_limits<T>::lowest)() || val > (std::numeric_limits<T>::max)()))
5809 return {};
5810 return { static_cast<T>(val) };
5811 }
5812 }
5813
5814 // float -> int
5815 else if constexpr (is_natively_one_of<T, int64_t>)
5816 {
5817 const double val = *ref_cast<double>();
5818 if (impl::fpclassify(val) == fp_class::ok
5819 && static_cast<double>(static_cast<int64_t>(val)) == val)
5820 return node_integer_cast<T>(static_cast<int64_t>(val));
5821 else
5822 return {};
5823 }
5824
5825 // float -> anything else
5826 else
5827 return {};
5828 }
5829
5830 // bool -> *
5831 case node_type::boolean:
5832 {
5833 // bool -> bool
5834 if constexpr (is_natively_one_of<T, bool>)
5835 return { *ref_cast<bool>() };
5836
5837 // bool -> int
5838 else if constexpr (is_natively_one_of<T, int64_t>)
5839 return { static_cast<T>(*ref_cast<bool>()) };
5840
5841 // bool -> anything else
5842 else
5843 return {};
5844 }
5845 }
5846
5847 // non-values, or 'exact' types covered above
5848 return {};
5849 }
5850 }
5851
5852 template <typename T>
5853 TOML_NODISCARD
5854 inline auto node::value_or(T && default_value) const noexcept(impl::value_retrieval_is_nothrow<T>)
5855 {
5856 using namespace impl;
5857
5858 static_assert(!is_wide_string<T> || TOML_ENABLE_WINDOWS_COMPAT,
5859 "Retrieving values as wide-character strings with node::value_or() is only "
5860 "supported on Windows with TOML_ENABLE_WINDOWS_COMPAT enabled.");
5861
5862 if constexpr (is_wide_string<T>)
5863 {
5864 #if TOML_ENABLE_WINDOWS_COMPAT
5865
5866 if (type() == node_type::string)
5867 return widen(*ref_cast<std::string>());
5868 return std::wstring{ static_cast<T&&>(default_value) };
5869
5870 #else
5871
5872 static_assert(always_false<T>, "Evaluated unreachable branch!");
5873
5874 #endif
5875 }
5876 else
5877 {
5878 using value_type =
5879 std::conditional_t<std::is_pointer_v<std::decay_t<T>>,
5880 std::add_pointer_t<std::add_const_t<std::remove_pointer_t<std::decay_t<T>>>>,
5881 std::decay_t<T>>;
5882 using traits = value_traits<value_type>;
5883
5884 // clang-format off
5885
5886 static_assert(
5887 traits::is_native || traits::can_represent_native || traits::can_partially_represent_native,
5888 "The default value type of node::value_or() must be one of:"
5889 TOML_SA_LIST_NEW "A native TOML value type"
5890 TOML_SA_NATIVE_VALUE_TYPE_LIST
5891
5892 TOML_SA_LIST_NXT "A non-view type capable of losslessly representing a native TOML value type"
5893 TOML_SA_LIST_BEG "std::string"
5894 #if TOML_ENABLE_WINDOWS_COMPAT
5895 TOML_SA_LIST_SEP "std::wstring"
5896 #endif
5897 TOML_SA_LIST_SEP "any signed integer type >= 64 bits"
5898 TOML_SA_LIST_SEP "any floating-point type >= 64 bits"
5899 TOML_SA_LIST_END
5900
5901 TOML_SA_LIST_NXT "A non-view type capable of (reasonably) representing a native TOML value type"
5902 TOML_SA_LIST_BEG "any other integral type"
5903 TOML_SA_LIST_SEP "any floating-point type"
5904 TOML_SA_LIST_END
5905
5906 TOML_SA_LIST_NXT "A compatible view type"
5907 TOML_SA_LIST_BEG "std::string_view"
5908 #if TOML_HAS_CHAR8
5909 TOML_SA_LIST_SEP "std::u8string_view"
5910 #endif
5911 #if TOML_ENABLE_WINDOWS_COMPAT
5912 TOML_SA_LIST_SEP "std::wstring_view"
5913 #endif
5914 TOML_SA_LIST_SEP "const char*"
5915 #if TOML_HAS_CHAR8
5916 TOML_SA_LIST_SEP "const char8_t*"
5917 #endif
5918 #if TOML_ENABLE_WINDOWS_COMPAT
5919 TOML_SA_LIST_SEP "const wchar_t*"
5920 #endif
5921 TOML_SA_LIST_END
5922 );
5923
5924 // clang-format on
5925
5926 // prevent additional compiler error spam when the static_assert fails by gating behind if constexpr
5927 if constexpr (traits::is_native || traits::can_represent_native || traits::can_partially_represent_native)
5928 {
5929 if constexpr (traits::is_native)
5930 {
5931 if (type() == node_type_of<value_type>)
5932 return *ref_cast<typename traits::native_type>();
5933 }
5934 if (auto val = this->value<value_type>())
5935 return *val;
5936 if constexpr (std::is_pointer_v<value_type>)
5937 return value_type{ default_value };
5938 else
5939 return static_cast<T&&>(default_value);
5940 }
5941 }
5942 }
5943 }
5944 TOML_NAMESPACE_END;
5945
5946 #ifdef _MSC_VER
5947 #pragma pop_macro("min")
5948 #pragma pop_macro("max")
5949 #ifndef __clang__
5950 #pragma inline_recursion(off)
5951 #endif
5952 #endif
5953 TOML_POP_WARNINGS;
5954
5955 //******** impl/make_node.hpp ****************************************************************************************
5956
5957 TOML_PUSH_WARNINGS;
5958 #ifdef _MSC_VER
5959 #ifndef __clang__
5960 #pragma inline_recursion(on)
5961 #endif
5962 #pragma push_macro("min")
5963 #pragma push_macro("max")
5964 #undef min
5965 #undef max
5966 #endif
5967
5968 TOML_IMPL_NAMESPACE_START
5969 {
5970 template <typename T>
5971 TOML_NODISCARD
5972 TOML_ATTR(returns_nonnull)
5973 auto* make_node_impl_specialized(T && val, [[maybe_unused]] value_flags flags)
5974 {
5975 using unwrapped_type = unwrap_node<remove_cvref<T>>;
5976 static_assert(!std::is_same_v<unwrapped_type, node>);
5977 static_assert(!is_node_view<unwrapped_type>);
5978
5979 // arrays + tables - invoke copy/move ctor
5980 if constexpr (is_one_of<unwrapped_type, array, table>)
5981 {
5982 return new unwrapped_type(static_cast<T&&>(val));
5983 }
5984
5985 // values
5986 else
5987 {
5988 using native_type = native_type_of<unwrapped_type>;
5989 using value_type = value<native_type>;
5990
5991 value_type* out;
5992
5993 // copy/move ctor
5994 if constexpr (std::is_same_v<remove_cvref<T>, value_type>)
5995 {
5996 out = new value_type{ static_cast<T&&>(val), flags };
5997 }
5998
5999 // creating from raw value
6000 else
6001 {
6002 static_assert(!is_wide_string<T> || TOML_ENABLE_WINDOWS_COMPAT,
6003 "Instantiating values from wide-character strings is only "
6004 "supported on Windows with TOML_ENABLE_WINDOWS_COMPAT enabled.");
6005
6006 if constexpr (!is_losslessly_convertible_to_native<unwrapped_type>)
6007 {
6008 if constexpr (std::is_same_v<native_type, int64_t>)
6009 static_assert(always_false<T>,
6010 "Integral value initializers must be losslessly convertible to int64_t");
6011 else if constexpr (std::is_same_v<native_type, double>)
6012 static_assert(always_false<T>,
6013 "Floating-point value initializers must be losslessly convertible to double");
6014 else
6015 static_assert(
6016 always_false<T>,
6017 "Value initializers must be losslessly convertible to one of the TOML value types");
6018 }
6019
6020 if constexpr (is_wide_string<T>)
6021 {
6022 #if TOML_ENABLE_WINDOWS_COMPAT
6023 out = new value_type{ narrow(static_cast<T&&>(val)) };
6024 #else
6025 static_assert(always_false<T>, "Evaluated unreachable branch!");
6026 #endif
6027 }
6028 else
6029 out = new value_type{ static_cast<T&&>(val) };
6030
6031 if (flags != preserve_source_value_flags)
6032 out->flags(flags);
6033 }
6034
6035 return out;
6036 }
6037 }
6038
6039 template <typename T>
6040 TOML_NODISCARD
6041 auto* make_node_impl(T && val, value_flags flags = preserve_source_value_flags)
6042 {
6043 using unwrapped_type = unwrap_node<remove_cvref<T>>;
6044 if constexpr (std::is_same_v<unwrapped_type, node> || is_node_view<unwrapped_type>)
6045 {
6046 if constexpr (is_node_view<unwrapped_type>)
6047 {
6048 if (!val)
6049 return static_cast<toml::node*>(nullptr);
6050 }
6051
6052 return static_cast<T&&>(val).visit(
6053 [flags](auto&& concrete) {
6054 return static_cast<toml::node*>(
6055 make_node_impl_specialized(static_cast<decltype(concrete)&&>(concrete), flags));
6056 });
6057 }
6058 else
6059 return make_node_impl_specialized(static_cast<T&&>(val), flags);
6060 }
6061
6062 template <typename T>
6063 TOML_NODISCARD
6064 auto* make_node_impl(inserter<T> && val, value_flags flags = preserve_source_value_flags)
6065 {
6066 return make_node_impl(static_cast<T&&>(val.value), flags);
6067 }
6068
6069 template <typename T, bool = (is_node<T> || is_node_view<T> || is_value<T> || can_partially_represent_native<T>)>
6070 struct inserted_type_of_
6071 {
6072 using type = std::remove_pointer_t<decltype(make_node_impl(std::declval<T>()))>;
6073 };
6074
6075 template <typename T>
6076 struct inserted_type_of_<inserter<T>, false>
6077 {
6078 using type = typename inserted_type_of_<remove_cvref<T>>::type;
6079 };
6080
6081 template <typename T>
6082 struct inserted_type_of_<T, false>
6083 {
6084 using type = void;
6085 };
6086
6087 template <typename T>
6088 TOML_NODISCARD
6089 node_ptr make_node(T && val, value_flags flags = preserve_source_value_flags)
6090 {
6091 return node_ptr{ make_node_impl(static_cast<T&&>(val), flags) };
6092 }
6093
6094 template <typename... T>
6095 struct emplaced_type_of_
6096 {
6097 using type = void;
6098 };
6099
6100 template <typename T>
6101 struct emplaced_type_of_<T>
6102 {
6103 using type = std::conditional_t<is_one_of<T, node, node_view<node>, node_view<const node>>,
6104 void,
6105 typename inserted_type_of_<T>::type>;
6106 };
6107
6108 template <typename T>
6109 struct emplaced_type_of_<inserter<T>>
6110 {
6111 using type = typename emplaced_type_of_<remove_cvref<T>>::type;
6112 };
6113
6114 template <typename... T>
6115 using emplaced_type_of = typename emplaced_type_of_<remove_cvref<T>...>::type;
6116 }
6117 TOML_IMPL_NAMESPACE_END;
6118
6119 TOML_NAMESPACE_START
6120 {
6121 template <typename T>
6122 using inserted_type_of = POXY_IMPLEMENTATION_DETAIL(typename impl::inserted_type_of_<impl::remove_cvref<T>>::type);
6123 }
6124 TOML_NAMESPACE_END;
6125
6126 #ifdef _MSC_VER
6127 #pragma pop_macro("min")
6128 #pragma pop_macro("max")
6129 #ifndef __clang__
6130 #pragma inline_recursion(off)
6131 #endif
6132 #endif
6133 TOML_POP_WARNINGS;
6134
6135 //******** impl/array.hpp ********************************************************************************************
6136
6137 TOML_PUSH_WARNINGS;
6138 #ifdef _MSC_VER
6139 #ifndef __clang__
6140 #pragma inline_recursion(on)
6141 #endif
6142 #pragma push_macro("min")
6143 #pragma push_macro("max")
6144 #undef min
6145 #undef max
6146 #endif
6147
6148 #ifndef TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN
6149 #if TOML_GCC && TOML_GCC <= 7
6150 #define TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN 1
6151 #else
6152 #define TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN 0
6153 #endif
6154 #endif
6155
6156 #if TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN && !defined(TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN_ACKNOWLEDGED)
6157 #define TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN_MESSAGE \
6158 "If you're seeing this error it's because you're using one of toml++'s for_each() functions on a compiler with " \
6159 "known bugs in that area (e.g. GCC 7). On these compilers returning a bool (or bool-convertible) value from the " \
6160 "for_each() callable causes spurious compilation failures, while returning nothing (void) works fine. " \
6161 "If you believe this message is incorrect for your compiler, you can try your luck by #defining " \
6162 "TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN as 0 and recompiling - if it works, great! Let me know at " \
6163 "https://github.com/marzer/tomlplusplus/issues. Alternatively, if you don't have any need for early-exiting from " \
6164 "for_each(), you can suppress this error by #defining TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN_ACKNOWLEDGED " \
6165 "and moving on with your life."
6166 #endif
6167
6168 TOML_IMPL_NAMESPACE_START
6169 {
6170 template <bool IsConst>
6171 class TOML_TRIVIAL_ABI array_iterator
6172 {
6173 private:
6174 template <bool>
6175 friend class array_iterator;
6176
6177 using mutable_vector_iterator = std::vector<node_ptr>::iterator;
6178 using const_vector_iterator = std::vector<node_ptr>::const_iterator;
6179 using vector_iterator = std::conditional_t<IsConst, const_vector_iterator, mutable_vector_iterator>;
6180
6181 mutable vector_iterator iter_;
6182
6183 public:
6184 using value_type = std::conditional_t<IsConst, const node, node>;
6185 using reference = value_type&;
6186 using pointer = value_type*;
6187 using difference_type = ptrdiff_t;
6188 using iterator_category = typename std::iterator_traits<vector_iterator>::iterator_category;
6189
6190 TOML_NODISCARD_CTOR
6191 array_iterator() noexcept = default;
6192
6193 TOML_NODISCARD_CTOR
6194 explicit array_iterator(mutable_vector_iterator iter) noexcept //
6195 : iter_{ iter }
6196 {}
6197
6198 TOML_CONSTRAINED_TEMPLATE(C, bool C = IsConst)
6199 TOML_NODISCARD_CTOR
6200 explicit array_iterator(const_vector_iterator iter) noexcept //
6201 : iter_{ iter }
6202 {}
6203
6204 TOML_CONSTRAINED_TEMPLATE(C, bool C = IsConst)
6205 TOML_NODISCARD_CTOR
6206 array_iterator(const array_iterator<false>& other) noexcept //
6207 : iter_{ other.iter_ }
6208 {}
6209
6210 TOML_NODISCARD_CTOR
6211 array_iterator(const array_iterator&) noexcept = default;
6212
6213 array_iterator& operator=(const array_iterator&) noexcept = default;
6214
6215 array_iterator& operator++() noexcept // ++pre
6216 {
6217 ++iter_;
6218 return *this;
6219 }
6220
6221 array_iterator operator++(int) noexcept // post++
6222 {
6223 array_iterator out{ iter_ };
6224 ++iter_;
6225 return out;
6226 }
6227
6228 array_iterator& operator--() noexcept // --pre
6229 {
6230 --iter_;
6231 return *this;
6232 }
6233
6234 array_iterator operator--(int) noexcept // post--
6235 {
6236 array_iterator out{ iter_ };
6237 --iter_;
6238 return out;
6239 }
6240
6241 TOML_PURE_INLINE_GETTER
6242 reference operator*() const noexcept
6243 {
6244 return *iter_->get();
6245 }
6246
6247 TOML_PURE_INLINE_GETTER
6248 pointer operator->() const noexcept
6249 {
6250 return iter_->get();
6251 }
6252
6253 TOML_PURE_INLINE_GETTER
6254 explicit operator const vector_iterator&() const noexcept
6255 {
6256 return iter_;
6257 }
6258
6259 TOML_CONSTRAINED_TEMPLATE(!C, bool C = IsConst)
6260 TOML_PURE_INLINE_GETTER
6261 explicit operator const const_vector_iterator() const noexcept
6262 {
6263 return iter_;
6264 }
6265
6266 array_iterator& operator+=(ptrdiff_t rhs) noexcept
6267 {
6268 iter_ += rhs;
6269 return *this;
6270 }
6271
6272 array_iterator& operator-=(ptrdiff_t rhs) noexcept
6273 {
6274 iter_ -= rhs;
6275 return *this;
6276 }
6277
6278 TOML_NODISCARD
6279 friend array_iterator operator+(const array_iterator& lhs, ptrdiff_t rhs) noexcept
6280 {
6281 return array_iterator{ lhs.iter_ + rhs };
6282 }
6283
6284 TOML_NODISCARD
6285 friend array_iterator operator+(ptrdiff_t lhs, const array_iterator& rhs) noexcept
6286 {
6287 return array_iterator{ rhs.iter_ + lhs };
6288 }
6289
6290 TOML_NODISCARD
6291 friend array_iterator operator-(const array_iterator& lhs, ptrdiff_t rhs) noexcept
6292 {
6293 return array_iterator{ lhs.iter_ - rhs };
6294 }
6295
6296 TOML_PURE_INLINE_GETTER
6297 friend ptrdiff_t operator-(const array_iterator& lhs, const array_iterator& rhs) noexcept
6298 {
6299 return lhs.iter_ - rhs.iter_;
6300 }
6301
6302 TOML_PURE_INLINE_GETTER
6303 friend bool operator==(const array_iterator& lhs, const array_iterator& rhs) noexcept
6304 {
6305 return lhs.iter_ == rhs.iter_;
6306 }
6307
6308 TOML_PURE_INLINE_GETTER
6309 friend bool operator!=(const array_iterator& lhs, const array_iterator& rhs) noexcept
6310 {
6311 return lhs.iter_ != rhs.iter_;
6312 }
6313
6314 TOML_PURE_INLINE_GETTER
6315 friend bool operator<(const array_iterator& lhs, const array_iterator& rhs) noexcept
6316 {
6317 return lhs.iter_ < rhs.iter_;
6318 }
6319
6320 TOML_PURE_INLINE_GETTER
6321 friend bool operator<=(const array_iterator& lhs, const array_iterator& rhs) noexcept
6322 {
6323 return lhs.iter_ <= rhs.iter_;
6324 }
6325
6326 TOML_PURE_INLINE_GETTER
6327 friend bool operator>(const array_iterator& lhs, const array_iterator& rhs) noexcept
6328 {
6329 return lhs.iter_ > rhs.iter_;
6330 }
6331
6332 TOML_PURE_INLINE_GETTER
6333 friend bool operator>=(const array_iterator& lhs, const array_iterator& rhs) noexcept
6334 {
6335 return lhs.iter_ >= rhs.iter_;
6336 }
6337
6338 TOML_PURE_INLINE_GETTER
6339 reference operator[](ptrdiff_t idx) const noexcept
6340 {
6341 return *(iter_ + idx)->get();
6342 }
6343 };
6344
6345 struct array_init_elem
6346 {
6347 mutable node_ptr value;
6348
6349 template <typename T>
6350 TOML_NODISCARD_CTOR
6351 array_init_elem(T&& val, value_flags flags = preserve_source_value_flags) //
6352 : value{ make_node(static_cast<T&&>(val), flags) }
6353 {}
6354 };
6355 }
6356 TOML_IMPL_NAMESPACE_END;
6357
6358 TOML_NAMESPACE_START
6359 {
6360 using array_iterator = POXY_IMPLEMENTATION_DETAIL(impl::array_iterator<false>);
6361
6362 using const_array_iterator = POXY_IMPLEMENTATION_DETAIL(impl::array_iterator<true>);
6363
6364 class TOML_EXPORTED_CLASS array : public node
6365 {
6366 private:
6367
6368 using vector_type = std::vector<impl::node_ptr>;
6369 using vector_iterator = typename vector_type::iterator;
6370 using const_vector_iterator = typename vector_type::const_iterator;
6371 vector_type elems_;
6372
6373 TOML_NODISCARD_CTOR
6374 TOML_EXPORTED_MEMBER_FUNCTION
6375 array(const impl::array_init_elem*, const impl::array_init_elem*);
6376
6377 TOML_NODISCARD_CTOR
6378 array(std::false_type, std::initializer_list<impl::array_init_elem> elems) //
6379 : array{ elems.begin(), elems.end() }
6380 {}
6381
6382 TOML_EXPORTED_MEMBER_FUNCTION
6383 void preinsertion_resize(size_t idx, size_t count);
6384
6385 TOML_EXPORTED_MEMBER_FUNCTION
6386 void insert_at_back(impl::node_ptr&&);
6387
6388 TOML_EXPORTED_MEMBER_FUNCTION
6389 vector_iterator insert_at(const_vector_iterator, impl::node_ptr&&);
6390
6391 template <typename T>
6392 void emplace_back_if_not_empty_view(T&& val, value_flags flags)
6393 {
6394 if constexpr (is_node_view<T>)
6395 {
6396 if (!val)
6397 return;
6398 }
6399 insert_at_back(impl::make_node(static_cast<T&&>(val), flags));
6400 }
6401
6402 TOML_NODISCARD
6403 TOML_EXPORTED_MEMBER_FUNCTION
6404 size_t total_leaf_count() const noexcept;
6405
6406 TOML_EXPORTED_MEMBER_FUNCTION
6407 void flatten_child(array&& child, size_t& dest_index) noexcept;
6408
6409 public:
6410 using value_type = node;
6411 using size_type = size_t;
6412 using difference_type = ptrdiff_t;
6413 using reference = node&;
6414 using const_reference = const node&;
6415
6416 TOML_NODISCARD_CTOR
6417 TOML_EXPORTED_MEMBER_FUNCTION
6418 array() noexcept;
6419
6420 TOML_EXPORTED_MEMBER_FUNCTION
6421 ~array() noexcept;
6422
6423 TOML_NODISCARD_CTOR
6424 TOML_EXPORTED_MEMBER_FUNCTION
6425 array(const array&);
6426
6427 TOML_NODISCARD_CTOR
6428 TOML_EXPORTED_MEMBER_FUNCTION
6429 array(array&& other) noexcept;
6430
6431 TOML_CONSTRAINED_TEMPLATE((sizeof...(ElemTypes) > 0 || !std::is_same_v<impl::remove_cvref<ElemType>, array>),
6432 typename ElemType,
6433 typename... ElemTypes)
6434 TOML_NODISCARD_CTOR
6435 explicit array(ElemType&& val, ElemTypes&&... vals)
6436 : array{ std::false_type{},
6437 std::initializer_list<impl::array_init_elem>{ static_cast<ElemType&&>(val),
6438 static_cast<ElemTypes&&>(vals)... } }
6439 {}
6440
6441 TOML_EXPORTED_MEMBER_FUNCTION
6442 array& operator=(const array&);
6443
6444 TOML_EXPORTED_MEMBER_FUNCTION
6445 array& operator=(array&& rhs) noexcept;
6446
6447 TOML_CONST_INLINE_GETTER
6448 node_type type() const noexcept final
6449 {
6450 return node_type::array;
6451 }
6452
6453 TOML_PURE_GETTER
6454 TOML_EXPORTED_MEMBER_FUNCTION
6455 bool is_homogeneous(node_type ntype) const noexcept final;
6456
6457 TOML_NODISCARD
6458 TOML_EXPORTED_MEMBER_FUNCTION
6459 bool is_homogeneous(node_type ntype, node*& first_nonmatch) noexcept final;
6460
6461 TOML_NODISCARD
6462 TOML_EXPORTED_MEMBER_FUNCTION
6463 bool is_homogeneous(node_type ntype, const node*& first_nonmatch) const noexcept final;
6464
6465 template <typename ElemType = void>
6466 TOML_PURE_GETTER
6467 bool is_homogeneous() const noexcept
6468 {
6469 using type = impl::remove_cvref<impl::unwrap_node<ElemType>>;
6470 static_assert(std::is_void_v<type> || toml::is_value<type> || toml::is_container<type>,
6471 "The template type argument of array::is_homogeneous() must be void or one "
6472 "of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
6473
6474 return is_homogeneous(impl::node_type_of<type>);
6475 }
6476 TOML_CONST_INLINE_GETTER
6477 bool is_table() const noexcept final
6478 {
6479 return false;
6480 }
6481
6482 TOML_CONST_INLINE_GETTER
6483 bool is_array() const noexcept final
6484 {
6485 return true;
6486 }
6487
6488 TOML_PURE_GETTER
6489 bool is_array_of_tables() const noexcept final
6490 {
6491 return is_homogeneous(node_type::table);
6492 }
6493
6494 TOML_CONST_INLINE_GETTER
6495 bool is_value() const noexcept final
6496 {
6497 return false;
6498 }
6499
6500 TOML_CONST_INLINE_GETTER
6501 bool is_string() const noexcept final
6502 {
6503 return false;
6504 }
6505
6506 TOML_CONST_INLINE_GETTER
6507 bool is_integer() const noexcept final
6508 {
6509 return false;
6510 }
6511
6512 TOML_CONST_INLINE_GETTER
6513 bool is_floating_point() const noexcept final
6514 {
6515 return false;
6516 }
6517
6518 TOML_CONST_INLINE_GETTER
6519 bool is_number() const noexcept final
6520 {
6521 return false;
6522 }
6523
6524 TOML_CONST_INLINE_GETTER
6525 bool is_boolean() const noexcept final
6526 {
6527 return false;
6528 }
6529
6530 TOML_CONST_INLINE_GETTER
6531 bool is_date() const noexcept final
6532 {
6533 return false;
6534 }
6535
6536 TOML_CONST_INLINE_GETTER
6537 bool is_time() const noexcept final
6538 {
6539 return false;
6540 }
6541
6542 TOML_CONST_INLINE_GETTER
6543 bool is_date_time() const noexcept final
6544 {
6545 return false;
6546 }
6547
6548 TOML_CONST_INLINE_GETTER
6549 table* as_table() noexcept final
6550 {
6551 return nullptr;
6552 }
6553
6554 TOML_CONST_INLINE_GETTER
6555 array* as_array() noexcept final
6556 {
6557 return this;
6558 }
6559
6560 TOML_CONST_INLINE_GETTER
6561 toml::value<std::string>* as_string() noexcept final
6562 {
6563 return nullptr;
6564 }
6565
6566 TOML_CONST_INLINE_GETTER
6567 toml::value<int64_t>* as_integer() noexcept final
6568 {
6569 return nullptr;
6570 }
6571
6572 TOML_CONST_INLINE_GETTER
6573 toml::value<double>* as_floating_point() noexcept final
6574 {
6575 return nullptr;
6576 }
6577
6578 TOML_CONST_INLINE_GETTER
6579 toml::value<bool>* as_boolean() noexcept final
6580 {
6581 return nullptr;
6582 }
6583
6584 TOML_CONST_INLINE_GETTER
6585 toml::value<date>* as_date() noexcept final
6586 {
6587 return nullptr;
6588 }
6589
6590 TOML_CONST_INLINE_GETTER
6591 toml::value<time>* as_time() noexcept final
6592 {
6593 return nullptr;
6594 }
6595
6596 TOML_CONST_INLINE_GETTER
6597 toml::value<date_time>* as_date_time() noexcept final
6598 {
6599 return nullptr;
6600 }
6601
6602 TOML_CONST_INLINE_GETTER
6603 const table* as_table() const noexcept final
6604 {
6605 return nullptr;
6606 }
6607
6608 TOML_CONST_INLINE_GETTER
6609 const array* as_array() const noexcept final
6610 {
6611 return this;
6612 }
6613
6614 TOML_CONST_INLINE_GETTER
6615 const toml::value<std::string>* as_string() const noexcept final
6616 {
6617 return nullptr;
6618 }
6619
6620 TOML_CONST_INLINE_GETTER
6621 const toml::value<int64_t>* as_integer() const noexcept final
6622 {
6623 return nullptr;
6624 }
6625
6626 TOML_CONST_INLINE_GETTER
6627 const toml::value<double>* as_floating_point() const noexcept final
6628 {
6629 return nullptr;
6630 }
6631
6632 TOML_CONST_INLINE_GETTER
6633 const toml::value<bool>* as_boolean() const noexcept final
6634 {
6635 return nullptr;
6636 }
6637
6638 TOML_CONST_INLINE_GETTER
6639 const toml::value<date>* as_date() const noexcept final
6640 {
6641 return nullptr;
6642 }
6643
6644 TOML_CONST_INLINE_GETTER
6645 const toml::value<time>* as_time() const noexcept final
6646 {
6647 return nullptr;
6648 }
6649
6650 TOML_CONST_INLINE_GETTER
6651 const toml::value<date_time>* as_date_time() const noexcept final
6652 {
6653 return nullptr;
6654 }
6655
6656 TOML_PURE_INLINE_GETTER
6657 node* get(size_t index) noexcept
6658 {
6659 return index < elems_.size() ? elems_[index].get() : nullptr;
6660 }
6661
6662 TOML_PURE_INLINE_GETTER
6663 const node* get(size_t index) const noexcept
6664 {
6665 return const_cast<array&>(*this).get(index);
6666 }
6667
6668 template <typename ElemType>
6669 TOML_NODISCARD
6670 impl::wrap_node<ElemType>* get_as(size_t index) noexcept
6671 {
6672 if (auto val = get(index))
6673 return val->template as<ElemType>();
6674 return nullptr;
6675 }
6676
6677 template <typename ElemType>
6678 TOML_NODISCARD
6679 const impl::wrap_node<ElemType>* get_as(size_t index) const noexcept
6680 {
6681 return const_cast<array&>(*this).template get_as<ElemType>(index);
6682 }
6683
6684 using node::operator[]; // inherit operator[toml::path]
6685 TOML_NODISCARD
6686 node& operator[](size_t index) noexcept
6687 {
6688 return *elems_[index];
6689 }
6690
6691 TOML_NODISCARD
6692 const node& operator[](size_t index) const noexcept
6693 {
6694 return *elems_[index];
6695 }
6696
6697 TOML_NODISCARD
6698 TOML_EXPORTED_MEMBER_FUNCTION
6699 node& at(size_t index);
6700
6701 TOML_NODISCARD
6702 const node& at(size_t index) const
6703 {
6704 return const_cast<array&>(*this).at(index);
6705 }
6706
6707 TOML_NODISCARD
6708 node& front() noexcept
6709 {
6710 return *elems_.front();
6711 }
6712
6713 TOML_NODISCARD
6714 const node& front() const noexcept
6715 {
6716 return *elems_.front();
6717 }
6718
6719 TOML_NODISCARD
6720 node& back() noexcept
6721 {
6722 return *elems_.back();
6723 }
6724
6725 TOML_NODISCARD
6726 const node& back() const noexcept
6727 {
6728 return *elems_.back();
6729 }
6730
6731 using iterator = array_iterator;
6732
6733 using const_iterator = const_array_iterator;
6734
6735 TOML_NODISCARD
6736 iterator begin() noexcept
6737 {
6738 return iterator{ elems_.begin() };
6739 }
6740
6741 TOML_NODISCARD
6742 const_iterator begin() const noexcept
6743 {
6744 return const_iterator{ elems_.cbegin() };
6745 }
6746
6747 TOML_NODISCARD
6748 const_iterator cbegin() const noexcept
6749 {
6750 return const_iterator{ elems_.cbegin() };
6751 }
6752
6753 TOML_NODISCARD
6754 iterator end() noexcept
6755 {
6756 return iterator{ elems_.end() };
6757 }
6758
6759 TOML_NODISCARD
6760 const_iterator end() const noexcept
6761 {
6762 return const_iterator{ elems_.cend() };
6763 }
6764
6765 TOML_NODISCARD
6766 const_iterator cend() const noexcept
6767 {
6768 return const_iterator{ elems_.cend() };
6769 }
6770
6771 private:
6772
6773 template <typename T, typename Array>
6774 using for_each_elem_ref = impl::copy_cvref<impl::wrap_node<impl::remove_cvref<impl::unwrap_node<T>>>, Array>;
6775
6776 template <typename Func, typename Array, typename T>
6777 using can_for_each = std::disjunction<std::is_invocable<Func, for_each_elem_ref<T, Array>, size_t>,
6778 std::is_invocable<Func, size_t, for_each_elem_ref<T, Array>>,
6779 std::is_invocable<Func, for_each_elem_ref<T, Array>>>;
6780
6781 template <typename Func, typename Array, typename T>
6782 using can_for_each_nothrow = std::conditional_t<
6783 // first form
6784 std::is_invocable_v<Func, for_each_elem_ref<T, Array>, size_t>,
6785 std::is_nothrow_invocable<Func, for_each_elem_ref<T, Array>, size_t>,
6786 std::conditional_t<
6787 // second form
6788 std::is_invocable_v<Func, size_t, for_each_elem_ref<T, Array>>,
6789 std::is_nothrow_invocable<Func, size_t, for_each_elem_ref<T, Array>>,
6790 std::conditional_t<
6791 // third form
6792 std::is_invocable_v<Func, for_each_elem_ref<T, Array>>,
6793 std::is_nothrow_invocable<Func, for_each_elem_ref<T, Array>>,
6794 std::false_type>>>;
6795
6796 template <typename Func, typename Array>
6797 using can_for_each_any = std::disjunction<can_for_each<Func, Array, table>,
6798 can_for_each<Func, Array, array>,
6799 can_for_each<Func, Array, std::string>,
6800 can_for_each<Func, Array, int64_t>,
6801 can_for_each<Func, Array, double>,
6802 can_for_each<Func, Array, bool>,
6803 can_for_each<Func, Array, date>,
6804 can_for_each<Func, Array, time>,
6805 can_for_each<Func, Array, date_time>>;
6806
6807 template <typename Func, typename Array, typename T>
6808 using for_each_is_nothrow_one = std::disjunction<std::negation<can_for_each<Func, Array, T>>, //
6809 can_for_each_nothrow<Func, Array, T>>;
6810
6811 template <typename Func, typename Array>
6812 using for_each_is_nothrow = std::conjunction<for_each_is_nothrow_one<Func, Array, table>,
6813 for_each_is_nothrow_one<Func, Array, array>,
6814 for_each_is_nothrow_one<Func, Array, std::string>,
6815 for_each_is_nothrow_one<Func, Array, int64_t>,
6816 for_each_is_nothrow_one<Func, Array, double>,
6817 for_each_is_nothrow_one<Func, Array, bool>,
6818 for_each_is_nothrow_one<Func, Array, date>,
6819 for_each_is_nothrow_one<Func, Array, time>,
6820 for_each_is_nothrow_one<Func, Array, date_time>>;
6821
6822 template <typename Func, typename Array>
6823 static void do_for_each(Func&& visitor, Array&& arr) //
6824 noexcept(for_each_is_nothrow<Func&&, Array&&>::value)
6825 {
6826 static_assert(can_for_each_any<Func&&, Array&&>::value,
6827 "TOML array for_each visitors must be invocable for at least one of the toml::node "
6828 "specializations:" TOML_SA_NODE_TYPE_LIST);
6829
6830 for (size_t i = 0; i < arr.size(); i++)
6831 {
6832 using node_ref = impl::copy_cvref<toml::node, Array&&>;
6833 static_assert(std::is_reference_v<node_ref>);
6834
6835 #if TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN
6836
6837 #ifndef TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN_ACKNOWLEDGED
6838 static_assert(impl::always_false<Func, Array, node_ref>, //
6839 TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN_MESSAGE);
6840 #endif
6841
6842 static_cast<node_ref>(static_cast<Array&&>(arr)[i])
6843 .visit(
6844 [&]([[maybe_unused]] auto&& elem) //
6845 noexcept(for_each_is_nothrow_one<Func&&, Array&&, decltype(elem)>::value)
6846 {
6847 using elem_ref = for_each_elem_ref<decltype(elem), Array&&>;
6848 static_assert(std::is_reference_v<elem_ref>);
6849
6850 // func(elem, i)
6851 if constexpr (std::is_invocable_v<Func&&, elem_ref, size_t>)
6852 {
6853 static_cast<Func&&>(visitor)(static_cast<elem_ref>(elem), i);
6854 }
6855
6856 // func(i, elem)
6857 else if constexpr (std::is_invocable_v<Func&&, size_t, elem_ref>)
6858 {
6859 static_cast<Func&&>(visitor)(i, static_cast<elem_ref>(elem));
6860 }
6861
6862 // func(elem)
6863 else if constexpr (std::is_invocable_v<Func&&, elem_ref>)
6864 {
6865 static_cast<Func&&>(visitor)(static_cast<elem_ref>(elem));
6866 }
6867 });
6868
6869 #else
6870 const auto keep_going =
6871 static_cast<node_ref>(static_cast<Array&&>(arr)[i])
6872 .visit(
6873 [&]([[maybe_unused]] auto&& elem) //
6874 // Define this macro as a workaround to compile errors caused by a bug in MSVC's "legacy lambda processor".
6875 #if !TOML_DISABLE_CONDITIONAL_NOEXCEPT_LAMBDA
6876 noexcept(for_each_is_nothrow_one<Func&&, Array&&, decltype(elem)>::value)
6877 #endif
6878 {
6879 using elem_ref = for_each_elem_ref<decltype(elem), Array&&>;
6880 static_assert(std::is_reference_v<elem_ref>);
6881
6882 // func(elem, i)
6883 if constexpr (std::is_invocable_v<Func&&, elem_ref, size_t>)
6884 {
6885 using return_type =
6886 decltype(static_cast<Func&&>(visitor)(static_cast<elem_ref>(elem), i));
6887
6888 if constexpr (impl::is_constructible_or_convertible<bool, return_type>)
6889 {
6890 return static_cast<bool>(
6891 static_cast<Func&&>(visitor)(static_cast<elem_ref>(elem), i));
6892 }
6893 else
6894 {
6895 static_cast<Func&&>(visitor)(static_cast<elem_ref>(elem), i);
6896 return true;
6897 }
6898 }
6899
6900 // func(i, elem)
6901 else if constexpr (std::is_invocable_v<Func&&, size_t, elem_ref>)
6902 {
6903 using return_type =
6904 decltype(static_cast<Func&&>(visitor)(i, static_cast<elem_ref>(elem)));
6905
6906 if constexpr (impl::is_constructible_or_convertible<bool, return_type>)
6907 {
6908 return static_cast<bool>(
6909 static_cast<Func&&>(visitor)(i, static_cast<elem_ref>(elem)));
6910 }
6911 else
6912 {
6913 static_cast<Func&&>(visitor)(i, static_cast<elem_ref>(elem));
6914 return true;
6915 }
6916 }
6917
6918 // func(elem)
6919 else if constexpr (std::is_invocable_v<Func&&, elem_ref>)
6920 {
6921 using return_type =
6922 decltype(static_cast<Func&&>(visitor)(static_cast<elem_ref>(elem)));
6923
6924 if constexpr (impl::is_constructible_or_convertible<bool, return_type>)
6925 {
6926 return static_cast<bool>(
6927 static_cast<Func&&>(visitor)(static_cast<elem_ref>(elem)));
6928 }
6929 else
6930 {
6931 static_cast<Func&&>(visitor)(static_cast<elem_ref>(elem));
6932 return true;
6933 }
6934 }
6935
6936 // visitor not compatible with this particular type
6937 else
6938 return true;
6939 });
6940
6941 if (!keep_going)
6942 return;
6943 #endif
6944 }
6945 }
6946
6947 public:
6948
6949 template <typename Func>
6950 array& for_each(Func&& visitor) & //
6951 noexcept(for_each_is_nothrow<Func&&, array&>::value)
6952 {
6953 do_for_each(static_cast<Func&&>(visitor), *this);
6954 return *this;
6955 }
6956
6957 template <typename Func>
6958 array&& for_each(Func&& visitor) && //
6959 noexcept(for_each_is_nothrow<Func&&, array&&>::value)
6960 {
6961 do_for_each(static_cast<Func&&>(visitor), static_cast<array&&>(*this));
6962 return static_cast<array&&>(*this);
6963 }
6964
6965 template <typename Func>
6966 const array& for_each(Func&& visitor) const& //
6967 noexcept(for_each_is_nothrow<Func&&, const array&>::value)
6968 {
6969 do_for_each(static_cast<Func&&>(visitor), *this);
6970 return *this;
6971 }
6972
6973 template <typename Func>
6974 const array&& for_each(Func&& visitor) const&& //
6975 noexcept(for_each_is_nothrow<Func&&, const array&&>::value)
6976 {
6977 do_for_each(static_cast<Func&&>(visitor), static_cast<const array&&>(*this));
6978 return static_cast<const array&&>(*this);
6979 }
6980
6981 TOML_NODISCARD
6982 bool empty() const noexcept
6983 {
6984 return elems_.empty();
6985 }
6986
6987 TOML_NODISCARD
6988 size_t size() const noexcept
6989 {
6990 return elems_.size();
6991 }
6992
6993 TOML_NODISCARD
6994 size_t max_size() const noexcept
6995 {
6996 return elems_.max_size();
6997 }
6998
6999 TOML_NODISCARD
7000 size_t capacity() const noexcept
7001 {
7002 return elems_.capacity();
7003 }
7004
7005 TOML_EXPORTED_MEMBER_FUNCTION
7006 void reserve(size_t new_capacity);
7007
7008 TOML_EXPORTED_MEMBER_FUNCTION
7009 void shrink_to_fit();
7010
7011 TOML_EXPORTED_MEMBER_FUNCTION
7012 void truncate(size_t new_size);
7013
7014 template <typename ElemType>
7015 void resize(size_t new_size,
7016 ElemType&& default_init_val,
7017 value_flags default_init_flags = preserve_source_value_flags)
7018 {
7019 static_assert(!is_node_view<ElemType>,
7020 "The default element type argument to toml::array::resize may not be toml::node_view.");
7021
7022 if (!new_size)
7023 clear();
7024 else if (new_size > elems_.size())
7025 insert(cend(), new_size - elems_.size(), static_cast<ElemType&&>(default_init_val), default_init_flags);
7026 else
7027 truncate(new_size);
7028 }
7029
7030 TOML_EXPORTED_MEMBER_FUNCTION
7031 iterator erase(const_iterator pos) noexcept;
7032
7033 TOML_EXPORTED_MEMBER_FUNCTION
7034 iterator erase(const_iterator first, const_iterator last) noexcept;
7035
7036 TOML_EXPORTED_MEMBER_FUNCTION
7037 array& flatten() &;
7038
7039 array&& flatten() &&
7040 {
7041 return static_cast<toml::array&&>(this->flatten());
7042 }
7043
7044 TOML_EXPORTED_MEMBER_FUNCTION
7045 array& prune(bool recursive = true) & noexcept;
7046
7047 array&& prune(bool recursive = true) && noexcept
7048 {
7049 return static_cast<toml::array&&>(this->prune(recursive));
7050 }
7051
7052 TOML_EXPORTED_MEMBER_FUNCTION
7053 void pop_back() noexcept;
7054
7055 TOML_EXPORTED_MEMBER_FUNCTION
7056 void clear() noexcept;
7057
7058 template <typename ElemType>
7059 iterator insert(const_iterator pos, ElemType&& val, value_flags flags = preserve_source_value_flags)
7060 {
7061 if constexpr (is_node_view<ElemType>)
7062 {
7063 if (!val)
7064 return end();
7065 }
7066 return iterator{ insert_at(const_vector_iterator{ pos },
7067 impl::make_node(static_cast<ElemType&&>(val), flags)) };
7068 }
7069
7070 template <typename ElemType>
7071 iterator insert(const_iterator pos,
7072 size_t count,
7073 ElemType&& val,
7074 value_flags flags = preserve_source_value_flags)
7075 {
7076 if constexpr (is_node_view<ElemType>)
7077 {
7078 if (!val)
7079 return end();
7080 }
7081 switch (count)
7082 {
7083 case 0: return iterator{ elems_.begin() + (const_vector_iterator{ pos } - elems_.cbegin()) };
7084 case 1: return insert(pos, static_cast<ElemType&&>(val), flags);
7085 default:
7086 {
7087 const auto start_idx = static_cast<size_t>(const_vector_iterator{ pos } - elems_.cbegin());
7088 preinsertion_resize(start_idx, count);
7089 size_t i = start_idx;
7090 for (size_t e = start_idx + count - 1u; i < e; i++)
7091 elems_[i] = impl::make_node(val, flags);
7092
7093 elems_[i] = impl::make_node(static_cast<ElemType&&>(val), flags);
7094 return iterator{ elems_.begin() + static_cast<ptrdiff_t>(start_idx) };
7095 }
7096 }
7097 }
7098
7099 template <typename Iter>
7100 iterator insert(const_iterator pos, Iter first, Iter last, value_flags flags = preserve_source_value_flags)
7101 {
7102 const auto distance = std::distance(first, last);
7103 if (distance <= 0)
7104 return iterator{ elems_.begin() + (const_vector_iterator{ pos } - elems_.cbegin()) };
7105 else
7106 {
7107 auto count = distance;
7108 using deref_type = decltype(*first);
7109 if constexpr (is_node_view<deref_type>)
7110 {
7111 for (auto it = first; it != last; it++)
7112 if (!(*it))
7113 count--;
7114 if (!count)
7115 return iterator{ elems_.begin() + (const_vector_iterator{ pos } - elems_.cbegin()) };
7116 }
7117 const auto start_idx = static_cast<size_t>(const_vector_iterator{ pos } - elems_.cbegin());
7118 preinsertion_resize(start_idx, static_cast<size_t>(count));
7119 size_t i = start_idx;
7120 for (auto it = first; it != last; it++)
7121 {
7122 if constexpr (is_node_view<deref_type>)
7123 {
7124 if (!(*it))
7125 continue;
7126 }
7127 if constexpr (std::is_rvalue_reference_v<deref_type>)
7128 elems_[i++] = impl::make_node(std::move(*it), flags);
7129 else
7130 elems_[i++] = impl::make_node(*it, flags);
7131 }
7132 return iterator{ elems_.begin() + static_cast<ptrdiff_t>(start_idx) };
7133 }
7134 }
7135
7136 template <typename ElemType>
7137 iterator insert(const_iterator pos,
7138 std::initializer_list<ElemType> ilist,
7139 value_flags flags = preserve_source_value_flags)
7140 {
7141 return insert(pos, ilist.begin(), ilist.end(), flags);
7142 }
7143
7144 template <typename ElemType = void, typename... Args>
7145 iterator emplace(const_iterator pos, Args&&... args)
7146 {
7147 using raw_elem_type = impl::remove_cvref<ElemType>;
7148 using elem_type = std::conditional_t<std::is_void_v<raw_elem_type>, //
7149 impl::emplaced_type_of<Args&&...>,
7150 raw_elem_type>;
7151
7152 using type = impl::remove_cvref<impl::unwrap_node<elem_type>>;
7153 static_assert(impl::is_native<type> || impl::is_one_of<type, table, array>,
7154 "Emplacement type parameter must be one of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
7155
7156 return iterator{ insert_at(const_vector_iterator{ pos },
7157 impl::node_ptr{ new impl::wrap_node<type>{ static_cast<Args&&>(args)... } }) };
7158 }
7159
7160 template <typename ElemType>
7161 iterator replace(const_iterator pos, ElemType&& val, value_flags flags = preserve_source_value_flags)
7162 {
7163 TOML_ASSERT(pos >= cbegin() && pos < cend());
7164
7165 if constexpr (is_node_view<ElemType>)
7166 {
7167 if (!val)
7168 return end();
7169 }
7170
7171 const auto it = elems_.begin() + (const_vector_iterator{ pos } - elems_.cbegin());
7172 *it = impl::make_node(static_cast<ElemType&&>(val), flags);
7173 return iterator{ it };
7174 }
7175
7176 template <typename ElemType>
7177 void push_back(ElemType&& val, value_flags flags = preserve_source_value_flags)
7178 {
7179 emplace_back_if_not_empty_view(static_cast<ElemType&&>(val), flags);
7180 }
7181
7182 template <typename ElemType = void, typename... Args>
7183 decltype(auto) emplace_back(Args&&... args)
7184 {
7185 using raw_elem_type = impl::remove_cvref<ElemType>;
7186 using elem_type = std::conditional_t<std::is_void_v<raw_elem_type>, //
7187 impl::emplaced_type_of<Args&&...>,
7188 raw_elem_type>;
7189
7190 static constexpr auto moving_node_ptr = std::is_same_v<elem_type, impl::node_ptr> //
7191 && sizeof...(Args) == 1u //
7192 && impl::first_is_same<impl::node_ptr&&, Args&&...>;
7193
7194 using unwrapped_type = impl::remove_cvref<impl::unwrap_node<elem_type>>;
7195
7196 static_assert(
7197 moving_node_ptr //
7198 || impl::is_native<unwrapped_type> //
7199 || impl::is_one_of<unwrapped_type, table, array>, //
7200 "ElemType argument of array::emplace_back() must be one of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
7201
7202 if constexpr (moving_node_ptr)
7203 {
7204 insert_at_back(static_cast<Args&&>(args)...);
7205 return *elems_.back();
7206 }
7207 else
7208 {
7209 auto ptr = new impl::wrap_node<unwrapped_type>{ static_cast<Args&&>(args)... };
7210 insert_at_back(impl::node_ptr{ ptr });
7211 return *ptr;
7212 }
7213 }
7214
7215 private:
7216
7217 TOML_NODISCARD
7218 TOML_EXPORTED_STATIC_FUNCTION
7219 static bool TOML_CALLCONV equal(const array&, const array&) noexcept;
7220
7221 template <typename T>
7222 TOML_NODISCARD
7223 static bool equal_to_container(const array& lhs, const T& rhs) noexcept
7224 {
7225 using element_type = std::remove_const_t<typename T::value_type>;
7226 static_assert(impl::is_losslessly_convertible_to_native<element_type>,
7227 "Container element type must be losslessly convertible one of the native TOML value types");
7228
7229 if (lhs.size() != rhs.size())
7230 return false;
7231 if (rhs.size() == 0u)
7232 return true;
7233
7234 size_t i{};
7235 for (auto& list_elem : rhs)
7236 {
7237 const auto elem = lhs.get_as<impl::native_type_of<element_type>>(i++);
7238 if (!elem || *elem != list_elem)
7239 return false;
7240 }
7241
7242 return true;
7243 }
7244
7245 public:
7246
7247 TOML_NODISCARD
7248 friend bool operator==(const array& lhs, const array& rhs) noexcept
7249 {
7250 return equal(lhs, rhs);
7251 }
7252
7253 TOML_NODISCARD
7254 friend bool operator!=(const array& lhs, const array& rhs) noexcept
7255 {
7256 return !equal(lhs, rhs);
7257 }
7258
7259 template <typename T>
7260 TOML_NODISCARD
7261 friend bool operator==(const array& lhs, const std::initializer_list<T>& rhs) noexcept
7262 {
7263 return equal_to_container(lhs, rhs);
7264 }
7265 TOML_ASYMMETRICAL_EQUALITY_OPS(const array&, const std::initializer_list<T>&, template <typename T>);
7266
7267 template <typename T>
7268 TOML_NODISCARD
7269 friend bool operator==(const array& lhs, const std::vector<T>& rhs) noexcept
7270 {
7271 return equal_to_container(lhs, rhs);
7272 }
7273 TOML_ASYMMETRICAL_EQUALITY_OPS(const array&, const std::vector<T>&, template <typename T>);
7274
7275 #if TOML_ENABLE_FORMATTERS
7276
7277 friend std::ostream& operator<<(std::ostream& lhs, const array& rhs)
7278 {
7279 impl::print_to_stream(lhs, rhs);
7280 return lhs;
7281 }
7282
7283 #endif
7284 };
7285 }
7286 TOML_NAMESPACE_END;
7287
7288 #ifdef _MSC_VER
7289 #pragma pop_macro("min")
7290 #pragma pop_macro("max")
7291 #ifndef __clang__
7292 #pragma inline_recursion(off)
7293 #endif
7294 #endif
7295 TOML_POP_WARNINGS;
7296
7297 //******** impl/key.hpp **********************************************************************************************
7298
7299 TOML_PUSH_WARNINGS;
7300 #ifdef _MSC_VER
7301 #ifndef __clang__
7302 #pragma inline_recursion(on)
7303 #endif
7304 #pragma push_macro("min")
7305 #pragma push_macro("max")
7306 #undef min
7307 #undef max
7308 #endif
7309
7310 TOML_NAMESPACE_START
7311 {
7312 class key
7313 {
7314 private:
7315 std::string key_;
7316 source_region source_;
7317
7318 public:
7319
7320 TOML_NODISCARD_CTOR
7321 key() noexcept = default;
7322
7323 TOML_NODISCARD_CTOR
7324 explicit key(std::string_view k, source_region&& src = {}) //
7325 : key_{ k },
7326 source_{ std::move(src) }
7327 {}
7328
7329 TOML_NODISCARD_CTOR
7330 explicit key(std::string_view k, const source_region& src) //
7331 : key_{ k },
7332 source_{ src }
7333 {}
7334
7335 TOML_NODISCARD_CTOR
7336 explicit key(std::string&& k, source_region&& src = {}) noexcept //
7337 : key_{ std::move(k) },
7338 source_{ std::move(src) }
7339 {}
7340
7341 TOML_NODISCARD_CTOR
7342 explicit key(std::string&& k, const source_region& src) noexcept //
7343 : key_{ std::move(k) },
7344 source_{ src }
7345 {}
7346
7347 TOML_NODISCARD_CTOR
7348 explicit key(const char* k, source_region&& src = {}) //
7349 : key_{ k },
7350 source_{ std::move(src) }
7351 {}
7352
7353 TOML_NODISCARD_CTOR
7354 explicit key(const char* k, const source_region& src) //
7355 : key_{ k },
7356 source_{ src }
7357 {}
7358
7359 #if TOML_ENABLE_WINDOWS_COMPAT
7360
7361 TOML_NODISCARD_CTOR
7362 explicit key(std::wstring_view k, source_region&& src = {}) //
7363 : key_{ impl::narrow(k) },
7364 source_{ std::move(src) }
7365 {}
7366
7367 TOML_NODISCARD_CTOR
7368 explicit key(std::wstring_view k, const source_region& src) //
7369 : key_{ impl::narrow(k) },
7370 source_{ src }
7371 {}
7372
7373 #endif
7374
7375 TOML_PURE_INLINE_GETTER
7376 std::string_view str() const noexcept
7377 {
7378 return std::string_view{ key_ };
7379 }
7380
7381 TOML_PURE_INLINE_GETTER
7382 /*implicit*/ operator std::string_view() const noexcept
7383 {
7384 return str();
7385 }
7386
7387 TOML_PURE_INLINE_GETTER
7388 bool empty() const noexcept
7389 {
7390 return key_.empty();
7391 }
7392
7393 TOML_PURE_INLINE_GETTER
7394 const char* data() const noexcept
7395 {
7396 return key_.data();
7397 }
7398
7399 TOML_PURE_INLINE_GETTER
7400 size_t length() const noexcept
7401 {
7402 return key_.length();
7403 }
7404
7405 TOML_PURE_INLINE_GETTER
7406 const source_region& source() const noexcept
7407 {
7408 return source_;
7409 }
7410
7411 TOML_PURE_INLINE_GETTER
7412 friend bool operator==(const key& lhs, const key& rhs) noexcept
7413 {
7414 return lhs.key_ == rhs.key_;
7415 }
7416
7417 TOML_PURE_INLINE_GETTER
7418 friend bool operator!=(const key& lhs, const key& rhs) noexcept
7419 {
7420 return lhs.key_ != rhs.key_;
7421 }
7422
7423 TOML_PURE_INLINE_GETTER
7424 friend bool operator<(const key& lhs, const key& rhs) noexcept
7425 {
7426 return lhs.key_ < rhs.key_;
7427 }
7428
7429 TOML_PURE_INLINE_GETTER
7430 friend bool operator<=(const key& lhs, const key& rhs) noexcept
7431 {
7432 return lhs.key_ <= rhs.key_;
7433 }
7434
7435 TOML_PURE_INLINE_GETTER
7436 friend bool operator>(const key& lhs, const key& rhs) noexcept
7437 {
7438 return lhs.key_ > rhs.key_;
7439 }
7440
7441 TOML_PURE_INLINE_GETTER
7442 friend bool operator>=(const key& lhs, const key& rhs) noexcept
7443 {
7444 return lhs.key_ >= rhs.key_;
7445 }
7446
7447 TOML_PURE_INLINE_GETTER
7448 friend bool operator==(const key& lhs, std::string_view rhs) noexcept
7449 {
7450 return lhs.key_ == rhs;
7451 }
7452
7453 TOML_PURE_INLINE_GETTER
7454 friend bool operator!=(const key& lhs, std::string_view rhs) noexcept
7455 {
7456 return lhs.key_ != rhs;
7457 }
7458
7459 TOML_PURE_INLINE_GETTER
7460 friend bool operator<(const key& lhs, std::string_view rhs) noexcept
7461 {
7462 return lhs.key_ < rhs;
7463 }
7464
7465 TOML_PURE_INLINE_GETTER
7466 friend bool operator<=(const key& lhs, std::string_view rhs) noexcept
7467 {
7468 return lhs.key_ <= rhs;
7469 }
7470
7471 TOML_PURE_INLINE_GETTER
7472 friend bool operator>(const key& lhs, std::string_view rhs) noexcept
7473 {
7474 return lhs.key_ > rhs;
7475 }
7476
7477 TOML_PURE_INLINE_GETTER
7478 friend bool operator>=(const key& lhs, std::string_view rhs) noexcept
7479 {
7480 return lhs.key_ >= rhs;
7481 }
7482
7483 TOML_PURE_INLINE_GETTER
7484 friend bool operator==(std::string_view lhs, const key& rhs) noexcept
7485 {
7486 return lhs == rhs.key_;
7487 }
7488
7489 TOML_PURE_INLINE_GETTER
7490 friend bool operator!=(std::string_view lhs, const key& rhs) noexcept
7491 {
7492 return lhs != rhs.key_;
7493 }
7494
7495 TOML_PURE_INLINE_GETTER
7496 friend bool operator<(std::string_view lhs, const key& rhs) noexcept
7497 {
7498 return lhs < rhs.key_;
7499 }
7500
7501 TOML_PURE_INLINE_GETTER
7502 friend bool operator<=(std::string_view lhs, const key& rhs) noexcept
7503 {
7504 return lhs <= rhs.key_;
7505 }
7506
7507 TOML_PURE_INLINE_GETTER
7508 friend bool operator>(std::string_view lhs, const key& rhs) noexcept
7509 {
7510 return lhs > rhs.key_;
7511 }
7512
7513 TOML_PURE_INLINE_GETTER
7514 friend bool operator>=(std::string_view lhs, const key& rhs) noexcept
7515 {
7516 return lhs >= rhs.key_;
7517 }
7518
7519 using const_iterator = const char*;
7520
7521 using iterator = const_iterator;
7522
7523 TOML_PURE_INLINE_GETTER
7524 const_iterator begin() const noexcept
7525 {
7526 return key_.data();
7527 }
7528
7529 TOML_PURE_INLINE_GETTER
7530 const_iterator end() const noexcept
7531 {
7532 return key_.data() + key_.length();
7533 }
7534
7535 friend std::ostream& operator<<(std::ostream& lhs, const key& rhs)
7536 {
7537 impl::print_to_stream(lhs, rhs.key_);
7538 return lhs;
7539 }
7540 };
7541
7542 template <typename T>
7543 inline constexpr bool is_key = std::is_same_v<impl::remove_cvref<T>, toml::key>;
7544
7545 template <typename T>
7546 inline constexpr bool is_key_or_convertible = is_key<T> //
7547 || impl::is_constructible_or_convertible<toml::key, T>;
7548 }
7549 TOML_NAMESPACE_END;
7550
7551 #ifdef _MSC_VER
7552 #pragma pop_macro("min")
7553 #pragma pop_macro("max")
7554 #ifndef __clang__
7555 #pragma inline_recursion(off)
7556 #endif
7557 #endif
7558 TOML_POP_WARNINGS;
7559
7560 //******** impl/std_map.hpp ******************************************************************************************
7561
7562 TOML_DISABLE_WARNINGS;
7563 #include <map>
7564 #include <iterator>
7565 TOML_ENABLE_WARNINGS;
7566
7567 //******** impl/table.hpp ********************************************************************************************
7568
7569 TOML_PUSH_WARNINGS;
7570 #ifdef _MSC_VER
7571 #ifndef __clang__
7572 #pragma inline_recursion(on)
7573 #endif
7574 #pragma push_macro("min")
7575 #pragma push_macro("max")
7576 #undef min
7577 #undef max
7578 #endif
7579
7580 TOML_IMPL_NAMESPACE_START
7581 {
7582 template <bool IsConst>
7583 struct table_proxy_pair
7584 {
7585 using value_type = std::conditional_t<IsConst, const node, node>;
7586
7587 const toml::key& first;
7588 value_type& second;
7589 };
7590
7591 template <bool IsConst>
7592 class table_iterator
7593 {
7594 private:
7595 template <bool>
7596 friend class table_iterator;
7597
7598 using proxy_type = table_proxy_pair<IsConst>;
7599 using mutable_map_iterator = std::map<toml::key, node_ptr, std::less<>>::iterator;
7600 using const_map_iterator = std::map<toml::key, node_ptr, std::less<>>::const_iterator;
7601 using map_iterator = std::conditional_t<IsConst, const_map_iterator, mutable_map_iterator>;
7602
7603 mutable map_iterator iter_;
7604 alignas(proxy_type) mutable unsigned char proxy_[sizeof(proxy_type)];
7605 mutable bool proxy_instantiated_ = false;
7606
7607 TOML_NODISCARD
7608 proxy_type* get_proxy() const noexcept
7609 {
7610 if (!proxy_instantiated_)
7611 {
7612 auto p = ::new (static_cast<void*>(proxy_)) proxy_type{ iter_->first, *iter_->second.get() };
7613 proxy_instantiated_ = true;
7614 return p;
7615 }
7616 else
7617 return TOML_LAUNDER(reinterpret_cast<proxy_type*>(proxy_));
7618 }
7619
7620 public:
7621 TOML_NODISCARD_CTOR
7622 table_iterator() noexcept = default;
7623
7624 TOML_NODISCARD_CTOR
7625 explicit table_iterator(mutable_map_iterator iter) noexcept //
7626 : iter_{ iter }
7627 {}
7628
7629 TOML_CONSTRAINED_TEMPLATE(C, bool C = IsConst)
7630 TOML_NODISCARD_CTOR
7631 explicit table_iterator(const_map_iterator iter) noexcept //
7632 : iter_{ iter }
7633 {}
7634
7635 TOML_CONSTRAINED_TEMPLATE(C, bool C = IsConst)
7636 TOML_NODISCARD_CTOR
7637 table_iterator(const table_iterator<false>& other) noexcept //
7638 : iter_{ other.iter_ }
7639 {}
7640
7641 TOML_NODISCARD_CTOR
7642 table_iterator(const table_iterator& other) noexcept //
7643 : iter_{ other.iter_ }
7644 {}
7645
7646 table_iterator& operator=(const table_iterator& rhs) noexcept
7647 {
7648 iter_ = rhs.iter_;
7649 proxy_instantiated_ = false;
7650 return *this;
7651 }
7652
7653 using value_type = table_proxy_pair<IsConst>;
7654 using reference = value_type&;
7655 using pointer = value_type*;
7656 using difference_type = typename std::iterator_traits<map_iterator>::difference_type;
7657 using iterator_category = typename std::iterator_traits<map_iterator>::iterator_category;
7658
7659 table_iterator& operator++() noexcept // ++pre
7660 {
7661 ++iter_;
7662 proxy_instantiated_ = false;
7663 return *this;
7664 }
7665
7666 table_iterator operator++(int) noexcept // post++
7667 {
7668 table_iterator out{ iter_ };
7669 ++iter_;
7670 proxy_instantiated_ = false;
7671 return out;
7672 }
7673
7674 table_iterator& operator--() noexcept // --pre
7675 {
7676 --iter_;
7677 proxy_instantiated_ = false;
7678 return *this;
7679 }
7680
7681 table_iterator operator--(int) noexcept // post--
7682 {
7683 table_iterator out{ iter_ };
7684 --iter_;
7685 proxy_instantiated_ = false;
7686 return out;
7687 }
7688
7689 TOML_PURE_INLINE_GETTER
7690 reference operator*() const noexcept
7691 {
7692 return *get_proxy();
7693 }
7694
7695 TOML_PURE_INLINE_GETTER
7696 pointer operator->() const noexcept
7697 {
7698 return get_proxy();
7699 }
7700
7701 TOML_PURE_INLINE_GETTER
7702 explicit operator const map_iterator&() const noexcept
7703 {
7704 return iter_;
7705 }
7706
7707 TOML_CONSTRAINED_TEMPLATE(!C, bool C = IsConst)
7708 TOML_PURE_INLINE_GETTER
7709 explicit operator const const_map_iterator() const noexcept
7710 {
7711 return iter_;
7712 }
7713
7714 TOML_PURE_INLINE_GETTER
7715 friend bool operator==(const table_iterator& lhs, const table_iterator& rhs) noexcept
7716 {
7717 return lhs.iter_ == rhs.iter_;
7718 }
7719
7720 TOML_PURE_INLINE_GETTER
7721 friend bool operator!=(const table_iterator& lhs, const table_iterator& rhs) noexcept
7722 {
7723 return lhs.iter_ != rhs.iter_;
7724 }
7725 };
7726
7727 struct table_init_pair
7728 {
7729 mutable toml::key key;
7730 mutable node_ptr value;
7731
7732 template <typename K, typename V>
7733 TOML_NODISCARD_CTOR
7734 table_init_pair(K&& k, V&& v, value_flags flags = preserve_source_value_flags) //
7735 : key{ static_cast<K&&>(k) },
7736 value{ make_node(static_cast<V&&>(v), flags) }
7737 {}
7738 };
7739 }
7740 TOML_IMPL_NAMESPACE_END;
7741
7742 TOML_NAMESPACE_START
7743 {
7744 using table_iterator = POXY_IMPLEMENTATION_DETAIL(impl::table_iterator<false>);
7745
7746 using const_table_iterator = POXY_IMPLEMENTATION_DETAIL(impl::table_iterator<true>);
7747
7748 class TOML_EXPORTED_CLASS table : public node
7749 {
7750 private:
7751
7752 using map_type = std::map<toml::key, impl::node_ptr, std::less<>>;
7753 using map_pair = std::pair<const toml::key, impl::node_ptr>;
7754 using map_iterator = typename map_type::iterator;
7755 using const_map_iterator = typename map_type::const_iterator;
7756 map_type map_;
7757
7758 bool inline_ = false;
7759
7760 TOML_NODISCARD_CTOR
7761 TOML_EXPORTED_MEMBER_FUNCTION
7762 table(const impl::table_init_pair*, const impl::table_init_pair*);
7763
7764 public:
7765
7766 TOML_NODISCARD_CTOR
7767 TOML_EXPORTED_MEMBER_FUNCTION
7768 table() noexcept;
7769
7770 TOML_EXPORTED_MEMBER_FUNCTION
7771 ~table() noexcept;
7772
7773 TOML_NODISCARD_CTOR
7774 TOML_EXPORTED_MEMBER_FUNCTION
7775 table(const table&);
7776
7777 TOML_NODISCARD_CTOR
7778 TOML_EXPORTED_MEMBER_FUNCTION
7779 table(table&& other) noexcept;
7780
7781 TOML_NODISCARD_CTOR
7782 TOML_EXPORTED_MEMBER_FUNCTION
7783 explicit table(std::initializer_list<impl::table_init_pair> kvps) //
7784 : table(kvps.begin(), kvps.end())
7785 {}
7786
7787 TOML_EXPORTED_MEMBER_FUNCTION
7788 table& operator=(const table&);
7789
7790 TOML_EXPORTED_MEMBER_FUNCTION
7791 table& operator=(table&& rhs) noexcept;
7792
7793 TOML_CONST_INLINE_GETTER
7794 node_type type() const noexcept final
7795 {
7796 return node_type::table;
7797 }
7798
7799 TOML_PURE_GETTER
7800 TOML_EXPORTED_MEMBER_FUNCTION
7801 bool is_homogeneous(node_type ntype) const noexcept final;
7802
7803 TOML_NODISCARD
7804 TOML_EXPORTED_MEMBER_FUNCTION
7805 bool is_homogeneous(node_type ntype, node*& first_nonmatch) noexcept final;
7806
7807 TOML_NODISCARD
7808 TOML_EXPORTED_MEMBER_FUNCTION
7809 bool is_homogeneous(node_type ntype, const node*& first_nonmatch) const noexcept final;
7810
7811 template <typename ElemType = void>
7812 TOML_PURE_GETTER
7813 bool is_homogeneous() const noexcept
7814 {
7815 using type = impl::remove_cvref<impl::unwrap_node<ElemType>>;
7816 static_assert(std::is_void_v<type> || toml::is_value<type> || toml::is_container<type>,
7817 "The template type argument of table::is_homogeneous() must be void or one "
7818 "of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
7819
7820 return is_homogeneous(impl::node_type_of<type>);
7821 }
7822 TOML_CONST_INLINE_GETTER
7823 bool is_table() const noexcept final
7824 {
7825 return true;
7826 }
7827
7828 TOML_CONST_INLINE_GETTER
7829 bool is_array() const noexcept final
7830 {
7831 return false;
7832 }
7833
7834 TOML_PURE_GETTER
7835 bool is_array_of_tables() const noexcept final
7836 {
7837 return false;
7838 }
7839
7840 TOML_CONST_INLINE_GETTER
7841 bool is_value() const noexcept final
7842 {
7843 return false;
7844 }
7845
7846 TOML_CONST_INLINE_GETTER
7847 bool is_string() const noexcept final
7848 {
7849 return false;
7850 }
7851
7852 TOML_CONST_INLINE_GETTER
7853 bool is_integer() const noexcept final
7854 {
7855 return false;
7856 }
7857
7858 TOML_CONST_INLINE_GETTER
7859 bool is_floating_point() const noexcept final
7860 {
7861 return false;
7862 }
7863
7864 TOML_CONST_INLINE_GETTER
7865 bool is_number() const noexcept final
7866 {
7867 return false;
7868 }
7869
7870 TOML_CONST_INLINE_GETTER
7871 bool is_boolean() const noexcept final
7872 {
7873 return false;
7874 }
7875
7876 TOML_CONST_INLINE_GETTER
7877 bool is_date() const noexcept final
7878 {
7879 return false;
7880 }
7881
7882 TOML_CONST_INLINE_GETTER
7883 bool is_time() const noexcept final
7884 {
7885 return false;
7886 }
7887
7888 TOML_CONST_INLINE_GETTER
7889 bool is_date_time() const noexcept final
7890 {
7891 return false;
7892 }
7893
7894 TOML_CONST_INLINE_GETTER
7895 table* as_table() noexcept final
7896 {
7897 return this;
7898 }
7899
7900 TOML_CONST_INLINE_GETTER
7901 array* as_array() noexcept final
7902 {
7903 return nullptr;
7904 }
7905
7906 TOML_CONST_INLINE_GETTER
7907 toml::value<std::string>* as_string() noexcept final
7908 {
7909 return nullptr;
7910 }
7911
7912 TOML_CONST_INLINE_GETTER
7913 toml::value<int64_t>* as_integer() noexcept final
7914 {
7915 return nullptr;
7916 }
7917
7918 TOML_CONST_INLINE_GETTER
7919 toml::value<double>* as_floating_point() noexcept final
7920 {
7921 return nullptr;
7922 }
7923
7924 TOML_CONST_INLINE_GETTER
7925 toml::value<bool>* as_boolean() noexcept final
7926 {
7927 return nullptr;
7928 }
7929
7930 TOML_CONST_INLINE_GETTER
7931 toml::value<date>* as_date() noexcept final
7932 {
7933 return nullptr;
7934 }
7935
7936 TOML_CONST_INLINE_GETTER
7937 toml::value<time>* as_time() noexcept final
7938 {
7939 return nullptr;
7940 }
7941
7942 TOML_CONST_INLINE_GETTER
7943 toml::value<date_time>* as_date_time() noexcept final
7944 {
7945 return nullptr;
7946 }
7947
7948 TOML_CONST_INLINE_GETTER
7949 const table* as_table() const noexcept final
7950 {
7951 return this;
7952 }
7953
7954 TOML_CONST_INLINE_GETTER
7955 const array* as_array() const noexcept final
7956 {
7957 return nullptr;
7958 }
7959
7960 TOML_CONST_INLINE_GETTER
7961 const toml::value<std::string>* as_string() const noexcept final
7962 {
7963 return nullptr;
7964 }
7965
7966 TOML_CONST_INLINE_GETTER
7967 const toml::value<int64_t>* as_integer() const noexcept final
7968 {
7969 return nullptr;
7970 }
7971
7972 TOML_CONST_INLINE_GETTER
7973 const toml::value<double>* as_floating_point() const noexcept final
7974 {
7975 return nullptr;
7976 }
7977
7978 TOML_CONST_INLINE_GETTER
7979 const toml::value<bool>* as_boolean() const noexcept final
7980 {
7981 return nullptr;
7982 }
7983
7984 TOML_CONST_INLINE_GETTER
7985 const toml::value<date>* as_date() const noexcept final
7986 {
7987 return nullptr;
7988 }
7989
7990 TOML_CONST_INLINE_GETTER
7991 const toml::value<time>* as_time() const noexcept final
7992 {
7993 return nullptr;
7994 }
7995
7996 TOML_CONST_INLINE_GETTER
7997 const toml::value<date_time>* as_date_time() const noexcept final
7998 {
7999 return nullptr;
8000 }
8001
8002 TOML_PURE_INLINE_GETTER
8003 bool is_inline() const noexcept
8004 {
8005 return inline_;
8006 }
8007
8008 void is_inline(bool val) noexcept
8009 {
8010 inline_ = val;
8011 }
8012
8013 TOML_PURE_GETTER
8014 TOML_EXPORTED_MEMBER_FUNCTION
8015 node* get(std::string_view key) noexcept;
8016
8017 TOML_PURE_INLINE_GETTER
8018 const node* get(std::string_view key) const noexcept
8019 {
8020 return const_cast<table&>(*this).get(key);
8021 }
8022
8023 #if TOML_ENABLE_WINDOWS_COMPAT
8024
8025 TOML_NODISCARD
8026 node* get(std::wstring_view key)
8027 {
8028 if (empty())
8029 return nullptr;
8030
8031 return get(impl::narrow(key));
8032 }
8033
8034 TOML_NODISCARD
8035 const node* get(std::wstring_view key) const
8036 {
8037 return const_cast<table&>(*this).get(key);
8038 }
8039
8040 #endif // TOML_ENABLE_WINDOWS_COMPAT
8041
8042 template <typename T>
8043 TOML_PURE_GETTER
8044 impl::wrap_node<T>* get_as(std::string_view key) noexcept
8045 {
8046 const auto n = this->get(key);
8047 return n ? n->template as<T>() : nullptr;
8048 }
8049
8050 template <typename T>
8051 TOML_PURE_GETTER
8052 const impl::wrap_node<T>* get_as(std::string_view key) const noexcept
8053 {
8054 return const_cast<table&>(*this).template get_as<T>(key);
8055 }
8056
8057 #if TOML_ENABLE_WINDOWS_COMPAT
8058
8059 template <typename T>
8060 TOML_NODISCARD
8061 impl::wrap_node<T>* get_as(std::wstring_view key)
8062 {
8063 if (empty())
8064 return nullptr;
8065
8066 return get_as<T>(impl::narrow(key));
8067 }
8068
8069 template <typename T>
8070 TOML_NODISCARD
8071 const impl::wrap_node<T>* get_as(std::wstring_view key) const
8072 {
8073 return const_cast<table&>(*this).template get_as<T>(key);
8074 }
8075
8076 #endif // TOML_ENABLE_WINDOWS_COMPAT
8077
8078 TOML_NODISCARD
8079 TOML_EXPORTED_MEMBER_FUNCTION
8080 node& at(std::string_view key);
8081
8082 TOML_NODISCARD
8083 const node& at(std::string_view key) const
8084 {
8085 return const_cast<table&>(*this).at(key);
8086 }
8087
8088 #if TOML_ENABLE_WINDOWS_COMPAT
8089
8090 TOML_NODISCARD
8091 node& at(std::wstring_view key)
8092 {
8093 return at(impl::narrow(key));
8094 }
8095
8096 TOML_NODISCARD
8097 const node& at(std::wstring_view key) const
8098 {
8099 return const_cast<table&>(*this).at(key);
8100 }
8101
8102 #endif // TOML_ENABLE_WINDOWS_COMPAT
8103
8104 using iterator = toml::table_iterator;
8105
8106 using const_iterator = toml::const_table_iterator;
8107
8108 TOML_PURE_INLINE_GETTER
8109 iterator begin() noexcept
8110 {
8111 return iterator{ map_.begin() };
8112 }
8113
8114 TOML_PURE_INLINE_GETTER
8115 const_iterator begin() const noexcept
8116 {
8117 return const_iterator{ map_.cbegin() };
8118 }
8119
8120 TOML_PURE_INLINE_GETTER
8121 const_iterator cbegin() const noexcept
8122 {
8123 return const_iterator{ map_.cbegin() };
8124 }
8125
8126 TOML_PURE_INLINE_GETTER
8127 iterator end() noexcept
8128 {
8129 return iterator{ map_.end() };
8130 }
8131
8132 TOML_PURE_INLINE_GETTER
8133 const_iterator end() const noexcept
8134 {
8135 return const_iterator{ map_.cend() };
8136 }
8137
8138 TOML_PURE_INLINE_GETTER
8139 const_iterator cend() const noexcept
8140 {
8141 return const_iterator{ map_.cend() };
8142 }
8143
8144 private:
8145
8146 template <typename T, typename Table>
8147 using for_each_value_ref = impl::copy_cvref<impl::wrap_node<impl::remove_cvref<impl::unwrap_node<T>>>, Table>;
8148
8149 template <typename Func, typename Table, typename T>
8150 using can_for_each = std::disjunction<std::is_invocable<Func, const key&, for_each_value_ref<T, Table>>, //
8151 std::is_invocable<Func, for_each_value_ref<T, Table>>>;
8152
8153 template <typename Func, typename Table, typename T>
8154 using can_for_each_nothrow = std::conditional_t<
8155 // first form
8156 std::is_invocable_v<Func, const key&, for_each_value_ref<T, Table>>,
8157 std::is_nothrow_invocable<Func, const key&, for_each_value_ref<T, Table>>,
8158 std::conditional_t<
8159 // second form
8160 std::is_invocable_v<Func, for_each_value_ref<T, Table>>,
8161 std::is_nothrow_invocable<Func, for_each_value_ref<T, Table>>,
8162 std::false_type>>;
8163
8164 template <typename Func, typename Table>
8165 using can_for_each_any = std::disjunction<can_for_each<Func, Table, table>,
8166 can_for_each<Func, Table, array>,
8167 can_for_each<Func, Table, std::string>,
8168 can_for_each<Func, Table, int64_t>,
8169 can_for_each<Func, Table, double>,
8170 can_for_each<Func, Table, bool>,
8171 can_for_each<Func, Table, date>,
8172 can_for_each<Func, Table, time>,
8173 can_for_each<Func, Table, date_time>>;
8174
8175 template <typename Func, typename Table, typename T>
8176 using for_each_is_nothrow_one = std::disjunction<std::negation<can_for_each<Func, Table, T>>, //
8177 can_for_each_nothrow<Func, Table, T>>;
8178
8179 template <typename Func, typename Table>
8180 using for_each_is_nothrow = std::conjunction<for_each_is_nothrow_one<Func, Table, table>,
8181 for_each_is_nothrow_one<Func, Table, array>,
8182 for_each_is_nothrow_one<Func, Table, std::string>,
8183 for_each_is_nothrow_one<Func, Table, int64_t>,
8184 for_each_is_nothrow_one<Func, Table, double>,
8185 for_each_is_nothrow_one<Func, Table, bool>,
8186 for_each_is_nothrow_one<Func, Table, date>,
8187 for_each_is_nothrow_one<Func, Table, time>,
8188 for_each_is_nothrow_one<Func, Table, date_time>>;
8189
8190 template <typename Func, typename Table>
8191 static void do_for_each(Func&& visitor, Table&& tbl) //
8192 noexcept(for_each_is_nothrow<Func&&, Table&&>::value)
8193 {
8194 static_assert(can_for_each_any<Func&&, Table&&>::value,
8195 "TOML table for_each visitors must be invocable for at least one of the toml::node "
8196 "specializations:" TOML_SA_NODE_TYPE_LIST);
8197
8198 using kvp_type = impl::copy_cv<map_pair, std::remove_reference_t<Table>>;
8199
8200 for (kvp_type& kvp : tbl.map_)
8201 {
8202 using node_ref = impl::copy_cvref<toml::node, Table&&>;
8203 static_assert(std::is_reference_v<node_ref>);
8204
8205 #if TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN
8206
8207 #ifndef TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN_ACKNOWLEDGED
8208 static_assert(impl::always_false<Func, Table, kvp_type, node_ref>, //
8209 TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN_MESSAGE);
8210 #endif
8211
8212 static_cast<node_ref>(*kvp.second)
8213 .visit(
8214 [&]([[maybe_unused]] auto&& v) //
8215 noexcept(for_each_is_nothrow_one<Func&&, Table&&, decltype(v)>::value)
8216 {
8217 using value_ref = for_each_value_ref<decltype(v), Table&&>;
8218 static_assert(std::is_reference_v<value_ref>);
8219
8220 // func(key, val)
8221 if constexpr (std::is_invocable_v<Func&&, const key&, value_ref>)
8222 {
8223 static_cast<Func&&>(visitor)(static_cast<const key&>(kvp.first),
8224 static_cast<value_ref>(v));
8225 }
8226
8227 // func(val)
8228 else if constexpr (std::is_invocable_v<Func&&, value_ref>)
8229 {
8230 static_cast<Func&&>(visitor)(static_cast<value_ref>(v));
8231 }
8232 });
8233
8234 #else
8235 const auto keep_going =
8236 static_cast<node_ref>(*kvp.second)
8237 .visit(
8238 [&]([[maybe_unused]] auto&& v) //
8239 // Define this macro as a workaround to compile errors caused by a bug in MSVC's "legacy lambda processor".
8240 #if !TOML_DISABLE_CONDITIONAL_NOEXCEPT_LAMBDA
8241 noexcept(for_each_is_nothrow_one<Func&&, Table&&, decltype(v)>::value)
8242 #endif
8243 {
8244 using value_ref = for_each_value_ref<decltype(v), Table&&>;
8245 static_assert(std::is_reference_v<value_ref>);
8246
8247 // func(key, val)
8248 if constexpr (std::is_invocable_v<Func&&, const key&, value_ref>)
8249 {
8250 using return_type = decltype(static_cast<Func&&>(
8251 visitor)(static_cast<const key&>(kvp.first), static_cast<value_ref>(v)));
8252
8253 if constexpr (impl::is_constructible_or_convertible<bool, return_type>)
8254 {
8255 return static_cast<bool>(static_cast<Func&&>(
8256 visitor)(static_cast<const key&>(kvp.first), static_cast<value_ref>(v)));
8257 }
8258 else
8259 {
8260 static_cast<Func&&>(visitor)(static_cast<const key&>(kvp.first),
8261 static_cast<value_ref>(v));
8262 return true;
8263 }
8264 }
8265
8266 // func(val)
8267 else if constexpr (std::is_invocable_v<Func&&, value_ref>)
8268 {
8269 using return_type =
8270 decltype(static_cast<Func&&>(visitor)(static_cast<value_ref>(v)));
8271
8272 if constexpr (impl::is_constructible_or_convertible<bool, return_type>)
8273 {
8274 return static_cast<bool>(
8275 static_cast<Func&&>(visitor)(static_cast<value_ref>(v)));
8276 }
8277 else
8278 {
8279 static_cast<Func&&>(visitor)(static_cast<value_ref>(v));
8280 return true;
8281 }
8282 }
8283
8284 // visitor not compatible with this particular type
8285 else
8286 return true;
8287 });
8288
8289 if (!keep_going)
8290 return;
8291 #endif
8292 }
8293 }
8294
8295 public:
8296
8297 template <typename Func>
8298 table& for_each(Func&& visitor) & //
8299 noexcept(for_each_is_nothrow<Func&&, table&>::value)
8300 {
8301 do_for_each(static_cast<Func&&>(visitor), *this);
8302 return *this;
8303 }
8304
8305 template <typename Func>
8306 table&& for_each(Func&& visitor) && //
8307 noexcept(for_each_is_nothrow<Func&&, table&&>::value)
8308 {
8309 do_for_each(static_cast<Func&&>(visitor), static_cast<table&&>(*this));
8310 return static_cast<table&&>(*this);
8311 }
8312
8313 template <typename Func>
8314 const table& for_each(Func&& visitor) const& //
8315 noexcept(for_each_is_nothrow<Func&&, const table&>::value)
8316 {
8317 do_for_each(static_cast<Func&&>(visitor), *this);
8318 return *this;
8319 }
8320
8321 template <typename Func>
8322 const table&& for_each(Func&& visitor) const&& //
8323 noexcept(for_each_is_nothrow<Func&&, const table&&>::value)
8324 {
8325 do_for_each(static_cast<Func&&>(visitor), static_cast<const table&&>(*this));
8326 return static_cast<const table&&>(*this);
8327 }
8328
8329 TOML_PURE_INLINE_GETTER
8330 bool empty() const noexcept
8331 {
8332 return map_.empty();
8333 }
8334
8335 TOML_PURE_INLINE_GETTER
8336 size_t size() const noexcept
8337 {
8338 return map_.size();
8339 }
8340
8341 private:
8342
8343 TOML_PURE_GETTER
8344 TOML_EXPORTED_MEMBER_FUNCTION
8345 map_iterator get_lower_bound(std::string_view) noexcept;
8346
8347 public:
8348
8349 TOML_PURE_GETTER
8350 iterator lower_bound(std::string_view key) noexcept
8351 {
8352 return iterator{ get_lower_bound(key) };
8353 }
8354
8355 TOML_PURE_GETTER
8356 const_iterator lower_bound(std::string_view key) const noexcept
8357 {
8358 return const_iterator{ const_cast<table&>(*this).get_lower_bound(key) };
8359 }
8360
8361 #if TOML_ENABLE_WINDOWS_COMPAT
8362
8363 TOML_NODISCARD
8364 iterator lower_bound(std::wstring_view key)
8365 {
8366 if (empty())
8367 return end();
8368
8369 return lower_bound(impl::narrow(key));
8370 }
8371
8372 TOML_NODISCARD
8373 const_iterator lower_bound(std::wstring_view key) const
8374 {
8375 if (empty())
8376 return end();
8377
8378 return lower_bound(impl::narrow(key));
8379 }
8380
8381 #endif // TOML_ENABLE_WINDOWS_COMPAT
8382
8383 TOML_PURE_GETTER
8384 TOML_EXPORTED_MEMBER_FUNCTION
8385 iterator find(std::string_view key) noexcept;
8386
8387 TOML_PURE_GETTER
8388 TOML_EXPORTED_MEMBER_FUNCTION
8389 const_iterator find(std::string_view key) const noexcept;
8390
8391 TOML_PURE_GETTER
8392 bool contains(std::string_view key) const noexcept
8393 {
8394 return get(key) != nullptr;
8395 }
8396
8397 #if TOML_ENABLE_WINDOWS_COMPAT
8398
8399 TOML_NODISCARD
8400 iterator find(std::wstring_view key)
8401 {
8402 if (empty())
8403 return end();
8404
8405 return find(impl::narrow(key));
8406 }
8407
8408 TOML_NODISCARD
8409 const_iterator find(std::wstring_view key) const
8410 {
8411 return find(impl::narrow(key));
8412 }
8413
8414 TOML_NODISCARD
8415 bool contains(std::wstring_view key) const
8416 {
8417 return contains(impl::narrow(key));
8418 }
8419
8420 #endif // TOML_ENABLE_WINDOWS_COMPAT
8421
8422 private:
8423
8424 TOML_EXPORTED_MEMBER_FUNCTION
8425 map_iterator erase(const_map_iterator) noexcept;
8426
8427 TOML_EXPORTED_MEMBER_FUNCTION
8428 map_iterator erase(const_map_iterator, const_map_iterator) noexcept;
8429
8430 public:
8431
8432 iterator erase(iterator pos) noexcept
8433 {
8434 return iterator{ erase(const_map_iterator{ pos }) };
8435 }
8436
8437 iterator erase(const_iterator pos) noexcept
8438 {
8439 return iterator{ erase(const_map_iterator{ pos }) };
8440 }
8441
8442 iterator erase(const_iterator begin, const_iterator end) noexcept
8443 {
8444 return iterator{ erase(const_map_iterator{ begin }, const_map_iterator{ end }) };
8445 }
8446
8447 TOML_EXPORTED_MEMBER_FUNCTION
8448 size_t erase(std::string_view key) noexcept;
8449
8450 #if TOML_ENABLE_WINDOWS_COMPAT
8451
8452 size_t erase(std::wstring_view key)
8453 {
8454 if (empty())
8455 return false;
8456
8457 return erase(impl::narrow(key));
8458 }
8459
8460 #endif // TOML_ENABLE_WINDOWS_COMPAT
8461
8462 TOML_EXPORTED_MEMBER_FUNCTION
8463 table& prune(bool recursive = true) & noexcept;
8464
8465 table&& prune(bool recursive = true) && noexcept
8466 {
8467 return static_cast<toml::table&&>(this->prune(recursive));
8468 }
8469
8470 TOML_EXPORTED_MEMBER_FUNCTION
8471 void clear() noexcept;
8472
8473 private:
8474
8475 TOML_EXPORTED_MEMBER_FUNCTION
8476 map_iterator insert_with_hint(const_iterator, key&&, impl::node_ptr&&);
8477
8478 public:
8479
8480 TOML_CONSTRAINED_TEMPLATE((is_key_or_convertible<KeyType&&> || impl::is_wide_string<KeyType>),
8481 typename ValueType = void,
8482 typename KeyType,
8483 typename... ValueArgs)
8484 iterator emplace_hint(const_iterator hint, KeyType&& key, ValueArgs&&... args)
8485 {
8486 static_assert(!impl::is_wide_string<KeyType> || TOML_ENABLE_WINDOWS_COMPAT,
8487 "Emplacement using wide-character keys is only supported on Windows with "
8488 "TOML_ENABLE_WINDOWS_COMPAT enabled.");
8489
8490 using raw_value_type = impl::remove_cvref<ValueType>;
8491 using value_type = std::
8492 conditional_t<std::is_void_v<raw_value_type>, impl::emplaced_type_of<ValueArgs&&...>, raw_value_type>;
8493
8494 if constexpr (impl::is_wide_string<KeyType>)
8495 {
8496 #if TOML_ENABLE_WINDOWS_COMPAT
8497 return emplace_hint<value_type>(hint,
8498 impl::narrow(static_cast<KeyType&&>(key)),
8499 static_cast<ValueArgs&&>(args)...);
8500 #else
8501 static_assert(impl::always_false<KeyType>, "Evaluated unreachable branch!");
8502 #endif
8503 }
8504 else
8505 {
8506 static constexpr auto moving_node_ptr = std::is_same_v<value_type, impl::node_ptr> //
8507 && sizeof...(ValueArgs) == 1u //
8508 && impl::first_is_same<impl::node_ptr&&, ValueArgs&&...>;
8509 using unwrapped_type = impl::remove_cvref<impl::unwrap_node<value_type>>;
8510
8511 static_assert(moving_node_ptr //
8512 || impl::is_native<unwrapped_type> //
8513 || impl::is_one_of<unwrapped_type, table, array>, //
8514 "ValueType argument of table::emplace_hint() must be one "
8515 "of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
8516
8517 map_iterator ipos = insert_with_hint(hint, toml::key{ static_cast<KeyType&&>(key) }, nullptr);
8518
8519 // if second is nullptr then we successully claimed the key and inserted the empty sentinel,
8520 // so now we have to construct the actual value
8521 if (!ipos->second)
8522 {
8523 if constexpr (moving_node_ptr)
8524 ipos->second = std::move(static_cast<ValueArgs&&>(args)...);
8525 else
8526 {
8527 #if TOML_COMPILER_HAS_EXCEPTIONS
8528 try
8529 {
8530 #endif
8531 ipos->second.reset(
8532 new impl::wrap_node<unwrapped_type>{ static_cast<ValueArgs&&>(args)... });
8533 #if TOML_COMPILER_HAS_EXCEPTIONS
8534 }
8535 catch (...)
8536 {
8537 erase(const_map_iterator{ ipos }); // strong exception guarantee
8538 throw;
8539 }
8540 #endif
8541 }
8542 }
8543 return iterator{ ipos };
8544 }
8545
8546 TOML_UNREACHABLE;
8547 }
8548
8549 TOML_CONSTRAINED_TEMPLATE((is_key_or_convertible<KeyType&&> || impl::is_wide_string<KeyType>),
8550 typename KeyType,
8551 typename ValueType)
8552 std::pair<iterator, bool> insert(KeyType&& key,
8553 ValueType&& val,
8554 value_flags flags = preserve_source_value_flags)
8555 {
8556 static_assert(!impl::is_wide_string<KeyType> || TOML_ENABLE_WINDOWS_COMPAT,
8557 "Insertion using wide-character keys is only supported on Windows with "
8558 "TOML_ENABLE_WINDOWS_COMPAT enabled.");
8559
8560 if constexpr (is_node_view<ValueType>)
8561 {
8562 if (!val)
8563 return { end(), false };
8564 }
8565
8566 if constexpr (impl::is_wide_string<KeyType>)
8567 {
8568 #if TOML_ENABLE_WINDOWS_COMPAT
8569 return insert(impl::narrow(static_cast<KeyType&&>(key)), static_cast<ValueType&&>(val), flags);
8570 #else
8571 static_assert(impl::always_false<KeyType>, "Evaluated unreachable branch!");
8572 #endif
8573 }
8574 else
8575 {
8576 const auto key_view = std::string_view{ key };
8577 map_iterator ipos = get_lower_bound(key_view);
8578 if (ipos == map_.end() || ipos->first != key_view)
8579 {
8580 ipos = insert_with_hint(const_iterator{ ipos },
8581 toml::key{ static_cast<KeyType&&>(key) },
8582 impl::make_node(static_cast<ValueType&&>(val), flags));
8583 return { iterator{ ipos }, true };
8584 }
8585 return { iterator{ ipos }, false };
8586 }
8587 }
8588
8589 TOML_CONSTRAINED_TEMPLATE((!is_key_or_convertible<Iter> && !impl::is_wide_string<Iter>), typename Iter)
8590 void insert(Iter begin, Iter end, value_flags flags = preserve_source_value_flags)
8591 {
8592 if (begin == end)
8593 return;
8594 for (auto it = begin; it != end; it++)
8595 {
8596 if constexpr (std::is_rvalue_reference_v<decltype(*it)>)
8597 insert(std::move((*it).first), std::move((*it).second), flags);
8598 else
8599 insert((*it).first, (*it).second, flags);
8600 }
8601 }
8602
8603 TOML_CONSTRAINED_TEMPLATE((is_key_or_convertible<KeyType&&> || impl::is_wide_string<KeyType>),
8604 typename KeyType,
8605 typename ValueType)
8606 std::pair<iterator, bool> insert_or_assign(KeyType&& key,
8607 ValueType&& val,
8608 value_flags flags = preserve_source_value_flags)
8609 {
8610 static_assert(!impl::is_wide_string<KeyType> || TOML_ENABLE_WINDOWS_COMPAT,
8611 "Insertion using wide-character keys is only supported on Windows with "
8612 "TOML_ENABLE_WINDOWS_COMPAT enabled.");
8613
8614 if constexpr (is_node_view<ValueType>)
8615 {
8616 if (!val)
8617 return { end(), false };
8618 }
8619
8620 if constexpr (impl::is_wide_string<KeyType>)
8621 {
8622 #if TOML_ENABLE_WINDOWS_COMPAT
8623 return insert_or_assign(impl::narrow(static_cast<KeyType&&>(key)),
8624 static_cast<ValueType&&>(val),
8625 flags);
8626 #else
8627 static_assert(impl::always_false<KeyType>, "Evaluated unreachable branch!");
8628 #endif
8629 }
8630 else
8631 {
8632 const auto key_view = std::string_view{ key };
8633 map_iterator ipos = get_lower_bound(key_view);
8634 if (ipos == map_.end() || ipos->first != key_view)
8635 {
8636 ipos = insert_with_hint(const_iterator{ ipos },
8637 toml::key{ static_cast<KeyType&&>(key) },
8638 impl::make_node(static_cast<ValueType&&>(val), flags));
8639 return { iterator{ ipos }, true };
8640 }
8641 else
8642 {
8643 (*ipos).second = impl::make_node(static_cast<ValueType&&>(val), flags);
8644 return { iterator{ ipos }, false };
8645 }
8646 }
8647 }
8648
8649 TOML_CONSTRAINED_TEMPLATE((is_key_or_convertible<KeyType&&> || impl::is_wide_string<KeyType>),
8650 typename ValueType = void,
8651 typename KeyType,
8652 typename... ValueArgs)
8653 std::pair<iterator, bool> emplace(KeyType&& key, ValueArgs&&... args)
8654 {
8655 static_assert(!impl::is_wide_string<KeyType> || TOML_ENABLE_WINDOWS_COMPAT,
8656 "Emplacement using wide-character keys is only supported on Windows with "
8657 "TOML_ENABLE_WINDOWS_COMPAT enabled.");
8658
8659 using raw_value_type = impl::remove_cvref<ValueType>;
8660 using value_type = std::
8661 conditional_t<std::is_void_v<raw_value_type>, impl::emplaced_type_of<ValueArgs&&...>, raw_value_type>;
8662
8663 if constexpr (impl::is_wide_string<KeyType>)
8664 {
8665 #if TOML_ENABLE_WINDOWS_COMPAT
8666 return emplace<value_type>(impl::narrow(static_cast<KeyType&&>(key)),
8667 static_cast<ValueArgs&&>(args)...);
8668 #else
8669 static_assert(impl::always_false<KeyType>, "Evaluated unreachable branch!");
8670 #endif
8671 }
8672 else
8673 {
8674 using unwrapped_type = impl::remove_cvref<impl::unwrap_node<value_type>>;
8675 static_assert((impl::is_native<unwrapped_type> || impl::is_one_of<unwrapped_type, table, array>),
8676 "ValueType argument of table::emplace() must be one "
8677 "of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
8678
8679 const auto key_view = std::string_view{ key };
8680 auto ipos = get_lower_bound(key_view);
8681 if (ipos == map_.end() || ipos->first != key_view)
8682 {
8683 ipos = insert_with_hint(
8684 const_iterator{ ipos },
8685 toml::key{ static_cast<KeyType&&>(key) },
8686 impl::node_ptr{ new impl::wrap_node<unwrapped_type>{ static_cast<ValueArgs&&>(args)... } });
8687 return { iterator{ ipos }, true };
8688 }
8689 return { iterator{ ipos }, false };
8690 }
8691 }
8692
8693 using node::operator[]; // inherit operator[toml::path]
8694
8695 TOML_NODISCARD
8696 node_view<node> operator[](std::string_view key) noexcept
8697 {
8698 return node_view<node>{ get(key) };
8699 }
8700
8701 TOML_NODISCARD
8702 node_view<const node> operator[](std::string_view key) const noexcept
8703 {
8704 return node_view<const node>{ get(key) };
8705 }
8706
8707 #if TOML_ENABLE_WINDOWS_COMPAT
8708
8709 TOML_NODISCARD
8710 node_view<node> operator[](std::wstring_view key)
8711 {
8712 return node_view<node>{ get(key) };
8713 }
8714
8715 TOML_NODISCARD
8716 node_view<const node> operator[](std::wstring_view key) const
8717 {
8718 return node_view<const node>{ get(key) };
8719 }
8720
8721 #endif // TOML_ENABLE_WINDOWS_COMPAT
8722
8723 private:
8724
8725 TOML_PURE_GETTER
8726 TOML_EXPORTED_STATIC_FUNCTION
8727 static bool TOML_CALLCONV equal(const table&, const table&) noexcept;
8728
8729 public:
8730
8731 TOML_NODISCARD
8732 friend bool operator==(const table& lhs, const table& rhs) noexcept
8733 {
8734 return equal(lhs, rhs);
8735 }
8736
8737 TOML_NODISCARD
8738 friend bool operator!=(const table& lhs, const table& rhs) noexcept
8739 {
8740 return !equal(lhs, rhs);
8741 }
8742
8743 #if TOML_ENABLE_FORMATTERS
8744
8745 friend std::ostream& operator<<(std::ostream& lhs, const table& rhs)
8746 {
8747 impl::print_to_stream(lhs, rhs);
8748 return lhs;
8749 }
8750
8751 #endif
8752 };
8753 }
8754 TOML_NAMESPACE_END;
8755
8756 #ifdef _MSC_VER
8757 #pragma pop_macro("min")
8758 #pragma pop_macro("max")
8759 #ifndef __clang__
8760 #pragma inline_recursion(off)
8761 #endif
8762 #endif
8763 TOML_POP_WARNINGS;
8764
8765 //******** impl/unicode_autogenerated.hpp ****************************************************************************
8766
8767 TOML_PUSH_WARNINGS;
8768 #ifdef _MSC_VER
8769 #ifndef __clang__
8770 #pragma inline_recursion(on)
8771 #endif
8772 #pragma push_macro("min")
8773 #pragma push_macro("max")
8774 #undef min
8775 #undef max
8776 #endif
8777
8778 #if TOML_GCC && TOML_GCC < 9
8779 #pragma GCC push_options
8780 #pragma GCC optimize("O1") // codegen bugs
8781 #endif
8782
8783 // the functions in this namespace block are automatically generated by a tool - they are not meant to be hand-edited
8784
8785 TOML_IMPL_NAMESPACE_START
8786 {
8787 TOML_CONST_GETTER
8788 constexpr bool is_ascii_horizontal_whitespace(char32_t c) noexcept
8789 {
8790 return c == U'\t' || c == U' ';
8791 }
8792
8793 TOML_CONST_GETTER
8794 constexpr bool is_non_ascii_horizontal_whitespace(char32_t c) noexcept
8795 {
8796 // 20 code units from 8 ranges (spanning a search area of 65120)
8797
8798 if (c < U'\xA0' || c > U'\uFEFF')
8799 return false;
8800
8801 const auto child_index_0 = (static_cast<uint_least64_t>(c) - 0xA0ull) / 0x3FAull;
8802 if ((1ull << child_index_0) & 0x7FFFFFFFFFFFF75Eull)
8803 return false;
8804 if (c == U'\xA0' || c == U'\u3000' || c == U'\uFEFF')
8805 return true;
8806 switch (child_index_0)
8807 {
8808 case 0x05: return c == U'\u1680' || c == U'\u180E';
8809 case 0x07:
8810 return (U'\u2000' <= c && c <= U'\u200B') || (U'\u205F' <= c && c <= U'\u2060') || c == U'\u202F';
8811 default: TOML_UNREACHABLE;
8812 }
8813
8814 TOML_UNREACHABLE;
8815 }
8816
8817 TOML_CONST_GETTER
8818 constexpr bool is_ascii_vertical_whitespace(char32_t c) noexcept
8819 {
8820 return c >= U'\n' && c <= U'\r';
8821 }
8822
8823 TOML_CONST_GETTER
8824 constexpr bool is_non_ascii_vertical_whitespace(char32_t c) noexcept
8825 {
8826 return (U'\u2028' <= c && c <= U'\u2029') || c == U'\x85';
8827 }
8828
8829 TOML_CONST_GETTER
8830 constexpr bool is_ascii_bare_key_character(char32_t c) noexcept
8831 {
8832 #if TOML_LANG_UNRELEASED // toml/issues/644 ('+' in bare keys)
8833 if TOML_UNLIKELY(c == U'+')
8834 return true;
8835 #endif
8836 // 64 code units from 5 ranges (spanning a search area of 78)
8837
8838 if (c < U'-' || c > U'z')
8839 return false;
8840
8841 return (((static_cast<uint_least64_t>(c) - 0x2Dull) / 0x40ull) != 0ull)
8842 || ((1ull << (static_cast<uint_least64_t>(c) - 0x2Dull)) & 0xFFF43FFFFFF01FF9ull);
8843 }
8844
8845 #if TOML_LANG_UNRELEASED // toml/pull/891 (unicode bare keys)
8846
8847 TOML_CONST_GETTER
8848 constexpr bool is_non_ascii_bare_key_character(char32_t c) noexcept
8849 {
8850 // 971732 code units from 16 ranges (spanning a search area of 982862)
8851
8852 if (c < U'\xB2' || c > U'\U000EFFFF')
8853 return false;
8854
8855 const auto child_index_0 = (static_cast<uint_least64_t>(c) - 0xB2ull) / 0x3BFEull;
8856 if ((1ull << child_index_0) & 0xFFFFFFFFFFFFFFE6ull)
8857 return true;
8858 switch (child_index_0)
8859 {
8860 case 0x00: // [0] 00B2 - 3CAF
8861 {
8862 // 12710 code units from 13 ranges (spanning a search area of 15358)
8863
8864 TOML_ASSUME(c >= U'\xB2' && c <= U'\u3CAF');
8865
8866 constexpr uint_least64_t bitmask_table_1[] = {
8867 0xFFFFFFDFFFFFDC83u, 0xFFFFFFFFFFFFFFDFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8868 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8869 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFEFFFu,
8870 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8871 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8872 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8873 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8874 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8875 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8876 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8877 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8878 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8879 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8880 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8881 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8882 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8883 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8884 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8885 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8886 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8887 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8888 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8889 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8890 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8891 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8892 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8893 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8894 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8895 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8896 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8897 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8898 0xFFFFFFFFFFFFFFFFu, 0x000000000C003FFFu, 0xC000000000006000u, 0xFFFFFFFFFFFFFFFFu,
8899 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0x000000003FFFFFFFu,
8900 0x0000000000000000u, 0x0000000000000000u, 0x0000000000000000u, 0x0000000000000000u,
8901 0x0000000000000000u, 0x0000000000000000u, 0x0000000000000000u, 0x0000000000000000u,
8902 0x0000000000000000u, 0x0000000000000000u, 0xFFFFC00000000000u, 0xFFFFFFFFFFFFFFFFu,
8903 0xFFFFFFFFFFFFFFFFu, 0x0000000000003FFFu, 0x0000000000000000u, 0x0000000000000000u,
8904 0x0000000000000000u, 0x0000000000000000u, 0x0000000000000000u, 0x0000000000000000u,
8905 0x0000000000000000u, 0x0000000000000000u, 0x0000000000000000u, 0x0000000000000000u,
8906 0x0000000000000000u, 0x0000000000000000u, 0x0000000000000000u, 0x0000000000000000u,
8907 0x0000000000000000u, 0x0000000000000000u, 0x0000000000000000u, 0x0000000000000000u,
8908 0x0000000000000000u, 0x0000000000000000u, 0x0000000000000000u, 0x0000000000000000u,
8909 0x0000000000000000u, 0x0000000000000000u, 0x0000000000000000u, 0x0000000000000000u,
8910 0x0000000000000000u, 0xFFFFFFFFFFFFC000u, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8911 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8912 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8913 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8914 0x3FFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFF8000u, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8915 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8916 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8917 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8918 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8919 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8920 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8921 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8922 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8923 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8924 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8925 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
8926 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0x3FFFFFFFFFFFFFFFu,
8927 };
8928 return bitmask_table_1[(static_cast<uint_least64_t>(c) - 0xB2ull) / 0x40ull]
8929 & (0x1ull << ((static_cast<uint_least64_t>(c) - 0xB2ull) % 0x40ull));
8930 }
8931 case 0x03: return c <= U'\uD7FF';
8932 case 0x04:
8933 return (U'\uF900' <= c && c <= U'\uFDCF') || (U'\uFDF0' <= c && c <= U'\uFFFD') || U'\U00010000' <= c;
8934 default: TOML_UNREACHABLE;
8935 }
8936
8937 TOML_UNREACHABLE;
8938 }
8939
8940 #endif // TOML_LANG_UNRELEASED
8941 }
8942 TOML_IMPL_NAMESPACE_END;
8943
8944 #if TOML_GCC && TOML_GCC < 9
8945 #pragma GCC pop_options
8946 #endif
8947
8948 #ifdef _MSC_VER
8949 #pragma pop_macro("min")
8950 #pragma pop_macro("max")
8951 #ifndef __clang__
8952 #pragma inline_recursion(off)
8953 #endif
8954 #endif
8955 TOML_POP_WARNINGS;
8956
8957 //******** impl/unicode.hpp ******************************************************************************************
8958
8959 TOML_PUSH_WARNINGS;
8960 #ifdef _MSC_VER
8961 #ifndef __clang__
8962 #pragma inline_recursion(on)
8963 #endif
8964 #pragma push_macro("min")
8965 #pragma push_macro("max")
8966 #undef min
8967 #undef max
8968 #endif
8969
8970 TOML_IMPL_NAMESPACE_START
8971 {
8972 TOML_CONST_GETTER
8973 constexpr bool is_string_delimiter(char32_t c) noexcept
8974 {
8975 return c == U'"' || c == U'\'';
8976 }
8977
8978 TOML_CONST_GETTER
8979 constexpr bool is_ascii_letter(char32_t c) noexcept
8980 {
8981 return (c >= U'a' && c <= U'z') || (c >= U'A' && c <= U'Z');
8982 }
8983
8984 TOML_CONST_GETTER
8985 constexpr bool is_binary_digit(char32_t c) noexcept
8986 {
8987 return c == U'0' || c == U'1';
8988 }
8989
8990 TOML_CONST_GETTER
8991 constexpr bool is_octal_digit(char32_t c) noexcept
8992 {
8993 return (c >= U'0' && c <= U'7');
8994 }
8995
8996 TOML_CONST_GETTER
8997 constexpr bool is_decimal_digit(char32_t c) noexcept
8998 {
8999 return (c >= U'0' && c <= U'9');
9000 }
9001
9002 TOML_CONST_GETTER
9003 constexpr bool is_hexadecimal_digit(char32_t c) noexcept
9004 {
9005 return U'0' <= c && c <= U'f' && (1ull << (static_cast<uint_least64_t>(c) - 0x30u)) & 0x7E0000007E03FFull;
9006 }
9007
9008 template <typename T>
9009 TOML_CONST_GETTER
9010 constexpr uint_least32_t hex_to_dec(const T c) noexcept
9011 {
9012 if constexpr (std::is_same_v<remove_cvref<T>, uint_least32_t>)
9013 return c >= 0x41u // >= 'A'
9014 ? 10u + (c | 0x20u) - 0x61u // - 'a'
9015 : c - 0x30u // - '0'
9016 ;
9017 else
9018 return hex_to_dec(static_cast<uint_least32_t>(c));
9019 }
9020
9021 TOML_CONST_GETTER
9022 constexpr bool is_horizontal_whitespace(char32_t c) noexcept
9023 {
9024 return is_ascii_horizontal_whitespace(c) || is_non_ascii_horizontal_whitespace(c);
9025 }
9026
9027 TOML_CONST_GETTER
9028 constexpr bool is_vertical_whitespace(char32_t c) noexcept
9029 {
9030 return is_ascii_vertical_whitespace(c) || is_non_ascii_vertical_whitespace(c);
9031 }
9032
9033 TOML_CONST_GETTER
9034 constexpr bool is_whitespace(char32_t c) noexcept
9035 {
9036 return is_horizontal_whitespace(c) || is_vertical_whitespace(c);
9037 }
9038
9039 TOML_CONST_GETTER
9040 constexpr bool is_bare_key_character(char32_t c) noexcept
9041 {
9042 return is_ascii_bare_key_character(c)
9043 #if TOML_LANG_UNRELEASED // toml/pull/891 (unicode bare keys)
9044 || is_non_ascii_bare_key_character(c)
9045 #endif
9046 ;
9047 }
9048
9049 TOML_CONST_GETTER
9050 constexpr bool is_value_terminator(char32_t c) noexcept
9051 {
9052 return is_whitespace(c) || c == U']' || c == U'}' || c == U',' || c == U'#';
9053 }
9054
9055 TOML_CONST_GETTER
9056 constexpr bool is_control_character(char c) noexcept
9057 {
9058 return c <= '\u001F' || c == '\u007F';
9059 }
9060
9061 TOML_CONST_GETTER
9062 constexpr bool is_control_character(char32_t c) noexcept
9063 {
9064 return c <= U'\u001F' || c == U'\u007F';
9065 }
9066
9067 TOML_CONST_GETTER
9068 constexpr bool is_nontab_control_character(char32_t c) noexcept
9069 {
9070 return c <= U'\u0008' || (c >= U'\u000A' && c <= U'\u001F') || c == U'\u007F';
9071 }
9072
9073 TOML_CONST_GETTER
9074 constexpr bool is_unicode_surrogate(char32_t c) noexcept
9075 {
9076 return c >= 0xD800u && c <= 0xDFFF;
9077 }
9078
9079 struct utf8_decoder
9080 {
9081 // utf8_decoder based on this: https://bjoern.hoehrmann.de/utf-8/decoder/dfa/
9082 // Copyright (c) 2008-2009 Bjoern Hoehrmann <[email protected]>
9083
9084 uint_least32_t state{};
9085 char32_t codepoint{};
9086
9087 static constexpr uint8_t state_table[]{
9088 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9089 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9090 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9091 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9092 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
9093 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 7, 7,
9094 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
9095 7, 7, 7, 8, 8, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
9096 2, 2, 2, 2, 2, 2, 2, 2, 10, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 3, 11, 6, 6,
9097 6, 5, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
9098 0, 12, 24, 36, 60, 96, 84, 12, 12, 12, 48, 72, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 0, 12,
9099 12, 12, 12, 12, 0, 12, 0, 12, 12, 12, 24, 12, 12, 12, 12, 12, 24, 12, 24, 12, 12, 12, 12, 12, 12, 12, 12,
9100 12, 24, 12, 12, 12, 12, 12, 24, 12, 12, 12, 12, 12, 12, 12, 24, 12, 12, 12, 12, 12, 12, 12, 12, 12, 36, 12,
9101 36, 12, 12, 12, 36, 12, 12, 12, 12, 12, 36, 12, 36, 12, 12, 12, 36, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12
9102 };
9103
9104 TOML_PURE_INLINE_GETTER
9105 constexpr bool error() const noexcept
9106 {
9107 return state == uint_least32_t{ 12u };
9108 }
9109
9110 TOML_PURE_INLINE_GETTER
9111 constexpr bool has_code_point() const noexcept
9112 {
9113 return state == uint_least32_t{};
9114 }
9115
9116 TOML_PURE_INLINE_GETTER
9117 constexpr bool needs_more_input() const noexcept
9118 {
9119 return !has_code_point() && !error();
9120 }
9121
9122 constexpr void operator()(uint8_t byte) noexcept
9123 {
9124 TOML_ASSERT_ASSUME(!error());
9125
9126 const auto type = state_table[byte];
9127
9128 codepoint = static_cast<char32_t>(has_code_point() ? (uint_least32_t{ 255u } >> type) & byte
9129 : (byte & uint_least32_t{ 63u })
9130 | (static_cast<uint_least32_t>(codepoint) << 6));
9131
9132 state = state_table[state + uint_least32_t{ 256u } + type];
9133 }
9134
9135 TOML_ALWAYS_INLINE
9136 constexpr void operator()(char c) noexcept
9137 {
9138 operator()(static_cast<uint8_t>(c));
9139 }
9140
9141 TOML_ALWAYS_INLINE
9142 constexpr void reset() noexcept
9143 {
9144 state = {};
9145 }
9146 };
9147
9148 TOML_PURE_GETTER
9149 TOML_ATTR(nonnull)
9150 bool is_ascii(const char* str, size_t len) noexcept;
9151 }
9152 TOML_IMPL_NAMESPACE_END;
9153
9154 #ifdef _MSC_VER
9155 #pragma pop_macro("min")
9156 #pragma pop_macro("max")
9157 #ifndef __clang__
9158 #pragma inline_recursion(off)
9159 #endif
9160 #endif
9161 TOML_POP_WARNINGS;
9162
9163 //******** impl/parse_error.hpp **************************************************************************************
9164
9165 #if TOML_ENABLE_PARSER
9166
9167 //******** impl/std_except.hpp ***************************************************************************************
9168
9169 TOML_DISABLE_WARNINGS;
9170 #if TOML_EXCEPTIONS
9171 #include <stdexcept>
9172 #endif
9173 TOML_ENABLE_WARNINGS;
9174
9175 //******** impl/parse_error.hpp **************************************************************************************
9176
9177 TOML_PUSH_WARNINGS;
9178 #ifdef _MSC_VER
9179 #ifndef __clang__
9180 #pragma inline_recursion(on)
9181 #endif
9182 #pragma push_macro("min")
9183 #pragma push_macro("max")
9184 #undef min
9185 #undef max
9186 #endif
9187
9188 #if TOML_DOXYGEN || !TOML_EXCEPTIONS
9189 #define TOML_PARSE_ERROR_BASE
9190 #else
9191 #define TOML_PARSE_ERROR_BASE : public std::runtime_error
9192 #endif
9193
9194 TOML_NAMESPACE_START
9195 {
9196 TOML_ABI_NAMESPACE_BOOL(TOML_EXCEPTIONS, ex, noex);
9197
9198 class parse_error TOML_PARSE_ERROR_BASE
9199 {
9200 private:
9201 #if !TOML_EXCEPTIONS
9202 std::string description_;
9203 #endif
9204 source_region source_;
9205
9206 public:
9207 #if TOML_EXCEPTIONS
9208
9209 TOML_NODISCARD_CTOR
9210 TOML_ATTR(nonnull)
9211 parse_error(const char* desc, source_region&& src) noexcept //
9212 : std::runtime_error{ desc },
9213 source_{ std::move(src) }
9214 {}
9215
9216 TOML_NODISCARD_CTOR
9217 TOML_ATTR(nonnull)
9218 parse_error(const char* desc, const source_region& src) noexcept //
9219 : parse_error{ desc, source_region{ src } }
9220 {}
9221
9222 TOML_NODISCARD_CTOR
9223 TOML_ATTR(nonnull)
9224 parse_error(const char* desc, const source_position& position, const source_path_ptr& path = {}) noexcept
9225 : parse_error{ desc, source_region{ position, position, path } }
9226 {}
9227
9228 #else
9229
9230 TOML_NODISCARD_CTOR
9231 parse_error(std::string&& desc, source_region&& src) noexcept //
9232 : description_{ std::move(desc) },
9233 source_{ std::move(src) }
9234 {}
9235
9236 TOML_NODISCARD_CTOR
9237 parse_error(std::string&& desc, const source_region& src) noexcept //
9238 : parse_error{ std::move(desc), source_region{ src } }
9239 {}
9240
9241 TOML_NODISCARD_CTOR
9242 parse_error(std::string&& desc, const source_position& position, const source_path_ptr& path = {}) noexcept
9243 : parse_error{ std::move(desc), source_region{ position, position, path } }
9244 {}
9245
9246 #endif
9247
9248 TOML_NODISCARD
9249 std::string_view description() const noexcept
9250 {
9251 #if TOML_EXCEPTIONS
9252 return std::string_view{ what() };
9253 #else
9254 return description_;
9255 #endif
9256 }
9257
9258 TOML_NODISCARD
9259 const source_region& source() const noexcept
9260 {
9261 return source_;
9262 }
9263
9264 friend std::ostream& operator<<(std::ostream& lhs, const parse_error& rhs)
9265 {
9266 impl::print_to_stream(lhs, rhs.description());
9267 impl::print_to_stream(lhs, "\n\t(error occurred at "sv);
9268 impl::print_to_stream(lhs, rhs.source());
9269 impl::print_to_stream(lhs, ")"sv);
9270 return lhs;
9271 }
9272 };
9273
9274 TOML_ABI_NAMESPACE_END; // TOML_EXCEPTIONS
9275 }
9276 TOML_NAMESPACE_END;
9277
9278 #undef TOML_PARSE_ERROR_BASE
9279
9280 #ifdef _MSC_VER
9281 #pragma pop_macro("min")
9282 #pragma pop_macro("max")
9283 #ifndef __clang__
9284 #pragma inline_recursion(off)
9285 #endif
9286 #endif
9287 TOML_POP_WARNINGS;
9288
9289 #endif // TOML_ENABLE_PARSER
9290
9291 //******** impl/parse_result.hpp *************************************************************************************
9292
9293 #if TOML_DOXYGEN || (TOML_ENABLE_PARSER && !TOML_EXCEPTIONS)
9294
9295 TOML_PUSH_WARNINGS;
9296 #ifdef _MSC_VER
9297 #ifndef __clang__
9298 #pragma inline_recursion(on)
9299 #endif
9300 #pragma push_macro("min")
9301 #pragma push_macro("max")
9302 #undef min
9303 #undef max
9304 #endif
9305
9306 TOML_NAMESPACE_START
9307 {
9308 TOML_ABI_NAMESPACE_START(noex);
9309
9310 class parse_result
9311 {
9312 private:
9313 struct storage_t
9314 {
9315 static constexpr size_t size =
9316 (sizeof(toml::table) < sizeof(parse_error) ? sizeof(parse_error) : sizeof(toml::table));
9317 static constexpr size_t align =
9318 (alignof(toml::table) < alignof(parse_error) ? alignof(parse_error) : alignof(toml::table));
9319
9320 alignas(align) unsigned char bytes[size];
9321 };
9322
9323 alignas(storage_t::align) mutable storage_t storage_;
9324 bool err_;
9325
9326 template <typename Type>
9327 TOML_NODISCARD
9328 TOML_ALWAYS_INLINE
9329 static Type* get_as(storage_t& s) noexcept
9330 {
9331 return TOML_LAUNDER(reinterpret_cast<Type*>(s.bytes));
9332 }
9333
9334 void destroy() noexcept
9335 {
9336 if (err_)
9337 get_as<parse_error>(storage_)->~parse_error();
9338 else
9339 get_as<toml::table>(storage_)->~table();
9340 }
9341
9342 public:
9343
9344 TOML_NODISCARD_CTOR
9345 parse_result() noexcept //
9346 : err_{ true }
9347 {
9348 ::new (static_cast<void*>(storage_.bytes)) parse_error{ std::string{}, source_region{} };
9349 }
9350
9351 TOML_NODISCARD_CTOR
9352 explicit parse_result(toml::table&& tbl) noexcept //
9353 : err_{ false }
9354 {
9355 ::new (static_cast<void*>(storage_.bytes)) toml::table{ std::move(tbl) };
9356 }
9357
9358 TOML_NODISCARD_CTOR
9359 explicit parse_result(parse_error&& err) noexcept //
9360 : err_{ true }
9361 {
9362 ::new (static_cast<void*>(storage_.bytes)) parse_error{ std::move(err) };
9363 }
9364
9365 TOML_NODISCARD_CTOR
9366 parse_result(parse_result&& res) noexcept //
9367 : err_{ res.err_ }
9368 {
9369 if (err_)
9370 ::new (static_cast<void*>(storage_.bytes)) parse_error{ std::move(res).error() };
9371 else
9372 ::new (static_cast<void*>(storage_.bytes)) toml::table{ std::move(res).table() };
9373 }
9374
9375 parse_result& operator=(parse_result&& rhs) noexcept
9376 {
9377 if (err_ != rhs.err_)
9378 {
9379 destroy();
9380 err_ = rhs.err_;
9381 if (err_)
9382 ::new (static_cast<void*>(storage_.bytes)) parse_error{ std::move(rhs).error() };
9383 else
9384 ::new (static_cast<void*>(storage_.bytes)) toml::table{ std::move(rhs).table() };
9385 }
9386 else
9387 {
9388 if (err_)
9389 error() = std::move(rhs).error();
9390 else
9391 table() = std::move(rhs).table();
9392 }
9393 return *this;
9394 }
9395
9396 ~parse_result() noexcept
9397 {
9398 destroy();
9399 }
9400
9401 TOML_NODISCARD
9402 bool succeeded() const noexcept
9403 {
9404 return !err_;
9405 }
9406
9407 TOML_NODISCARD
9408 bool failed() const noexcept
9409 {
9410 return err_;
9411 }
9412
9413 TOML_NODISCARD
9414 explicit operator bool() const noexcept
9415 {
9416 return !err_;
9417 }
9418
9419 TOML_NODISCARD
9420 toml::table& table() & noexcept
9421 {
9422 TOML_ASSERT_ASSUME(!err_);
9423 return *get_as<toml::table>(storage_);
9424 }
9425
9426 TOML_NODISCARD
9427 toml::table&& table() && noexcept
9428 {
9429 TOML_ASSERT_ASSUME(!err_);
9430 return static_cast<toml::table&&>(*get_as<toml::table>(storage_));
9431 }
9432
9433 TOML_NODISCARD
9434 const toml::table& table() const& noexcept
9435 {
9436 TOML_ASSERT_ASSUME(!err_);
9437 return *get_as<const toml::table>(storage_);
9438 }
9439
9440 TOML_NODISCARD
9441 /* implicit */ operator toml::table&() noexcept
9442 {
9443 return table();
9444 }
9445
9446 TOML_NODISCARD
9447 /* implicit */ operator toml::table&&() noexcept
9448 {
9449 return std::move(table());
9450 }
9451
9452 TOML_NODISCARD
9453 /* implicit */ operator const toml::table&() const noexcept
9454 {
9455 return table();
9456 }
9457
9458 TOML_NODISCARD
9459 parse_error& error() & noexcept
9460 {
9461 TOML_ASSERT_ASSUME(err_);
9462 return *get_as<parse_error>(storage_);
9463 }
9464
9465 TOML_NODISCARD
9466 parse_error&& error() && noexcept
9467 {
9468 TOML_ASSERT_ASSUME(err_);
9469 return static_cast<parse_error&&>(*get_as<parse_error>(storage_));
9470 }
9471
9472 TOML_NODISCARD
9473 const parse_error& error() const& noexcept
9474 {
9475 TOML_ASSERT_ASSUME(err_);
9476 return *get_as<const parse_error>(storage_);
9477 }
9478
9479 TOML_NODISCARD
9480 explicit operator parse_error&() noexcept
9481 {
9482 return error();
9483 }
9484
9485 TOML_NODISCARD
9486 explicit operator parse_error&&() noexcept
9487 {
9488 return std::move(error());
9489 }
9490
9491 TOML_NODISCARD
9492 explicit operator const parse_error&() const noexcept
9493 {
9494 return error();
9495 }
9496
9497 using iterator = table_iterator;
9498
9499 using const_iterator = const_table_iterator;
9500
9501 TOML_NODISCARD
9502 table_iterator begin() noexcept
9503 {
9504 return err_ ? table_iterator{} : table().begin();
9505 }
9506
9507 TOML_NODISCARD
9508 const_table_iterator begin() const noexcept
9509 {
9510 return err_ ? const_table_iterator{} : table().begin();
9511 }
9512
9513 TOML_NODISCARD
9514 const_table_iterator cbegin() const noexcept
9515 {
9516 return err_ ? const_table_iterator{} : table().cbegin();
9517 }
9518
9519 TOML_NODISCARD
9520 table_iterator end() noexcept
9521 {
9522 return err_ ? table_iterator{} : table().end();
9523 }
9524
9525 TOML_NODISCARD
9526 const_table_iterator end() const noexcept
9527 {
9528 return err_ ? const_table_iterator{} : table().end();
9529 }
9530
9531 TOML_NODISCARD
9532 const_table_iterator cend() const noexcept
9533 {
9534 return err_ ? const_table_iterator{} : table().cend();
9535 }
9536
9537 TOML_NODISCARD
9538 node_view<node> at_path(std::string_view path) noexcept
9539 {
9540 return err_ ? node_view<node>{} : table().at_path(path);
9541 }
9542
9543 TOML_NODISCARD
9544 node_view<const node> at_path(std::string_view path) const noexcept
9545 {
9546 return err_ ? node_view<const node>{} : table().at_path(path);
9547 }
9548
9549 TOML_NODISCARD
9550 node_view<node> at_path(const toml::path& path) noexcept
9551 {
9552 return err_ ? node_view<node>{} : table().at_path(path);
9553 }
9554
9555 TOML_NODISCARD
9556 node_view<const node> at_path(const toml::path& path) const noexcept
9557 {
9558 return err_ ? node_view<const node>{} : table().at_path(path);
9559 }
9560
9561 #if TOML_ENABLE_WINDOWS_COMPAT
9562
9563 TOML_NODISCARD
9564 node_view<node> at_path(std::wstring_view path)
9565 {
9566 return err_ ? node_view<node>{} : table().at_path(path);
9567 }
9568
9569 TOML_NODISCARD
9570 node_view<const node> at_path(std::wstring_view path) const
9571 {
9572 return err_ ? node_view<const node>{} : table().at_path(path);
9573 }
9574
9575 #endif
9576
9577 TOML_NODISCARD
9578 node_view<node> operator[](const toml::path& path) noexcept
9579 {
9580 return err_ ? node_view<node>{} : table()[path];
9581 }
9582
9583 TOML_NODISCARD
9584 node_view<const node> operator[](const toml::path& path) const noexcept
9585 {
9586 return err_ ? node_view<const node>{} : table()[path];
9587 }
9588
9589 TOML_NODISCARD
9590 node_view<node> operator[](std::string_view key) noexcept
9591 {
9592 return err_ ? node_view<node>{} : table()[key];
9593 }
9594
9595 TOML_NODISCARD
9596 node_view<const node> operator[](std::string_view key) const noexcept
9597 {
9598 return err_ ? node_view<const node>{} : table()[key];
9599 }
9600
9601 #if TOML_ENABLE_WINDOWS_COMPAT
9602
9603 TOML_NODISCARD
9604 node_view<node> operator[](std::wstring_view key)
9605 {
9606 return err_ ? node_view<node>{} : table()[key];
9607 }
9608
9609 TOML_NODISCARD
9610 node_view<const node> operator[](std::wstring_view key) const
9611 {
9612 return err_ ? node_view<const node>{} : table()[key];
9613 }
9614
9615 #endif // TOML_ENABLE_WINDOWS_COMPAT
9616
9617 #if TOML_ENABLE_FORMATTERS
9618
9619 friend std::ostream& operator<<(std::ostream& os, const parse_result& result)
9620 {
9621 return result.err_ ? (os << result.error()) : (os << result.table());
9622 }
9623
9624 #endif
9625 };
9626
9627 TOML_ABI_NAMESPACE_END;
9628 }
9629 TOML_NAMESPACE_END;
9630
9631 #ifdef _MSC_VER
9632 #pragma pop_macro("min")
9633 #pragma pop_macro("max")
9634 #ifndef __clang__
9635 #pragma inline_recursion(off)
9636 #endif
9637 #endif
9638 TOML_POP_WARNINGS;
9639
9640 #endif // TOML_ENABLE_PARSER && !TOML_EXCEPTIONS
9641
9642 //******** impl/parser.hpp *******************************************************************************************
9643
9644 #if TOML_ENABLE_PARSER
9645
9646 TOML_PUSH_WARNINGS;
9647 #ifdef _MSC_VER
9648 #ifndef __clang__
9649 #pragma inline_recursion(on)
9650 #endif
9651 #pragma push_macro("min")
9652 #pragma push_macro("max")
9653 #undef min
9654 #undef max
9655 #endif
9656
9657 TOML_NAMESPACE_START
9658 {
9659 TOML_ABI_NAMESPACE_BOOL(TOML_EXCEPTIONS, ex, noex);
9660
9661 TOML_NODISCARD
9662 TOML_EXPORTED_FREE_FUNCTION
9663 parse_result TOML_CALLCONV parse(std::string_view doc, std::string_view source_path = {});
9664
9665 TOML_NODISCARD
9666 TOML_EXPORTED_FREE_FUNCTION
9667 parse_result TOML_CALLCONV parse(std::string_view doc, std::string && source_path);
9668
9669 TOML_NODISCARD
9670 TOML_EXPORTED_FREE_FUNCTION
9671 parse_result TOML_CALLCONV parse_file(std::string_view file_path);
9672
9673 #if TOML_HAS_CHAR8
9674
9675 TOML_NODISCARD
9676 TOML_EXPORTED_FREE_FUNCTION
9677 parse_result TOML_CALLCONV parse(std::u8string_view doc, std::string_view source_path = {});
9678
9679 TOML_NODISCARD
9680 TOML_EXPORTED_FREE_FUNCTION
9681 parse_result TOML_CALLCONV parse(std::u8string_view doc, std::string && source_path);
9682
9683 TOML_NODISCARD
9684 TOML_EXPORTED_FREE_FUNCTION
9685 parse_result TOML_CALLCONV parse_file(std::u8string_view file_path);
9686
9687 #endif // TOML_HAS_CHAR8
9688
9689 #if TOML_ENABLE_WINDOWS_COMPAT
9690
9691 TOML_NODISCARD
9692 TOML_EXPORTED_FREE_FUNCTION
9693 parse_result TOML_CALLCONV parse(std::string_view doc, std::wstring_view source_path);
9694
9695 TOML_NODISCARD
9696 TOML_EXPORTED_FREE_FUNCTION
9697 parse_result TOML_CALLCONV parse(std::istream & doc, std::wstring_view source_path);
9698
9699 TOML_NODISCARD
9700 TOML_EXPORTED_FREE_FUNCTION
9701 parse_result TOML_CALLCONV parse_file(std::wstring_view file_path);
9702
9703 #endif // TOML_ENABLE_WINDOWS_COMPAT
9704
9705 #if TOML_HAS_CHAR8 && TOML_ENABLE_WINDOWS_COMPAT
9706
9707 TOML_NODISCARD
9708 TOML_EXPORTED_FREE_FUNCTION
9709 parse_result TOML_CALLCONV parse(std::u8string_view doc, std::wstring_view source_path);
9710
9711 #endif // TOML_HAS_CHAR8 && TOML_ENABLE_WINDOWS_COMPAT
9712
9713 TOML_NODISCARD
9714 TOML_EXPORTED_FREE_FUNCTION
9715 parse_result TOML_CALLCONV parse(std::istream & doc, std::string_view source_path = {});
9716
9717 TOML_NODISCARD
9718 TOML_EXPORTED_FREE_FUNCTION
9719 parse_result TOML_CALLCONV parse(std::istream & doc, std::string && source_path);
9720
9721 TOML_ABI_NAMESPACE_END; // TOML_EXCEPTIONS
9722
9723 inline namespace literals
9724 {
9725 TOML_ABI_NAMESPACE_BOOL(TOML_EXCEPTIONS, lit_ex, lit_noex);
9726
9727 TOML_NODISCARD
9728 TOML_ALWAYS_INLINE
9729 parse_result operator""_toml(const char* str, size_t len)
9730 {
9731 return parse(std::string_view{ str, len });
9732 }
9733
9734 #if TOML_HAS_CHAR8
9735
9736 TOML_NODISCARD
9737 TOML_ALWAYS_INLINE
9738 parse_result operator""_toml(const char8_t* str, size_t len)
9739 {
9740 return parse(std::u8string_view{ str, len });
9741 }
9742
9743 #endif // TOML_HAS_CHAR8
9744
9745 TOML_ABI_NAMESPACE_END; // TOML_EXCEPTIONS
9746 }
9747 }
9748 TOML_NAMESPACE_END;
9749
9750 #ifdef _MSC_VER
9751 #pragma pop_macro("min")
9752 #pragma pop_macro("max")
9753 #ifndef __clang__
9754 #pragma inline_recursion(off)
9755 #endif
9756 #endif
9757 TOML_POP_WARNINGS;
9758
9759 #endif // TOML_ENABLE_PARSER
9760
9761 //******** impl/formatter.hpp ****************************************************************************************
9762
9763 #if TOML_ENABLE_FORMATTERS
9764
9765 TOML_PUSH_WARNINGS;
9766 #ifdef _MSC_VER
9767 #ifndef __clang__
9768 #pragma inline_recursion(on)
9769 #endif
9770 #pragma push_macro("min")
9771 #pragma push_macro("max")
9772 #undef min
9773 #undef max
9774 #endif
9775
9776 TOML_IMPL_NAMESPACE_START
9777 {
9778 struct formatter_constants
9779 {
9780 format_flags mandatory_flags;
9781 format_flags ignored_flags;
9782
9783 std::string_view float_pos_inf;
9784 std::string_view float_neg_inf;
9785 std::string_view float_nan;
9786
9787 std::string_view bool_true;
9788 std::string_view bool_false;
9789 };
9790
9791 struct formatter_config
9792 {
9793 format_flags flags;
9794 std::string_view indent;
9795 };
9796
9797 class TOML_EXPORTED_CLASS formatter
9798 {
9799 private:
9800 const node* source_;
9801 #if TOML_ENABLE_PARSER && !TOML_EXCEPTIONS
9802 const parse_result* result_;
9803 #endif
9804 const formatter_constants* constants_;
9805 formatter_config config_;
9806 size_t indent_columns_;
9807 format_flags int_format_mask_;
9808 std::ostream* stream_; //
9809 int indent_; // these are set in attach()
9810 bool naked_newline_; //
9811
9812 protected:
9813 TOML_PURE_INLINE_GETTER
9814 const node& source() const noexcept
9815 {
9816 return *source_;
9817 }
9818
9819 TOML_PURE_INLINE_GETTER
9820 std::ostream& stream() const noexcept
9821 {
9822 return *stream_;
9823 }
9824
9825 TOML_PURE_INLINE_GETTER
9826 int indent() const noexcept
9827 {
9828 return indent_;
9829 }
9830
9831 void indent(int level) noexcept
9832 {
9833 indent_ = level;
9834 }
9835
9836 void increase_indent() noexcept
9837 {
9838 indent_++;
9839 }
9840
9841 void decrease_indent() noexcept
9842 {
9843 indent_--;
9844 }
9845
9846 TOML_PURE_INLINE_GETTER
9847 size_t indent_columns() const noexcept
9848 {
9849 return indent_columns_;
9850 }
9851
9852 TOML_PURE_INLINE_GETTER
9853 bool indent_array_elements() const noexcept
9854 {
9855 return !!(config_.flags & format_flags::indent_array_elements);
9856 }
9857
9858 TOML_PURE_INLINE_GETTER
9859 bool indent_sub_tables() const noexcept
9860 {
9861 return !!(config_.flags & format_flags::indent_sub_tables);
9862 }
9863
9864 TOML_PURE_INLINE_GETTER
9865 bool literal_strings_allowed() const noexcept
9866 {
9867 return !!(config_.flags & format_flags::allow_literal_strings);
9868 }
9869
9870 TOML_PURE_INLINE_GETTER
9871 bool multi_line_strings_allowed() const noexcept
9872 {
9873 return !!(config_.flags & format_flags::allow_multi_line_strings);
9874 }
9875
9876 TOML_PURE_INLINE_GETTER
9877 bool real_tabs_in_strings_allowed() const noexcept
9878 {
9879 return !!(config_.flags & format_flags::allow_real_tabs_in_strings);
9880 }
9881
9882 TOML_PURE_INLINE_GETTER
9883 bool unicode_strings_allowed() const noexcept
9884 {
9885 return !!(config_.flags & format_flags::allow_unicode_strings);
9886 }
9887
9888 TOML_PURE_INLINE_GETTER
9889 bool terse_kvps() const noexcept
9890 {
9891 return !!(config_.flags & format_flags::terse_key_value_pairs);
9892 }
9893
9894 TOML_PURE_INLINE_GETTER
9895 bool force_multiline_arrays() const noexcept
9896 {
9897 return !!(config_.flags & format_flags::force_multiline_arrays);
9898 }
9899
9900 TOML_EXPORTED_MEMBER_FUNCTION
9901 void attach(std::ostream& stream) noexcept;
9902
9903 TOML_EXPORTED_MEMBER_FUNCTION
9904 void detach() noexcept;
9905
9906 TOML_EXPORTED_MEMBER_FUNCTION
9907 void print_newline(bool force = false);
9908
9909 TOML_EXPORTED_MEMBER_FUNCTION
9910 void print_indent();
9911
9912 TOML_EXPORTED_MEMBER_FUNCTION
9913 void print_unformatted(char);
9914
9915 TOML_EXPORTED_MEMBER_FUNCTION
9916 void print_unformatted(std::string_view);
9917
9918 TOML_EXPORTED_MEMBER_FUNCTION
9919 void print_string(std::string_view str,
9920 bool allow_multi_line = true,
9921 bool allow_bare = false,
9922 bool allow_literal_whitespace = true);
9923
9924 TOML_EXPORTED_MEMBER_FUNCTION
9925 void print(const value<std::string>&);
9926
9927 TOML_EXPORTED_MEMBER_FUNCTION
9928 void print(const value<int64_t>&);
9929
9930 TOML_EXPORTED_MEMBER_FUNCTION
9931 void print(const value<double>&);
9932
9933 TOML_EXPORTED_MEMBER_FUNCTION
9934 void print(const value<bool>&);
9935
9936 TOML_EXPORTED_MEMBER_FUNCTION
9937 void print(const value<date>&);
9938
9939 TOML_EXPORTED_MEMBER_FUNCTION
9940 void print(const value<time>&);
9941
9942 TOML_EXPORTED_MEMBER_FUNCTION
9943 void print(const value<date_time>&);
9944
9945 TOML_EXPORTED_MEMBER_FUNCTION
9946 void print_value(const node&, node_type);
9947
9948 TOML_NODISCARD
9949 TOML_EXPORTED_MEMBER_FUNCTION
9950 bool dump_failed_parse_result();
9951
9952 TOML_NODISCARD_CTOR
9953 TOML_EXPORTED_MEMBER_FUNCTION
9954 formatter(const node*, const parse_result*, const formatter_constants&, const formatter_config&) noexcept;
9955 };
9956 }
9957 TOML_IMPL_NAMESPACE_END;
9958
9959 #ifdef _MSC_VER
9960 #pragma pop_macro("min")
9961 #pragma pop_macro("max")
9962 #ifndef __clang__
9963 #pragma inline_recursion(off)
9964 #endif
9965 #endif
9966 TOML_POP_WARNINGS;
9967
9968 #endif // TOML_ENABLE_FORMATTERS
9969
9970 //******** impl/toml_formatter.hpp ***********************************************************************************
9971
9972 #if TOML_ENABLE_FORMATTERS
9973
9974 TOML_PUSH_WARNINGS;
9975 #ifdef _MSC_VER
9976 #ifndef __clang__
9977 #pragma inline_recursion(on)
9978 #endif
9979 #pragma push_macro("min")
9980 #pragma push_macro("max")
9981 #undef min
9982 #undef max
9983 #endif
9984
9985 TOML_NAMESPACE_START
9986 {
9987 class TOML_EXPORTED_CLASS toml_formatter : impl::formatter
9988 {
9989 private:
9990
9991 using base = impl::formatter;
9992 std::vector<const key*> key_path_;
9993 bool pending_table_separator_ = false;
9994
9995 TOML_EXPORTED_MEMBER_FUNCTION
9996 void print_pending_table_separator();
9997
9998 TOML_EXPORTED_MEMBER_FUNCTION
9999 void print(const key&);
10000
10001 TOML_EXPORTED_MEMBER_FUNCTION
10002 void print_inline(const toml::table&);
10003
10004 TOML_EXPORTED_MEMBER_FUNCTION
10005 void print(const toml::array&);
10006
10007 TOML_EXPORTED_MEMBER_FUNCTION
10008 void print(const toml::table&);
10009
10010 TOML_EXPORTED_MEMBER_FUNCTION
10011 void print();
10012
10013 static constexpr impl::formatter_constants constants = { format_flags::none, // mandatory
10014 format_flags::none, // ignored
10015 "inf"sv,
10016 "-inf"sv,
10017 "nan"sv,
10018 "true"sv,
10019 "false"sv };
10020
10021 public:
10022
10023 static constexpr format_flags default_flags = constants.mandatory_flags //
10024 | format_flags::allow_literal_strings //
10025 | format_flags::allow_multi_line_strings //
10026 | format_flags::allow_unicode_strings //
10027 | format_flags::allow_real_tabs_in_strings //
10028 | format_flags::allow_binary_integers //
10029 | format_flags::allow_octal_integers //
10030 | format_flags::allow_hexadecimal_integers //
10031 | format_flags::indentation;
10032
10033 TOML_NODISCARD_CTOR
10034 explicit toml_formatter(const toml::node& source, format_flags flags = default_flags) noexcept
10035 : base{ &source, nullptr, constants, { flags, " "sv } }
10036 {}
10037
10038 #if TOML_DOXYGEN || (TOML_ENABLE_PARSER && !TOML_EXCEPTIONS)
10039
10040 TOML_NODISCARD_CTOR
10041 explicit toml_formatter(const toml::parse_result& result, format_flags flags = default_flags) noexcept
10042 : base{ nullptr, &result, constants, { flags, " "sv } }
10043 {}
10044
10045 #endif
10046
10047 friend std::ostream& operator<<(std::ostream& lhs, toml_formatter& rhs)
10048 {
10049 rhs.attach(lhs);
10050 rhs.key_path_.clear();
10051 rhs.print();
10052 rhs.detach();
10053 return lhs;
10054 }
10055
10056 friend std::ostream& operator<<(std::ostream& lhs, toml_formatter&& rhs)
10057 {
10058 return lhs << rhs; // as lvalue
10059 }
10060 };
10061 }
10062 TOML_NAMESPACE_END;
10063
10064 #ifdef _MSC_VER
10065 #pragma pop_macro("min")
10066 #pragma pop_macro("max")
10067 #ifndef __clang__
10068 #pragma inline_recursion(off)
10069 #endif
10070 #endif
10071 TOML_POP_WARNINGS;
10072
10073 #endif // TOML_ENABLE_FORMATTERS
10074
10075 //******** impl/json_formatter.hpp ***********************************************************************************
10076
10077 #if TOML_ENABLE_FORMATTERS
10078
10079 TOML_PUSH_WARNINGS;
10080 #ifdef _MSC_VER
10081 #ifndef __clang__
10082 #pragma inline_recursion(on)
10083 #endif
10084 #pragma push_macro("min")
10085 #pragma push_macro("max")
10086 #undef min
10087 #undef max
10088 #endif
10089
10090 TOML_NAMESPACE_START
10091 {
10092 class TOML_EXPORTED_CLASS json_formatter : impl::formatter
10093 {
10094 private:
10095
10096 using base = impl::formatter;
10097
10098 TOML_EXPORTED_MEMBER_FUNCTION
10099 void print(const toml::table&);
10100
10101 TOML_EXPORTED_MEMBER_FUNCTION
10102 void print(const toml::array&);
10103
10104 TOML_EXPORTED_MEMBER_FUNCTION
10105 void print();
10106
10107 static constexpr impl::formatter_constants constants = {
10108 format_flags::quote_dates_and_times, // mandatory
10109 format_flags::allow_literal_strings | format_flags::allow_multi_line_strings, // ignored
10110 "Infinity"sv,
10111 "-Infinity"sv,
10112 "NaN"sv,
10113 "true"sv,
10114 "false"sv
10115 };
10116
10117 public:
10118
10119 static constexpr format_flags default_flags = constants.mandatory_flags //
10120 | format_flags::quote_infinities_and_nans //
10121 | format_flags::allow_unicode_strings //
10122 | format_flags::indentation;
10123
10124 TOML_NODISCARD_CTOR
10125 explicit json_formatter(const toml::node& source, format_flags flags = default_flags) noexcept
10126 : base{ &source, nullptr, constants, { flags, " "sv } }
10127 {}
10128
10129 #if TOML_DOXYGEN || (TOML_ENABLE_PARSER && !TOML_EXCEPTIONS)
10130
10131 TOML_NODISCARD_CTOR
10132 explicit json_formatter(const toml::parse_result& result, format_flags flags = default_flags) noexcept
10133 : base{ nullptr, &result, constants, { flags, " "sv } }
10134 {}
10135
10136 #endif
10137
10138 friend std::ostream& operator<<(std::ostream& lhs, json_formatter& rhs)
10139 {
10140 rhs.attach(lhs);
10141 rhs.print();
10142 rhs.detach();
10143 return lhs;
10144 }
10145
10146 friend std::ostream& operator<<(std::ostream& lhs, json_formatter&& rhs)
10147 {
10148 return lhs << rhs; // as lvalue
10149 }
10150 };
10151 }
10152 TOML_NAMESPACE_END;
10153
10154 #ifdef _MSC_VER
10155 #pragma pop_macro("min")
10156 #pragma pop_macro("max")
10157 #ifndef __clang__
10158 #pragma inline_recursion(off)
10159 #endif
10160 #endif
10161 TOML_POP_WARNINGS;
10162
10163 #endif // TOML_ENABLE_FORMATTERS
10164
10165 //******** impl/yaml_formatter.hpp ***********************************************************************************
10166
10167 #if TOML_ENABLE_FORMATTERS
10168
10169 TOML_PUSH_WARNINGS;
10170 #ifdef _MSC_VER
10171 #ifndef __clang__
10172 #pragma inline_recursion(on)
10173 #endif
10174 #pragma push_macro("min")
10175 #pragma push_macro("max")
10176 #undef min
10177 #undef max
10178 #endif
10179
10180 TOML_NAMESPACE_START
10181 {
10182 class TOML_EXPORTED_CLASS yaml_formatter : impl::formatter
10183 {
10184 private:
10185
10186 using base = impl::formatter;
10187
10188 TOML_EXPORTED_MEMBER_FUNCTION
10189 void print_yaml_string(const value<std::string>&);
10190
10191 TOML_EXPORTED_MEMBER_FUNCTION
10192 void print(const toml::table&, bool = false);
10193
10194 TOML_EXPORTED_MEMBER_FUNCTION
10195 void print(const toml::array&, bool = false);
10196
10197 TOML_EXPORTED_MEMBER_FUNCTION
10198 void print();
10199
10200 static constexpr impl::formatter_constants constants = {
10201 //
10202 format_flags::quote_dates_and_times | format_flags::indentation, // mandatory
10203 format_flags::allow_multi_line_strings, // ignored
10204 ".inf"sv,
10205 "-.inf"sv,
10206 ".NAN"sv,
10207 "true"sv,
10208 "false"sv
10209 };
10210
10211 public:
10212
10213 static constexpr format_flags default_flags = constants.mandatory_flags //
10214 | format_flags::allow_literal_strings //
10215 | format_flags::allow_unicode_strings //
10216 | format_flags::allow_octal_integers //
10217 | format_flags::allow_hexadecimal_integers;
10218
10219 TOML_NODISCARD_CTOR
10220 explicit yaml_formatter(const toml::node& source, format_flags flags = default_flags) noexcept
10221 : base{ &source, nullptr, constants, { flags, " "sv } }
10222 {}
10223
10224 #if TOML_DOXYGEN || (TOML_ENABLE_PARSER && !TOML_EXCEPTIONS)
10225
10226 TOML_NODISCARD_CTOR
10227 explicit yaml_formatter(const toml::parse_result& result, format_flags flags = default_flags) noexcept
10228 : base{ nullptr, &result, constants, { flags, " "sv } }
10229 {}
10230
10231 #endif
10232
10233 friend std::ostream& TOML_CALLCONV operator<<(std::ostream& lhs, yaml_formatter& rhs)
10234 {
10235 rhs.attach(lhs);
10236 rhs.print();
10237 rhs.detach();
10238 return lhs;
10239 }
10240
10241 friend std::ostream& TOML_CALLCONV operator<<(std::ostream& lhs, yaml_formatter&& rhs)
10242 {
10243 return lhs << rhs; // as lvalue
10244 }
10245 };
10246 }
10247 TOML_NAMESPACE_END;
10248
10249 #ifdef _MSC_VER
10250 #pragma pop_macro("min")
10251 #pragma pop_macro("max")
10252 #ifndef __clang__
10253 #pragma inline_recursion(off)
10254 #endif
10255 #endif
10256 TOML_POP_WARNINGS;
10257
10258 #endif // TOML_ENABLE_FORMATTERS
10259
10260 #if TOML_IMPLEMENTATION
10261
10262 //******** impl/std_string.inl ***************************************************************************************
10263
10264 #if TOML_WINDOWS
10265
10266 #ifndef _WINDOWS_
10267 #if TOML_INCLUDE_WINDOWS_H
10268 #include <Windows.h>
10269 #else
10270
10271 extern "C" __declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int CodePage,
10272 unsigned long dwFlags,
10273 const wchar_t* lpWideCharStr,
10274 int cchWideChar,
10275 char* lpMultiByteStr,
10276 int cbMultiByte,
10277 const char* lpDefaultChar,
10278 int* lpUsedDefaultChar);
10279
10280 extern "C" __declspec(dllimport) int __stdcall MultiByteToWideChar(unsigned int CodePage,
10281 unsigned long dwFlags,
10282 const char* lpMultiByteStr,
10283 int cbMultiByte,
10284 wchar_t* lpWideCharStr,
10285 int cchWideChar);
10286
10287 #endif // TOML_INCLUDE_WINDOWS_H
10288 #endif // _WINDOWS_
10289
10290 TOML_PUSH_WARNINGS;
10291 #ifdef _MSC_VER
10292 #ifndef __clang__
10293 #pragma inline_recursion(on)
10294 #endif
10295 #pragma push_macro("min")
10296 #pragma push_macro("max")
10297 #undef min
10298 #undef max
10299 #endif
10300
10301 TOML_IMPL_NAMESPACE_START
10302 {
10303 TOML_EXTERNAL_LINKAGE
10304 std::string narrow(std::wstring_view str)
10305 {
10306 if (str.empty())
10307 return {};
10308
10309 std::string s;
10310 const auto len =
10311 ::WideCharToMultiByte(65001, 0, str.data(), static_cast<int>(str.length()), nullptr, 0, nullptr, nullptr);
10312 if (len)
10313 {
10314 s.resize(static_cast<size_t>(len));
10315 ::WideCharToMultiByte(65001,
10316 0,
10317 str.data(),
10318 static_cast<int>(str.length()),
10319 s.data(),
10320 len,
10321 nullptr,
10322 nullptr);
10323 }
10324 return s;
10325 }
10326
10327 TOML_EXTERNAL_LINKAGE
10328 std::wstring widen(std::string_view str)
10329 {
10330 if (str.empty())
10331 return {};
10332
10333 std::wstring s;
10334 const auto len = ::MultiByteToWideChar(65001, 0, str.data(), static_cast<int>(str.length()), nullptr, 0);
10335 if (len)
10336 {
10337 s.resize(static_cast<size_t>(len));
10338 ::MultiByteToWideChar(65001, 0, str.data(), static_cast<int>(str.length()), s.data(), len);
10339 }
10340 return s;
10341 }
10342
10343 #if TOML_HAS_CHAR8
10344
10345 TOML_EXTERNAL_LINKAGE
10346 std::wstring widen(std::u8string_view str)
10347 {
10348 if (str.empty())
10349 return {};
10350
10351 return widen(std::string_view{ reinterpret_cast<const char*>(str.data()), str.length() });
10352 }
10353
10354 #endif // TOML_HAS_CHAR8
10355 }
10356 TOML_IMPL_NAMESPACE_END;
10357
10358 #ifdef _MSC_VER
10359 #pragma pop_macro("min")
10360 #pragma pop_macro("max")
10361 #ifndef __clang__
10362 #pragma inline_recursion(off)
10363 #endif
10364 #endif
10365 TOML_POP_WARNINGS;
10366
10367 #endif // TOML_WINDOWS
10368
10369 //******** impl/print_to_stream.inl **********************************************************************************
10370
10371 TOML_DISABLE_WARNINGS;
10372 #include <ostream>
10373 #if TOML_INT_CHARCONV || TOML_FLOAT_CHARCONV
10374 #include <charconv>
10375 #endif
10376 #if !TOML_INT_CHARCONV || !TOML_FLOAT_CHARCONV
10377 #include <sstream>
10378 #endif
10379 #if !TOML_INT_CHARCONV
10380 #include <iomanip>
10381 #endif
10382 TOML_ENABLE_WARNINGS;
10383 TOML_PUSH_WARNINGS;
10384 #ifdef _MSC_VER
10385 #ifndef __clang__
10386 #pragma inline_recursion(on)
10387 #endif
10388 #pragma push_macro("min")
10389 #pragma push_macro("max")
10390 #undef min
10391 #undef max
10392 #endif
10393
10394 TOML_ANON_NAMESPACE_START
10395 {
10396 template <typename T>
10397 inline constexpr size_t charconv_buffer_length = 0;
10398
10399 template <>
10400 inline constexpr size_t charconv_buffer_length<int8_t> = 4; // strlen("-128")
10401
10402 template <>
10403 inline constexpr size_t charconv_buffer_length<int16_t> = 6; // strlen("-32768")
10404
10405 template <>
10406 inline constexpr size_t charconv_buffer_length<int32_t> = 11; // strlen("-2147483648")
10407
10408 template <>
10409 inline constexpr size_t charconv_buffer_length<int64_t> = 20; // strlen("-9223372036854775808")
10410
10411 template <>
10412 inline constexpr size_t charconv_buffer_length<uint8_t> = 3; // strlen("255")
10413
10414 template <>
10415 inline constexpr size_t charconv_buffer_length<uint16_t> = 5; // strlen("65535")
10416
10417 template <>
10418 inline constexpr size_t charconv_buffer_length<uint32_t> = 10; // strlen("4294967295")
10419
10420 template <>
10421 inline constexpr size_t charconv_buffer_length<uint64_t> = 20; // strlen("18446744073709551615")
10422
10423 template <>
10424 inline constexpr size_t charconv_buffer_length<float> = 64;
10425
10426 template <>
10427 inline constexpr size_t charconv_buffer_length<double> = 64;
10428
10429 template <typename T>
10430 TOML_INTERNAL_LINKAGE
10431 void print_integer_to_stream(std::ostream & stream, T val, value_flags format = {}, size_t min_digits = 0)
10432 {
10433 if (!val)
10434 {
10435 if (!min_digits)
10436 min_digits = 1;
10437
10438 for (size_t i = 0; i < min_digits; i++)
10439 stream.put('0');
10440
10441 return;
10442 }
10443
10444 static constexpr auto value_flags_mask =
10445 value_flags::format_as_binary | value_flags::format_as_octal | value_flags::format_as_hexadecimal;
10446 format &= value_flags_mask;
10447
10448 int base = 10;
10449 if (format != value_flags::none && val > T{})
10450 {
10451 switch (format)
10452 {
10453 case value_flags::format_as_binary: base = 2; break;
10454 case value_flags::format_as_octal: base = 8; break;
10455 case value_flags::format_as_hexadecimal: base = 16; break;
10456 default: break;
10457 }
10458 }
10459
10460 #if TOML_INT_CHARCONV
10461
10462 char buf[(sizeof(T) * CHAR_BIT)];
10463 const auto res = std::to_chars(buf, buf + sizeof(buf), val, base);
10464 const auto len = static_cast<size_t>(res.ptr - buf);
10465 for (size_t i = len; i < min_digits; i++)
10466 stream.put('0');
10467 if (base == 16)
10468 {
10469 for (size_t i = 0; i < len; i++)
10470 if (buf[i] >= 'a')
10471 buf[i] -= 32;
10472 }
10473 impl::print_to_stream(stream, buf, len);
10474
10475 #else
10476
10477 using unsigned_type = std::conditional_t<(sizeof(T) > sizeof(unsigned)), std::make_unsigned_t<T>, unsigned>;
10478 using cast_type = std::conditional_t<std::is_signed_v<T>, std::make_signed_t<unsigned_type>, unsigned_type>;
10479
10480 if (base == 2)
10481 {
10482 const auto len = sizeof(T) * CHAR_BIT;
10483 for (size_t i = len; i < min_digits; i++)
10484 stream.put('0');
10485
10486 bool found_one = false;
10487 const auto v = static_cast<unsigned_type>(val);
10488 unsigned_type mask = unsigned_type{ 1 } << (len - 1u);
10489 for (size_t i = 0; i < len; i++)
10490 {
10491 if ((v & mask))
10492 {
10493 stream.put('1');
10494 found_one = true;
10495 }
10496 else if (found_one)
10497 stream.put('0');
10498 mask >>= 1;
10499 }
10500 }
10501 else
10502 {
10503 std::ostringstream ss;
10504 ss.imbue(std::locale::classic());
10505 ss << std::uppercase << std::setbase(base);
10506 if (min_digits)
10507 ss << std::setfill('0') << std::setw(static_cast<int>(min_digits));
10508 ss << static_cast<cast_type>(val);
10509 const auto str = std::move(ss).str();
10510 impl::print_to_stream(stream, str);
10511 }
10512
10513 #endif
10514 }
10515
10516 template <typename T>
10517 TOML_INTERNAL_LINKAGE
10518 void print_floating_point_to_stream(std::ostream & stream,
10519 T val,
10520 value_flags format,
10521 [[maybe_unused]] bool relaxed_precision)
10522 {
10523 switch (impl::fpclassify(val))
10524 {
10525 case impl::fp_class::neg_inf: impl::print_to_stream(stream, "-inf"sv); break;
10526
10527 case impl::fp_class::pos_inf: impl::print_to_stream(stream, "inf"sv); break;
10528
10529 case impl::fp_class::nan: impl::print_to_stream(stream, "nan"sv); break;
10530
10531 case impl::fp_class::ok:
10532 {
10533 static constexpr auto needs_decimal_point = [](auto&& s) noexcept
10534 {
10535 for (auto c : s)
10536 if (c == '.' || c == 'E' || c == 'e')
10537 return false;
10538 return true;
10539 };
10540
10541 #if TOML_FLOAT_CHARCONV
10542
10543 const auto hex = !!(format & value_flags::format_as_hexadecimal);
10544 char buf[charconv_buffer_length<T>];
10545 auto res = hex ? std::to_chars(buf, buf + sizeof(buf), val, std::chars_format::hex)
10546 : std::to_chars(buf, buf + sizeof(buf), val);
10547 auto str = std::string_view{ buf, static_cast<size_t>(res.ptr - buf) };
10548
10549 char buf2[charconv_buffer_length<T>];
10550 if (!hex && relaxed_precision)
10551 {
10552 res = std::to_chars(buf2, buf2 + sizeof(buf2), val, std::chars_format::general, 6);
10553 const auto str2 = std::string_view{ buf2, static_cast<size_t>(res.ptr - buf2) };
10554 if (str2.length() < str.length())
10555 str = str2;
10556 }
10557
10558 impl::print_to_stream(stream, str);
10559 if (!hex && needs_decimal_point(str))
10560 toml::impl::print_to_stream(stream, ".0"sv);
10561
10562 #else
10563
10564 std::ostringstream ss;
10565 ss.imbue(std::locale::classic());
10566 if (!relaxed_precision)
10567 ss.precision(std::numeric_limits<T>::max_digits10);
10568 if (!!(format & value_flags::format_as_hexadecimal))
10569 ss << std::hexfloat;
10570 ss << val;
10571 const auto str = std::move(ss).str();
10572 impl::print_to_stream(stream, str);
10573 if (!(format & value_flags::format_as_hexadecimal) && needs_decimal_point(str))
10574 impl::print_to_stream(stream, ".0"sv);
10575
10576 #endif
10577 }
10578 break;
10579
10580 default: TOML_UNREACHABLE;
10581 }
10582 }
10583 }
10584 TOML_ANON_NAMESPACE_END;
10585
10586 TOML_IMPL_NAMESPACE_START
10587 {
10588 TOML_EXTERNAL_LINKAGE
10589 TOML_ATTR(nonnull)
10590 void TOML_CALLCONV print_to_stream(std::ostream & stream, const char* val, size_t len)
10591 {
10592 stream.write(val, static_cast<std::streamsize>(len));
10593 }
10594
10595 TOML_EXTERNAL_LINKAGE
10596 void TOML_CALLCONV print_to_stream(std::ostream & stream, std::string_view val)
10597 {
10598 stream.write(val.data(), static_cast<std::streamsize>(val.length()));
10599 }
10600
10601 TOML_EXTERNAL_LINKAGE
10602 void TOML_CALLCONV print_to_stream(std::ostream & stream, const std::string& val)
10603 {
10604 stream.write(val.data(), static_cast<std::streamsize>(val.length()));
10605 }
10606
10607 TOML_EXTERNAL_LINKAGE
10608 void TOML_CALLCONV print_to_stream(std::ostream & stream, char val)
10609 {
10610 stream.put(val);
10611 }
10612
10613 TOML_EXTERNAL_LINKAGE
10614 void TOML_CALLCONV print_to_stream(std::ostream & stream, signed char val, value_flags format, size_t min_digits)
10615 {
10616 TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
10617 }
10618
10619 TOML_EXTERNAL_LINKAGE
10620 void TOML_CALLCONV print_to_stream(std::ostream & stream, signed short val, value_flags format, size_t min_digits)
10621 {
10622 TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
10623 }
10624
10625 TOML_EXTERNAL_LINKAGE
10626 void TOML_CALLCONV print_to_stream(std::ostream & stream, signed int val, value_flags format, size_t min_digits)
10627 {
10628 TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
10629 }
10630
10631 TOML_EXTERNAL_LINKAGE
10632 void TOML_CALLCONV print_to_stream(std::ostream & stream, signed long val, value_flags format, size_t min_digits)
10633 {
10634 TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
10635 }
10636
10637 TOML_EXTERNAL_LINKAGE
10638 void TOML_CALLCONV print_to_stream(std::ostream & stream,
10639 signed long long val,
10640 value_flags format,
10641 size_t min_digits)
10642 {
10643 TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
10644 }
10645
10646 TOML_EXTERNAL_LINKAGE
10647 void TOML_CALLCONV print_to_stream(std::ostream & stream, unsigned char val, value_flags format, size_t min_digits)
10648 {
10649 TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
10650 }
10651
10652 TOML_EXTERNAL_LINKAGE
10653 void TOML_CALLCONV print_to_stream(std::ostream & stream, unsigned short val, value_flags format, size_t min_digits)
10654 {
10655 TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
10656 }
10657
10658 TOML_EXTERNAL_LINKAGE
10659 void TOML_CALLCONV print_to_stream(std::ostream & stream, unsigned int val, value_flags format, size_t min_digits)
10660 {
10661 TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
10662 }
10663
10664 TOML_EXTERNAL_LINKAGE
10665 void TOML_CALLCONV print_to_stream(std::ostream & stream, unsigned long val, value_flags format, size_t min_digits)
10666 {
10667 TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
10668 }
10669
10670 TOML_EXTERNAL_LINKAGE
10671 void TOML_CALLCONV print_to_stream(std::ostream & stream,
10672 unsigned long long val,
10673 value_flags format,
10674 size_t min_digits)
10675 {
10676 TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
10677 }
10678
10679 TOML_EXTERNAL_LINKAGE
10680 void TOML_CALLCONV print_to_stream(std::ostream & stream, float val, value_flags format, bool relaxed_precision)
10681 {
10682 TOML_ANON_NAMESPACE::print_floating_point_to_stream(stream, val, format, relaxed_precision);
10683 }
10684
10685 TOML_EXTERNAL_LINKAGE
10686 void TOML_CALLCONV print_to_stream(std::ostream & stream, double val, value_flags format, bool relaxed_precision)
10687 {
10688 TOML_ANON_NAMESPACE::print_floating_point_to_stream(stream, val, format, relaxed_precision);
10689 }
10690
10691 TOML_EXTERNAL_LINKAGE
10692 void TOML_CALLCONV print_to_stream(std::ostream & stream, bool val)
10693 {
10694 print_to_stream(stream, val ? "true"sv : "false"sv);
10695 }
10696
10697 TOML_EXTERNAL_LINKAGE
10698 void TOML_CALLCONV print_to_stream(std::ostream & stream, const toml::date& val)
10699 {
10700 print_to_stream(stream, val.year, {}, 4);
10701 stream.put('-');
10702 print_to_stream(stream, val.month, {}, 2);
10703 stream.put('-');
10704 print_to_stream(stream, val.day, {}, 2);
10705 }
10706
10707 TOML_EXTERNAL_LINKAGE
10708 void TOML_CALLCONV print_to_stream(std::ostream & stream, const toml::time& val)
10709 {
10710 print_to_stream(stream, val.hour, {}, 2);
10711 stream.put(':');
10712 print_to_stream(stream, val.minute, {}, 2);
10713 stream.put(':');
10714 print_to_stream(stream, val.second, {}, 2);
10715 if (val.nanosecond && val.nanosecond <= 999999999u)
10716 {
10717 stream.put('.');
10718 auto ns = val.nanosecond;
10719 size_t digits = 9u;
10720 while (ns % 10u == 0u)
10721 {
10722 ns /= 10u;
10723 digits--;
10724 }
10725 print_to_stream(stream, ns, {}, digits);
10726 }
10727 }
10728
10729 TOML_EXTERNAL_LINKAGE
10730 void TOML_CALLCONV print_to_stream(std::ostream & stream, const toml::time_offset& val)
10731 {
10732 if (!val.minutes)
10733 {
10734 stream.put('Z');
10735 return;
10736 }
10737
10738 auto mins = static_cast<int>(val.minutes);
10739 if (mins < 0)
10740 {
10741 stream.put('-');
10742 mins = -mins;
10743 }
10744 else
10745 stream.put('+');
10746 const auto hours = mins / 60;
10747 if (hours)
10748 {
10749 print_to_stream(stream, static_cast<unsigned int>(hours), {}, 2);
10750 mins -= hours * 60;
10751 }
10752 else
10753 print_to_stream(stream, "00"sv);
10754 stream.put(':');
10755 print_to_stream(stream, static_cast<unsigned int>(mins), {}, 2);
10756 }
10757
10758 TOML_EXTERNAL_LINKAGE
10759 void TOML_CALLCONV print_to_stream(std::ostream & stream, const toml::date_time& val)
10760 {
10761 print_to_stream(stream, val.date);
10762 stream.put('T');
10763 print_to_stream(stream, val.time);
10764 if (val.offset)
10765 print_to_stream(stream, *val.offset);
10766 }
10767
10768 TOML_EXTERNAL_LINKAGE
10769 void TOML_CALLCONV print_to_stream(std::ostream & stream, const source_position& val)
10770 {
10771 print_to_stream(stream, "line "sv);
10772 print_to_stream(stream, val.line);
10773 print_to_stream(stream, ", column "sv);
10774 print_to_stream(stream, val.column);
10775 }
10776
10777 TOML_EXTERNAL_LINKAGE
10778 void TOML_CALLCONV print_to_stream(std::ostream & stream, const source_region& val)
10779 {
10780 print_to_stream(stream, val.begin);
10781 if (val.begin != val.end)
10782 {
10783 print_to_stream(stream, " to "sv);
10784 print_to_stream(stream, val.end);
10785 }
10786 if (val.path)
10787 {
10788 print_to_stream(stream, " of '"sv);
10789 print_to_stream(stream, *val.path);
10790 stream.put('\'');
10791 }
10792 }
10793
10794 #if TOML_ENABLE_FORMATTERS
10795
10796 TOML_EXTERNAL_LINKAGE
10797 void TOML_CALLCONV print_to_stream(std::ostream & stream, const array& arr)
10798 {
10799 stream << toml_formatter{ arr };
10800 }
10801
10802 TOML_EXTERNAL_LINKAGE
10803 void TOML_CALLCONV print_to_stream(std::ostream & stream, const table& tbl)
10804 {
10805 stream << toml_formatter{ tbl };
10806 }
10807
10808 TOML_EXTERNAL_LINKAGE
10809 void TOML_CALLCONV print_to_stream(std::ostream & stream, const value<std::string>& val)
10810 {
10811 stream << toml_formatter{ val };
10812 }
10813
10814 TOML_EXTERNAL_LINKAGE
10815 void TOML_CALLCONV print_to_stream(std::ostream & stream, const value<int64_t>& val)
10816 {
10817 stream << toml_formatter{ val };
10818 }
10819
10820 TOML_EXTERNAL_LINKAGE
10821 void TOML_CALLCONV print_to_stream(std::ostream & stream, const value<double>& val)
10822 {
10823 stream << toml_formatter{ val };
10824 }
10825
10826 TOML_EXTERNAL_LINKAGE
10827 void TOML_CALLCONV print_to_stream(std::ostream & stream, const value<bool>& val)
10828 {
10829 stream << toml_formatter{ val };
10830 }
10831
10832 TOML_EXTERNAL_LINKAGE
10833 void TOML_CALLCONV print_to_stream(std::ostream & stream, const value<date>& val)
10834 {
10835 stream << toml_formatter{ val };
10836 }
10837
10838 TOML_EXTERNAL_LINKAGE
10839 void TOML_CALLCONV print_to_stream(std::ostream & stream, const value<time>& val)
10840 {
10841 stream << toml_formatter{ val };
10842 }
10843
10844 TOML_EXTERNAL_LINKAGE
10845 void TOML_CALLCONV print_to_stream(std::ostream & stream, const value<date_time>& val)
10846 {
10847 stream << toml_formatter{ val };
10848 }
10849
10850 #endif
10851 }
10852 TOML_IMPL_NAMESPACE_END;
10853
10854 #ifdef _MSC_VER
10855 #pragma pop_macro("min")
10856 #pragma pop_macro("max")
10857 #ifndef __clang__
10858 #pragma inline_recursion(off)
10859 #endif
10860 #endif
10861 TOML_POP_WARNINGS;
10862
10863 //******** impl/node.inl *********************************************************************************************
10864
10865 TOML_PUSH_WARNINGS;
10866 #ifdef _MSC_VER
10867 #ifndef __clang__
10868 #pragma inline_recursion(on)
10869 #endif
10870 #pragma push_macro("min")
10871 #pragma push_macro("max")
10872 #undef min
10873 #undef max
10874 #endif
10875
10876 TOML_NAMESPACE_START
10877 {
10878 TOML_EXTERNAL_LINKAGE
10879 node::node() noexcept = default;
10880
10881 TOML_EXTERNAL_LINKAGE
10882 node::~node() noexcept = default;
10883
10884 TOML_EXTERNAL_LINKAGE
10885 node::node(node && other) noexcept //
10886 : source_{ std::exchange(other.source_, {}) }
10887 {}
10888
10889 TOML_EXTERNAL_LINKAGE
10890 node::node(const node& /*other*/) noexcept
10891 {
10892 // does not copy source information - this is not an error
10893 //
10894 // see https://github.com/marzer/tomlplusplus/issues/49#issuecomment-665089577
10895 }
10896
10897 TOML_EXTERNAL_LINKAGE
10898 node& node::operator=(const node& /*rhs*/) noexcept
10899 {
10900 // does not copy source information - this is not an error
10901 //
10902 // see https://github.com/marzer/tomlplusplus/issues/49#issuecomment-665089577
10903
10904 source_ = {};
10905 return *this;
10906 }
10907
10908 TOML_EXTERNAL_LINKAGE
10909 node& node::operator=(node&& rhs) noexcept
10910 {
10911 if (&rhs != this)
10912 source_ = std::exchange(rhs.source_, {});
10913 return *this;
10914 }
10915
10916 TOML_EXTERNAL_LINKAGE
10917 node_view<node> node::at_path(std::string_view path) noexcept
10918 {
10919 return toml::at_path(*this, path);
10920 }
10921
10922 TOML_EXTERNAL_LINKAGE
10923 node_view<const node> node::at_path(std::string_view path) const noexcept
10924 {
10925 return toml::at_path(*this, path);
10926 }
10927
10928 TOML_EXTERNAL_LINKAGE
10929 node_view<node> node::at_path(const path& p) noexcept
10930 {
10931 return toml::at_path(*this, p);
10932 }
10933
10934 TOML_EXTERNAL_LINKAGE
10935 node_view<const node> node::at_path(const path& p) const noexcept
10936 {
10937 return toml::at_path(*this, p);
10938 }
10939
10940 #if TOML_ENABLE_WINDOWS_COMPAT
10941
10942 TOML_EXTERNAL_LINKAGE
10943 node_view<node> node::at_path(std::wstring_view path)
10944 {
10945 return toml::at_path(*this, path);
10946 }
10947
10948 TOML_EXTERNAL_LINKAGE
10949 node_view<const node> node::at_path(std::wstring_view path) const
10950 {
10951 return toml::at_path(*this, path);
10952 }
10953
10954 #endif // TOML_ENABLE_WINDOWS_COMPAT
10955
10956 TOML_EXTERNAL_LINKAGE
10957 node_view<node> node::operator[](const path& p) noexcept
10958 {
10959 return toml::at_path(*this, p);
10960 }
10961
10962 TOML_EXTERNAL_LINKAGE
10963 node_view<const node> node::operator[](const path& p) const noexcept
10964 {
10965 return toml::at_path(*this, p);
10966 }
10967 }
10968 TOML_NAMESPACE_END;
10969
10970 TOML_IMPL_NAMESPACE_START
10971 {
10972 TOML_PURE_GETTER
10973 TOML_EXTERNAL_LINKAGE
10974 bool TOML_CALLCONV node_deep_equality(const node* lhs, const node* rhs) noexcept
10975 {
10976 // both same or both null
10977 if (lhs == rhs)
10978 return true;
10979
10980 // lhs null != rhs null or different types
10981 if ((!lhs != !rhs) || lhs->type() != rhs->type())
10982 return false;
10983
10984 return lhs->visit(
10985 [=](auto& l) noexcept
10986 {
10987 using concrete_type = remove_cvref<decltype(l)>;
10988
10989 return l == *(rhs->as<concrete_type>());
10990 });
10991 }
10992 }
10993 TOML_IMPL_NAMESPACE_END;
10994
10995 #ifdef _MSC_VER
10996 #pragma pop_macro("min")
10997 #pragma pop_macro("max")
10998 #ifndef __clang__
10999 #pragma inline_recursion(off)
11000 #endif
11001 #endif
11002 TOML_POP_WARNINGS;
11003
11004 //******** impl/at_path.inl ******************************************************************************************
11005
11006 TOML_DISABLE_WARNINGS;
11007 #if TOML_INT_CHARCONV
11008 #include <charconv>
11009 #else
11010 #include <sstream>
11011 #endif
11012 TOML_ENABLE_WARNINGS;
11013 TOML_PUSH_WARNINGS;
11014 #ifdef _MSC_VER
11015 #ifndef __clang__
11016 #pragma inline_recursion(on)
11017 #endif
11018 #pragma push_macro("min")
11019 #pragma push_macro("max")
11020 #undef min
11021 #undef max
11022 #endif
11023
11024 TOML_IMPL_NAMESPACE_START
11025 {
11026 TOML_EXTERNAL_LINKAGE
11027 bool TOML_CALLCONV parse_path(const std::string_view path,
11028 void* const data,
11029 const parse_path_callback<std::string_view> on_key,
11030 const parse_path_callback<size_t> on_index)
11031 {
11032 // a blank string is a valid path; it's just one component representing the "" key
11033 if (path.empty())
11034 return on_key(data, ""sv);
11035
11036 size_t pos = 0;
11037 const auto end = path.length();
11038 bool prev_was_array_indexer = false;
11039 bool prev_was_dot = true; // invisible root 'dot'
11040
11041 while (pos < end)
11042 {
11043 // start of an array indexer
11044 if (path[pos] == '[')
11045 {
11046 // find first digit in index
11047 size_t index_start = pos + 1u;
11048 while (true)
11049 {
11050 if TOML_UNLIKELY(index_start >= path.length())
11051 return false;
11052
11053 const auto c = path[index_start];
11054 if TOML_LIKELY(c >= '0' && c <= '9')
11055 break;
11056 else if (c == ' ' || c == '\t')
11057 index_start++;
11058 else
11059 return false;
11060 }
11061 TOML_ASSERT(path[index_start] >= '0');
11062 TOML_ASSERT(path[index_start] <= '9');
11063
11064 // find end of index (first non-digit character)
11065 size_t index_end = index_start + 1u;
11066 while (true)
11067 {
11068 // if an array indexer is missing the trailing ']' at the end of the string, permissively accept it
11069 if TOML_UNLIKELY(index_end >= path.length())
11070 break;
11071
11072 const auto c = path[index_end];
11073 if (c >= '0' && c <= '9')
11074 index_end++;
11075 else if (c == ']' || c == ' ' || c == '\t' || c == '.' || c == '[')
11076 break;
11077 else
11078 return false;
11079 }
11080 TOML_ASSERT(path[index_end - 1u] >= '0');
11081 TOML_ASSERT(path[index_end - 1u] <= '9');
11082
11083 // move pos to after indexer (char after closing ']' or permissively EOL/subkey '.'/next opening '[')
11084 pos = index_end;
11085 while (true)
11086 {
11087 if TOML_UNLIKELY(pos >= path.length())
11088 break;
11089
11090 const auto c = path[pos];
11091 if (c == ']')
11092 {
11093 pos++;
11094 break;
11095 }
11096 else if TOML_UNLIKELY(c == '.' || c == '[')
11097 break;
11098 else if (c == '\t' || c == ' ')
11099 pos++;
11100 else
11101 return false;
11102 }
11103
11104 // get array index substring
11105 auto index_str = path.substr(index_start, index_end - index_start);
11106
11107 // parse the actual array index to an integer type
11108 size_t index;
11109 if (index_str.length() == 1u)
11110 index = static_cast<size_t>(index_str[0] - '0');
11111 else
11112 {
11113 #if TOML_INT_CHARCONV
11114
11115 auto fc_result = std::from_chars(index_str.data(), index_str.data() + index_str.length(), index);
11116 if (fc_result.ec != std::errc{})
11117 return false;
11118
11119 #else
11120
11121 std::stringstream ss;
11122 ss.imbue(std::locale::classic());
11123 ss.write(index_str.data(), static_cast<std::streamsize>(index_str.length()));
11124 if (!(ss >> index))
11125 return false;
11126
11127 #endif
11128 }
11129
11130 prev_was_dot = false;
11131 prev_was_array_indexer = true;
11132
11133 if (!on_index(data, index))
11134 return false;
11135 }
11136
11137 // start of a new table child
11138 else if (path[pos] == '.')
11139 {
11140 // a dot immediately following another dot (or at the beginning of the string) is as if we'd asked
11141 // for an empty child in between, e.g.
11142 //
11143 // foo..bar
11144 //
11145 // is equivalent to
11146 //
11147 // "foo".""."bar"
11148 //
11149 if (prev_was_dot && !on_key(data, ""sv))
11150 return false;
11151
11152 pos++;
11153 prev_was_dot = true;
11154 prev_was_array_indexer = false;
11155 }
11156
11157 // an errant closing ']'
11158 else if TOML_UNLIKELY(path[pos] == ']')
11159 return false;
11160
11161 // some regular subkey
11162 else
11163 {
11164 const auto subkey_start = pos;
11165 const auto subkey_len =
11166 impl::min(path.find_first_of(".[]"sv, subkey_start + 1u), path.length()) - subkey_start;
11167 const auto subkey = path.substr(subkey_start, subkey_len);
11168
11169 // a regular subkey segment immediately after an array indexer is OK if it was all whitespace, e.g.:
11170 //
11171 // "foo[0] .bar"
11172 // ^^ skip this
11173 //
11174 // otherwise its an error (since it would have to be preceeded by a dot)
11175 if (prev_was_array_indexer)
11176 {
11177 auto non_ws = subkey.find_first_not_of(" \t");
11178 if (non_ws == std::string_view::npos)
11179 {
11180 pos += subkey_len;
11181 prev_was_dot = false;
11182 prev_was_array_indexer = false;
11183 continue;
11184 }
11185 else
11186 return false;
11187 }
11188
11189 pos += subkey_len;
11190 prev_was_dot = false;
11191 prev_was_array_indexer = false;
11192
11193 if (!on_key(data, subkey))
11194 return false;
11195 }
11196 }
11197
11198 // Last character was a '.', which implies an empty string key at the end of the path
11199 if (prev_was_dot && !on_key(data, ""sv))
11200 return false;
11201
11202 return true;
11203 }
11204 }
11205 TOML_IMPL_NAMESPACE_END;
11206
11207 TOML_NAMESPACE_START
11208 {
11209 TOML_EXTERNAL_LINKAGE
11210 node_view<node> TOML_CALLCONV at_path(node & root, std::string_view path) noexcept
11211 {
11212 // early-exit sanity-checks
11213 if (root.is_value())
11214 return {};
11215 if (auto tbl = root.as_table(); tbl && tbl->empty())
11216 return {};
11217 if (auto arr = root.as_array(); arr && arr->empty())
11218 return {};
11219
11220 node* current = &root;
11221
11222 static constexpr auto on_key = [](void* data, std::string_view key) noexcept -> bool
11223 {
11224 auto& curr = *static_cast<node**>(data);
11225 TOML_ASSERT_ASSUME(curr);
11226
11227 const auto current_table = curr->as<table>();
11228 if (!current_table)
11229 return false;
11230
11231 curr = current_table->get(key);
11232 return curr != nullptr;
11233 };
11234
11235 static constexpr auto on_index = [](void* data, size_t index) noexcept -> bool
11236 {
11237 auto& curr = *static_cast<node**>(data);
11238 TOML_ASSERT_ASSUME(curr);
11239
11240 const auto current_array = curr->as<array>();
11241 if (!current_array)
11242 return false;
11243
11244 curr = current_array->get(index);
11245 return curr != nullptr;
11246 };
11247
11248 if (!impl::parse_path(path, &current, on_key, on_index))
11249 current = nullptr;
11250
11251 return node_view{ current };
11252 }
11253
11254 TOML_EXTERNAL_LINKAGE
11255 node_view<const node> TOML_CALLCONV at_path(const node& root, std::string_view path) noexcept
11256 {
11257 return node_view<const node>{ at_path(const_cast<node&>(root), path).node() };
11258 }
11259
11260 #if TOML_ENABLE_WINDOWS_COMPAT
11261
11262 TOML_EXTERNAL_LINKAGE
11263 node_view<node> TOML_CALLCONV at_path(node & root, std::wstring_view path)
11264 {
11265 // these are the same top-level checks from the narrow-string version;
11266 // they're hoisted up here to avoid doing the wide -> narrow conversion where it would not be necessary
11267 // (avoids an allocation)
11268 if (root.is_value())
11269 return {};
11270 if (auto tbl = root.as_table(); tbl && tbl->empty())
11271 return {};
11272 if (auto arr = root.as_array(); arr && arr->empty())
11273 return {};
11274
11275 return at_path(root, impl::narrow(path));
11276 }
11277
11278 TOML_EXTERNAL_LINKAGE
11279 node_view<const node> TOML_CALLCONV at_path(const node& root, std::wstring_view path)
11280 {
11281 return node_view<const node>{ at_path(const_cast<node&>(root), path).node() };
11282 }
11283
11284 #endif // TOML_ENABLE_WINDOWS_COMPAT
11285 }
11286 TOML_NAMESPACE_END;
11287
11288 #ifdef _MSC_VER
11289 #pragma pop_macro("min")
11290 #pragma pop_macro("max")
11291 #ifndef __clang__
11292 #pragma inline_recursion(off)
11293 #endif
11294 #endif
11295 TOML_POP_WARNINGS;
11296
11297 //******** impl/path.inl *********************************************************************************************
11298
11299 TOML_DISABLE_WARNINGS;
11300 #if TOML_INT_CHARCONV
11301 #include <charconv>
11302 #endif
11303 #include <sstream>
11304 TOML_ENABLE_WARNINGS;
11305 TOML_PUSH_WARNINGS;
11306 #ifdef _MSC_VER
11307 #ifndef __clang__
11308 #pragma inline_recursion(on)
11309 #endif
11310 #pragma push_macro("min")
11311 #pragma push_macro("max")
11312 #undef min
11313 #undef max
11314 #endif
11315
11316 TOML_NAMESPACE_START
11317 {
11318 TOML_EXTERNAL_LINKAGE
11319 path_component::path_component() //
11320 : type_{ path_component_type::key }
11321 {
11322 store_key("", value_storage_);
11323 }
11324
11325 TOML_EXTERNAL_LINKAGE
11326 path_component::path_component(size_t index) noexcept //
11327 : type_(path_component_type::array_index)
11328 {
11329 store_index(index, value_storage_);
11330 }
11331
11332 TOML_EXTERNAL_LINKAGE
11333 path_component::path_component(std::string_view key) //
11334 : type_(path_component_type::key)
11335 {
11336 store_key(key, value_storage_);
11337 }
11338
11339 #if TOML_ENABLE_WINDOWS_COMPAT
11340
11341 TOML_EXTERNAL_LINKAGE
11342 path_component::path_component(std::wstring_view key) //
11343 : path_component(impl::narrow(key))
11344 {}
11345
11346 #endif
11347
11348 TOML_EXTERNAL_LINKAGE
11349 path_component::path_component(const path_component& pc) //
11350 : type_{ pc.type_ }
11351 {
11352 if (type_ == path_component_type::array_index)
11353 store_index(pc.index(), value_storage_);
11354 else
11355 store_key(pc.key(), value_storage_);
11356 }
11357
11358 TOML_EXTERNAL_LINKAGE
11359 path_component::path_component(path_component && pc) noexcept //
11360 : type_{ pc.type_ }
11361 {
11362 if (type_ == path_component_type::array_index)
11363 store_index(pc.index_ref(), value_storage_);
11364 else
11365 store_key(std::move(pc.key_ref()), value_storage_);
11366 }
11367
11368 TOML_EXTERNAL_LINKAGE
11369 path_component& path_component::operator=(const path_component& rhs)
11370 {
11371 if (type_ != rhs.type_)
11372 {
11373 destroy();
11374
11375 type_ = rhs.type_;
11376 if (type_ == path_component_type::array_index)
11377 store_index(rhs.index(), value_storage_);
11378 else
11379 store_key(rhs.key(), value_storage_);
11380 }
11381 else
11382 {
11383 if (type_ == path_component_type::array_index)
11384 index_ref() = rhs.index();
11385 else
11386 key_ref() = rhs.key();
11387 }
11388 return *this;
11389 }
11390
11391 TOML_EXTERNAL_LINKAGE
11392 path_component& path_component::operator=(path_component&& rhs) noexcept
11393 {
11394 if (type_ != rhs.type_)
11395 {
11396 destroy();
11397
11398 type_ = rhs.type_;
11399 if (type_ == path_component_type::array_index)
11400 store_index(rhs.index(), value_storage_);
11401 else
11402 store_key(std::move(rhs.key_ref()), value_storage_);
11403 }
11404 else
11405 {
11406 if (type_ == path_component_type::array_index)
11407 index_ref() = rhs.index();
11408 else
11409 key_ref() = std::move(rhs.key_ref());
11410 }
11411 return *this;
11412 }
11413
11414 TOML_PURE_GETTER
11415 TOML_EXTERNAL_LINKAGE
11416 bool TOML_CALLCONV path_component::equal(const path_component& lhs, const path_component& rhs) noexcept
11417 {
11418 // Different comparison depending on contents
11419 if (lhs.type_ != rhs.type_)
11420 return false;
11421
11422 if (lhs.type_ == path_component_type::array_index)
11423 return lhs.index() == rhs.index();
11424 else // path_component_type::key
11425 return lhs.key() == rhs.key();
11426 }
11427
11428 TOML_EXTERNAL_LINKAGE
11429 path_component& path_component::operator=(size_t new_index) noexcept
11430 {
11431 // If currently a key, string will need to be destroyed regardless
11432 destroy();
11433
11434 type_ = path_component_type::array_index;
11435 store_index(new_index, value_storage_);
11436
11437 return *this;
11438 }
11439
11440 TOML_EXTERNAL_LINKAGE
11441 path_component& path_component::operator=(std::string_view new_key)
11442 {
11443 if (type_ == path_component_type::key)
11444 key_ref() = new_key;
11445 else
11446 {
11447 type_ = path_component_type::key;
11448 store_key(new_key, value_storage_);
11449 }
11450
11451 return *this;
11452 }
11453
11454 #if TOML_ENABLE_WINDOWS_COMPAT
11455
11456 TOML_EXTERNAL_LINKAGE
11457 path_component& path_component::operator=(std::wstring_view new_key)
11458 {
11459 if (type_ == path_component_type::key)
11460 key_ref() = impl::narrow(new_key);
11461 else
11462 {
11463 type_ = path_component_type::key;
11464 store_key(impl::narrow(new_key), value_storage_);
11465 }
11466
11467 return *this;
11468 }
11469
11470 #endif
11471 }
11472 TOML_NAMESPACE_END;
11473
11474 TOML_ANON_NAMESPACE_START
11475 {
11476 TOML_INTERNAL_LINKAGE
11477 bool parse_path_into(std::string_view path_str, std::vector<path_component> & components)
11478 {
11479 using components_type = std::remove_reference_t<decltype(components)>;
11480
11481 const auto original_size = components.size();
11482
11483 static constexpr auto on_key = [](void* data, std::string_view key) -> bool
11484 {
11485 auto& comps = *static_cast<components_type*>(data);
11486 comps.emplace_back(key);
11487 return true;
11488 };
11489
11490 static constexpr auto on_index = [](void* data, size_t index) -> bool
11491 {
11492 auto& comps = *static_cast<components_type*>(data);
11493 comps.emplace_back(index);
11494 return true;
11495 };
11496
11497 if (!impl::parse_path(path_str, &components, on_key, on_index))
11498 {
11499 components.resize(original_size);
11500 return false;
11501 }
11502
11503 return true;
11504 }
11505 }
11506 TOML_ANON_NAMESPACE_END;
11507
11508 TOML_NAMESPACE_START
11509 {
11510 TOML_EXTERNAL_LINKAGE
11511 void path::print_to(std::ostream & os) const
11512 {
11513 bool root = true;
11514 for (const auto& component : components_)
11515 {
11516 if (component.type() == path_component_type::key) // key
11517 {
11518 if (!root)
11519 impl::print_to_stream(os, '.');
11520 impl::print_to_stream(os, component.key());
11521 }
11522 else if (component.type() == path_component_type::array_index) // array
11523 {
11524 impl::print_to_stream(os, '[');
11525 impl::print_to_stream(os, component.index());
11526 impl::print_to_stream(os, ']');
11527 }
11528 root = false;
11529 }
11530 }
11531
11532 TOML_PURE_GETTER
11533 TOML_EXTERNAL_LINKAGE
11534 bool TOML_CALLCONV path::equal(const path& lhs, const path& rhs) noexcept
11535 {
11536 return lhs.components_ == rhs.components_;
11537 }
11538
11539 TOML_EXTERNAL_LINKAGE
11540 path::path(std::string_view str) //
11541 {
11542 TOML_ANON_NAMESPACE::parse_path_into(str, components_);
11543 }
11544
11545 #if TOML_ENABLE_WINDOWS_COMPAT
11546
11547 TOML_EXTERNAL_LINKAGE
11548 path::path(std::wstring_view str) //
11549 : path(impl::narrow(str))
11550 {}
11551
11552 #endif
11553
11554 TOML_EXTERNAL_LINKAGE
11555 path& path::operator=(std::string_view rhs)
11556 {
11557 components_.clear();
11558 TOML_ANON_NAMESPACE::parse_path_into(rhs, components_);
11559 return *this;
11560 }
11561
11562 #if TOML_ENABLE_WINDOWS_COMPAT
11563
11564 TOML_EXTERNAL_LINKAGE
11565 path& path::operator=(std::wstring_view rhs)
11566 {
11567 return assign(impl::narrow(rhs));
11568 }
11569
11570 #endif
11571
11572 TOML_EXTERNAL_LINKAGE
11573 path& path::operator+=(const path& rhs)
11574 {
11575 components_.insert(components_.cend(), rhs.begin(), rhs.end());
11576 return *this;
11577 }
11578
11579 TOML_EXTERNAL_LINKAGE
11580 path& path::operator+=(path&& rhs)
11581 {
11582 components_.insert(components_.end(),
11583 std::make_move_iterator(rhs.components_.begin()),
11584 std::make_move_iterator(rhs.components_.end()));
11585 return *this;
11586 }
11587
11588 TOML_EXTERNAL_LINKAGE
11589 path& path::operator+=(std::string_view str)
11590 {
11591 TOML_ANON_NAMESPACE::parse_path_into(str, components_);
11592 return *this;
11593 }
11594
11595 #if TOML_ENABLE_WINDOWS_COMPAT
11596
11597 TOML_EXTERNAL_LINKAGE
11598 path& path::operator+=(std::wstring_view str)
11599 {
11600 return *this += impl::narrow(str);
11601 }
11602
11603 #endif
11604
11605 TOML_EXTERNAL_LINKAGE
11606 path& path::prepend(const path& source)
11607 {
11608 components_.insert(components_.begin(), source.components_.begin(), source.components_.end());
11609 return *this;
11610 }
11611
11612 TOML_EXTERNAL_LINKAGE
11613 path& path::prepend(path && source)
11614 {
11615 components_.insert(components_.begin(),
11616 std::make_move_iterator(source.components_.begin()),
11617 std::make_move_iterator(source.components_.end()));
11618 return *this;
11619 }
11620
11621 TOML_EXTERNAL_LINKAGE
11622 path& path::prepend(std::string_view source)
11623 {
11624 return prepend(path{ source });
11625 }
11626
11627 #if TOML_ENABLE_WINDOWS_COMPAT
11628
11629 TOML_EXTERNAL_LINKAGE
11630 path& path::prepend(std::wstring_view source)
11631 {
11632 return prepend(impl::narrow(source));
11633 }
11634
11635 #endif
11636
11637 TOML_EXTERNAL_LINKAGE
11638 std::string path::str() const
11639 {
11640 if (empty())
11641 return "";
11642
11643 std::ostringstream ss;
11644 print_to(ss);
11645 return std::move(ss).str();
11646 }
11647
11648 #if TOML_ENABLE_WINDOWS_COMPAT
11649
11650 TOML_EXTERNAL_LINKAGE
11651 std::wstring path::wide_str() const
11652 {
11653 return impl::widen(str());
11654 }
11655
11656 #endif
11657
11658 TOML_EXTERNAL_LINKAGE
11659 void path::clear() noexcept
11660 {
11661 components_.clear();
11662 }
11663
11664 TOML_EXTERNAL_LINKAGE
11665 path& path::truncate(size_t n)
11666 {
11667 n = n > components_.size() ? components_.size() : n;
11668
11669 auto it_end = components_.end();
11670 components_.erase(it_end - static_cast<int>(n), it_end);
11671
11672 return *this;
11673 }
11674
11675 TOML_EXTERNAL_LINKAGE
11676 path path::truncated(size_t n) const
11677 {
11678 path truncated_path{};
11679
11680 n = n > components_.size() ? components_.size() : n;
11681
11682 // Copy all components except one
11683 // Need at least two path components to have a parent, since if there is
11684 // only one path component, the parent is the root/null path ""
11685 truncated_path.components_.insert(truncated_path.components_.begin(),
11686 components_.begin(),
11687 components_.end() - static_cast<int>(n));
11688
11689 return truncated_path;
11690 }
11691
11692 TOML_EXTERNAL_LINKAGE
11693 path path::parent() const
11694 {
11695 return truncated(1);
11696 }
11697
11698 TOML_EXTERNAL_LINKAGE
11699 path path::leaf(size_t n) const
11700 {
11701 path leaf_path{};
11702
11703 n = n > components_.size() ? components_.size() : n;
11704
11705 if (n > 0)
11706 {
11707 leaf_path.components_.insert(leaf_path.components_.begin(),
11708 components_.end() - static_cast<int>(n),
11709 components_.end());
11710 }
11711
11712 return leaf_path;
11713 }
11714
11715 TOML_EXTERNAL_LINKAGE
11716 path path::subpath(std::vector<path_component>::const_iterator start,
11717 std::vector<path_component>::const_iterator end) const
11718 {
11719 if (start >= end)
11720 return {};
11721
11722 path subpath;
11723 subpath.components_.insert(subpath.components_.begin(), start, end);
11724 return subpath;
11725 }
11726
11727 TOML_EXTERNAL_LINKAGE
11728 path path::subpath(size_t start, size_t length) const
11729 {
11730 return subpath(begin() + static_cast<int>(start), begin() + static_cast<int>(start + length));
11731 }
11732 }
11733 TOML_NAMESPACE_END;
11734
11735 TOML_NAMESPACE_START
11736 {
11737 TOML_EXTERNAL_LINKAGE
11738 node_view<node> TOML_CALLCONV at_path(node & root, const toml::path& path) noexcept
11739 {
11740 // early-exit sanity-checks
11741 if (root.is_value())
11742 return {};
11743 if (auto tbl = root.as_table(); tbl && tbl->empty())
11744 return {};
11745 if (auto arr = root.as_array(); arr && arr->empty())
11746 return {};
11747
11748 node* current = &root;
11749
11750 for (const auto& component : path)
11751 {
11752 auto type = component.type();
11753 if (type == path_component_type::array_index)
11754 {
11755 const auto current_array = current->as<array>();
11756 if (!current_array)
11757 return {}; // not an array, using array index doesn't work
11758
11759 current = current_array->get(component.index());
11760 }
11761 else if (type == path_component_type::key)
11762 {
11763 const auto current_table = current->as<table>();
11764 if (!current_table)
11765 return {};
11766
11767 current = current_table->get(component.key());
11768 }
11769 else
11770 {
11771 // Error: invalid component
11772 return {};
11773 }
11774
11775 if (!current)
11776 return {}; // not found
11777 }
11778
11779 return node_view{ current };
11780 }
11781
11782 TOML_EXTERNAL_LINKAGE
11783 node_view<const node> TOML_CALLCONV at_path(const node& root, const toml::path& path) noexcept
11784 {
11785 return node_view<const node>{ at_path(const_cast<node&>(root), path).node() };
11786 }
11787 }
11788 TOML_NAMESPACE_END;
11789
11790 #ifdef _MSC_VER
11791 #pragma pop_macro("min")
11792 #pragma pop_macro("max")
11793 #ifndef __clang__
11794 #pragma inline_recursion(off)
11795 #endif
11796 #endif
11797 TOML_POP_WARNINGS;
11798
11799 //******** impl/array.inl ********************************************************************************************
11800
11801 TOML_PUSH_WARNINGS;
11802 #ifdef _MSC_VER
11803 #ifndef __clang__
11804 #pragma inline_recursion(on)
11805 #endif
11806 #pragma push_macro("min")
11807 #pragma push_macro("max")
11808 #undef min
11809 #undef max
11810 #endif
11811
11812 TOML_NAMESPACE_START
11813 {
11814 TOML_EXTERNAL_LINKAGE
11815 array::array() noexcept
11816 {
11817 #if TOML_LIFETIME_HOOKS
11818 TOML_ARRAY_CREATED;
11819 #endif
11820 }
11821
11822 TOML_EXTERNAL_LINKAGE
11823 array::~array() noexcept
11824 {
11825 #if TOML_LIFETIME_HOOKS
11826 TOML_ARRAY_DESTROYED;
11827 #endif
11828 }
11829
11830 TOML_EXTERNAL_LINKAGE
11831 array::array(const impl::array_init_elem* b, const impl::array_init_elem* e)
11832 {
11833 #if TOML_LIFETIME_HOOKS
11834 TOML_ARRAY_CREATED;
11835 #endif
11836
11837 TOML_ASSERT_ASSUME(b);
11838 TOML_ASSERT_ASSUME(e);
11839 TOML_ASSERT_ASSUME(b <= e);
11840
11841 if TOML_UNLIKELY(b == e)
11842 return;
11843
11844 size_t cap{};
11845 for (auto it = b; it != e; it++)
11846 {
11847 if (it->value)
11848 cap++;
11849 }
11850 if TOML_UNLIKELY(!cap)
11851 return;
11852
11853 elems_.reserve(cap);
11854 for (; b != e; b++)
11855 {
11856 if (b->value)
11857 elems_.push_back(std::move(b->value));
11858 }
11859 }
11860
11861 TOML_EXTERNAL_LINKAGE
11862 array::array(const array& other) //
11863 : node(other)
11864 {
11865 elems_.reserve(other.elems_.size());
11866 for (const auto& elem : other)
11867 elems_.emplace_back(impl::make_node(elem));
11868
11869 #if TOML_LIFETIME_HOOKS
11870 TOML_ARRAY_CREATED;
11871 #endif
11872 }
11873
11874 TOML_EXTERNAL_LINKAGE
11875 array::array(array && other) noexcept //
11876 : node(std::move(other)),
11877 elems_(std::move(other.elems_))
11878 {
11879 #if TOML_LIFETIME_HOOKS
11880 TOML_ARRAY_CREATED;
11881 #endif
11882 }
11883
11884 TOML_EXTERNAL_LINKAGE
11885 array& array::operator=(const array& rhs)
11886 {
11887 if (&rhs != this)
11888 {
11889 node::operator=(rhs);
11890 elems_.clear();
11891 elems_.reserve(rhs.elems_.size());
11892 for (const auto& elem : rhs)
11893 elems_.emplace_back(impl::make_node(elem));
11894 }
11895 return *this;
11896 }
11897
11898 TOML_EXTERNAL_LINKAGE
11899 array& array::operator=(array&& rhs) noexcept
11900 {
11901 if (&rhs != this)
11902 {
11903 node::operator=(std::move(rhs));
11904 elems_ = std::move(rhs.elems_);
11905 }
11906 return *this;
11907 }
11908
11909 TOML_EXTERNAL_LINKAGE
11910 void array::preinsertion_resize(size_t idx, size_t count)
11911 {
11912 TOML_ASSERT(idx <= elems_.size());
11913 TOML_ASSERT_ASSUME(count >= 1u);
11914 const auto old_size = elems_.size();
11915 const auto new_size = old_size + count;
11916 const auto inserting_at_end = idx == old_size;
11917 elems_.resize(new_size);
11918 if (!inserting_at_end)
11919 {
11920 for (size_t left = old_size, right = new_size - 1u; left-- > idx; right--)
11921 elems_[right] = std::move(elems_[left]);
11922 }
11923 }
11924
11925 TOML_EXTERNAL_LINKAGE
11926 void array::insert_at_back(impl::node_ptr && elem)
11927 {
11928 TOML_ASSERT(elem);
11929 elems_.push_back(std::move(elem));
11930 }
11931
11932 TOML_EXTERNAL_LINKAGE
11933 array::vector_iterator array::insert_at(const_vector_iterator pos, impl::node_ptr && elem)
11934 {
11935 return elems_.insert(pos, std::move(elem));
11936 }
11937
11938 TOML_PURE_GETTER
11939 TOML_EXTERNAL_LINKAGE
11940 bool array::is_homogeneous(node_type ntype) const noexcept
11941 {
11942 if (elems_.empty())
11943 return false;
11944
11945 if (ntype == node_type::none)
11946 ntype = elems_[0]->type();
11947
11948 for (const auto& val : elems_)
11949 if (val->type() != ntype)
11950 return false;
11951
11952 return true;
11953 }
11954
11955 TOML_NODISCARD
11956 TOML_EXTERNAL_LINKAGE
11957 bool array::is_homogeneous(node_type ntype, node * &first_nonmatch) noexcept
11958 {
11959 if (elems_.empty())
11960 {
11961 first_nonmatch = {};
11962 return false;
11963 }
11964 if (ntype == node_type::none)
11965 ntype = elems_[0]->type();
11966 for (const auto& val : elems_)
11967 {
11968 if (val->type() != ntype)
11969 {
11970 first_nonmatch = val.get();
11971 return false;
11972 }
11973 }
11974 return true;
11975 }
11976
11977 TOML_NODISCARD
11978 TOML_EXTERNAL_LINKAGE
11979 bool array::is_homogeneous(node_type ntype, const node*& first_nonmatch) const noexcept
11980 {
11981 node* fnm = nullptr;
11982 const auto result = const_cast<array&>(*this).is_homogeneous(ntype, fnm);
11983 first_nonmatch = fnm;
11984 return result;
11985 }
11986
11987 TOML_EXTERNAL_LINKAGE
11988 node& array::at(size_t index)
11989 {
11990 #if TOML_COMPILER_HAS_EXCEPTIONS
11991
11992 return *elems_.at(index);
11993
11994 #else
11995
11996 auto n = get(index);
11997 TOML_ASSERT_ASSUME(n && "element index not found in array!");
11998 return *n;
11999
12000 #endif
12001 }
12002
12003 TOML_EXTERNAL_LINKAGE
12004 void array::reserve(size_t new_capacity)
12005 {
12006 elems_.reserve(new_capacity);
12007 }
12008
12009 TOML_EXTERNAL_LINKAGE
12010 void array::shrink_to_fit()
12011 {
12012 elems_.shrink_to_fit();
12013 }
12014
12015 TOML_EXTERNAL_LINKAGE
12016 void array::truncate(size_t new_size)
12017 {
12018 if (new_size < elems_.size())
12019 elems_.resize(new_size);
12020 }
12021
12022 TOML_EXTERNAL_LINKAGE
12023 array::iterator array::erase(const_iterator pos) noexcept
12024 {
12025 return iterator{ elems_.erase(const_vector_iterator{ pos }) };
12026 }
12027
12028 TOML_EXTERNAL_LINKAGE
12029 array::iterator array::erase(const_iterator first, const_iterator last) noexcept
12030 {
12031 return iterator{ elems_.erase(const_vector_iterator{ first }, const_vector_iterator{ last }) };
12032 }
12033
12034 TOML_EXTERNAL_LINKAGE
12035 size_t array::total_leaf_count() const noexcept
12036 {
12037 size_t leaves{};
12038 for (size_t i = 0, e = elems_.size(); i < e; i++)
12039 {
12040 auto arr = elems_[i]->as_array();
12041 leaves += arr ? arr->total_leaf_count() : size_t{ 1 };
12042 }
12043 return leaves;
12044 }
12045
12046 TOML_EXTERNAL_LINKAGE
12047 void array::flatten_child(array && child, size_t & dest_index) noexcept
12048 {
12049 for (size_t i = 0, e = child.size(); i < e; i++)
12050 {
12051 auto type = child.elems_[i]->type();
12052 if (type == node_type::array)
12053 {
12054 array& arr = *reinterpret_cast<array*>(child.elems_[i].get());
12055 if (!arr.empty())
12056 flatten_child(std::move(arr), dest_index);
12057 }
12058 else
12059 elems_[dest_index++] = std::move(child.elems_[i]);
12060 }
12061 }
12062
12063 TOML_EXTERNAL_LINKAGE
12064 array& array::flatten()&
12065 {
12066 if (elems_.empty())
12067 return *this;
12068
12069 bool requires_flattening = false;
12070 size_t size_after_flattening = elems_.size();
12071 for (size_t i = elems_.size(); i-- > 0u;)
12072 {
12073 auto arr = elems_[i]->as_array();
12074 if (!arr)
12075 continue;
12076 size_after_flattening--; // discount the array itself
12077 const auto leaf_count = arr->total_leaf_count();
12078 if (leaf_count > 0u)
12079 {
12080 requires_flattening = true;
12081 size_after_flattening += leaf_count;
12082 }
12083 else
12084 elems_.erase(elems_.cbegin() + static_cast<ptrdiff_t>(i));
12085 }
12086
12087 if (!requires_flattening)
12088 return *this;
12089
12090 elems_.reserve(size_after_flattening);
12091
12092 size_t i = 0;
12093 while (i < elems_.size())
12094 {
12095 auto arr = elems_[i]->as_array();
12096 if (!arr)
12097 {
12098 i++;
12099 continue;
12100 }
12101
12102 impl::node_ptr arr_storage = std::move(elems_[i]);
12103 const auto leaf_count = arr->total_leaf_count();
12104 if (leaf_count > 1u)
12105 preinsertion_resize(i + 1u, leaf_count - 1u);
12106 flatten_child(std::move(*arr), i); // increments i
12107 }
12108
12109 return *this;
12110 }
12111
12112 TOML_EXTERNAL_LINKAGE
12113 array& array::prune(bool recursive)& noexcept
12114 {
12115 if (elems_.empty())
12116 return *this;
12117
12118 for (size_t i = elems_.size(); i-- > 0u;)
12119 {
12120 if (auto arr = elems_[i]->as_array())
12121 {
12122 if (recursive)
12123 arr->prune(true);
12124 if (arr->empty())
12125 elems_.erase(elems_.cbegin() + static_cast<ptrdiff_t>(i));
12126 }
12127 else if (auto tbl = elems_[i]->as_table())
12128 {
12129 if (recursive)
12130 tbl->prune(true);
12131 if (tbl->empty())
12132 elems_.erase(elems_.cbegin() + static_cast<ptrdiff_t>(i));
12133 }
12134 }
12135
12136 return *this;
12137 }
12138
12139 TOML_EXTERNAL_LINKAGE
12140 void array::pop_back() noexcept
12141 {
12142 elems_.pop_back();
12143 }
12144
12145 TOML_EXTERNAL_LINKAGE
12146 void array::clear() noexcept
12147 {
12148 elems_.clear();
12149 }
12150
12151 TOML_EXTERNAL_LINKAGE
12152 bool TOML_CALLCONV array::equal(const array& lhs, const array& rhs) noexcept
12153 {
12154 if (&lhs == &rhs)
12155 return true;
12156 if (lhs.elems_.size() != rhs.elems_.size())
12157 return false;
12158 for (size_t i = 0, e = lhs.elems_.size(); i < e; i++)
12159 {
12160 const auto lhs_type = lhs.elems_[i]->type();
12161 const node& rhs_ = *rhs.elems_[i];
12162 const auto rhs_type = rhs_.type();
12163 if (lhs_type != rhs_type)
12164 return false;
12165
12166 const bool equal = lhs.elems_[i]->visit(
12167 [&](const auto& lhs_) noexcept
12168 { return lhs_ == *reinterpret_cast<std::remove_reference_t<decltype(lhs_)>*>(&rhs_); });
12169 if (!equal)
12170 return false;
12171 }
12172 return true;
12173 }
12174 }
12175 TOML_NAMESPACE_END;
12176
12177 #ifdef _MSC_VER
12178 #pragma pop_macro("min")
12179 #pragma pop_macro("max")
12180 #ifndef __clang__
12181 #pragma inline_recursion(off)
12182 #endif
12183 #endif
12184 TOML_POP_WARNINGS;
12185
12186 //******** impl/table.inl ********************************************************************************************
12187
12188 TOML_PUSH_WARNINGS;
12189 #ifdef _MSC_VER
12190 #ifndef __clang__
12191 #pragma inline_recursion(on)
12192 #endif
12193 #pragma push_macro("min")
12194 #pragma push_macro("max")
12195 #undef min
12196 #undef max
12197 #endif
12198
12199 TOML_NAMESPACE_START
12200 {
12201 TOML_EXTERNAL_LINKAGE
12202 table::table() noexcept
12203 {
12204 #if TOML_LIFETIME_HOOKS
12205 TOML_TABLE_CREATED;
12206 #endif
12207 }
12208
12209 TOML_EXTERNAL_LINKAGE
12210 table::~table() noexcept
12211 {
12212 #if TOML_LIFETIME_HOOKS
12213 TOML_TABLE_DESTROYED;
12214 #endif
12215 }
12216
12217 TOML_EXTERNAL_LINKAGE
12218 table::table(const impl::table_init_pair* b, const impl::table_init_pair* e)
12219 {
12220 #if TOML_LIFETIME_HOOKS
12221 TOML_TABLE_CREATED;
12222 #endif
12223
12224 TOML_ASSERT_ASSUME(b);
12225 TOML_ASSERT_ASSUME(e);
12226 TOML_ASSERT_ASSUME(b <= e);
12227
12228 if TOML_UNLIKELY(b == e)
12229 return;
12230
12231 for (; b != e; b++)
12232 {
12233 if (!b->value) // empty node_views
12234 continue;
12235
12236 map_.insert_or_assign(std::move(b->key), std::move(b->value));
12237 }
12238 }
12239
12240 TOML_EXTERNAL_LINKAGE
12241 table::table(const table& other) //
12242 : node(other),
12243 inline_{ other.inline_ }
12244 {
12245 for (auto&& [k, v] : other.map_)
12246 map_.emplace_hint(map_.end(), k, impl::make_node(*v));
12247
12248 #if TOML_LIFETIME_HOOKS
12249 TOML_TABLE_CREATED;
12250 #endif
12251 }
12252
12253 TOML_EXTERNAL_LINKAGE
12254 table::table(table && other) noexcept //
12255 : node(std::move(other)),
12256 map_{ std::move(other.map_) },
12257 inline_{ other.inline_ }
12258 {
12259 #if TOML_LIFETIME_HOOKS
12260 TOML_TABLE_CREATED;
12261 #endif
12262 }
12263
12264 TOML_EXTERNAL_LINKAGE
12265 table& table::operator=(const table& rhs)
12266 {
12267 if (&rhs != this)
12268 {
12269 node::operator=(rhs);
12270 map_.clear();
12271 for (auto&& [k, v] : rhs.map_)
12272 map_.emplace_hint(map_.end(), k, impl::make_node(*v));
12273 inline_ = rhs.inline_;
12274 }
12275 return *this;
12276 }
12277
12278 TOML_EXTERNAL_LINKAGE
12279 table& table::operator=(table&& rhs) noexcept
12280 {
12281 if (&rhs != this)
12282 {
12283 node::operator=(std::move(rhs));
12284 map_ = std::move(rhs.map_);
12285 inline_ = rhs.inline_;
12286 }
12287 return *this;
12288 }
12289
12290 TOML_PURE_GETTER
12291 TOML_EXTERNAL_LINKAGE
12292 bool table::is_homogeneous(node_type ntype) const noexcept
12293 {
12294 if (map_.empty())
12295 return false;
12296
12297 if (ntype == node_type::none)
12298 ntype = map_.cbegin()->second->type();
12299
12300 for (auto&& [k, v] : map_)
12301 {
12302 TOML_UNUSED(k);
12303 if (v->type() != ntype)
12304 return false;
12305 }
12306
12307 return true;
12308 }
12309
12310 TOML_NODISCARD
12311 TOML_EXTERNAL_LINKAGE
12312 bool table::is_homogeneous(node_type ntype, node * &first_nonmatch) noexcept
12313 {
12314 if (map_.empty())
12315 {
12316 first_nonmatch = {};
12317 return false;
12318 }
12319 if (ntype == node_type::none)
12320 ntype = map_.cbegin()->second->type();
12321 for (const auto& [k, v] : map_)
12322 {
12323 TOML_UNUSED(k);
12324 if (v->type() != ntype)
12325 {
12326 first_nonmatch = v.get();
12327 return false;
12328 }
12329 }
12330 return true;
12331 }
12332
12333 TOML_NODISCARD
12334 TOML_EXTERNAL_LINKAGE
12335 bool table::is_homogeneous(node_type ntype, const node*& first_nonmatch) const noexcept
12336 {
12337 node* fnm = nullptr;
12338 const auto result = const_cast<table&>(*this).is_homogeneous(ntype, fnm);
12339 first_nonmatch = fnm;
12340 return result;
12341 }
12342
12343 TOML_PURE_GETTER
12344 TOML_EXTERNAL_LINKAGE
12345 node* table::get(std::string_view key) noexcept
12346 {
12347 if (auto it = map_.find(key); it != map_.end())
12348 return it->second.get();
12349 return nullptr;
12350 }
12351
12352 TOML_EXTERNAL_LINKAGE
12353 node& table::at(std::string_view key)
12354 {
12355 auto n = get(key);
12356
12357 #if TOML_COMPILER_HAS_EXCEPTIONS
12358
12359 if (!n)
12360 {
12361 auto err = "key '"s;
12362 err.append(key);
12363 err.append("' not found in table"sv);
12364 throw std::out_of_range{ err };
12365 }
12366
12367 #else
12368
12369 TOML_ASSERT_ASSUME(n && "key not found in table!");
12370
12371 #endif
12372
12373 return *n;
12374 }
12375
12376 TOML_PURE_GETTER
12377 TOML_EXTERNAL_LINKAGE
12378 table::map_iterator table::get_lower_bound(std::string_view key) noexcept
12379 {
12380 return map_.lower_bound(key);
12381 }
12382
12383 TOML_PURE_GETTER
12384 TOML_EXTERNAL_LINKAGE
12385 table::iterator table::find(std::string_view key) noexcept
12386 {
12387 return iterator{ map_.find(key) };
12388 }
12389
12390 TOML_PURE_GETTER
12391 TOML_EXTERNAL_LINKAGE
12392 table::const_iterator table::find(std::string_view key) const noexcept
12393 {
12394 return const_iterator{ map_.find(key) };
12395 }
12396
12397 TOML_EXTERNAL_LINKAGE
12398 table::map_iterator table::erase(const_map_iterator pos) noexcept
12399 {
12400 return map_.erase(pos);
12401 }
12402
12403 TOML_EXTERNAL_LINKAGE
12404 table::map_iterator table::erase(const_map_iterator begin, const_map_iterator end) noexcept
12405 {
12406 return map_.erase(begin, end);
12407 }
12408
12409 TOML_EXTERNAL_LINKAGE
12410 size_t table::erase(std::string_view key) noexcept
12411 {
12412 if (auto it = map_.find(key); it != map_.end())
12413 {
12414 map_.erase(it);
12415 return size_t{ 1 };
12416 }
12417 return size_t{};
12418 }
12419
12420 TOML_EXTERNAL_LINKAGE
12421 table& table::prune(bool recursive)& noexcept
12422 {
12423 if (map_.empty())
12424 return *this;
12425
12426 for (auto it = map_.begin(); it != map_.end();)
12427 {
12428 if (auto arr = it->second->as_array())
12429 {
12430 if (recursive)
12431 arr->prune(true);
12432
12433 if (arr->empty())
12434 {
12435 it = map_.erase(it);
12436 continue;
12437 }
12438 }
12439 else if (auto tbl = it->second->as_table())
12440 {
12441 if (recursive)
12442 tbl->prune(true);
12443
12444 if (tbl->empty())
12445 {
12446 it = map_.erase(it);
12447 continue;
12448 }
12449 }
12450 it++;
12451 }
12452
12453 return *this;
12454 }
12455
12456 TOML_EXTERNAL_LINKAGE
12457 void table::clear() noexcept
12458 {
12459 map_.clear();
12460 }
12461
12462 TOML_EXTERNAL_LINKAGE
12463 table::map_iterator table::insert_with_hint(const_iterator hint, key && k, impl::node_ptr && v)
12464 {
12465 return map_.emplace_hint(const_map_iterator{ hint }, std::move(k), std::move(v));
12466 }
12467
12468 TOML_PURE_GETTER
12469 TOML_EXTERNAL_LINKAGE
12470 bool TOML_CALLCONV table::equal(const table& lhs, const table& rhs) noexcept
12471 {
12472 if (&lhs == &rhs)
12473 return true;
12474 if (lhs.map_.size() != rhs.map_.size())
12475 return false;
12476
12477 for (auto l = lhs.map_.begin(), r = rhs.map_.begin(), e = lhs.map_.end(); l != e; l++, r++)
12478 {
12479 if (l->first != r->first)
12480 return false;
12481
12482 const auto lhs_type = l->second->type();
12483 const node& rhs_ = *r->second;
12484 const auto rhs_type = rhs_.type();
12485 if (lhs_type != rhs_type)
12486 return false;
12487
12488 const bool equal = l->second->visit(
12489 [&](const auto& lhs_) noexcept
12490 { return lhs_ == *reinterpret_cast<std::remove_reference_t<decltype(lhs_)>*>(&rhs_); });
12491 if (!equal)
12492 return false;
12493 }
12494 return true;
12495 }
12496 }
12497 TOML_NAMESPACE_END;
12498
12499 #ifdef _MSC_VER
12500 #pragma pop_macro("min")
12501 #pragma pop_macro("max")
12502 #ifndef __clang__
12503 #pragma inline_recursion(off)
12504 #endif
12505 #endif
12506 TOML_POP_WARNINGS;
12507
12508 //******** impl/simd.hpp *********************************************************************************************
12509
12510 #if TOML_ENABLE_SIMD
12511
12512 #if defined(__SSE2__) \
12513 || (defined(_MSC_VER) && (defined(_M_AMD64) || defined(_M_X64) || (defined(_M_IX86_FP) && _M_IX86_FP >= 2)))
12514 #define TOML_HAS_SSE2 1
12515 #endif
12516
12517 #if defined(__SSE4_1__) || (defined(_MSC_VER) && (defined(__AVX__) || defined(__AVX2__)))
12518 #define TOML_HAS_SSE4_1 1
12519 #endif
12520
12521 #endif // TOML_ENABLE_SIMD
12522
12523 #ifndef TOML_HAS_SSE2
12524 #define TOML_HAS_SSE2 0
12525 #endif
12526 #ifndef TOML_HAS_SSE4_1
12527 #define TOML_HAS_SSE4_1 0
12528 #endif
12529
12530 TOML_DISABLE_WARNINGS;
12531 #if TOML_HAS_SSE4_1
12532 #include <smmintrin.h>
12533 #endif
12534 #if TOML_HAS_SSE2
12535 #include <emmintrin.h>
12536 #endif
12537 TOML_ENABLE_WARNINGS;
12538
12539 //******** impl/unicode.inl ******************************************************************************************
12540
12541 TOML_PUSH_WARNINGS;
12542 #ifdef _MSC_VER
12543 #ifndef __clang__
12544 #pragma inline_recursion(on)
12545 #endif
12546 #pragma push_macro("min")
12547 #pragma push_macro("max")
12548 #undef min
12549 #undef max
12550 #endif
12551
12552 TOML_IMPL_NAMESPACE_START
12553 {
12554 TOML_PURE_GETTER
12555 TOML_EXTERNAL_LINKAGE
12556 bool is_ascii(const char* str, size_t len) noexcept
12557 {
12558 const char* const end = str + len;
12559
12560 #if TOML_HAS_SSE2 && (128 % CHAR_BIT) == 0
12561 {
12562 constexpr size_t chars_per_vector = 128u / CHAR_BIT;
12563
12564 if (const size_t simdable = len - (len % chars_per_vector))
12565 {
12566 __m128i mask = _mm_setzero_si128();
12567 for (const char* const e = str + simdable; str < e; str += chars_per_vector)
12568 {
12569 const __m128i current_bytes = _mm_loadu_si128(reinterpret_cast<const __m128i*>(str));
12570 mask = _mm_or_si128(mask, current_bytes);
12571 }
12572 const __m128i has_error = _mm_cmpgt_epi8(_mm_setzero_si128(), mask);
12573
12574 #if TOML_HAS_SSE4_1
12575 if (!_mm_testz_si128(has_error, has_error))
12576 return false;
12577 #else
12578 if (_mm_movemask_epi8(_mm_cmpeq_epi8(has_error, _mm_setzero_si128())) != 0xFFFF)
12579 return false;
12580 #endif
12581 }
12582 }
12583 #endif
12584
12585 for (; str < end; str++)
12586 if (static_cast<unsigned char>(*str) > 127u)
12587 return false;
12588
12589 return true;
12590 }
12591 }
12592 TOML_IMPL_NAMESPACE_END;
12593
12594 #ifdef _MSC_VER
12595 #pragma pop_macro("min")
12596 #pragma pop_macro("max")
12597 #ifndef __clang__
12598 #pragma inline_recursion(off)
12599 #endif
12600 #endif
12601 TOML_POP_WARNINGS;
12602
12603 //******** impl/parser.inl *******************************************************************************************
12604
12605 #if TOML_ENABLE_PARSER
12606
12607 TOML_DISABLE_WARNINGS;
12608 #include <istream>
12609 #include <fstream>
12610 #if TOML_INT_CHARCONV || TOML_FLOAT_CHARCONV
12611 #include <charconv>
12612 #endif
12613 #if !TOML_INT_CHARCONV || !TOML_FLOAT_CHARCONV
12614 #include <sstream>
12615 #endif
12616 #if !TOML_INT_CHARCONV
12617 #include <iomanip>
12618 #endif
12619 TOML_ENABLE_WARNINGS;
12620 TOML_PUSH_WARNINGS;
12621 #ifdef _MSC_VER
12622 #ifndef __clang__
12623 #pragma inline_recursion(on)
12624 #endif
12625 #pragma push_macro("min")
12626 #pragma push_macro("max")
12627 #undef min
12628 #undef max
12629 #endif
12630
12631 TOML_ANON_NAMESPACE_START
12632 {
12633 template <typename T>
12634 class utf8_byte_stream;
12635
12636 TOML_INTERNAL_LINKAGE
12637 constexpr auto utf8_byte_order_mark = "\xEF\xBB\xBF"sv;
12638
12639 template <typename Char>
12640 class utf8_byte_stream<std::basic_string_view<Char>>
12641 {
12642 static_assert(sizeof(Char) == 1);
12643
12644 private:
12645 std::basic_string_view<Char> source_;
12646 size_t position_ = {};
12647
12648 public:
12649 TOML_NODISCARD_CTOR
12650 explicit constexpr utf8_byte_stream(std::basic_string_view<Char> sv) noexcept //
12651 : source_{ sv }
12652 {
12653 // skip bom
12654 if (source_.length() >= 3u && memcmp(utf8_byte_order_mark.data(), source_.data(), 3u) == 0)
12655 position_ += 3u;
12656 }
12657
12658 TOML_CONST_INLINE_GETTER
12659 constexpr bool error() const noexcept
12660 {
12661 return false;
12662 }
12663
12664 TOML_PURE_INLINE_GETTER
12665 constexpr bool eof() const noexcept
12666 {
12667 return position_ >= source_.length();
12668 }
12669
12670 TOML_PURE_INLINE_GETTER
12671 explicit constexpr operator bool() const noexcept
12672 {
12673 return !eof();
12674 }
12675
12676 TOML_PURE_INLINE_GETTER
12677 constexpr bool peek_eof() const noexcept
12678 {
12679 return eof();
12680 }
12681
12682 TOML_NODISCARD
12683 TOML_ATTR(nonnull)
12684 size_t operator()(void* dest, size_t num) noexcept
12685 {
12686 TOML_ASSERT_ASSUME(!eof());
12687
12688 num = impl::min(position_ + num, source_.length()) - position_;
12689 std::memcpy(dest, source_.data() + position_, num);
12690 position_ += num;
12691 return num;
12692 }
12693 };
12694
12695 template <>
12696 class utf8_byte_stream<std::istream>
12697 {
12698 private:
12699 std::istream* source_;
12700
12701 public:
12702 TOML_NODISCARD_CTOR
12703 explicit utf8_byte_stream(std::istream& stream) noexcept(!TOML_COMPILER_HAS_EXCEPTIONS) //
12704 : source_{ &stream }
12705 {
12706 if (!*this) // eof, bad
12707 return;
12708
12709 const auto initial_pos = source_->tellg();
12710 char bom[3];
12711 source_->read(bom, 3);
12712 if (source_->bad() || (source_->gcount() == 3 && memcmp(utf8_byte_order_mark.data(), bom, 3u) == 0))
12713 return;
12714
12715 source_->clear();
12716 source_->seekg(initial_pos, std::istream::beg);
12717 }
12718
12719 TOML_PURE_INLINE_GETTER
12720 bool error() const noexcept
12721 {
12722 return !!(source_->rdstate() & std::istream::badbit);
12723 }
12724
12725 TOML_PURE_INLINE_GETTER
12726 bool eof() const noexcept
12727 {
12728 return !!(source_->rdstate() & std::istream::eofbit);
12729 }
12730
12731 TOML_PURE_INLINE_GETTER
12732 explicit operator bool() const noexcept
12733 {
12734 return !(source_->rdstate() & (std::istream::badbit | std::istream::eofbit));
12735 }
12736
12737 TOML_NODISCARD
12738 bool peek_eof() const noexcept(!TOML_COMPILER_HAS_EXCEPTIONS)
12739 {
12740 return eof() || source_->peek() == std::istream::traits_type::eof();
12741 }
12742
12743 TOML_NODISCARD
12744 TOML_ATTR(nonnull)
12745 size_t operator()(void* dest, size_t num) noexcept(!TOML_COMPILER_HAS_EXCEPTIONS)
12746 {
12747 TOML_ASSERT(*this);
12748
12749 source_->read(static_cast<char*>(dest), static_cast<std::streamsize>(num));
12750 return static_cast<size_t>(source_->gcount());
12751 }
12752 };
12753
12754 struct utf8_codepoint
12755 {
12756 char32_t value;
12757 char bytes[4];
12758 size_t count;
12759 source_position position;
12760
12761 TOML_PURE_INLINE_GETTER
12762 constexpr operator const char32_t&() const noexcept
12763 {
12764 return value;
12765 }
12766
12767 TOML_PURE_INLINE_GETTER
12768 constexpr const char32_t& operator*() const noexcept
12769 {
12770 return value;
12771 }
12772 };
12773 static_assert(std::is_trivially_default_constructible_v<utf8_codepoint> && std::is_trivially_copyable_v<utf8_codepoint>);
12774 static_assert(std::is_standard_layout_v<utf8_codepoint>);
12775
12776 struct TOML_ABSTRACT_INTERFACE utf8_reader_interface
12777 {
12778 TOML_NODISCARD
12779 virtual const source_path_ptr& source_path() const noexcept = 0;
12780
12781 TOML_NODISCARD
12782 virtual const utf8_codepoint* read_next() noexcept(!TOML_COMPILER_HAS_EXCEPTIONS) = 0;
12783
12784 TOML_NODISCARD
12785 virtual bool peek_eof() const noexcept(!TOML_COMPILER_HAS_EXCEPTIONS) = 0;
12786
12787 #if !TOML_EXCEPTIONS
12788
12789 TOML_NODISCARD
12790 virtual optional<parse_error>&& error() noexcept = 0;
12791
12792 #endif
12793
12794 virtual ~utf8_reader_interface() noexcept = default;
12795 };
12796
12797 #if TOML_EXCEPTIONS
12798 #define utf8_reader_error(...) throw parse_error(__VA_ARGS__)
12799 #define utf8_reader_return_after_error(...) static_assert(true)
12800 #define utf8_reader_error_check(...) static_assert(true)
12801 #else
12802 #define utf8_reader_error(...) err_.emplace(__VA_ARGS__)
12803 #define utf8_reader_return_after_error(...) return __VA_ARGS__
12804 #define utf8_reader_error_check(...) \
12805 do \
12806 { \
12807 if TOML_UNLIKELY(err_) \
12808 return __VA_ARGS__; \
12809 } \
12810 while (false)
12811
12812 #endif
12813
12814 #if defined(__APPLE__) || defined(__MINGW32__) || defined(__MINGW64__)
12815 #define TOML_OVERALIGNED
12816 #else
12817 #define TOML_OVERALIGNED alignas(32)
12818 #endif
12819
12820 template <typename T>
12821 class TOML_EMPTY_BASES utf8_reader final : public utf8_reader_interface
12822 {
12823 private:
12824 static constexpr size_t block_capacity = 32;
12825 utf8_byte_stream<T> stream_;
12826 source_position next_pos_ = { 1, 1 };
12827
12828 impl::utf8_decoder decoder_;
12829 struct currently_decoding_t
12830 {
12831 char bytes[4];
12832 size_t count;
12833 } currently_decoding_;
12834
12835 struct codepoints_t
12836 {
12837 TOML_OVERALIGNED utf8_codepoint buffer[block_capacity];
12838 size_t current;
12839 size_t count;
12840 } codepoints_;
12841
12842 source_path_ptr source_path_;
12843
12844 #if !TOML_EXCEPTIONS
12845 optional<parse_error> err_;
12846 #endif
12847
12848 bool read_next_block() noexcept(!TOML_COMPILER_HAS_EXCEPTIONS)
12849 {
12850 TOML_ASSERT(stream_);
12851
12852 TOML_OVERALIGNED char raw_bytes[block_capacity];
12853 size_t raw_bytes_read;
12854
12855 // read the next raw (encoded) block in from the stream
12856 if constexpr (noexcept(stream_(raw_bytes, block_capacity)) || !TOML_EXCEPTIONS)
12857 {
12858 raw_bytes_read = stream_(raw_bytes, block_capacity);
12859 }
12860 #if TOML_EXCEPTIONS
12861 else
12862 {
12863 try
12864 {
12865 raw_bytes_read = stream_(raw_bytes, block_capacity);
12866 }
12867 catch (const std::exception& exc)
12868 {
12869 throw parse_error{ exc.what(), next_pos_, source_path_ };
12870 }
12871 catch (...)
12872 {
12873 throw parse_error{ "An unspecified error occurred", next_pos_, source_path_ };
12874 }
12875 }
12876 #endif // TOML_EXCEPTIONS
12877
12878 // handle a zero-byte read
12879 if TOML_UNLIKELY(!raw_bytes_read)
12880 {
12881 if (stream_.eof())
12882 {
12883 // EOF only sets the error state if the decoder wants more input, otherwise
12884 // a zero-byte read might have just caused the underlying stream to realize it's exhaused and set
12885 // the EOF flag, and that's totally fine
12886 if (decoder_.needs_more_input())
12887 utf8_reader_error("Encountered EOF during incomplete utf-8 code point sequence",
12888 next_pos_,
12889 source_path_);
12890 }
12891 else
12892 {
12893 utf8_reader_error("Reading from the underlying stream failed - zero bytes read",
12894 next_pos_,
12895 source_path_);
12896 }
12897 return false;
12898 }
12899
12900 TOML_ASSERT_ASSUME(raw_bytes_read);
12901 std::memset(&codepoints_, 0, sizeof(codepoints_));
12902
12903 // helper for calculating decoded codepoint line+cols
12904 const auto calc_positions = [&]() noexcept
12905 {
12906 for (size_t i = 0; i < codepoints_.count; i++)
12907 {
12908 auto& cp = codepoints_.buffer[i];
12909 cp.position = next_pos_;
12910
12911 if (cp == U'\n')
12912 {
12913 next_pos_.line++;
12914 next_pos_.column = source_index{ 1 };
12915 }
12916 else
12917 next_pos_.column++;
12918 }
12919 };
12920
12921 // decide whether we need to use the UTF-8 decoder or if we can treat this block as plain ASCII
12922 const auto ascii_fast_path = !decoder_.needs_more_input() && impl::is_ascii(raw_bytes, raw_bytes_read);
12923
12924 // ASCII fast-path
12925 if (ascii_fast_path)
12926 {
12927 decoder_.reset();
12928 currently_decoding_.count = {};
12929
12930 codepoints_.count = raw_bytes_read;
12931 for (size_t i = 0; i < codepoints_.count; i++)
12932 {
12933 auto& cp = codepoints_.buffer[i];
12934 cp.value = static_cast<char32_t>(raw_bytes[i]);
12935 cp.bytes[0] = raw_bytes[i];
12936 cp.count = 1u;
12937 }
12938 }
12939
12940 // UTF-8 slow-path
12941 else
12942 {
12943 // helper for getting precise error location
12944 const auto error_pos = [&]() noexcept -> const source_position&
12945 { //
12946 return codepoints_.count ? codepoints_.buffer[codepoints_.count - 1u].position : next_pos_;
12947 };
12948
12949 for (size_t i = 0; i < raw_bytes_read; i++)
12950 {
12951 decoder_(static_cast<uint8_t>(raw_bytes[i]));
12952 if TOML_UNLIKELY(decoder_.error())
12953 {
12954 calc_positions();
12955 utf8_reader_error("Encountered invalid utf-8 sequence", error_pos(), source_path_);
12956 utf8_reader_return_after_error(false);
12957 }
12958
12959 currently_decoding_.bytes[currently_decoding_.count++] = raw_bytes[i];
12960
12961 if (decoder_.has_code_point())
12962 {
12963 auto& cp = codepoints_.buffer[codepoints_.count++];
12964
12965 cp.value = decoder_.codepoint;
12966 cp.count = currently_decoding_.count;
12967 std::memcpy(cp.bytes, currently_decoding_.bytes, currently_decoding_.count);
12968 currently_decoding_.count = {};
12969 }
12970 else if TOML_UNLIKELY(currently_decoding_.count == 4u)
12971 {
12972 calc_positions();
12973 utf8_reader_error("Encountered overlong utf-8 sequence", error_pos(), source_path_);
12974 utf8_reader_return_after_error(false);
12975 }
12976 }
12977 if TOML_UNLIKELY(decoder_.needs_more_input() && stream_.eof())
12978 {
12979 calc_positions();
12980 utf8_reader_error("Encountered EOF during incomplete utf-8 code point sequence",
12981 error_pos(),
12982 source_path_);
12983 utf8_reader_return_after_error(false);
12984 }
12985 }
12986
12987 TOML_ASSERT_ASSUME(codepoints_.count);
12988 calc_positions();
12989
12990 // handle general I/O errors
12991 // (down here so the next_pos_ benefits from calc_positions())
12992 if TOML_UNLIKELY(stream_.error())
12993 {
12994 utf8_reader_error("An I/O error occurred while reading from the underlying stream",
12995 next_pos_,
12996 source_path_);
12997 utf8_reader_return_after_error(false);
12998 }
12999
13000 return true;
13001 }
13002
13003 public:
13004 template <typename U, typename String = std::string_view>
13005 TOML_NODISCARD_CTOR
13006 explicit utf8_reader(U&& source, String&& source_path = {}) noexcept(
13007 std::is_nothrow_constructible_v<utf8_byte_stream<T>, U&&>)
13008 : stream_{ static_cast<U&&>(source) }
13009 {
13010 currently_decoding_.count = {};
13011
13012 codepoints_.current = {};
13013 codepoints_.count = {};
13014
13015 if (!source_path.empty())
13016 source_path_ = std::make_shared<const std::string>(static_cast<String&&>(source_path));
13017 }
13018
13019 TOML_PURE_INLINE_GETTER
13020 const source_path_ptr& source_path() const noexcept final
13021 {
13022 return source_path_;
13023 }
13024
13025 TOML_NODISCARD
13026 const utf8_codepoint* read_next() noexcept(!TOML_COMPILER_HAS_EXCEPTIONS) final
13027 {
13028 utf8_reader_error_check({});
13029
13030 if (codepoints_.current == codepoints_.count)
13031 {
13032 if TOML_UNLIKELY(!stream_ || !read_next_block())
13033 return nullptr;
13034
13035 TOML_ASSERT_ASSUME(!codepoints_.current);
13036 }
13037 TOML_ASSERT_ASSUME(codepoints_.count);
13038 TOML_ASSERT_ASSUME(codepoints_.count <= block_capacity);
13039 TOML_ASSERT_ASSUME(codepoints_.current < codepoints_.count);
13040
13041 return &codepoints_.buffer[codepoints_.current++];
13042 }
13043
13044 TOML_NODISCARD
13045 bool peek_eof() const noexcept(!TOML_COMPILER_HAS_EXCEPTIONS) final
13046 {
13047 return stream_.peek_eof();
13048 }
13049
13050 #if !TOML_EXCEPTIONS
13051
13052 TOML_NODISCARD
13053 optional<parse_error>&& error() noexcept final
13054 {
13055 return std::move(err_);
13056 }
13057
13058 #endif
13059 };
13060
13061 template <typename Char>
13062 utf8_reader(std::basic_string_view<Char>, std::string_view) -> utf8_reader<std::basic_string_view<Char>>;
13063 template <typename Char>
13064 utf8_reader(std::basic_string_view<Char>, std::string&&) -> utf8_reader<std::basic_string_view<Char>>;
13065 template <typename Char>
13066 utf8_reader(std::basic_istream<Char>&, std::string_view) -> utf8_reader<std::basic_istream<Char>>;
13067 template <typename Char>
13068 utf8_reader(std::basic_istream<Char>&, std::string&&) -> utf8_reader<std::basic_istream<Char>>;
13069
13070 #if TOML_EXCEPTIONS
13071 #define utf8_buffered_reader_error_check(...) static_assert(true)
13072 #else
13073 #define utf8_buffered_reader_error_check(...) \
13074 do \
13075 { \
13076 if TOML_UNLIKELY(reader_.error()) \
13077 return __VA_ARGS__; \
13078 } \
13079 while (false)
13080
13081 #endif
13082
13083 class TOML_EMPTY_BASES utf8_buffered_reader
13084 {
13085 public:
13086 static constexpr size_t max_history_length = 128;
13087
13088 private:
13089 static constexpr size_t history_buffer_size = max_history_length - 1; //'head' is stored in the reader
13090 utf8_reader_interface& reader_;
13091 struct
13092 {
13093 utf8_codepoint buffer[history_buffer_size];
13094 size_t count, first;
13095 } history_ = {};
13096 const utf8_codepoint* head_ = {};
13097 size_t negative_offset_ = {};
13098
13099 public:
13100 TOML_NODISCARD_CTOR
13101 explicit utf8_buffered_reader(utf8_reader_interface& reader) noexcept //
13102 : reader_{ reader }
13103 {}
13104
13105 TOML_PURE_INLINE_GETTER
13106 const source_path_ptr& source_path() const noexcept
13107 {
13108 return reader_.source_path();
13109 }
13110
13111 TOML_NODISCARD
13112 const utf8_codepoint* read_next() noexcept(!TOML_COMPILER_HAS_EXCEPTIONS)
13113 {
13114 utf8_buffered_reader_error_check({});
13115
13116 if (negative_offset_)
13117 {
13118 negative_offset_--;
13119
13120 // an entry negative offset of 1 just means "replay the current head"
13121 if (!negative_offset_)
13122 return head_;
13123
13124 // otherwise step back into the history buffer
13125 else
13126 return history_.buffer
13127 + ((history_.first + history_.count - negative_offset_) % history_buffer_size);
13128 }
13129 else
13130 {
13131 // first character read from stream
13132 if TOML_UNLIKELY(!history_.count && !head_)
13133 head_ = reader_.read_next();
13134
13135 // subsequent characters and not eof
13136 else if (head_)
13137 {
13138 if TOML_UNLIKELY(history_.count < history_buffer_size)
13139 history_.buffer[history_.count++] = *head_;
13140 else
13141 history_.buffer[(history_.first++ + history_buffer_size) % history_buffer_size] = *head_;
13142
13143 head_ = reader_.read_next();
13144 }
13145
13146 return head_;
13147 }
13148 }
13149
13150 TOML_NODISCARD
13151 const utf8_codepoint* step_back(size_t count) noexcept
13152 {
13153 utf8_buffered_reader_error_check({});
13154
13155 TOML_ASSERT_ASSUME(history_.count);
13156 TOML_ASSERT_ASSUME(negative_offset_ + count <= history_.count);
13157
13158 negative_offset_ += count;
13159
13160 return negative_offset_
13161 ? history_.buffer + ((history_.first + history_.count - negative_offset_) % history_buffer_size)
13162 : head_;
13163 }
13164
13165 TOML_NODISCARD
13166 bool peek_eof() const noexcept(!TOML_COMPILER_HAS_EXCEPTIONS)
13167 {
13168 return reader_.peek_eof();
13169 }
13170
13171 #if !TOML_EXCEPTIONS
13172
13173 TOML_NODISCARD
13174 optional<parse_error>&& error() noexcept
13175 {
13176 return reader_.error();
13177 }
13178
13179 #endif
13180 };
13181 }
13182 TOML_ANON_NAMESPACE_END;
13183
13184 #if TOML_EXCEPTIONS
13185 #define TOML_RETURNS_BY_THROWING [[noreturn]]
13186 #else
13187 #define TOML_RETURNS_BY_THROWING
13188 #endif
13189
13190 TOML_ANON_NAMESPACE_START
13191 {
13192 template <typename... T>
13193 TOML_CONST_GETTER
13194 TOML_INTERNAL_LINKAGE
13195 constexpr bool is_match(char32_t codepoint, T... vals) noexcept
13196 {
13197 static_assert((std::is_same_v<char32_t, T> && ...));
13198 return ((codepoint == vals) || ...);
13199 }
13200
13201 template <uint64_t>
13202 struct parse_integer_traits;
13203 template <>
13204 struct parse_integer_traits<2>
13205 {
13206 static constexpr auto scope_qualifier = "binary integer"sv;
13207 static constexpr auto is_digit = impl::is_binary_digit;
13208 static constexpr auto is_signed = false;
13209 static constexpr auto max_digits = 63;
13210 static constexpr auto prefix_codepoint = U'b';
13211 static constexpr auto prefix = "b"sv;
13212 static constexpr auto full_prefix = "0b"sv;
13213 };
13214 template <>
13215 struct parse_integer_traits<8>
13216 {
13217 static constexpr auto scope_qualifier = "octal integer"sv;
13218 static constexpr auto is_digit = impl::is_octal_digit;
13219 static constexpr auto is_signed = false;
13220 static constexpr auto max_digits = 21; // strlen("777777777777777777777")
13221 static constexpr auto prefix_codepoint = U'o';
13222 static constexpr auto prefix = "o"sv;
13223 static constexpr auto full_prefix = "0o"sv;
13224 };
13225 template <>
13226 struct parse_integer_traits<10>
13227 {
13228 static constexpr auto scope_qualifier = "decimal integer"sv;
13229 static constexpr auto is_digit = impl::is_decimal_digit;
13230 static constexpr auto is_signed = true;
13231 static constexpr auto max_digits = 19; // strlen("9223372036854775807")
13232 static constexpr auto full_prefix = ""sv;
13233 };
13234 template <>
13235 struct parse_integer_traits<16>
13236 {
13237 static constexpr auto scope_qualifier = "hexadecimal integer"sv;
13238 static constexpr auto is_digit = impl::is_hexadecimal_digit;
13239 static constexpr auto is_signed = false;
13240 static constexpr auto max_digits = 16; // strlen("7FFFFFFFFFFFFFFF")
13241 static constexpr auto prefix_codepoint = U'x';
13242 static constexpr auto prefix = "x"sv;
13243 static constexpr auto full_prefix = "0x"sv;
13244 };
13245
13246 TOML_PURE_GETTER
13247 TOML_INTERNAL_LINKAGE
13248 std::string_view to_sv(node_type val) noexcept
13249 {
13250 return impl::node_type_friendly_names[impl::unwrap_enum(val)];
13251 }
13252
13253 TOML_PURE_GETTER
13254 TOML_INTERNAL_LINKAGE
13255 std::string_view to_sv(const std::string& str) noexcept
13256 {
13257 return std::string_view{ str };
13258 }
13259
13260 TOML_CONST_GETTER
13261 TOML_INTERNAL_LINKAGE
13262 std::string_view to_sv(bool val) noexcept
13263 {
13264 using namespace std::string_view_literals;
13265
13266 return val ? "true"sv : "false"sv;
13267 }
13268
13269 TOML_PURE_GETTER
13270 TOML_INTERNAL_LINKAGE
13271 std::string_view to_sv(const utf8_codepoint& cp) noexcept
13272 {
13273 if (cp.value <= U'\x1F')
13274 return impl::control_char_escapes[cp.value];
13275 else if (cp.value == U'\x7F')
13276 return "\\u007F"sv;
13277 else
13278 return std::string_view{ cp.bytes, cp.count };
13279 }
13280
13281 TOML_PURE_GETTER
13282 TOML_INTERNAL_LINKAGE
13283 std::string_view to_sv(const utf8_codepoint* cp) noexcept
13284 {
13285 if (cp)
13286 return to_sv(*cp);
13287 return ""sv;
13288 }
13289
13290 struct escaped_codepoint
13291 {
13292 const utf8_codepoint& cp;
13293 };
13294
13295 template <typename T>
13296 TOML_ATTR(nonnull)
13297 TOML_INTERNAL_LINKAGE
13298 void concatenate(char*& write_pos, char* const buf_end, const T& arg) noexcept
13299 {
13300 if TOML_UNLIKELY(write_pos >= buf_end)
13301 return;
13302
13303 using arg_type = impl::remove_cvref<T>;
13304
13305 // string views
13306 if constexpr (std::is_same_v<arg_type, std::string_view>)
13307 {
13308 const auto max_chars = static_cast<size_t>(buf_end - write_pos);
13309 const auto len = max_chars < arg.length() ? max_chars : arg.length();
13310 std::memcpy(write_pos, arg.data(), len);
13311 write_pos += len;
13312 }
13313
13314 // doubles
13315 else if constexpr (std::is_same_v<arg_type, double>)
13316 {
13317 #if TOML_FLOAT_CHARCONV
13318 const auto result = std::to_chars(write_pos, buf_end, arg);
13319 write_pos = result.ptr;
13320 #else
13321 std::ostringstream ss;
13322 ss.imbue(std::locale::classic());
13323 ss.precision(std::numeric_limits<arg_type>::max_digits10);
13324 ss << arg;
13325 concatenate(write_pos, buf_end, to_sv(std::move(ss).str()));
13326 #endif
13327 }
13328
13329 // 64-bit integers
13330 else if constexpr (impl::is_one_of<arg_type, int64_t, uint64_t>)
13331 {
13332 #if TOML_INT_CHARCONV
13333 const auto result = std::to_chars(write_pos, buf_end, arg);
13334 write_pos = result.ptr;
13335 #else
13336 std::ostringstream ss;
13337 ss.imbue(std::locale::classic());
13338 using cast_type = std::conditional_t<std::is_signed_v<arg_type>, int64_t, uint64_t>;
13339 ss << static_cast<cast_type>(arg);
13340 concatenate(write_pos, buf_end, to_sv(std::move(ss).str()));
13341 #endif
13342 }
13343
13344 // escaped_codepoint
13345 else if constexpr (std::is_same_v<arg_type, escaped_codepoint>)
13346 {
13347 if (arg.cp.value <= U'\x7F')
13348 concatenate(write_pos, buf_end, to_sv(arg.cp));
13349 else
13350 {
13351 auto val = static_cast<uint_least32_t>(arg.cp.value);
13352 const auto digits = val > 0xFFFFu ? 8u : 4u;
13353 constexpr auto mask = uint_least32_t{ 0xFu };
13354 char buf[10] = { '\\', digits > 4 ? 'U' : 'u' };
13355 for (auto i = 2u + digits; i-- > 2u;)
13356 {
13357 const auto hexdig = val & mask;
13358 buf[i] = static_cast<char>(hexdig >= 0xAu ? ('A' + (hexdig - 0xAu)) : ('0' + hexdig));
13359 val >>= 4;
13360 }
13361 concatenate(write_pos, buf_end, std::string_view{ buf, digits + 2u });
13362 }
13363 }
13364
13365 // all other floats (fallback - coerce to double)
13366 else if constexpr (std::is_floating_point_v<arg_type>)
13367 concatenate(write_pos, buf_end, static_cast<double>(arg));
13368
13369 // all other integers (fallback - coerce to (u)int64_t)
13370 else if constexpr (std::is_arithmetic_v<arg_type> && std::is_integral_v<arg_type>)
13371 {
13372 using cast_type = std::conditional_t<std::is_unsigned_v<arg_type>, uint64_t, int64_t>;
13373 concatenate(write_pos, buf_end, static_cast<cast_type>(arg));
13374 }
13375
13376 else
13377 {
13378 static_assert(
13379 impl::always_false<T>,
13380 "concatenate() inputs are limited to std::string_views, integers, floats, and escaped_codepoint");
13381 }
13382 }
13383
13384 struct error_builder
13385 {
13386 static constexpr std::size_t buf_size = 512;
13387 char buf[buf_size];
13388 char* write_pos = buf;
13389 char* const max_write_pos = buf + (buf_size - std::size_t{ 1 }); // allow for null terminator
13390
13391 TOML_NODISCARD_CTOR
13392 error_builder(std::string_view scope) noexcept
13393 {
13394 concatenate(write_pos, max_write_pos, "Error while parsing "sv);
13395 concatenate(write_pos, max_write_pos, scope);
13396 concatenate(write_pos, max_write_pos, ": "sv);
13397 }
13398
13399 template <typename T>
13400 void append(const T& arg) noexcept
13401 {
13402 concatenate(write_pos, max_write_pos, arg);
13403 }
13404
13405 TOML_RETURNS_BY_THROWING
13406 auto finish(const source_position& pos, const source_path_ptr& source_path) const
13407 {
13408 *write_pos = '\0';
13409
13410 #if TOML_EXCEPTIONS
13411 throw parse_error{ buf, pos, source_path };
13412 #else
13413 return parse_error{ std::string(buf, static_cast<size_t>(write_pos - buf)), pos, source_path };
13414 #endif
13415 }
13416
13417 TOML_DELETE_DEFAULTS(error_builder);
13418 };
13419
13420 struct parse_scope
13421 {
13422 std::string_view& storage_;
13423 std::string_view parent_;
13424
13425 TOML_NODISCARD_CTOR
13426 explicit parse_scope(std::string_view& current_scope, std::string_view new_scope) noexcept
13427 : storage_{ current_scope },
13428 parent_{ current_scope }
13429 {
13430 storage_ = new_scope;
13431 }
13432
13433 ~parse_scope() noexcept
13434 {
13435 storage_ = parent_;
13436 }
13437
13438 TOML_DELETE_DEFAULTS(parse_scope);
13439 };
13440 #define push_parse_scope_2(scope, line) parse_scope ps_##line(current_scope, scope)
13441 #define push_parse_scope_1(scope, line) push_parse_scope_2(scope, line)
13442 #define push_parse_scope(scope) push_parse_scope_1(scope, __LINE__)
13443
13444 struct parse_key_buffer
13445 {
13446 std::string buffer;
13447 std::vector<std::pair<size_t, size_t>> segments;
13448 std::vector<source_position> starts;
13449 std::vector<source_position> ends;
13450
13451 void clear() noexcept
13452 {
13453 buffer.clear();
13454 segments.clear();
13455 starts.clear();
13456 ends.clear();
13457 }
13458
13459 void push_back(std::string_view segment, source_position b, source_position e)
13460 {
13461 segments.push_back({ buffer.length(), segment.length() });
13462 buffer.append(segment);
13463 starts.push_back(b);
13464 ends.push_back(e);
13465 }
13466
13467 TOML_PURE_INLINE_GETTER
13468 std::string_view operator[](size_t i) const noexcept
13469 {
13470 return std::string_view{ buffer.c_str() + segments[i].first, segments[i].second };
13471 }
13472
13473 TOML_PURE_INLINE_GETTER
13474 std::string_view back() const noexcept
13475 {
13476 return (*this)[segments.size() - 1u];
13477 }
13478
13479 TOML_PURE_INLINE_GETTER
13480 bool empty() const noexcept
13481 {
13482 return segments.empty();
13483 }
13484
13485 TOML_PURE_INLINE_GETTER
13486 size_t size() const noexcept
13487 {
13488 return segments.size();
13489 }
13490 };
13491
13492 struct depth_counter_scope
13493 {
13494 size_t& depth_;
13495
13496 TOML_NODISCARD_CTOR
13497 explicit depth_counter_scope(size_t& depth) noexcept //
13498 : depth_{ depth }
13499 {
13500 depth_++;
13501 }
13502
13503 ~depth_counter_scope() noexcept
13504 {
13505 depth_--;
13506 }
13507
13508 TOML_DELETE_DEFAULTS(depth_counter_scope);
13509 };
13510
13511 struct parsed_string
13512 {
13513 std::string_view value;
13514 bool was_multi_line;
13515 };
13516
13517 struct table_vector_scope
13518 {
13519 std::vector<table*>& tables;
13520
13521 TOML_NODISCARD_CTOR
13522 explicit table_vector_scope(std::vector<table*>& tables_, table& tbl) //
13523 : tables{ tables_ }
13524 {
13525 tables.push_back(&tbl);
13526 }
13527
13528 ~table_vector_scope() noexcept
13529 {
13530 tables.pop_back();
13531 }
13532
13533 TOML_DELETE_DEFAULTS(table_vector_scope);
13534 };
13535 }
13536 TOML_ANON_NAMESPACE_END;
13537
13538 #if 1 // parser helper macros
13539
13540 // Q: "what the fuck is this? MACROS????"
13541 // A: The parser needs to work in exceptionless mode (returning error objects directly)
13542 // and exception mode (reporting parse failures by throwing). Two totally different control flows.
13543 // These macros encapsulate the differences between the two modes so I can write code code
13544 // as though I was only targeting one mode and not want yeet myself into the sun.
13545 // They're all #undef'd at the bottom of the parser's implementation so they should be harmless outside
13546 // of toml++.
13547
13548 #define is_eof() !cp
13549 #define assert_not_eof() TOML_ASSERT_ASSUME(cp != nullptr)
13550 #define return_if_eof(...) \
13551 do \
13552 { \
13553 if TOML_UNLIKELY(is_eof()) \
13554 return __VA_ARGS__; \
13555 } \
13556 while (false)
13557
13558 #if TOML_EXCEPTIONS
13559 #define is_error() false
13560 #define return_after_error(...) TOML_UNREACHABLE
13561 #define assert_not_error() static_assert(true)
13562 #define return_if_error(...) static_assert(true)
13563 #define return_if_error_or_eof(...) return_if_eof(__VA_ARGS__)
13564 #else
13565 #define is_error() !!err
13566 #define return_after_error(...) return __VA_ARGS__
13567 #define assert_not_error() TOML_ASSERT(!is_error())
13568 #define return_if_error(...) \
13569 do \
13570 { \
13571 if TOML_UNLIKELY(is_error()) \
13572 return __VA_ARGS__; \
13573 } \
13574 while (false)
13575 #define return_if_error_or_eof(...) \
13576 do \
13577 { \
13578 if TOML_UNLIKELY(is_eof() || is_error()) \
13579 return __VA_ARGS__; \
13580 } \
13581 while (false)
13582 #endif
13583
13584 #if defined(TOML_BREAK_AT_PARSE_ERRORS) && TOML_BREAK_AT_PARSE_ERRORS
13585 #if defined(__has_builtin)
13586 #if __has_builtin(__builtin_debugtrap)
13587 #define parse_error_break() __builtin_debugtrap()
13588 #elif __has_builtin(__debugbreak)
13589 #define parse_error_break() __debugbreak()
13590 #endif
13591 #endif
13592 #ifndef parse_error_break
13593 #if TOML_MSVC || TOML_ICC
13594 #define parse_error_break() __debugbreak()
13595 #else
13596 #define parse_error_break() TOML_ASSERT(false)
13597 #endif
13598 #endif
13599 #else
13600 #define parse_error_break() static_assert(true)
13601 #endif
13602
13603 #define set_error_and_return(ret, ...) \
13604 do \
13605 { \
13606 if (!is_error()) \
13607 set_error(__VA_ARGS__); \
13608 return_after_error(ret); \
13609 } \
13610 while (false)
13611
13612 #define set_error_and_return_default(...) set_error_and_return({}, __VA_ARGS__)
13613
13614 #define set_error_and_return_if_eof(...) \
13615 do \
13616 { \
13617 if TOML_UNLIKELY(is_eof()) \
13618 set_error_and_return(__VA_ARGS__, "encountered end-of-file"sv); \
13619 } \
13620 while (false)
13621
13622 #define advance_and_return_if_error(...) \
13623 do \
13624 { \
13625 assert_not_eof(); \
13626 advance(); \
13627 return_if_error(__VA_ARGS__); \
13628 } \
13629 while (false)
13630
13631 #define advance_and_return_if_error_or_eof(...) \
13632 do \
13633 { \
13634 assert_not_eof(); \
13635 advance(); \
13636 return_if_error(__VA_ARGS__); \
13637 set_error_and_return_if_eof(__VA_ARGS__); \
13638 } \
13639 while (false)
13640
13641 #endif // parser helper macros
13642
13643 TOML_IMPL_NAMESPACE_START
13644 {
13645 TOML_ABI_NAMESPACE_BOOL(TOML_EXCEPTIONS, impl_ex, impl_noex);
13646
13647 class parser
13648 {
13649 private:
13650 static constexpr size_t max_nested_values = TOML_MAX_NESTED_VALUES;
13651 static constexpr size_t max_dotted_keys_depth = TOML_MAX_DOTTED_KEYS_DEPTH;
13652
13653 utf8_buffered_reader reader;
13654 table root;
13655 source_position prev_pos = { 1, 1 };
13656 const utf8_codepoint* cp = {};
13657 std::vector<table*> implicit_tables;
13658 std::vector<table*> dotted_key_tables;
13659 std::vector<table*> open_inline_tables;
13660 std::vector<array*> table_arrays;
13661 parse_key_buffer key_buffer;
13662 std::string string_buffer;
13663 std::string recording_buffer; // for diagnostics
13664 bool recording = false, recording_whitespace = true;
13665 std::string_view current_scope;
13666 size_t nested_values = {};
13667 #if !TOML_EXCEPTIONS
13668 mutable optional<parse_error> err;
13669 #endif
13670
13671 TOML_NODISCARD
13672 source_position current_position(source_index fallback_offset = 0) const noexcept
13673 {
13674 if (!is_eof())
13675 return cp->position;
13676 return { prev_pos.line, static_cast<source_index>(prev_pos.column + fallback_offset) };
13677 }
13678
13679 template <typename... T>
13680 TOML_RETURNS_BY_THROWING
13681 TOML_NEVER_INLINE
13682 void set_error_at(source_position pos, const T&... reason) const
13683 {
13684 static_assert(sizeof...(T) > 0);
13685 return_if_error();
13686
13687 error_builder builder{ current_scope };
13688 (builder.append(reason), ...);
13689
13690 parse_error_break();
13691
13692 #if TOML_EXCEPTIONS
13693 builder.finish(pos, reader.source_path());
13694 #else
13695 err.emplace(builder.finish(pos, reader.source_path()));
13696 #endif
13697 }
13698
13699 template <typename... T>
13700 TOML_RETURNS_BY_THROWING
13701 void set_error(const T&... reason) const
13702 {
13703 set_error_at(current_position(1), reason...);
13704 }
13705
13706 void go_back(size_t count = 1) noexcept
13707 {
13708 return_if_error();
13709 TOML_ASSERT_ASSUME(count);
13710
13711 cp = reader.step_back(count);
13712 prev_pos = cp->position;
13713 }
13714
13715 void advance()
13716 {
13717 return_if_error();
13718 assert_not_eof();
13719
13720 prev_pos = cp->position;
13721 cp = reader.read_next();
13722
13723 #if !TOML_EXCEPTIONS
13724 if (reader.error())
13725 {
13726 err = std::move(reader.error());
13727 return;
13728 }
13729 #endif
13730
13731 if (recording && !is_eof())
13732 {
13733 if (recording_whitespace || !is_whitespace(*cp))
13734 recording_buffer.append(cp->bytes, cp->count);
13735 }
13736 }
13737
13738 void start_recording(bool include_current = true) noexcept
13739 {
13740 return_if_error();
13741
13742 recording = true;
13743 recording_whitespace = true;
13744 recording_buffer.clear();
13745 if (include_current && !is_eof())
13746 recording_buffer.append(cp->bytes, cp->count);
13747 }
13748
13749 void stop_recording(size_t pop_bytes = 0) noexcept
13750 {
13751 return_if_error();
13752
13753 recording = false;
13754 if (pop_bytes)
13755 {
13756 if (pop_bytes >= recording_buffer.length())
13757 recording_buffer.clear();
13758 else if (pop_bytes == 1u)
13759 recording_buffer.pop_back();
13760 else
13761 recording_buffer.erase(recording_buffer.begin()
13762 + static_cast<ptrdiff_t>(recording_buffer.length() - pop_bytes),
13763 recording_buffer.end());
13764 }
13765 }
13766
13767 bool consume_leading_whitespace()
13768 {
13769 return_if_error_or_eof({});
13770
13771 bool consumed = false;
13772 while (!is_eof() && is_horizontal_whitespace(*cp))
13773 {
13774 if TOML_UNLIKELY(!is_ascii_horizontal_whitespace(*cp))
13775 set_error_and_return_default("expected space or tab, saw '"sv, escaped_codepoint{ *cp }, "'"sv);
13776
13777 consumed = true;
13778 advance_and_return_if_error({});
13779 }
13780 return consumed;
13781 }
13782
13783 bool consume_line_break()
13784 {
13785 return_if_error_or_eof({});
13786
13787 if TOML_UNLIKELY(is_match(*cp, U'\v', U'\f'))
13788 set_error_and_return_default(
13789 R"(vertical tabs '\v' and form-feeds '\f' are not legal line breaks in TOML)"sv);
13790
13791 if (*cp == U'\r')
13792 {
13793 advance_and_return_if_error({}); // skip \r
13794
13795 if TOML_UNLIKELY(is_eof())
13796 set_error_and_return_default("expected '\\n' after '\\r', saw EOF"sv);
13797
13798 if TOML_UNLIKELY(*cp != U'\n')
13799 set_error_and_return_default("expected '\\n' after '\\r', saw '"sv,
13800 escaped_codepoint{ *cp },
13801 "'"sv);
13802 }
13803 else if (*cp != U'\n')
13804 return false;
13805
13806 advance_and_return_if_error({}); // skip \n
13807 return true;
13808 }
13809
13810 bool consume_rest_of_line()
13811 {
13812 return_if_error_or_eof({});
13813
13814 do
13815 {
13816 if (is_ascii_vertical_whitespace(*cp))
13817 return consume_line_break();
13818 else
13819 advance();
13820 return_if_error({});
13821 }
13822 while (!is_eof());
13823
13824 return true;
13825 }
13826
13827 bool consume_comment()
13828 {
13829 return_if_error_or_eof({});
13830
13831 if (*cp != U'#')
13832 return false;
13833
13834 push_parse_scope("comment"sv);
13835
13836 advance_and_return_if_error({}); // skip the '#'
13837
13838 while (!is_eof())
13839 {
13840 if (consume_line_break())
13841 return true;
13842 return_if_error({});
13843
13844 #if TOML_LANG_AT_LEAST(1, 0, 0)
13845
13846 // toml/issues/567 (disallow non-TAB control characters in comments)
13847 if TOML_UNLIKELY(is_nontab_control_character(*cp))
13848 set_error_and_return_default(
13849 "control characters other than TAB (U+0009) are explicitly prohibited in comments"sv);
13850
13851 // toml/pull/720 (disallow surrogates in comments)
13852 else if TOML_UNLIKELY(is_unicode_surrogate(*cp))
13853 set_error_and_return_default(
13854 "unicode surrogates (U+D800 to U+DFFF) are explicitly prohibited in comments"sv);
13855 #endif
13856
13857 advance_and_return_if_error({});
13858 }
13859
13860 return true;
13861 }
13862
13863 TOML_NODISCARD
13864 bool consume_expected_sequence(std::u32string_view seq)
13865 {
13866 return_if_error({});
13867 TOML_ASSERT(!seq.empty());
13868
13869 for (auto c : seq)
13870 {
13871 set_error_and_return_if_eof({});
13872 if (*cp != c)
13873 return false;
13874 advance_and_return_if_error({});
13875 }
13876 return true;
13877 }
13878
13879 template <typename T>
13880 TOML_NODISCARD
13881 bool consume_digit_sequence(T* digits, size_t len)
13882 {
13883 return_if_error({});
13884 TOML_ASSERT_ASSUME(digits);
13885 TOML_ASSERT_ASSUME(len);
13886
13887 for (size_t i = 0; i < len; i++)
13888 {
13889 set_error_and_return_if_eof({});
13890 if (!is_decimal_digit(*cp))
13891 return false;
13892
13893 digits[i] = static_cast<T>(*cp - U'0');
13894 advance_and_return_if_error({});
13895 }
13896 return true;
13897 }
13898
13899 template <typename T>
13900 TOML_NODISCARD
13901 size_t consume_variable_length_digit_sequence(T* buffer, size_t max_len)
13902 {
13903 return_if_error({});
13904 TOML_ASSERT_ASSUME(buffer);
13905 TOML_ASSERT_ASSUME(max_len);
13906
13907 size_t i = {};
13908 for (; i < max_len; i++)
13909 {
13910 if (is_eof() || !is_decimal_digit(*cp))
13911 break;
13912
13913 buffer[i] = static_cast<T>(*cp - U'0');
13914 advance_and_return_if_error({});
13915 }
13916 return i;
13917 }
13918
13919 TOML_NODISCARD
13920 TOML_NEVER_INLINE
13921 std::string_view parse_basic_string(bool multi_line)
13922 {
13923 return_if_error({});
13924 assert_not_eof();
13925 TOML_ASSERT_ASSUME(*cp == U'"');
13926 push_parse_scope("string"sv);
13927
13928 // skip the '"'
13929 advance_and_return_if_error_or_eof({});
13930
13931 // multi-line strings ignore a single line ending right at the beginning
13932 if (multi_line)
13933 {
13934 consume_line_break();
13935 return_if_error({});
13936 set_error_and_return_if_eof({});
13937 }
13938
13939 auto& str = string_buffer;
13940 str.clear();
13941 bool escaped = false;
13942 bool skipping_whitespace = false;
13943 do
13944 {
13945 if (escaped)
13946 {
13947 escaped = false;
13948
13949 // handle 'line ending slashes' in multi-line mode
13950 if (multi_line && is_whitespace(*cp))
13951 {
13952 consume_leading_whitespace();
13953
13954 if TOML_UNLIKELY(!consume_line_break())
13955 set_error_and_return_default(
13956 "line-ending backslashes must be the last non-whitespace character on the line"sv);
13957
13958 skipping_whitespace = true;
13959 return_if_error({});
13960 continue;
13961 }
13962
13963 bool skip_escaped_codepoint = true;
13964 assert_not_eof();
13965 switch (const auto escaped_codepoint = *cp)
13966 {
13967 // 'regular' escape codes
13968 case U'b': str += '\b'; break;
13969 case U'f': str += '\f'; break;
13970 case U'n': str += '\n'; break;
13971 case U'r': str += '\r'; break;
13972 case U't': str += '\t'; break;
13973 case U'"': str += '"'; break;
13974 case U'\\': str += '\\'; break;
13975
13976 #if TOML_LANG_UNRELEASED // toml/pull/790 (\e shorthand for \x1B)
13977 case U'e': str += '\x1B'; break;
13978 #else
13979 case U'e':
13980 set_error_and_return_default(
13981 "escape sequence '\\e' is not supported in TOML 1.0.0 and earlier"sv);
13982 #endif
13983
13984 #if TOML_LANG_UNRELEASED // toml/pull/796 (\xHH unicode scalar sequences)
13985 case U'x': [[fallthrough]];
13986 #else
13987 case U'x':
13988 set_error_and_return_default(
13989 "escape sequence '\\x' is not supported in TOML 1.0.0 and earlier"sv);
13990 #endif
13991
13992 // unicode scalar sequences
13993 case U'u': [[fallthrough]];
13994 case U'U':
13995 {
13996 push_parse_scope("unicode scalar sequence"sv);
13997 advance_and_return_if_error_or_eof({});
13998 skip_escaped_codepoint = false;
13999
14000 uint32_t place_value =
14001 escaped_codepoint == U'U' ? 0x10000000u : (escaped_codepoint == U'u' ? 0x1000u : 0x10u);
14002 uint32_t sequence_value{};
14003 while (place_value)
14004 {
14005 set_error_and_return_if_eof({});
14006
14007 if TOML_UNLIKELY(!is_hexadecimal_digit(*cp))
14008 set_error_and_return_default("expected hex digit, saw '"sv, to_sv(*cp), "'"sv);
14009
14010 sequence_value += place_value * hex_to_dec(*cp);
14011 place_value /= 16u;
14012 advance_and_return_if_error({});
14013 }
14014
14015 if TOML_UNLIKELY(is_unicode_surrogate(sequence_value))
14016 set_error_and_return_default(
14017 "unicode surrogates (U+D800 - U+DFFF) are explicitly prohibited"sv);
14018 else if TOML_UNLIKELY(sequence_value > 0x10FFFFu)
14019 set_error_and_return_default("values greater than U+10FFFF are invalid"sv);
14020
14021 if (sequence_value < 0x80)
14022 {
14023 str += static_cast<char>(sequence_value);
14024 }
14025 else if (sequence_value < 0x800u)
14026 {
14027 str += static_cast<char>((sequence_value >> 6) | 0xC0u);
14028 str += static_cast<char>((sequence_value & 0x3Fu) | 0x80u);
14029 }
14030 else if (sequence_value < 0x10000u)
14031 {
14032 str += static_cast<char>((sequence_value >> 12) | 0xE0u);
14033 str += static_cast<char>(((sequence_value >> 6) & 0x3Fu) | 0x80u);
14034 str += static_cast<char>((sequence_value & 0x3Fu) | 0x80u);
14035 }
14036 else if (sequence_value < 0x110000u)
14037 {
14038 str += static_cast<char>((sequence_value >> 18) | 0xF0u);
14039 str += static_cast<char>(((sequence_value >> 12) & 0x3Fu) | 0x80u);
14040 str += static_cast<char>(((sequence_value >> 6) & 0x3Fu) | 0x80u);
14041 str += static_cast<char>((sequence_value & 0x3Fu) | 0x80u);
14042 }
14043 break;
14044 }
14045
14046 // ???
14047 TOML_UNLIKELY_CASE
14048 default: set_error_and_return_default("unknown escape sequence '\\"sv, to_sv(*cp), "'"sv);
14049 }
14050
14051 if (skip_escaped_codepoint)
14052 advance_and_return_if_error_or_eof({});
14053 }
14054 else
14055 {
14056 // handle closing delimiters
14057 if (*cp == U'"')
14058 {
14059 if (multi_line)
14060 {
14061 size_t lookaheads = {};
14062 size_t consecutive_delimiters = 1;
14063 do
14064 {
14065 advance_and_return_if_error({});
14066 lookaheads++;
14067 if (!is_eof() && *cp == U'"')
14068 consecutive_delimiters++;
14069 else
14070 break;
14071 }
14072 while (lookaheads < 4u);
14073
14074 switch (consecutive_delimiters)
14075 {
14076 // """ " (one quote somewhere in a ML string)
14077 case 1:
14078 str += '"';
14079 skipping_whitespace = false;
14080 continue;
14081
14082 // """ "" (two quotes somewhere in a ML string)
14083 case 2:
14084 str.append("\"\""sv);
14085 skipping_whitespace = false;
14086 continue;
14087
14088 // """ """ (the end of the string)
14089 case 3: return str;
14090
14091 // """ """" (one at the end of the string)
14092 case 4: str += '"'; return str;
14093
14094 // """ """"" (two quotes at the end of the string)
14095 case 5:
14096 str.append("\"\""sv);
14097 advance_and_return_if_error({}); // skip the last '"'
14098 return str;
14099
14100 default: TOML_UNREACHABLE;
14101 }
14102 }
14103 else
14104 {
14105 advance_and_return_if_error({}); // skip the closing delimiter
14106 return str;
14107 }
14108 }
14109
14110 // handle escapes
14111 else if (*cp == U'\\')
14112 {
14113 advance_and_return_if_error_or_eof({}); // skip the '\'
14114 skipping_whitespace = false;
14115 escaped = true;
14116 continue;
14117 }
14118
14119 // handle line endings in multi-line mode
14120 if (multi_line && is_ascii_vertical_whitespace(*cp))
14121 {
14122 consume_line_break();
14123 return_if_error({});
14124 if (!skipping_whitespace)
14125 str += '\n';
14126 continue;
14127 }
14128
14129 // handle control characters
14130 if TOML_UNLIKELY(is_nontab_control_character(*cp))
14131 set_error_and_return_default(
14132 "unescaped control characters other than TAB (U+0009) are explicitly prohibited"sv);
14133
14134 #if TOML_LANG_AT_LEAST(1, 0, 0)
14135
14136 // handle surrogates in strings
14137 if TOML_UNLIKELY(is_unicode_surrogate(*cp))
14138 set_error_and_return_default(
14139 "unescaped unicode surrogates (U+D800 to U+DFFF) are explicitly prohibited"sv);
14140 #endif
14141
14142 if (multi_line)
14143 {
14144 if (!skipping_whitespace || !is_horizontal_whitespace(*cp))
14145 {
14146 skipping_whitespace = false;
14147 str.append(cp->bytes, cp->count);
14148 }
14149 }
14150 else
14151 str.append(cp->bytes, cp->count);
14152
14153 advance_and_return_if_error({});
14154 }
14155 }
14156 while (!is_eof());
14157
14158 set_error_and_return_default("encountered end-of-file"sv);
14159 }
14160
14161 TOML_NODISCARD
14162 TOML_NEVER_INLINE
14163 std::string_view parse_literal_string(bool multi_line)
14164 {
14165 return_if_error({});
14166 assert_not_eof();
14167 TOML_ASSERT_ASSUME(*cp == U'\'');
14168 push_parse_scope("literal string"sv);
14169
14170 // skip the delimiter
14171 advance_and_return_if_error_or_eof({});
14172
14173 // multi-line strings ignore a single line ending right at the beginning
14174 if (multi_line)
14175 {
14176 consume_line_break();
14177 return_if_error({});
14178 set_error_and_return_if_eof({});
14179 }
14180
14181 auto& str = string_buffer;
14182 str.clear();
14183 do
14184 {
14185 return_if_error({});
14186
14187 // handle closing delimiters
14188 if (*cp == U'\'')
14189 {
14190 if (multi_line)
14191 {
14192 size_t lookaheads = {};
14193 size_t consecutive_delimiters = 1;
14194 do
14195 {
14196 advance_and_return_if_error({});
14197 lookaheads++;
14198 if (!is_eof() && *cp == U'\'')
14199 consecutive_delimiters++;
14200 else
14201 break;
14202 }
14203 while (lookaheads < 4u);
14204
14205 switch (consecutive_delimiters)
14206 {
14207 // ''' ' (one quote somewhere in a ML string)
14208 case 1: str += '\''; continue;
14209
14210 // ''' '' (two quotes somewhere in a ML string)
14211 case 2: str.append("''"sv); continue;
14212
14213 // ''' ''' (the end of the string)
14214 case 3: return str;
14215
14216 // ''' '''' (one at the end of the string)
14217 case 4: str += '\''; return str;
14218
14219 // ''' ''''' (two quotes at the end of the string)
14220 case 5:
14221 str.append("''"sv);
14222 advance_and_return_if_error({}); // skip the last '
14223 return str;
14224
14225 default: TOML_UNREACHABLE;
14226 }
14227 }
14228 else
14229 {
14230 advance_and_return_if_error({}); // skip the closing delimiter
14231 return str;
14232 }
14233 }
14234
14235 // handle line endings in multi-line mode
14236 if (multi_line && is_ascii_vertical_whitespace(*cp))
14237 {
14238 consume_line_break();
14239 return_if_error({});
14240 str += '\n';
14241 continue;
14242 }
14243
14244 // handle control characters
14245 if TOML_UNLIKELY(is_nontab_control_character(*cp))
14246 set_error_and_return_default(
14247 "control characters other than TAB (U+0009) are explicitly prohibited"sv);
14248
14249 #if TOML_LANG_AT_LEAST(1, 0, 0)
14250
14251 // handle surrogates in strings
14252 if TOML_UNLIKELY(is_unicode_surrogate(*cp))
14253 set_error_and_return_default("unicode surrogates (U+D800 - U+DFFF) are explicitly prohibited"sv);
14254 #endif
14255
14256 str.append(cp->bytes, cp->count);
14257 advance_and_return_if_error({});
14258 }
14259 while (!is_eof());
14260
14261 set_error_and_return_default("encountered end-of-file"sv);
14262 }
14263
14264 TOML_NODISCARD
14265 TOML_NEVER_INLINE
14266 parsed_string parse_string()
14267 {
14268 return_if_error({});
14269 assert_not_eof();
14270 TOML_ASSERT_ASSUME(is_string_delimiter(*cp));
14271 push_parse_scope("string"sv);
14272
14273 // get the first three characters to determine the string type
14274 const auto first = cp->value;
14275 advance_and_return_if_error_or_eof({});
14276 const auto second = cp->value;
14277 advance_and_return_if_error({});
14278 const auto third = cp ? cp->value : U'\0';
14279
14280 // if we were eof at the third character then first and second need to be
14281 // the same string character (otherwise it's an unterminated string)
14282 if (is_eof())
14283 {
14284 if (second == first)
14285 return {};
14286
14287 set_error_and_return_default("encountered end-of-file"sv);
14288 }
14289
14290 // if the first three characters are all the same string delimiter then
14291 // it's a multi-line string.
14292 else if (first == second && first == third)
14293 {
14294 return { first == U'\'' ? parse_literal_string(true) : parse_basic_string(true), true };
14295 }
14296
14297 // otherwise it's just a regular string.
14298 else
14299 {
14300 // step back two characters so that the current
14301 // character is the string delimiter
14302 go_back(2u);
14303
14304 return { first == U'\'' ? parse_literal_string(false) : parse_basic_string(false), false };
14305 }
14306 }
14307
14308 TOML_NODISCARD
14309 TOML_NEVER_INLINE
14310 std::string_view parse_bare_key_segment()
14311 {
14312 return_if_error({});
14313 assert_not_eof();
14314 TOML_ASSERT_ASSUME(is_bare_key_character(*cp));
14315
14316 string_buffer.clear();
14317
14318 while (!is_eof())
14319 {
14320 if (!is_bare_key_character(*cp))
14321 break;
14322
14323 string_buffer.append(cp->bytes, cp->count);
14324 advance_and_return_if_error({});
14325 }
14326
14327 return string_buffer;
14328 }
14329
14330 TOML_NODISCARD
14331 TOML_NEVER_INLINE
14332 bool parse_boolean()
14333 {
14334 return_if_error({});
14335 assert_not_eof();
14336 TOML_ASSERT_ASSUME(is_match(*cp, U't', U'f', U'T', U'F'));
14337 push_parse_scope("boolean"sv);
14338
14339 start_recording(true);
14340 auto result = is_match(*cp, U't', U'T');
14341 if (!consume_expected_sequence(result ? U"true"sv : U"false"sv))
14342 set_error_and_return_default("expected '"sv,
14343 to_sv(result),
14344 "', saw '"sv,
14345 to_sv(recording_buffer),
14346 "'"sv);
14347 stop_recording();
14348
14349 if (cp && !is_value_terminator(*cp))
14350 set_error_and_return_default("expected value-terminator, saw '"sv, to_sv(*cp), "'"sv);
14351
14352 return result;
14353 }
14354
14355 TOML_NODISCARD
14356 TOML_NEVER_INLINE
14357 double parse_inf_or_nan()
14358 {
14359 return_if_error({});
14360 assert_not_eof();
14361 TOML_ASSERT_ASSUME(is_match(*cp, U'i', U'n', U'I', U'N', U'+', U'-'));
14362 push_parse_scope("floating-point"sv);
14363
14364 start_recording(true);
14365 const bool negative = *cp == U'-';
14366 if (negative || *cp == U'+')
14367 advance_and_return_if_error_or_eof({});
14368
14369 const bool inf = is_match(*cp, U'i', U'I');
14370 if (!consume_expected_sequence(inf ? U"inf"sv : U"nan"sv))
14371 set_error_and_return_default("expected '"sv,
14372 inf ? "inf"sv : "nan"sv,
14373 "', saw '"sv,
14374 to_sv(recording_buffer),
14375 "'"sv);
14376 stop_recording();
14377
14378 if (cp && !is_value_terminator(*cp))
14379 set_error_and_return_default("expected value-terminator, saw '"sv, to_sv(*cp), "'"sv);
14380
14381 return inf ? (negative ? -std::numeric_limits<double>::infinity() : std::numeric_limits<double>::infinity())
14382 : std::numeric_limits<double>::quiet_NaN();
14383 }
14384
14385 TOML_NODISCARD
14386 TOML_NEVER_INLINE
14387 double parse_float()
14388 {
14389 return_if_error({});
14390 assert_not_eof();
14391 TOML_ASSERT_ASSUME(is_match(*cp, U'+', U'-', U'.') || is_decimal_digit(*cp));
14392 push_parse_scope("floating-point"sv);
14393
14394 // sign
14395 const int sign = *cp == U'-' ? -1 : 1;
14396 if (is_match(*cp, U'+', U'-'))
14397 advance_and_return_if_error_or_eof({});
14398
14399 // consume value chars
14400 char chars[utf8_buffered_reader::max_history_length];
14401 size_t length = {};
14402 const utf8_codepoint* prev = {};
14403 bool seen_decimal = false, seen_exponent = false;
14404 char first_integer_part = '\0';
14405 while (!is_eof() && !is_value_terminator(*cp))
14406 {
14407 if (*cp == U'_')
14408 {
14409 if (!prev || !is_decimal_digit(*prev))
14410 set_error_and_return_default("underscores may only follow digits"sv);
14411
14412 prev = cp;
14413 advance_and_return_if_error_or_eof({});
14414 continue;
14415 }
14416 else if TOML_UNLIKELY(prev && *prev == U'_' && !is_decimal_digit(*cp))
14417 set_error_and_return_default("underscores must be followed by digits"sv);
14418 else if TOML_UNLIKELY(length == sizeof(chars))
14419 set_error_and_return_default("exceeds length limit of "sv,
14420 sizeof(chars),
14421 " digits"sv,
14422 (seen_exponent ? ""sv : " (consider using exponent notation)"sv));
14423 else if (*cp == U'.')
14424 {
14425 // .1
14426 // -.1
14427 // +.1 (no integer part)
14428 if (!first_integer_part)
14429 set_error_and_return_default("expected decimal digit, saw '.'"sv);
14430
14431 // 1.0e+.10 (exponent cannot have '.')
14432 else if (seen_exponent)
14433 set_error_and_return_default("expected exponent decimal digit or sign, saw '.'"sv);
14434
14435 // 1.0.e+.10
14436 // 1..0
14437 // (multiple '.')
14438 else if (seen_decimal)
14439 set_error_and_return_default("expected decimal digit or exponent, saw '.'"sv);
14440
14441 seen_decimal = true;
14442 }
14443 else if (is_match(*cp, U'e', U'E'))
14444 {
14445 if (prev && !is_decimal_digit(*prev))
14446 set_error_and_return_default("expected decimal digit, saw '"sv, to_sv(*cp), "'"sv);
14447
14448 // 1.0ee+10 (multiple 'e')
14449 else if (seen_exponent)
14450 set_error_and_return_default("expected decimal digit, saw '"sv, to_sv(*cp), "'"sv);
14451
14452 seen_decimal = true; // implied
14453 seen_exponent = true;
14454 }
14455 else if (is_match(*cp, U'+', U'-'))
14456 {
14457 // 1.-0 (sign in mantissa)
14458 if (!seen_exponent)
14459 set_error_and_return_default("expected decimal digit or '.', saw '"sv, to_sv(*cp), "'"sv);
14460
14461 // 1.0e1-0 (misplaced exponent sign)
14462 else if (!is_match(*prev, U'e', U'E'))
14463 set_error_and_return_default("expected exponent digit, saw '"sv, to_sv(*cp), "'"sv);
14464 }
14465 else if (is_decimal_digit(*cp))
14466 {
14467 if (!seen_decimal)
14468 {
14469 if (!first_integer_part)
14470 first_integer_part = static_cast<char>(cp->bytes[0]);
14471 else if (first_integer_part == '0')
14472 set_error_and_return_default("leading zeroes are prohibited"sv);
14473 }
14474 }
14475 else
14476 set_error_and_return_default("expected decimal digit, saw '"sv, to_sv(*cp), "'"sv);
14477
14478 chars[length++] = static_cast<char>(cp->bytes[0]);
14479 prev = cp;
14480 advance_and_return_if_error({});
14481 }
14482
14483 // sanity-check ending state
14484 if (prev)
14485 {
14486 if (*prev == U'_')
14487 {
14488 set_error_and_return_if_eof({});
14489 set_error_and_return_default("underscores must be followed by digits"sv);
14490 }
14491 else if (is_match(*prev, U'e', U'E', U'+', U'-', U'.'))
14492 {
14493 set_error_and_return_if_eof({});
14494 set_error_and_return_default("expected decimal digit, saw '"sv, to_sv(*cp), "'"sv);
14495 }
14496 }
14497
14498 // convert to double
14499 double result;
14500 #if TOML_FLOAT_CHARCONV
14501 {
14502 auto fc_result = std::from_chars(chars, chars + length, result);
14503 switch (fc_result.ec)
14504 {
14505 TOML_LIKELY_CASE
14506 case std::errc{}: // ok
14507 return result * sign;
14508
14509 case std::errc::invalid_argument:
14510 set_error_and_return_default("'"sv,
14511 std::string_view{ chars, length },
14512 "' could not be interpreted as a value"sv);
14513 break;
14514
14515 case std::errc::result_out_of_range:
14516 set_error_and_return_default("'"sv,
14517 std::string_view{ chars, length },
14518 "' is not representable in 64 bits"sv);
14519 break;
14520
14521 default: //??
14522 set_error_and_return_default("an unspecified error occurred while trying to interpret '"sv,
14523 std::string_view{ chars, length },
14524 "' as a value"sv);
14525 }
14526 }
14527 #else
14528 {
14529 std::stringstream ss;
14530 ss.imbue(std::locale::classic());
14531 ss.write(chars, static_cast<std::streamsize>(length));
14532 if ((ss >> result))
14533 return result * sign;
14534 else
14535 set_error_and_return_default("'"sv,
14536 std::string_view{ chars, length },
14537 "' could not be interpreted as a value"sv);
14538 }
14539 #endif
14540 }
14541
14542 TOML_NODISCARD
14543 TOML_NEVER_INLINE
14544 double parse_hex_float()
14545 {
14546 return_if_error({});
14547 assert_not_eof();
14548 TOML_ASSERT_ASSUME(is_match(*cp, U'0', U'+', U'-'));
14549 push_parse_scope("hexadecimal floating-point"sv);
14550
14551 #if TOML_LANG_UNRELEASED // toml/issues/562 (hexfloats)
14552
14553 // sign
14554 const int sign = *cp == U'-' ? -1 : 1;
14555 if (is_match(*cp, U'+', U'-'))
14556 advance_and_return_if_error_or_eof({});
14557
14558 // '0'
14559 if (*cp != U'0')
14560 set_error_and_return_default(" expected '0', saw '"sv, to_sv(*cp), "'"sv);
14561 advance_and_return_if_error_or_eof({});
14562
14563 // 'x' or 'X'
14564 if (!is_match(*cp, U'x', U'X'))
14565 set_error_and_return_default("expected 'x' or 'X', saw '"sv, to_sv(*cp), "'"sv);
14566 advance_and_return_if_error_or_eof({});
14567
14568 // <HEX DIGITS> ([.]<HEX DIGITS>)? [pP] [+-]? <DEC DIGITS>
14569
14570 // consume value fragments
14571 struct fragment
14572 {
14573 char chars[24];
14574 size_t length;
14575 double value;
14576 };
14577 fragment fragments[] = {
14578 {}, // mantissa, whole part
14579 {}, // mantissa, fractional part
14580 {} // exponent
14581 };
14582 fragment* current_fragment = fragments;
14583 const utf8_codepoint* prev = {};
14584 int exponent_sign = 1;
14585 while (!is_eof() && !is_value_terminator(*cp))
14586 {
14587 if (*cp == U'_')
14588 {
14589 if (!prev || !is_hexadecimal_digit(*prev))
14590 set_error_and_return_default("underscores may only follow digits"sv);
14591
14592 prev = cp;
14593 advance_and_return_if_error_or_eof({});
14594 continue;
14595 }
14596 else if (prev && *prev == U'_' && !is_hexadecimal_digit(*cp))
14597 set_error_and_return_default("underscores must be followed by digits"sv);
14598 else if (*cp == U'.')
14599 {
14600 // 0x10.0p-.0 (exponent cannot have '.')
14601 if (current_fragment == fragments + 2)
14602 set_error_and_return_default("expected exponent digit or sign, saw '.'"sv);
14603
14604 // 0x10.0.p-0 (multiple '.')
14605 else if (current_fragment == fragments + 1)
14606 set_error_and_return_default("expected hexadecimal digit or exponent, saw '.'"sv);
14607
14608 else
14609 current_fragment++;
14610 }
14611 else if (is_match(*cp, U'p', U'P'))
14612 {
14613 // 0x10.0pp-0 (multiple 'p')
14614 if (current_fragment == fragments + 2)
14615 set_error_and_return_default("expected exponent digit or sign, saw '"sv, to_sv(*cp), "'"sv);
14616
14617 // 0x.p-0 (mantissa is just '.')
14618 else if (fragments[0].length == 0u && fragments[1].length == 0u)
14619 set_error_and_return_default("expected hexadecimal digit, saw '"sv, to_sv(*cp), "'"sv);
14620
14621 else
14622 current_fragment = fragments + 2;
14623 }
14624 else if (is_match(*cp, U'+', U'-'))
14625 {
14626 // 0x-10.0p-0 (sign in mantissa)
14627 if (current_fragment != fragments + 2)
14628 set_error_and_return_default("expected hexadecimal digit or '.', saw '"sv, to_sv(*cp), "'"sv);
14629
14630 // 0x10.0p0- (misplaced exponent sign)
14631 else if (!is_match(*prev, U'p', U'P'))
14632 set_error_and_return_default("expected exponent digit, saw '"sv, to_sv(*cp), "'"sv);
14633
14634 else
14635 exponent_sign = *cp == U'-' ? -1 : 1;
14636 }
14637 else if (current_fragment < fragments + 2 && !is_hexadecimal_digit(*cp))
14638 set_error_and_return_default("expected hexadecimal digit or '.', saw '"sv, to_sv(*cp), "'"sv);
14639 else if (current_fragment == fragments + 2 && !is_decimal_digit(*cp))
14640 set_error_and_return_default("expected exponent digit or sign, saw '"sv, to_sv(*cp), "'"sv);
14641 else if (current_fragment->length == sizeof(fragment::chars))
14642 set_error_and_return_default("fragment exceeeds maximum length of "sv,
14643 sizeof(fragment::chars),
14644 " characters"sv);
14645 else
14646 current_fragment->chars[current_fragment->length++] = static_cast<char>(cp->bytes[0]);
14647
14648 prev = cp;
14649 advance_and_return_if_error({});
14650 }
14651
14652 // sanity-check ending state
14653 if (current_fragment != fragments + 2 || current_fragment->length == 0u)
14654 {
14655 set_error_and_return_if_eof({});
14656 set_error_and_return_default("missing exponent"sv);
14657 }
14658 else if (prev && *prev == U'_')
14659 {
14660 set_error_and_return_if_eof({});
14661 set_error_and_return_default("underscores must be followed by digits"sv);
14662 }
14663
14664 // calculate values for the three fragments
14665 for (int fragment_idx = 0; fragment_idx < 3; fragment_idx++)
14666 {
14667 auto& f = fragments[fragment_idx];
14668 const uint32_t base = fragment_idx == 2 ? 10u : 16u;
14669
14670 // left-trim zeroes
14671 const char* c = f.chars;
14672 size_t sig = {};
14673 while (f.length && *c == '0')
14674 {
14675 f.length--;
14676 c++;
14677 sig++;
14678 }
14679 if (!f.length)
14680 continue;
14681
14682 // calculate value
14683 auto place = 1u;
14684 for (size_t i = 0; i < f.length - 1u; i++)
14685 place *= base;
14686 uint32_t val{};
14687 while (place)
14688 {
14689 if (base == 16)
14690 val += place * hex_to_dec(*c);
14691 else
14692 val += place * static_cast<uint32_t>(*c - '0');
14693 if (fragment_idx == 1)
14694 sig++;
14695 c++;
14696 place /= base;
14697 }
14698 f.value = static_cast<double>(val);
14699
14700 // shift the fractional part
14701 if (fragment_idx == 1)
14702 {
14703 while (sig--)
14704 f.value /= base;
14705 }
14706 }
14707
14708 return (fragments[0].value + fragments[1].value) * pow(2.0, fragments[2].value * exponent_sign) * sign;
14709
14710 #else // !TOML_LANG_UNRELEASED
14711
14712 set_error_and_return_default("hexadecimal floating-point values are not supported "
14713 "in TOML 1.0.0 and earlier"sv);
14714
14715 #endif // !TOML_LANG_UNRELEASED
14716 }
14717
14718 template <uint64_t base>
14719 TOML_NODISCARD
14720 TOML_NEVER_INLINE
14721 int64_t parse_integer()
14722 {
14723 return_if_error({});
14724 assert_not_eof();
14725 using traits = parse_integer_traits<base>;
14726 push_parse_scope(traits::scope_qualifier);
14727
14728 [[maybe_unused]] int64_t sign = 1;
14729 if constexpr (traits::is_signed)
14730 {
14731 sign = *cp == U'-' ? -1 : 1;
14732 if (is_match(*cp, U'+', U'-'))
14733 advance_and_return_if_error_or_eof({});
14734 }
14735
14736 if constexpr (base == 10)
14737 {
14738 if (!traits::is_digit(*cp))
14739 set_error_and_return_default("expected expected digit or sign, saw '"sv, to_sv(*cp), "'"sv);
14740 }
14741 else
14742 {
14743 // '0'
14744 if (*cp != U'0')
14745 set_error_and_return_default("expected '0', saw '"sv, to_sv(*cp), "'"sv);
14746 advance_and_return_if_error_or_eof({});
14747
14748 // 'b', 'o', 'x'
14749 if (*cp != traits::prefix_codepoint)
14750 set_error_and_return_default("expected '"sv, traits::prefix, "', saw '"sv, to_sv(*cp), "'"sv);
14751 advance_and_return_if_error_or_eof({});
14752
14753 if (!traits::is_digit(*cp))
14754 set_error_and_return_default("expected digit, saw '"sv, to_sv(*cp), "'"sv);
14755 }
14756
14757 // consume digits
14758 char digits[utf8_buffered_reader::max_history_length];
14759 size_t length = {};
14760 const utf8_codepoint* prev = {};
14761 while (!is_eof() && !is_value_terminator(*cp))
14762 {
14763 if (*cp == U'_')
14764 {
14765 if (!prev || !traits::is_digit(*prev))
14766 set_error_and_return_default("underscores may only follow digits"sv);
14767
14768 prev = cp;
14769 advance_and_return_if_error_or_eof({});
14770 continue;
14771 }
14772 else if TOML_UNLIKELY(prev && *prev == U'_' && !traits::is_digit(*cp))
14773 set_error_and_return_default("underscores must be followed by digits"sv);
14774 else if TOML_UNLIKELY(!traits::is_digit(*cp))
14775 set_error_and_return_default("expected digit, saw '"sv, to_sv(*cp), "'"sv);
14776 else if TOML_UNLIKELY(length == sizeof(digits))
14777 set_error_and_return_default("exceeds length limit of "sv, sizeof(digits), " digits"sv);
14778 else
14779 digits[length++] = static_cast<char>(cp->bytes[0]);
14780
14781 prev = cp;
14782 advance_and_return_if_error({});
14783 }
14784
14785 // sanity check ending state
14786 if (prev && *prev == U'_')
14787 {
14788 set_error_and_return_if_eof({});
14789 set_error_and_return_default("underscores must be followed by digits"sv);
14790 }
14791
14792 // single digits can be converted trivially
14793 if (length == 1u)
14794 {
14795 int64_t result;
14796
14797 if constexpr (base == 16)
14798 result = static_cast<int64_t>(hex_to_dec(digits[0]));
14799 else
14800 result = static_cast<int64_t>(digits[0] - '0');
14801
14802 if constexpr (traits::is_signed)
14803 result *= sign;
14804
14805 return result;
14806 }
14807
14808 // bin, oct and hex allow leading zeroes so trim them first
14809 const char* end = digits + length;
14810 const char* msd = digits;
14811 if constexpr (base != 10)
14812 {
14813 while (msd < end && *msd == '0')
14814 msd++;
14815 if (msd == end)
14816 return 0ll;
14817 }
14818
14819 // decimal integers do not allow leading zeroes
14820 else
14821 {
14822 if TOML_UNLIKELY(digits[0] == '0')
14823 set_error_and_return_default("leading zeroes are prohibited"sv);
14824 }
14825
14826 // range check
14827 if TOML_UNLIKELY(static_cast<size_t>(end - msd) > traits::max_digits)
14828 set_error_and_return_default("'"sv,
14829 traits::full_prefix,
14830 std::string_view{ digits, length },
14831 "' is not representable as a signed 64-bit integer"sv);
14832
14833 // do the thing
14834 {
14835 uint64_t result = {};
14836 {
14837 uint64_t power = 1;
14838 while (--end >= msd)
14839 {
14840 if constexpr (base == 16)
14841 result += power * hex_to_dec(*end);
14842 else
14843 result += power * static_cast<uint64_t>(*end - '0');
14844
14845 power *= base;
14846 }
14847 }
14848
14849 // range check
14850 static constexpr auto i64_max = static_cast<uint64_t>((std::numeric_limits<int64_t>::max)());
14851 if TOML_UNLIKELY(result > i64_max + (sign < 0 ? 1u : 0u))
14852 set_error_and_return_default("'"sv,
14853 traits::full_prefix,
14854 std::string_view{ digits, length },
14855 "' is not representable as a signed 64-bit integer"sv);
14856
14857 if constexpr (traits::is_signed)
14858 {
14859 // avoid signed multiply UB when parsing INT64_MIN
14860 if TOML_UNLIKELY(sign < 0 && result == i64_max + 1u)
14861 return (std::numeric_limits<int64_t>::min)();
14862
14863 return static_cast<int64_t>(result) * sign;
14864 }
14865 else
14866 return static_cast<int64_t>(result);
14867 }
14868 }
14869
14870 TOML_NODISCARD
14871 TOML_NEVER_INLINE
14872 date parse_date(bool part_of_datetime = false)
14873 {
14874 return_if_error({});
14875 assert_not_eof();
14876 TOML_ASSERT_ASSUME(is_decimal_digit(*cp));
14877 push_parse_scope("date"sv);
14878
14879 // "YYYY"
14880 uint32_t digits[4];
14881 if (!consume_digit_sequence(digits, 4u))
14882 set_error_and_return_default("expected 4-digit year, saw '"sv, to_sv(cp), "'"sv);
14883 const auto year = digits[3] + digits[2] * 10u + digits[1] * 100u + digits[0] * 1000u;
14884 const auto is_leap_year = (year % 4u == 0u) && ((year % 100u != 0u) || (year % 400u == 0u));
14885 set_error_and_return_if_eof({});
14886
14887 // '-'
14888 if (*cp != U'-')
14889 set_error_and_return_default("expected '-', saw '"sv, to_sv(*cp), "'"sv);
14890 advance_and_return_if_error_or_eof({});
14891
14892 // "MM"
14893 if (!consume_digit_sequence(digits, 2u))
14894 set_error_and_return_default("expected 2-digit month, saw '"sv, to_sv(cp), "'"sv);
14895 const auto month = digits[1] + digits[0] * 10u;
14896 if (month == 0u || month > 12u)
14897 set_error_and_return_default("expected month between 1 and 12 (inclusive), saw "sv, month);
14898 const auto max_days_in_month = month == 2u
14899 ? (is_leap_year ? 29u : 28u)
14900 : (month == 4u || month == 6u || month == 9u || month == 11u ? 30u : 31u);
14901 set_error_and_return_if_eof({});
14902
14903 // '-'
14904 if (*cp != U'-')
14905 set_error_and_return_default("expected '-', saw '"sv, to_sv(*cp), "'"sv);
14906 advance_and_return_if_error_or_eof({});
14907
14908 // "DD"
14909 if (!consume_digit_sequence(digits, 2u))
14910 set_error_and_return_default("expected 2-digit day, saw '"sv, to_sv(cp), "'"sv);
14911 const auto day = digits[1] + digits[0] * 10u;
14912 if (day == 0u || day > max_days_in_month)
14913 set_error_and_return_default("expected day between 1 and "sv,
14914 max_days_in_month,
14915 " (inclusive), saw "sv,
14916 day);
14917
14918 if (!part_of_datetime && !is_eof() && !is_value_terminator(*cp))
14919 set_error_and_return_default("expected value-terminator, saw '"sv, to_sv(*cp), "'"sv);
14920
14921 return { year, month, day };
14922 }
14923
14924 TOML_NODISCARD
14925 TOML_NEVER_INLINE
14926 time parse_time(bool part_of_datetime = false)
14927 {
14928 return_if_error({});
14929 assert_not_eof();
14930 TOML_ASSERT_ASSUME(is_decimal_digit(*cp));
14931 push_parse_scope("time"sv);
14932
14933 static constexpr size_t max_digits = 64; // far more than necessary but needed to allow fractional
14934 // millisecond truncation per the spec
14935 uint32_t digits[max_digits];
14936
14937 // "HH"
14938 if (!consume_digit_sequence(digits, 2u))
14939 set_error_and_return_default("expected 2-digit hour, saw '"sv, to_sv(cp), "'"sv);
14940 const auto hour = digits[1] + digits[0] * 10u;
14941 if (hour > 23u)
14942 set_error_and_return_default("expected hour between 0 to 59 (inclusive), saw "sv, hour);
14943 set_error_and_return_if_eof({});
14944
14945 // ':'
14946 if (*cp != U':')
14947 set_error_and_return_default("expected ':', saw '"sv, to_sv(*cp), "'"sv);
14948 advance_and_return_if_error_or_eof({});
14949
14950 // "MM"
14951 if (!consume_digit_sequence(digits, 2u))
14952 set_error_and_return_default("expected 2-digit minute, saw '"sv, to_sv(cp), "'"sv);
14953 const auto minute = digits[1] + digits[0] * 10u;
14954 if (minute > 59u)
14955 set_error_and_return_default("expected minute between 0 and 59 (inclusive), saw "sv, minute);
14956 auto time = toml::time{ hour, minute };
14957
14958 // ':'
14959 if constexpr (TOML_LANG_UNRELEASED) // toml/issues/671 (allow omission of seconds)
14960 {
14961 if (is_eof() || is_value_terminator(*cp) || (part_of_datetime && is_match(*cp, U'+', U'-', U'Z', U'z')))
14962 return time;
14963 }
14964 else
14965 set_error_and_return_if_eof({});
14966 if (*cp != U':')
14967 set_error_and_return_default("expected ':', saw '"sv, to_sv(*cp), "'"sv);
14968 advance_and_return_if_error_or_eof({});
14969
14970 // "SS"
14971 if (!consume_digit_sequence(digits, 2u))
14972 set_error_and_return_default("expected 2-digit second, saw '"sv, to_sv(cp), "'"sv);
14973 const auto second = digits[1] + digits[0] * 10u;
14974 if (second > 59u)
14975 set_error_and_return_default("expected second between 0 and 59 (inclusive), saw "sv, second);
14976 time.second = static_cast<decltype(time.second)>(second);
14977
14978 // '.' (early-exiting is allowed; fractional is optional)
14979 if (is_eof() || is_value_terminator(*cp) || (part_of_datetime && is_match(*cp, U'+', U'-', U'Z', U'z')))
14980 return time;
14981 if (*cp != U'.')
14982 set_error_and_return_default("expected '.', saw '"sv, to_sv(*cp), "'"sv);
14983 advance_and_return_if_error_or_eof({});
14984
14985 // "FFFFFFFFF"
14986 size_t digit_count = consume_variable_length_digit_sequence(digits, max_digits);
14987 if (!digit_count)
14988 {
14989 set_error_and_return_if_eof({});
14990 set_error_and_return_default("expected fractional digits, saw '"sv, to_sv(*cp), "'"sv);
14991 }
14992 else if (!is_eof())
14993 {
14994 if (digit_count == max_digits && is_decimal_digit(*cp))
14995 set_error_and_return_default("fractional component exceeds maximum precision of "sv, max_digits);
14996 else if (!part_of_datetime && !is_value_terminator(*cp))
14997 set_error_and_return_default("expected value-terminator, saw '"sv, to_sv(*cp), "'"sv);
14998 }
14999 uint32_t value = 0u;
15000 uint32_t place = 1u;
15001 for (auto i = impl::min<size_t>(digit_count, 9u); i-- > 0u;)
15002 {
15003 value += digits[i] * place;
15004 place *= 10u;
15005 }
15006 for (auto i = digit_count; i < 9u; i++) // implicit zeros
15007 value *= 10u;
15008 time.nanosecond = value;
15009 return time;
15010 }
15011
15012 TOML_NODISCARD
15013 TOML_NEVER_INLINE
15014 date_time parse_date_time()
15015 {
15016 return_if_error({});
15017 assert_not_eof();
15018 TOML_ASSERT_ASSUME(is_decimal_digit(*cp));
15019 push_parse_scope("date-time"sv);
15020
15021 // "YYYY-MM-DD"
15022 auto date = parse_date(true);
15023 set_error_and_return_if_eof({});
15024
15025 // ' ', 'T' or 't'
15026 if (!is_match(*cp, U' ', U'T', U't'))
15027 set_error_and_return_default("expected space, 'T' or 't', saw '"sv, to_sv(*cp), "'"sv);
15028 advance_and_return_if_error_or_eof({});
15029
15030 // "HH:MM:SS.FFFFFFFFF"
15031 auto time = parse_time(true);
15032 return_if_error({});
15033
15034 // no offset
15035 if (is_eof() || is_value_terminator(*cp))
15036 return { date, time };
15037
15038 // zero offset ('Z' or 'z')
15039 time_offset offset{};
15040 if (is_match(*cp, U'Z', U'z'))
15041 advance_and_return_if_error({});
15042
15043 // explicit offset ("+/-HH:MM")
15044 else if (is_match(*cp, U'+', U'-'))
15045 {
15046 push_parse_scope("date-time offset"sv);
15047
15048 // sign
15049 int sign = *cp == U'-' ? -1 : 1;
15050 advance_and_return_if_error_or_eof({});
15051
15052 // "HH"
15053 int digits[2];
15054 if (!consume_digit_sequence(digits, 2u))
15055 set_error_and_return_default("expected 2-digit hour, saw '"sv, to_sv(cp), "'"sv);
15056 const auto hour = digits[1] + digits[0] * 10;
15057 if (hour > 23)
15058 set_error_and_return_default("expected hour between 0 and 23 (inclusive), saw "sv, hour);
15059 set_error_and_return_if_eof({});
15060
15061 // ':'
15062 if (*cp != U':')
15063 set_error_and_return_default("expected ':', saw '"sv, to_sv(*cp), "'"sv);
15064 advance_and_return_if_error_or_eof({});
15065
15066 // "MM"
15067 if (!consume_digit_sequence(digits, 2u))
15068 set_error_and_return_default("expected 2-digit minute, saw '"sv, to_sv(cp), "'"sv);
15069 const auto minute = digits[1] + digits[0] * 10;
15070 if (minute > 59)
15071 set_error_and_return_default("expected minute between 0 and 59 (inclusive), saw "sv, minute);
15072 offset.minutes = static_cast<decltype(offset.minutes)>((hour * 60 + minute) * sign);
15073 }
15074
15075 if (!is_eof() && !is_value_terminator(*cp))
15076 set_error_and_return_default("expected value-terminator, saw '"sv, to_sv(*cp), "'"sv);
15077
15078 return { date, time, offset };
15079 }
15080
15081 TOML_NODISCARD
15082 node_ptr parse_array();
15083
15084 TOML_NODISCARD
15085 node_ptr parse_inline_table();
15086
15087 TOML_NODISCARD
15088 node_ptr parse_value_known_prefixes()
15089 {
15090 return_if_error({});
15091 assert_not_eof();
15092 TOML_ASSERT_ASSUME(!is_control_character(*cp));
15093 TOML_ASSERT_ASSUME(*cp != U'_');
15094
15095 switch (cp->value)
15096 {
15097 // arrays
15098 case U'[': return parse_array();
15099
15100 // inline tables
15101 case U'{': return parse_inline_table();
15102
15103 // floats beginning with '.'
15104 case U'.': return node_ptr{ new value{ parse_float() } };
15105
15106 // strings
15107 case U'"': [[fallthrough]];
15108 case U'\'': return node_ptr{ new value{ parse_string().value } };
15109
15110 default:
15111 {
15112 const auto cp_upper = static_cast<uint_least32_t>(cp->value) & ~0x20u;
15113
15114 // bools
15115 if (cp_upper == 70u || cp_upper == 84u) // F or T
15116 return node_ptr{ new value{ parse_boolean() } };
15117
15118 // inf/nan
15119 else if (cp_upper == 73u || cp_upper == 78u) // I or N
15120 return node_ptr{ new value{ parse_inf_or_nan() } };
15121
15122 else
15123 return nullptr;
15124 }
15125 }
15126 TOML_UNREACHABLE;
15127 }
15128
15129 TOML_NODISCARD
15130 node_ptr parse_value()
15131 {
15132 return_if_error({});
15133 assert_not_eof();
15134 TOML_ASSERT_ASSUME(!is_value_terminator(*cp));
15135 push_parse_scope("value"sv);
15136
15137 const depth_counter_scope depth_counter{ nested_values };
15138 if TOML_UNLIKELY(nested_values > max_nested_values)
15139 set_error_and_return_default("exceeded maximum nested value depth of "sv,
15140 max_nested_values,
15141 " (TOML_MAX_NESTED_VALUES)"sv);
15142
15143 // check if it begins with some control character
15144 // (note that this will also fail for whitespace but we're assuming we've
15145 // called consume_leading_whitespace() before calling parse_value())
15146 if TOML_UNLIKELY(is_control_character(*cp))
15147 set_error_and_return_default("unexpected control character"sv);
15148
15149 // underscores at the beginning
15150 else if (*cp == U'_')
15151 set_error_and_return_default("values may not begin with underscores"sv);
15152
15153 const auto begin_pos = cp->position;
15154 node_ptr val;
15155
15156 do
15157 {
15158 TOML_ASSERT_ASSUME(!is_control_character(*cp));
15159 TOML_ASSERT_ASSUME(*cp != U'_');
15160
15161 // detect the value type and parse accordingly,
15162 // starting with value types that can be detected
15163 // unambiguously from just one character.
15164
15165 val = parse_value_known_prefixes();
15166 return_if_error({});
15167 if (val)
15168 break;
15169
15170 // value types from here down require more than one character to unambiguously identify
15171 // so scan ahead and collect a set of value 'traits'.
15172 enum TOML_CLOSED_FLAGS_ENUM value_traits : int
15173 {
15174 has_nothing = 0,
15175 has_digits = 1,
15176 has_b = 1 << 1, // as second char only (0b)
15177 has_e = 1 << 2, // only float exponents
15178 has_o = 1 << 3, // as second char only (0o)
15179 has_p = 1 << 4, // only hexfloat exponents
15180 has_t = 1 << 5,
15181 has_x = 1 << 6, // as second or third char only (0x, -0x, +0x)
15182 has_z = 1 << 7,
15183 has_colon = 1 << 8,
15184 has_plus = 1 << 9,
15185 has_minus = 1 << 10,
15186 has_dot = 1 << 11,
15187 begins_sign = 1 << 12,
15188 begins_digit = 1 << 13,
15189 begins_zero = 1 << 14,
15190 signs_msk = has_plus | has_minus,
15191 bdigit_msk = has_digits | begins_digit,
15192 bzero_msk = bdigit_msk | begins_zero,
15193 };
15194 value_traits traits = has_nothing;
15195 const auto has_any = [&](auto t) noexcept { return (traits & t) != has_nothing; };
15196 const auto has_none = [&](auto t) noexcept { return (traits & t) == has_nothing; };
15197 const auto add_trait = [&](auto t) noexcept { traits = static_cast<value_traits>(traits | t); };
15198
15199 // examine the first character to get the 'begins with' traits
15200 // (good fail-fast opportunity; all the remaining types begin with numeric digits or signs)
15201 if (is_decimal_digit(*cp))
15202 {
15203 add_trait(begins_digit);
15204 if (*cp == U'0')
15205 add_trait(begins_zero);
15206 }
15207 else if (is_match(*cp, U'+', U'-'))
15208 add_trait(begins_sign);
15209 else
15210 break;
15211
15212 // scan the rest of the value to determine the remaining traits
15213 char32_t chars[utf8_buffered_reader::max_history_length];
15214 size_t char_count = {}, advance_count = {};
15215 bool eof_while_scanning = false;
15216 const auto scan = [&]() noexcept(!TOML_COMPILER_HAS_EXCEPTIONS)
15217 {
15218 if (is_eof())
15219 return;
15220 TOML_ASSERT_ASSUME(!is_value_terminator(*cp));
15221
15222 do
15223 {
15224 if (const auto c = **cp; c != U'_')
15225 {
15226 chars[char_count++] = c;
15227
15228 if (is_decimal_digit(c))
15229 add_trait(has_digits);
15230 else if (is_ascii_letter(c))
15231 {
15232 TOML_ASSERT_ASSUME((c >= U'a' && c <= U'z') || (c >= U'A' && c <= U'Z'));
15233 switch (static_cast<char32_t>(c | 32u))
15234 {
15235 case U'b':
15236 if (char_count == 2u && has_any(begins_zero))
15237 add_trait(has_b);
15238 break;
15239
15240 case U'e':
15241 if (char_count > 1u
15242 && has_none(has_b | has_o | has_p | has_t | has_x | has_z | has_colon)
15243 && (has_none(has_plus | has_minus) || has_any(begins_sign)))
15244 add_trait(has_e);
15245 break;
15246
15247 case U'o':
15248 if (char_count == 2u && has_any(begins_zero))
15249 add_trait(has_o);
15250 break;
15251
15252 case U'p':
15253 if (has_any(has_x))
15254 add_trait(has_p);
15255 break;
15256
15257 case U'x':
15258 if ((char_count == 2u && has_any(begins_zero))
15259 || (char_count == 3u && has_any(begins_sign) && chars[1] == U'0'))
15260 add_trait(has_x);
15261 break;
15262
15263 case U't': add_trait(has_t); break;
15264 case U'z': add_trait(has_z); break;
15265 }
15266 }
15267 else if (c <= U':')
15268 {
15269 TOML_ASSERT_ASSUME(c < U'0' || c > U'9');
15270 switch (c)
15271 {
15272 case U'+': add_trait(has_plus); break;
15273 case U'-': add_trait(has_minus); break;
15274 case U'.': add_trait(has_dot); break;
15275 case U':': add_trait(has_colon); break;
15276 }
15277 }
15278 }
15279
15280 advance_and_return_if_error();
15281 advance_count++;
15282 eof_while_scanning = is_eof();
15283 }
15284 while (advance_count < (utf8_buffered_reader::max_history_length - 1u) && !is_eof()
15285 && !is_value_terminator(*cp));
15286 };
15287 scan();
15288 return_if_error({});
15289
15290 // force further scanning if this could have been a date-time with a space instead of a T
15291 if (char_count == 10u //
15292 && (traits | begins_zero) == (bzero_msk | has_minus) //
15293 && chars[4] == U'-' //
15294 && chars[7] == U'-' //
15295 && !is_eof() //
15296 && *cp == U' ')
15297 {
15298 const auto pre_advance_count = advance_count;
15299 const auto pre_scan_traits = traits;
15300 chars[char_count++] = *cp;
15301 add_trait(has_t);
15302
15303 const auto backpedal = [&]() noexcept
15304 {
15305 go_back(advance_count - pre_advance_count);
15306 advance_count = pre_advance_count;
15307 traits = pre_scan_traits;
15308 char_count = 10u;
15309 };
15310
15311 advance_and_return_if_error({});
15312 advance_count++;
15313
15314 if (is_eof() || !is_decimal_digit(*cp))
15315 backpedal();
15316 else
15317 {
15318 chars[char_count++] = *cp;
15319
15320 advance_and_return_if_error({});
15321 advance_count++;
15322
15323 scan();
15324 return_if_error({});
15325
15326 if (char_count == 12u)
15327 backpedal();
15328 }
15329 }
15330
15331 // set the reader back to where we started
15332 go_back(advance_count);
15333
15334 // if after scanning ahead we still only have one value character,
15335 // the only valid value type is an integer.
15336 if (char_count == 1u)
15337 {
15338 if (has_any(begins_digit))
15339 {
15340 val.reset(new value{ static_cast<int64_t>(chars[0] - U'0') });
15341 advance(); // skip the digit
15342 break;
15343 }
15344
15345 // anything else would be ambiguous.
15346 else
15347 set_error_and_return_default(eof_while_scanning ? "encountered end-of-file"sv
15348 : "could not determine value type"sv);
15349 }
15350
15351 // now things that can be identified from two or more characters
15352 return_if_error({});
15353 TOML_ASSERT_ASSUME(char_count >= 2u);
15354
15355 // do some 'fuzzy matching' where there's no ambiguity, since that allows the specific
15356 // typed parse functions to take over and show better diagnostics if there's an issue
15357 // (as opposed to the fallback "could not determine type" message)
15358 if (has_any(has_p))
15359 val.reset(new value{ parse_hex_float() });
15360 else if (has_any(has_x | has_o | has_b))
15361 {
15362 int64_t i;
15363 value_flags flags;
15364 if (has_any(has_x))
15365 {
15366 i = parse_integer<16>();
15367 flags = value_flags::format_as_hexadecimal;
15368 }
15369 else if (has_any(has_o))
15370 {
15371 i = parse_integer<8>();
15372 flags = value_flags::format_as_octal;
15373 }
15374 else // has_b
15375 {
15376 i = parse_integer<2>();
15377 flags = value_flags::format_as_binary;
15378 }
15379 return_if_error({});
15380
15381 val.reset(new value{ i });
15382 val->ref_cast<int64_t>().flags(flags);
15383 }
15384 else if (has_any(has_e) || (has_any(begins_digit) && chars[1] == U'.'))
15385 val.reset(new value{ parse_float() });
15386 else if (has_any(begins_sign))
15387 {
15388 // single-digit signed integers
15389 if (char_count == 2u && has_any(has_digits))
15390 {
15391 val.reset(new value{ static_cast<int64_t>(chars[1] - U'0') * (chars[0] == U'-' ? -1LL : 1LL) });
15392 advance(); // skip the sign
15393 advance(); // skip the digit
15394 break;
15395 }
15396
15397 // simple signed floats (e.g. +1.0)
15398 if (is_decimal_digit(chars[1]) && chars[2] == U'.')
15399 val.reset(new value{ parse_float() });
15400
15401 // signed infinity or nan
15402 else if (is_match(chars[1], U'i', U'n', U'I', U'N'))
15403 val.reset(new value{ parse_inf_or_nan() });
15404 }
15405
15406 return_if_error({});
15407 if (val)
15408 break;
15409
15410 // match trait masks against what they can match exclusively.
15411 // all correct value parses will come out of this list, so doing this as a switch is likely to
15412 // be a better friend to the optimizer on the success path (failure path can be slow but that
15413 // doesn't matter much).
15414 switch (unwrap_enum(traits))
15415 {
15416 // binary integers
15417 // 0b10
15418 case bzero_msk | has_b:
15419 val.reset(new value{ parse_integer<2>() });
15420 val->ref_cast<int64_t>().flags(value_flags::format_as_binary);
15421 break;
15422
15423 // octal integers
15424 // 0o10
15425 case bzero_msk | has_o:
15426 val.reset(new value{ parse_integer<8>() });
15427 val->ref_cast<int64_t>().flags(value_flags::format_as_octal);
15428 break;
15429
15430 // decimal integers
15431 // 00
15432 // 10
15433 // +10
15434 // -10
15435 case bzero_msk: [[fallthrough]];
15436 case bdigit_msk: [[fallthrough]];
15437 case begins_sign | has_digits | has_minus: [[fallthrough]];
15438 case begins_sign | has_digits | has_plus:
15439 {
15440 // if the value was so long we exhausted the history buffer it's reasonable to assume
15441 // there was more and the value's actual type is impossible to identify without making the
15442 // buffer bigger (since it could have actually been a float), so emit an error.
15443 //
15444 // (this will likely only come up during fuzzing and similar scenarios)
15445 static constexpr size_t max_numeric_value_length =
15446 utf8_buffered_reader::max_history_length - 2u;
15447 if TOML_UNLIKELY(!eof_while_scanning && advance_count > max_numeric_value_length)
15448 set_error_and_return_default("numeric value too long to identify type - cannot exceed "sv,
15449 max_numeric_value_length,
15450 " characters"sv);
15451
15452 val.reset(new value{ parse_integer<10>() });
15453 break;
15454 }
15455
15456 // hexadecimal integers
15457 // 0x10
15458 case bzero_msk | has_x:
15459 val.reset(new value{ parse_integer<16>() });
15460 val->ref_cast<int64_t>().flags(value_flags::format_as_hexadecimal);
15461 break;
15462
15463 // decimal floats
15464 // 0e1
15465 // 0e-1
15466 // 0e+1
15467 // 0.0
15468 // 0.0e1
15469 // 0.0e-1
15470 // 0.0e+1
15471 case bzero_msk | has_e: [[fallthrough]];
15472 case bzero_msk | has_e | has_minus: [[fallthrough]];
15473 case bzero_msk | has_e | has_plus: [[fallthrough]];
15474 case bzero_msk | has_dot: [[fallthrough]];
15475 case bzero_msk | has_dot | has_e: [[fallthrough]];
15476 case bzero_msk | has_dot | has_e | has_minus: [[fallthrough]];
15477 case bzero_msk | has_dot | has_e | has_plus: [[fallthrough]];
15478 // 1e1
15479 // 1e-1
15480 // 1e+1
15481 // 1.0
15482 // 1.0e1
15483 // 1.0e-1
15484 // 1.0e+1
15485 case bdigit_msk | has_e: [[fallthrough]];
15486 case bdigit_msk | has_e | has_minus: [[fallthrough]];
15487 case bdigit_msk | has_e | has_plus: [[fallthrough]];
15488 case bdigit_msk | has_dot: [[fallthrough]];
15489 case bdigit_msk | has_dot | has_e: [[fallthrough]];
15490 case bdigit_msk | has_dot | has_e | has_minus: [[fallthrough]];
15491 case bdigit_msk | has_dot | has_e | has_plus: [[fallthrough]];
15492 // +1e1
15493 // +1.0
15494 // +1.0e1
15495 // +1.0e+1
15496 // +1.0e-1
15497 // -1.0e+1
15498 case begins_sign | has_digits | has_e | has_plus: [[fallthrough]];
15499 case begins_sign | has_digits | has_dot | has_plus: [[fallthrough]];
15500 case begins_sign | has_digits | has_dot | has_e | has_plus: [[fallthrough]];
15501 case begins_sign | has_digits | has_dot | has_e | signs_msk: [[fallthrough]];
15502 // -1e1
15503 // -1e+1
15504 // +1e-1
15505 // -1.0
15506 // -1.0e1
15507 // -1.0e-1
15508 case begins_sign | has_digits | has_e | has_minus: [[fallthrough]];
15509 case begins_sign | has_digits | has_e | signs_msk: [[fallthrough]];
15510 case begins_sign | has_digits | has_dot | has_minus: [[fallthrough]];
15511 case begins_sign | has_digits | has_dot | has_e | has_minus:
15512 val.reset(new value{ parse_float() });
15513 break;
15514
15515 // hexadecimal floats
15516 // 0x10p0
15517 // 0x10p-0
15518 // 0x10p+0
15519 case bzero_msk | has_x | has_p: [[fallthrough]];
15520 case bzero_msk | has_x | has_p | has_minus: [[fallthrough]];
15521 case bzero_msk | has_x | has_p | has_plus: [[fallthrough]];
15522 // -0x10p0
15523 // -0x10p-0
15524 // +0x10p0
15525 // +0x10p+0
15526 // -0x10p+0
15527 // +0x10p-0
15528 case begins_sign | has_digits | has_x | has_p | has_minus: [[fallthrough]];
15529 case begins_sign | has_digits | has_x | has_p | has_plus: [[fallthrough]];
15530 case begins_sign | has_digits | has_x | has_p | signs_msk: [[fallthrough]];
15531 // 0x10.1p0
15532 // 0x10.1p-0
15533 // 0x10.1p+0
15534 case bzero_msk | has_x | has_dot | has_p: [[fallthrough]];
15535 case bzero_msk | has_x | has_dot | has_p | has_minus: [[fallthrough]];
15536 case bzero_msk | has_x | has_dot | has_p | has_plus: [[fallthrough]];
15537 // -0x10.1p0
15538 // -0x10.1p-0
15539 // +0x10.1p0
15540 // +0x10.1p+0
15541 // -0x10.1p+0
15542 // +0x10.1p-0
15543 case begins_sign | has_digits | has_x | has_dot | has_p | has_minus: [[fallthrough]];
15544 case begins_sign | has_digits | has_x | has_dot | has_p | has_plus: [[fallthrough]];
15545 case begins_sign | has_digits | has_x | has_dot | has_p | signs_msk:
15546 val.reset(new value{ parse_hex_float() });
15547 break;
15548
15549 // times
15550 // HH:MM
15551 // HH:MM:SS
15552 // HH:MM:SS.FFFFFF
15553 case bzero_msk | has_colon: [[fallthrough]];
15554 case bzero_msk | has_colon | has_dot: [[fallthrough]];
15555 case bdigit_msk | has_colon: [[fallthrough]];
15556 case bdigit_msk | has_colon | has_dot: val.reset(new value{ parse_time() }); break;
15557
15558 // local dates
15559 // YYYY-MM-DD
15560 case bzero_msk | has_minus: [[fallthrough]];
15561 case bdigit_msk | has_minus: val.reset(new value{ parse_date() }); break;
15562
15563 // date-times
15564 // YYYY-MM-DDTHH:MM
15565 // YYYY-MM-DDTHH:MM-HH:MM
15566 // YYYY-MM-DDTHH:MM+HH:MM
15567 // YYYY-MM-DD HH:MM
15568 // YYYY-MM-DD HH:MM-HH:MM
15569 // YYYY-MM-DD HH:MM+HH:MM
15570 // YYYY-MM-DDTHH:MM:SS
15571 // YYYY-MM-DDTHH:MM:SS-HH:MM
15572 // YYYY-MM-DDTHH:MM:SS+HH:MM
15573 // YYYY-MM-DD HH:MM:SS
15574 // YYYY-MM-DD HH:MM:SS-HH:MM
15575 // YYYY-MM-DD HH:MM:SS+HH:MM
15576 case bzero_msk | has_minus | has_colon | has_t: [[fallthrough]];
15577 case bzero_msk | signs_msk | has_colon | has_t: [[fallthrough]];
15578 case bdigit_msk | has_minus | has_colon | has_t: [[fallthrough]];
15579 case bdigit_msk | signs_msk | has_colon | has_t: [[fallthrough]];
15580 // YYYY-MM-DDTHH:MM:SS.FFFFFF
15581 // YYYY-MM-DDTHH:MM:SS.FFFFFF-HH:MM
15582 // YYYY-MM-DDTHH:MM:SS.FFFFFF+HH:MM
15583 // YYYY-MM-DD HH:MM:SS.FFFFFF
15584 // YYYY-MM-DD HH:MM:SS.FFFFFF-HH:MM
15585 // YYYY-MM-DD HH:MM:SS.FFFFFF+HH:MM
15586 case bzero_msk | has_minus | has_colon | has_dot | has_t: [[fallthrough]];
15587 case bzero_msk | signs_msk | has_colon | has_dot | has_t: [[fallthrough]];
15588 case bdigit_msk | has_minus | has_colon | has_dot | has_t: [[fallthrough]];
15589 case bdigit_msk | signs_msk | has_colon | has_dot | has_t: [[fallthrough]];
15590 // YYYY-MM-DDTHH:MMZ
15591 // YYYY-MM-DD HH:MMZ
15592 // YYYY-MM-DDTHH:MM:SSZ
15593 // YYYY-MM-DD HH:MM:SSZ
15594 // YYYY-MM-DDTHH:MM:SS.FFFFFFZ
15595 // YYYY-MM-DD HH:MM:SS.FFFFFFZ
15596 case bzero_msk | has_minus | has_colon | has_z | has_t: [[fallthrough]];
15597 case bzero_msk | has_minus | has_colon | has_dot | has_z | has_t: [[fallthrough]];
15598 case bdigit_msk | has_minus | has_colon | has_z | has_t: [[fallthrough]];
15599 case bdigit_msk | has_minus | has_colon | has_dot | has_z | has_t:
15600 val.reset(new value{ parse_date_time() });
15601 break;
15602 }
15603 }
15604 while (false);
15605
15606 if (!val)
15607 {
15608 set_error_at(begin_pos, "could not determine value type"sv);
15609 return_after_error({});
15610 }
15611
15612 val->source_ = { begin_pos, current_position(1), reader.source_path() };
15613 return val;
15614 }
15615
15616 TOML_NEVER_INLINE
15617 bool parse_key()
15618 {
15619 return_if_error({});
15620 assert_not_eof();
15621 TOML_ASSERT_ASSUME(is_bare_key_character(*cp) || is_string_delimiter(*cp));
15622 push_parse_scope("key"sv);
15623
15624 key_buffer.clear();
15625 recording_whitespace = false;
15626
15627 while (!is_error())
15628 {
15629 std::string_view key_segment;
15630 const auto key_begin = current_position();
15631
15632 // bare_key_segment
15633 if (is_bare_key_character(*cp))
15634 key_segment = parse_bare_key_segment();
15635
15636 // "quoted key segment"
15637 else if (is_string_delimiter(*cp))
15638 {
15639 const auto begin_pos = cp->position;
15640
15641 recording_whitespace = true;
15642 parsed_string str = parse_string();
15643 recording_whitespace = false;
15644 return_if_error({});
15645
15646 if (str.was_multi_line)
15647 {
15648 set_error_at(begin_pos,
15649 "multi-line strings are prohibited in "sv,
15650 key_buffer.empty() ? ""sv : "dotted "sv,
15651 "keys"sv);
15652 return_after_error({});
15653 }
15654 else
15655 key_segment = str.value;
15656 }
15657
15658 // ???
15659 else
15660 set_error_and_return_default("expected bare key starting character or string delimiter, saw '"sv,
15661 to_sv(*cp),
15662 "'"sv);
15663
15664 const auto key_end = current_position();
15665
15666 // whitespace following the key segment
15667 consume_leading_whitespace();
15668
15669 // store segment
15670 key_buffer.push_back(key_segment, key_begin, key_end);
15671
15672 if TOML_UNLIKELY(key_buffer.size() > max_dotted_keys_depth)
15673 set_error_and_return_default("exceeded maximum dotted keys depth of "sv,
15674 max_dotted_keys_depth,
15675 " (TOML_MAX_DOTTED_KEYS_DEPTH)"sv);
15676
15677 // eof or no more key to come
15678 if (is_eof() || *cp != U'.')
15679 break;
15680
15681 // was a dotted key - go around again
15682 advance_and_return_if_error_or_eof({});
15683 consume_leading_whitespace();
15684 set_error_and_return_if_eof({});
15685 }
15686 return_if_error({});
15687
15688 return true;
15689 }
15690
15691 TOML_NODISCARD
15692 key make_key(size_t segment_index) const
15693 {
15694 TOML_ASSERT(key_buffer.size() > segment_index);
15695
15696 return key{
15697 key_buffer[segment_index],
15698 source_region{ key_buffer.starts[segment_index], key_buffer.ends[segment_index], root.source().path }
15699 };
15700 }
15701
15702 TOML_NODISCARD
15703 TOML_NEVER_INLINE
15704 table* parse_table_header()
15705 {
15706 return_if_error({});
15707 assert_not_eof();
15708 TOML_ASSERT_ASSUME(*cp == U'[');
15709 push_parse_scope("table header"sv);
15710
15711 const source_position header_begin_pos = cp->position;
15712 source_position header_end_pos;
15713 bool is_arr = false;
15714
15715 // parse header
15716 {
15717 // skip first '['
15718 advance_and_return_if_error_or_eof({});
15719
15720 // skip past any whitespace that followed the '['
15721 const bool had_leading_whitespace = consume_leading_whitespace();
15722 set_error_and_return_if_eof({});
15723
15724 // skip second '[' (if present)
15725 if (*cp == U'[')
15726 {
15727 if (had_leading_whitespace)
15728 set_error_and_return_default(
15729 "[[array-of-table]] brackets must be contiguous (i.e. [ [ this ] ] is prohibited)"sv);
15730
15731 is_arr = true;
15732 advance_and_return_if_error_or_eof({});
15733
15734 // skip past any whitespace that followed the '['
15735 consume_leading_whitespace();
15736 set_error_and_return_if_eof({});
15737 }
15738
15739 // check for a premature closing ']'
15740 if (*cp == U']')
15741 set_error_and_return_default("tables with blank bare keys are explicitly prohibited"sv);
15742
15743 // get the actual key
15744 start_recording();
15745 parse_key();
15746 stop_recording(1u);
15747 return_if_error({});
15748
15749 // skip past any whitespace that followed the key
15750 consume_leading_whitespace();
15751 return_if_error({});
15752 set_error_and_return_if_eof({});
15753
15754 // consume the closing ']'
15755 if (*cp != U']')
15756 set_error_and_return_default("expected ']', saw '"sv, to_sv(*cp), "'"sv);
15757 if (is_arr)
15758 {
15759 advance_and_return_if_error_or_eof({});
15760 if (*cp != U']')
15761 set_error_and_return_default("expected ']', saw '"sv, to_sv(*cp), "'"sv);
15762 }
15763 advance_and_return_if_error({});
15764 header_end_pos = current_position(1);
15765
15766 // handle the rest of the line after the header
15767 consume_leading_whitespace();
15768 if (!is_eof() && !consume_comment() && !consume_line_break())
15769 set_error_and_return_default("expected a comment or whitespace, saw '"sv, to_sv(cp), "'"sv);
15770 }
15771 TOML_ASSERT(!key_buffer.empty());
15772
15773 // check if each parent is a table/table array, or can be created implicitly as a table.
15774 table* parent = &root;
15775 for (size_t i = 0, e = key_buffer.size() - 1u; i < e; i++)
15776 {
15777 const std::string_view segment = key_buffer[i];
15778 auto pit = parent->lower_bound(segment);
15779
15780 // parent already existed
15781 if (pit != parent->end() && pit->first == segment)
15782 {
15783 node& p = pit->second;
15784
15785 if (auto tbl = p.as_table())
15786 {
15787 // adding to closed inline tables is illegal
15788 if (tbl->is_inline() && !impl::find(open_inline_tables.begin(), open_inline_tables.end(), tbl))
15789 set_error_and_return_default("cannot insert '"sv,
15790 to_sv(recording_buffer),
15791 "' into existing inline table"sv);
15792
15793 parent = tbl;
15794 }
15795 else if (auto arr = p.as_array(); arr && impl::find(table_arrays.begin(), table_arrays.end(), arr))
15796 {
15797 // table arrays are a special case;
15798 // the spec dictates we select the most recently declared element in the array.
15799 TOML_ASSERT(!arr->empty());
15800 TOML_ASSERT(arr->back().is_table());
15801 parent = &arr->back().ref_cast<table>();
15802 }
15803 else
15804 {
15805 if (!is_arr && p.type() == node_type::table)
15806 set_error_and_return_default("cannot redefine existing table '"sv,
15807 to_sv(recording_buffer),
15808 "'"sv);
15809 else
15810 set_error_and_return_default("cannot redefine existing "sv,
15811 to_sv(p.type()),
15812 " '"sv,
15813 to_sv(recording_buffer),
15814 "' as "sv,
15815 is_arr ? "array-of-tables"sv : "table"sv);
15816 }
15817 }
15818
15819 // need to create a new implicit table
15820 else
15821 {
15822 pit = parent->emplace_hint<table>(pit, make_key(i));
15823 table& p = pit->second.ref_cast<table>();
15824 p.source_ = { header_begin_pos, header_end_pos, reader.source_path() };
15825
15826 implicit_tables.push_back(&p);
15827 parent = &p;
15828 }
15829 }
15830
15831 const auto last_segment = key_buffer.back();
15832 auto it = parent->lower_bound(last_segment);
15833
15834 // if there was already a matching node some sanity checking is necessary;
15835 // this is ok if we're making an array and the existing element is already an array (new element)
15836 // or if we're making a table and the existing element is an implicitly-created table (promote it),
15837 // otherwise this is a redefinition error.
15838 if (it != parent->end() && it->first == last_segment)
15839 {
15840 node& matching_node = it->second;
15841 if (auto arr = matching_node.as_array();
15842 is_arr && arr && impl::find(table_arrays.begin(), table_arrays.end(), arr))
15843 {
15844 table& tbl = arr->emplace_back<table>();
15845 tbl.source_ = { header_begin_pos, header_end_pos, reader.source_path() };
15846 return &tbl;
15847 }
15848
15849 else if (auto tbl = matching_node.as_table(); !is_arr && tbl && !implicit_tables.empty())
15850 {
15851 if (auto found = impl::find(implicit_tables.begin(), implicit_tables.end(), tbl); found)
15852 {
15853 bool ok = true;
15854 if (!tbl->empty())
15855 {
15856 for (auto& [_, child] : *tbl)
15857 {
15858 if (!child.is_table() && !child.is_array_of_tables())
15859 {
15860 ok = false;
15861 break;
15862 }
15863 }
15864 }
15865
15866 if (ok)
15867 {
15868 implicit_tables.erase(implicit_tables.cbegin() + (found - implicit_tables.data()));
15869 tbl->source_.begin = header_begin_pos;
15870 tbl->source_.end = header_end_pos;
15871 return tbl;
15872 }
15873 }
15874 }
15875
15876 // if we get here it's a redefinition error.
15877 if (!is_arr && matching_node.type() == node_type::table)
15878 {
15879 set_error_at(header_begin_pos,
15880 "cannot redefine existing table '"sv,
15881 to_sv(recording_buffer),
15882 "'"sv);
15883 return_after_error({});
15884 }
15885 else
15886 {
15887 set_error_at(header_begin_pos,
15888 "cannot redefine existing "sv,
15889 to_sv(matching_node.type()),
15890 " '"sv,
15891 to_sv(recording_buffer),
15892 "' as "sv,
15893 is_arr ? "array-of-tables"sv : "table"sv);
15894 return_after_error({});
15895 }
15896 }
15897
15898 // there was no matching node, sweet - we can freely instantiate a new table/table array.
15899 else
15900 {
15901 auto last_key = make_key(key_buffer.size() - 1u);
15902
15903 // if it's an array we need to make the array and it's first table element,
15904 // set the starting regions, and return the table element
15905 if (is_arr)
15906 {
15907 it = parent->emplace_hint<array>(it, std::move(last_key));
15908 array& tbl_arr = it->second.ref_cast<array>();
15909 table_arrays.push_back(&tbl_arr);
15910 tbl_arr.source_ = { header_begin_pos, header_end_pos, reader.source_path() };
15911
15912 table& tbl = tbl_arr.emplace_back<table>();
15913 tbl.source_ = { header_begin_pos, header_end_pos, reader.source_path() };
15914 return &tbl;
15915 }
15916
15917 // otherwise we're just making a table
15918 else
15919 {
15920 it = parent->emplace_hint<table>(it, std::move(last_key));
15921 table& tbl = it->second.ref_cast<table>();
15922 tbl.source_ = { header_begin_pos, header_end_pos, reader.source_path() };
15923 return &tbl;
15924 }
15925 }
15926 }
15927
15928 TOML_NEVER_INLINE
15929 bool parse_key_value_pair_and_insert(table* tbl)
15930 {
15931 return_if_error({});
15932 assert_not_eof();
15933 TOML_ASSERT_ASSUME(is_string_delimiter(*cp) || is_bare_key_character(*cp));
15934 push_parse_scope("key-value pair"sv);
15935
15936 // read the key into the key buffer
15937 start_recording();
15938 parse_key();
15939 stop_recording(1u);
15940 return_if_error({});
15941 TOML_ASSERT(key_buffer.size() >= 1u);
15942
15943 // skip past any whitespace that followed the key
15944 consume_leading_whitespace();
15945 set_error_and_return_if_eof({});
15946
15947 // '='
15948 if (*cp != U'=')
15949 set_error_and_return_default("expected '=', saw '"sv, to_sv(*cp), "'"sv);
15950 advance_and_return_if_error_or_eof({});
15951
15952 // skip past any whitespace that followed the '='
15953 consume_leading_whitespace();
15954 return_if_error({});
15955 set_error_and_return_if_eof({});
15956
15957 // check that the next character could actually be a value
15958 if (is_value_terminator(*cp))
15959 set_error_and_return_default("expected value, saw '"sv, to_sv(*cp), "'"sv);
15960
15961 // if it's a dotted kvp we need to spawn the parent sub-tables if necessary,
15962 // and set the target table to the second-to-last one in the chain
15963 if (key_buffer.size() > 1u)
15964 {
15965 for (size_t i = 0; i < key_buffer.size() - 1u; i++)
15966 {
15967 const std::string_view segment = key_buffer[i];
15968 auto pit = tbl->lower_bound(segment);
15969
15970 // parent already existed
15971 if (pit != tbl->end() && pit->first == segment)
15972 {
15973 table* p = pit->second.as_table();
15974
15975 // redefinition
15976 if TOML_UNLIKELY(!p
15977 || !(impl::find(dotted_key_tables.begin(), dotted_key_tables.end(), p)
15978 || impl::find(implicit_tables.begin(), implicit_tables.end(), p)))
15979 {
15980 set_error_at(key_buffer.starts[i],
15981 "cannot redefine existing "sv,
15982 to_sv(pit->second.type()),
15983 " as dotted key-value pair"sv);
15984 return_after_error({});
15985 }
15986
15987 tbl = p;
15988 }
15989
15990 // need to create a new implicit table
15991 else
15992 {
15993 pit = tbl->emplace_hint<table>(pit, make_key(i));
15994 table& p = pit->second.ref_cast<table>();
15995 p.source_ = pit->first.source();
15996
15997 dotted_key_tables.push_back(&p);
15998 tbl = &p;
15999 }
16000 }
16001 }
16002
16003 // ensure this isn't a redefinition
16004 const std::string_view last_segment = key_buffer.back();
16005 auto it = tbl->lower_bound(last_segment);
16006 if (it != tbl->end() && it->first == last_segment)
16007 {
16008 set_error("cannot redefine existing "sv,
16009 to_sv(it->second.type()),
16010 " '"sv,
16011 to_sv(recording_buffer),
16012 "'"sv);
16013 return_after_error({});
16014 }
16015
16016 // create the key first since the key buffer will likely get overwritten during value parsing (inline
16017 // tables)
16018 auto last_key = make_key(key_buffer.size() - 1u);
16019
16020 // now we can actually parse the value
16021 node_ptr val = parse_value();
16022 return_if_error({});
16023
16024 tbl->emplace_hint<node_ptr>(it, std::move(last_key), std::move(val));
16025 return true;
16026 }
16027
16028 void parse_document()
16029 {
16030 assert_not_error();
16031 assert_not_eof();
16032 push_parse_scope("root table"sv);
16033
16034 table* current_table = &root;
16035
16036 do
16037 {
16038 return_if_error();
16039
16040 // leading whitespace, line endings, comments
16041 if (consume_leading_whitespace() || consume_line_break() || consume_comment())
16042 continue;
16043 return_if_error();
16044
16045 // [tables]
16046 // [[table array]]
16047 if (*cp == U'[')
16048 current_table = parse_table_header();
16049
16050 // bare_keys
16051 // dotted.keys
16052 // "quoted keys"
16053 else if (is_bare_key_character(*cp) || is_string_delimiter(*cp))
16054 {
16055 push_parse_scope("key-value pair"sv);
16056
16057 parse_key_value_pair_and_insert(current_table);
16058
16059 // handle the rest of the line after the kvp
16060 // (this is not done in parse_key_value_pair() because that is also used for inline tables)
16061 consume_leading_whitespace();
16062 return_if_error();
16063 if (!is_eof() && !consume_comment() && !consume_line_break())
16064 set_error("expected a comment or whitespace, saw '"sv, to_sv(cp), "'"sv);
16065 }
16066
16067 else // ??
16068 set_error("expected keys, tables, whitespace or comments, saw '"sv, to_sv(cp), "'"sv);
16069 }
16070 while (!is_eof());
16071
16072 auto eof_pos = current_position(1);
16073 root.source_.end = eof_pos;
16074 if (current_table && current_table != &root && current_table->source_.end <= current_table->source_.begin)
16075 current_table->source_.end = eof_pos;
16076 }
16077
16078 static void update_region_ends(node& nde) noexcept
16079 {
16080 const auto type = nde.type();
16081 if (type > node_type::array)
16082 return;
16083
16084 if (type == node_type::table)
16085 {
16086 auto& tbl = nde.ref_cast<table>();
16087 if (tbl.is_inline()) // inline tables (and all their inline descendants) are already correctly
16088 // terminated
16089 return;
16090
16091 auto end = nde.source_.end;
16092 for (auto&& [k, v] : tbl)
16093 {
16094 TOML_UNUSED(k);
16095 update_region_ends(v);
16096 if (end < v.source_.end)
16097 end = v.source_.end;
16098 }
16099 }
16100 else // arrays
16101 {
16102 auto& arr = nde.ref_cast<array>();
16103 auto end = nde.source_.end;
16104 for (auto&& v : arr)
16105 {
16106 update_region_ends(v);
16107 if (end < v.source_.end)
16108 end = v.source_.end;
16109 }
16110 nde.source_.end = end;
16111 }
16112 }
16113
16114 public:
16115 parser(utf8_reader_interface&& reader_) //
16116 : reader{ reader_ }
16117 {
16118 root.source_ = { prev_pos, prev_pos, reader.source_path() };
16119
16120 if (!reader.peek_eof())
16121 {
16122 cp = reader.read_next();
16123
16124 #if !TOML_EXCEPTIONS
16125 if (reader.error())
16126 {
16127 err = std::move(reader.error());
16128 return;
16129 }
16130 #endif
16131
16132 if (cp)
16133 parse_document();
16134 }
16135
16136 update_region_ends(root);
16137 }
16138
16139 TOML_NODISCARD
16140 operator parse_result() && noexcept
16141 {
16142 #if TOML_EXCEPTIONS
16143
16144 return { std::move(root) };
16145
16146 #else
16147
16148 if (err)
16149 return parse_result{ *std::move(err) };
16150 else
16151 return parse_result{ std::move(root) };
16152
16153 #endif
16154 }
16155 };
16156
16157 TOML_EXTERNAL_LINKAGE
16158 node_ptr parser::parse_array()
16159 {
16160 return_if_error({});
16161 assert_not_eof();
16162 TOML_ASSERT_ASSUME(*cp == U'[');
16163 push_parse_scope("array"sv);
16164
16165 // skip opening '['
16166 advance_and_return_if_error_or_eof({});
16167
16168 node_ptr arr_ptr{ new array{} };
16169 array& arr = arr_ptr->ref_cast<array>();
16170 enum class TOML_CLOSED_ENUM parse_type : int
16171 {
16172 none,
16173 comma,
16174 val
16175 };
16176 parse_type prev = parse_type::none;
16177
16178 while (!is_error())
16179 {
16180 while (consume_leading_whitespace() || consume_line_break() || consume_comment())
16181 continue;
16182 set_error_and_return_if_eof({});
16183
16184 // commas - only legal after a value
16185 if (*cp == U',')
16186 {
16187 if (prev == parse_type::val)
16188 {
16189 prev = parse_type::comma;
16190 advance_and_return_if_error_or_eof({});
16191 continue;
16192 }
16193 set_error_and_return_default("expected value or closing ']', saw comma"sv);
16194 }
16195
16196 // closing ']'
16197 else if (*cp == U']')
16198 {
16199 advance_and_return_if_error({});
16200 break;
16201 }
16202
16203 // must be a value
16204 else
16205 {
16206 if (prev == parse_type::val)
16207 {
16208 set_error_and_return_default("expected comma or closing ']', saw '"sv, to_sv(*cp), "'"sv);
16209 continue;
16210 }
16211 prev = parse_type::val;
16212
16213 auto val = parse_value();
16214 return_if_error({});
16215
16216 if (!arr.capacity())
16217 arr.reserve(4u);
16218 arr.emplace_back<node_ptr>(std::move(val));
16219 }
16220 }
16221
16222 return_if_error({});
16223 return arr_ptr;
16224 }
16225
16226 TOML_EXTERNAL_LINKAGE
16227 node_ptr parser::parse_inline_table()
16228 {
16229 return_if_error({});
16230 assert_not_eof();
16231 TOML_ASSERT_ASSUME(*cp == U'{');
16232 push_parse_scope("inline table"sv);
16233
16234 // skip opening '{'
16235 advance_and_return_if_error_or_eof({});
16236
16237 node_ptr tbl_ptr{ new table{} };
16238 table& tbl = tbl_ptr->ref_cast<table>();
16239 tbl.is_inline(true);
16240 table_vector_scope table_scope{ open_inline_tables, tbl };
16241
16242 enum class TOML_CLOSED_ENUM parse_type : int
16243 {
16244 none,
16245 comma,
16246 kvp
16247 };
16248 parse_type prev = parse_type::none;
16249 while (!is_error())
16250 {
16251 if constexpr (TOML_LANG_UNRELEASED) // toml/issues/516 (newlines/trailing commas in inline tables)
16252 {
16253 while (consume_leading_whitespace() || consume_line_break() || consume_comment())
16254 continue;
16255 }
16256 else
16257 {
16258 while (consume_leading_whitespace())
16259 continue;
16260 }
16261 return_if_error({});
16262 set_error_and_return_if_eof({});
16263
16264 // commas - only legal after a key-value pair
16265 if (*cp == U',')
16266 {
16267 if (prev == parse_type::kvp)
16268 {
16269 prev = parse_type::comma;
16270 advance_and_return_if_error_or_eof({});
16271 }
16272 else
16273 set_error_and_return_default("expected key-value pair or closing '}', saw comma"sv);
16274 }
16275
16276 // closing '}'
16277 else if (*cp == U'}')
16278 {
16279 if constexpr (!TOML_LANG_UNRELEASED) // toml/issues/516 (newlines/trailing commas in inline tables)
16280 {
16281 if (prev == parse_type::comma)
16282 {
16283 set_error_and_return_default("expected key-value pair, saw closing '}' (dangling comma)"sv);
16284 continue;
16285 }
16286 }
16287 advance_and_return_if_error({});
16288 break;
16289 }
16290
16291 // key-value pair
16292 else if (is_string_delimiter(*cp) || is_bare_key_character(*cp))
16293 {
16294 if (prev == parse_type::kvp)
16295 set_error_and_return_default("expected comma or closing '}', saw '"sv, to_sv(*cp), "'"sv);
16296 else
16297 {
16298 prev = parse_type::kvp;
16299 parse_key_value_pair_and_insert(&tbl);
16300 }
16301 }
16302
16303 else
16304 set_error_and_return_default("expected key or closing '}', saw '"sv, to_sv(*cp), "'"sv);
16305 }
16306
16307 return_if_error({});
16308 return tbl_ptr;
16309 }
16310
16311 TOML_ABI_NAMESPACE_END; // TOML_EXCEPTIONS
16312 }
16313 TOML_IMPL_NAMESPACE_END;
16314
16315 #undef TOML_RETURNS_BY_THROWING
16316 #undef advance_and_return_if_error
16317 #undef advance_and_return_if_error_or_eof
16318 #undef assert_not_eof
16319 #undef assert_not_error
16320 #undef is_eof
16321 #undef is_error
16322 #undef parse_error_break
16323 #undef push_parse_scope
16324 #undef push_parse_scope_1
16325 #undef push_parse_scope_2
16326 #undef return_after_error
16327 #undef return_if_eof
16328 #undef return_if_error
16329 #undef return_if_error_or_eof
16330 #undef set_error_and_return
16331 #undef set_error_and_return_default
16332 #undef set_error_and_return_if_eof
16333 #undef utf8_buffered_reader_error_check
16334 #undef utf8_reader_error
16335 #undef utf8_reader_error_check
16336 #undef utf8_reader_return_after_error
16337
16338 TOML_ANON_NAMESPACE_START
16339 {
16340 TOML_NODISCARD
16341 TOML_INTERNAL_LINKAGE
16342 parse_result do_parse(utf8_reader_interface && reader)
16343 {
16344 return impl::parser{ std::move(reader) };
16345 }
16346
16347 TOML_NODISCARD
16348 TOML_INTERNAL_LINKAGE
16349 parse_result do_parse_file(std::string_view file_path)
16350 {
16351 #if TOML_EXCEPTIONS
16352 #define TOML_PARSE_FILE_ERROR(msg, path) \
16353 throw parse_error(msg, source_position{}, std::make_shared<const std::string>(std::move(path)))
16354 #else
16355 #define TOML_PARSE_FILE_ERROR(msg, path) \
16356 return parse_result(parse_error(msg, source_position{}, std::make_shared<const std::string>(std::move(path))))
16357 #endif
16358
16359 std::string file_path_str(file_path);
16360
16361 // open file with a custom-sized stack buffer
16362 std::ifstream file;
16363 TOML_OVERALIGNED char file_buffer[sizeof(void*) * 1024u];
16364 file.rdbuf()->pubsetbuf(file_buffer, sizeof(file_buffer));
16365 #if TOML_WINDOWS && !(defined(__MINGW32__) || defined(__MINGW64__))
16366 file.open(impl::widen(file_path_str).c_str(), std::ifstream::in | std::ifstream::binary | std::ifstream::ate);
16367 #else
16368 file.open(file_path_str, std::ifstream::in | std::ifstream::binary | std::ifstream::ate);
16369 #endif
16370 if (!file.is_open())
16371 TOML_PARSE_FILE_ERROR("File could not be opened for reading", file_path_str);
16372
16373 // get size
16374 const auto file_size = file.tellg();
16375 if (file_size == -1)
16376 TOML_PARSE_FILE_ERROR("Could not determine file size", file_path_str);
16377 file.seekg(0, std::ifstream::beg);
16378
16379 // read the whole file into memory first if the file isn't too large
16380 constexpr auto large_file_threshold = 1024 * 1024 * 2; // 2 MB
16381 if (file_size <= large_file_threshold)
16382 {
16383 std::vector<char> file_data;
16384 file_data.resize(static_cast<size_t>(file_size));
16385 file.read(file_data.data(), static_cast<std::streamsize>(file_size));
16386 return parse(std::string_view{ file_data.data(), file_data.size() }, std::move(file_path_str));
16387 }
16388
16389 // otherwise parse it using the streams
16390 else
16391 return parse(file, std::move(file_path_str));
16392
16393 #undef TOML_PARSE_FILE_ERROR
16394 }
16395 }
16396 TOML_ANON_NAMESPACE_END;
16397
16398 TOML_NAMESPACE_START
16399 {
16400 TOML_ABI_NAMESPACE_BOOL(TOML_EXCEPTIONS, ex, noex);
16401
16402 TOML_EXTERNAL_LINKAGE
16403 parse_result TOML_CALLCONV parse(std::string_view doc, std::string_view source_path)
16404 {
16405 return TOML_ANON_NAMESPACE::do_parse(TOML_ANON_NAMESPACE::utf8_reader{ doc, source_path });
16406 }
16407
16408 TOML_EXTERNAL_LINKAGE
16409 parse_result TOML_CALLCONV parse(std::string_view doc, std::string && source_path)
16410 {
16411 return TOML_ANON_NAMESPACE::do_parse(TOML_ANON_NAMESPACE::utf8_reader{ doc, std::move(source_path) });
16412 }
16413
16414 TOML_EXTERNAL_LINKAGE
16415 parse_result TOML_CALLCONV parse(std::istream & doc, std::string_view source_path)
16416 {
16417 return TOML_ANON_NAMESPACE::do_parse(TOML_ANON_NAMESPACE::utf8_reader{ doc, source_path });
16418 }
16419
16420 TOML_EXTERNAL_LINKAGE
16421 parse_result TOML_CALLCONV parse(std::istream & doc, std::string && source_path)
16422 {
16423 return TOML_ANON_NAMESPACE::do_parse(TOML_ANON_NAMESPACE::utf8_reader{ doc, std::move(source_path) });
16424 }
16425
16426 TOML_EXTERNAL_LINKAGE
16427 parse_result TOML_CALLCONV parse_file(std::string_view file_path)
16428 {
16429 return TOML_ANON_NAMESPACE::do_parse_file(file_path);
16430 }
16431
16432 #if TOML_HAS_CHAR8
16433
16434 TOML_EXTERNAL_LINKAGE
16435 parse_result TOML_CALLCONV parse(std::u8string_view doc, std::string_view source_path)
16436 {
16437 return TOML_ANON_NAMESPACE::do_parse(TOML_ANON_NAMESPACE::utf8_reader{ doc, source_path });
16438 }
16439
16440 TOML_EXTERNAL_LINKAGE
16441 parse_result TOML_CALLCONV parse(std::u8string_view doc, std::string && source_path)
16442 {
16443 return TOML_ANON_NAMESPACE::do_parse(TOML_ANON_NAMESPACE::utf8_reader{ doc, std::move(source_path) });
16444 }
16445
16446 TOML_EXTERNAL_LINKAGE
16447 parse_result TOML_CALLCONV parse_file(std::u8string_view file_path)
16448 {
16449 std::string file_path_str;
16450 file_path_str.resize(file_path.length());
16451 memcpy(file_path_str.data(), file_path.data(), file_path.length());
16452 return TOML_ANON_NAMESPACE::do_parse_file(file_path_str);
16453 }
16454
16455 #endif // TOML_HAS_CHAR8
16456
16457 #if TOML_ENABLE_WINDOWS_COMPAT
16458
16459 TOML_EXTERNAL_LINKAGE
16460 parse_result TOML_CALLCONV parse(std::string_view doc, std::wstring_view source_path)
16461 {
16462 return TOML_ANON_NAMESPACE::do_parse(TOML_ANON_NAMESPACE::utf8_reader{ doc, impl::narrow(source_path) });
16463 }
16464
16465 TOML_EXTERNAL_LINKAGE
16466 parse_result TOML_CALLCONV parse(std::istream & doc, std::wstring_view source_path)
16467 {
16468 return TOML_ANON_NAMESPACE::do_parse(TOML_ANON_NAMESPACE::utf8_reader{ doc, impl::narrow(source_path) });
16469 }
16470
16471 TOML_EXTERNAL_LINKAGE
16472 parse_result TOML_CALLCONV parse_file(std::wstring_view file_path)
16473 {
16474 return TOML_ANON_NAMESPACE::do_parse_file(impl::narrow(file_path));
16475 }
16476
16477 #endif // TOML_ENABLE_WINDOWS_COMPAT
16478
16479 #if TOML_HAS_CHAR8 && TOML_ENABLE_WINDOWS_COMPAT
16480
16481 TOML_EXTERNAL_LINKAGE
16482 parse_result TOML_CALLCONV parse(std::u8string_view doc, std::wstring_view source_path)
16483 {
16484 return TOML_ANON_NAMESPACE::do_parse(TOML_ANON_NAMESPACE::utf8_reader{ doc, impl::narrow(source_path) });
16485 }
16486
16487 #endif // TOML_HAS_CHAR8 && TOML_ENABLE_WINDOWS_COMPAT
16488
16489 TOML_ABI_NAMESPACE_END; // TOML_EXCEPTIONS
16490 }
16491 TOML_NAMESPACE_END;
16492
16493 #undef TOML_OVERALIGNED
16494
16495 #ifdef _MSC_VER
16496 #pragma pop_macro("min")
16497 #pragma pop_macro("max")
16498 #ifndef __clang__
16499 #pragma inline_recursion(off)
16500 #endif
16501 #endif
16502 TOML_POP_WARNINGS;
16503
16504 #endif // TOML_ENABLE_PARSER
16505
16506 //******** impl/formatter.inl ****************************************************************************************
16507
16508 #if TOML_ENABLE_FORMATTERS
16509
16510 TOML_PUSH_WARNINGS;
16511 #ifdef _MSC_VER
16512 #ifndef __clang__
16513 #pragma inline_recursion(on)
16514 #endif
16515 #pragma push_macro("min")
16516 #pragma push_macro("max")
16517 #undef min
16518 #undef max
16519 #endif
16520
16521 TOML_IMPL_NAMESPACE_START
16522 {
16523 enum class TOML_CLOSED_FLAGS_ENUM formatted_string_traits : unsigned
16524 {
16525 none,
16526 line_breaks = 1u << 0, // \n
16527 tabs = 1u << 1, // \t
16528 control_chars = 1u << 2, // also includes non-ascii vertical whitespace
16529 single_quotes = 1u << 3,
16530 non_bare = 1u << 4, // anything not satisfying "is bare key character"
16531 non_ascii = 1u << 5, // any codepoint >= 128
16532
16533 all = (non_ascii << 1u) - 1u
16534 };
16535 TOML_MAKE_FLAGS(formatted_string_traits);
16536
16537 TOML_EXTERNAL_LINKAGE
16538 formatter::formatter(const node* source_node,
16539 const parse_result* source_pr,
16540 const formatter_constants& constants,
16541 const formatter_config& config) noexcept //
16542 #if TOML_ENABLE_PARSER && !TOML_EXCEPTIONS
16543 : source_{ source_pr && *source_pr ? &source_pr->table() : source_node },
16544 result_{ source_pr },
16545 #else
16546 : source_{ source_pr ? source_pr : source_node },
16547 #endif
16548 constants_{ &constants },
16549 config_{ config }
16550 {
16551 TOML_ASSERT_ASSUME(source_);
16552
16553 config_.flags = (config_.flags | constants_->mandatory_flags) & ~constants_->ignored_flags;
16554
16555 indent_columns_ = {};
16556 for (auto c : config_.indent)
16557 indent_columns_ += c == '\t' ? 4u : 1u;
16558
16559 int_format_mask_ = config_.flags
16560 & (format_flags::allow_binary_integers | format_flags::allow_octal_integers
16561 | format_flags::allow_hexadecimal_integers);
16562 }
16563
16564 TOML_EXTERNAL_LINKAGE
16565 void formatter::attach(std::ostream & stream) noexcept
16566 {
16567 indent_ = {};
16568 naked_newline_ = true;
16569 stream_ = &stream;
16570 }
16571
16572 TOML_EXTERNAL_LINKAGE
16573 void formatter::detach() noexcept
16574 {
16575 stream_ = nullptr;
16576 }
16577
16578 TOML_EXTERNAL_LINKAGE
16579 void formatter::print_newline(bool force)
16580 {
16581 if (!naked_newline_ || force)
16582 {
16583 print_to_stream(*stream_, '\n');
16584 naked_newline_ = true;
16585 }
16586 }
16587
16588 TOML_EXTERNAL_LINKAGE
16589 void formatter::print_indent()
16590 {
16591 for (int i = 0; i < indent_; i++)
16592 {
16593 print_to_stream(*stream_, config_.indent);
16594 naked_newline_ = false;
16595 }
16596 }
16597
16598 TOML_EXTERNAL_LINKAGE
16599 void formatter::print_unformatted(char c)
16600 {
16601 print_to_stream(*stream_, c);
16602 naked_newline_ = false;
16603 }
16604
16605 TOML_EXTERNAL_LINKAGE
16606 void formatter::print_unformatted(std::string_view str)
16607 {
16608 print_to_stream(*stream_, str);
16609 naked_newline_ = false;
16610 }
16611
16612 TOML_EXTERNAL_LINKAGE
16613 void formatter::print_string(std::string_view str,
16614 bool allow_multi_line,
16615 bool allow_bare,
16616 bool allow_literal_whitespace)
16617 {
16618 if (str.empty())
16619 {
16620 print_unformatted(literal_strings_allowed() ? "''"sv : "\"\""sv);
16621 return;
16622 }
16623
16624 // pre-scan the string to determine how we should output it
16625 formatted_string_traits traits = {};
16626
16627 if (!allow_bare)
16628 traits |= formatted_string_traits::non_bare;
16629 bool unicode_allowed = unicode_strings_allowed();
16630
16631 // ascii fast path
16632 if (is_ascii(str.data(), str.length()))
16633 {
16634 for (auto c : str)
16635 {
16636 switch (c)
16637 {
16638 case '\n': traits |= formatted_string_traits::line_breaks; break;
16639 case '\t': traits |= formatted_string_traits::tabs; break;
16640 case '\'': traits |= formatted_string_traits::single_quotes; break;
16641 default:
16642 {
16643 if TOML_UNLIKELY(is_control_character(c))
16644 traits |= formatted_string_traits::control_chars;
16645
16646 if (!is_ascii_bare_key_character(static_cast<char32_t>(c)))
16647 traits |= formatted_string_traits::non_bare;
16648 break;
16649 }
16650 }
16651
16652 static constexpr auto all_ascii_traits =
16653 formatted_string_traits::all & ~formatted_string_traits::non_ascii;
16654 if (traits == all_ascii_traits)
16655 break;
16656 }
16657 }
16658
16659 // unicode slow path
16660 else
16661 {
16662 traits |= formatted_string_traits::non_ascii;
16663 utf8_decoder decoder;
16664
16665 // if the unicode is malformed just treat the string as a single-line non-literal and
16666 // escape all non-ascii characters (to ensure round-tripping and help with diagnostics)
16667 const auto bad_unicode = [&]() noexcept
16668 {
16669 traits &= ~formatted_string_traits::line_breaks;
16670 traits |= formatted_string_traits::control_chars | formatted_string_traits::non_bare;
16671 unicode_allowed = false;
16672 };
16673
16674 for (auto c : str)
16675 {
16676 decoder(c);
16677
16678 if TOML_UNLIKELY(decoder.error())
16679 {
16680 bad_unicode();
16681 break;
16682 }
16683
16684 if (!decoder.has_code_point())
16685 continue;
16686
16687 switch (decoder.codepoint)
16688 {
16689 case U'\n': traits |= formatted_string_traits::line_breaks; break;
16690 case U'\t': traits |= formatted_string_traits::tabs; break;
16691 case U'\'': traits |= formatted_string_traits::single_quotes; break;
16692 default:
16693 {
16694 if TOML_UNLIKELY(is_control_character(decoder.codepoint)
16695 || is_non_ascii_vertical_whitespace(decoder.codepoint))
16696 traits |= formatted_string_traits::control_chars;
16697
16698 if (!is_bare_key_character(decoder.codepoint))
16699 traits |= formatted_string_traits::non_bare;
16700 break;
16701 }
16702 }
16703 }
16704
16705 if (decoder.needs_more_input())
16706 bad_unicode();
16707 }
16708
16709 // strings with line breaks, tabs, and single-quotes can't be bare
16710 if (!!(traits
16711 & (formatted_string_traits::line_breaks | formatted_string_traits::tabs
16712 | formatted_string_traits::single_quotes)))
16713 traits |= formatted_string_traits::non_bare;
16714
16715 // if the string meets the requirements of being 'bare' we can emit a bare string
16716 // (bare strings are composed of letters and numbers; no whitespace, control chars, quotes, etc)
16717 if (!(traits & formatted_string_traits::non_bare)
16718 && (!(traits & formatted_string_traits::non_ascii) || unicode_allowed))
16719 {
16720 print_unformatted(str);
16721 return;
16722 }
16723 const auto real_tabs_allowed = allow_literal_whitespace && real_tabs_in_strings_allowed();
16724
16725 // determine if this should be a multi-line string (triple-quotes)
16726 const auto multi_line = allow_literal_whitespace //
16727 && allow_multi_line //
16728 && multi_line_strings_allowed() //
16729 && !!(traits & formatted_string_traits::line_breaks);
16730
16731 // determine if this should be a literal string (single-quotes with no escaping)
16732 const auto literal = literal_strings_allowed() //
16733 && !(traits & formatted_string_traits::control_chars) //
16734 && (!(traits & formatted_string_traits::single_quotes) || multi_line) //
16735 && (!(traits & formatted_string_traits::tabs) || real_tabs_allowed) //
16736 && (!(traits & formatted_string_traits::line_breaks) || multi_line) //
16737 && (!(traits & formatted_string_traits::non_ascii) || unicode_allowed);
16738
16739 // literal strings (single quotes, no escape codes)
16740 if (literal)
16741 {
16742 const auto quot = multi_line ? R"(''')"sv : R"(')"sv;
16743 print_unformatted(quot);
16744 print_unformatted(str);
16745 print_unformatted(quot);
16746 return;
16747 }
16748
16749 // anything from here down is a non-literal string, so requires iteration and escaping.
16750 print_unformatted(multi_line ? R"(""")"sv : R"(")"sv);
16751
16752 // ascii fast path
16753 if (!(traits & formatted_string_traits::non_ascii))
16754 {
16755 for (auto c : str)
16756 {
16757 switch (c)
16758 {
16759 case '"': print_to_stream(*stream_, R"(\")"sv); break;
16760 case '\\': print_to_stream(*stream_, R"(\\)"sv); break;
16761 case '\x7F': print_to_stream(*stream_, R"(\u007F)"sv); break;
16762 case '\t': print_to_stream(*stream_, real_tabs_allowed ? "\t"sv : R"(\t)"sv); break;
16763 case '\n': print_to_stream(*stream_, multi_line ? "\n"sv : R"(\n)"sv); break;
16764 default:
16765 {
16766 // control characters from lookup table
16767 if TOML_UNLIKELY(c >= '\x00' && c <= '\x1F')
16768 print_to_stream(*stream_, control_char_escapes[c]);
16769
16770 // regular characters
16771 else
16772 print_to_stream(*stream_, c);
16773 }
16774 }
16775 }
16776 }
16777
16778 // unicode slow path
16779 else
16780 {
16781 utf8_decoder decoder;
16782 const char* cp_start = str.data();
16783 const char* cp_end = cp_start;
16784 for (auto c : str)
16785 {
16786 decoder(c);
16787 cp_end++;
16788
16789 // if the decoder encounters malformed unicode just emit raw bytes and
16790 if (decoder.error())
16791 {
16792 while (cp_start != cp_end)
16793 {
16794 print_to_stream(*stream_, R"(\u00)"sv);
16795 print_to_stream(*stream_,
16796 static_cast<uint8_t>(*cp_start),
16797 value_flags::format_as_hexadecimal,
16798 2);
16799 cp_start++;
16800 }
16801 decoder.reset();
16802 continue;
16803 }
16804
16805 if (!decoder.has_code_point())
16806 continue;
16807
16808 switch (decoder.codepoint)
16809 {
16810 case U'"': print_to_stream(*stream_, R"(\")"sv); break;
16811 case U'\\': print_to_stream(*stream_, R"(\\)"sv); break;
16812 case U'\x7F': print_to_stream(*stream_, R"(\u007F)"sv); break;
16813 case U'\t': print_to_stream(*stream_, real_tabs_allowed ? "\t"sv : R"(\t)"sv); break;
16814 case U'\n': print_to_stream(*stream_, multi_line ? "\n"sv : R"(\n)"sv); break;
16815 default:
16816 {
16817 // control characters from lookup table
16818 if TOML_UNLIKELY(decoder.codepoint <= U'\x1F')
16819 print_to_stream(*stream_,
16820 control_char_escapes[static_cast<uint_least32_t>(decoder.codepoint)]);
16821
16822 // escaped unicode characters
16823 else if (decoder.codepoint > U'\x7F'
16824 && (!unicode_allowed || is_non_ascii_vertical_whitespace(decoder.codepoint)))
16825 {
16826 if (static_cast<uint_least32_t>(decoder.codepoint) > 0xFFFFu)
16827 {
16828 print_to_stream(*stream_, R"(\U)"sv);
16829 print_to_stream(*stream_,
16830 static_cast<uint_least32_t>(decoder.codepoint),
16831 value_flags::format_as_hexadecimal,
16832 8);
16833 }
16834 else
16835 {
16836 print_to_stream(*stream_, R"(\u)"sv);
16837 print_to_stream(*stream_,
16838 static_cast<uint_least32_t>(decoder.codepoint),
16839 value_flags::format_as_hexadecimal,
16840 4);
16841 }
16842 }
16843
16844 // regular characters
16845 else
16846 print_to_stream(*stream_, cp_start, static_cast<size_t>(cp_end - cp_start));
16847 }
16848 }
16849
16850 cp_start = cp_end;
16851 }
16852 }
16853
16854 print_unformatted(multi_line ? R"(""")"sv : R"(")"sv);
16855 }
16856
16857 TOML_EXTERNAL_LINKAGE
16858 void formatter::print(const value<std::string>& val)
16859 {
16860 print_string(val.get());
16861 }
16862
16863 TOML_EXTERNAL_LINKAGE
16864 void formatter::print(const value<int64_t>& val)
16865 {
16866 naked_newline_ = false;
16867
16868 if (*val >= 0 && !!int_format_mask_)
16869 {
16870 static constexpr auto value_flags_mask =
16871 value_flags::format_as_binary | value_flags::format_as_octal | value_flags::format_as_hexadecimal;
16872
16873 const auto fmt = val.flags() & value_flags_mask;
16874 switch (fmt)
16875 {
16876 case value_flags::format_as_binary:
16877 if (!!(int_format_mask_ & format_flags::allow_binary_integers))
16878 {
16879 print_to_stream(*stream_, "0b"sv);
16880 print_to_stream(*stream_, *val, fmt);
16881 return;
16882 }
16883 break;
16884
16885 case value_flags::format_as_octal:
16886 if (!!(int_format_mask_ & format_flags::allow_octal_integers))
16887 {
16888 print_to_stream(*stream_, "0o"sv);
16889 print_to_stream(*stream_, *val, fmt);
16890 return;
16891 }
16892 break;
16893
16894 case value_flags::format_as_hexadecimal:
16895 if (!!(int_format_mask_ & format_flags::allow_hexadecimal_integers))
16896 {
16897 print_to_stream(*stream_, "0x"sv);
16898 print_to_stream(*stream_, *val, fmt);
16899 return;
16900 }
16901 break;
16902
16903 default: break;
16904 }
16905 }
16906
16907 // fallback to decimal
16908 print_to_stream(*stream_, *val);
16909 }
16910
16911 TOML_EXTERNAL_LINKAGE
16912 void formatter::print(const value<double>& val)
16913 {
16914 const std::string_view* inf_nan = nullptr;
16915 switch (fpclassify(*val))
16916 {
16917 case fp_class::neg_inf: inf_nan = &constants_->float_neg_inf; break;
16918 case fp_class::pos_inf: inf_nan = &constants_->float_pos_inf; break;
16919 case fp_class::nan: inf_nan = &constants_->float_nan; break;
16920 case fp_class::ok:
16921 print_to_stream(*stream_,
16922 *val,
16923 value_flags::none,
16924 !!(config_.flags & format_flags::relaxed_float_precision));
16925 break;
16926 default: TOML_UNREACHABLE;
16927 }
16928
16929 if (inf_nan)
16930 {
16931 if (!!(config_.flags & format_flags::quote_infinities_and_nans))
16932 print_to_stream_bookended(*stream_, *inf_nan, '"');
16933 else
16934 print_to_stream(*stream_, *inf_nan);
16935 }
16936
16937 naked_newline_ = false;
16938 }
16939
16940 TOML_EXTERNAL_LINKAGE
16941 void formatter::print(const value<bool>& val)
16942 {
16943 print_unformatted(*val ? constants_->bool_true : constants_->bool_false);
16944 }
16945
16946 TOML_EXTERNAL_LINKAGE
16947 void formatter::print(const value<date>& val)
16948 {
16949 if (!!(config_.flags & format_flags::quote_dates_and_times))
16950 print_to_stream_bookended(*stream_, *val, literal_strings_allowed() ? '\'' : '"');
16951 else
16952 print_to_stream(*stream_, *val);
16953 naked_newline_ = false;
16954 }
16955
16956 TOML_EXTERNAL_LINKAGE
16957 void formatter::print(const value<time>& val)
16958 {
16959 if (!!(config_.flags & format_flags::quote_dates_and_times))
16960 print_to_stream_bookended(*stream_, *val, literal_strings_allowed() ? '\'' : '"');
16961 else
16962 print_to_stream(*stream_, *val);
16963 naked_newline_ = false;
16964 }
16965
16966 TOML_EXTERNAL_LINKAGE
16967 void formatter::print(const value<date_time>& val)
16968 {
16969 if (!!(config_.flags & format_flags::quote_dates_and_times))
16970 print_to_stream_bookended(*stream_, *val, literal_strings_allowed() ? '\'' : '"');
16971 else
16972 print_to_stream(*stream_, *val);
16973 naked_newline_ = false;
16974 }
16975
16976 TOML_EXTERNAL_LINKAGE
16977 void formatter::print_value(const node& val_node, node_type type)
16978 {
16979 TOML_ASSUME(type > node_type::array);
16980 switch (type)
16981 {
16982 case node_type::string: print(*reinterpret_cast<const value<std::string>*>(&val_node)); break;
16983 case node_type::integer: print(*reinterpret_cast<const value<int64_t>*>(&val_node)); break;
16984 case node_type::floating_point: print(*reinterpret_cast<const value<double>*>(&val_node)); break;
16985 case node_type::boolean: print(*reinterpret_cast<const value<bool>*>(&val_node)); break;
16986 case node_type::date: print(*reinterpret_cast<const value<date>*>(&val_node)); break;
16987 case node_type::time: print(*reinterpret_cast<const value<time>*>(&val_node)); break;
16988 case node_type::date_time: print(*reinterpret_cast<const value<date_time>*>(&val_node)); break;
16989 default: TOML_UNREACHABLE;
16990 }
16991 }
16992
16993 #if TOML_ENABLE_PARSER && !TOML_EXCEPTIONS
16994
16995 TOML_EXTERNAL_LINKAGE
16996 bool formatter::dump_failed_parse_result()
16997 {
16998 if (result_ && !(*result_))
16999 {
17000 stream() << result_->error();
17001 return true;
17002 }
17003 return false;
17004 }
17005
17006 #else
17007
17008 TOML_EXTERNAL_LINKAGE
17009 TOML_ATTR(const)
17010 bool formatter::dump_failed_parse_result()
17011 {
17012 return false;
17013 }
17014
17015 #endif
17016 }
17017 TOML_IMPL_NAMESPACE_END;
17018
17019 #ifdef _MSC_VER
17020 #pragma pop_macro("min")
17021 #pragma pop_macro("max")
17022 #ifndef __clang__
17023 #pragma inline_recursion(off)
17024 #endif
17025 #endif
17026 TOML_POP_WARNINGS;
17027
17028 #endif // TOML_ENABLE_FORMATTERS
17029
17030 //******** impl/toml_formatter.inl ***********************************************************************************
17031
17032 #if TOML_ENABLE_FORMATTERS
17033
17034 TOML_PUSH_WARNINGS;
17035 #ifdef _MSC_VER
17036 #ifndef __clang__
17037 #pragma inline_recursion(on)
17038 #endif
17039 #pragma push_macro("min")
17040 #pragma push_macro("max")
17041 #undef min
17042 #undef max
17043 #endif
17044
17045 TOML_DISABLE_ARITHMETIC_WARNINGS;
17046
17047 TOML_ANON_NAMESPACE_START
17048 {
17049 TOML_INTERNAL_LINKAGE
17050 size_t toml_formatter_count_inline_columns(const node& node, size_t line_wrap_cols) noexcept
17051 {
17052 switch (node.type())
17053 {
17054 case node_type::table:
17055 {
17056 auto& tbl = *reinterpret_cast<const table*>(&node);
17057 if (tbl.empty())
17058 return 2u; // "{}"
17059 size_t weight = 3u; // "{ }"
17060 for (auto&& [k, v] : tbl)
17061 {
17062 weight += k.length() + toml_formatter_count_inline_columns(v, line_wrap_cols) + 2u; // + ", "
17063 if (weight >= line_wrap_cols)
17064 break;
17065 }
17066 return weight;
17067 }
17068
17069 case node_type::array:
17070 {
17071 auto& arr = *reinterpret_cast<const array*>(&node);
17072 if (arr.empty())
17073 return 2u; // "[]"
17074 size_t weight = 3u; // "[ ]"
17075 for (auto& elem : arr)
17076 {
17077 weight += toml_formatter_count_inline_columns(elem, line_wrap_cols) + 2u; // + ", "
17078 if (weight >= line_wrap_cols)
17079 break;
17080 }
17081 return weight;
17082 }
17083
17084 case node_type::string:
17085 {
17086 // todo: proper utf8 decoding?
17087 // todo: tab awareness?
17088 auto& str = (*reinterpret_cast<const value<std::string>*>(&node)).get();
17089 return str.length() + 2u; // + ""
17090 }
17091
17092 case node_type::integer:
17093 {
17094 auto val = (*reinterpret_cast<const value<int64_t>*>(&node)).get();
17095 if (!val)
17096 return 1u;
17097 size_t weight = {};
17098 if (val < 0)
17099 {
17100 weight += 1u;
17101 val *= -1;
17102 }
17103 return weight + static_cast<size_t>(log10(static_cast<double>(val))) + 1u;
17104 }
17105
17106 case node_type::floating_point:
17107 {
17108 auto val = (*reinterpret_cast<const value<double>*>(&node)).get();
17109 if (val == 0.0)
17110 return 3u; // "0.0"
17111 size_t weight = 2u; // ".0"
17112 if (val < 0.0)
17113 {
17114 weight += 1u;
17115 val *= -1.0;
17116 }
17117 return weight + static_cast<size_t>(abs(log10(val))) + 1u;
17118 }
17119
17120 case node_type::boolean: return 5u;
17121 case node_type::date: [[fallthrough]];
17122 case node_type::time: return 10u;
17123 case node_type::date_time: return 30u;
17124 case node_type::none: TOML_UNREACHABLE;
17125 default: TOML_UNREACHABLE;
17126 }
17127
17128 TOML_UNREACHABLE;
17129 }
17130
17131 TOML_INTERNAL_LINKAGE
17132 bool toml_formatter_forces_multiline(const node& node, size_t line_wrap_cols, size_t starting_column_bias) noexcept
17133 {
17134 return (toml_formatter_count_inline_columns(node, line_wrap_cols) + starting_column_bias) >= line_wrap_cols;
17135 }
17136 }
17137 TOML_ANON_NAMESPACE_END;
17138
17139 TOML_NAMESPACE_START
17140 {
17141 TOML_EXTERNAL_LINKAGE
17142 void toml_formatter::print_pending_table_separator()
17143 {
17144 if (pending_table_separator_)
17145 {
17146 print_newline(true);
17147 print_newline(true);
17148 pending_table_separator_ = false;
17149 }
17150 }
17151
17152 TOML_EXTERNAL_LINKAGE
17153 void toml_formatter::print(const key& k)
17154 {
17155 print_string(k.str(), false, true, false);
17156 }
17157
17158 TOML_EXTERNAL_LINKAGE
17159 void toml_formatter::print_inline(const table& tbl)
17160 {
17161 if (tbl.empty())
17162 {
17163 print_unformatted("{}"sv);
17164 return;
17165 }
17166
17167 print_unformatted("{ "sv);
17168
17169 bool first = false;
17170 for (auto&& [k, v] : tbl)
17171 {
17172 if (first)
17173 print_unformatted(", "sv);
17174 first = true;
17175
17176 print(k);
17177 if (terse_kvps())
17178 print_unformatted("="sv);
17179 else
17180 print_unformatted(" = "sv);
17181
17182 const auto type = v.type();
17183 TOML_ASSUME(type != node_type::none);
17184 switch (type)
17185 {
17186 case node_type::table: print_inline(*reinterpret_cast<const table*>(&v)); break;
17187 case node_type::array: print(*reinterpret_cast<const array*>(&v)); break;
17188 default: print_value(v, type);
17189 }
17190 }
17191
17192 print_unformatted(" }"sv);
17193 }
17194
17195 TOML_EXTERNAL_LINKAGE
17196 void toml_formatter::print(const array& arr)
17197 {
17198 if (arr.empty())
17199 {
17200 print_unformatted("[]"sv);
17201 return;
17202 }
17203
17204 const auto original_indent = indent();
17205 const auto multiline = force_multiline_arrays()
17206 || TOML_ANON_NAMESPACE::toml_formatter_forces_multiline(
17207 arr,
17208 120u,
17209 indent_columns() * static_cast<size_t>(original_indent < 0 ? 0 : original_indent));
17210
17211 print_unformatted("["sv);
17212
17213 if (multiline)
17214 {
17215 if (original_indent < 0)
17216 indent(0);
17217 if (indent_array_elements())
17218 increase_indent();
17219 }
17220 else
17221 print_unformatted(' ');
17222
17223 for (size_t i = 0; i < arr.size(); i++)
17224 {
17225 if (i > 0u)
17226 {
17227 print_unformatted(',');
17228 if (!multiline)
17229 print_unformatted(' ');
17230 }
17231
17232 if (multiline)
17233 {
17234 print_newline(true);
17235 print_indent();
17236 }
17237
17238 auto& v = arr[i];
17239 const auto type = v.type();
17240 TOML_ASSUME(type != node_type::none);
17241 switch (type)
17242 {
17243 case node_type::table: print_inline(*reinterpret_cast<const table*>(&v)); break;
17244 case node_type::array: print(*reinterpret_cast<const array*>(&v)); break;
17245 default: print_value(v, type);
17246 }
17247 }
17248 if (multiline)
17249 {
17250 indent(original_indent);
17251 print_newline(true);
17252 print_indent();
17253 }
17254 else
17255 print_unformatted(' ');
17256
17257 print_unformatted("]"sv);
17258 }
17259
17260 TOML_EXTERNAL_LINKAGE
17261 void toml_formatter::print(const table& tbl)
17262 {
17263 static constexpr auto is_non_inline_array_of_tables = [](const node& n) noexcept
17264 {
17265 const auto arr = n.as_array();
17266 if (!arr || !arr->is_array_of_tables())
17267 return false;
17268
17269 return !reinterpret_cast<const table*>(&(*arr)[0])->is_inline();
17270 };
17271
17272 // values, arrays, and inline tables/table arrays
17273 for (auto&& [k, v] : tbl)
17274 {
17275 const auto type = v.type();
17276 if ((type == node_type::table && !reinterpret_cast<const table*>(&v)->is_inline())
17277 || (type == node_type::array && is_non_inline_array_of_tables(v)))
17278 continue;
17279
17280 pending_table_separator_ = true;
17281 print_newline();
17282 print_indent();
17283 print(k);
17284 if (terse_kvps())
17285 print_unformatted("="sv);
17286 else
17287 print_unformatted(" = "sv);
17288 TOML_ASSUME(type != node_type::none);
17289 switch (type)
17290 {
17291 case node_type::table: print_inline(*reinterpret_cast<const table*>(&v)); break;
17292 case node_type::array: print(*reinterpret_cast<const array*>(&v)); break;
17293 default: print_value(v, type);
17294 }
17295 }
17296
17297 const auto print_key_path = [&]()
17298 {
17299 size_t i{};
17300 for (const auto k : key_path_)
17301 {
17302 if (i++)
17303 print_unformatted('.');
17304 print(*k);
17305 }
17306 };
17307
17308 // non-inline tables
17309 for (auto&& [k, v] : tbl)
17310 {
17311 const auto type = v.type();
17312 if (type != node_type::table || reinterpret_cast<const table*>(&v)->is_inline())
17313 continue;
17314 auto& child_tbl = *reinterpret_cast<const table*>(&v);
17315
17316 // we can skip indenting and emitting the headers for tables that only contain other tables
17317 // (so we don't over-nest)
17318 size_t child_value_count{}; // includes inline tables and non-table arrays
17319 size_t child_table_count{};
17320 size_t child_table_array_count{};
17321 for (auto&& [child_k, child_v] : child_tbl)
17322 {
17323 TOML_UNUSED(child_k);
17324 const auto child_type = child_v.type();
17325 TOML_ASSUME(child_type != node_type::none);
17326 switch (child_type)
17327 {
17328 case node_type::table:
17329 if (reinterpret_cast<const table*>(&child_v)->is_inline())
17330 child_value_count++;
17331 else
17332 child_table_count++;
17333 break;
17334
17335 case node_type::array:
17336 if (is_non_inline_array_of_tables(child_v))
17337 child_table_array_count++;
17338 else
17339 child_value_count++;
17340 break;
17341
17342 default: child_value_count++;
17343 }
17344 }
17345 bool skip_self = false;
17346 if (child_value_count == 0u && (child_table_count > 0u || child_table_array_count > 0u))
17347 skip_self = true;
17348
17349 key_path_.push_back(&k);
17350
17351 if (!skip_self)
17352 {
17353 print_pending_table_separator();
17354 if (indent_sub_tables())
17355 increase_indent();
17356 print_indent();
17357 print_unformatted("["sv);
17358 print_key_path();
17359 print_unformatted("]"sv);
17360 pending_table_separator_ = true;
17361 }
17362
17363 print(child_tbl);
17364
17365 key_path_.pop_back();
17366 if (!skip_self && indent_sub_tables())
17367 decrease_indent();
17368 }
17369
17370 // table arrays
17371 for (auto&& [k, v] : tbl)
17372 {
17373 if (!is_non_inline_array_of_tables(v))
17374 continue;
17375 auto& arr = *reinterpret_cast<const array*>(&v);
17376
17377 if (indent_sub_tables())
17378 increase_indent();
17379 key_path_.push_back(&k);
17380
17381 for (size_t i = 0; i < arr.size(); i++)
17382 {
17383 print_pending_table_separator();
17384 print_indent();
17385 print_unformatted("[["sv);
17386 print_key_path();
17387 print_unformatted("]]"sv);
17388 pending_table_separator_ = true;
17389 print(*reinterpret_cast<const table*>(&arr[i]));
17390 }
17391
17392 key_path_.pop_back();
17393 if (indent_sub_tables())
17394 decrease_indent();
17395 }
17396 }
17397
17398 TOML_EXTERNAL_LINKAGE
17399 void toml_formatter::print()
17400 {
17401 if (dump_failed_parse_result())
17402 return;
17403
17404 switch (auto source_type = source().type())
17405 {
17406 case node_type::table:
17407 {
17408 auto& tbl = *reinterpret_cast<const table*>(&source());
17409 if (tbl.is_inline())
17410 print_inline(tbl);
17411 else
17412 {
17413 decrease_indent(); // so root kvps and tables have the same indent
17414 print(tbl);
17415 }
17416 break;
17417 }
17418
17419 case node_type::array: print(*reinterpret_cast<const array*>(&source())); break;
17420
17421 default: print_value(source(), source_type);
17422 }
17423 }
17424 }
17425 TOML_NAMESPACE_END;
17426
17427 #ifdef _MSC_VER
17428 #pragma pop_macro("min")
17429 #pragma pop_macro("max")
17430 #ifndef __clang__
17431 #pragma inline_recursion(off)
17432 #endif
17433 #endif
17434 TOML_POP_WARNINGS;
17435
17436 #endif // TOML_ENABLE_FORMATTERS
17437
17438 //******** impl/json_formatter.inl ***********************************************************************************
17439
17440 #if TOML_ENABLE_FORMATTERS
17441
17442 TOML_PUSH_WARNINGS;
17443 #ifdef _MSC_VER
17444 #ifndef __clang__
17445 #pragma inline_recursion(on)
17446 #endif
17447 #pragma push_macro("min")
17448 #pragma push_macro("max")
17449 #undef min
17450 #undef max
17451 #endif
17452
17453 TOML_NAMESPACE_START
17454 {
17455 TOML_EXTERNAL_LINKAGE
17456 void json_formatter::print(const toml::table& tbl)
17457 {
17458 if (tbl.empty())
17459 {
17460 print_unformatted("{}"sv);
17461 return;
17462 }
17463
17464 print_unformatted('{');
17465
17466 if (indent_sub_tables())
17467 increase_indent();
17468 bool first = false;
17469 for (auto&& [k, v] : tbl)
17470 {
17471 if (first)
17472 print_unformatted(',');
17473 first = true;
17474 print_newline(true);
17475 print_indent();
17476
17477 print_string(k.str(), false);
17478 if (terse_kvps())
17479 print_unformatted(":"sv);
17480 else
17481 print_unformatted(" : "sv);
17482
17483 const auto type = v.type();
17484 TOML_ASSUME(type != node_type::none);
17485 switch (type)
17486 {
17487 case node_type::table: print(*reinterpret_cast<const table*>(&v)); break;
17488 case node_type::array: print(*reinterpret_cast<const array*>(&v)); break;
17489 default: print_value(v, type);
17490 }
17491 }
17492 if (indent_sub_tables())
17493 decrease_indent();
17494 print_newline(true);
17495 print_indent();
17496
17497 print_unformatted('}');
17498 }
17499
17500 TOML_EXTERNAL_LINKAGE
17501 void json_formatter::print(const toml::array& arr)
17502 {
17503 if (arr.empty())
17504 {
17505 print_unformatted("[]"sv);
17506 return;
17507 }
17508
17509 print_unformatted('[');
17510 if (indent_array_elements())
17511 increase_indent();
17512 for (size_t i = 0; i < arr.size(); i++)
17513 {
17514 if (i > 0u)
17515 print_unformatted(',');
17516 print_newline(true);
17517 print_indent();
17518
17519 auto& v = arr[i];
17520 const auto type = v.type();
17521 TOML_ASSUME(type != node_type::none);
17522 switch (type)
17523 {
17524 case node_type::table: print(*reinterpret_cast<const table*>(&v)); break;
17525 case node_type::array: print(*reinterpret_cast<const array*>(&v)); break;
17526 default: print_value(v, type);
17527 }
17528 }
17529 if (indent_array_elements())
17530 decrease_indent();
17531 print_newline(true);
17532 print_indent();
17533 print_unformatted(']');
17534 }
17535
17536 TOML_EXTERNAL_LINKAGE
17537 void json_formatter::print()
17538 {
17539 if (dump_failed_parse_result())
17540 return;
17541
17542 switch (auto source_type = source().type())
17543 {
17544 case node_type::table: print(*reinterpret_cast<const table*>(&source())); break;
17545 case node_type::array: print(*reinterpret_cast<const array*>(&source())); break;
17546 default: print_value(source(), source_type);
17547 }
17548 }
17549 }
17550 TOML_NAMESPACE_END;
17551
17552 #ifdef _MSC_VER
17553 #pragma pop_macro("min")
17554 #pragma pop_macro("max")
17555 #ifndef __clang__
17556 #pragma inline_recursion(off)
17557 #endif
17558 #endif
17559 TOML_POP_WARNINGS;
17560
17561 #endif // TOML_ENABLE_FORMATTERS
17562
17563 //******** impl/yaml_formatter.inl ***********************************************************************************
17564
17565 #if TOML_ENABLE_FORMATTERS
17566
17567 TOML_PUSH_WARNINGS;
17568 #ifdef _MSC_VER
17569 #ifndef __clang__
17570 #pragma inline_recursion(on)
17571 #endif
17572 #pragma push_macro("min")
17573 #pragma push_macro("max")
17574 #undef min
17575 #undef max
17576 #endif
17577
17578 TOML_NAMESPACE_START
17579 {
17580 TOML_EXTERNAL_LINKAGE
17581 void yaml_formatter::print_yaml_string(const value<std::string>& str)
17582 {
17583 if (str->empty())
17584 {
17585 base::print(str);
17586 return;
17587 }
17588
17589 bool contains_newline = false;
17590 for (auto c = str->c_str(), e = str->c_str() + str->length(); c < e && !contains_newline; c++)
17591 contains_newline = *c == '\n';
17592
17593 if (contains_newline)
17594 {
17595 print_unformatted("|-"sv);
17596
17597 increase_indent();
17598
17599 auto line_end = str->c_str() - 1u;
17600 const auto end = str->c_str() + str->length();
17601 while (line_end != end)
17602 {
17603 auto line_start = line_end + 1u;
17604 line_end = line_start;
17605 for (; line_end != end && *line_end != '\n'; line_end++)
17606 ;
17607
17608 if TOML_LIKELY(line_start != line_end || line_end != end)
17609 {
17610 print_newline();
17611 print_indent();
17612 print_unformatted(std::string_view{ line_start, static_cast<size_t>(line_end - line_start) });
17613 }
17614 }
17615
17616 decrease_indent();
17617 }
17618 else
17619 print_string(*str, false, true);
17620 }
17621
17622 TOML_EXTERNAL_LINKAGE
17623 void yaml_formatter::print(const toml::table& tbl, bool parent_is_array)
17624 {
17625 if (tbl.empty())
17626 {
17627 print_unformatted("{}"sv);
17628 return;
17629 }
17630
17631 increase_indent();
17632
17633 for (auto&& [k, v] : tbl)
17634 {
17635 if (!parent_is_array)
17636 {
17637 print_newline();
17638 print_indent();
17639 }
17640 parent_is_array = false;
17641
17642 print_string(k.str(), false, true);
17643 if (terse_kvps())
17644 print_unformatted(":"sv);
17645 else
17646 print_unformatted(": "sv);
17647
17648 const auto type = v.type();
17649 TOML_ASSUME(type != node_type::none);
17650 switch (type)
17651 {
17652 case node_type::table: print(*reinterpret_cast<const table*>(&v)); break;
17653 case node_type::array: print(*reinterpret_cast<const array*>(&v)); break;
17654 case node_type::string: print_yaml_string(*reinterpret_cast<const value<std::string>*>(&v)); break;
17655 default: print_value(v, type);
17656 }
17657 }
17658
17659 decrease_indent();
17660 }
17661
17662 TOML_EXTERNAL_LINKAGE
17663 void yaml_formatter::print(const toml::array& arr, bool parent_is_array)
17664 {
17665 if (arr.empty())
17666 {
17667 print_unformatted("[]"sv);
17668 return;
17669 }
17670
17671 increase_indent();
17672
17673 for (auto&& v : arr)
17674 {
17675 if (!parent_is_array)
17676 {
17677 print_newline();
17678 print_indent();
17679 }
17680 parent_is_array = false;
17681
17682 print_unformatted("- "sv);
17683
17684 const auto type = v.type();
17685 TOML_ASSUME(type != node_type::none);
17686 switch (type)
17687 {
17688 case node_type::table: print(*reinterpret_cast<const table*>(&v), true); break;
17689 case node_type::array: print(*reinterpret_cast<const array*>(&v), true); break;
17690 case node_type::string: print_yaml_string(*reinterpret_cast<const value<std::string>*>(&v)); break;
17691 default: print_value(v, type);
17692 }
17693 }
17694
17695 decrease_indent();
17696 }
17697
17698 TOML_EXTERNAL_LINKAGE
17699 void yaml_formatter::print()
17700 {
17701 if (dump_failed_parse_result())
17702 return;
17703
17704 switch (auto source_type = source().type())
17705 {
17706 case node_type::table:
17707 decrease_indent(); // so root kvps and tables have the same indent
17708 print(*reinterpret_cast<const table*>(&source()));
17709 break;
17710
17711 case node_type::array: print(*reinterpret_cast<const array*>(&source())); break;
17712
17713 case node_type::string: print_yaml_string(*reinterpret_cast<const value<std::string>*>(&source())); break;
17714
17715 default: print_value(source(), source_type);
17716 }
17717 }
17718 }
17719 TOML_NAMESPACE_END;
17720
17721 #ifdef _MSC_VER
17722 #pragma pop_macro("min")
17723 #pragma pop_macro("max")
17724 #ifndef __clang__
17725 #pragma inline_recursion(off)
17726 #endif
17727 #endif
17728 TOML_POP_WARNINGS;
17729
17730 #endif // TOML_ENABLE_FORMATTERS
17731
17732 #endif // TOML_IMPLEMENTATION
17733
17734 TOML_POP_WARNINGS;
17735
17736 // macro hygiene
17737 #if TOML_UNDEF_MACROS
17738 #undef TOML_ABI_NAMESPACE_BOOL
17739 #undef TOML_ABI_NAMESPACE_END
17740 #undef TOML_ABI_NAMESPACE_START
17741 #undef TOML_ABI_NAMESPACES
17742 #undef TOML_ABSTRACT_INTERFACE
17743 #undef TOML_ALWAYS_INLINE
17744 #undef TOML_ANON_NAMESPACE
17745 #undef TOML_ANON_NAMESPACE_END
17746 #undef TOML_ANON_NAMESPACE_START
17747 #undef TOML_ARCH_AMD64
17748 #undef TOML_ARCH_ARM
17749 #undef TOML_ARCH_ARM32
17750 #undef TOML_ARCH_ARM64
17751 #undef TOML_ARCH_BITNESS
17752 #undef TOML_ARCH_ITANIUM
17753 #undef TOML_ARCH_X64
17754 #undef TOML_ARCH_X86
17755 #undef TOML_ASSERT
17756 #undef TOML_ASSERT_ASSUME
17757 #undef TOML_ASSUME
17758 #undef TOML_ASYMMETRICAL_EQUALITY_OPS
17759 #undef TOML_ATTR
17760 #undef TOML_CLANG
17761 #undef TOML_CLANG_VERSION
17762 #undef TOML_CLOSED_ENUM
17763 #undef TOML_CLOSED_FLAGS_ENUM
17764 #undef TOML_COMPILER_HAS_EXCEPTIONS
17765 #undef TOML_COMPILER_HAS_RTTI
17766 #undef TOML_CONST
17767 #undef TOML_CONST_GETTER
17768 #undef TOML_CONST_INLINE_GETTER
17769 #undef TOML_CONSTRAINED_TEMPLATE
17770 #undef TOML_CPP
17771 #undef TOML_DECLSPEC
17772 #undef TOML_DELETE_DEFAULTS
17773 #undef TOML_DISABLE_ARITHMETIC_WARNINGS
17774 #undef TOML_DISABLE_CODE_ANALYSIS_WARNINGS
17775 #undef TOML_DISABLE_SPAM_WARNINGS
17776 #undef TOML_DISABLE_SPAM_WARNINGS_CLANG_10
17777 #undef TOML_DISABLE_SPAM_WARNINGS_CLANG_11
17778 #undef TOML_DISABLE_SUGGEST_ATTR_WARNINGS
17779 #undef TOML_DISABLE_SWITCH_WARNINGS
17780 #undef TOML_DISABLE_WARNINGS
17781 #undef TOML_DOXYGEN
17782 #undef TOML_EMPTY_BASES
17783 #undef TOML_ENABLE_IF
17784 #undef TOML_ENABLE_WARNINGS
17785 #undef TOML_EVAL_BOOL_0
17786 #undef TOML_EVAL_BOOL_1
17787 #undef TOML_EXTERNAL_LINKAGE
17788 #undef TOML_FLAGS_ENUM
17789 #undef TOML_FLOAT_CHARCONV
17790 #undef TOML_FLOAT128
17791 #undef TOML_FLOAT16_DIG
17792 #undef TOML_FLOAT16_LIMITS_SET
17793 #undef TOML_FLOAT16_MANT_DIG
17794 #undef TOML_FLOAT16_MAX_10_EXP
17795 #undef TOML_FLOAT16_MAX_EXP
17796 #undef TOML_FLOAT16_MIN_10_EXP
17797 #undef TOML_FLOAT16_MIN_EXP
17798 #undef TOML_GCC
17799 #undef TOML_GCC_LIKE
17800 #undef TOML_HAS_ATTR
17801 #undef TOML_HAS_BUILTIN
17802 #undef TOML_HAS_CHAR8
17803 #undef TOML_HAS_CPP_ATTR
17804 #undef TOML_HAS_CUSTOM_OPTIONAL_TYPE
17805 #undef TOML_HAS_FEATURE
17806 #undef TOML_HAS_INCLUDE
17807 #undef TOML_HAS_SSE2
17808 #undef TOML_HAS_SSE4_1
17809 #undef TOML_HIDDEN_CONSTRAINT
17810 #undef TOML_ICC
17811 #undef TOML_ICC_CL
17812 #undef TOML_IMPL_NAMESPACE_END
17813 #undef TOML_IMPL_NAMESPACE_START
17814 #undef TOML_IMPLEMENTATION
17815 #undef TOML_INCLUDE_WINDOWS_H
17816 #undef TOML_INLINE_GETTER
17817 #undef TOML_INT_CHARCONV
17818 #undef TOML_INT128
17819 #undef TOML_INTELLISENSE
17820 #undef TOML_INTERNAL_LINKAGE
17821 #undef TOML_LANG_AT_LEAST
17822 #undef TOML_LANG_EFFECTIVE_VERSION
17823 #undef TOML_LANG_HIGHER_THAN
17824 #undef TOML_LANG_UNRELEASED
17825 #undef TOML_LAUNDER
17826 #undef TOML_LIFETIME_HOOKS
17827 #undef TOML_LIKELY
17828 #undef TOML_LIKELY_CASE
17829 #undef TOML_LINUX
17830 #undef TOML_MAKE_FLAGS
17831 #undef TOML_MAKE_FLAGS_
17832 #undef TOML_MAKE_FLAGS_1
17833 #undef TOML_MAKE_FLAGS_2
17834 #undef TOML_MAKE_STRING
17835 #undef TOML_MAKE_STRING_1
17836 #undef TOML_MAKE_VERSION
17837 #undef TOML_MSVC
17838 #undef TOML_MSVC_LIKE
17839 #undef TOML_NAMESPACE
17840 #undef TOML_NEVER_INLINE
17841 #undef TOML_NODISCARD
17842 #undef TOML_NODISCARD_CTOR
17843 #undef TOML_NVCC
17844 #undef TOML_OPEN_ENUM
17845 #undef TOML_OPEN_FLAGS_ENUM
17846 #undef TOML_PARSER_TYPENAME
17847 #undef TOML_POP_WARNINGS
17848 #undef TOML_PRAGMA_CLANG
17849 #undef TOML_PRAGMA_CLANG_GE_10
17850 #undef TOML_PRAGMA_CLANG_GE_11
17851 #undef TOML_PRAGMA_CLANG_GE_8
17852 #undef TOML_PRAGMA_CLANG_GE_9
17853 #undef TOML_PRAGMA_GCC
17854 #undef TOML_PRAGMA_ICC
17855 #undef TOML_PRAGMA_MSVC
17856 #undef TOML_PURE
17857 #undef TOML_PURE_GETTER
17858 #undef TOML_PURE_INLINE_GETTER
17859 #undef TOML_PUSH_WARNINGS
17860 #undef TOML_REQUIRES
17861 #undef TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN
17862 #undef TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN_MESSAGE
17863 #undef TOML_SA_LIST_BEG
17864 #undef TOML_SA_LIST_END
17865 #undef TOML_SA_LIST_NEW
17866 #undef TOML_SA_LIST_NXT
17867 #undef TOML_SA_LIST_SEP
17868 #undef TOML_SA_NATIVE_VALUE_TYPE_LIST
17869 #undef TOML_SA_NEWLINE
17870 #undef TOML_SA_NODE_TYPE_LIST
17871 #undef TOML_SA_UNWRAPPED_NODE_TYPE_LIST
17872 #undef TOML_SA_VALUE_EXACT_FUNC_MESSAGE
17873 #undef TOML_SA_VALUE_FUNC_MESSAGE
17874 #undef TOML_SA_VALUE_MESSAGE_CONST_CHAR8
17875 #undef TOML_SA_VALUE_MESSAGE_U8STRING_VIEW
17876 #undef TOML_SA_VALUE_MESSAGE_WSTRING
17877 #undef TOML_SIMPLE_STATIC_ASSERT_MESSAGES
17878 #undef TOML_TRIVIAL_ABI
17879 #undef TOML_UINT128
17880 #undef TOML_UNIX
17881 #undef TOML_UNLIKELY
17882 #undef TOML_UNLIKELY_CASE
17883 #undef TOML_UNREACHABLE
17884 #undef TOML_UNUSED
17885 #undef TOML_WINDOWS
17886 #endif
17887
17888 #endif // TOMLPLUSPLUS_HPP