From ba86cc40a5f741f2655d653d41da55fae8e7a3f6 Mon Sep 17 00:00:00 2001 From: Andreas Tsouchlos Date: Mon, 22 Nov 2021 12:42:36 +0100 Subject: [PATCH] Fixed issues with minus on integers --- inc/format.h | 18 +++++++++++++++--- inc/utility.h | 2 -- src/main.cpp | 10 +--------- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/inc/format.h b/inc/format.h index c76463e..4a66415 100644 --- a/inc/format.h +++ b/inc/format.h @@ -33,21 +33,32 @@ constexpr int get_output_len() { } // TODO: See if this is possible with +// TODO: Steal some code from fmtlib template constexpr std::array format_arg(arg_t arg) { check_fmt_params(); auto result = get_init_array(); - for (unsigned i = 1; (i <= result.size()) && arg > 0; ++i) { - result[result.size() - i] = arg % 10 + 48; - arg = arg / 10; + unsigned offset = 0; + + // TODO: Does making this branchless make it more efficient? + if (arg < 0) { + result[0] = '-'; + arg = -arg; + ++offset; + } + + for (int i = result.size() - 1; (i >= offset) && (arg > 0); --i) { + result[i] = arg % 10 + 48; + arg = arg / 10; } return result; } // TODO: See if this is possible with +// TODO: Steal some code from fmtlib template constexpr std::array format_arg(arg_t arg) { check_fmt_params(); @@ -76,6 +87,7 @@ constexpr std::array format_arg(arg_t arg) { return result; } +// TODO: Steal some code from fmtlib template constexpr std::array format_arg(const char* arg) { check_fmt_params(); diff --git a/inc/utility.h b/inc/utility.h index 0d8e20b..b43eba5 100644 --- a/inc/utility.h +++ b/inc/utility.h @@ -4,8 +4,6 @@ #include -#include "parse_types.h" - namespace detail { diff --git a/src/main.cpp b/src/main.cpp index 4c5e307..65559b6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,16 +3,8 @@ #include -class Uart { -public: - void write(char c) { - std::cout << c; - } -}; - - int main() { - constexpr auto formatted = format<"Test: {:012.5} {} {:034}">(142.4334, "abc", 1234); + constexpr auto formatted = format<"Test: {:012.5} {} {:8}">(142.4334, "abc", -1234); for (const auto& c : formatted) std::cout << c;