mirror of
https://github.com/fmtlib/fmt.git
synced 2025-01-07 13:20:05 +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 (...) {}
|
} catch (...) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
const Arg DUMMY_ARG = {Arg::INT, {0}};
|
|
||||||
|
|
||||||
// IsZeroInt::visit(arg) returns true iff arg is a zero integer.
|
// IsZeroInt::visit(arg) returns true iff arg is a zero integer.
|
||||||
class IsZeroInt : public fmt::internal::ArgVisitor<IsZeroInt, bool> {
|
class IsZeroInt : public fmt::internal::ArgVisitor<IsZeroInt, bool> {
|
||||||
public:
|
public:
|
||||||
@ -752,20 +750,9 @@ void fmt::BasicWriter<Char>::write_str(
|
|||||||
|
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
inline const Arg &fmt::BasicFormatter<Char>::parse_arg_index(const Char *&s) {
|
inline const Arg &fmt::BasicFormatter<Char>::parse_arg_index(const Char *&s) {
|
||||||
const Arg *arg = 0;
|
|
||||||
const char *error = 0;
|
const char *error = 0;
|
||||||
if (*s < '0' || *s > '9') {
|
const Arg *arg = *s < '0' || *s > '9' ?
|
||||||
arg = &next_arg(error);
|
next_arg(error) : get_arg(parse_nonnegative_int(s), 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";
|
|
||||||
}
|
|
||||||
if (error)
|
if (error)
|
||||||
throw FormatError(*s != '}' && *s != ':' ? "invalid format string" : error);
|
throw FormatError(*s != '}' && *s != ':' ? "invalid format string" : error);
|
||||||
return *arg;
|
return *arg;
|
||||||
@ -786,36 +773,29 @@ void fmt::BasicFormatter<Char>::check_sign(
|
|||||||
++s;
|
++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) {
|
if (next_arg_index_ < 0) {
|
||||||
error = "cannot switch from manual to automatic argument indexing";
|
error = "cannot switch from manual to automatic argument indexing";
|
||||||
return DUMMY_ARG;
|
return 0;
|
||||||
}
|
}
|
||||||
unsigned arg_index = next_arg_index_++;
|
unsigned arg_index = next_arg_index_++;
|
||||||
if (arg_index < args_.size())
|
if (arg_index < args_.size())
|
||||||
return args_[arg_index];
|
return &args_[arg_index];
|
||||||
error = "argument index is out of range in format";
|
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 Arg *fmt::internal::FormatterBase::get_arg(
|
||||||
const char *error = 0;
|
unsigned arg_index, const char *&error) {
|
||||||
const Arg *arg = 0;
|
if (next_arg_index_ > 0) {
|
||||||
if (arg_index == UINT_MAX) {
|
error = "cannot switch from automatic to manual argument indexing";
|
||||||
arg = &next_arg(error);
|
return 0;
|
||||||
} 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";
|
|
||||||
}
|
}
|
||||||
if (error)
|
next_arg_index_ = -1;
|
||||||
throw FormatError(error);
|
if (arg_index < args_.size())
|
||||||
return *arg;
|
return &args_[arg_index];
|
||||||
|
error = "argument index is out of range in format";
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Char>
|
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>
|
template <typename Char>
|
||||||
unsigned fmt::internal::PrintfFormatter<Char>::parse_header(
|
unsigned fmt::internal::PrintfFormatter<Char>::parse_header(
|
||||||
const Char *&s, FormatSpec &spec) {
|
const Char *&s, FormatSpec &spec) {
|
||||||
|
20
format.h
20
format.h
@ -846,11 +846,11 @@ protected:
|
|||||||
ArgList args_;
|
ArgList args_;
|
||||||
int next_arg_index_;
|
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
|
// Returns the argument with specified index.
|
||||||
// to the maximum unsigned value, the next argument.
|
const Arg *get_arg(unsigned arg_index, const char *&error);
|
||||||
const Arg &handle_arg_index(unsigned arg_index);
|
|
||||||
|
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
void write(BasicWriter<Char> &w, const Char *start, const Char *end) {
|
void write(BasicWriter<Char> &w, const Char *start, const Char *end) {
|
||||||
@ -865,16 +865,14 @@ class PrintfFormatter : private FormatterBase {
|
|||||||
private:
|
private:
|
||||||
void parse_flags(FormatSpec &spec, const Char *&s);
|
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.
|
// Parses argument index, flags and width and returns the argument index.
|
||||||
unsigned parse_header(const Char *&s, FormatSpec &spec);
|
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:
|
public:
|
||||||
void format(BasicWriter<Char> &writer,
|
void format(BasicWriter<Char> &writer,
|
||||||
BasicStringRef<Char> format, const ArgList &args);
|
BasicStringRef<Char> format, const ArgList &args);
|
||||||
|
Loading…
Reference in New Issue
Block a user