Removed length from parse_result_t
This commit is contained in:
parent
435a7c442f
commit
bb24f86d5d
140
inc/parsing.h
140
inc/parsing.h
@ -22,7 +22,6 @@ template <typename result_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;
|
||||||
unsigned length = 0;
|
|
||||||
result_t result;
|
result_t result;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -33,6 +32,9 @@ struct fmt_string_result_t {
|
|||||||
FormatType type = FormatType::s;
|
FormatType type = FormatType::s;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <std::size_t N>
|
||||||
|
using string_result_t = std::array<fmt_string_result_t, N>;
|
||||||
|
|
||||||
|
|
||||||
// clang-format off
|
// clang-format off
|
||||||
|
|
||||||
@ -90,68 +92,68 @@ constexpr parse_result_t<int> parse_number(unsigned i) {
|
|||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {true, i, 0, number};
|
return {true, i, number};
|
||||||
}
|
}
|
||||||
|
|
||||||
template <ConstString s>
|
template <ConstString s>
|
||||||
constexpr parse_result_t<FormatType> parse_type(unsigned i) {
|
constexpr parse_result_t<FormatType> parse_type(unsigned i) {
|
||||||
if (s[i] == 's') { // string
|
if (s[i] == 's') { // string
|
||||||
++i;
|
++i;
|
||||||
return {true, i, 1, FormatType::s};
|
return {true, i, FormatType::s};
|
||||||
} else if (s[i] == 'c') { // char
|
} else if (s[i] == 'c') { // char
|
||||||
++i;
|
++i;
|
||||||
return {true, i, 1, FormatType::c};
|
return {true, i, FormatType::c};
|
||||||
} else if (s[i] == 'b') { // int
|
} else if (s[i] == 'b') { // int
|
||||||
++i;
|
++i;
|
||||||
return {true, i, 1, FormatType::b};
|
return {true, i, FormatType::b};
|
||||||
} else if (s[i] == 'B') {
|
} else if (s[i] == 'B') {
|
||||||
++i;
|
++i;
|
||||||
return {true, i, 1, FormatType::B};
|
return {true, i, FormatType::B};
|
||||||
// } else if (s[i] == 'c') {
|
// } else if (s[i] == 'c') {
|
||||||
// ++i;
|
// ++i;
|
||||||
// return {true, i, 1, FormatType::c};
|
// return {true, i, FormatType::c};
|
||||||
} else if (s[i] == 'd') {
|
} else if (s[i] == 'd') {
|
||||||
++i;
|
++i;
|
||||||
return {true, i, 1, FormatType::d};
|
return {true, i, FormatType::d};
|
||||||
} else if (s[i] == 'o') {
|
} else if (s[i] == 'o') {
|
||||||
++i;
|
++i;
|
||||||
return {true, i, 1, FormatType::o};
|
return {true, i, FormatType::o};
|
||||||
} else if (s[i] == 'x') {
|
} else if (s[i] == 'x') {
|
||||||
++i;
|
++i;
|
||||||
return {true, i, 1, FormatType::x};
|
return {true, i, FormatType::x};
|
||||||
} else if (s[i] == 'X') {
|
} else if (s[i] == 'X') {
|
||||||
++i;
|
++i;
|
||||||
return {true, i, 1, FormatType::X};
|
return {true, i, FormatType::X};
|
||||||
} else if (s[i] == 'a') { // float
|
} else if (s[i] == 'a') { // float
|
||||||
++i;
|
++i;
|
||||||
return {true, i, 1, FormatType::a};
|
return {true, i, FormatType::a};
|
||||||
} else if (s[i] == 'A') {
|
} else if (s[i] == 'A') {
|
||||||
++i;
|
++i;
|
||||||
return {true, i, 1, FormatType::A};
|
return {true, i, FormatType::A};
|
||||||
} else if (s[i] == 'e') {
|
} else if (s[i] == 'e') {
|
||||||
++i;
|
++i;
|
||||||
return {true, i, 1, FormatType::e};
|
return {true, i, FormatType::e};
|
||||||
} else if (s[i] == 'E') {
|
} else if (s[i] == 'E') {
|
||||||
++i;
|
++i;
|
||||||
return {true, i, 1, FormatType::E};
|
return {true, i, FormatType::E};
|
||||||
} else if (s[i] == 'f') {
|
} else if (s[i] == 'f') {
|
||||||
++i;
|
++i;
|
||||||
return {true, i, 1, FormatType::f};
|
return {true, i, FormatType::f};
|
||||||
} else if (s[i] == 'F') {
|
} else if (s[i] == 'F') {
|
||||||
++i;
|
++i;
|
||||||
return {true, i, 1, FormatType::F};
|
return {true, i, FormatType::F};
|
||||||
} else if (s[i] == 'g') {
|
} else if (s[i] == 'g') {
|
||||||
++i;
|
++i;
|
||||||
return {true, i, 1, FormatType::g};
|
return {true, i, FormatType::g};
|
||||||
} else if (s[i] == 'G') {
|
} else if (s[i] == 'G') {
|
||||||
++i;
|
++i;
|
||||||
return {true, i, 1, FormatType::G};
|
return {true, i, FormatType::G};
|
||||||
} else if (s[i] == 'p') { // pointer
|
} else if (s[i] == 'p') { // pointer
|
||||||
++i;
|
++i;
|
||||||
return {true, i, 1, FormatType::p};
|
return {true, i, FormatType::p};
|
||||||
}
|
}
|
||||||
|
|
||||||
return {false, i, 0, FormatType::s};
|
return {false, i, FormatType::s};
|
||||||
}
|
}
|
||||||
|
|
||||||
template <ConstString s>
|
template <ConstString s>
|
||||||
@ -159,99 +161,105 @@ constexpr parse_result_t<fmt_string_result_t> parse_fmt_string(unsigned i) {
|
|||||||
|
|
||||||
fmt_string_result_t result;
|
fmt_string_result_t result;
|
||||||
|
|
||||||
unsigned result_extra_len = 0;
|
|
||||||
|
|
||||||
if (s[i] == '0') {
|
if (s[i] == '0') {
|
||||||
++i;
|
++i;
|
||||||
result.has_zero_padding = true;
|
result.has_zero_padding = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_digit(s, i)) {
|
if (is_digit(s, i)) {
|
||||||
auto [is_valid, new_i, len, number] = parse_number<s>(i);
|
auto [is_valid, new_i, number] = parse_number<s>(i);
|
||||||
if (!is_valid)
|
if (!is_valid)
|
||||||
return {false, i, 0, result};
|
return {false, i, result};
|
||||||
i = new_i;
|
i = new_i;
|
||||||
result.length = number;
|
result.length = number;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (s[i] == '.') {
|
if (s[i] == '.') {
|
||||||
++i;
|
++i;
|
||||||
auto [is_valid, new_i, len, number] = parse_number<s>(i);
|
auto [is_valid, new_i, number] = parse_number<s>(i);
|
||||||
if (!is_valid)
|
if (!is_valid)
|
||||||
return {false, i, 0, result};
|
return {false, i, result};
|
||||||
i = new_i;
|
i = new_i;
|
||||||
result.precision = number;
|
result.precision = number;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (s[i] != '}') {
|
if (s[i] != '}') {
|
||||||
auto [is_valid, new_i, len, type] = parse_type<s>(i);
|
auto [is_valid, new_i, type] = parse_type<s>(i);
|
||||||
if (!is_valid)
|
if (!is_valid)
|
||||||
return {false, i, 0, result};
|
return {false, i, result};
|
||||||
i = new_i;
|
i = new_i;
|
||||||
result.type = type;
|
result.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {true, i, result_extra_len, result};
|
return {true, i, result};
|
||||||
}
|
}
|
||||||
|
|
||||||
template <ConstString s>
|
template <ConstString s>
|
||||||
constexpr unsigned count_braces(unsigned i) {
|
constexpr parse_result_t<fmt_string_result_t> parse_braces(unsigned i) {
|
||||||
|
if (s[i] == '}') {
|
||||||
|
++i;
|
||||||
|
return {true, i, {}};
|
||||||
|
} else if (s[i] == ':') {
|
||||||
|
++i;
|
||||||
|
|
||||||
|
auto [is_valid, new_i, format_node] = parse_fmt_string<s>(i);
|
||||||
|
if (!is_valid)
|
||||||
|
return {false, i, {}};
|
||||||
|
i = new_i;
|
||||||
|
|
||||||
|
if (s[i] == '}') {
|
||||||
|
++i;
|
||||||
|
return {true, i, format_node};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {false, i, {}};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <ConstString s>
|
||||||
|
constexpr unsigned count_braces() {
|
||||||
unsigned result = 0;
|
unsigned result = 0;
|
||||||
|
|
||||||
while (i < s.size()) {
|
for (unsigned i = 0; i < s.size(); ++i) {
|
||||||
if (s[i] == '{')
|
if (s[i] == '{')
|
||||||
++result;
|
++result;
|
||||||
++i;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <ConstString s>
|
template <ConstString s>
|
||||||
constexpr std::pair<unsigned, int> parse_braces(unsigned i) {
|
constexpr parse_result_t<string_result_t<count_braces<s>()>> parse_string() {
|
||||||
int result_extra_len = 0;
|
parse_result_t<string_result_t<count_braces<s>()>> result;
|
||||||
|
result.is_valid = true;
|
||||||
|
|
||||||
if (s[i] == '}') {
|
unsigned format_node_pos = 0;
|
||||||
++i;
|
|
||||||
return {i, result_extra_len};
|
|
||||||
} else if (s[i] == ':') {
|
|
||||||
++i;
|
|
||||||
|
|
||||||
auto [is_valid, new_i, len, format_node] = parse_fmt_string<s>(i);
|
|
||||||
if (!is_valid)
|
|
||||||
return {i, -1};
|
|
||||||
i = new_i;
|
|
||||||
result_extra_len += len;
|
|
||||||
|
|
||||||
if (s[i] == '}') {
|
|
||||||
++i;
|
|
||||||
return {i, result_extra_len};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return {i, -1};
|
|
||||||
}
|
|
||||||
|
|
||||||
template <ConstString s>
|
|
||||||
constexpr int get_output_len() {
|
|
||||||
int result_extra_len = 0;
|
|
||||||
|
|
||||||
for (unsigned i = 0; i < s.size(); ++i) {
|
for (unsigned i = 0; i < s.size(); ++i) {
|
||||||
if (s[i] == '{') {
|
if (s[i] == '{') {
|
||||||
++i;
|
++i;
|
||||||
|
|
||||||
auto [new_i, extra_len] = parse_braces<s>(i);
|
auto [is_valid, new_i, format_node] = parse_braces<s>(i);
|
||||||
if (extra_len < 0)
|
if (!is_valid) {
|
||||||
return -1;
|
return {false, i, {}};
|
||||||
|
}
|
||||||
i = new_i;
|
i = new_i;
|
||||||
result_extra_len += extra_len;
|
result.result[format_node_pos] = format_node;
|
||||||
|
|
||||||
} else if (s[i] == '}') {
|
} else if (s[i] == '}') {
|
||||||
return -1;
|
return {false, i, {}};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (result_extra_len + s.size());
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <ConstString s>
|
||||||
|
constexpr int get_output_len() {
|
||||||
|
constexpr auto result = parse_string<s>();
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
return result.is_valid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -259,7 +267,7 @@ constexpr int get_output_len() {
|
|||||||
|
|
||||||
|
|
||||||
template <detail::ConstString s, typename... args_t>
|
template <detail::ConstString s, typename... args_t>
|
||||||
const std::array<char, detail::get_output_len(s)> format(args_t...) {
|
constexpr std::array<char, detail::get_output_len<s>()> format(args_t...) {
|
||||||
constexpr int len = detail::get_output_len<s>();
|
constexpr int len = detail::get_output_len<s>();
|
||||||
static_assert(len > 0, "Syntax error in log string");
|
static_assert(len > 0, "Syntax error in log string");
|
||||||
|
|
||||||
|
|||||||
22
src/main.cpp
22
src/main.cpp
@ -13,11 +13,25 @@ private:
|
|||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
//Uart uart;
|
Uart uart;
|
||||||
|
|
||||||
// Logger logger(uart);
|
Logger logger(uart);
|
||||||
// logger.log<"Test:{:8.2}">(1, 2, 3);
|
logger.log<"Test:{}">(1, 2, 3);
|
||||||
|
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
constexpr auto ast = detail::parse_string<"Test: {:16.8} {: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<int>(format_node.type) << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
constexpr detail::ConstString s = "{:8.14c}";
|
constexpr detail::ConstString s = "{:8.14c}";
|
||||||
|
|
||||||
constexpr auto result = detail::parse_fmt_string<s>(2);
|
constexpr auto result = detail::parse_fmt_string<s>(2);
|
||||||
@ -28,7 +42,7 @@ int main() {
|
|||||||
std::cout << "\tresult.length: " << result.result.length << std::endl;
|
std::cout << "\tresult.length: " << result.result.length << std::endl;
|
||||||
std::cout << "\tresult.precision: " << result.result.precision << std::endl;
|
std::cout << "\tresult.precision: " << result.result.precision << std::endl;
|
||||||
std::cout << "\tresult.type: " << static_cast<int>(result.result.type) << std::endl;
|
std::cout << "\tresult.type: " << static_cast<int>(result.result.type) << std::endl;
|
||||||
|
*/
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user