Minor tweaks to the named arguments support code

* Restore privacy of FormatterBase
* Pass BasicStringRef by value
* Comment on why some overloads of arg are deleted
* Set next_arg_index_ in check_no_auto_index
* A few minor stylistic changes for simplicity & consistency with other code
This commit is contained in:
vitaut 2015-06-11 07:19:00 -07:00
parent b83ab16676
commit fccff7be5f
2 changed files with 21 additions and 22 deletions

View File

@ -590,7 +590,7 @@ void fmt::internal::ArgMap<Char>::init(const ArgList &args) {
if (!map_.empty())
return;
typedef internal::NamedArg<Char> NamedArg;
const NamedArg* named_arg = 0;
const NamedArg *named_arg = 0;
bool use_values =
args.type(ArgList::MAX_PACKED_ARGS - 1) == internal::Arg::NONE;
if (use_values) {
@ -733,11 +733,10 @@ void fmt::BasicWriter<Char>::write_str(
template <typename Char>
inline Arg fmt::BasicFormatter<Char>::get_arg(
const BasicStringRef<Char>& arg_name, const char *&error) {
BasicStringRef<Char> arg_name, const char *&error) {
if (check_no_auto_index(error)) {
next_arg_index_ = -1;
map_.init(args_);
const Arg* arg = map_.find(arg_name);
map_.init(args());
const Arg *arg = map_.find(arg_name);
if (arg)
return *arg;
error = "argument not found";
@ -794,21 +793,19 @@ inline Arg fmt::internal::FormatterBase::next_arg(const char *&error) {
return Arg();
}
inline bool fmt::internal::FormatterBase::check_no_auto_index(const char *&error) {
inline bool fmt::internal::FormatterBase::check_no_auto_index(
const char *&error) {
if (next_arg_index_ > 0) {
error = "cannot switch from automatic to manual argument indexing";
return false;
}
next_arg_index_ = -1;
return true;
}
inline Arg fmt::internal::FormatterBase::get_arg(
unsigned arg_index, const char *&error) {
if (check_no_auto_index(error)) {
next_arg_index_ = -1;
return do_get_arg(arg_index, error);
}
return Arg();
return check_no_auto_index(error) ? do_get_arg(arg_index, error) : Arg();
}
template <typename Char>
@ -1131,7 +1128,8 @@ const Char *fmt::BasicFormatter<Char>::format(
spec.width_ = parse_nonnegative_int(s);
} else if (*s == '{') {
++s;
const Arg &width_arg = is_name_start(*s) ? parse_arg_name(s) : parse_arg_index(s);
Arg width_arg = is_name_start(*s) ?
parse_arg_name(s) : parse_arg_index(s);
if (*s++ != '}')
FMT_THROW(FormatError("invalid format string"));
ULongLong value = 0;
@ -1168,7 +1166,8 @@ const Char *fmt::BasicFormatter<Char>::format(
spec.precision_ = parse_nonnegative_int(s);
} else if (*s == '{') {
++s;
const Arg &precision_arg = is_name_start(*s) ? parse_arg_name(s) : parse_arg_index(s);
Arg precision_arg =
is_name_start(*s) ? parse_arg_name(s) : parse_arg_index(s);
if (*s++ != '}')
FMT_THROW(FormatError("invalid format string"));
ULongLong value = 0;

View File

@ -1197,14 +1197,12 @@ class ArgMap {
const internal::Arg* find(const fmt::BasicStringRef<Char> &name) const {
typename MapType::const_iterator it = map_.find(name);
if (it != map_.end())
return &it->second;
return 0;
return it != map_.end() ? &it->second : 0;
}
};
class FormatterBase {
protected:
private:
ArgList args_;
int next_arg_index_;
@ -1212,6 +1210,8 @@ class FormatterBase {
Arg do_get_arg(unsigned arg_index, const char *&error);
protected:
const ArgList &args() const { return args_; }
void set_args(const ArgList &args) {
args_ = args;
next_arg_index_ = 0;
@ -1267,8 +1267,7 @@ class BasicFormatter : private internal::FormatterBase {
// Checks if manual indexing is used and returns the argument with
// specified name.
internal::Arg get_arg(const BasicStringRef<Char>& arg_name,
const char *&error);
internal::Arg get_arg(BasicStringRef<Char> arg_name, const char *&error);
// Parses argument index and returns corresponding argument.
internal::Arg parse_arg_index(const Char *&s);
@ -2762,11 +2761,12 @@ inline internal::NamedArg<wchar_t> arg(WStringRef name, const T &arg) {
return internal::NamedArg<wchar_t>(name, arg);
}
// The following two functions are deleted intentionally to disable
// nested named arguments as in ``format("{}", arg("a", arg("b", 42)))``.
template <typename Char>
void arg(StringRef name, const internal::NamedArg<Char>&) FMT_DELETED_OR_UNDEFINED;
void arg(StringRef, const internal::NamedArg<Char>&) FMT_DELETED_OR_UNDEFINED;
template <typename Char>
void arg(WStringRef name, const internal::NamedArg<Char>&) FMT_DELETED_OR_UNDEFINED;
void arg(WStringRef, const internal::NamedArg<Char>&) FMT_DELETED_OR_UNDEFINED;
}
#if FMT_GCC_VERSION