2 PROJECT: FlowerSoft C++ library
3 FILE : string-convert.cc
11 #include "libc-extension.hh"
13 #include "string-convert.hh"
14 #include "rational.hh"
18 A safe length for stringconversion buffers.
20 worst case would be %f printing HUGE (or 1/HUGE), which is approx
21 2e318, this number would have approx 318 zero's in its string.
23 Should enlarge buff dynamically.
28 static const int STRING_BUFFER_LEN
=1024;
31 String_convert::bool_string (bool b
)
33 return String (b
? "true" : "false");
37 String_convert::bin2hex (String bin_string
)
40 Byte
const* byte
= bin_string
.to_bytes ();
41 for (int i
= 0; i
< bin_string
.length (); i
++)
43 str
+= to_string ((char)nibble2hex_byte (*byte
>> 4));
44 str
+= to_string ((char)nibble2hex_byte (*byte
++));
50 String_convert::bin2int (String bin_string
)
52 return bin2unsigned (bin_string
);
56 String_convert::bin2unsigned (String bin_string
)
58 assert (bin_string
.length () <= (int)sizeof (unsigned));
60 unsigned result_u
= 0;
61 for (int i
= 0; i
< bin_string
.length (); i
++)
64 result_u
+= (Byte
)bin_string
[ i
];
69 // breendet imp from String
71 String_convert::dec2int (String dec_string
)
73 if (!dec_string
.length ())
77 int conv
= sscanf (dec_string
.to_str0 (), "%ld", &l
);
84 String_convert::i64_string (I64 i64
, char const* fmt
)
86 char buffer
[STRING_BUFFER_LEN
];
87 snprintf (buffer
, STRING_BUFFER_LEN
,
88 (fmt
? fmt
: "%Ld"), i64
); // assume radix 10
89 return String (buffer
);
92 // breendet imp from String
94 String_convert::dec2double (String dec_string
)
96 if (!dec_string
.length ())
99 int conv
= sscanf (dec_string
.to_str0 (), "%lf", &d
);
105 String_convert::hex2bin (String hex_string
, String
& bin_string_r
)
107 if (hex_string
.length () % 2)
108 hex_string
= "0" + hex_string
;
111 Byte
const* byte
= hex_string
.to_bytes ();
113 while (i
< hex_string
.length ())
115 int high_i
= hex2nibble (*byte
++);
116 int low_i
= hex2nibble (*byte
++);
117 if (high_i
< 0 || low_i
< 0)
118 return 1; // illegal char
119 bin_string_r
+= to_string ((char) (high_i
<< 4 | low_i
), 1 );
126 String_convert::hex2bin (String hex_string
)
129 // silly, asserts should alway be "on"!
130 // assert (!hex2bin (hex_string, str) );
131 int error_i
= hex2bin (hex_string
, str
);
137 String_convert::hex2nibble (Byte byte
)
139 if (byte
>= '0' && byte
<= '9')
141 if (byte
>= 'A' && byte
<= 'F')
142 return byte
- 'A' + 10;
143 if (byte
>= 'a' && byte
<= 'f')
144 return byte
- 'a' + 10;
148 // stupido. Should use int_string ()
150 String_convert::int2dec (int i
, int length_i
, char ch
)
157 String dec_string
= to_string (i
);
160 return to_string (fill_char
, length_i
- dec_string
.length ()) + dec_string
;
164 // stupido. Should use int_string ()
166 String_convert::unsigned2hex (unsigned u
, int length_i
, char fill_char
)
175 str
= to_string ((char) ((u
% 16)["0123456789abcdef"] ) ) + str
;
179 str
+= int_string (u
, "%x"); // hmm. %lx vs. %x -> portability?
182 str
= to_string (fill_char
, length_i
- str
.length ()) + str
;
183 while ((str
.length () > length_i
) && (str
[ 0 ] == 'f' ) )
184 str
= str
.cut_string (2, INT_MAX
);
190 String_convert::int2hex (int i
, int length_i
, char fill_char
)
192 return unsigned2hex ((unsigned)i
, length_i
, fill_char
);
196 String_convert::nibble2hex_byte (Byte byte
)
198 if ((byte
& 0x0f) <= 9 )
199 return (byte
& 0x0f) + '0';
201 return (byte
& 0x0f) - 10 + 'a';
204 Convert an integer to a string
207 #fmt# is a printf style format, default assumes "%d" as format.
210 String_convert::int_string (int i
, char const* fmt
)
212 char buffer
[STRING_BUFFER_LEN
];
213 snprintf (buffer
, STRING_BUFFER_LEN
,
214 (fmt
? fmt
: "%d"), i
); // assume radix 10
215 return String (buffer
);
219 String_convert::form_string (char const* format
, ...)
222 va_start (args
, format
);
223 char buffer
[STRING_BUFFER_LEN
];
224 vsnprintf (buffer
, STRING_BUFFER_LEN
, format
, args
);
226 return String (buffer
);
230 String_convert::vform_string (char const* format
, va_list args
)
232 char buffer
[STRING_BUFFER_LEN
];
233 vsnprintf (buffer
, STRING_BUFFER_LEN
, format
, args
);
234 return String (buffer
);
238 Convert a double to a string.
240 @param #fmt# is a printf style format, default assumes "%lf" as format
243 String_convert::double_string (double f
, char const* fmt
)
245 char buf
[STRING_BUFFER_LEN
];
247 snprintf (buf
, STRING_BUFFER_LEN
, fmt
? fmt
: "%f", f
);
252 Make a string from a single character.
255 #n# is a repetition count, default value is 1
258 String_convert::char_string (char c
, int n
)
261 char* ch
= new char[ n
];
263 String
s ((Byte
*)ch
, n
);
269 String_convert::rational_string (Rational r
)
271 return r
.to_string ();
275 String_convert::pointer_string (void const *l
)
277 char buffer
[STRING_BUFFER_LEN
];
278 snprintf (buffer
, STRING_BUFFER_LEN
, "%p", l
); // assume radix 10
279 return String (buffer
);
283 Convert a double to a string.
286 #n# is the number of nonzero digits
289 String_convert::precision_string (double x
, int n
)
291 String format
= "%." + to_string (0 >? n
- 1) + "e";
292 String str
= double_string (abs (x
), format
.to_str0 ());
294 int exp
= str
.right_string (3).to_int ();
295 str
= str
.left_string (str
.length () - 4);
297 while (str
[str
.length () - 1] == '0')
298 str
= str
.left_string (str
.length () - 1);
299 if (str
[str
.length () - 1] == '.')
300 str
= str
.left_string (str
.length () - 1);
303 return (sign (x
) > 0 ? str
: "-" + str
);
305 str
= str
.left_string (1) + str
.cut_string (2, INT_MAX
);
308 str
= "0." + to_string ('0', -dot
) + str
;
309 else if (dot
>= str
.length ())
310 str
+= to_string ('0', dot
- str
.length ());
311 else if (( dot
> 0) && (dot
< str
.length ()))
312 str
= str
.left_string (dot
) + "." + str
.cut_string (dot
, INT_MAX
);
316 return (sign (x
) > 0 ? str
: "-" + str
);
320 String_convert::split (String str
, char c
)
323 int i
= str
.index (c
);
326 String s
= str
.left_string (i
);
328 while (str
[++i
] == c
)
330 str
= str
.cut_string (i
, INT_MAX
);
340 String_convert::long_string (long l
)
342 char s
[STRING_BUFFER_LEN
];
343 sprintf (s
,"%ld", l
);
348 String_convert::pad_to (String s
, int n
)
350 return s
+ to_string (' ' , (n
- s
.length ()) >? 0);