From fccff7be5f3ffb59557d0fc28891a97afbb5b03f Mon Sep 17 00:00:00 2001 From: vitaut Date: Thu, 11 Jun 2015 07:19:00 -0700 Subject: [PATCH] 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 --- format.cc | 25 ++++++++++++------------- format.h | 18 +++++++++--------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/format.cc b/format.cc index 92291c1b..e8714a33 100644 --- a/format.cc +++ b/format.cc @@ -590,7 +590,7 @@ void fmt::internal::ArgMap::init(const ArgList &args) { if (!map_.empty()) return; typedef internal::NamedArg 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::write_str( template inline Arg fmt::BasicFormatter::get_arg( - const BasicStringRef& arg_name, const char *&error) { + BasicStringRef 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 @@ -1131,7 +1128,8 @@ const Char *fmt::BasicFormatter::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::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; diff --git a/format.h b/format.h index 2c73d7cd..0515b516 100644 --- a/format.h +++ b/format.h @@ -1197,14 +1197,12 @@ class ArgMap { const internal::Arg* find(const fmt::BasicStringRef &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& arg_name, - const char *&error); + internal::Arg get_arg(BasicStringRef 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 arg(WStringRef name, const T &arg) { return internal::NamedArg(name, arg); } +// The following two functions are deleted intentionally to disable +// nested named arguments as in ``format("{}", arg("a", arg("b", 42)))``. template -void arg(StringRef name, const internal::NamedArg&) FMT_DELETED_OR_UNDEFINED; - +void arg(StringRef, const internal::NamedArg&) FMT_DELETED_OR_UNDEFINED; template -void arg(WStringRef name, const internal::NamedArg&) FMT_DELETED_OR_UNDEFINED; +void arg(WStringRef, const internal::NamedArg&) FMT_DELETED_OR_UNDEFINED; } #if FMT_GCC_VERSION