From ad737f1c7300a3cf8b945e96397b662ea12ed06b Mon Sep 17 00:00:00 2001 From: Andreas Tsouchlos Date: Sun, 21 Nov 2021 20:01:00 +0100 Subject: [PATCH] Started implementing zero padding --- inc/Logger.h | 2 ++ inc/format.h | 52 +++++++++++++++++++++++++++++----------------------- src/main.cpp | 2 +- 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/inc/Logger.h b/inc/Logger.h index 0347564..bbe952e 100644 --- a/inc/Logger.h +++ b/inc/Logger.h @@ -41,6 +41,8 @@ public: for (const auto& c : formatted_msg) { m_output_policy.write(c); } + + m_output_policy.write('\n'); } private: diff --git a/inc/format.h b/inc/format.h index f8a0d05..74fb01f 100644 --- a/inc/format.h +++ b/inc/format.h @@ -8,9 +8,10 @@ namespace detail { -template +template constexpr void check_fmt_params() { - static_assert(fmt_node.length > fmt_node.precision + 1, "Insufficient length for desired precision"); + static_assert(fmt_node.length > fmt_node.precision + 1, + "Insufficient length for desired precision"); } @@ -32,14 +33,25 @@ constexpr int get_output_len() { } +template +constexpr std::array get_init_array() { + if constexpr (zeroed) { + return {'0'}; + } else { + return {' '}; + } +} + + +// TODO: See if this is possible with charconv template constexpr std::array format_arg(arg_t arg) { check_fmt_params(); - std::array result; + std::array result = + get_init_array(); - // TODO: See if this is possible with charconv - for (unsigned i = 1; i <= result.size(); ++i) { + for (unsigned i = 1; (i <= result.size()) && arg > 0; ++i) { result[result.size() - i] = arg % 10 + 48; arg = arg / 10; } @@ -47,38 +59,32 @@ constexpr std::array format_arg(arg_t arg) { return result; } +// TODO: See if this is possible with charconv template constexpr std::array format_arg(arg_t arg) { check_fmt_params(); - std::array result; - constexpr unsigned len_before_point = fmt_node.length - fmt_node.precision - 1; - - result[result.size() - fmt_node.precision - 1] = '.'; - - // TODO: See if this is possible with charconv - constexpr unsigned multiplier = const_pow(10, fmt_node.precision); + + std::array result = + get_init_array(); + result[len_before_point] = '.'; + arg = arg * multiplier; - for (unsigned i = result.size()-1; i >= result.size() - fmt_node.precision; --i) { + + for (unsigned i = result.size() - 1; i > len_before_point; --i) { result[i] = static_cast(arg) % 10 + 48; - arg = arg / 10; + arg = arg / 10; } - for (unsigned i = fmt_node.precision + 2; i <= result.size(); ++i) { + for (unsigned i = fmt_node.precision + 2; (i <= result.size()) && (arg >= 1); ++i) { result[result.size() - i] = static_cast(arg) % 10 + 48; arg = arg / 10; } - /* - for (unsigned i = fmt_node.precision + 1; i <= result.size(); ++i) { - result[result.size() - i] = static_cast(arg) % 10 + 48; - arg = arg / 10; - } - */ return result; } @@ -93,19 +99,19 @@ constexpr std::array format_arg(const char* arg) { if (*arg != '\0') c = *(arg++); else - c = '_'; + c = ' '; } return result; } + template constexpr char_array_t format_args(char_array_t result) { return result; } - template constexpr char_array_t format_args(char_array_t result, first_arg_t first_arg, diff --git a/src/main.cpp b/src/main.cpp index 8df2b1c..15a823b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,7 +12,7 @@ public: int main() { - const auto formatted = format<"Test: {:8.3} {:6.3} {:05.2}">(1432.4334, "abc", 1234); + constexpr auto formatted = format<"Test: {} {} {:0}">(142.4334, "abc", 1234); for (const auto& c : formatted) std::cout << c;