diff --git a/inc/parsing.h b/inc/parsing.h index ef680de..20fc553 100644 --- a/inc/parsing.h +++ b/inc/parsing.h @@ -10,11 +10,11 @@ namespace detail { enum class FormatType { s, c, b, B, d, o, x, X, a, A, e, E, f, F, g, G, p }; -template +template struct parse_result_t { bool is_valid = false; unsigned new_index = 0; - result_t result; + value_t value; }; struct fmt_node_t { @@ -211,6 +211,28 @@ constexpr unsigned count_braces() { return result; } +template +constexpr unsigned len_braces() { + unsigned result = 0; + + bool brace_open = false; + for (unsigned i = 0; i < s.size(); ++i) { + if (!brace_open) { + if (s[i] == '{') { + brace_open = true; + ++result; + } + } else { + ++result; + if (s[i] == '}') { + brace_open = false; + } + } + } + + return result; +} + template constexpr parse_result_t()>> parse_string() { parse_result_t()>> result; @@ -226,8 +248,8 @@ constexpr parse_result_t()>> parse_string() { if (!is_valid) { return {false, i, {}}; } - i = new_i; - result.result[format_node_pos++] = format_node; + i = new_i; + result.value[format_node_pos++] = format_node; } else if (s[i] == '}') { return {false, i, {}}; @@ -239,10 +261,16 @@ constexpr parse_result_t()>> parse_string() { template constexpr int get_output_len() { - constexpr auto result = parse_string(); + constexpr auto parse_result = parse_string(); + static_assert(parse_result.is_valid, "Syntax error in log string"); - // TODO - return result.is_valid; + unsigned result = s.size() - len_braces(); + + for (const auto& fmt_node : parse_result.value) { + result += fmt_node.length; + } + + return result; } @@ -250,13 +278,26 @@ constexpr int get_output_len() { template -constexpr std::array()> format(args_t...) { - constexpr int len = detail::get_output_len(); - static_assert(len > 0, "Syntax error in log string"); +std::array()> format(args_t...) { + std::array()> result; - std::cout << "Computed Length: " << len << std::endl; + constexpr auto parse_result = detail::parse_string(); + static_assert(parse_result.is_valid); - return {0}; + std::cout << "Total computed length: " << result.size() << std::endl; + + for (const auto& format_node : parse_result.value) { + std::cout << "\tFormat Node:" << std::endl; + std::cout << "\t\thas_zero_padding:\t" << format_node.has_zero_padding + << std::endl; + std::cout << "\t\tlength:\t\t\t\t" << format_node.length << std::endl; + std::cout << "\t\tprecision:\t\t\t" << format_node.precision + << std::endl; + std::cout << "\t\ttype:\t\t\t\t" << static_cast(format_node.type) + << std::endl; + } + + return result; } diff --git a/src/main.cpp b/src/main.cpp index f7d2a11..341fe54 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,23 +13,18 @@ private: int main() { - Uart uart; +/* Uart uart; Logger logger(uart); logger.log<"Test:{}">(1, 2, 3); std::cout << std::endl; - constexpr auto ast = detail::parse_string<"Test: {:16.8f} {:03.5} {:08.2}">(); - static_assert(ast.is_valid); + */ - for (const auto& format_node : ast.result) { - std::cout << "\tFormat Node:" << std::endl; - std::cout << "\t\thas_zero_padding:\t" << format_node.has_zero_padding << std::endl; - std::cout << "\t\tlength:\t\t\t\t" << format_node.length << std::endl; - std::cout << "\t\tprecision:\t\t\t" << format_node.precision << std::endl; - std::cout << "\t\ttype:\t\t\t\t" << static_cast(format_node.type) << std::endl; - } + constexpr detail::ConstString s{"Test: {:16.8f} {:03.5} {:08.2}"}; + + const auto formatted = format(3.4, "abc", 8.98754); /* constexpr detail::ConstString s = "{:8.14c}";