Made code easier to read
This commit is contained in:
parent
800c141a87
commit
5ad045fa58
20
inc/format.h
20
inc/format.h
@ -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>
|
template <fmt_node_t fmt_node, std::integral arg_t>
|
||||||
constexpr std::array<char, fmt_node.length> format_arg(arg_t arg) {
|
constexpr std::array<char, fmt_node.length> format_arg(arg_t arg) {
|
||||||
check_fmt_params<fmt_node, arg_t>();
|
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;
|
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>
|
template <fmt_node_t fmt_node, std::floating_point arg_t>
|
||||||
constexpr std::array<char, fmt_node.length> format_arg(arg_t arg) {
|
constexpr std::array<char, fmt_node.length> format_arg(arg_t arg) {
|
||||||
check_fmt_params<fmt_node, arg_t>();
|
check_fmt_params<fmt_node, arg_t>();
|
||||||
|
|
||||||
constexpr unsigned len_before_point =
|
constexpr unsigned point_index = fmt_node.length - fmt_node.precision - 1;
|
||||||
fmt_node.length - fmt_node.precision - 1;
|
constexpr unsigned multiplier = const_pow(10, fmt_node.precision);
|
||||||
constexpr unsigned multiplier = const_pow(10, fmt_node.precision);
|
|
||||||
|
|
||||||
std::array<char, fmt_node.length> result =
|
std::array<char, fmt_node.length> result =
|
||||||
get_init_array<fmt_node.length, fmt_node.has_zero_padding>();
|
get_init_array<fmt_node.length, fmt_node.has_zero_padding>();
|
||||||
result[len_before_point] = '.';
|
result[point_index] = '.';
|
||||||
|
|
||||||
arg = arg * multiplier;
|
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;
|
result[i] = static_cast<int>(arg) % 10 + 48;
|
||||||
arg = arg / 10;
|
arg = arg / 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (unsigned i = fmt_node.precision + 2; (i <= result.size()) && (arg >= 1); ++i) {
|
for (unsigned i = point_index - 1; (i >= 0) && (arg >= 1); --i) {
|
||||||
result[result.size() - i] = static_cast<int>(arg) % 10 + 48;
|
result[i] = static_cast<int>(arg) % 10 + 48;
|
||||||
arg = arg / 10;
|
arg = arg / 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -251,9 +251,8 @@ constexpr parse_result_t<string_result_t<get_ast_len<s>()>> parse_string() {
|
|||||||
++i;
|
++i;
|
||||||
|
|
||||||
auto [is_valid, new_i, format_node] = parse_braces<s>(i);
|
auto [is_valid, new_i, format_node] = parse_braces<s>(i);
|
||||||
if (!is_valid) {
|
if (!is_valid)
|
||||||
return {false, i, {}};
|
return {false, i, {}};
|
||||||
}
|
|
||||||
i = new_i;
|
i = new_i;
|
||||||
result.value[ast_position++].set_node(format_node);
|
result.value[ast_position++].set_node(format_node);
|
||||||
|
|
||||||
|
|||||||
@ -12,7 +12,7 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
constexpr auto formatted = format<"Test: {} {} {:0}">(142.4334, "abc", 1234);
|
constexpr auto formatted = format<"Test: {:012.5} {} {:034}">(142.4334, "abc", 1234);
|
||||||
|
|
||||||
for (const auto& c : formatted)
|
for (const auto& c : formatted)
|
||||||
std::cout << c;
|
std::cout << c;
|
||||||
|
|||||||
@ -6,9 +6,9 @@ using namespace detail;
|
|||||||
|
|
||||||
|
|
||||||
TEST(Format, positive_int) {
|
TEST(Format, positive_int) {
|
||||||
constexpr std::array<char, 8> control1 = {'0', '0', '0', '2',
|
constexpr std::array<char, 8> control1 = {'0', '0', '0', '0',
|
||||||
'2', '2', '2', '2'};
|
'0', '0', '0', '2'};
|
||||||
constexpr std::array<char, 8> formatted1 = format<"{:08}">(22222);
|
constexpr std::array<char, 8> formatted1 = format<"{:08}">(2);
|
||||||
|
|
||||||
constexpr std::array<char, 8> control2 = {' ', ' ', ' ', '2',
|
constexpr std::array<char, 8> control2 = {' ', ' ', ' ', '2',
|
||||||
'2', '2', '2', '2'};
|
'2', '2', '2', '2'};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user