* A custom FormatSpec type can be passed as a template argument to the ArgFormatter chain (#439) * Corrected nested-name-specifier error * Spec template argument defaulted to FormatSpec * Forward declaration of FormatSpec * Style * Style (part 2) * Style (part 3)
This commit is contained in:
parent
b9aaa507fc
commit
e1689cb985
@ -120,7 +120,7 @@ custom argument formatter class::
|
|||||||
// A custom argument formatter that formats negative integers as unsigned
|
// A custom argument formatter that formats negative integers as unsigned
|
||||||
// with the ``x`` format specifier.
|
// with the ``x`` format specifier.
|
||||||
class CustomArgFormatter :
|
class CustomArgFormatter :
|
||||||
public fmt::BasicArgFormatter<CustomArgFormatter, char> {
|
public fmt::BasicArgFormatter<CustomArgFormatter, char> {
|
||||||
public:
|
public:
|
||||||
CustomArgFormatter(fmt::BasicFormatter<char, CustomArgFormatter> &f,
|
CustomArgFormatter(fmt::BasicFormatter<char, CustomArgFormatter> &f,
|
||||||
fmt::FormatSpec &s, const char *fmt)
|
fmt::FormatSpec &s, const char *fmt)
|
||||||
|
57
fmt/format.h
57
fmt/format.h
@ -431,7 +431,9 @@ typedef BasicWriter<wchar_t> WWriter;
|
|||||||
template <typename Char>
|
template <typename Char>
|
||||||
class ArgFormatter;
|
class ArgFormatter;
|
||||||
|
|
||||||
template <typename Impl, typename Char>
|
struct FormatSpec;
|
||||||
|
|
||||||
|
template <typename Impl, typename Char, typename Spec = fmt::FormatSpec>
|
||||||
class BasicPrintfArgFormatter;
|
class BasicPrintfArgFormatter;
|
||||||
|
|
||||||
template <typename CharType,
|
template <typename CharType,
|
||||||
@ -1697,6 +1699,7 @@ struct TypeSpec : EmptySpec {
|
|||||||
int precision() const { return -1; }
|
int precision() const { return -1; }
|
||||||
bool flag(unsigned) const { return false; }
|
bool flag(unsigned) const { return false; }
|
||||||
char type() const { return TYPE; }
|
char type() const { return TYPE; }
|
||||||
|
char type_prefix() const { return TYPE; }
|
||||||
char fill() const { return ' '; }
|
char fill() const { return ' '; }
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1732,6 +1735,7 @@ struct AlignTypeSpec : AlignSpec {
|
|||||||
|
|
||||||
bool flag(unsigned) const { return false; }
|
bool flag(unsigned) const { return false; }
|
||||||
char type() const { return TYPE; }
|
char type() const { return TYPE; }
|
||||||
|
char type_prefix() const { return TYPE; }
|
||||||
};
|
};
|
||||||
|
|
||||||
// A full format specifier.
|
// A full format specifier.
|
||||||
@ -1747,6 +1751,7 @@ struct FormatSpec : AlignSpec {
|
|||||||
bool flag(unsigned f) const { return (flags_ & f) != 0; }
|
bool flag(unsigned f) const { return (flags_ & f) != 0; }
|
||||||
int precision() const { return precision_; }
|
int precision() const { return precision_; }
|
||||||
char type() const { return type_; }
|
char type() const { return type_; }
|
||||||
|
char type_prefix() const { return type_; }
|
||||||
};
|
};
|
||||||
|
|
||||||
// An integer format specifier.
|
// An integer format specifier.
|
||||||
@ -1922,11 +1927,11 @@ class ArgMap {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename Impl, typename Char>
|
template <typename Impl, typename Char, typename Spec = fmt::FormatSpec>
|
||||||
class ArgFormatterBase : public ArgVisitor<Impl, void> {
|
class ArgFormatterBase : public ArgVisitor<Impl, void> {
|
||||||
private:
|
private:
|
||||||
BasicWriter<Char> &writer_;
|
BasicWriter<Char> &writer_;
|
||||||
FormatSpec &spec_;
|
Spec &spec_;
|
||||||
|
|
||||||
FMT_DISALLOW_COPY_AND_ASSIGN(ArgFormatterBase);
|
FMT_DISALLOW_COPY_AND_ASSIGN(ArgFormatterBase);
|
||||||
|
|
||||||
@ -1938,7 +1943,7 @@ class ArgFormatterBase : public ArgVisitor<Impl, void> {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
BasicWriter<Char> &writer() { return writer_; }
|
BasicWriter<Char> &writer() { return writer_; }
|
||||||
FormatSpec &spec() { return spec_; }
|
Spec &spec() { return spec_; }
|
||||||
|
|
||||||
void write(bool value) {
|
void write(bool value) {
|
||||||
const char *str_value = value ? "true" : "false";
|
const char *str_value = value ? "true" : "false";
|
||||||
@ -1952,7 +1957,9 @@ class ArgFormatterBase : public ArgVisitor<Impl, void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ArgFormatterBase(BasicWriter<Char> &w, FormatSpec &s)
|
typedef Spec SpecType;
|
||||||
|
|
||||||
|
ArgFormatterBase(BasicWriter<Char> &w, Spec &s)
|
||||||
: writer_(w), spec_(s) {}
|
: writer_(w), spec_(s) {}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@ -2086,8 +2093,8 @@ class FormatterBase {
|
|||||||
will be called.
|
will be called.
|
||||||
\endrst
|
\endrst
|
||||||
*/
|
*/
|
||||||
template <typename Impl, typename Char>
|
template <typename Impl, typename Char, typename Spec = fmt::FormatSpec>
|
||||||
class BasicArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
|
class BasicArgFormatter : public internal::ArgFormatterBase<Impl, Char, Spec> {
|
||||||
private:
|
private:
|
||||||
BasicFormatter<Char, Impl> &formatter_;
|
BasicFormatter<Char, Impl> &formatter_;
|
||||||
const Char *format_;
|
const Char *format_;
|
||||||
@ -2102,8 +2109,8 @@ class BasicArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
|
|||||||
\endrst
|
\endrst
|
||||||
*/
|
*/
|
||||||
BasicArgFormatter(BasicFormatter<Char, Impl> &formatter,
|
BasicArgFormatter(BasicFormatter<Char, Impl> &formatter,
|
||||||
FormatSpec &spec, const Char *fmt)
|
Spec &spec, const Char *fmt)
|
||||||
: internal::ArgFormatterBase<Impl, Char>(formatter.writer(), spec),
|
: internal::ArgFormatterBase<Impl, Char, Spec>(formatter.writer(), spec),
|
||||||
formatter_(formatter), format_(fmt) {}
|
formatter_(formatter), format_(fmt) {}
|
||||||
|
|
||||||
/** Formats an argument of a custom (user-defined) type. */
|
/** Formats an argument of a custom (user-defined) type. */
|
||||||
@ -2114,12 +2121,14 @@ class BasicArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
|
|||||||
|
|
||||||
/** The default argument formatter. */
|
/** The default argument formatter. */
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
class ArgFormatter : public BasicArgFormatter<ArgFormatter<Char>, Char> {
|
class ArgFormatter :
|
||||||
|
public BasicArgFormatter<ArgFormatter<Char>, Char, FormatSpec> {
|
||||||
public:
|
public:
|
||||||
/** Constructs an argument formatter object. */
|
/** Constructs an argument formatter object. */
|
||||||
ArgFormatter(BasicFormatter<Char> &formatter,
|
ArgFormatter(BasicFormatter<Char> &formatter,
|
||||||
FormatSpec &spec, const Char *fmt)
|
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. */
|
/** This template formats data and writes the output to a writer. */
|
||||||
@ -2501,16 +2510,16 @@ class BasicWriter {
|
|||||||
void write_int(T value, Spec spec);
|
void write_int(T value, Spec spec);
|
||||||
|
|
||||||
// Formats a floating-point number (double or long double).
|
// Formats a floating-point number (double or long double).
|
||||||
template <typename T>
|
template <typename T, typename Spec>
|
||||||
void write_double(T value, const FormatSpec &spec);
|
void write_double(T value, const Spec &spec);
|
||||||
|
|
||||||
// Writes a formatted string.
|
// Writes a formatted string.
|
||||||
template <typename StrChar>
|
template <typename StrChar>
|
||||||
CharPtr write_str(const StrChar *s, std::size_t size, const AlignSpec &spec);
|
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,
|
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
|
// 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
|
// and strings to a char stream. If you want to print a wide string as a
|
||||||
@ -2529,10 +2538,10 @@ class BasicWriter {
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
void append_float_length(Char *&, T) {}
|
void append_float_length(Char *&, T) {}
|
||||||
|
|
||||||
template <typename Impl, typename Char_>
|
template <typename Impl, typename Char_, typename Spec_>
|
||||||
friend class internal::ArgFormatterBase;
|
friend class internal::ArgFormatterBase;
|
||||||
|
|
||||||
template <typename Impl, typename Char_>
|
template <typename Impl, typename Char_, typename Spec_>
|
||||||
friend class BasicPrintfArgFormatter;
|
friend class BasicPrintfArgFormatter;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
@ -2729,9 +2738,9 @@ typename BasicWriter<Char>::CharPtr BasicWriter<Char>::write_str(
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
template <typename StrChar>
|
template <typename StrChar, typename Spec>
|
||||||
void BasicWriter<Char>::write_str(
|
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.
|
// Check if StrChar is convertible to Char.
|
||||||
internal::CharTraits<Char>::convert(StrChar());
|
internal::CharTraits<Char>::convert(StrChar());
|
||||||
if (spec.type_ && spec.type_ != 's')
|
if (spec.type_ && spec.type_ != 's')
|
||||||
@ -2855,7 +2864,7 @@ void BasicWriter<Char>::write_int(T value, Spec spec) {
|
|||||||
UnsignedType n = abs_value;
|
UnsignedType n = abs_value;
|
||||||
if (spec.flag(HASH_FLAG)) {
|
if (spec.flag(HASH_FLAG)) {
|
||||||
prefix[prefix_size++] = '0';
|
prefix[prefix_size++] = '0';
|
||||||
prefix[prefix_size++] = spec.type();
|
prefix[prefix_size++] = spec.type_prefix();
|
||||||
}
|
}
|
||||||
unsigned num_digits = 0;
|
unsigned num_digits = 0;
|
||||||
do {
|
do {
|
||||||
@ -2875,7 +2884,7 @@ void BasicWriter<Char>::write_int(T value, Spec spec) {
|
|||||||
UnsignedType n = abs_value;
|
UnsignedType n = abs_value;
|
||||||
if (spec.flag(HASH_FLAG)) {
|
if (spec.flag(HASH_FLAG)) {
|
||||||
prefix[prefix_size++] = '0';
|
prefix[prefix_size++] = '0';
|
||||||
prefix[prefix_size++] = spec.type();
|
prefix[prefix_size++] = spec.type_prefix();
|
||||||
}
|
}
|
||||||
unsigned num_digits = 0;
|
unsigned num_digits = 0;
|
||||||
do {
|
do {
|
||||||
@ -2923,8 +2932,8 @@ void BasicWriter<Char>::write_int(T value, Spec spec) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
template <typename T>
|
template <typename T, typename Spec>
|
||||||
void BasicWriter<Char>::write_double(T value, const FormatSpec &spec) {
|
void BasicWriter<Char>::write_double(T value, const Spec &spec) {
|
||||||
// Check type.
|
// Check type.
|
||||||
char type = spec.type();
|
char type = spec.type();
|
||||||
bool upper = false;
|
bool upper = false;
|
||||||
@ -3661,7 +3670,7 @@ const Char *BasicFormatter<Char, ArgFormatter>::format(
|
|||||||
const Char *&format_str, const internal::Arg &arg) {
|
const Char *&format_str, const internal::Arg &arg) {
|
||||||
using internal::Arg;
|
using internal::Arg;
|
||||||
const Char *s = format_str;
|
const Char *s = format_str;
|
||||||
FormatSpec spec;
|
typename ArgFormatter::SpecType spec;
|
||||||
if (*s == ':') {
|
if (*s == ':') {
|
||||||
if (arg.type == Arg::CUSTOM) {
|
if (arg.type == Arg::CUSTOM) {
|
||||||
arg.custom.format(this, arg.custom.value, &s);
|
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.
|
superclass will be called.
|
||||||
\endrst
|
\endrst
|
||||||
*/
|
*/
|
||||||
template <typename Impl, typename Char>
|
template <typename Impl, typename Char, typename Spec>
|
||||||
class BasicPrintfArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
|
class BasicPrintfArgFormatter :
|
||||||
|
public internal::ArgFormatterBase<Impl, Char, Spec> {
|
||||||
private:
|
private:
|
||||||
void write_null_pointer() {
|
void write_null_pointer() {
|
||||||
this->spec().type_ = 0;
|
this->spec().type_ = 0;
|
||||||
this->write("(nil)");
|
this->write("(nil)");
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef internal::ArgFormatterBase<Impl, Char> Base;
|
typedef internal::ArgFormatterBase<Impl, Char, Spec> Base;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
@ -207,12 +208,12 @@ class BasicPrintfArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
|
|||||||
specifier information for standard argument types.
|
specifier information for standard argument types.
|
||||||
\endrst
|
\endrst
|
||||||
*/
|
*/
|
||||||
BasicPrintfArgFormatter(BasicWriter<Char> &w, FormatSpec &s)
|
BasicPrintfArgFormatter(BasicWriter<Char> &w, Spec &s)
|
||||||
: internal::ArgFormatterBase<Impl, Char>(w, s) {}
|
: internal::ArgFormatterBase<Impl, Char, Spec>(w, s) {}
|
||||||
|
|
||||||
/** Formats an argument of type ``bool``. */
|
/** Formats an argument of type ``bool``. */
|
||||||
void visit_bool(bool value) {
|
void visit_bool(bool value) {
|
||||||
FormatSpec &fmt_spec = this->spec();
|
Spec &fmt_spec = this->spec();
|
||||||
if (fmt_spec.type_ != 's')
|
if (fmt_spec.type_ != 's')
|
||||||
return this->visit_any_int(value);
|
return this->visit_any_int(value);
|
||||||
fmt_spec.type_ = 0;
|
fmt_spec.type_ = 0;
|
||||||
@ -221,7 +222,7 @@ class BasicPrintfArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
|
|||||||
|
|
||||||
/** Formats a character. */
|
/** Formats a character. */
|
||||||
void visit_char(int value) {
|
void visit_char(int value) {
|
||||||
const FormatSpec &fmt_spec = this->spec();
|
const Spec &fmt_spec = this->spec();
|
||||||
BasicWriter<Char> &w = this->writer();
|
BasicWriter<Char> &w = this->writer();
|
||||||
if (fmt_spec.type_ && fmt_spec.type_ != 'c')
|
if (fmt_spec.type_ && fmt_spec.type_ != 'c')
|
||||||
w.write_int(value, fmt_spec);
|
w.write_int(value, fmt_spec);
|
||||||
@ -271,12 +272,12 @@ class BasicPrintfArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
|
|||||||
|
|
||||||
/** The default printf argument formatter. */
|
/** The default printf argument formatter. */
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
class PrintfArgFormatter
|
class PrintfArgFormatter :
|
||||||
: public BasicPrintfArgFormatter<PrintfArgFormatter<Char>, Char> {
|
public BasicPrintfArgFormatter<PrintfArgFormatter<Char>, Char, FormatSpec> {
|
||||||
public:
|
public:
|
||||||
/** Constructs an argument formatter object. */
|
/** Constructs an argument formatter object. */
|
||||||
PrintfArgFormatter(BasicWriter<Char> &w, FormatSpec &s)
|
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. */
|
/** This template formats data and writes the output to a writer. */
|
||||||
|
@ -1660,4 +1660,3 @@ void convert(int);
|
|||||||
TEST(FormatTest, ConvertCollision) {
|
TEST(FormatTest, ConvertCollision) {
|
||||||
fmt::format("{}", 42);
|
fmt::format("{}", 42);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user