From 932ab2bfca1e77ae2a9df5f866262f59595b3298 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 21 Oct 2017 08:37:52 -0700 Subject: [PATCH] Report error from parse_nonnegative_int via handler --- include/fmt/format.h | 20 ++++++++++---------- include/fmt/printf.h | 9 ++++++--- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index c4b72b1c..8e7e0121 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1924,7 +1924,7 @@ class parse_context { return 0; } - void check_arg_id(unsigned index) { + void check_arg_id(unsigned) { const char *error = 0; if (!check_no_auto_index(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; } -// Parses an unsigned integer advancing it to the end of the parsed input. -// This function assumes that the first character of it is a digit and a -// presence of a non-digit character at the end. -template -constexpr unsigned parse_nonnegative_int(Iterator &it) { +// Parses the input as an unsigned integer. This function assumes that the +// first character is a digit and presence of a non-digit character at the end. +// it: an iterator pointing to the beginning of the input range. +template +constexpr unsigned parse_nonnegative_int(Iterator &it, ErrorHandler& handler) { assert('0' <= *it && *it <= '9'); unsigned value = 0; do { @@ -3039,7 +3039,7 @@ constexpr unsigned parse_nonnegative_int(Iterator &it) { // Convert to unsigned to prevent a warning. unsigned max_int = (std::numeric_limits::max)(); if (value > max_int) - FMT_THROW(format_error("number is too big")); + handler.on_error("number is too big"); return value; } @@ -3341,7 +3341,7 @@ constexpr Iterator parse_arg_id(Iterator it, Handler& handler) { return it; } if (c >= '0' && c <= '9') { - unsigned index = parse_nonnegative_int(it); + unsigned index = parse_nonnegative_int(it, handler); if (*it != '}' && *it != ':') { handler.on_error("invalid format string"); return it; @@ -3464,7 +3464,7 @@ constexpr Iterator parse_format_specs(Iterator it, Handler &handler) { // Parse width. if ('0' <= *it && *it <= '9') { - handler.on_width(parse_nonnegative_int(it)); + handler.on_width(parse_nonnegative_int(it, handler)); } else if (*it == '{') { width_handler wh(handler); it = parse_arg_id(it + 1, wh); @@ -3478,7 +3478,7 @@ constexpr Iterator parse_format_specs(Iterator it, Handler &handler) { if (*it == '.') { ++it; if ('0' <= *it && *it <= '9') { - handler.on_precision(parse_nonnegative_int(it)); + handler.on_precision(parse_nonnegative_int(it, handler)); } else if (*it == '{') { precision_handler ph(handler); it = parse_arg_id(it + 1, ph); diff --git a/include/fmt/printf.h b/include/fmt/printf.h index eb9e39db..a8533ef2 100644 --- a/include/fmt/printf.h +++ b/include/fmt/printf.h @@ -389,7 +389,8 @@ unsigned printf_context::parse_header( if (c >= '0' && c <= '9') { // Parse an argument index (if followed by '$') or a width possibly // 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 ++it; arg_index = value; @@ -407,7 +408,8 @@ unsigned printf_context::parse_header( parse_flags(spec, it); // Parse width. 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 == '*') { ++it; spec.width_ = visit(internal::PrintfWidthHandler(spec), get_arg(it)); @@ -441,7 +443,8 @@ void printf_context::format(basic_buffer &buffer) { if (*it == '.') { ++it; if ('0' <= *it && *it <= '9') { - spec.precision_ = static_cast(internal::parse_nonnegative_int(it)); + internal::error_handler eh; + spec.precision_ = static_cast(parse_nonnegative_int(it, eh)); } else if (*it == '*') { ++it; spec.precision_ =