Renamed result to value and moved testing code

This commit is contained in:
Andreas Tsouchlos 2021-11-20 23:47:15 +01:00
parent 129b16d6e0
commit 92d8f423d8
2 changed files with 58 additions and 22 deletions

View File

@ -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 }; enum class FormatType { s, c, b, B, d, o, x, X, a, A, e, E, f, F, g, G, p };
template <typename result_t> template <typename value_t>
struct parse_result_t { struct parse_result_t {
bool is_valid = false; bool is_valid = false;
unsigned new_index = 0; unsigned new_index = 0;
result_t result; value_t value;
}; };
struct fmt_node_t { struct fmt_node_t {
@ -211,6 +211,28 @@ constexpr unsigned count_braces() {
return result; return result;
} }
template <ConstString s>
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 <ConstString s> template <ConstString s>
constexpr parse_result_t<string_result_t<count_braces<s>()>> parse_string() { constexpr parse_result_t<string_result_t<count_braces<s>()>> parse_string() {
parse_result_t<string_result_t<count_braces<s>()>> result; parse_result_t<string_result_t<count_braces<s>()>> result;
@ -226,8 +248,8 @@ constexpr parse_result_t<string_result_t<count_braces<s>()>> parse_string() {
if (!is_valid) { if (!is_valid) {
return {false, i, {}}; return {false, i, {}};
} }
i = new_i; i = new_i;
result.result[format_node_pos++] = format_node; result.value[format_node_pos++] = format_node;
} else if (s[i] == '}') { } else if (s[i] == '}') {
return {false, i, {}}; return {false, i, {}};
@ -239,10 +261,16 @@ constexpr parse_result_t<string_result_t<count_braces<s>()>> parse_string() {
template <ConstString s> template <ConstString s>
constexpr int get_output_len() { constexpr int get_output_len() {
constexpr auto result = parse_string<s>(); constexpr auto parse_result = parse_string<s>();
static_assert(parse_result.is_valid, "Syntax error in log string");
// TODO unsigned result = s.size() - len_braces<s>();
return result.is_valid;
for (const auto& fmt_node : parse_result.value) {
result += fmt_node.length;
}
return result;
} }
@ -250,13 +278,26 @@ constexpr int get_output_len() {
template <detail::ConstString s, typename... args_t> template <detail::ConstString s, typename... args_t>
constexpr std::array<char, detail::get_output_len<s>()> format(args_t...) { std::array<char, detail::get_output_len<s>()> format(args_t...) {
constexpr int len = detail::get_output_len<s>(); std::array<char, detail::get_output_len<s>()> result;
static_assert(len > 0, "Syntax error in log string");
std::cout << "Computed Length: " << len << std::endl; constexpr auto parse_result = detail::parse_string<s>();
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<int>(format_node.type)
<< std::endl;
}
return result;
} }

View File

@ -13,23 +13,18 @@ private:
int main() { int main() {
Uart uart; /* Uart uart;
Logger logger(uart); Logger logger(uart);
logger.log<"Test:{}">(1, 2, 3); logger.log<"Test:{}">(1, 2, 3);
std::cout << std::endl; 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) { constexpr detail::ConstString s{"Test: {:16.8f} {:03.5} {:08.2}"};
std::cout << "\tFormat Node:" << std::endl;
std::cout << "\t\thas_zero_padding:\t" << format_node.has_zero_padding << std::endl; const auto formatted = format<s>(3.4, "abc", 8.98754);
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<int>(format_node.type) << std::endl;
}
/* /*
constexpr detail::ConstString s = "{:8.14c}"; constexpr detail::ConstString s = "{:8.14c}";