Added recursive implementation of detail::format_args
This commit is contained in:
parent
0d72d35229
commit
8bc8fa835d
23
inc/format.h
23
inc/format.h
@ -31,7 +31,7 @@ constexpr bool is_valid_type() {
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T, fmt_node_t fmt_node>
|
||||
template <fmt_node_t fmt_node, typename T>
|
||||
constexpr std::array<char, fmt_node.length> format_arg(T arg) {
|
||||
static_assert(is_valid_type<T, fmt_node>(), "Invalid argument type");
|
||||
|
||||
@ -44,6 +44,25 @@ constexpr std::array<char, fmt_node.length> format_arg(T arg) {
|
||||
}
|
||||
|
||||
|
||||
template<auto t_ast, unsigned t_ast_i=0, unsigned t_result_i=0, typename char_array_t, typename first_arg_t, typename... other_args_t>
|
||||
constexpr char_array_t format_args(char_array_t result, first_arg_t first_arg, other_args_t... other_args) {
|
||||
if constexpr(t_ast_i >= t_ast.size()) {
|
||||
return result;
|
||||
} else {
|
||||
if (t_ast[t_ast_i].is_char()) {
|
||||
result[t_result_i] = t_ast[t_ast_i].get_char();
|
||||
return format_args<t_ast, t_ast_i+1, t_result_i+1>(result, first_arg, other_args...);
|
||||
} else {
|
||||
const auto formatted_arg = format_arg<t_ast[t_ast_i].get_node()>(first_arg);
|
||||
|
||||
std::copy(formatted_arg.begin(), formatted_arg.end(), result.begin()+t_result_i);
|
||||
|
||||
return format_args<t_ast, t_ast_i+1, t_result_i+t_ast[t_ast_i].get_node().length>(result, first_arg, other_args...);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // namespace detail
|
||||
|
||||
|
||||
@ -54,7 +73,7 @@ std::array<char, detail::get_output_len<s>()> format(args_t... args) {
|
||||
|
||||
std::array<char, detail::get_output_len<s>()> result;
|
||||
|
||||
return result;
|
||||
return detail::format_args<parse_result.value>(result, args...);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -46,10 +46,8 @@ public:
|
||||
return m_is_char;
|
||||
}
|
||||
|
||||
private:
|
||||
bool m_is_char = false;
|
||||
|
||||
char m_c = 'c';
|
||||
bool m_is_char = false;
|
||||
char m_c = 'c';
|
||||
fmt_node_t m_node;
|
||||
};
|
||||
|
||||
@ -58,4 +56,4 @@ template <std::size_t N>
|
||||
using string_result_t = std::array<ast_node_t, N>;
|
||||
|
||||
|
||||
#endif //LOGGER_PARSE_TYPES_H
|
||||
#endif // LOGGER_PARSE_TYPES_H
|
||||
|
||||
@ -12,9 +12,13 @@ public:
|
||||
|
||||
|
||||
int main() {
|
||||
constexpr detail::ConstString s{"Test: {:16.8f} {:03.5} {:08.2}"};
|
||||
constexpr detail::ConstString s{"Test: {:3.8f} {:02.5} {:05.2}"};
|
||||
|
||||
const auto formatted = format<s>(3.4, "abc", 8.98754);
|
||||
|
||||
for (const auto& c : formatted)
|
||||
std::cout << c;
|
||||
std::cout << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user