custom printfArgFormatter template param
This commit is contained in:
parent
96c28f748d
commit
321a541ee9
@ -686,8 +686,8 @@ FMT_FUNC Arg fmt::internal::FormatterBase::do_get_arg(
|
||||
return arg;
|
||||
}
|
||||
|
||||
template <typename Char>
|
||||
void fmt::internal::PrintfFormatter<Char>::parse_flags(
|
||||
template <typename Char, typename PAF>
|
||||
void fmt::internal::PrintfFormatter<Char, PAF>::parse_flags(
|
||||
FormatSpec &spec, const Char *&s) {
|
||||
for (;;) {
|
||||
switch (*s++) {
|
||||
@ -713,8 +713,8 @@ void fmt::internal::PrintfFormatter<Char>::parse_flags(
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Char>
|
||||
Arg fmt::internal::PrintfFormatter<Char>::get_arg(
|
||||
template <typename Char, typename PAF>
|
||||
Arg fmt::internal::PrintfFormatter<Char, PAF>::get_arg(
|
||||
const Char *s, unsigned arg_index) {
|
||||
(void)s;
|
||||
const char *error = 0;
|
||||
@ -725,8 +725,8 @@ Arg fmt::internal::PrintfFormatter<Char>::get_arg(
|
||||
return arg;
|
||||
}
|
||||
|
||||
template <typename Char>
|
||||
unsigned fmt::internal::PrintfFormatter<Char>::parse_header(
|
||||
template <typename Char, typename PAF>
|
||||
unsigned fmt::internal::PrintfFormatter<Char, PAF>::parse_header(
|
||||
const Char *&s, FormatSpec &spec) {
|
||||
unsigned arg_index = UINT_MAX;
|
||||
Char c = *s;
|
||||
@ -759,8 +759,8 @@ unsigned fmt::internal::PrintfFormatter<Char>::parse_header(
|
||||
return arg_index;
|
||||
}
|
||||
|
||||
template <typename Char>
|
||||
void fmt::internal::PrintfFormatter<Char>::format(
|
||||
template <typename Char, typename PAF >
|
||||
void fmt::internal::PrintfFormatter<Char,PAF>::format(
|
||||
BasicWriter<Char> &writer, BasicCStringRef<Char> format_str) {
|
||||
const Char *start = format_str.c_str();
|
||||
const Char *s = start;
|
||||
@ -853,11 +853,12 @@ void fmt::internal::PrintfFormatter<Char>::format(
|
||||
start = s;
|
||||
|
||||
// Format argument.
|
||||
internal::PrintfArgFormatter<Char>(writer, spec).visit(arg);
|
||||
PAF(writer, spec).visit(arg);
|
||||
}
|
||||
write(writer, start, s);
|
||||
}
|
||||
|
||||
|
||||
FMT_FUNC void fmt::report_system_error(
|
||||
int error_code, fmt::StringRef message) FMT_NOEXCEPT {
|
||||
// 'fmt::' is for bcc32.
|
||||
|
||||
10
fmt/format.h
10
fmt/format.h
@ -378,6 +378,7 @@ class BasicWriter;
|
||||
typedef BasicWriter<char> Writer;
|
||||
typedef BasicWriter<wchar_t> WWriter;
|
||||
|
||||
// Define default ArgFormatter and BasicFormatter.
|
||||
template <typename Char>
|
||||
class ArgFormatter;
|
||||
|
||||
@ -1939,7 +1940,7 @@ class FormatterBase {
|
||||
};
|
||||
|
||||
// A printf formatter.
|
||||
template <typename Char>
|
||||
template <typename Char, typename PAF = fmt::internal::PrintfArgFormatter<Char> >
|
||||
class PrintfFormatter : private FormatterBase {
|
||||
private:
|
||||
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);
|
||||
|
||||
template <typename Char>
|
||||
template <typename Char>
|
||||
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
|
||||
|
||||
|
||||
inline namespace literals {
|
||||
|
||||
/**
|
||||
@ -3850,6 +3853,7 @@ inline internal::UdlArg<wchar_t>
|
||||
operator"" _a(const wchar_t *s, std::size_t) { return {s}; }
|
||||
|
||||
} // inline namespace literals
|
||||
|
||||
} // namespace fmt
|
||||
#endif // FMT_USE_USER_DEFINED_LITERALS
|
||||
|
||||
|
||||
@ -80,6 +80,7 @@ add_fmt_test(printf-test)
|
||||
add_fmt_test(string-test)
|
||||
add_fmt_test(util-test mock-allocator.h)
|
||||
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
|
||||
# warnings.
|
||||
|
||||
41
test/custom-formatter-test.cc
Normal file
41
test/custom-formatter-test.cc
Normal 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;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user