mirror of
https://github.com/fmtlib/fmt.git
synced 2026-05-04 03:40:49 +02:00
Fix type safety when using custom formatters (#394)
This commit is contained in:
@@ -45,10 +45,13 @@ class CustomPrintfArgFormatter :
|
||||
}
|
||||
};
|
||||
|
||||
std::string custom_vformat(const char *format_str, fmt::format_args args) {
|
||||
typedef fmt::BasicFormatter<char, CustomArgFormatter> CustomFormatter;
|
||||
|
||||
std::string custom_vformat(const char *format_str,
|
||||
fmt::basic_format_args<CustomFormatter> args) {
|
||||
fmt::MemoryWriter writer;
|
||||
// Pass custom argument formatter as a template arg to BasicFormatter.
|
||||
fmt::BasicFormatter<char, CustomArgFormatter> formatter(args, writer);
|
||||
CustomFormatter formatter(args, writer);
|
||||
formatter.format(format_str);
|
||||
return writer.str();
|
||||
}
|
||||
@@ -59,9 +62,14 @@ std::string custom_format(const char *format_str, const Args & ... args) {
|
||||
return custom_vformat(format_str, va);
|
||||
}
|
||||
|
||||
std::string custom_vsprintf(const char* format_str, fmt::format_args args) {
|
||||
typedef fmt::PrintfFormatter<char, CustomPrintfArgFormatter>
|
||||
CustomPrintfFormatter;
|
||||
|
||||
std::string custom_vsprintf(
|
||||
const char* format_str,
|
||||
fmt::basic_format_args<CustomPrintfFormatter> args) {
|
||||
fmt::MemoryWriter writer;
|
||||
fmt::PrintfFormatter<char, CustomPrintfArgFormatter> formatter(args, writer);
|
||||
CustomPrintfFormatter formatter(args, writer);
|
||||
formatter.format(format_str);
|
||||
return writer.str();
|
||||
}
|
||||
|
||||
+6
-3
@@ -1633,16 +1633,19 @@ class MockArgFormatter :
|
||||
MOCK_METHOD1(visit_int, void (int value));
|
||||
};
|
||||
|
||||
void vcustom_format(const char *format_str, fmt::format_args args) {
|
||||
typedef fmt::BasicFormatter<char, MockArgFormatter> CustomFormatter;
|
||||
|
||||
void custom_vformat(const char *format_str,
|
||||
fmt::basic_format_args<CustomFormatter> args) {
|
||||
fmt::MemoryWriter writer;
|
||||
fmt::BasicFormatter<char, MockArgFormatter> formatter(args, writer);
|
||||
CustomFormatter formatter(args, writer);
|
||||
formatter.format(format_str);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
void custom_format(const char *format_str, const Args & ... args) {
|
||||
auto va = fmt::make_format_args<fmt::BasicFormatter<char>>(args...);
|
||||
return vcustom_format(format_str, va);
|
||||
return custom_vformat(format_str, va);
|
||||
}
|
||||
|
||||
TEST(FormatTest, CustomArgFormatter) {
|
||||
|
||||
@@ -67,7 +67,7 @@ struct TestArgFormatter : fmt::BasicArgFormatter<TestArgFormatter, char> {
|
||||
TEST(OStreamTest, CustomArg) {
|
||||
fmt::MemoryWriter writer;
|
||||
typedef fmt::BasicFormatter<char, TestArgFormatter> Formatter;
|
||||
Formatter formatter(fmt::format_args(), writer);
|
||||
Formatter formatter(fmt::basic_format_args<Formatter>(), writer);
|
||||
fmt::FormatSpec spec;
|
||||
TestArgFormatter af(formatter, spec, "}");
|
||||
af.visit(fmt::internal::MakeArg<Formatter>(TestEnum()));
|
||||
|
||||
Reference in New Issue
Block a user