Commit a1bd3358 authored by Victor Zverovich's avatar Victor Zverovich

Produce a compile error when using wide fill character on a narrow stream.

parent fa3efb13
...@@ -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 &spec, Answer) { void Format(BasicWriter<Char> &f, const fmt::FormatSpec<Char> &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 &spec, char sign) { unsigned size, const AlignSpec<Char> &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);
...@@ -180,6 +180,7 @@ typename fmt::BasicWriter<Char>::CharPtr ...@@ -180,6 +180,7 @@ typename fmt::BasicWriter<Char>::CharPtr
CharPtr p = GrowBuffer(width); CharPtr p = GrowBuffer(width);
CharPtr end = p + width; CharPtr end = p + width;
Alignment align = spec.align(); Alignment align = spec.align();
// TODO: error if fill is not convertible to Char
Char fill = static_cast<Char>(spec.fill()); Char fill = static_cast<Char>(spec.fill());
if (align == ALIGN_LEFT) { if (align == ALIGN_LEFT) {
*p = sign; *p = sign;
...@@ -207,7 +208,7 @@ typename fmt::BasicWriter<Char>::CharPtr ...@@ -207,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 &spec, int precision) { T value, const FormatSpec<Char> &spec, int precision) {
// Check type. // Check type.
char type = spec.type(); char type = spec.type();
bool upper = false; bool upper = false;
...@@ -437,7 +438,7 @@ void fmt::BasicFormatter<Char>::DoFormat() { ...@@ -437,7 +438,7 @@ void fmt::BasicFormatter<Char>::DoFormat() {
const Arg &arg = ParseArgIndex(s); const Arg &arg = ParseArgIndex(s);
FormatSpec spec; FormatSpec<Char> spec;
int precision = -1; int precision = -1;
if (*s == ':') { if (*s == ':') {
++s; ++s;
...@@ -665,10 +666,10 @@ void fmt::BasicFormatter<Char>::DoFormat() { ...@@ -665,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 &spec, int precision); double value, const FormatSpec<char> &spec, int precision);
template void fmt::BasicWriter<char>::FormatDouble<long double>( template void fmt::BasicWriter<char>::FormatDouble<long double>(
long double value, const FormatSpec &spec, int precision); long double value, const FormatSpec<char> &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,
...@@ -679,7 +680,7 @@ template void fmt::BasicWriter<char>::FormatDecimal( ...@@ -679,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 &spec, char sign); unsigned size, const AlignSpec<char> &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;
...@@ -697,10 +698,10 @@ template void fmt::BasicFormatter<char>::DoFormat(); ...@@ -697,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 &spec, int precision); double value, const FormatSpec<wchar_t> &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 &spec, int precision); long double value, const FormatSpec<wchar_t> &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,
...@@ -711,7 +712,7 @@ template void fmt::BasicWriter<wchar_t>::FormatDecimal( ...@@ -711,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 &spec, char sign); unsigned size, const AlignSpec<wchar_t> &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;
......
...@@ -353,6 +353,7 @@ struct WidthSpec { ...@@ -353,6 +353,7 @@ struct WidthSpec {
}; };
// An alignment specifier. // An alignment specifier.
template <typename Char>
struct AlignSpec : WidthSpec { struct AlignSpec : WidthSpec {
Alignment align_; Alignment align_;
...@@ -363,9 +364,9 @@ struct AlignSpec : WidthSpec { ...@@ -363,9 +364,9 @@ struct AlignSpec : WidthSpec {
}; };
// An alignment and type specifier. // An alignment and type specifier.
template <char TYPE> template <typename Char, char TYPE>
struct AlignTypeSpec : AlignSpec { struct AlignTypeSpec : AlignSpec<Char> {
AlignTypeSpec(unsigned width, wchar_t fill) : AlignSpec(width, fill) {} AlignTypeSpec(unsigned width, wchar_t fill) : AlignSpec<Char>(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; }
...@@ -375,12 +376,13 @@ struct AlignTypeSpec : AlignSpec { ...@@ -375,12 +376,13 @@ struct AlignTypeSpec : AlignSpec {
}; };
// A full format specifier. // A full format specifier.
struct FormatSpec : AlignSpec { template <typename Char>
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(width, fill), flags_(0), type_(type) {} : AlignSpec<Char>(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; }
...@@ -390,6 +392,7 @@ struct FormatSpec : AlignSpec { ...@@ -390,6 +392,7 @@ struct FormatSpec : AlignSpec {
}; };
// An integer format specifier. // An integer format specifier.
// TODO: additional argument typename Char = char
template <typename T, typename SpecT> template <typename T, typename SpecT>
class IntFormatSpec : public SpecT { class IntFormatSpec : public SpecT {
private: private:
...@@ -404,13 +407,13 @@ class IntFormatSpec : public SpecT { ...@@ -404,13 +407,13 @@ class IntFormatSpec : public SpecT {
// A string format specifier. // A string format specifier.
template <typename T> template <typename T>
class StrFormatSpec : public AlignSpec { class StrFormatSpec : public AlignSpec<T> {
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(width, fill), str_(str) {} : AlignSpec<T>(width, fill), str_(str) {}
const T *str() const { return str_; } const T *str() const { return str_; }
}; };
...@@ -450,9 +453,9 @@ IntFormatSpec<int, TypeSpec<'X'> > hexu(int value); ...@@ -450,9 +453,9 @@ IntFormatSpec<int, TypeSpec<'X'> > hexu(int value);
\endrst \endrst
*/ */
template <char TYPE_CODE> template <typename Char, char TYPE_CODE>
IntFormatSpec<int, AlignTypeSpec<TYPE_CODE> > pad( IntFormatSpec<int, AlignTypeSpec<Char, TYPE_CODE> > pad(
int value, unsigned width, wchar_t fill = ' '); int value, unsigned width, Char fill = ' ');
#define DEFINE_INT_FORMATTERS(TYPE) \ #define DEFINE_INT_FORMATTERS(TYPE) \
inline IntFormatSpec<TYPE, TypeSpec<'b'> > bin(TYPE value) { \ inline IntFormatSpec<TYPE, TypeSpec<'b'> > bin(TYPE value) { \
...@@ -472,17 +475,35 @@ inline IntFormatSpec<TYPE, TypeSpec<'X'> > hexu(TYPE value) { \ ...@@ -472,17 +475,35 @@ inline IntFormatSpec<TYPE, TypeSpec<'X'> > hexu(TYPE value) { \
} \ } \
\ \
template <char TYPE_CODE> \ template <char TYPE_CODE> \
inline IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE> > pad( \ inline IntFormatSpec<TYPE, AlignTypeSpec<char, TYPE_CODE> > pad( \
IntFormatSpec<TYPE, TypeSpec<TYPE_CODE> > f, unsigned width) { \
return IntFormatSpec<TYPE, AlignTypeSpec<char, TYPE_CODE> >( \
f.value(), AlignTypeSpec<char, TYPE_CODE>(width, ' ')); \
} \
\
/* 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 */ \
/* can be replaced with one overload and making the template argument Char */ \
/* default to char (C++11). */ \
template <char TYPE_CODE, typename Char> \
inline IntFormatSpec<TYPE, AlignTypeSpec<Char, TYPE_CODE> > pad( \
IntFormatSpec<TYPE, TypeSpec<TYPE_CODE> > f, \ IntFormatSpec<TYPE, TypeSpec<TYPE_CODE> > f, \
unsigned width, wchar_t fill = ' ') { \ unsigned width, Char fill) { \
return IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE> >( \ return IntFormatSpec<TYPE, AlignTypeSpec<Char, TYPE_CODE> >( \
f.value(), AlignTypeSpec<TYPE_CODE>(width, fill)); \ f.value(), AlignTypeSpec<Char, TYPE_CODE>(width, fill)); \
} \
\
inline IntFormatSpec<TYPE, AlignTypeSpec<char, 0> > pad( \
TYPE value, unsigned width) { \
return IntFormatSpec<TYPE, AlignTypeSpec<char, 0> >( \
value, AlignTypeSpec<char, 0>(width, ' ')); \
} \ } \
\ \
inline IntFormatSpec<TYPE, AlignTypeSpec<0> > pad( \ template <typename Char> \
TYPE value, unsigned width, wchar_t fill = ' ') { \ inline IntFormatSpec<TYPE, AlignTypeSpec<Char, 0> > pad( \
return IntFormatSpec<TYPE, AlignTypeSpec<0> >( \ TYPE value, unsigned width, Char fill) { \
value, AlignTypeSpec<0>(width, fill)); \ return IntFormatSpec<TYPE, AlignTypeSpec<Char, 0> >( \
value, AlignTypeSpec<Char, 0>(width, fill)); \
} }
DEFINE_INT_FORMATTERS(int) DEFINE_INT_FORMATTERS(int)
...@@ -587,16 +608,17 @@ class BasicWriter { ...@@ -587,16 +608,17 @@ class BasicWriter {
return p + size - 1; return p + size - 1;
} }
CharPtr PrepareFilledBuffer(unsigned size, const AlignSpec &spec, char sign); CharPtr PrepareFilledBuffer(
unsigned size, const AlignSpec<Char> &spec, char sign);
// 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 &spec, int precision); void FormatDouble(T value, const FormatSpec<Char> &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 &spec); const StringChar *s, std::size_t size, const AlignSpec<Char> &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
...@@ -687,7 +709,7 @@ class BasicWriter { ...@@ -687,7 +709,7 @@ class BasicWriter {
} }
BasicWriter &operator<<(double value) { BasicWriter &operator<<(double value) {
FormatDouble(value, FormatSpec(), -1); FormatDouble(value, FormatSpec<Char>(), -1);
return *this; return *this;
} }
...@@ -696,7 +718,7 @@ class BasicWriter { ...@@ -696,7 +718,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(), -1); FormatDouble(value, FormatSpec<Char>(), -1);
return *this; return *this;
} }
...@@ -733,7 +755,7 @@ class BasicWriter { ...@@ -733,7 +755,7 @@ class BasicWriter {
return *this; return *this;
} }
void Write(const std::basic_string<char> &s, const FormatSpec &spec) { void Write(const std::basic_string<char> &s, const FormatSpec<Char> &spec) {
FormatString(s.data(), s.size(), spec); FormatString(s.data(), s.size(), spec);
} }
...@@ -745,7 +767,7 @@ class BasicWriter { ...@@ -745,7 +767,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 &spec) { const StringChar *s, std::size_t size, const AlignSpec<Char> &spec) {
CharPtr out = CharPtr(); CharPtr out = CharPtr();
if (spec.width() > size) { if (spec.width() > size) {
out = GrowBuffer(spec.width()); out = GrowBuffer(spec.width());
...@@ -862,7 +884,7 @@ typedef BasicWriter<wchar_t> WWriter; ...@@ -862,7 +884,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 &spec, const T &value) { void Format(BasicWriter<Char> &w, const FormatSpec<Char> &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);
...@@ -872,7 +894,7 @@ namespace internal { ...@@ -872,7 +894,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 &spec) { BasicWriter<Char> &w, const void *arg, const FormatSpec<Char> &spec) {
Format(w, spec, *static_cast<const T*>(arg)); Format(w, spec, *static_cast<const T*>(arg));
} }
} }
...@@ -899,7 +921,7 @@ class BasicFormatter { ...@@ -899,7 +921,7 @@ class BasicFormatter {
}; };
typedef void (*FormatFunc)( typedef void (*FormatFunc)(
BasicWriter<Char> &w, const void *arg, const FormatSpec &spec); BasicWriter<Char> &w, const void *arg, const FormatSpec<Char> &spec);
// A format argument. // A format argument.
class Arg { class Arg {
...@@ -1042,8 +1064,8 @@ class BasicFormatter { ...@@ -1042,8 +1064,8 @@ class BasicFormatter {
// Formats an integer. // Formats an integer.
template <typename T> template <typename T>
void FormatInt(T value, const FormatSpec &spec) { void FormatInt(T value, const FormatSpec<Char> &spec) {
*writer_ << IntFormatSpec<T, FormatSpec>(value, spec); *writer_ << IntFormatSpec<T, FormatSpec<Char> >(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