Made code easier to read

This commit is contained in:
2021-11-22 11:07:14 +01:00
parent 800c141a87
commit 5ad045fa58
4 changed files with 14 additions and 17 deletions

View File

@@ -49,7 +49,7 @@ constexpr std::array<char, len> get_init_array() {
}
// TODO: See if this is possible with charconv
// TODO: See if this is possible with <charconv>
template <fmt_node_t fmt_node, std::integral arg_t>
constexpr std::array<char, fmt_node.length> format_arg(arg_t arg) {
check_fmt_params<fmt_node, arg_t>();
@@ -65,33 +65,31 @@ constexpr std::array<char, fmt_node.length> format_arg(arg_t arg) {
return result;
}
// TODO: See if this is possible with charconv
// TODO: See if this is possible with <charconv>
template <fmt_node_t fmt_node, std::floating_point arg_t>
constexpr std::array<char, fmt_node.length> format_arg(arg_t arg) {
check_fmt_params<fmt_node, arg_t>();
constexpr unsigned len_before_point =
fmt_node.length - fmt_node.precision - 1;
constexpr unsigned multiplier = const_pow(10, fmt_node.precision);
constexpr unsigned point_index = fmt_node.length - fmt_node.precision - 1;
constexpr unsigned multiplier = const_pow(10, fmt_node.precision);
std::array<char, fmt_node.length> result =
get_init_array<fmt_node.length, fmt_node.has_zero_padding>();
result[len_before_point] = '.';
result[point_index] = '.';
arg = arg * multiplier;
for (unsigned i = result.size() - 1; i > len_before_point; --i) {
for (unsigned i = result.size() - 1; (i > point_index) && (arg >= 1); --i) {
result[i] = static_cast<int>(arg) % 10 + 48;
arg = arg / 10;
}
for (unsigned i = fmt_node.precision + 2; (i <= result.size()) && (arg >= 1); ++i) {
result[result.size() - i] = static_cast<int>(arg) % 10 + 48;
arg = arg / 10;
for (unsigned i = point_index - 1; (i >= 0) && (arg >= 1); --i) {
result[i] = static_cast<int>(arg) % 10 + 48;
arg = arg / 10;
}
return result;
}

View File

@@ -251,9 +251,8 @@ constexpr parse_result_t<string_result_t<get_ast_len<s>()>> parse_string() {
++i;
auto [is_valid, new_i, format_node] = parse_braces<s>(i);
if (!is_valid) {
if (!is_valid)
return {false, i, {}};
}
i = new_i;
result.value[ast_position++].set_node(format_node);