diff --git a/format.h b/format.h index fb7eaf02..6cff0554 100644 --- a/format.h +++ b/format.h @@ -389,13 +389,14 @@ struct FormatSpec : AlignSpec { char type() const { return type_; } }; +// An integer format specifier. template -class IntFormatter : public SpecT { +class IntFormatSpec : public SpecT { private: T value_; public: - IntFormatter(T value, const SpecT &spec = SpecT()) + IntFormatSpec(T value, const SpecT &spec = SpecT()) : SpecT(spec), value_(value) {} T value() const { return value_; } @@ -414,31 +415,32 @@ class StrFormatter : public AlignSpec { }; /** - Returns an integer formatter that formats the value in base 2. + Returns an integer format specifier to format the value in base 2. */ -IntFormatter > bin(int value); +IntFormatSpec > bin(int value); /** - Returns an integer formatter that formats the value in base 8. + Returns an integer format specifier to format the value in base 8. */ -IntFormatter > oct(int value); +IntFormatSpec > oct(int value); /** - Returns an integer formatter that formats the value in base 16 using + Returns an integer format specifier to format the value in base 16 using lower-case letters for the digits above 9. */ -IntFormatter > hex(int value); +IntFormatSpec > hex(int value); /** - Returns an integer formatter that formats the value in base 16 using + Returns an integer formatter format specifier to format in base 16 using upper-case letters for the digits above 9. */ -IntFormatter > hexu(int value); +IntFormatSpec > hexu(int value); /** \rst - Returns an integer formatter that pads the formatted argument with the fill - character to the specified width using the default (right) numeric alignment. + Returns an integer format specifier to pad the formatted argument with the + fill character to the specified width using the default (right) numeric + alignment. **Example**:: @@ -448,37 +450,37 @@ IntFormatter > hexu(int value); \endrst */ template -IntFormatter > pad( +IntFormatSpec > pad( int value, unsigned width, wchar_t fill = ' '); #define DEFINE_INT_FORMATTERS(TYPE) \ -inline IntFormatter > bin(TYPE value) { \ - return IntFormatter >(value, TypeSpec<'b'>()); \ +inline IntFormatSpec > bin(TYPE value) { \ + return IntFormatSpec >(value, TypeSpec<'b'>()); \ } \ \ -inline IntFormatter > oct(TYPE value) { \ - return IntFormatter >(value, TypeSpec<'o'>()); \ +inline IntFormatSpec > oct(TYPE value) { \ + return IntFormatSpec >(value, TypeSpec<'o'>()); \ } \ \ -inline IntFormatter > hex(TYPE value) { \ - return IntFormatter >(value, TypeSpec<'x'>()); \ +inline IntFormatSpec > hex(TYPE value) { \ + return IntFormatSpec >(value, TypeSpec<'x'>()); \ } \ \ -inline IntFormatter > hexu(TYPE value) { \ - return IntFormatter >(value, TypeSpec<'X'>()); \ +inline IntFormatSpec > hexu(TYPE value) { \ + return IntFormatSpec >(value, TypeSpec<'X'>()); \ } \ \ template \ -inline IntFormatter > pad( \ - IntFormatter > f, \ +inline IntFormatSpec > pad( \ + IntFormatSpec > f, \ unsigned width, wchar_t fill = ' ') { \ - return IntFormatter >( \ + return IntFormatSpec >( \ f.value(), AlignTypeSpec(width, fill)); \ } \ \ -inline IntFormatter > pad( \ +inline IntFormatSpec > pad( \ TYPE value, unsigned width, wchar_t fill = ' ') { \ - return IntFormatter >( \ + return IntFormatSpec >( \ value, AlignTypeSpec<0>(width, fill)); \ } @@ -584,6 +586,7 @@ class BasicWriter { template void FormatDouble(T value, const FormatSpec &spec, int precision); + // Formats a string. template CharPtr FormatString( const StringChar *s, std::size_t size, const AlignSpec &spec); @@ -651,20 +654,21 @@ class BasicWriter { BasicFormatter Format(StringRef format); BasicWriter &operator<<(int value) { - return *this << IntFormatter >(value, TypeSpec<0>()); + return *this << IntFormatSpec >(value, TypeSpec<0>()); } BasicWriter &operator<<(unsigned value) { - return *this << IntFormatter >(value, TypeSpec<0>()); + return *this << IntFormatSpec >(value, TypeSpec<0>()); } BasicWriter &operator<<(long value) { - return *this << IntFormatter >(value, TypeSpec<0>()); + return *this << IntFormatSpec >(value, TypeSpec<0>()); } BasicWriter &operator<<(unsigned long value) { - return *this << - IntFormatter >(value, TypeSpec<0>()); + return *this + << IntFormatSpec >(value, TypeSpec<0>()); } BasicWriter &operator<<(long long value) { - return *this << IntFormatter >(value, TypeSpec<0>()); + return *this + << IntFormatSpec >(value, TypeSpec<0>()); } /** @@ -672,7 +676,7 @@ class BasicWriter { */ BasicWriter &operator<<(unsigned long long value) { return *this << - IntFormatter >(value, TypeSpec<0>()); + IntFormatSpec >(value, TypeSpec<0>()); } BasicWriter &operator<<(double value) { @@ -705,7 +709,7 @@ class BasicWriter { } template - BasicWriter &operator<<(const IntFormatter &f); + BasicWriter &operator<<(const IntFormatSpec &spec); template BasicWriter &operator<<(const StrFormatter &f) { @@ -749,8 +753,8 @@ typename BasicWriter::CharPtr BasicWriter::FormatString( template template BasicWriter &BasicWriter::operator<<( - const IntFormatter &f) { - T value = f.value(); + const IntFormatSpec &spec) { + T value = spec.value(); unsigned size = 0; char sign = 0; typedef typename internal::IntTraits::UnsignedType UnsignedType; @@ -759,64 +763,64 @@ BasicWriter &BasicWriter::operator<<( sign = '-'; ++size; abs_value = 0 - abs_value; - } else if (f.sign_flag()) { - sign = f.plus_flag() ? '+' : ' '; + } else if (spec.sign_flag()) { + sign = spec.plus_flag() ? '+' : ' '; ++size; } - switch (f.type()) { + switch (spec.type()) { case 0: case 'd': { unsigned num_digits = internal::CountDigits(abs_value); CharPtr p = - PrepareFilledBuffer(size + num_digits, f, sign) + 1 - num_digits; + PrepareFilledBuffer(size + num_digits, spec, sign) + 1 - num_digits; BasicWriter::FormatDecimal(p, abs_value, num_digits); break; } case 'x': case 'X': { UnsignedType n = abs_value; - bool print_prefix = f.hash_flag(); + bool print_prefix = spec.hash_flag(); if (print_prefix) size += 2; do { ++size; } while ((n >>= 4) != 0); - Char *p = GetBase(PrepareFilledBuffer(size, f, sign)); + Char *p = GetBase(PrepareFilledBuffer(size, spec, sign)); n = abs_value; - const char *digits = f.type() == 'x' ? + const char *digits = spec.type() == 'x' ? "0123456789abcdef" : "0123456789ABCDEF"; do { *p-- = digits[n & 0xf]; } while ((n >>= 4) != 0); if (print_prefix) { - *p-- = f.type(); + *p-- = spec.type(); *p = '0'; } break; } case 'b': case 'B': { UnsignedType n = abs_value; - bool print_prefix = f.hash_flag(); + bool print_prefix = spec.hash_flag(); if (print_prefix) size += 2; do { ++size; } while ((n >>= 1) != 0); - Char *p = GetBase(PrepareFilledBuffer(size, f, sign)); + Char *p = GetBase(PrepareFilledBuffer(size, spec, sign)); n = abs_value; do { *p-- = '0' + (n & 1); } while ((n >>= 1) != 0); if (print_prefix) { - *p-- = f.type(); + *p-- = spec.type(); *p = '0'; } break; } case 'o': { UnsignedType n = abs_value; - bool print_prefix = f.hash_flag(); + bool print_prefix = spec.hash_flag(); if (print_prefix) ++size; do { ++size; } while ((n >>= 3) != 0); - Char *p = GetBase(PrepareFilledBuffer(size, f, sign)); + Char *p = GetBase(PrepareFilledBuffer(size, spec, sign)); n = abs_value; do { *p-- = '0' + (n & 7); @@ -826,7 +830,7 @@ BasicWriter &BasicWriter::operator<<( break; } default: - internal::ReportUnknownType(f.type(), "integer"); + internal::ReportUnknownType(spec.type(), "integer"); break; } return *this; @@ -1024,7 +1028,7 @@ class BasicFormatter { // Formats an integer. template void FormatInt(T value, const FormatSpec &spec) { - *writer_ << IntFormatter(value, spec); + *writer_ << IntFormatSpec(value, spec); } struct Proxy {