Commit ea5dce39 authored by Victor Zverovich's avatar Victor Zverovich

Simplify format specs.

parent a1bd3358
...@@ -1227,7 +1227,7 @@ TEST(FormatterTest, FormatUsingIOStreams) { ...@@ -1227,7 +1227,7 @@ TEST(FormatterTest, FormatUsingIOStreams) {
class Answer {}; class Answer {};
template <typename Char> template <typename Char>
void Format(BasicWriter<Char> &f, const fmt::FormatSpec<Char> &spec, Answer) { void Format(BasicWriter<Char> &f, const fmt::FormatSpec &spec, Answer) {
f.Write("42", spec); f.Write("42", spec);
} }
......
...@@ -170,7 +170,7 @@ void fmt::BasicWriter<Char>::FormatDecimal( ...@@ -170,7 +170,7 @@ void fmt::BasicWriter<Char>::FormatDecimal(
template <typename Char> template <typename Char>
typename fmt::BasicWriter<Char>::CharPtr typename fmt::BasicWriter<Char>::CharPtr
fmt::BasicWriter<Char>::PrepareFilledBuffer( fmt::BasicWriter<Char>::PrepareFilledBuffer(
unsigned size, const AlignSpec<Char> &spec, char sign) { unsigned size, const AlignSpec &spec, char sign) {
unsigned width = spec.width(); unsigned width = spec.width();
if (width <= size) { if (width <= size) {
CharPtr p = GrowBuffer(size); CharPtr p = GrowBuffer(size);
...@@ -208,7 +208,7 @@ typename fmt::BasicWriter<Char>::CharPtr ...@@ -208,7 +208,7 @@ typename fmt::BasicWriter<Char>::CharPtr
template <typename Char> template <typename Char>
template <typename T> template <typename T>
void fmt::BasicWriter<Char>::FormatDouble( void fmt::BasicWriter<Char>::FormatDouble(
T value, const FormatSpec<Char> &spec, int precision) { T value, const FormatSpec &spec, int precision) {
// Check type. // Check type.
char type = spec.type(); char type = spec.type();
bool upper = false; bool upper = false;
...@@ -438,7 +438,7 @@ void fmt::BasicFormatter<Char>::DoFormat() { ...@@ -438,7 +438,7 @@ void fmt::BasicFormatter<Char>::DoFormat() {
const Arg &arg = ParseArgIndex(s); const Arg &arg = ParseArgIndex(s);
FormatSpec<Char> spec; FormatSpec spec;
int precision = -1; int precision = -1;
if (*s == ':') { if (*s == ':') {
++s; ++s;
...@@ -666,10 +666,10 @@ void fmt::BasicFormatter<Char>::DoFormat() { ...@@ -666,10 +666,10 @@ void fmt::BasicFormatter<Char>::DoFormat() {
// Explicit instantiations for char. // Explicit instantiations for char.
template void fmt::BasicWriter<char>::FormatDouble<double>( template void fmt::BasicWriter<char>::FormatDouble<double>(
double value, const FormatSpec<char> &spec, int precision); double value, const FormatSpec &spec, int precision);
template void fmt::BasicWriter<char>::FormatDouble<long double>( template void fmt::BasicWriter<char>::FormatDouble<long double>(
long double value, const FormatSpec<char> &spec, int precision); long double value, const FormatSpec &spec, int precision);
template fmt::BasicWriter<char>::CharPtr template fmt::BasicWriter<char>::CharPtr
fmt::BasicWriter<char>::FillPadding(CharPtr buffer, fmt::BasicWriter<char>::FillPadding(CharPtr buffer,
...@@ -680,7 +680,7 @@ template void fmt::BasicWriter<char>::FormatDecimal( ...@@ -680,7 +680,7 @@ template void fmt::BasicWriter<char>::FormatDecimal(
template fmt::BasicWriter<char>::CharPtr template fmt::BasicWriter<char>::CharPtr
fmt::BasicWriter<char>::PrepareFilledBuffer( fmt::BasicWriter<char>::PrepareFilledBuffer(
unsigned size, const AlignSpec<char> &spec, char sign); unsigned size, const AlignSpec &spec, char sign);
template void fmt::BasicFormatter<char>::ReportError( template void fmt::BasicFormatter<char>::ReportError(
const char *s, StringRef message) const; const char *s, StringRef message) const;
...@@ -698,10 +698,10 @@ template void fmt::BasicFormatter<char>::DoFormat(); ...@@ -698,10 +698,10 @@ template void fmt::BasicFormatter<char>::DoFormat();
// Explicit instantiations for wchar_t. // Explicit instantiations for wchar_t.
template void fmt::BasicWriter<wchar_t>::FormatDouble<double>( template void fmt::BasicWriter<wchar_t>::FormatDouble<double>(
double value, const FormatSpec<wchar_t> &spec, int precision); double value, const FormatSpec &spec, int precision);
template void fmt::BasicWriter<wchar_t>::FormatDouble<long double>( template void fmt::BasicWriter<wchar_t>::FormatDouble<long double>(
long double value, const FormatSpec<wchar_t> &spec, int precision); long double value, const FormatSpec &spec, int precision);
template fmt::BasicWriter<wchar_t>::CharPtr template fmt::BasicWriter<wchar_t>::CharPtr
fmt::BasicWriter<wchar_t>::FillPadding(CharPtr buffer, fmt::BasicWriter<wchar_t>::FillPadding(CharPtr buffer,
...@@ -712,7 +712,7 @@ template void fmt::BasicWriter<wchar_t>::FormatDecimal( ...@@ -712,7 +712,7 @@ template void fmt::BasicWriter<wchar_t>::FormatDecimal(
template fmt::BasicWriter<wchar_t>::CharPtr template fmt::BasicWriter<wchar_t>::CharPtr
fmt::BasicWriter<wchar_t>::PrepareFilledBuffer( fmt::BasicWriter<wchar_t>::PrepareFilledBuffer(
unsigned size, const AlignSpec<wchar_t> &spec, char sign); unsigned size, const AlignSpec &spec, char sign);
template void fmt::BasicFormatter<wchar_t>::ReportError( template void fmt::BasicFormatter<wchar_t>::ReportError(
const wchar_t *s, StringRef message) const; const wchar_t *s, StringRef message) const;
......
...@@ -175,11 +175,14 @@ template <> ...@@ -175,11 +175,14 @@ template <>
class CharTraits<char> { class CharTraits<char> {
private: private:
// Conversion from wchar_t to char is not supported. // Conversion from wchar_t to char is not supported.
// TODO: rename to ConvertChar
static char ConvertWChar(wchar_t); static char ConvertWChar(wchar_t);
public: public:
typedef const wchar_t *UnsupportedStrType; typedef const wchar_t *UnsupportedStrType;
static char ConvertWChar(char value) { return value; }
template <typename T> template <typename T>
static int FormatFloat(char *buffer, std::size_t size, static int FormatFloat(char *buffer, std::size_t size,
const char *format, unsigned width, int precision, T value); const char *format, unsigned width, int precision, T value);
...@@ -190,6 +193,7 @@ class CharTraits<wchar_t> { ...@@ -190,6 +193,7 @@ class CharTraits<wchar_t> {
public: public:
typedef const char *UnsupportedStrType; typedef const char *UnsupportedStrType;
static wchar_t ConvertWChar(char value) { return value; }
static wchar_t ConvertWChar(wchar_t value) { return value; } static wchar_t ConvertWChar(wchar_t value) { return value; }
template <typename T> template <typename T>
...@@ -353,7 +357,6 @@ struct WidthSpec { ...@@ -353,7 +357,6 @@ struct WidthSpec {
}; };
// An alignment specifier. // An alignment specifier.
template <typename Char>
struct AlignSpec : WidthSpec { struct AlignSpec : WidthSpec {
Alignment align_; Alignment align_;
...@@ -364,9 +367,9 @@ struct AlignSpec : WidthSpec { ...@@ -364,9 +367,9 @@ struct AlignSpec : WidthSpec {
}; };
// An alignment and type specifier. // An alignment and type specifier.
template <typename Char, char TYPE> template <char TYPE>
struct AlignTypeSpec : AlignSpec<Char> { struct AlignTypeSpec : AlignSpec {
AlignTypeSpec(unsigned width, wchar_t fill) : AlignSpec<Char>(width, fill) {} AlignTypeSpec(unsigned width, wchar_t fill) : AlignSpec(width, fill) {}
bool sign_flag() const { return false; } bool sign_flag() const { return false; }
bool plus_flag() const { return false; } bool plus_flag() const { return false; }
...@@ -376,13 +379,12 @@ struct AlignTypeSpec : AlignSpec<Char> { ...@@ -376,13 +379,12 @@ struct AlignTypeSpec : AlignSpec<Char> {
}; };
// A full format specifier. // A full format specifier.
template <typename Char> struct FormatSpec : AlignSpec {
struct FormatSpec : AlignSpec<Char> {
unsigned flags_; unsigned flags_;
char type_; char type_;
FormatSpec(unsigned width = 0, char type = 0, wchar_t fill = ' ') FormatSpec(unsigned width = 0, char type = 0, wchar_t fill = ' ')
: AlignSpec<Char>(width, fill), flags_(0), type_(type) {} : AlignSpec(width, fill), flags_(0), type_(type) {}
bool sign_flag() const { return (flags_ & SIGN_FLAG) != 0; } bool sign_flag() const { return (flags_ & SIGN_FLAG) != 0; }
bool plus_flag() const { return (flags_ & PLUS_FLAG) != 0; } bool plus_flag() const { return (flags_ & PLUS_FLAG) != 0; }
...@@ -392,8 +394,7 @@ struct FormatSpec : AlignSpec<Char> { ...@@ -392,8 +394,7 @@ struct FormatSpec : AlignSpec<Char> {
}; };
// An integer format specifier. // An integer format specifier.
// TODO: additional argument typename Char = char template <typename T, typename SpecT = TypeSpec<0>, typename Char = char>
template <typename T, typename SpecT>
class IntFormatSpec : public SpecT { class IntFormatSpec : public SpecT {
private: private:
T value_; T value_;
...@@ -407,13 +408,13 @@ class IntFormatSpec : public SpecT { ...@@ -407,13 +408,13 @@ class IntFormatSpec : public SpecT {
// A string format specifier. // A string format specifier.
template <typename T> template <typename T>
class StrFormatSpec : public AlignSpec<T> { class StrFormatSpec : public AlignSpec {
private: private:
const T *str_; const T *str_;
public: public:
StrFormatSpec(const T *str, unsigned width, wchar_t fill) StrFormatSpec(const T *str, unsigned width, wchar_t fill)
: AlignSpec<T>(width, fill), str_(str) {} : AlignSpec(width, fill), str_(str) {}
const T *str() const { return str_; } const T *str() const { return str_; }
}; };
...@@ -453,8 +454,8 @@ IntFormatSpec<int, TypeSpec<'X'> > hexu(int value); ...@@ -453,8 +454,8 @@ IntFormatSpec<int, TypeSpec<'X'> > hexu(int value);
\endrst \endrst
*/ */
template <typename Char, char TYPE_CODE> template <char TYPE_CODE, typename Char>
IntFormatSpec<int, AlignTypeSpec<Char, TYPE_CODE> > pad( IntFormatSpec<int, AlignTypeSpec<TYPE_CODE>, Char> pad(
int value, unsigned width, Char fill = ' '); int value, unsigned width, Char fill = ' ');
#define DEFINE_INT_FORMATTERS(TYPE) \ #define DEFINE_INT_FORMATTERS(TYPE) \
...@@ -475,35 +476,35 @@ inline IntFormatSpec<TYPE, TypeSpec<'X'> > hexu(TYPE value) { \ ...@@ -475,35 +476,35 @@ inline IntFormatSpec<TYPE, TypeSpec<'X'> > hexu(TYPE value) { \
} \ } \
\ \
template <char TYPE_CODE> \ template <char TYPE_CODE> \
inline IntFormatSpec<TYPE, AlignTypeSpec<char, TYPE_CODE> > pad( \ inline IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE> > pad( \
IntFormatSpec<TYPE, TypeSpec<TYPE_CODE> > f, unsigned width) { \ IntFormatSpec<TYPE, TypeSpec<TYPE_CODE> > f, unsigned width) { \
return IntFormatSpec<TYPE, AlignTypeSpec<char, TYPE_CODE> >( \ return IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE> >( \
f.value(), AlignTypeSpec<char, TYPE_CODE>(width, ' ')); \ f.value(), AlignTypeSpec<TYPE_CODE>(width, ' ')); \
} \ } \
\ \
/* For compatibility with older compilers we provide two overloads for pad, */ \ /* For compatibility with older compilers we provide two overloads for pad, */ \
/* one that takes a fill character and one that doesn't. In the future this */ \ /* one that takes a fill character and one that doesn't. In the future this */ \
/* can be replaced with one overload and making the template argument Char */ \ /* can be replaced with one overload making the template argument Char */ \
/* default to char (C++11). */ \ /* default to char (C++11). */ \
template <char TYPE_CODE, typename Char> \ template <char TYPE_CODE, typename Char> \
inline IntFormatSpec<TYPE, AlignTypeSpec<Char, TYPE_CODE> > pad( \ inline IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE>, Char> pad( \
IntFormatSpec<TYPE, TypeSpec<TYPE_CODE> > f, \ IntFormatSpec<TYPE, TypeSpec<TYPE_CODE>, Char> f, \
unsigned width, Char fill) { \ unsigned width, Char fill) { \
return IntFormatSpec<TYPE, AlignTypeSpec<Char, TYPE_CODE> >( \ return IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE>, Char>( \
f.value(), AlignTypeSpec<Char, TYPE_CODE>(width, fill)); \ f.value(), AlignTypeSpec<TYPE_CODE>(width, fill)); \
} \ } \
\ \
inline IntFormatSpec<TYPE, AlignTypeSpec<char, 0> > pad( \ inline IntFormatSpec<TYPE, AlignTypeSpec<0> > pad( \
TYPE value, unsigned width) { \ TYPE value, unsigned width) { \
return IntFormatSpec<TYPE, AlignTypeSpec<char, 0> >( \ return IntFormatSpec<TYPE, AlignTypeSpec<0> >( \
value, AlignTypeSpec<char, 0>(width, ' ')); \ value, AlignTypeSpec<0>(width, ' ')); \
} \ } \
\ \
template <typename Char> \ template <typename Char> \
inline IntFormatSpec<TYPE, AlignTypeSpec<Char, 0> > pad( \ inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad( \
TYPE value, unsigned width, Char fill) { \ TYPE value, unsigned width, Char fill) { \
return IntFormatSpec<TYPE, AlignTypeSpec<Char, 0> >( \ return IntFormatSpec<TYPE, AlignTypeSpec<0>, Char>( \
value, AlignTypeSpec<Char, 0>(width, fill)); \ value, AlignTypeSpec<0>(width, fill)); \
} }
DEFINE_INT_FORMATTERS(int) DEFINE_INT_FORMATTERS(int)
...@@ -608,17 +609,20 @@ class BasicWriter { ...@@ -608,17 +609,20 @@ class BasicWriter {
return p + size - 1; return p + size - 1;
} }
CharPtr PrepareFilledBuffer( CharPtr PrepareFilledBuffer(unsigned size, const AlignSpec &spec, char sign);
unsigned size, const AlignSpec<Char> &spec, char sign);
// Formats an integer.
template <typename T, typename Spec>
void FormatInt(T value, const 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>
void FormatDouble(T value, const FormatSpec<Char> &spec, int precision); void FormatDouble(T value, const FormatSpec &spec, int precision);
// Formats a string. // Formats a string.
template <typename StringChar> template <typename StringChar>
CharPtr FormatString( CharPtr FormatString(
const StringChar *s, std::size_t size, const AlignSpec<Char> &spec); const StringChar *s, std::size_t size, const AlignSpec &spec);
// This method is private to disallow writing a wide string to a // This method is private to disallow writing a wide string to a
// char stream and vice versa. If you want to print a wide string // char stream and vice versa. If you want to print a wide string
...@@ -683,33 +687,30 @@ class BasicWriter { ...@@ -683,33 +687,30 @@ class BasicWriter {
BasicFormatter<Char> Format(StringRef format); BasicFormatter<Char> Format(StringRef format);
BasicWriter &operator<<(int value) { BasicWriter &operator<<(int value) {
return *this << IntFormatSpec<int, TypeSpec<0> >(value, TypeSpec<0>()); return *this << IntFormatSpec<int>(value);
} }
BasicWriter &operator<<(unsigned value) { BasicWriter &operator<<(unsigned value) {
return *this << IntFormatSpec<unsigned, TypeSpec<0> >(value, TypeSpec<0>()); return *this << IntFormatSpec<unsigned>(value);
} }
BasicWriter &operator<<(long value) { BasicWriter &operator<<(long value) {
return *this << IntFormatSpec<long, TypeSpec<0> >(value, TypeSpec<0>()); return *this << IntFormatSpec<long>(value);
} }
BasicWriter &operator<<(unsigned long value) { BasicWriter &operator<<(unsigned long value) {
return *this return *this << IntFormatSpec<unsigned long>(value);
<< IntFormatSpec<unsigned long, TypeSpec<0> >(value, TypeSpec<0>());
} }
BasicWriter &operator<<(long long value) { BasicWriter &operator<<(long long value) {
return *this return *this << IntFormatSpec<long long>(value);
<< IntFormatSpec<long long, TypeSpec<0> >(value, TypeSpec<0>());
} }
/** /**
Formats *value* and writes it to the stream. Formats *value* and writes it to the stream.
*/ */
BasicWriter &operator<<(unsigned long long value) { BasicWriter &operator<<(unsigned long long value) {
return *this << return *this << IntFormatSpec<unsigned long long>(value);
IntFormatSpec<unsigned long long, TypeSpec<0> >(value, TypeSpec<0>());
} }
BasicWriter &operator<<(double value) { BasicWriter &operator<<(double value) {
FormatDouble(value, FormatSpec<Char>(), -1); FormatDouble(value, FormatSpec(), -1);
return *this; return *this;
} }
...@@ -718,7 +719,7 @@ class BasicWriter { ...@@ -718,7 +719,7 @@ class BasicWriter {
(``'g'``) and writes it to the stream. (``'g'``) and writes it to the stream.
*/ */
BasicWriter &operator<<(long double value) { BasicWriter &operator<<(long double value) {
FormatDouble(value, FormatSpec<Char>(), -1); FormatDouble(value, FormatSpec(), -1);
return *this; return *this;
} }
...@@ -745,8 +746,12 @@ class BasicWriter { ...@@ -745,8 +746,12 @@ class BasicWriter {
return *this; return *this;
} }
template <typename T, typename Spec> template <typename T, typename Spec, typename FillChar>
BasicWriter &operator<<(const IntFormatSpec<T, Spec> &spec); BasicWriter &operator<<(const IntFormatSpec<T, Spec, FillChar> &spec) {
internal::CharTraits<Char>::ConvertWChar(FillChar());
FormatInt(spec.value(), spec);
return *this;
}
template <typename StringChar> template <typename StringChar>
BasicWriter &operator<<(const StrFormatSpec<StringChar> &spec) { BasicWriter &operator<<(const StrFormatSpec<StringChar> &spec) {
...@@ -755,7 +760,7 @@ class BasicWriter { ...@@ -755,7 +760,7 @@ class BasicWriter {
return *this; return *this;
} }
void Write(const std::basic_string<char> &s, const FormatSpec<Char> &spec) { void Write(const std::basic_string<char> &s, const FormatSpec &spec) {
FormatString(s.data(), s.size(), spec); FormatString(s.data(), s.size(), spec);
} }
...@@ -767,7 +772,7 @@ class BasicWriter { ...@@ -767,7 +772,7 @@ class BasicWriter {
template <typename Char> template <typename Char>
template <typename StringChar> template <typename StringChar>
typename BasicWriter<Char>::CharPtr BasicWriter<Char>::FormatString( typename BasicWriter<Char>::CharPtr BasicWriter<Char>::FormatString(
const StringChar *s, std::size_t size, const AlignSpec<Char> &spec) { const StringChar *s, std::size_t size, const AlignSpec &spec) {
CharPtr out = CharPtr(); CharPtr out = CharPtr();
if (spec.width() > size) { if (spec.width() > size) {
out = GrowBuffer(spec.width()); out = GrowBuffer(spec.width());
...@@ -789,9 +794,7 @@ typename BasicWriter<Char>::CharPtr BasicWriter<Char>::FormatString( ...@@ -789,9 +794,7 @@ typename BasicWriter<Char>::CharPtr BasicWriter<Char>::FormatString(
template <typename Char> template <typename Char>
template <typename T, typename Spec> template <typename T, typename Spec>
BasicWriter<Char> &BasicWriter<Char>::operator<<( void BasicWriter<Char>::FormatInt(T value, const Spec &spec) {
const IntFormatSpec<T, Spec> &spec) {
T value = spec.value();
unsigned size = 0; unsigned size = 0;
char sign = 0; char sign = 0;
typedef typename internal::IntTraits<T>::UnsignedType UnsignedType; typedef typename internal::IntTraits<T>::UnsignedType UnsignedType;
...@@ -870,7 +873,6 @@ BasicWriter<Char> &BasicWriter<Char>::operator<<( ...@@ -870,7 +873,6 @@ BasicWriter<Char> &BasicWriter<Char>::operator<<(
internal::ReportUnknownType(spec.type(), "integer"); internal::ReportUnknownType(spec.type(), "integer");
break; break;
} }
return *this;
} }
template <typename Char> template <typename Char>
...@@ -884,7 +886,7 @@ typedef BasicWriter<wchar_t> WWriter; ...@@ -884,7 +886,7 @@ typedef BasicWriter<wchar_t> WWriter;
// The default formatting function. // The default formatting function.
template <typename Char, typename T> template <typename Char, typename T>
void Format(BasicWriter<Char> &w, const FormatSpec<Char> &spec, const T &value) { void Format(BasicWriter<Char> &w, const FormatSpec &spec, const T &value) {
std::basic_ostringstream<Char> os; std::basic_ostringstream<Char> os;
os << value; os << value;
w.Write(os.str(), spec); w.Write(os.str(), spec);
...@@ -894,7 +896,7 @@ namespace internal { ...@@ -894,7 +896,7 @@ namespace internal {
// Formats an argument of a custom type, such as a user-defined class. // Formats an argument of a custom type, such as a user-defined class.
template <typename Char, typename T> template <typename Char, typename T>
void FormatCustomArg( void FormatCustomArg(
BasicWriter<Char> &w, const void *arg, const FormatSpec<Char> &spec) { BasicWriter<Char> &w, const void *arg, const FormatSpec &spec) {
Format(w, spec, *static_cast<const T*>(arg)); Format(w, spec, *static_cast<const T*>(arg));
} }
} }
...@@ -921,7 +923,7 @@ class BasicFormatter { ...@@ -921,7 +923,7 @@ class BasicFormatter {
}; };
typedef void (*FormatFunc)( typedef void (*FormatFunc)(
BasicWriter<Char> &w, const void *arg, const FormatSpec<Char> &spec); BasicWriter<Char> &w, const void *arg, const FormatSpec &spec);
// A format argument. // A format argument.
class Arg { class Arg {
...@@ -1063,9 +1065,10 @@ class BasicFormatter { ...@@ -1063,9 +1065,10 @@ class BasicFormatter {
void DoFormat(); void DoFormat();
// Formats an integer. // Formats an integer.
// TODO: remove
template <typename T> template <typename T>
void FormatInt(T value, const FormatSpec<Char> &spec) { void FormatInt(T value, const FormatSpec &spec) {
*writer_ << IntFormatSpec<T, FormatSpec<Char> >(value, spec); *writer_ << IntFormatSpec<T, FormatSpec>(value, spec);
} }
struct Proxy { struct Proxy {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment