Added more tests; is_digit(s, i) -> is_digit<s>(i); Minor other stuff

This commit is contained in:
2021-11-22 10:53:55 +01:00
parent 7b5290983a
commit 800c141a87
6 changed files with 130 additions and 19 deletions

View File

@@ -2,6 +2,9 @@
#define LOGGER_CONSTSTRING_H
#include <array>
namespace detail {

View File

@@ -35,11 +35,17 @@ constexpr int get_output_len() {
template <std::size_t len, bool zeroed>
constexpr std::array<char, len> get_init_array() {
std::array<char, len> result;
if constexpr (zeroed) {
return {'0'};
for (auto& c : result)
c = '0';
} else {
return {' '};
for (auto& c : result)
c = ' ';
}
return result;
}
@@ -148,7 +154,7 @@ constexpr std::array<char, detail::get_output_len<s>()> format(args_t... args) {
constexpr auto parse_result = detail::parse_string<s>();
static_assert(parse_result.is_valid, "Syntax error in format string");
std::array<char, detail::get_output_len<s>()> result;
std::array<char, detail::get_output_len<s>()> result = {};
return detail::format_args<parse_result.value>(result, args...);
}

View File

@@ -95,17 +95,21 @@ constexpr int get_ast_len() {
*/
template <std::size_t N>
constexpr bool is_digit(ConstString<N> s, unsigned i) {
template <ConstString s>
constexpr bool is_digit(unsigned i) {
return (s[i] > 47) && (s[i] < 58);
}
template <ConstString s>
constexpr parse_result_t<int> parse_number(unsigned i) {
int number = 0;
constexpr parse_result_t<unsigned> parse_number(unsigned i) {
unsigned number = 0;
while ((i < s.size()) && is_digit(s, i)) {
if (!is_digit<s>(i)) {
return {false, i, number};
}
while ((i < s.size()) && is_digit<s>(i)) {
number = number * 10;
number += (s[i] - 48);
++i;
@@ -184,7 +188,7 @@ constexpr parse_result_t<fmt_node_t> parse_fmt_string(unsigned i) {
result.has_zero_padding = true;
}
if (is_digit(s, i)) {
if (is_digit<s>(i)) {
auto [is_valid, new_i, number] = parse_number<s>(i);
if (!is_valid)
return {false, i, result};