mirror of
https://github.com/fmtlib/fmt.git
synced 2024-12-24 21:16:56 +00:00
Pass writer to format_value
This commit is contained in:
parent
64ca334a2d
commit
edf98792a5
24
fmt/format.h
24
fmt/format.h
@ -993,7 +993,7 @@ struct Value {
|
||||
};
|
||||
|
||||
typedef void (*FormatFunc)(
|
||||
void *formatter, const void *arg, void *format_str_ptr);
|
||||
void *writer, void *formatter, const void *arg, void *format_str_ptr);
|
||||
|
||||
struct CustomValue {
|
||||
const void *value;
|
||||
@ -1160,7 +1160,7 @@ inline fmt::StringRef thousands_sep(...) { return ""; }
|
||||
#endif
|
||||
|
||||
template <typename Formatter, typename Char, typename T>
|
||||
void format_value(Formatter &, const Char *, const T &) {
|
||||
void format_value(BasicWriter<Char> &, Formatter &, const Char *, const T &) {
|
||||
FMT_STATIC_ASSERT(False<T>::value,
|
||||
"Cannot format argument. To enable the use of ostream "
|
||||
"operator<< include fmt/ostream.h. Otherwise provide "
|
||||
@ -1271,8 +1271,10 @@ class MakeValue : public Arg {
|
||||
// Formats an argument of a custom type, such as a user-defined class.
|
||||
template <typename T>
|
||||
static void format_custom_arg(
|
||||
void *formatter, const void *arg, void *format_str_ptr) {
|
||||
format_value(*static_cast<Formatter*>(formatter),
|
||||
void *writer, void *formatter, const void *arg, void *format_str_ptr) {
|
||||
typedef BasicWriter<typename Formatter::char_type> Writer;
|
||||
format_value(*static_cast<Writer*>(writer),
|
||||
*static_cast<Formatter*>(formatter),
|
||||
*static_cast<const Char**>(format_str_ptr),
|
||||
*static_cast<const T*>(arg));
|
||||
}
|
||||
@ -2178,7 +2180,7 @@ class BasicArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
|
||||
|
||||
/** Formats an argument of a custom (user-defined) type. */
|
||||
void visit_custom(internal::Arg::CustomValue c) {
|
||||
c.format(&formatter_, c.value, &format_);
|
||||
c.format(&formatter_.writer(), &formatter_, c.value, &format_);
|
||||
}
|
||||
};
|
||||
|
||||
@ -2240,6 +2242,14 @@ class basic_formatter :
|
||||
const Char *format(const Char *&format_str, const internal::Arg &arg);
|
||||
};
|
||||
|
||||
template <typename ArgFormatter, typename Char = typename ArgFormatter::Char>
|
||||
void vformat(BasicWriter<Char> &writer,
|
||||
BasicCStringRef<Char> format_str,
|
||||
basic_format_args<basic_formatter<Char, ArgFormatter>> args) {
|
||||
basic_formatter<Char, ArgFormatter> formatter(args, writer);
|
||||
formatter.format(format_str);
|
||||
}
|
||||
|
||||
/**
|
||||
An error returned by an operating system or a language runtime,
|
||||
for example a file opening error.
|
||||
@ -2470,7 +2480,7 @@ class BasicWriter {
|
||||
|
||||
void vwrite(BasicCStringRef<Char> format,
|
||||
basic_format_args<basic_formatter<Char>> args) {
|
||||
basic_formatter<Char>(args, *this).format(format);
|
||||
vformat(*this, format, args);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -3459,7 +3469,7 @@ const Char *basic_formatter<Char, ArgFormatter>::format(
|
||||
FormatSpec spec;
|
||||
if (*s == ':') {
|
||||
if (arg.type == Arg::CUSTOM) {
|
||||
arg.custom.format(this, arg.custom.value, &s);
|
||||
arg.custom.format(&writer(), this, arg.custom.value, &s);
|
||||
return s;
|
||||
}
|
||||
++s;
|
||||
|
@ -83,7 +83,7 @@ BasicStringRef<Char> format_value(
|
||||
|
||||
// Formats a value.
|
||||
template <typename Char, typename ArgFormatter, typename T>
|
||||
void format_value(basic_formatter<Char, ArgFormatter> &f,
|
||||
void format_value(BasicWriter<Char> &w, basic_formatter<Char, ArgFormatter> &f,
|
||||
const Char *&format_str, const T &value) {
|
||||
internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer;
|
||||
auto str = internal::format_value(buffer, value);
|
||||
|
@ -266,7 +266,7 @@ class BasicPrintfArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
|
||||
this->writer());
|
||||
const Char format_str[] = {'}', 0};
|
||||
const Char *format = format_str;
|
||||
c.format(&formatter, c.value, &format);
|
||||
c.format(&formatter.writer(), &formatter, c.value, &format);
|
||||
}
|
||||
};
|
||||
|
||||
@ -497,7 +497,8 @@ void PrintfFormatter<Char, AF>::format(BasicCStringRef<Char> format_str) {
|
||||
|
||||
// Formats a value.
|
||||
template <typename Char, typename T>
|
||||
void format_value(PrintfFormatter<Char> &f, const Char *&, const T &value) {
|
||||
void format_value(BasicWriter<Char> &w, PrintfFormatter<Char> &f,
|
||||
const Char *&, const T &value) {
|
||||
internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer;
|
||||
f.writer() << internal::format_value(buffer, value);
|
||||
}
|
||||
|
@ -14,8 +14,9 @@
|
||||
#include <ctime>
|
||||
|
||||
namespace fmt {
|
||||
|
||||
template <typename ArgFormatter>
|
||||
void format_value(basic_formatter<char, ArgFormatter> &f,
|
||||
void format_value(Writer &w, basic_formatter<char, ArgFormatter> &f,
|
||||
const char *&format_str, const std::tm &tm) {
|
||||
if (*format_str == ':')
|
||||
++format_str;
|
||||
@ -27,7 +28,7 @@ void format_value(basic_formatter<char, ArgFormatter> &f,
|
||||
internal::MemoryBuffer<char, internal::INLINE_BUFFER_SIZE> format;
|
||||
format.append(format_str, end + 1);
|
||||
format[format.size() - 1] = '\0';
|
||||
Buffer<char> &buffer = f.writer().buffer();
|
||||
Buffer<char> &buffer = w.buffer();
|
||||
std::size_t start = buffer.size();
|
||||
for (;;) {
|
||||
std::size_t size = buffer.capacity() - start;
|
||||
|
@ -1355,7 +1355,8 @@ TEST(FormatterTest, FormatCStringRef) {
|
||||
EXPECT_EQ("test", format("{0}", CStringRef("test")));
|
||||
}
|
||||
|
||||
void format_value(fmt::basic_formatter<char> &f, const char *, const Date &d) {
|
||||
void format_value(fmt::Writer &w, fmt::basic_formatter<char> &f,
|
||||
const char *, const Date &d) {
|
||||
f.writer() << d.year() << '-' << d.month() << '-' << d.day();
|
||||
}
|
||||
|
||||
@ -1368,7 +1369,8 @@ TEST(FormatterTest, FormatCustom) {
|
||||
class Answer {};
|
||||
|
||||
template <typename Char>
|
||||
void format_value(fmt::basic_formatter<Char> &f, const Char *, Answer) {
|
||||
void format_value(BasicWriter<Char> &w, fmt::basic_formatter<Char> &f,
|
||||
const Char *, Answer) {
|
||||
f.writer() << "42";
|
||||
}
|
||||
|
||||
|
@ -64,8 +64,9 @@ namespace {
|
||||
struct Test {};
|
||||
|
||||
template <typename Char>
|
||||
void format_value(fmt::basic_formatter<Char> &f, const Char *, Test) {
|
||||
f.writer() << "test";
|
||||
void format_value(fmt::BasicWriter<Char> &w, fmt::basic_formatter<Char> &f,
|
||||
const Char *, Test) {
|
||||
w << "test";
|
||||
}
|
||||
|
||||
template <typename Char, typename T>
|
||||
@ -568,7 +569,7 @@ TEST(ArgTest, MakeArg) {
|
||||
fmt::MemoryWriter w;
|
||||
fmt::basic_formatter<char> formatter(fmt::format_args(), w);
|
||||
const char *s = "}";
|
||||
arg.custom.format(&formatter, &t, &s);
|
||||
arg.custom.format(&formatter.writer(), &formatter, &t, &s);
|
||||
EXPECT_EQ("test", w.str());
|
||||
}
|
||||
|
||||
@ -581,7 +582,8 @@ struct CustomFormatter {
|
||||
typedef char char_type;
|
||||
};
|
||||
|
||||
void format_value(CustomFormatter &, const char *&s, const Test &) {
|
||||
void format_value(fmt::Writer &, CustomFormatter &, const char *&s,
|
||||
const Test &) {
|
||||
s = "custom_format";
|
||||
}
|
||||
|
||||
@ -590,7 +592,8 @@ TEST(UtilTest, MakeValueWithCustomFormatter) {
|
||||
Arg arg = fmt::internal::MakeValue<CustomFormatter>(t);
|
||||
CustomFormatter formatter;
|
||||
const char *s = "";
|
||||
arg.custom.format(&formatter, &t, &s);
|
||||
fmt::MemoryWriter w;
|
||||
arg.custom.format(&w, &formatter, &t, &s);
|
||||
EXPECT_STREQ("custom_format", s);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user