A custom FormatSpec type can be passed as a template argument to the ArgFormatter chain (#439)
This commit is contained in:
parent
db780cb119
commit
0cce094c89
@ -120,17 +120,19 @@ custom argument formatter class::
|
||||
// A custom argument formatter that formats negative integers as unsigned
|
||||
// with the ``x`` format specifier.
|
||||
class CustomArgFormatter :
|
||||
public fmt::BasicArgFormatter<CustomArgFormatter, char> {
|
||||
public fmt::BasicArgFormatter<CustomArgFormatter, char, fmt::FormatSpec> {
|
||||
public:
|
||||
CustomArgFormatter(fmt::BasicFormatter<char, CustomArgFormatter> &f,
|
||||
fmt::FormatSpec &s, const char *fmt)
|
||||
: fmt::BasicArgFormatter<CustomArgFormatter, char>(f, s, fmt) {}
|
||||
: fmt::BasicArgFormatter<CustomArgFormatter, char,
|
||||
fmt::FormatSpec>(f, s, fmt) {}
|
||||
|
||||
void visit_int(int value) {
|
||||
if (spec().type() == 'x')
|
||||
visit_uint(value); // convert to unsigned and format
|
||||
else
|
||||
fmt::BasicArgFormatter<CustomArgFormatter, char>::visit_int(value);
|
||||
fmt::BasicArgFormatter<CustomArgFormatter, char,
|
||||
fmt::FormatSpec>::visit_int(value);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
55
fmt/format.h
55
fmt/format.h
@ -431,7 +431,7 @@ typedef BasicWriter<wchar_t> WWriter;
|
||||
template <typename Char>
|
||||
class ArgFormatter;
|
||||
|
||||
template <typename Impl, typename Char>
|
||||
template <typename Impl, typename Char, typename Spec>
|
||||
class BasicPrintfArgFormatter;
|
||||
|
||||
template <typename CharType,
|
||||
@ -1697,6 +1697,7 @@ struct TypeSpec : EmptySpec {
|
||||
int precision() const { return -1; }
|
||||
bool flag(unsigned) const { return false; }
|
||||
char type() const { return TYPE; }
|
||||
char type_prefix() const { return TYPE; }
|
||||
char fill() const { return ' '; }
|
||||
};
|
||||
|
||||
@ -1732,6 +1733,7 @@ struct AlignTypeSpec : AlignSpec {
|
||||
|
||||
bool flag(unsigned) const { return false; }
|
||||
char type() const { return TYPE; }
|
||||
char type_prefix() const { return TYPE; }
|
||||
};
|
||||
|
||||
// A full format specifier.
|
||||
@ -1747,6 +1749,7 @@ struct FormatSpec : AlignSpec {
|
||||
bool flag(unsigned f) const { return (flags_ & f) != 0; }
|
||||
int precision() const { return precision_; }
|
||||
char type() const { return type_; }
|
||||
char type_prefix() const { return type_; }
|
||||
};
|
||||
|
||||
// An integer format specifier.
|
||||
@ -1922,11 +1925,11 @@ class ArgMap {
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Impl, typename Char>
|
||||
template <typename Impl, typename Char, typename Spec>
|
||||
class ArgFormatterBase : public ArgVisitor<Impl, void> {
|
||||
private:
|
||||
BasicWriter<Char> &writer_;
|
||||
FormatSpec &spec_;
|
||||
Spec &spec_;
|
||||
|
||||
FMT_DISALLOW_COPY_AND_ASSIGN(ArgFormatterBase);
|
||||
|
||||
@ -1938,7 +1941,7 @@ class ArgFormatterBase : public ArgVisitor<Impl, void> {
|
||||
|
||||
protected:
|
||||
BasicWriter<Char> &writer() { return writer_; }
|
||||
FormatSpec &spec() { return spec_; }
|
||||
Spec &spec() { return spec_; }
|
||||
|
||||
void write(bool value) {
|
||||
const char *str_value = value ? "true" : "false";
|
||||
@ -1952,7 +1955,9 @@ class ArgFormatterBase : public ArgVisitor<Impl, void> {
|
||||
}
|
||||
|
||||
public:
|
||||
ArgFormatterBase(BasicWriter<Char> &w, FormatSpec &s)
|
||||
typedef typename Spec SpecType;
|
||||
|
||||
ArgFormatterBase(BasicWriter<Char> &w, Spec &s)
|
||||
: writer_(w), spec_(s) {}
|
||||
|
||||
template <typename T>
|
||||
@ -2086,8 +2091,8 @@ class FormatterBase {
|
||||
will be called.
|
||||
\endrst
|
||||
*/
|
||||
template <typename Impl, typename Char>
|
||||
class BasicArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
|
||||
template <typename Impl, typename Char, typename Spec>
|
||||
class BasicArgFormatter : public internal::ArgFormatterBase<Impl, Char, Spec> {
|
||||
private:
|
||||
BasicFormatter<Char, Impl> &formatter_;
|
||||
const Char *format_;
|
||||
@ -2102,8 +2107,8 @@ class BasicArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
|
||||
\endrst
|
||||
*/
|
||||
BasicArgFormatter(BasicFormatter<Char, Impl> &formatter,
|
||||
FormatSpec &spec, const Char *fmt)
|
||||
: internal::ArgFormatterBase<Impl, Char>(formatter.writer(), spec),
|
||||
Spec &spec, const Char *fmt)
|
||||
: internal::ArgFormatterBase<Impl, Char, Spec>(formatter.writer(), spec),
|
||||
formatter_(formatter), format_(fmt) {}
|
||||
|
||||
/** Formats an argument of a custom (user-defined) type. */
|
||||
@ -2114,12 +2119,14 @@ class BasicArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
|
||||
|
||||
/** The default argument formatter. */
|
||||
template <typename Char>
|
||||
class ArgFormatter : public BasicArgFormatter<ArgFormatter<Char>, Char> {
|
||||
class ArgFormatter :
|
||||
public BasicArgFormatter<ArgFormatter<Char>, Char, FormatSpec> {
|
||||
public:
|
||||
/** Constructs an argument formatter object. */
|
||||
ArgFormatter(BasicFormatter<Char> &formatter,
|
||||
FormatSpec &spec, const Char *fmt)
|
||||
: BasicArgFormatter<ArgFormatter<Char>, Char>(formatter, spec, fmt) {}
|
||||
: BasicArgFormatter<ArgFormatter<Char>,
|
||||
Char, FormatSpec>(formatter, spec, fmt) {}
|
||||
};
|
||||
|
||||
/** This template formats data and writes the output to a writer. */
|
||||
@ -2501,16 +2508,16 @@ class BasicWriter {
|
||||
void write_int(T value, Spec spec);
|
||||
|
||||
// Formats a floating-point number (double or long double).
|
||||
template <typename T>
|
||||
void write_double(T value, const FormatSpec &spec);
|
||||
template <typename T, typename Spec>
|
||||
void write_double(T value, const Spec &spec);
|
||||
|
||||
// Writes a formatted string.
|
||||
template <typename StrChar>
|
||||
CharPtr write_str(const StrChar *s, std::size_t size, const AlignSpec &spec);
|
||||
|
||||
template <typename StrChar>
|
||||
template <typename StrChar, typename Spec>
|
||||
void write_str(const internal::Arg::StringValue<StrChar> &str,
|
||||
const FormatSpec &spec);
|
||||
const Spec &spec);
|
||||
|
||||
// This following methods are private to disallow writing wide characters
|
||||
// and strings to a char stream. If you want to print a wide string as a
|
||||
@ -2529,10 +2536,10 @@ class BasicWriter {
|
||||
template<typename T>
|
||||
void append_float_length(Char *&, T) {}
|
||||
|
||||
template <typename Impl, typename Char_>
|
||||
template <typename Impl, typename Char_, typename Spec_>
|
||||
friend class internal::ArgFormatterBase;
|
||||
|
||||
template <typename Impl, typename Char_>
|
||||
template <typename Impl, typename Char_, typename Spec_>
|
||||
friend class BasicPrintfArgFormatter;
|
||||
|
||||
protected:
|
||||
@ -2729,9 +2736,9 @@ typename BasicWriter<Char>::CharPtr BasicWriter<Char>::write_str(
|
||||
}
|
||||
|
||||
template <typename Char>
|
||||
template <typename StrChar>
|
||||
template <typename StrChar, typename Spec>
|
||||
void BasicWriter<Char>::write_str(
|
||||
const internal::Arg::StringValue<StrChar> &s, const FormatSpec &spec) {
|
||||
const internal::Arg::StringValue<StrChar> &s, const Spec &spec) {
|
||||
// Check if StrChar is convertible to Char.
|
||||
internal::CharTraits<Char>::convert(StrChar());
|
||||
if (spec.type_ && spec.type_ != 's')
|
||||
@ -2855,7 +2862,7 @@ void BasicWriter<Char>::write_int(T value, Spec spec) {
|
||||
UnsignedType n = abs_value;
|
||||
if (spec.flag(HASH_FLAG)) {
|
||||
prefix[prefix_size++] = '0';
|
||||
prefix[prefix_size++] = spec.type();
|
||||
prefix[prefix_size++] = spec.type_prefix();
|
||||
}
|
||||
unsigned num_digits = 0;
|
||||
do {
|
||||
@ -2875,7 +2882,7 @@ void BasicWriter<Char>::write_int(T value, Spec spec) {
|
||||
UnsignedType n = abs_value;
|
||||
if (spec.flag(HASH_FLAG)) {
|
||||
prefix[prefix_size++] = '0';
|
||||
prefix[prefix_size++] = spec.type();
|
||||
prefix[prefix_size++] = spec.type_prefix();
|
||||
}
|
||||
unsigned num_digits = 0;
|
||||
do {
|
||||
@ -2923,8 +2930,8 @@ void BasicWriter<Char>::write_int(T value, Spec spec) {
|
||||
}
|
||||
|
||||
template <typename Char>
|
||||
template <typename T>
|
||||
void BasicWriter<Char>::write_double(T value, const FormatSpec &spec) {
|
||||
template <typename T, typename Spec>
|
||||
void BasicWriter<Char>::write_double(T value, const Spec &spec) {
|
||||
// Check type.
|
||||
char type = spec.type();
|
||||
bool upper = false;
|
||||
@ -3661,7 +3668,7 @@ const Char *BasicFormatter<Char, ArgFormatter>::format(
|
||||
const Char *&format_str, const internal::Arg &arg) {
|
||||
using internal::Arg;
|
||||
const Char *s = format_str;
|
||||
FormatSpec spec;
|
||||
typename ArgFormatter::SpecType spec;
|
||||
if (*s == ':') {
|
||||
if (arg.type == Arg::CUSTOM) {
|
||||
arg.custom.format(this, arg.custom.value, &s);
|
||||
|
||||
21
fmt/printf.h
21
fmt/printf.h
@ -189,15 +189,16 @@ class WidthHandler : public ArgVisitor<WidthHandler, unsigned> {
|
||||
superclass will be called.
|
||||
\endrst
|
||||
*/
|
||||
template <typename Impl, typename Char>
|
||||
class BasicPrintfArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
|
||||
template <typename Impl, typename Char, typename Spec>
|
||||
class BasicPrintfArgFormatter :
|
||||
public internal::ArgFormatterBase<Impl, Char, Spec> {
|
||||
private:
|
||||
void write_null_pointer() {
|
||||
this->spec().type_ = 0;
|
||||
this->write("(nil)");
|
||||
}
|
||||
|
||||
typedef internal::ArgFormatterBase<Impl, Char> Base;
|
||||
typedef internal::ArgFormatterBase<Impl, Char, Spec> Base;
|
||||
|
||||
public:
|
||||
/**
|
||||
@ -207,12 +208,12 @@ class BasicPrintfArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
|
||||
specifier information for standard argument types.
|
||||
\endrst
|
||||
*/
|
||||
BasicPrintfArgFormatter(BasicWriter<Char> &w, FormatSpec &s)
|
||||
: internal::ArgFormatterBase<Impl, Char>(w, s) {}
|
||||
BasicPrintfArgFormatter(BasicWriter<Char> &w, Spec &s)
|
||||
: internal::ArgFormatterBase<Impl, Char, Spec>(w, s) {}
|
||||
|
||||
/** Formats an argument of type ``bool``. */
|
||||
void visit_bool(bool value) {
|
||||
FormatSpec &fmt_spec = this->spec();
|
||||
Spec &fmt_spec = this->spec();
|
||||
if (fmt_spec.type_ != 's')
|
||||
return this->visit_any_int(value);
|
||||
fmt_spec.type_ = 0;
|
||||
@ -221,7 +222,7 @@ class BasicPrintfArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
|
||||
|
||||
/** Formats a character. */
|
||||
void visit_char(int value) {
|
||||
const FormatSpec &fmt_spec = this->spec();
|
||||
const Spec &fmt_spec = this->spec();
|
||||
BasicWriter<Char> &w = this->writer();
|
||||
if (fmt_spec.type_ && fmt_spec.type_ != 'c')
|
||||
w.write_int(value, fmt_spec);
|
||||
@ -271,12 +272,12 @@ class BasicPrintfArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
|
||||
|
||||
/** The default printf argument formatter. */
|
||||
template <typename Char>
|
||||
class PrintfArgFormatter
|
||||
: public BasicPrintfArgFormatter<PrintfArgFormatter<Char>, Char> {
|
||||
class PrintfArgFormatter :
|
||||
public BasicPrintfArgFormatter<PrintfArgFormatter<Char>, Char, FormatSpec> {
|
||||
public:
|
||||
/** Constructs an argument formatter object. */
|
||||
PrintfArgFormatter(BasicWriter<Char> &w, FormatSpec &s)
|
||||
: BasicPrintfArgFormatter<PrintfArgFormatter<Char>, Char>(w, s) {}
|
||||
: BasicPrintfArgFormatter<PrintfArgFormatter<Char>, Char, FormatSpec>(w, s) {}
|
||||
};
|
||||
|
||||
/** This template formats data and writes the output to a writer. */
|
||||
|
||||
@ -14,26 +14,30 @@ using fmt::BasicPrintfArgFormatter;
|
||||
|
||||
// A custom argument formatter that doesn't print `-` for floating-point values
|
||||
// rounded to 0.
|
||||
class CustomArgFormatter
|
||||
: public fmt::BasicArgFormatter<CustomArgFormatter, char> {
|
||||
class CustomArgFormatter :
|
||||
public fmt::BasicArgFormatter<CustomArgFormatter, char, fmt::FormatSpec> {
|
||||
public:
|
||||
CustomArgFormatter(fmt::BasicFormatter<char, CustomArgFormatter> &f,
|
||||
fmt::FormatSpec &s, const char *fmt)
|
||||
: fmt::BasicArgFormatter<CustomArgFormatter, char>(f, s, fmt) {}
|
||||
: fmt::BasicArgFormatter<CustomArgFormatter, char,
|
||||
fmt::FormatSpec>(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);
|
||||
fmt::BasicArgFormatter<CustomArgFormatter, char,
|
||||
fmt::FormatSpec>::visit_double(value);
|
||||
}
|
||||
};
|
||||
|
||||
// A custom argument formatter that doesn't print `-` for floating-point values
|
||||
// rounded to 0.
|
||||
class CustomPrintfArgFormatter :
|
||||
public BasicPrintfArgFormatter<CustomPrintfArgFormatter, char> {
|
||||
public BasicPrintfArgFormatter<CustomPrintfArgFormatter, char,
|
||||
fmt::FormatSpec> {
|
||||
public:
|
||||
typedef BasicPrintfArgFormatter<CustomPrintfArgFormatter, char> Base;
|
||||
typedef BasicPrintfArgFormatter<CustomPrintfArgFormatter, char,
|
||||
fmt::FormatSpec> Base;
|
||||
|
||||
CustomPrintfArgFormatter(fmt::BasicWriter<char> &w, fmt::FormatSpec &spec)
|
||||
: Base(w, spec) {}
|
||||
|
||||
@ -1630,13 +1630,16 @@ TEST(FormatTest, Enum) {
|
||||
}
|
||||
|
||||
class MockArgFormatter :
|
||||
public fmt::internal::ArgFormatterBase<MockArgFormatter, char> {
|
||||
public fmt::internal::ArgFormatterBase<MockArgFormatter, char,
|
||||
fmt::FormatSpec> {
|
||||
public:
|
||||
typedef fmt::internal::ArgFormatterBase<MockArgFormatter, char> Base;
|
||||
typedef fmt::internal::ArgFormatterBase<MockArgFormatter, char,
|
||||
fmt::FormatSpec> Base;
|
||||
|
||||
MockArgFormatter(fmt::BasicFormatter<char, MockArgFormatter> &f,
|
||||
fmt::FormatSpec &s, const char *)
|
||||
: fmt::internal::ArgFormatterBase<MockArgFormatter, char>(f.writer(), s) {
|
||||
: fmt::internal::ArgFormatterBase<MockArgFormatter,
|
||||
char, fmt::FormatSpec>(f.writer(), s) {
|
||||
EXPECT_CALL(*this, visit_int(42));
|
||||
}
|
||||
|
||||
|
||||
@ -58,10 +58,12 @@ TEST(OStreamTest, Enum) {
|
||||
EXPECT_EQ("0", fmt::format("{}", A));
|
||||
}
|
||||
|
||||
struct TestArgFormatter : fmt::BasicArgFormatter<TestArgFormatter, char> {
|
||||
struct TestArgFormatter :
|
||||
fmt::BasicArgFormatter<TestArgFormatter, char, fmt::FormatSpec> {
|
||||
TestArgFormatter(fmt::BasicFormatter<char, TestArgFormatter> &f,
|
||||
fmt::FormatSpec &s, const char *fmt)
|
||||
: fmt::BasicArgFormatter<TestArgFormatter, char>(f, s, fmt) {}
|
||||
: fmt::BasicArgFormatter<TestArgFormatter, char,
|
||||
fmt::FormatSpec>(f, s, fmt) {}
|
||||
};
|
||||
|
||||
TEST(OStreamTest, CustomArg) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user