mirror of
https://github.com/fmtlib/fmt.git
synced 2024-12-02 06:20:09 +00:00
Temporarily revert parsing changes
This commit is contained in:
parent
a7fb321ac6
commit
bc1b89da26
@ -2514,63 +2514,12 @@ template <typename Handler, typename Char> struct id_adapter {
|
||||
Handler& handler;
|
||||
};
|
||||
|
||||
template <typename Char, typename Handler>
|
||||
FMT_CONSTEXPR_DECL FMT_INLINE const Char* parse_replacement_field(
|
||||
const Char* begin, const Char* end, Handler&& handler) {
|
||||
++begin;
|
||||
if (begin == end) return handler.on_error("invalid format string"), end;
|
||||
if (static_cast<char>(*begin) == '}') {
|
||||
handler.on_arg_id();
|
||||
handler.on_replacement_field(begin);
|
||||
} else if (*begin == '{') {
|
||||
handler.on_text(begin, begin + 1);
|
||||
} else {
|
||||
begin = parse_arg_id(begin, end, id_adapter<Handler, Char>{handler});
|
||||
Char c = begin != end ? *begin : Char();
|
||||
if (c == '}') {
|
||||
handler.on_replacement_field(begin);
|
||||
} else if (c == ':') {
|
||||
begin = handler.on_format_specs(begin + 1, end);
|
||||
if (begin == end || *begin != '}')
|
||||
return handler.on_error("unknown format specifier"), end;
|
||||
} else {
|
||||
return handler.on_error("missing '}' in format string"), end;
|
||||
}
|
||||
}
|
||||
return begin + 1;
|
||||
}
|
||||
|
||||
template <bool IS_CONSTEXPR, typename Char, typename Handler>
|
||||
FMT_CONSTEXPR void parse_format_string(basic_string_view<Char> format_str,
|
||||
Handler&& handler) {
|
||||
auto begin = format_str.data();
|
||||
auto end = begin + format_str.size();
|
||||
if (end - begin < 32) {
|
||||
// Use a simple loop instead of memchr for small strings.
|
||||
const Char* p = begin;
|
||||
while (p != end) {
|
||||
auto c = *p++;
|
||||
if (c == '{') {
|
||||
handler.on_text(begin, p - 1);
|
||||
begin = p = parse_replacement_field(p - 1, end, handler);
|
||||
} else if (c == '}') {
|
||||
if (p == end || *p != '}')
|
||||
return handler.on_error("unmatched '}' in format string");
|
||||
handler.on_text(begin, p);
|
||||
begin = ++p;
|
||||
}
|
||||
}
|
||||
handler.on_text(begin, end);
|
||||
return;
|
||||
}
|
||||
struct writer {
|
||||
FMT_CONSTEXPR void operator()(const Char* begin, const Char* end) {
|
||||
if (begin + 1 >= end) {
|
||||
if (begin == end) return;
|
||||
if (*begin == '}')
|
||||
return handler_.on_error("unmatched '}' in format string");
|
||||
return handler_.on_text(begin, begin + 1);
|
||||
}
|
||||
if (begin == end) return;
|
||||
for (;;) {
|
||||
const Char* p = nullptr;
|
||||
if (!find<IS_CONSTEXPR>(begin, end, '}', p))
|
||||
@ -2584,6 +2533,8 @@ FMT_CONSTEXPR void parse_format_string(basic_string_view<Char> format_str,
|
||||
}
|
||||
Handler& handler_;
|
||||
} write{handler};
|
||||
auto begin = format_str.data();
|
||||
auto end = begin + format_str.size();
|
||||
while (begin != end) {
|
||||
// Doing two passes with memchr (one for '{' and another for '}') is up to
|
||||
// 2.5x faster than the naive one-pass implementation on big format strings.
|
||||
@ -2591,7 +2542,27 @@ FMT_CONSTEXPR void parse_format_string(basic_string_view<Char> format_str,
|
||||
if (*begin != '{' && !find<IS_CONSTEXPR>(begin + 1, end, '{', p))
|
||||
return write(begin, end);
|
||||
write(begin, p);
|
||||
begin = parse_replacement_field(p, end, handler);
|
||||
++p;
|
||||
if (p == end) return handler.on_error("invalid format string");
|
||||
if (static_cast<char>(*p) == '}') {
|
||||
handler.on_arg_id();
|
||||
handler.on_replacement_field(p);
|
||||
} else if (*p == '{') {
|
||||
handler.on_text(p, p + 1);
|
||||
} else {
|
||||
p = parse_arg_id(p, end, id_adapter<Handler, Char>{handler});
|
||||
Char c = p != end ? *p : Char();
|
||||
if (c == '}') {
|
||||
handler.on_replacement_field(p);
|
||||
} else if (c == ':') {
|
||||
p = handler.on_format_specs(p + 1, end);
|
||||
if (p == end || *p != '}')
|
||||
return handler.on_error("unknown format specifier");
|
||||
} else {
|
||||
return handler.on_error("missing '}' in format string");
|
||||
}
|
||||
}
|
||||
begin = p + 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user