From 1338888e0ba6942669ac0638f6992c6a28932c1c Mon Sep 17 00:00:00 2001 From: Andreas Tsouchlos Date: Sun, 13 Feb 2022 21:52:52 +0100 Subject: [PATCH] Now deciding at compile time how to format minus sign --- const_fmt/format.h | 5 ++--- const_fmt/format_impl.h | 38 ++++++++++++++++++++++---------------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/const_fmt/format.h b/const_fmt/format.h index 9d42efd..f84572c 100644 --- a/const_fmt/format.h +++ b/const_fmt/format.h @@ -42,12 +42,12 @@ constexpr inline void check_fmt_params() { template constexpr inline void format_arg(char* dest, arg_t arg) { - const_fmt_detail::format_int(dest, arg, fmt_data); + const_fmt_detail::format_int(dest, arg); }; template constexpr inline void format_arg(char* dest, arg_t arg){ - // const_fmt_detail::format_float(dest, arg, fmt_data); +// const_fmt_detail::format_float(dest, arg); }; // TODO: Error handling @@ -93,7 +93,6 @@ consteval inline auto get_preproc_string() { } else { for (int j = 0; j < ast_node.get_node().length; ++j) result[i++] = ast_node.get_node().has_zero_padding ? '0' : ' '; - //i += ast_node.get_node().length; } } diff --git a/const_fmt/format_impl.h b/const_fmt/format_impl.h index 2cd6cc5..412e2db 100644 --- a/const_fmt/format_impl.h +++ b/const_fmt/format_impl.h @@ -87,8 +87,9 @@ constexpr inline void copy2(char* dst, const char* src) { } template -constexpr inline void format_decimal(char* out, uint_t value, int size) { - if (count_digits(value) > size) return; +constexpr inline void format_decimal(char* out, uint_t value, int n_digits, + int size) { + if (n_digits > size) return; out += size; while (value >= 100) { @@ -114,21 +115,27 @@ constexpr inline void format_decimal(char* out, uint_t value, int size) { */ -template -constexpr inline void format_int(char* out, uint_t value, fmt_data_t fmt_node) { - format_decimal(out, value, fmt_node.length); +template +constexpr inline void format_int(char* out, uint_t value) { + format_decimal(out, value, count_digits(value), t_fmt_node.length); } -template -constexpr inline void format_int(char* out, uint_t value, fmt_data_t fmt_node) { - auto abs_value = static_cast(value); +template +constexpr inline void format_int(char* out, int_t value) { + uint64_t abs_value = static_cast(value); const bool negative = value < 0; - if (negative) abs_value = 0 - abs_value; - format_decimal(out + 1 * (negative), abs_value, - fmt_node.length - 1 * (negative)); - if (negative) *out = '-'; + const int n_digits = count_digits(abs_value); + + format_decimal(out + 1 * (negative), abs_value, n_digits, + t_fmt_node.length - 1 * (negative)); + + if constexpr (t_fmt_node.has_zero_padding) { + if (negative) *(out) = '-'; + } else { + if (negative) *(out + t_fmt_node.length - n_digits - 1) = '-'; + } } @@ -139,12 +146,11 @@ constexpr inline void format_int(char* out, uint_t value, fmt_data_t fmt_node) { */ -template -constexpr inline void format_float(char* out, float_t value, - fmt_data_t fmt_data) { +template +constexpr inline void format_float(char* out, float_t value) { *(out) = 'f'; - *(out + fmt_data.length - fmt_data.precision - 1) = '.'; + *(out + t_fmt_node.length - t_fmt_node.precision - 1) = '.'; }