mirror of
https://github.com/fmtlib/fmt.git
synced 2025-01-06 04:50:05 +00:00
Make parse_format_string constexpr
This commit is contained in:
parent
e926ae78ac
commit
db9ffa1405
@ -3519,7 +3519,7 @@ constexpr Iterator parse_format_specs(Iterator it, SpecHandler &handler) {
|
||||
}
|
||||
|
||||
template <typename Iterator, typename Handler>
|
||||
void parse_format_string(Iterator it, Handler &handler) {
|
||||
constexpr void parse_format_string(Iterator it, Handler &handler) {
|
||||
using char_type = typename std::iterator_traits<Iterator>::value_type;
|
||||
auto start = it;
|
||||
while (*it) {
|
||||
@ -3530,20 +3530,24 @@ void parse_format_string(Iterator it, Handler &handler) {
|
||||
start = ++it;
|
||||
continue;
|
||||
}
|
||||
if (ch == '}')
|
||||
if (ch == '}') {
|
||||
handler.on_error("unmatched '}' in format string");
|
||||
return;
|
||||
}
|
||||
handler.on_text(start, it - 1);
|
||||
|
||||
struct id_adapter {
|
||||
explicit id_adapter(Handler &h): handler(h) {}
|
||||
constexpr explicit id_adapter(Handler &h): handler(h) {}
|
||||
|
||||
void operator()() { handler.on_arg_id(); }
|
||||
void operator()(unsigned id) { handler.on_arg_id(id); }
|
||||
void operator()(basic_string_view<char_type> id) {
|
||||
constexpr void operator()() { handler.on_arg_id(); }
|
||||
constexpr void operator()(unsigned id) { handler.on_arg_id(id); }
|
||||
constexpr void operator()(basic_string_view<char_type> id) {
|
||||
handler.on_arg_id(id);
|
||||
}
|
||||
|
||||
void on_error(const char *message) { handler.on_error(message); }
|
||||
constexpr void on_error(const char *message) {
|
||||
handler.on_error(message);
|
||||
}
|
||||
|
||||
Handler &handler;
|
||||
} adapter(handler);
|
||||
|
@ -1780,3 +1780,35 @@ TEST(FormatTest, ConstexprSpecsChecker) {
|
||||
static_assert(check_specs("d").type == 'd', "");
|
||||
static_assert(check_specs("{<").res == handler::ERROR, "");
|
||||
}
|
||||
|
||||
struct test_format_string_handler {
|
||||
constexpr void on_text(const char *, const char *) {}
|
||||
|
||||
constexpr void on_arg_id() {}
|
||||
|
||||
template <typename T>
|
||||
constexpr void on_arg_id(T) {}
|
||||
|
||||
constexpr void on_replacement_field(const char *) {}
|
||||
|
||||
constexpr const char *on_format_specs(const char *s) { return s; }
|
||||
|
||||
constexpr void on_error(const char *) { error = true; }
|
||||
|
||||
bool error = false;
|
||||
};
|
||||
|
||||
constexpr bool parse_string(const char *s) {
|
||||
test_format_string_handler h;
|
||||
fmt::internal::parse_format_string(s, h);
|
||||
return !h.error;
|
||||
}
|
||||
|
||||
TEST(FormatTest, ConstexprParseFormatString) {
|
||||
static_assert(parse_string("foo"), "");
|
||||
static_assert(!parse_string("}"), "");
|
||||
static_assert(parse_string("{}"), "");
|
||||
static_assert(parse_string("{42}"), "");
|
||||
static_assert(parse_string("{foo}"), "");
|
||||
static_assert(parse_string("{:}"), "");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user