diff --git a/format.cc b/format.cc index 673cc2a8..bf355910 100644 --- a/format.cc +++ b/format.cc @@ -175,19 +175,6 @@ int parse_nonnegative_int(const Char *&s) { return value; } -template -const Char *find_closing_brace(const Char *s, int num_open_braces = 1) { - for (int n = num_open_braces; *s; ++s) { - if (*s == '{') { - ++n; - } else if (*s == '}') { - if (--n == 0) - return s; - } - } - throw fmt::FormatError("unmatched '{' in format"); -} - template void check_sign(const Char *&s, const Arg &arg) { char sign = static_cast(*s); @@ -574,7 +561,7 @@ class fmt::internal::ArgFormatter : } void visit_custom(Arg::CustomValue c) { - c.format(&formatter_, c.value, format_); + c.format(&formatter_, c.value, &format_); } }; @@ -1019,11 +1006,13 @@ void fmt::internal::PrintfFormatter::format( spec.type_ = 'x'; writer.write_int(reinterpret_cast(arg.pointer_value), spec); break; - case Arg::CUSTOM: + case Arg::CUSTOM: { if (spec.type_) internal::report_unknown_type(spec.type_, "object"); - arg.custom.format(&writer, arg.custom.value, "s"); + const void *s = "s"; + arg.custom.format(&writer, arg.custom.value, &s); break; + } default: assert(false); break; @@ -1034,14 +1023,14 @@ void fmt::internal::PrintfFormatter::format( template const Char *fmt::BasicFormatter::format( - const Char *format_str, const Arg &arg) { + const Char *&format_str, const Arg &arg) { const Char *s = format_str; const char *error = 0; FormatSpec spec; if (*s == ':') { if (arg.type == Arg::CUSTOM) { - arg.custom.format(this, arg.custom.value, s); - return find_closing_brace(s) + 1; + arg.custom.format(this, arg.custom.value, &s); + return s; } ++s; // Parse fill and alignment. diff --git a/format.h b/format.h index 6ee80112..ed681fb6 100644 --- a/format.h +++ b/format.h @@ -132,7 +132,7 @@ template class BasicFormatter; template -void format(BasicFormatter &f, const Char *format_str, const T &value); +void format(BasicFormatter &f, const Char *&format_str, const T &value); /** \rst @@ -583,7 +583,7 @@ struct Arg { }; typedef void (*FormatFunc)( - void *formatter, const void *arg, const void *format_str); + void *formatter, const void *arg, void *format_str_ptr); struct CustomValue { const void *value; @@ -634,9 +634,9 @@ class MakeArg : public Arg { // Formats an argument of a custom type, such as a user-defined class. template static void format_custom_arg( - void *formatter, const void *arg, const void *format_str) { + void *formatter, const void *arg, void *format_str_ptr) { format(*static_cast*>(formatter), - static_cast(format_str), *static_cast(arg)); + *static_cast(format_str_ptr), *static_cast(arg)); } public: @@ -896,7 +896,7 @@ public: void format(BasicStringRef format_str, const ArgList &args); - const Char *format(const Char *format_str, const internal::Arg &arg); + const Char *format(const Char *&format_str, const internal::Arg &arg); }; enum Alignment { @@ -1681,10 +1681,10 @@ void BasicWriter::write_int(T value, const Spec &spec) { // Formats a value. template -void format(BasicFormatter &f, const Char *format_str, const T &value) { +void format(BasicFormatter &f, const Char *&format_str, const T &value) { std::basic_ostringstream os; os << value; - f.format(format_str, internal::MakeArg(os.str())); + format_str = f.format(format_str, internal::MakeArg(os.str())); } // Reports a system error without throwing an exception. diff --git a/test/util-test.cc b/test/util-test.cc index 7f4fd84e..b0b80766 100644 --- a/test/util-test.cc +++ b/test/util-test.cc @@ -238,7 +238,8 @@ TEST(ArgTest, MakeArg) { EXPECT_EQ(&t, arg.custom.value); fmt::Writer w; fmt::BasicFormatter formatter(w); - arg.custom.format(&formatter, &t, "}"); + const char *s = "}"; + arg.custom.format(&formatter, &t, &s); EXPECT_EQ("test", w.str()); }