Report error from parse_nonnegative_int via handler

This commit is contained in:
Victor Zverovich 2017-10-21 08:37:52 -07:00
parent 0ebdf41efa
commit 932ab2bfca
2 changed files with 16 additions and 13 deletions

View File

@ -1924,7 +1924,7 @@ class parse_context {
return 0; return 0;
} }
void check_arg_id(unsigned index) { void check_arg_id(unsigned) {
const char *error = 0; const char *error = 0;
if (!check_no_auto_index(error)) if (!check_no_auto_index(error))
FMT_THROW(format_error(error)); FMT_THROW(format_error(error));
@ -3016,11 +3016,11 @@ constexpr bool is_name_start(Char c) {
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || '_' == c; return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || '_' == c;
} }
// Parses an unsigned integer advancing it to the end of the parsed input. // Parses the input as an unsigned integer. This function assumes that the
// This function assumes that the first character of it is a digit and a // first character is a digit and presence of a non-digit character at the end.
// presence of a non-digit character at the end. // it: an iterator pointing to the beginning of the input range.
template <typename Iterator> template <typename Iterator, typename ErrorHandler>
constexpr unsigned parse_nonnegative_int(Iterator &it) { constexpr unsigned parse_nonnegative_int(Iterator &it, ErrorHandler& handler) {
assert('0' <= *it && *it <= '9'); assert('0' <= *it && *it <= '9');
unsigned value = 0; unsigned value = 0;
do { do {
@ -3039,7 +3039,7 @@ constexpr unsigned parse_nonnegative_int(Iterator &it) {
// Convert to unsigned to prevent a warning. // Convert to unsigned to prevent a warning.
unsigned max_int = (std::numeric_limits<int>::max)(); unsigned max_int = (std::numeric_limits<int>::max)();
if (value > max_int) if (value > max_int)
FMT_THROW(format_error("number is too big")); handler.on_error("number is too big");
return value; return value;
} }
@ -3341,7 +3341,7 @@ constexpr Iterator parse_arg_id(Iterator it, Handler& handler) {
return it; return it;
} }
if (c >= '0' && c <= '9') { if (c >= '0' && c <= '9') {
unsigned index = parse_nonnegative_int(it); unsigned index = parse_nonnegative_int(it, handler);
if (*it != '}' && *it != ':') { if (*it != '}' && *it != ':') {
handler.on_error("invalid format string"); handler.on_error("invalid format string");
return it; return it;
@ -3464,7 +3464,7 @@ constexpr Iterator parse_format_specs(Iterator it, Handler &handler) {
// Parse width. // Parse width.
if ('0' <= *it && *it <= '9') { if ('0' <= *it && *it <= '9') {
handler.on_width(parse_nonnegative_int(it)); handler.on_width(parse_nonnegative_int(it, handler));
} else if (*it == '{') { } else if (*it == '{') {
width_handler<Handler, char_type> wh(handler); width_handler<Handler, char_type> wh(handler);
it = parse_arg_id(it + 1, wh); it = parse_arg_id(it + 1, wh);
@ -3478,7 +3478,7 @@ constexpr Iterator parse_format_specs(Iterator it, Handler &handler) {
if (*it == '.') { if (*it == '.') {
++it; ++it;
if ('0' <= *it && *it <= '9') { if ('0' <= *it && *it <= '9') {
handler.on_precision(parse_nonnegative_int(it)); handler.on_precision(parse_nonnegative_int(it, handler));
} else if (*it == '{') { } else if (*it == '{') {
precision_handler<Handler, char_type> ph(handler); precision_handler<Handler, char_type> ph(handler);
it = parse_arg_id(it + 1, ph); it = parse_arg_id(it + 1, ph);

View File

@ -389,7 +389,8 @@ unsigned printf_context<Char, AF>::parse_header(
if (c >= '0' && c <= '9') { if (c >= '0' && c <= '9') {
// Parse an argument index (if followed by '$') or a width possibly // Parse an argument index (if followed by '$') or a width possibly
// preceded with '0' flag(s). // preceded with '0' flag(s).
unsigned value = internal::parse_nonnegative_int(it); internal::error_handler eh;
unsigned value = parse_nonnegative_int(it, eh);
if (*it == '$') { // value is an argument index if (*it == '$') { // value is an argument index
++it; ++it;
arg_index = value; arg_index = value;
@ -407,7 +408,8 @@ unsigned printf_context<Char, AF>::parse_header(
parse_flags(spec, it); parse_flags(spec, it);
// Parse width. // Parse width.
if (*it >= '0' && *it <= '9') { if (*it >= '0' && *it <= '9') {
spec.width_ = internal::parse_nonnegative_int(it); internal::error_handler eh;
spec.width_ = parse_nonnegative_int(it, eh);
} else if (*it == '*') { } else if (*it == '*') {
++it; ++it;
spec.width_ = visit(internal::PrintfWidthHandler<Char>(spec), get_arg(it)); spec.width_ = visit(internal::PrintfWidthHandler<Char>(spec), get_arg(it));
@ -441,7 +443,8 @@ void printf_context<Char, AF>::format(basic_buffer<Char> &buffer) {
if (*it == '.') { if (*it == '.') {
++it; ++it;
if ('0' <= *it && *it <= '9') { if ('0' <= *it && *it <= '9') {
spec.precision_ = static_cast<int>(internal::parse_nonnegative_int(it)); internal::error_handler eh;
spec.precision_ = static_cast<int>(parse_nonnegative_int(it, eh));
} else if (*it == '*') { } else if (*it == '*') {
++it; ++it;
spec.precision_ = spec.precision_ =