Improve spec parsing

This commit is contained in:
Victor Zverovich
2024-08-02 11:57:02 -07:00
parent 8445327c84
commit b9c0e4dd82
2 changed files with 15 additions and 28 deletions
+7 -23
View File
@@ -2398,14 +2398,11 @@ FMT_CONSTEXPR auto parse_format_specs(const Char* begin, const Char* end,
constexpr auto integral_set = sint_set | uint_set | bool_set | char_set;
struct {
const Char*& begin;
dynamic_format_specs<Char>& specs;
format_specs& specs;
type arg_type;
FMT_CONSTEXPR auto operator()(pres pres_type, int set) -> const Char* {
if (!in(arg_type, set)) {
if (arg_type == type::none_type) return begin;
report_error("invalid format specifier");
}
if (!in(arg_type, set)) report_error("invalid format specifier");
specs.type = pres_type;
return begin + 1;
}
@@ -2421,34 +2418,23 @@ FMT_CONSTEXPR auto parse_format_specs(const Char* begin, const Char* end,
++begin;
break;
case '+':
case '-':
FMT_FALLTHROUGH;
case ' ':
if (arg_type == type::none_type) return begin;
specs.sign = c == ' ' ? sign::space : sign::plus;
FMT_FALLTHROUGH;
case '-':
enter_state(state::sign, in(arg_type, sint_set | float_set));
switch (c) {
case '+':
specs.sign = sign::plus;
break;
case '-':
break;
case ' ':
specs.sign = sign::space;
break;
}
++begin;
break;
case '#':
if (arg_type == type::none_type) return begin;
enter_state(state::hash, is_arithmetic_type(arg_type));
specs.alt = true;
++begin;
break;
case '0':
enter_state(state::zero);
if (!is_arithmetic_type(arg_type)) {
if (arg_type == type::none_type) return begin;
if (!is_arithmetic_type(arg_type))
report_error("format specifier requires numeric argument");
}
if (specs.align == align::none) {
// Ignore 0 if align is specified for compatibility with std::format.
specs.align = align::numeric;
@@ -2470,14 +2456,12 @@ FMT_CONSTEXPR auto parse_format_specs(const Char* begin, const Char* end,
begin = parse_dynamic_spec(begin, end, specs.width, specs.width_ref, ctx);
break;
case '.':
if (arg_type == type::none_type) return begin;
enter_state(state::precision,
in(arg_type, float_set | string_set | cstring_set));
begin = parse_precision(begin, end, specs.precision, specs.precision_ref,
ctx);
break;
case 'L':
if (arg_type == type::none_type) return begin;
enter_state(state::locale, is_arithmetic_type(arg_type));
specs.localized = true;
++begin;