custom printfArgFormatter template param

This commit is contained in:
Glen Stark 2016-05-27 16:28:13 +02:00
parent 96c28f748d
commit 321a541ee9
4 changed files with 66 additions and 19 deletions

View File

@ -686,8 +686,8 @@ FMT_FUNC Arg fmt::internal::FormatterBase::do_get_arg(
return arg; return arg;
} }
template <typename Char> template <typename Char, typename PAF>
void fmt::internal::PrintfFormatter<Char>::parse_flags( void fmt::internal::PrintfFormatter<Char, PAF>::parse_flags(
FormatSpec &spec, const Char *&s) { FormatSpec &spec, const Char *&s) {
for (;;) { for (;;) {
switch (*s++) { switch (*s++) {
@ -713,8 +713,8 @@ void fmt::internal::PrintfFormatter<Char>::parse_flags(
} }
} }
template <typename Char> template <typename Char, typename PAF>
Arg fmt::internal::PrintfFormatter<Char>::get_arg( Arg fmt::internal::PrintfFormatter<Char, PAF>::get_arg(
const Char *s, unsigned arg_index) { const Char *s, unsigned arg_index) {
(void)s; (void)s;
const char *error = 0; const char *error = 0;
@ -725,8 +725,8 @@ Arg fmt::internal::PrintfFormatter<Char>::get_arg(
return arg; return arg;
} }
template <typename Char> template <typename Char, typename PAF>
unsigned fmt::internal::PrintfFormatter<Char>::parse_header( unsigned fmt::internal::PrintfFormatter<Char, PAF>::parse_header(
const Char *&s, FormatSpec &spec) { const Char *&s, FormatSpec &spec) {
unsigned arg_index = UINT_MAX; unsigned arg_index = UINT_MAX;
Char c = *s; Char c = *s;
@ -759,8 +759,8 @@ unsigned fmt::internal::PrintfFormatter<Char>::parse_header(
return arg_index; return arg_index;
} }
template <typename Char> template <typename Char, typename PAF >
void fmt::internal::PrintfFormatter<Char>::format( void fmt::internal::PrintfFormatter<Char,PAF>::format(
BasicWriter<Char> &writer, BasicCStringRef<Char> format_str) { BasicWriter<Char> &writer, BasicCStringRef<Char> format_str) {
const Char *start = format_str.c_str(); const Char *start = format_str.c_str();
const Char *s = start; const Char *s = start;
@ -853,11 +853,12 @@ void fmt::internal::PrintfFormatter<Char>::format(
start = s; start = s;
// Format argument. // Format argument.
internal::PrintfArgFormatter<Char>(writer, spec).visit(arg); PAF(writer, spec).visit(arg);
} }
write(writer, start, s); write(writer, start, s);
} }
FMT_FUNC void fmt::report_system_error( FMT_FUNC void fmt::report_system_error(
int error_code, fmt::StringRef message) FMT_NOEXCEPT { int error_code, fmt::StringRef message) FMT_NOEXCEPT {
// 'fmt::' is for bcc32. // 'fmt::' is for bcc32.

View File

@ -378,6 +378,7 @@ class BasicWriter;
typedef BasicWriter<char> Writer; typedef BasicWriter<char> Writer;
typedef BasicWriter<wchar_t> WWriter; typedef BasicWriter<wchar_t> WWriter;
// Define default ArgFormatter and BasicFormatter.
template <typename Char> template <typename Char>
class ArgFormatter; class ArgFormatter;
@ -1939,7 +1940,7 @@ class FormatterBase {
}; };
// A printf formatter. // A printf formatter.
template <typename Char> template <typename Char, typename PAF = fmt::internal::PrintfArgFormatter<Char> >
class PrintfFormatter : private FormatterBase { class PrintfFormatter : private FormatterBase {
private: private:
void parse_flags(FormatSpec &spec, const Char *&s); void parse_flags(FormatSpec &spec, const Char *&s);
@ -3187,9 +3188,9 @@ FMT_API void print(std::FILE *f, CStringRef format_str, ArgList args);
*/ */
FMT_API void print(CStringRef format_str, ArgList args); FMT_API void print(CStringRef format_str, ArgList args);
template <typename Char> template <typename Char>
void printf(BasicWriter<Char> &w, BasicCStringRef<Char> format, ArgList args) { void printf(BasicWriter<Char> &w, BasicCStringRef<Char> format, ArgList args) {
internal::PrintfFormatter<Char>(args).format(w, format); internal::PrintfFormatter<Char>(args).format(w, format);
} }
/** /**
@ -3815,8 +3816,10 @@ struct UdlArg {
} }
}; };
} // namespace internal } // namespace internal
inline namespace literals { inline namespace literals {
/** /**
@ -3850,6 +3853,7 @@ inline internal::UdlArg<wchar_t>
operator"" _a(const wchar_t *s, std::size_t) { return {s}; } operator"" _a(const wchar_t *s, std::size_t) { return {s}; }
} // inline namespace literals } // inline namespace literals
} // namespace fmt } // namespace fmt
#endif // FMT_USE_USER_DEFINED_LITERALS #endif // FMT_USE_USER_DEFINED_LITERALS

View File

@ -80,6 +80,7 @@ add_fmt_test(printf-test)
add_fmt_test(string-test) add_fmt_test(string-test)
add_fmt_test(util-test mock-allocator.h) add_fmt_test(util-test mock-allocator.h)
add_fmt_test(macro-test) add_fmt_test(macro-test)
add_fmt_test(custom-formatter-test)
# Enable stricter options for one test to make sure that the header is free of # Enable stricter options for one test to make sure that the header is free of
# warnings. # warnings.

View File

@ -0,0 +1,41 @@
#include "fmt/format.h"
#include <iostream>
// A custom argument formatter that doesn't print `-` for floating-point values
// rounded to 0.
class CustomArgFormatter :
public fmt::BasicArgFormatter<CustomArgFormatter, char> {
public:
CustomArgFormatter(fmt::BasicFormatter<char, CustomArgFormatter> &f,
fmt::FormatSpec &s, const char *fmt)
: fmt::BasicArgFormatter<CustomArgFormatter, char>(f, s, fmt) {}
void visit_double(double value) {
if (round(value * pow(10, spec().precision())) == 0)
value = 0;
fmt::BasicArgFormatter<CustomArgFormatter, char>::visit_double(value);
}
};
std::string custom_format(const char *format_str, fmt::ArgList args) {
fmt::MemoryWriter writer;
// Pass custom argument formatter as a template arg to BasicFormatter.
fmt::BasicFormatter<char, CustomArgFormatter> formatter(args, writer);
formatter.format(format_str);
return writer.str();
}
FMT_VARIADIC(std::string, custom_format, const char *)
std::string printfer(const char* fstr, fmt::ArgList args){
fmt::MemoryWriter writer;
fmt::internal::PrintfFormatter<char> pfer(args);
pfer.format(writer,fstr);
return writer.str();
}
FMT_VARIADIC(std::string, printfer, const char*);
int main() {
std::cout << custom_format("custom: {:.2f}", -0.000001) << std::endl;
std::cout << printfer("printf: %.2f", -0.0001) << std::endl;
}