mirror of
https://github.com/fmtlib/fmt.git
synced 2024-12-24 12:14:26 +00:00
Factor out argument processing logic common between format and printf to FormatterBase::{next_arg,get_arg}.
This commit is contained in:
parent
56fc525e98
commit
c57d7a506f
63
format.cc
63
format.cc
@ -148,8 +148,6 @@ void report_error(FormatFunc func,
|
||||
} catch (...) {}
|
||||
}
|
||||
|
||||
const Arg DUMMY_ARG = {Arg::INT, {0}};
|
||||
|
||||
// IsZeroInt::visit(arg) returns true iff arg is a zero integer.
|
||||
class IsZeroInt : public fmt::internal::ArgVisitor<IsZeroInt, bool> {
|
||||
public:
|
||||
@ -752,20 +750,9 @@ void fmt::BasicWriter<Char>::write_str(
|
||||
|
||||
template <typename Char>
|
||||
inline const Arg &fmt::BasicFormatter<Char>::parse_arg_index(const Char *&s) {
|
||||
const Arg *arg = 0;
|
||||
const char *error = 0;
|
||||
if (*s < '0' || *s > '9') {
|
||||
arg = &next_arg(error);
|
||||
} else {
|
||||
if (next_arg_index_ > 0)
|
||||
error = "cannot switch from automatic to manual argument indexing";
|
||||
next_arg_index_ = -1;
|
||||
unsigned arg_index = parse_nonnegative_int(s);
|
||||
if (arg_index < args_.size())
|
||||
arg = &args_[arg_index];
|
||||
else if (!error)
|
||||
error = "argument index is out of range in format";
|
||||
}
|
||||
const Arg *arg = *s < '0' || *s > '9' ?
|
||||
next_arg(error) : get_arg(parse_nonnegative_int(s), error);
|
||||
if (error)
|
||||
throw FormatError(*s != '}' && *s != ':' ? "invalid format string" : error);
|
||||
return *arg;
|
||||
@ -786,36 +773,29 @@ void fmt::BasicFormatter<Char>::check_sign(
|
||||
++s;
|
||||
}
|
||||
|
||||
const Arg &fmt::internal::FormatterBase::next_arg(const char *&error) {
|
||||
const Arg *fmt::internal::FormatterBase::next_arg(const char *&error) {
|
||||
if (next_arg_index_ < 0) {
|
||||
error = "cannot switch from manual to automatic argument indexing";
|
||||
return DUMMY_ARG;
|
||||
return 0;
|
||||
}
|
||||
unsigned arg_index = next_arg_index_++;
|
||||
if (arg_index < args_.size())
|
||||
return args_[arg_index];
|
||||
return &args_[arg_index];
|
||||
error = "argument index is out of range in format";
|
||||
return DUMMY_ARG;
|
||||
return 0;
|
||||
}
|
||||
|
||||
const Arg &fmt::internal::FormatterBase::handle_arg_index(unsigned arg_index) {
|
||||
const char *error = 0;
|
||||
const Arg *arg = 0;
|
||||
if (arg_index == UINT_MAX) {
|
||||
arg = &next_arg(error);
|
||||
} else {
|
||||
if (next_arg_index_ > 0)
|
||||
error = "cannot switch from automatic to manual argument indexing";
|
||||
next_arg_index_ = -1;
|
||||
--arg_index;
|
||||
if (arg_index < args_.size())
|
||||
arg = &args_[arg_index];
|
||||
else if (!error)
|
||||
error = "argument index is out of range in format";
|
||||
const Arg *fmt::internal::FormatterBase::get_arg(
|
||||
unsigned arg_index, const char *&error) {
|
||||
if (next_arg_index_ > 0) {
|
||||
error = "cannot switch from automatic to manual argument indexing";
|
||||
return 0;
|
||||
}
|
||||
if (error)
|
||||
throw FormatError(error);
|
||||
return *arg;
|
||||
next_arg_index_ = -1;
|
||||
if (arg_index < args_.size())
|
||||
return &args_[arg_index];
|
||||
error = "argument index is out of range in format";
|
||||
return 0;
|
||||
}
|
||||
|
||||
template <typename Char>
|
||||
@ -845,6 +825,17 @@ void fmt::internal::PrintfFormatter<Char>::parse_flags(
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Char>
|
||||
const Arg &fmt::internal::PrintfFormatter<Char>::get_arg(
|
||||
const Char *s, unsigned arg_index) {
|
||||
const char *error = 0;
|
||||
const Arg *arg = arg_index == UINT_MAX ?
|
||||
next_arg(error) : FormatterBase::get_arg(arg_index - 1, error);
|
||||
if (error)
|
||||
throw FormatError(!*s ? "invalid format string" : error);
|
||||
return *arg;
|
||||
}
|
||||
|
||||
template <typename Char>
|
||||
unsigned fmt::internal::PrintfFormatter<Char>::parse_header(
|
||||
const Char *&s, FormatSpec &spec) {
|
||||
|
20
format.h
20
format.h
@ -846,11 +846,11 @@ protected:
|
||||
ArgList args_;
|
||||
int next_arg_index_;
|
||||
|
||||
const Arg &next_arg(const char *&error);
|
||||
// Returns the next argument.
|
||||
const Arg *next_arg(const char *&error);
|
||||
|
||||
// Returns the argument with specified index or, if arg_index is equal
|
||||
// to the maximum unsigned value, the next argument.
|
||||
const Arg &handle_arg_index(unsigned arg_index);
|
||||
// Returns the argument with specified index.
|
||||
const Arg *get_arg(unsigned arg_index, const char *&error);
|
||||
|
||||
template <typename Char>
|
||||
void write(BasicWriter<Char> &w, const Char *start, const Char *end) {
|
||||
@ -865,16 +865,14 @@ class PrintfFormatter : private FormatterBase {
|
||||
private:
|
||||
void parse_flags(FormatSpec &spec, const Char *&s);
|
||||
|
||||
// Returns the argument with specified index or, if arg_index is equal
|
||||
// to the maximum unsigned value, the next argument.
|
||||
const Arg &get_arg(const Char *s,
|
||||
unsigned arg_index = std::numeric_limits<unsigned>::max());
|
||||
|
||||
// Parses argument index, flags and width and returns the argument index.
|
||||
unsigned parse_header(const Char *&s, FormatSpec &spec);
|
||||
|
||||
const Arg &get_arg(const Char *s,
|
||||
unsigned arg_index = std::numeric_limits<unsigned>::max()) {
|
||||
if (!*s)
|
||||
throw FormatError("invalid format string");
|
||||
return handle_arg_index(arg_index);
|
||||
}
|
||||
|
||||
public:
|
||||
void format(BasicWriter<Char> &writer,
|
||||
BasicStringRef<Char> format, const ArgList &args);
|
||||
|
Loading…
Reference in New Issue
Block a user