Merge pull request #4 from gcflymoto/master

Added support for binary modifier
This commit is contained in:
vitaut
2013-11-14 07:57:16 -08:00
3 changed files with 70 additions and 12 deletions
+37 -2
View File
@@ -369,6 +369,16 @@ class IntFormatter : public SpecT {
T value() const { return value_; }
};
/**
Returns an integer formatter that formats the value in base 2.
*/
IntFormatter<int, TypeSpec<'b'> > bin(int value);
/**
Returns an integer formatter that formats the value in base 2.
*/
IntFormatter<int, TypeSpec<'B'> > binu(int value);
/**
Returns an integer formatter that formats the value in base 8.
*/
@@ -403,6 +413,12 @@ IntFormatter<int, AlignTypeSpec<TYPE_CODE> > pad(
int value, unsigned width, wchar_t fill = ' ');
#define DEFINE_INT_FORMATTERS(TYPE) \
inline IntFormatter<TYPE, TypeSpec<'b'> > bin(TYPE value) { \
return IntFormatter<TYPE, TypeSpec<'b'> >(value, TypeSpec<'b'>()); \
} \
inline IntFormatter<TYPE, TypeSpec<'B'> > binu(TYPE value) { \
return IntFormatter<TYPE, TypeSpec<'B'> >(value, TypeSpec<'B'>()); \
} \
inline IntFormatter<TYPE, TypeSpec<'o'> > oct(TYPE value) { \
return IntFormatter<TYPE, TypeSpec<'o'> >(value, TypeSpec<'o'>()); \
} \
@@ -718,6 +734,25 @@ BasicWriter<Char> &BasicWriter<Char>::operator<<(
}
break;
}
case 'b': case 'B': {
UnsignedType n = abs_value;
bool print_prefix = f.hash_flag();
if (print_prefix) size += 2;
do {
++size;
} while ((n >>= 1) != 0);
Char *p = GetBase(PrepareFilledBuffer(size, f, sign));
n = abs_value;
const char *digits = "01";
do {
*p-- = digits[n & 0x1];
} while ((n >>= 1) != 0);
if (print_prefix) {
*p-- = f.type();
*p = '0';
}
break;
}
case 'o': {
UnsignedType n = abs_value;
bool print_prefix = f.hash_flag();
@@ -1155,8 +1190,8 @@ class FormatInt {
}
explicit FormatInt(unsigned value) : str_(FormatDecimal(value)) {}
const char *c_str() const { return str_; }
std::string str() const { return str_; }
inline const char *c_str() const { return str_; }
inline std::string str() const { return str_; }
};
/**