LEFT | RIGHT |
1 /* | 1 /* |
2 PROJECT: FlowerSoft C++ library | 2 PROJECT: FlowerSoft C++ library |
3 FILE : string-convert.cc | 3 FILE : string-convert.cc |
4 | 4 |
5 --*/ | 5 --*/ |
6 | 6 |
7 #include "string-convert.hh" | 7 #include "string-convert.hh" |
8 | 8 |
9 #include <cstring> | 9 #include <cstring> |
10 #include <cstdio> | 10 #include <cstdio> |
11 using namespace std; | 11 using namespace std; |
12 | 12 |
13 #include "libc-extension.hh" | 13 #include "libc-extension.hh" |
14 #include "std-vector.hh" | 14 #include "std-vector.hh" |
15 | 15 |
16 /** | 16 /** |
17 A safe length for stringconversion buffers. | 17 A safe length for stringconversion buffers. |
18 | 18 |
19 worst case would be %f printing HUGE (or 1/HUGE), which is approx | 19 worst case would be %f printing HUGE (or 1/HUGE), which is approx |
20 2e318, this number would have approx 318 zero's in its string. | 20 2e318, this number would have approx 318 zero's in its string. |
21 | 21 |
22 Should enlarge buff dynamically. | 22 Should enlarge buff dynamically. |
23 | 23 |
24 @see | 24 @see |
25 man 3 snprintf | 25 man 3 snprintf |
26 */ | 26 */ |
27 static const int STRING_BUFFER_LEN = 1024; | 27 static const int STRING_BUFFER_LEN = 1024; |
28 | 28 |
29 string | 29 string |
30 String_convert::bool_string (bool b) | |
31 { | |
32 return string (b ? "true" : "false"); | |
33 } | |
34 | |
35 string | |
36 String_convert::bin2hex (Byte bin_char) | 30 String_convert::bin2hex (Byte bin_char) |
37 { | 31 { |
38 string str; | 32 string str; |
39 str += nibble2hex_byte ((Byte) (bin_char >> 4)); | 33 str += nibble2hex_byte ((Byte) (bin_char >> 4)); |
40 str += nibble2hex_byte (bin_char++); | 34 str += nibble2hex_byte (bin_char++); |
41 return str; | 35 return str; |
42 } | 36 } |
43 | 37 |
44 string | 38 string |
45 String_convert::bin2hex (const string &bin_string) | 39 String_convert::bin2hex (const string &bin_string) |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 if (!dec_string.length ()) | 74 if (!dec_string.length ()) |
81 return 0; | 75 return 0; |
82 | 76 |
83 long l = 0; | 77 long l = 0; |
84 if (!sscanf (dec_string.c_str (), "%ld", &l)) | 78 if (!sscanf (dec_string.c_str (), "%ld", &l)) |
85 assert (false); | 79 assert (false); |
86 | 80 |
87 return (int)l; | 81 return (int)l; |
88 } | 82 } |
89 | 83 |
90 string | |
91 String_convert::i64_string (I64 i64, char const *fmt) | |
92 { | |
93 char buffer[STRING_BUFFER_LEN]; | |
94 snprintf (buffer, STRING_BUFFER_LEN, | |
95 (fmt ? fmt : "%Ld"), i64); // assume radix 10 | |
96 return string (buffer); | |
97 } | |
98 // breendet imp from string | 84 // breendet imp from string |
99 double | 85 double |
100 String_convert::dec2double (const string &dec_string) | 86 String_convert::dec2double (const string &dec_string) |
101 { | 87 { |
102 if (!dec_string.length ()) | 88 if (!dec_string.length ()) |
103 return 0; | 89 return 0; |
104 | 90 |
105 double d = 0.0; | 91 double d = 0.0; |
106 if (!sscanf (dec_string.c_str (), "%lf", &d)) | 92 if (!sscanf (dec_string.c_str (), "%lf", &d)) |
107 assert (false); | 93 assert (false); |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 } | 219 } |
234 | 220 |
235 string | 221 string |
236 String_convert::vform_string (char const *format, va_list args) | 222 String_convert::vform_string (char const *format, va_list args) |
237 { | 223 { |
238 char buffer[STRING_BUFFER_LEN]; | 224 char buffer[STRING_BUFFER_LEN]; |
239 vsnprintf (buffer, STRING_BUFFER_LEN, format, args); | 225 vsnprintf (buffer, STRING_BUFFER_LEN, format, args); |
240 return string (buffer); | 226 return string (buffer); |
241 } | 227 } |
242 | 228 |
243 /** | |
244 Convert a double to a string. | |
245 | |
246 @param #fmt# is a printf style format, default assumes "%lf" as format | |
247 */ | |
248 string | |
249 String_convert::double_string (double f, char const *fmt) | |
250 { | |
251 char buf[STRING_BUFFER_LEN]; | |
252 | |
253 snprintf (buf, STRING_BUFFER_LEN, fmt ? fmt : "%f", f); | |
254 return string (buf); | |
255 } | |
256 | |
257 string | |
258 String_convert::long_string (long l) | |
259 { | |
260 char s[STRING_BUFFER_LEN]; | |
261 sprintf (s, "%ld", l); | |
262 return s; | |
263 } | |
264 | |
265 string | |
266 String_convert::unsigned_string (unsigned u) | |
267 { | |
268 char s[STRING_BUFFER_LEN]; | |
269 sprintf (s, "%u", u); | |
270 return s; | |
271 } | |
272 | |
273 string | |
274 String_convert::unsigned_long_string (unsigned long ul) | |
275 { | |
276 char s[STRING_BUFFER_LEN]; | |
277 sprintf (s, "%lu", ul); | |
278 return s; | |
279 } | |
280 | |
281 string | 229 string |
282 String_convert::pad_to (const string &s, size_t n) | 230 String_convert::pad_to (const string &s, size_t n) |
283 { | 231 { |
284 return s + string (max (int (n - s.length ()), 0), ' '); | 232 return s + string (max (int (n - s.length ()), 0), ' '); |
285 } | 233 } |
286 | 234 |
287 string | 235 string |
288 String_convert::to_upper (string s) | 236 String_convert::to_upper (string s) |
289 { | 237 { |
290 return strnupr (const_cast<char*>(s.c_str ()), s.length ()); | 238 return strnupr (const_cast<char*>(s.c_str ()), s.length ()); |
291 } | 239 } |
292 | 240 |
293 string | 241 string |
294 String_convert::to_lower (string s) | 242 String_convert::to_lower (string s) |
295 { | 243 { |
296 return strnlwr (const_cast<char*>(s.c_str ()), s.length ()); | 244 return strnlwr (const_cast<char*>(s.c_str ()), s.length ()); |
297 } | 245 } |
LEFT | RIGHT |