From 5ddf9ee1bd193da3f7cc462088b812998f190141 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 20 Jun 2020 07:12:50 -0700 Subject: [PATCH] Streamline default FP formatting --- include/fmt/format.h | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 2f13c8ec..e1b27a3b 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1611,7 +1611,7 @@ OutputIt write_nonfinite(OutputIt out, bool isinf, template ::value)> -OutputIt write(OutputIt out, T value, basic_format_specs specs = {}, +OutputIt write(OutputIt out, T value, basic_format_specs specs, locale_ref loc = {}) { if (const_check(!is_supported_floating_point(value))) return out; float_specs fspecs = parse_float_type_spec(specs); @@ -1658,6 +1658,31 @@ OutputIt write(OutputIt out, T value, basic_format_specs specs = {}, return write_padded(out, specs, w.size(), w); } +template ::value)> +OutputIt write(OutputIt out, T value) { + if (const_check(!is_supported_floating_point(value))) return out; + auto fspecs = float_specs(); + if (std::signbit(value)) { // value < 0 is false for NaN so use signbit. + fspecs.sign = sign::minus; + value = -value; + } + + auto specs = basic_format_specs(); + if (!std::isfinite(value)) + return write_nonfinite(out, std::isinf(value), specs, fspecs); + + memory_buffer buffer; + int precision = -1; + if (const_check(std::is_same())) fspecs.binary32 = true; + fspecs.use_grisu = use_grisu(); + int exp = format_float(promote_float(value), precision, fspecs, buffer); + fspecs.precision = precision; + float_writer w(buffer.data(), static_cast(buffer.size()), exp, + fspecs, static_cast('.')); + return base_iterator(out, w(reserve(out, w.size()))); +} + template OutputIt write_char(OutputIt out, Char value, const basic_format_specs& specs) {