Replace multiple error reporting mechanisms with report_error

This commit is contained in:
Victor Zverovich
2024-01-15 06:48:10 -08:00
parent f9294f0e60
commit fe0d910a7d
8 changed files with 70 additions and 74 deletions

View File

@@ -1033,8 +1033,6 @@ template <typename OutputIt, typename Char> class generic_context {
return args_;
}
void on_error(const char* message) { throw_format_error(message); }
FMT_CONSTEXPR auto out() -> iterator { return out_; }
void advance_to(iterator it) {
@@ -2333,7 +2331,7 @@ FMT_CONSTEXPR auto write(OutputIt out, const Char* s,
-> OutputIt {
if (specs.type == presentation_type::pointer)
return write_ptr<Char>(out, bit_cast<uintptr_t>(s), &specs);
if (!s) throw_format_error("string pointer is null");
if (!s) report_error("string pointer is null");
return write(out, basic_string_view<Char>(s), specs, {});
}
@@ -2384,7 +2382,7 @@ FMT_CONSTEXPR auto parse_align(const Char* begin, const Char* end,
auto c = *begin;
if (c == '}') return begin;
if (c == '{') {
throw_format_error("invalid fill character '{'");
report_error("invalid fill character '{'");
return begin;
}
specs.fill = {begin, to_unsigned(p - begin)};
@@ -3600,7 +3598,7 @@ FMT_CONSTEXPR20 auto write_float(OutputIt out, T value,
: 6;
if (fspecs.format == float_format::exp) {
if (precision == max_value<int>())
throw_format_error("number is too big");
report_error("number is too big");
else
++precision;
} else if (fspecs.format != float_format::fixed && precision == 0) {
@@ -3707,7 +3705,7 @@ FMT_CONSTEXPR auto write(OutputIt out, Char value) -> OutputIt {
template <typename Char, typename OutputIt>
FMT_CONSTEXPR20 auto write(OutputIt out, const Char* value) -> OutputIt {
if (value) return write(out, basic_string_view<Char>(value));
throw_format_error("string pointer is null");
report_error("string pointer is null");
return out;
}
@@ -3785,13 +3783,13 @@ template <typename Char> struct arg_formatter {
struct width_checker {
template <typename T, FMT_ENABLE_IF(is_integer<T>::value)>
FMT_CONSTEXPR auto operator()(T value) -> unsigned long long {
if (is_negative(value)) throw_format_error("negative width");
if (is_negative(value)) report_error("negative width");
return static_cast<unsigned long long>(value);
}
template <typename T, FMT_ENABLE_IF(!is_integer<T>::value)>
FMT_CONSTEXPR auto operator()(T) -> unsigned long long {
throw_format_error("width is not integer");
report_error("width is not integer");
return 0;
}
};
@@ -3799,13 +3797,13 @@ struct width_checker {
struct precision_checker {
template <typename T, FMT_ENABLE_IF(is_integer<T>::value)>
FMT_CONSTEXPR auto operator()(T value) -> unsigned long long {
if (is_negative(value)) throw_format_error("negative precision");
if (is_negative(value)) report_error("negative precision");
return static_cast<unsigned long long>(value);
}
template <typename T, FMT_ENABLE_IF(!is_integer<T>::value)>
FMT_CONSTEXPR auto operator()(T) -> unsigned long long {
throw_format_error("precision is not integer");
report_error("precision is not integer");
return 0;
}
};
@@ -3813,15 +3811,14 @@ struct precision_checker {
template <typename Handler, typename FormatArg>
FMT_CONSTEXPR auto get_dynamic_spec(FormatArg arg) -> int {
unsigned long long value = arg.visit(Handler());
if (value > to_unsigned(max_value<int>()))
throw_format_error("number is too big");
if (value > to_unsigned(max_value<int>())) report_error("number is too big");
return static_cast<int>(value);
}
template <typename Context, typename ID>
FMT_CONSTEXPR auto get_arg(Context& ctx, ID id) -> decltype(ctx.arg(id)) {
auto arg = ctx.arg(id);
if (!arg) ctx.on_error("argument not found");
if (!arg) report_error("argument not found");
return arg;
}
@@ -3893,6 +3890,7 @@ using format_func = void (*)(detail::buffer<char>&, int, const char*);
FMT_API void format_error_code(buffer<char>& out, int error_code,
string_view message) noexcept;
using fmt::report_error;
FMT_API void report_error(format_func func, int error_code,
const char* message) noexcept;
} // namespace detail
@@ -4253,7 +4251,7 @@ void vformat_to(buffer<Char>& buf, basic_string_view<Char> fmt,
auto out = basic_appender<Char>(buf);
if (fmt.size() == 2 && equal2(fmt.data(), "{}")) {
auto arg = args.get(0);
if (!arg) throw_format_error("argument not found");
if (!arg) report_error("argument not found");
arg.visit(default_arg_formatter<Char>{out, args, loc});
return;
}
@@ -4280,7 +4278,7 @@ void vformat_to(buffer<Char>& buf, basic_string_view<Char> fmt,
}
FMT_CONSTEXPR auto on_arg_id(basic_string_view<Char> id) -> int {
int arg_id = context.arg_id(id);
if (arg_id < 0) throw_format_error("argument not found");
if (arg_id < 0) report_error("argument not found");
return arg_id;
}
@@ -4303,13 +4301,13 @@ void vformat_to(buffer<Char>& buf, basic_string_view<Char> fmt,
detail::handle_dynamic_spec<detail::precision_checker>(
specs.precision, specs.precision_ref, context);
if (begin == end || *begin != '}')
throw_format_error("missing '}' in format string");
report_error("missing '}' in format string");
context.advance_to(arg.visit(
arg_formatter<Char>{context.out(), specs, context.locale()}));
return begin;
}
void on_error(const char* message) { throw_format_error(message); }
void on_error(const char* message) { report_error(message); }
};
detail::parse_format_string<false>(fmt, format_handler(out, fmt, args, loc));
}