1 #ifndef DOZERG_TO_STRING_H_20130411 2 #define DOZERG_TO_STRING_H_20130411 38 #include "template.hh" 39 #include "impl/to_string_impl.hh" 52 uint8_t adjustleft_:1;
55 __Flags():fill_(
' '),width_(0),base_(0),nobase_(0),autocase_(0),adjustleft_(0),boolnum_(0),showpos_(0){}
58 static const uint8_t kDec = 0;
59 static const uint8_t kHex = 1;
60 static const uint8_t kOct = 2;
61 static const uint8_t kBin = 3;
64 static const uint8_t kAutoCase = 0;
65 static const uint8_t kUpperCase = 1;
66 static const uint8_t kLowerCase = 2;
68 void boolalpha(
bool c){flags_.boolnum_ = (c ? 0 : 1);}
69 bool boolalpha()
const{
return (0 == flags_.boolnum_);}
72 void width(
int n){flags_.width_ = (n >= 0 ? (n <= 255 ? n : 255) : 0);}
73 int width()
const{
return flags_.width_;}
75 void adjustright(
bool v){flags_.adjustleft_ = (v ? 0 : 1);}
76 bool adjustright()
const{
return (0 == flags_.adjustleft_);}
78 void fill(
char c){flags_.fill_ = c;}
79 char fill()
const{
return flags_.fill_;}
89 case 16:flags_.base_ = kHex;
break;
90 case 8:flags_.base_ = kOct;
break;
91 case 2:flags_.base_ = kBin;
break;
92 default:flags_.base_ = kDec;
95 int base()
const{
return (kDec == fbase() ? 10 : (kHex == fbase() ? 16 : (kOct == fbase() ? 8 : 2)));}
97 void showbase(
bool v){flags_.nobase_ = (v ? 0 : 1);}
98 bool showbase()
const{
return (0 == flags_.nobase_);}
105 void charcase(
int v){flags_.autocase_ = (v & 3);}
106 int charcase()
const{
return (flags_.autocase_ & 3);}
108 void showpos(
bool c){flags_.showpos_ = (c ? 1 : 0);}
109 bool showpos()
const{
return (0 != flags_.showpos_);}
111 __Myt & operator <<(
bool c){
113 return (c ? printStr(
"true", 4) : printStr(
"false", 5));
114 return printChar(c ?
'1' :
'0');
116 __Myt & operator <<(
char c){
return printChar(c);}
117 __Myt & operator <<(
signed char c){
return printChar(c);}
118 __Myt & operator <<(
unsigned char c){
return printChar(c);}
119 __Myt & operator <<(
short c){
return printInt(c);}
120 __Myt & operator <<(
unsigned short c){
return printInt(c);}
121 __Myt & operator <<(
int c){
return printInt(c);}
122 __Myt & operator <<(
unsigned int c){
return printInt(c);}
123 __Myt & operator <<(
long c){
return printInt(c);}
124 __Myt & operator <<(
unsigned long c){
return printInt(c);}
125 __Myt & operator <<(
long long c){
return printInt(c);}
126 __Myt & operator <<(
unsigned long long c){
return printInt(c);}
127 __Myt & operator <<(
float c);
128 __Myt & operator <<(
double c);
129 __Myt & operator <<(
const char * c){
return printStr(c);}
130 __Myt & operator <<(
char * c){
return operator <<(static_cast<const char *>(c));}
131 __Myt & operator <<(
const std::string & c){
134 return printStr(c.c_str(), c.length());
136 __Myt & operator <<(
const void * c){
return printInt(reinterpret_cast<long>(c),
true, kHex, kLowerCase);}
137 __Myt & operator <<(
void * c){
return operator <<(static_cast<const void *>(c));}
139 __Myt & operator <<(
const NS_IMPL::CToStringBase & c){
143 __Myt & operator <<(
const NS_IMPL::CToStringWidth & c){
147 __Myt & operator <<(
const NS_IMPL::CToStringFill & c){
151 __Myt & operator <<(__Myt & (*func)(__Myt &)){
157 void str(
const std::string & str){buf_ = str;}
158 const std::string & str()
const{
return buf_;}
160 uint8_t fbase()
const{
return flags_.base_;};
161 static bool baseUpper(
int ccase){
return (kUpperCase == ccase);}
162 static bool valueUpper(
int ccase){
return (kLowerCase != ccase);}
163 template<
typename Int>
164 bool realShowPos(Int c)
const{
165 typedef std::numeric_limits<Int> __Limits;
166 return (__Limits::is_signed
167 #ifdef __0_NO_POSITIVE
172 template<
typename Int>
173 __Myt & printInt(Int c){
return printInt(c, showbase(), fbase(), charcase());}
174 template<
typename Int>
175 __Myt & printInt(Int c,
bool showbase, uint8_t base,
int ccase){
177 char * e = tmp +
sizeof tmp;
182 s = int2Str(e, c, base, showbase, ccase);
186 __Myt & printChar(
char c){
190 __Myt & printStr(
const char * c,
size_t len = 0){
192 return printStr(
"(NULL)", 6);
199 template<
typename Int>
200 char * int2Str10(
char * e, Int c){
203 const char *
const kDig99 =
204 "00010203040506070809" 205 "10111213141516171819" 206 "20212223242526272829" 207 "30313233343536373839" 208 "40414243444546474849" 209 "50515253545556575859" 210 "60616263646566676869" 211 "70717273747576777879" 212 "80818283848586878889" 213 "90919293949596979899";
215 const bool negtive = !(c > 0 || c == 0);
218 for(__Unsigned i;cc > 99;){
221 memcpy((e -= 2), kDig99 + ((i << 1) - cc * 200), 2);
226 memcpy((e -= 2), kDig99 + cc * 2, 2);
229 else if(realShowPos(c))
233 template<
typename Int>
234 char * int2Str(
char * e, Int c, uint8_t base,
bool showbase,
int ccase){
237 const bool sb = (showbase && 0 != c);
242 i = (valueUpper(ccase) ? 0 : 16);
244 *--e =
"0123456789ABCDEF0123456789abcdef"[(cc & 0xF) + i];
248 *--e = (baseUpper(ccase) ?
'X' :
'x');
254 *--e = (cc & 7) +
'0';
262 *--e = (cc & 1) +
'0';
266 *--e = (baseUpper(ccase) ?
'B' :
'b');
274 void setWidth(
const char * s,
size_t len){
278 const bool r = adjustright();
282 buf_.append(w - len, fill());
302 tos.boolalpha(
false);
306 inline NS_IMPL::CToStringWidth setw(
int w)
308 return NS_IMPL::CToStringWidth(w);
311 inline NS_IMPL::CToStringFill fill(
char c)
313 return NS_IMPL::CToStringFill(c);
318 tos.adjustright(
false);
324 tos.adjustright(
true);
328 inline NS_IMPL::CToStringBase setbase(
int base)
330 return NS_IMPL::CToStringBase(base);
335 return (tos<<setbase(10));
340 return (tos<<setbase(16));
345 return (tos<<setbase(8));
350 return (tos<<setbase(2));
367 tos.charcase(CToString::kAutoCase);
373 tos.charcase(CToString::kUpperCase);
379 tos.charcase(CToString::kLowerCase);
Definition: to_string.hh:43
Definition: template.hh:23
Manipulators for stream interface APIs.
Definition: data_stream.hh:41