Commit 93e41252 authored by Victor Zverovich's avatar Victor Zverovich

Move DIGITS to the implementation. Widen fill_ to support both char and wchar_t.

parent f060db02
...@@ -108,14 +108,14 @@ struct CharTraits<wchar_t> { ...@@ -108,14 +108,14 @@ struct CharTraits<wchar_t> {
swprintf(buffer, size, format, width, precision, value); swprintf(buffer, size, format, width, precision, value);
} }
}; };
}
const char fmt::internal::DIGITS[] = const char DIGITS[] =
"0001020304050607080910111213141516171819" "0001020304050607080910111213141516171819"
"2021222324252627282930313233343536373839" "2021222324252627282930313233343536373839"
"4041424344454647484950515253545556575859" "4041424344454647484950515253545556575859"
"6061626364656667686970717273747576777879" "6061626364656667686970717273747576777879"
"8081828384858687888990919293949596979899"; "8081828384858687888990919293949596979899";
}
void fmt::internal::ReportUnknownType(char code, const char *type) { void fmt::internal::ReportUnknownType(char code, const char *type) {
if (std::isprint(static_cast<unsigned char>(code))) { if (std::isprint(static_cast<unsigned char>(code))) {
...@@ -131,8 +131,9 @@ void fmt::internal::ReportUnknownType(char code, const char *type) { ...@@ -131,8 +131,9 @@ void fmt::internal::ReportUnknownType(char code, const char *type) {
// Fills the padding around the content and returns the pointer to the // Fills the padding around the content and returns the pointer to the
// content area. // content area.
template <typename Char> template <typename Char>
typename fmt::BasicWriter<Char>::CharPtr fmt::BasicWriter<Char>::FillPadding( typename fmt::BasicWriter<Char>::CharPtr
CharPtr buffer, unsigned total_size, std::size_t content_size, char fill) { fmt::BasicWriter<Char>::FillPadding(CharPtr buffer,
unsigned total_size, std::size_t content_size, wchar_t fill) {
std::size_t padding = total_size - content_size; std::size_t padding = total_size - content_size;
std::size_t left_padding = padding / 2; std::size_t left_padding = padding / 2;
std::fill_n(buffer, left_padding, fill); std::fill_n(buffer, left_padding, fill);
...@@ -152,8 +153,8 @@ void fmt::BasicWriter<Char>::FormatDecimal( ...@@ -152,8 +153,8 @@ void fmt::BasicWriter<Char>::FormatDecimal(
// "Three Optimization Tips for C++". See speed-test for a comparison. // "Three Optimization Tips for C++". See speed-test for a comparison.
unsigned index = (value % 100) * 2; unsigned index = (value % 100) * 2;
value /= 100; value /= 100;
buffer[num_digits] = internal::DIGITS[index + 1]; buffer[num_digits] = DIGITS[index + 1];
buffer[num_digits - 1] = internal::DIGITS[index]; buffer[num_digits - 1] = DIGITS[index];
num_digits -= 2; num_digits -= 2;
} }
if (value < 10) { if (value < 10) {
...@@ -161,8 +162,8 @@ void fmt::BasicWriter<Char>::FormatDecimal( ...@@ -161,8 +162,8 @@ void fmt::BasicWriter<Char>::FormatDecimal(
return; return;
} }
unsigned index = static_cast<unsigned>(value * 2); unsigned index = static_cast<unsigned>(value * 2);
buffer[1] = internal::DIGITS[index + 1]; buffer[1] = DIGITS[index + 1];
buffer[0] = internal::DIGITS[index]; buffer[0] = DIGITS[index];
} }
template <typename Char> template <typename Char>
...@@ -651,8 +652,9 @@ template void fmt::BasicWriter<char>::FormatDouble<double>( ...@@ -651,8 +652,9 @@ template void fmt::BasicWriter<char>::FormatDouble<double>(
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 &spec, int precision);
template fmt::BasicWriter<char>::CharPtr fmt::BasicWriter<char>::FillPadding( template fmt::BasicWriter<char>::CharPtr
CharPtr buffer, unsigned total_size, std::size_t content_size, char fill); fmt::BasicWriter<char>::FillPadding(CharPtr buffer,
unsigned total_size, std::size_t content_size, wchar_t fill);
template void fmt::BasicWriter<char>::FormatDecimal( template void fmt::BasicWriter<char>::FormatDecimal(
CharPtr buffer, uint64_t value, unsigned num_digits); CharPtr buffer, uint64_t value, unsigned num_digits);
...@@ -683,8 +685,8 @@ template void fmt::BasicWriter<wchar_t>::FormatDouble<long double>( ...@@ -683,8 +685,8 @@ template void fmt::BasicWriter<wchar_t>::FormatDouble<long double>(
long double value, const FormatSpec &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( fmt::BasicWriter<wchar_t>::FillPadding(CharPtr buffer,
CharPtr buffer, unsigned total_size, std::size_t content_size, char fill); unsigned total_size, std::size_t content_size, wchar_t fill);
template void fmt::BasicWriter<wchar_t>::FormatDecimal( template void fmt::BasicWriter<wchar_t>::FormatDecimal(
CharPtr buffer, uint64_t value, unsigned num_digits); CharPtr buffer, uint64_t value, unsigned num_digits);
......
...@@ -169,8 +169,6 @@ struct IsLongDouble { enum {VALUE = 0}; }; ...@@ -169,8 +169,6 @@ struct IsLongDouble { enum {VALUE = 0}; };
template <> template <>
struct IsLongDouble<long double> { enum {VALUE = 1}; }; struct IsLongDouble<long double> { enum {VALUE = 1}; };
extern const char DIGITS[];
void ReportUnknownType(char code, const char *type); void ReportUnknownType(char code, const char *type);
// Returns the number of decimal digits in n. Leading zeros are not counted // Returns the number of decimal digits in n. Leading zeros are not counted
...@@ -281,18 +279,20 @@ struct TypeSpec : Spec { ...@@ -281,18 +279,20 @@ struct TypeSpec : Spec {
struct WidthSpec { struct WidthSpec {
unsigned width_; unsigned width_;
char fill_; // Fill is always wchar_t and cast to char if necessary to avoid having
// two specialization of WidthSpec and its subclasses.
wchar_t fill_;
WidthSpec(unsigned width, char fill) : width_(width), fill_(fill) {} WidthSpec(unsigned width, wchar_t fill) : width_(width), fill_(fill) {}
unsigned width() const { return width_; } unsigned width() const { return width_; }
char fill() const { return fill_; } wchar_t fill() const { return fill_; }
}; };
struct AlignSpec : WidthSpec { struct AlignSpec : WidthSpec {
Alignment align_; Alignment align_;
AlignSpec(unsigned width, char fill) AlignSpec(unsigned width, wchar_t fill)
: WidthSpec(width, fill), align_(ALIGN_DEFAULT) {} : WidthSpec(width, fill), align_(ALIGN_DEFAULT) {}
Alignment align() const { return align_; } Alignment align() const { return align_; }
...@@ -300,7 +300,7 @@ struct AlignSpec : WidthSpec { ...@@ -300,7 +300,7 @@ struct AlignSpec : WidthSpec {
template <char TYPE> template <char TYPE>
struct AlignTypeSpec : AlignSpec { struct AlignTypeSpec : AlignSpec {
AlignTypeSpec(unsigned width, char fill) : AlignSpec(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; }
...@@ -313,7 +313,7 @@ struct FormatSpec : AlignSpec { ...@@ -313,7 +313,7 @@ struct FormatSpec : AlignSpec {
unsigned flags_; unsigned flags_;
char type_; char type_;
FormatSpec(unsigned width = 0, char type = 0, char fill = ' ') FormatSpec(unsigned width = 0, char type = 0, wchar_t fill = ' ')
: AlignSpec(width, fill), flags_(0), type_(type) {} : AlignSpec(width, fill), flags_(0), type_(type) {}
Alignment align() const { return align_; } Alignment align() const { return align_; }
...@@ -368,7 +368,7 @@ IntFormatter<int, TypeSpec<'X'> > hexu(int value); ...@@ -368,7 +368,7 @@ IntFormatter<int, TypeSpec<'X'> > hexu(int value);
*/ */
template <char TYPE_CODE> template <char TYPE_CODE>
IntFormatter<int, AlignTypeSpec<TYPE_CODE> > pad( IntFormatter<int, AlignTypeSpec<TYPE_CODE> > pad(
int value, unsigned width, char fill = ' '); int value, unsigned width, wchar_t fill = ' ');
#define DEFINE_INT_FORMATTERS(TYPE) \ #define DEFINE_INT_FORMATTERS(TYPE) \
inline IntFormatter<TYPE, TypeSpec<'o'> > oct(TYPE value) { \ inline IntFormatter<TYPE, TypeSpec<'o'> > oct(TYPE value) { \
...@@ -386,13 +386,13 @@ inline IntFormatter<TYPE, TypeSpec<'X'> > hexu(TYPE value) { \ ...@@ -386,13 +386,13 @@ inline IntFormatter<TYPE, TypeSpec<'X'> > hexu(TYPE value) { \
template <char TYPE_CODE> \ template <char TYPE_CODE> \
inline IntFormatter<TYPE, AlignTypeSpec<TYPE_CODE> > pad( \ inline IntFormatter<TYPE, AlignTypeSpec<TYPE_CODE> > pad( \
IntFormatter<TYPE, TypeSpec<TYPE_CODE> > f, \ IntFormatter<TYPE, TypeSpec<TYPE_CODE> > f, \
unsigned width, char fill = ' ') { \ unsigned width, wchar_t fill = ' ') { \
return IntFormatter<TYPE, AlignTypeSpec<TYPE_CODE> >( \ return IntFormatter<TYPE, AlignTypeSpec<TYPE_CODE> >( \
f.value(), AlignTypeSpec<TYPE_CODE>(width, fill)); \ f.value(), AlignTypeSpec<TYPE_CODE>(width, fill)); \
} \ } \
\ \
inline IntFormatter<TYPE, AlignTypeSpec<0> > pad( \ inline IntFormatter<TYPE, AlignTypeSpec<0> > pad( \
TYPE value, unsigned width, char fill = ' ') { \ TYPE value, unsigned width, wchar_t fill = ' ') { \
return IntFormatter<TYPE, AlignTypeSpec<0> >( \ return IntFormatter<TYPE, AlignTypeSpec<0> >( \
value, AlignTypeSpec<0>(width, fill)); \ value, AlignTypeSpec<0>(width, fill)); \
} }
...@@ -430,7 +430,7 @@ class BasicWriter { ...@@ -430,7 +430,7 @@ class BasicWriter {
CharPtr buffer, uint64_t value, unsigned num_digits); CharPtr buffer, uint64_t value, unsigned num_digits);
static CharPtr FillPadding(CharPtr buffer, static CharPtr FillPadding(CharPtr buffer,
unsigned total_size, std::size_t content_size, char fill); unsigned total_size, std::size_t content_size, wchar_t fill);
// Grows the buffer by n characters and returns a pointer to the newly // Grows the buffer by n characters and returns a pointer to the newly
// allocated area. // allocated area.
......
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