diff --git a/inc/format.h b/inc/format.h index 839e3a4..e3c3403 100644 --- a/inc/format.h +++ b/inc/format.h @@ -49,7 +49,7 @@ constexpr std::array get_init_array() { } -// TODO: See if this is possible with charconv +// TODO: See if this is possible with template constexpr std::array format_arg(arg_t arg) { check_fmt_params(); @@ -65,33 +65,31 @@ constexpr std::array format_arg(arg_t arg) { return result; } -// TODO: See if this is possible with charconv +// TODO: See if this is possible with template constexpr std::array format_arg(arg_t arg) { check_fmt_params(); - constexpr unsigned len_before_point = - fmt_node.length - fmt_node.precision - 1; - constexpr unsigned multiplier = const_pow(10, fmt_node.precision); + constexpr unsigned point_index = fmt_node.length - fmt_node.precision - 1; + constexpr unsigned multiplier = const_pow(10, fmt_node.precision); std::array result = get_init_array(); - result[len_before_point] = '.'; + result[point_index] = '.'; arg = arg * multiplier; - for (unsigned i = result.size() - 1; i > len_before_point; --i) { + for (unsigned i = result.size() - 1; (i > point_index) && (arg >= 1); --i) { result[i] = static_cast(arg) % 10 + 48; arg = arg / 10; } - 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 = point_index - 1; (i >= 0) && (arg >= 1); --i) { + result[i] = static_cast(arg) % 10 + 48; + arg = arg / 10; } - return result; } diff --git a/inc/parse.h b/inc/parse.h index 8107a28..c4ab4e3 100644 --- a/inc/parse.h +++ b/inc/parse.h @@ -251,9 +251,8 @@ constexpr parse_result_t()>> parse_string() { ++i; auto [is_valid, new_i, format_node] = parse_braces(i); - if (!is_valid) { + if (!is_valid) return {false, i, {}}; - } i = new_i; result.value[ast_position++].set_node(format_node); diff --git a/src/main.cpp b/src/main.cpp index 15a823b..4c5e307 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,7 +12,7 @@ public: int main() { - constexpr auto formatted = format<"Test: {} {} {:0}">(142.4334, "abc", 1234); + constexpr auto formatted = format<"Test: {:012.5} {} {:034}">(142.4334, "abc", 1234); for (const auto& c : formatted) std::cout << c; diff --git a/test/src/format.cpp b/test/src/format.cpp index 882b48f..800ec11 100644 --- a/test/src/format.cpp +++ b/test/src/format.cpp @@ -6,9 +6,9 @@ using namespace detail; TEST(Format, positive_int) { - constexpr std::array control1 = {'0', '0', '0', '2', - '2', '2', '2', '2'}; - constexpr std::array formatted1 = format<"{:08}">(22222); + constexpr std::array control1 = {'0', '0', '0', '0', + '0', '0', '0', '2'}; + constexpr std::array formatted1 = format<"{:08}">(2); constexpr std::array control2 = {' ', ' ', ' ', '2', '2', '2', '2', '2'};