Commit f1dfd59a authored by Victor Zverovich's avatar Victor Zverovich

HandleArgIndex -> handle_arg_index. Use error_ instead of a local.

parent f86c812e
...@@ -681,10 +681,9 @@ inline const Arg ...@@ -681,10 +681,9 @@ inline const Arg
"cannot switch from automatic to manual argument indexing"); "cannot switch from automatic to manual argument indexing");
} }
next_arg_index_ = -1; next_arg_index_ = -1;
const char *error = 0; arg_index = ParseNonnegativeInt(s, error_);
arg_index = ParseNonnegativeInt(s, error); if (error_)
if (error) report_error_(s, error_); // TODO: don't use report_error_
report_error_(s, error); // TODO
if (arg_index >= args_.size()) if (arg_index >= args_.size())
report_error_(s, "argument index is out of range in format"); report_error_(s, "argument index is out of range in format");
return args_[arg_index]; return args_[arg_index];
...@@ -719,25 +718,21 @@ const Arg &fmt::internal::FormatterBase::next_arg() { ...@@ -719,25 +718,21 @@ const Arg &fmt::internal::FormatterBase::next_arg() {
return DUMMY_ARG; return DUMMY_ARG;
} }
const Arg &fmt::internal::FormatterBase::HandleArgIndex( const Arg &fmt::internal::FormatterBase::handle_arg_index(unsigned arg_index) {
unsigned arg_index, const char *&error) {
if (arg_index != UINT_MAX) { if (arg_index != UINT_MAX) {
if (next_arg_index_ <= 0) { if (next_arg_index_ <= 0) {
next_arg_index_ = -1; next_arg_index_ = -1;
--arg_index; --arg_index;
} else if (!error) { } else if (!error_) {
error = "cannot switch from automatic to manual argument indexing"; 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()) if (arg_index < args_.size())
return args_[arg_index]; return args_[arg_index];
if (!error) if (!error_)
error = "argument index is out of range in format"; error_ = "argument index is out of range in format";
return DUMMY_ARG; return DUMMY_ARG;
}
return next_arg();
} }
template <typename Char> template <typename Char>
...@@ -769,13 +764,13 @@ void fmt::internal::PrintfFormatter<Char>::ParseFlags( ...@@ -769,13 +764,13 @@ void fmt::internal::PrintfFormatter<Char>::ParseFlags(
template <typename Char> template <typename Char>
unsigned fmt::internal::PrintfFormatter<Char>::ParseHeader( unsigned fmt::internal::PrintfFormatter<Char>::ParseHeader(
const Char *&s, FormatSpec &spec, const char *&error) { const Char *&s, FormatSpec &spec) {
unsigned arg_index = UINT_MAX; unsigned arg_index = UINT_MAX;
Char c = *s; Char c = *s;
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 = ParseNonnegativeInt(s, error); unsigned value = ParseNonnegativeInt(s, error_);
if (*s == '$') { // value is an argument index if (*s == '$') { // value is an argument index
++s; ++s;
arg_index = value; arg_index = value;
...@@ -793,10 +788,10 @@ unsigned fmt::internal::PrintfFormatter<Char>::ParseHeader( ...@@ -793,10 +788,10 @@ unsigned fmt::internal::PrintfFormatter<Char>::ParseHeader(
ParseFlags(spec, s); ParseFlags(spec, s);
// Parse width. // Parse width.
if (*s >= '0' && *s <= '9') { if (*s >= '0' && *s <= '9') {
spec.width_ = ParseNonnegativeInt(s, error); spec.width_ = ParseNonnegativeInt(s, error_);
} else if (*s == '*') { } else if (*s == '*') {
++s; ++s;
spec.width_ = WidthHandler(spec).visit(HandleArgIndex(UINT_MAX, error)); spec.width_ = WidthHandler(spec).visit(handle_arg_index(UINT_MAX));
} }
return arg_index; return arg_index;
} }
...@@ -826,33 +821,32 @@ void fmt::internal::PrintfFormatter<Char>::Format( ...@@ -826,33 +821,32 @@ void fmt::internal::PrintfFormatter<Char>::Format(
// completely parsed. This is done to avoid potentially confusing // completely parsed. This is done to avoid potentially confusing
// error messages for incomplete format strings. For example, in // error messages for incomplete format strings. For example, in
// sprintf("%2$", 42); // 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 // 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 // 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 // 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 // user will get an error that the format string is invalid which
// is OK for both cases. // is OK for both cases.
const char *error = 0;
// Parse argument index, flags and width. // Parse argument index, flags and width.
unsigned arg_index = ParseHeader(s, spec, error); unsigned arg_index = ParseHeader(s, spec);
// Parse precision. // Parse precision.
if (*s == '.') { if (*s == '.') {
++s; ++s;
if ('0' <= *s && *s <= '9') { if ('0' <= *s && *s <= '9') {
spec.precision_ = ParseNonnegativeInt(s, error); spec.precision_ = ParseNonnegativeInt(s, error_);
} else if (*s == '*') { } else if (*s == '*') {
++s; ++s;
const Arg &arg = HandleArgIndex(UINT_MAX, error); const Arg &arg = handle_arg_index(UINT_MAX);
if (arg.type <= Arg::LAST_INTEGER_TYPE) if (arg.type <= Arg::LAST_INTEGER_TYPE)
spec.precision_ = static_cast<int>(GetIntValue(arg)); // TODO: check for overflow spec.precision_ = static_cast<int>(GetIntValue(arg)); // TODO: check for overflow
else if (!error) else if (!error_)
error = "precision is not integer"; 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) if (spec.hash_flag() && GetIntValue(arg) == 0)
spec.flags_ &= ~HASH_FLAG; spec.flags_ &= ~HASH_FLAG;
if (spec.fill_ == '0') { if (spec.fill_ == '0') {
...@@ -879,8 +873,8 @@ void fmt::internal::PrintfFormatter<Char>::Format( ...@@ -879,8 +873,8 @@ void fmt::internal::PrintfFormatter<Char>::Format(
// Parse type. // Parse type.
if (!*s) if (!*s)
throw FormatError("invalid format string"); throw FormatError("invalid format string");
if (error) if (error_)
throw FormatError(error); throw FormatError(error_);
spec.type_ = static_cast<char>(*s++); spec.type_ = static_cast<char>(*s++);
start = s; start = s;
......
...@@ -851,7 +851,7 @@ protected: ...@@ -851,7 +851,7 @@ protected:
const Arg &next_arg(); const Arg &next_arg();
const Arg &HandleArgIndex(unsigned arg_index, const char *&error); const Arg &handle_arg_index(unsigned arg_index);
// TODO // TODO
}; };
...@@ -864,7 +864,7 @@ class PrintfFormatter : private FormatterBase { ...@@ -864,7 +864,7 @@ class PrintfFormatter : private FormatterBase {
// Parses argument index, flags and width and returns the parsed // Parses argument index, flags and width and returns the parsed
// argument index. // argument index.
unsigned ParseHeader(const Char *&s, FormatSpec &spec, const char *&error); unsigned ParseHeader(const Char *&s, FormatSpec &spec);
public: public:
void Format(BasicWriter<Char> &writer, void Format(BasicWriter<Char> &writer,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment