From 65f6ee5f2e5a6bd88a748b2d28ff7a05ba991f77 Mon Sep 17 00:00:00 2001 From: Dennis Bonke Date: Mon, 10 Jun 2024 13:55:52 +0200 Subject: [PATCH] formatting: Add support for dropping trailing zeroes printf: Add support for %g and %G --- include/frg/formatting.hpp | 4 ++-- include/frg/printf.hpp | 26 +++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/include/frg/formatting.hpp b/include/frg/formatting.hpp index eb284ff..10af841 100644 --- a/include/frg/formatting.hpp +++ b/include/frg/formatting.hpp @@ -224,7 +224,7 @@ namespace _fmt_basics { template void print_float(S &sink, T number, int width = 0, int precision = 6, char padding = ' ', bool left_justify = false, bool use_capitals = false, - bool group_thousands = false, locale_options locale_opts = {}) { + bool group_thousands = false, bool drop_trailing_decimal_zeroes = false, locale_options locale_opts = {}) { (void)group_thousands; bool has_sign = false; @@ -307,7 +307,7 @@ namespace _fmt_basics { i++; } - while (i < precision) { + while (i < precision && !drop_trailing_decimal_zeroes) { sink.append('0'); i++; } diff --git a/include/frg/printf.hpp b/include/frg/printf.hpp index 1d82b82..d76c6e5 100644 --- a/include/frg/printf.hpp +++ b/include/frg/printf.hpp @@ -471,6 +471,7 @@ void do_printf_floats(S &sink, char t, format_options opts, printf_size_mod szmod, va_struct *vsp, locale_options locale_opts = {}) { int precision_or_default = opts.precision.has_value() ? *opts.precision : 6; bool use_capitals = true; + bool drop_trailing_decimal_zeroes = false; switch(t) { case 'f': use_capitals = false; @@ -481,7 +482,7 @@ void do_printf_floats(S &sink, char t, format_options opts, _fmt_basics::print_float(sink, pop_arg(vsp, &opts), opts.minimum_width, precision_or_default, opts.fill_zeros ? '0' : ' ', opts.left_justify, use_capitals, - opts.group_thousands, locale_opts); + opts.group_thousands, drop_trailing_decimal_zeroes, locale_opts); break; } #endif @@ -489,13 +490,32 @@ void do_printf_floats(S &sink, char t, format_options opts, _fmt_basics::print_float(sink, pop_arg(vsp, &opts), opts.minimum_width, precision_or_default, opts.fill_zeros ? '0' : ' ', opts.left_justify, use_capitals, - opts.group_thousands, locale_opts); + opts.group_thousands, drop_trailing_decimal_zeroes, locale_opts); break; case 'g': + use_capitals = false; + drop_trailing_decimal_zeroes = true; + [[fallthrough]]; case 'G': +#ifndef FRG_DONT_USE_LONG_DOUBLE + if (szmod == printf_size_mod::longdouble_size) { + _fmt_basics::print_float(sink, pop_arg(vsp, &opts), + opts.minimum_width, precision_or_default, + opts.fill_zeros ? '0' : ' ', opts.left_justify, use_capitals, + opts.group_thousands, drop_trailing_decimal_zeroes, locale_opts); + break; + } +#endif + FRG_ASSERT(szmod == printf_size_mod::default_size || szmod == printf_size_mod::long_size); + _fmt_basics::print_float(sink, pop_arg(vsp, &opts), + opts.minimum_width, precision_or_default, + opts.fill_zeros ? '0' : ' ', opts.left_justify, use_capitals, + opts.group_thousands, drop_trailing_decimal_zeroes, locale_opts); + break; case 'e': case 'E': - sink.append("%f"); + sink.append("%"); + sink.append(t); break; default: FRG_ASSERT(!"Unexpected printf terminal");