9#if !TOML_IMPLEMENTATION
10#error This is an implementation-only header.
23#if TOML_INT_CHARCONV || TOML_FLOAT_CHARCONV
26#if !TOML_INT_CHARCONV || !TOML_FLOAT_CHARCONV
37 inline constexpr size_t charconv_buffer_length = 0;
71 std::ostream & stream, T val,
value_flags format = {},
size_t min_digits = 0) {
73 if (!min_digits) min_digits = 1;
75 for (
size_t i = 0; i < min_digits; i++) stream.put(
'0');
80 static constexpr auto value_flags_mask = value_flags::format_as_binary |
81 value_flags::format_as_octal |
82 value_flags::format_as_hexadecimal;
83 format &= value_flags_mask;
86 if (format != value_flags::none && val > T{}) {
88 case value_flags::format_as_binary:
91 case value_flags::format_as_octal:
94 case value_flags::format_as_hexadecimal:
104 char buf[(
sizeof(T) * CHAR_BIT)];
105 const auto res = std::to_chars(buf, buf +
sizeof(buf), val, base);
106 const auto len =
static_cast<size_t>(res.ptr - buf);
107 for (
size_t i = len; i < min_digits; i++) stream.put(
'0');
109 for (
size_t i = 0; i < len; i++)
110 if (buf[i] >=
'a') buf[i] -= 32;
112 impl::print_to_stream(stream, buf, len);
116 using unsigned_type =
117 std::conditional_t<(
sizeof(T) >
sizeof(
unsigned)), std::make_unsigned_t<T>,
unsigned>;
119 std::conditional_t<std::is_signed_v<T>, std::make_signed_t<unsigned_type>, unsigned_type>;
122 const auto len =
sizeof(T) * CHAR_BIT;
123 for (
size_t i = len; i < min_digits; i++) stream.put(
'0');
125 bool found_one =
false;
126 const auto v =
static_cast<unsigned_type
>(val);
127 unsigned_type mask = unsigned_type{1} << (len - 1u);
128 for (
size_t i = 0; i < len; i++) {
132 }
else if (found_one)
137 std::ostringstream ss;
138 ss.imbue(std::locale::classic());
139 ss << std::uppercase << std::setbase(base);
140 if (min_digits) ss << std::setfill('0') << std::setw(static_cast<int>(min_digits));
141 ss << static_cast<cast_type>(val);
142 const auto str = std::move(ss).str();
143 impl::print_to_stream(stream, str);
149 template <
typename T>
151 std::ostream & stream, T val,
value_flags format, [[maybe_unused]]
bool relaxed_precision) {
152 switch (impl::fpclassify(val)) {
153 case impl::fp_class::neg_inf:
154 impl::print_to_stream(stream,
"-inf"sv);
157 case impl::fp_class::pos_inf:
158 impl::print_to_stream(stream,
"inf"sv);
161 case impl::fp_class::nan:
162 impl::print_to_stream(stream,
"nan"sv);
165 case impl::fp_class::ok: {
166 static constexpr auto needs_decimal_point = [](
auto&& s)
noexcept {
168 if (c ==
'.' || c ==
'E' || c ==
'e')
return false;
172#if TOML_FLOAT_CHARCONV
174 const auto hex = !!(format & value_flags::format_as_hexadecimal);
175 char buf[charconv_buffer_length<T>];
176 auto res = hex ? std::to_chars(buf, buf +
sizeof(buf), val, std::chars_format::hex)
177 : std::to_chars(buf, buf +
sizeof(buf), val);
178 auto str = std::string_view{buf,
static_cast<size_t>(res.ptr - buf)};
180 char buf2[charconv_buffer_length<T>];
181 if (!hex && relaxed_precision) {
182 res = std::to_chars(buf2, buf2 +
sizeof(buf2), val, std::chars_format::general, 6);
183 const auto str2 = std::string_view{buf2,
static_cast<size_t>(res.ptr - buf2)};
184 if (str2.length() < str.length()) str = str2;
187 impl::print_to_stream(stream, str);
188 if (!hex && needs_decimal_point(str)) toml::impl::print_to_stream(stream,
".0"sv);
192 std::ostringstream ss;
193 ss.imbue(std::locale::classic());
194 if (!relaxed_precision) ss.precision(std::numeric_limits<T>::max_digits10);
195 if (!!(format & value_flags::format_as_hexadecimal)) ss << std::hexfloat;
197 const auto str = std::move(ss).str();
198 impl::print_to_stream(stream, str);
199 if (!(format & value_flags::format_as_hexadecimal) && needs_decimal_point(str))
200 impl::print_to_stream(stream,
".0"sv);
216 stream.write(val,
static_cast<std::streamsize
>(len));
221 stream.write(val.data(),
static_cast<std::streamsize
>(val.length()));
226 stream.write(val.data(),
static_cast<std::streamsize
>(val.length()));
237 TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
243 TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
249 TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
255 TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
261 TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
267 TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
273 TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
279 TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
285 TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
291 TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
296 bool relaxed_precision) {
297 TOML_ANON_NAMESPACE::print_floating_point_to_stream(stream, val, format, relaxed_precision);
302 bool relaxed_precision) {
303 TOML_ANON_NAMESPACE::print_floating_point_to_stream(stream, val, format, relaxed_precision);
327 if (val.nanosecond && val.nanosecond <= 999999999u) {
329 auto ns = val.nanosecond;
331 while (ns % 10u == 0u) {
346 auto mins =
static_cast<int>(val.minutes);
352 const auto hours = mins / 60;
388#if TOML_ENABLE_FORMATTERS
392 stream << toml_formatter{arr};
397 stream << toml_formatter{tbl};
402 stream << toml_formatter{val};
407 stream << toml_formatter{val};
412 stream << toml_formatter{val};
417 stream << toml_formatter{val};
422 stream << toml_formatter{val};
427 stream << toml_formatter{val};
432 stream << toml_formatter{val};
A TOML array.
Definition array.hpp:285
A TOML table.
Definition table.hpp:220
enum TOML_OPEN_FLAGS_ENUM value_flags
Metadata associated with TOML values.
Definition forward_declarations.hpp:272
#define TOML_CALLCONV
Calling convention to apply to exported free/static functions. \detail Not defined by default (let th...
Definition preprocessor.hpp:1134
#define TOML_INTERNAL_LINKAGE
Definition preprocessor.hpp:1340
#define TOML_EXTERNAL_LINKAGE
Definition preprocessor.hpp:1339
#define TOML_UNREACHABLE
Definition preprocessor.hpp:515
#define TOML_ATTR(...)
Definition preprocessor.hpp:316
#define TOML_IMPL_NAMESPACE_END
Definition preprocessor.hpp:1334
TOML_EXPORTED_FREE_FUNCTION void TOML_CALLCONV print_to_stream(std::ostream &, std::string_view)
constexpr size_t charconv_buffer_length< int16_t >
Definition print_to_stream.inl:43
constexpr size_t charconv_buffer_length< uint8_t >
Definition print_to_stream.inl:52
constexpr size_t charconv_buffer_length< int64_t >
Definition print_to_stream.inl:49
TOML_ANON_NAMESPACE_START
Definition print_to_stream.inl:35
TOML_ENABLE_WARNINGS
Definition print_to_stream.inl:32
TOML_DISABLE_WARNINGS
Definition print_to_stream.inl:21
TOML_INTERNAL_LINKAGE void print_floating_point_to_stream(std::ostream &stream, T val, value_flags format, bool relaxed_precision)
Definition print_to_stream.inl:150
constexpr size_t charconv_buffer_length< uint16_t >
Definition print_to_stream.inl:55
constexpr size_t charconv_buffer_length< int8_t >
Definition print_to_stream.inl:40
constexpr size_t charconv_buffer_length< uint32_t >
Definition print_to_stream.inl:58
constexpr size_t charconv_buffer_length< int32_t >
Definition print_to_stream.inl:46
constexpr size_t charconv_buffer_length< uint64_t >
Definition print_to_stream.inl:61
constexpr size_t charconv_buffer_length< double >
Definition print_to_stream.inl:67
TOML_ANON_NAMESPACE_END
Definition print_to_stream.inl:210
constexpr size_t charconv_buffer_length< float >
Definition print_to_stream.inl:64
TOML_INTERNAL_LINKAGE void print_integer_to_stream(std::ostream &stream, T val, value_flags format={}, size_t min_digits=0)
Definition print_to_stream.inl:70
TOML_IMPL_NAMESPACE_START
Definition print_to_stream.inl:212
A source document line-and-column pair.
Definition source_region.hpp:43
source_index line
The line number.
Definition source_region.hpp:46
source_index column
The column number.
Definition source_region.hpp:50
A source document region.
Definition source_region.hpp:167
source_path_ptr path
The path to the corresponding source document.
Definition source_region.hpp:177
source_position begin
The beginning of the region (inclusive).
Definition source_region.hpp:169