From e028e43b6ae6d6a5ef025cb164d1519344ce95c1 Mon Sep 17 00:00:00 2001 From: Andreas Tsouchlos Date: Thu, 18 Nov 2021 20:56:02 +0100 Subject: [PATCH] Split logger.h into 3 files --- inc/Logger.h | 264 +------------------------------------------------- inc/parsing.h | 200 ++++++++++++++++++++++++++++++++++++++ inc/utility.h | 28 ++++++ 3 files changed, 233 insertions(+), 259 deletions(-) create mode 100644 inc/parsing.h create mode 100644 inc/utility.h diff --git a/inc/Logger.h b/inc/Logger.h index 0466643..c4e2d4f 100644 --- a/inc/Logger.h +++ b/inc/Logger.h @@ -1,267 +1,13 @@ -#ifndef CPP_SANDBOX_LOGGER_H -#define CPP_SANDBOX_LOGGER_H +#ifndef LOGGER_LOGGER_H +#define LOGGER_LOGGER_H #include #include #include - -/* - * - * Const string - * - */ - - -template -class ConstString { -public: - constexpr ConstString(const char (&content)[N]) noexcept { - std::copy(std::begin(content), std::end(content), - std::begin(m_content)); - } - - constexpr char operator[](std::size_t index) const noexcept { - return m_content[index]; - } - - constexpr std::size_t size() const noexcept { - return N-1; - } - - std::array m_content; -}; - -template -ConstString(const char (&)[N]) -> ConstString; - - -/* - * - * Parsing - * - */ - - -//template -//constexpr bool has_char() { -// bool result = false; -// -// for (unsigned i = 0; i < s.size(); ++i) { -// if (s[i] == c) -// result = true; -// } -// -// return result; -//} -// -//template -//constexpr std::size_t get_char_index() { -// static_assert(has_char(), -// "ConstString does not contain the specified character"); -// -// std::size_t result = s.size() + 1; -// -// for (unsigned i = 0; i < s.size(); ++i) { -// if (s[i] == c) { -// result = i; -// break; -// } -// } -// -// return result; -//} - - -template -constexpr bool is_digit(ConstString s, unsigned i) { - return (s[i] > 47) && (s[i] < 58); -} - -template -constexpr std::pair parse_number(ConstString s, unsigned i) { - while ((i < s.size()) && is_digit(s, i)) { - ++i; - } - - return {i, 0}; -} - -template -constexpr std::pair parse_type(ConstString s, unsigned i) { - if (s[i] == 's') { // string - ++i; - return {i, 0}; - } else if (s[i] == 'c') { // char - ++i; - return {i, 0}; - } else if (s[i] == 'b') { // int - ++i; - return {i, 0}; - } else if (s[i] == 'B') { - ++i; - return {i, 0}; - } else if (s[i] == 'c') { - ++i; - return {i, 0}; - } else if (s[i] == 'd') { - ++i; - return {i, 0}; - } else if (s[i] == '0') { - ++i; - return {i, 0}; - } else if (s[i] == 'x') { - ++i; - return {i, 0}; - } else if (s[i] == 'X') { - ++i; - return {i, 0}; - } else if (s[i] == 'a') { // float - ++i; - return {i, 0}; - } else if (s[i] == 'A') { - ++i; - return {i, 0}; - } else if (s[i] == 'e') { - ++i; - return {i, 0}; - } else if (s[i] == 'E') { - ++i; - return {i, 0}; - } else if (s[i] == 'f') { - ++i; - return {i, 0}; - } else if (s[i] == 'F') { - ++i; - return {i, 0}; - } else if (s[i] == 'g') { - ++i; - return {i, 0}; - } else if (s[i] == 'G') { - ++i; - return {i, 0}; - } else if (s[i] == 'p') { // pointer - ++i; - return {i, 0}; - } - - return {i, -1}; -} - -template -constexpr std::pair parse_fmt_string(ConstString s, unsigned i) { - int result_extra_len = 0; - - if (s[i] == '0') - ++i; - - if (is_digit(s, i)) { - auto [new_i, extra_len] = parse_number(s, i); - if (extra_len < 0) - return {i, -1}; - i = new_i; - result_extra_len += extra_len; - } - - if (s[i] == '.') { - ++i; - auto [new_i, extra_len] = parse_number(s, i); - if (extra_len < 0) - return {i, -1}; - i = new_i; - result_extra_len += extra_len; - } - - if (s[i] != '}') { - auto [new_i, extra_len] = parse_type(s, i); - if (extra_len < 0) - return {i, -1}; - i = new_i; - result_extra_len += extra_len; - - } - - return {i, result_extra_len}; -} - -template -constexpr std::pair parse_braces(ConstString s, unsigned i) { - int result_extra_len = 0; - - if (s[i] == '}') { - ++i; - return {i, result_extra_len}; - } else if (s[i] == ':') { - ++i; - - auto [new_i, extra_len] = parse_fmt_string(s, i); - if (extra_len < 0) - return {i, -1}; - i = new_i; - result_extra_len += extra_len; - - if (s[i] == '}') { - ++i; - return {i, result_extra_len}; - } - } - - return {i, -1}; -} - -template -constexpr int get_output_len(ConstString s) { - 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); - if (extra_len < 0) - return -1; - i = new_i; - result_extra_len += extra_len; - - } else if (s[i] == '}') { - return -1; - } - } - - return (result_extra_len + s.size()); -} - -// clang-format off - -/* - * - * fmtlib grammar: - * replacement_field ::= "{" [arg_id] [":" (format_spec | chrono_format_spec)] "}" - * arg_id ::= integer | identifier - * integer ::= digit+ - * digit ::= "0"..."9" - * identifier ::= id_start id_continue* - * id_start ::= "a"..."z" | "A"..."Z" | "_" - * id_continue ::= id_start | digit - * - * format_spec ::= [[fill]align][sign]["#"]["0"][width]["." precision]["L"][type] - * fill ::= - * align ::= "<" | ">" | "^" - * sign ::= "+" | "-" | " " - * width ::= integer | "{" [arg_id] "}" - * precision ::= integer | "{" [arg_id] "}" - * type ::= "a" | "A" | "b" | "B" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | - * "o" | "p" | "s" | "x" | "X" - * - * grammar: - * string ::= { braces | } - * braces ::= "{" [":" fmt_string] "}" - * fmt_string ::= ["0"][width]["." precision][type] - * - */ - -// clang-format on +#include +#include /* @@ -299,4 +45,4 @@ private: }; -#endif // CPP_SANDBOX_LOGGER_H +#endif // LOGGER_LOGGER_H diff --git a/inc/parsing.h b/inc/parsing.h new file mode 100644 index 0000000..94e26b2 --- /dev/null +++ b/inc/parsing.h @@ -0,0 +1,200 @@ +// +// Created by andreas on 11/18/21. +// + +#ifndef LOGGER_PARSING_H +#define LOGGER_PARSING_H + + +// clang-format off + +/* + * + * fmtlib grammar: + * replacement_field ::= "{" [arg_id] [":" (format_spec | chrono_format_spec)] "}" + * arg_id ::= integer | identifier + * integer ::= digit+ + * digit ::= "0"..."9" + * identifier ::= id_start id_continue* + * id_start ::= "a"..."z" | "A"..."Z" | "_" + * id_continue ::= id_start | digit + * + * format_spec ::= [[fill]align][sign]["#"]["0"][width]["." precision]["L"][type] + * fill ::= + * align ::= "<" | ">" | "^" + * sign ::= "+" | "-" | " " + * width ::= integer | "{" [arg_id] "}" + * precision ::= integer | "{" [arg_id] "}" + * type ::= "a" | "A" | "b" | "B" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | + * "o" | "p" | "s" | "x" | "X" + * + * grammar: + * string ::= { braces | } + * braces ::= "{" [":" fmt_string] "}" + * fmt_string ::= ["0"][width]["." precision][type] + * + */ + +// clang-format on + + +template +constexpr bool is_digit(ConstString s, unsigned i) { + return (s[i] > 47) && (s[i] < 58); +} + +template +constexpr std::pair parse_number(ConstString s, unsigned i) { + while ((i < s.size()) && is_digit(s, i)) { + ++i; + } + + return {i, 0}; +} + +template +constexpr std::pair parse_type(ConstString s, unsigned i) { + if (s[i] == 's') { // string + ++i; + return {i, 0}; + } else if (s[i] == 'c') { // char + ++i; + return {i, 0}; + } else if (s[i] == 'b') { // int + ++i; + return {i, 0}; + } else if (s[i] == 'B') { + ++i; + return {i, 0}; + } else if (s[i] == 'c') { + ++i; + return {i, 0}; + } else if (s[i] == 'd') { + ++i; + return {i, 0}; + } else if (s[i] == '0') { + ++i; + return {i, 0}; + } else if (s[i] == 'x') { + ++i; + return {i, 0}; + } else if (s[i] == 'X') { + ++i; + return {i, 0}; + } else if (s[i] == 'a') { // float + ++i; + return {i, 0}; + } else if (s[i] == 'A') { + ++i; + return {i, 0}; + } else if (s[i] == 'e') { + ++i; + return {i, 0}; + } else if (s[i] == 'E') { + ++i; + return {i, 0}; + } else if (s[i] == 'f') { + ++i; + return {i, 0}; + } else if (s[i] == 'F') { + ++i; + return {i, 0}; + } else if (s[i] == 'g') { + ++i; + return {i, 0}; + } else if (s[i] == 'G') { + ++i; + return {i, 0}; + } else if (s[i] == 'p') { // pointer + ++i; + return {i, 0}; + } + + return {i, -1}; +} + +template +constexpr std::pair parse_fmt_string(ConstString s, unsigned i) { + int result_extra_len = 0; + + if (s[i] == '0') + ++i; + + if (is_digit(s, i)) { + auto [new_i, extra_len] = parse_number(s, i); + if (extra_len < 0) + return {i, -1}; + i = new_i; + result_extra_len += extra_len; + } + + if (s[i] == '.') { + ++i; + auto [new_i, extra_len] = parse_number(s, i); + if (extra_len < 0) + return {i, -1}; + i = new_i; + result_extra_len += extra_len; + } + + if (s[i] != '}') { + auto [new_i, extra_len] = parse_type(s, i); + if (extra_len < 0) + return {i, -1}; + i = new_i; + result_extra_len += extra_len; + + } + + return {i, result_extra_len}; +} + +template +constexpr std::pair parse_braces(ConstString s, unsigned i) { + int result_extra_len = 0; + + if (s[i] == '}') { + ++i; + return {i, result_extra_len}; + } else if (s[i] == ':') { + ++i; + + auto [new_i, extra_len] = parse_fmt_string(s, i); + if (extra_len < 0) + return {i, -1}; + i = new_i; + result_extra_len += extra_len; + + if (s[i] == '}') { + ++i; + return {i, result_extra_len}; + } + } + + return {i, -1}; +} + +template +constexpr int get_output_len(ConstString s) { + 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); + if (extra_len < 0) + return -1; + i = new_i; + result_extra_len += extra_len; + + } else if (s[i] == '}') { + return -1; + } + } + + return (result_extra_len + s.size()); +} + + +#endif //LOGGER_PARSING_H diff --git a/inc/utility.h b/inc/utility.h new file mode 100644 index 0000000..f22ee41 --- /dev/null +++ b/inc/utility.h @@ -0,0 +1,28 @@ +#ifndef LOGGER_UTILITY_H +#define LOGGER_UTILITY_H + + +template +class ConstString { +public: + constexpr ConstString(const char (&content)[N]) noexcept { + std::copy(std::begin(content), std::end(content), + std::begin(m_content)); + } + + constexpr char operator[](std::size_t index) const noexcept { + return m_content[index]; + } + + constexpr std::size_t size() const noexcept { + return N-1; + } + + std::array m_content; +}; + +template +ConstString(const char (&)[N]) -> ConstString; + + +#endif //LOGGER_UTILITY_H