From 435a7c442f0fcf144fed6eb7c40f8f4324688b07 Mon Sep 17 00:00:00 2001 From: Andreas Tsouchlos Date: Fri, 19 Nov 2021 00:26:40 +0100 Subject: [PATCH] Refactored all parsing code to get the ConstString passed as a template parameter --- inc/parsing.h | 47 ++++++++++++++++++++++++++++++----------------- src/main.cpp | 2 +- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/inc/parsing.h b/inc/parsing.h index 1029091..c243963 100644 --- a/inc/parsing.h +++ b/inc/parsing.h @@ -17,6 +17,7 @@ namespace detail { enum class FormatType { s, c, b, B, d, o, x, X, a, A, e, E, f, F, g, G, p }; +// TODO: Maybe remove the length from here template struct parse_result_t { bool is_valid = false; @@ -79,8 +80,8 @@ constexpr bool is_digit(ConstString s, unsigned i) { */ -template -constexpr parse_result_t parse_number(ConstString s, unsigned i) { +template +constexpr parse_result_t parse_number(unsigned i) { int number = 0; while ((i < s.size()) && is_digit(s, i)) { @@ -92,8 +93,8 @@ constexpr parse_result_t parse_number(ConstString s, unsigned i) { return {true, i, 0, number}; } -template -constexpr parse_result_t parse_type(ConstString s, unsigned i) { + template +constexpr parse_result_t parse_type(unsigned i) { if (s[i] == 's') { // string ++i; return {true, i, 1, FormatType::s}; @@ -153,9 +154,8 @@ constexpr parse_result_t parse_type(ConstString s, unsigned i) { return {false, i, 0, FormatType::s}; } -template -constexpr parse_result_t parse_fmt_string(ConstString s, - unsigned i) { +template +constexpr parse_result_t parse_fmt_string(unsigned i) { fmt_string_result_t result; @@ -167,7 +167,7 @@ constexpr parse_result_t parse_fmt_string(ConstString s, } if (is_digit(s, i)) { - auto [is_valid, new_i, len, number] = parse_number(s, i); + auto [is_valid, new_i, len, number] = parse_number(i); if (!is_valid) return {false, i, 0, result}; i = new_i; @@ -176,7 +176,7 @@ constexpr parse_result_t parse_fmt_string(ConstString s, if (s[i] == '.') { ++i; - auto [is_valid, new_i, len, number] = parse_number(s, i); + auto [is_valid, new_i, len, number] = parse_number(i); if (!is_valid) return {false, i, 0, result}; i = new_i; @@ -184,7 +184,7 @@ constexpr parse_result_t parse_fmt_string(ConstString s, } if (s[i] != '}') { - auto [is_valid, new_i, len, type] = parse_type(s, i); + auto [is_valid, new_i, len, type] = parse_type(i); if (!is_valid) return {false, i, 0, result}; i = new_i; @@ -194,8 +194,21 @@ constexpr parse_result_t parse_fmt_string(ConstString s, return {true, i, result_extra_len, result}; } -template -constexpr std::pair parse_braces(ConstString s, unsigned i) { +template +constexpr unsigned count_braces(unsigned i) { + unsigned result = 0; + + while (i < s.size()) { + if (s[i] == '{') + ++result; + ++i; + } + + return result; +} + +template +constexpr std::pair parse_braces(unsigned i) { int result_extra_len = 0; if (s[i] == '}') { @@ -204,7 +217,7 @@ constexpr std::pair parse_braces(ConstString s, unsigned i) { } else if (s[i] == ':') { ++i; - auto [is_valid, new_i, len, format_node] = parse_fmt_string(s, i); + auto [is_valid, new_i, len, format_node] = parse_fmt_string(i); if (!is_valid) return {i, -1}; i = new_i; @@ -219,15 +232,15 @@ constexpr std::pair parse_braces(ConstString s, unsigned i) { return {i, -1}; } -template -constexpr int get_output_len(ConstString s) { +template +constexpr int get_output_len() { int result_extra_len = 0; for (unsigned i = 0; i < s.size(); ++i) { if (s[i] == '{') { ++i; - auto [new_i, extra_len] = parse_braces(s, i); + auto [new_i, extra_len] = parse_braces(i); if (extra_len < 0) return -1; i = new_i; @@ -247,7 +260,7 @@ constexpr int get_output_len(ConstString s) { template const std::array format(args_t...) { - constexpr int len = get_output_len(s); + constexpr int len = detail::get_output_len(); static_assert(len > 0, "Syntax error in log string"); std::cout << "Computed Length: " << len << std::endl; diff --git a/src/main.cpp b/src/main.cpp index 7dfdc2a..74ac859 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,7 +20,7 @@ int main() { constexpr detail::ConstString s = "{:8.14c}"; - constexpr auto result = detail::parse_fmt_string(s, 2); + constexpr auto result = detail::parse_fmt_string(2); std::cout << "Is valid: " << result.is_valid << std::endl << std::endl;