diff --git a/fmt/format.h b/fmt/format.h index e012bba1..6ed68352 100644 --- a/fmt/format.h +++ b/fmt/format.h @@ -1494,8 +1494,8 @@ class basic_format_args { basic_format_args() : types_(0) {} - template - basic_format_args(const format_arg_store &store) + template + basic_format_args(const format_arg_store &store) : types_(store.TYPES) { set_data(store.data()); } diff --git a/fmt/ostream.h b/fmt/ostream.h index 87b80471..a941f6c9 100644 --- a/fmt/ostream.h +++ b/fmt/ostream.h @@ -69,19 +69,24 @@ struct ConvertToIntImpl { // Write the content of w to os. void write(std::ostream &os, Writer &w); + +template +BasicStringRef format_value( + internal::MemoryBuffer &buffer, + const T &value) { + internal::FormatBuf format_buf(buffer); + std::basic_ostream output(&format_buf); + output << value; + return BasicStringRef(&buffer[0], format_buf.size()); +} } // namespace internal // Formats a value. template void format_value(BasicFormatter &f, - const Char *&format_str, const T &value) { + const Char *&format_str, const T &value) { internal::MemoryBuffer buffer; - - internal::FormatBuf format_buf(buffer); - std::basic_ostream output(&format_buf); - output << value; - - BasicStringRef str(&buffer[0], format_buf.size()); + auto str = internal::format_value(buffer, value); typedef internal::MakeArg< BasicFormatter > MakeArg; format_str = f.format(format_str, MakeArg(str)); } diff --git a/fmt/printf.h b/fmt/printf.h index 4317f10b..75ae5598 100644 --- a/fmt/printf.h +++ b/fmt/printf.h @@ -281,9 +281,14 @@ class PrintfArgFormatter }; /** This template formats data and writes the output to a writer. */ -template > +template > class PrintfFormatter : - private internal::FormatterBase> { + private internal::FormatterBase> { + public: + /** The character type for the output. */ + typedef CharType Char; + private: BasicWriter &writer_; @@ -312,6 +317,8 @@ class PrintfFormatter : BasicWriter &w) : Base(args), writer_(w) {} + BasicWriter &writer() { return writer_; } + /** Formats stored arguments and writes the output to the writer. */ FMT_API void format(BasicCStringRef format_str); }; @@ -488,6 +495,13 @@ void PrintfFormatter::format(BasicCStringRef format_str) { this->write(writer_, start, s); } +// Formats a value. +template +void format_value(PrintfFormatter &f, const Char *&, const T &value) { + internal::MemoryBuffer buffer; + f.writer() << internal::format_value(buffer, value); +} + template void printf(BasicWriter &w, BasicCStringRef format, basic_format_args> args) { @@ -512,7 +526,7 @@ inline std::string vsprintf(CStringRef format, */ template inline std::string sprintf(CStringRef format_str, const Args & ... args) { - return vsprintf(format_str, make_format_args>(args...)); + return vsprintf(format_str, make_format_args>(args...)); } inline std::wstring vsprintf(WCStringRef format, @@ -524,7 +538,7 @@ inline std::wstring vsprintf(WCStringRef format, template inline std::wstring sprintf(WCStringRef format_str, const Args & ... args) { - auto vargs = make_format_args>(args...); + auto vargs = make_format_args>(args...); return vsprintf(format_str, vargs); } @@ -562,7 +576,7 @@ inline int vprintf(CStringRef format, */ template inline int printf(CStringRef format_str, const Args & ... args) { - return vprintf(format_str, make_format_args>(args...)); + return vprintf(format_str, make_format_args>(args...)); } inline int vfprintf(std::ostream &os, CStringRef format_str, @@ -585,7 +599,7 @@ inline int vfprintf(std::ostream &os, CStringRef format_str, template inline int fprintf(std::ostream &os, CStringRef format_str, const Args & ... args) { - auto vargs = make_format_args>(args...); + auto vargs = make_format_args>(args...); return vfprintf(os, format_str, vargs); } } // namespace fmt diff --git a/test/custom-formatter-test.cc b/test/custom-formatter-test.cc index b556ee87..85f307dd 100644 --- a/test/custom-formatter-test.cc +++ b/test/custom-formatter-test.cc @@ -58,7 +58,7 @@ std::string custom_vformat(const char *format_str, template std::string custom_format(const char *format_str, const Args & ... args) { - auto va = fmt::make_format_args>(args...); + auto va = fmt::make_format_args(args...); return custom_vformat(format_str, va); } @@ -76,7 +76,7 @@ std::string custom_vsprintf( template std::string custom_sprintf(const char *format_str, const Args & ... args) { - auto va = fmt::make_format_args>(args...); + auto va = fmt::make_format_args(args...); return custom_vsprintf(format_str, va); } diff --git a/test/format-test.cc b/test/format-test.cc index 616c015d..1be9a821 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1644,7 +1644,7 @@ void custom_vformat(const char *format_str, template void custom_format(const char *format_str, const Args & ... args) { - auto va = fmt::make_format_args>(args...); + auto va = fmt::make_format_args(args...); return custom_vformat(format_str, va); }