From f1dfd59a410dda848ec8cdb2a3bb764ac5eb6ab2 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 16 Jul 2014 08:49:23 -0700 Subject: [PATCH] HandleArgIndex -> handle_arg_index. Use error_ instead of a local. --- format.cc | 56 +++++++++++++++++++++++++------------------------------ format.h | 4 ++-- 2 files changed, 27 insertions(+), 33 deletions(-) diff --git a/format.cc b/format.cc index 46c5de42..8cb43450 100644 --- a/format.cc +++ b/format.cc @@ -681,10 +681,9 @@ inline const Arg "cannot switch from automatic to manual argument indexing"); } next_arg_index_ = -1; - const char *error = 0; - arg_index = ParseNonnegativeInt(s, error); - if (error) - report_error_(s, error); // TODO + arg_index = ParseNonnegativeInt(s, error_); + if (error_) + report_error_(s, error_); // TODO: don't use report_error_ if (arg_index >= args_.size()) report_error_(s, "argument index is out of range in format"); return args_[arg_index]; @@ -719,25 +718,21 @@ const Arg &fmt::internal::FormatterBase::next_arg() { return DUMMY_ARG; } -const Arg &fmt::internal::FormatterBase::HandleArgIndex( - unsigned arg_index, const char *&error) { +const Arg &fmt::internal::FormatterBase::handle_arg_index(unsigned arg_index) { if (arg_index != UINT_MAX) { if (next_arg_index_ <= 0) { next_arg_index_ = -1; --arg_index; - } else if (!error) { - error = "cannot switch from automatic to manual argument indexing"; + } else if (!error_) { + error_ = "cannot switch from automatic to manual argument indexing"; } - } else if (next_arg_index_ >= 0) { - arg_index = next_arg_index_++; - } else if (!error) { - error = "cannot switch from manual to automatic argument indexing"; + if (arg_index < args_.size()) + return args_[arg_index]; + if (!error_) + error_ = "argument index is out of range in format"; + return DUMMY_ARG; } - if (arg_index < args_.size()) - return args_[arg_index]; - if (!error) - error = "argument index is out of range in format"; - return DUMMY_ARG; + return next_arg(); } template @@ -769,13 +764,13 @@ void fmt::internal::PrintfFormatter::ParseFlags( template unsigned fmt::internal::PrintfFormatter::ParseHeader( - const Char *&s, FormatSpec &spec, const char *&error) { + const Char *&s, FormatSpec &spec) { unsigned arg_index = UINT_MAX; Char c = *s; if (c >= '0' && c <= '9') { // Parse an argument index (if followed by '$') or a width possibly // preceded with '0' flag(s). - unsigned value = ParseNonnegativeInt(s, error); + unsigned value = ParseNonnegativeInt(s, error_); if (*s == '$') { // value is an argument index ++s; arg_index = value; @@ -793,10 +788,10 @@ unsigned fmt::internal::PrintfFormatter::ParseHeader( ParseFlags(spec, s); // Parse width. if (*s >= '0' && *s <= '9') { - spec.width_ = ParseNonnegativeInt(s, error); + spec.width_ = ParseNonnegativeInt(s, error_); } else if (*s == '*') { ++s; - spec.width_ = WidthHandler(spec).visit(HandleArgIndex(UINT_MAX, error)); + spec.width_ = WidthHandler(spec).visit(handle_arg_index(UINT_MAX)); } return arg_index; } @@ -826,33 +821,32 @@ void fmt::internal::PrintfFormatter::Format( // completely parsed. This is done to avoid potentially confusing // error messages for incomplete format strings. For example, in // sprintf("%2$", 42); - // the format specification is incomplete. In naive approach we + // the format specification is incomplete. In a naive approach we // would parse 2 as an argument index and report an error that the // index is out of range which would be rather confusing if the // use meant "%2d$" rather than "%2$d". If we delay an error, the // user will get an error that the format string is invalid which // is OK for both cases. - const char *error = 0; // Parse argument index, flags and width. - unsigned arg_index = ParseHeader(s, spec, error); + unsigned arg_index = ParseHeader(s, spec); // Parse precision. if (*s == '.') { ++s; if ('0' <= *s && *s <= '9') { - spec.precision_ = ParseNonnegativeInt(s, error); + spec.precision_ = ParseNonnegativeInt(s, error_); } else if (*s == '*') { ++s; - const Arg &arg = HandleArgIndex(UINT_MAX, error); + const Arg &arg = handle_arg_index(UINT_MAX); if (arg.type <= Arg::LAST_INTEGER_TYPE) spec.precision_ = static_cast(GetIntValue(arg)); // TODO: check for overflow - else if (!error) - error = "precision is not integer"; + else if (!error_) + error_ = "precision is not integer"; } } - const Arg &arg = HandleArgIndex(arg_index, error); + const Arg &arg = handle_arg_index(arg_index); if (spec.hash_flag() && GetIntValue(arg) == 0) spec.flags_ &= ~HASH_FLAG; if (spec.fill_ == '0') { @@ -879,8 +873,8 @@ void fmt::internal::PrintfFormatter::Format( // Parse type. if (!*s) throw FormatError("invalid format string"); - if (error) - throw FormatError(error); + if (error_) + throw FormatError(error_); spec.type_ = static_cast(*s++); start = s; diff --git a/format.h b/format.h index d369d3a2..342b8daa 100644 --- a/format.h +++ b/format.h @@ -851,7 +851,7 @@ protected: const Arg &next_arg(); - const Arg &HandleArgIndex(unsigned arg_index, const char *&error); + const Arg &handle_arg_index(unsigned arg_index); // TODO }; @@ -864,7 +864,7 @@ class PrintfFormatter : private FormatterBase { // Parses argument index, flags and width and returns the parsed // argument index. - unsigned ParseHeader(const Char *&s, FormatSpec &spec, const char *&error); + unsigned ParseHeader(const Char *&s, FormatSpec &spec); public: void Format(BasicWriter &writer,