2 PROJECT: FlowerSoft C++ library
3 FILE : string-convert.cc
7 #include "string-convert.hh"
13 #include "libc-extension.hh"
14 #include "rational.hh"
15 #include "std-vector.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 = (Byte const*)bin_string.data ();
41 for (ssize 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 (ssize 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.c_str (), "%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);
91 // breendet imp from string
93 String_convert::dec2double (string dec_string)
95 if (!dec_string.length ())
98 int conv = sscanf (dec_string.c_str (), "%lf", &d);
104 String_convert::hex2bin (string hex_string, string &bin_string_r)
106 if (hex_string.length () % 2)
107 hex_string = "0" + hex_string;
110 Byte const *byte = (Byte const*) hex_string.data ();
112 while (i < hex_string.length ())
114 int high_i = hex2nibble (*byte++);
115 int low_i = hex2nibble (*byte++);
116 if (high_i < 0 || low_i < 0)
117 return 1; // illegal char
118 bin_string_r += to_string ((char) (high_i << 4 | low_i), 1);
125 String_convert::hex2bin (string hex_string)
128 // silly, asserts should alway be "on"!
129 // assert (!hex2bin (hex_string, str) );
130 int error_i = hex2bin (hex_string, str);
136 String_convert::hex2nibble (Byte byte)
138 if (byte >= '0' && byte <= '9')
140 if (byte >= 'A' && byte <= 'F')
141 return byte - 'A' + 10;
142 if (byte >= 'a' && byte <= 'f')
143 return byte - 'a' + 10;
147 // stupido. Should use int_string ()
149 String_convert::int2dec (int i, int length_i, char ch)
156 string dec_string = to_string (i);
159 return to_string (fill_char, length_i - dec_string.length ()) + dec_string;
162 // stupido. Should use int_string ()
164 String_convert::unsigned2hex (unsigned u, ssize length, char fill_char)
173 str = to_string ((char) ((u % 16)["0123456789abcdef"])) + str;
177 str += int_string (u, "%x"); // hmm. %lx vs. %x -> portability?
180 str = to_string (fill_char, length - str.length ()) + str;
181 while ((str.length () > length) && (str[ 0 ] == 'f'))
182 str = str.substr (2);
188 String_convert::int2hex (int i, int length_i, char fill_char)
190 return unsigned2hex ((unsigned)i, length_i, fill_char);
194 String_convert::nibble2hex_byte (Byte byte)
196 if ((byte & 0x0f) <= 9)
197 return (byte & 0x0f) + '0';
199 return (byte & 0x0f) - 10 + 'a';
202 Convert an integer to a string
205 #fmt# is a printf style format, default assumes "%d" as format.
208 String_convert::int_string (int i, char const *fmt)
210 char buffer[STRING_BUFFER_LEN];
211 snprintf (buffer, STRING_BUFFER_LEN,
212 (fmt ? fmt : "%d"), i); // assume radix 10
213 return string (buffer);
217 String_convert::form_string (char const *format, ...)
220 va_start (args, format);
221 char buffer[STRING_BUFFER_LEN];
222 vsnprintf (buffer, STRING_BUFFER_LEN, format, args);
224 return string (buffer);
228 String_convert::vform_string (char const *format, va_list args)
230 char buffer[STRING_BUFFER_LEN];
231 vsnprintf (buffer, STRING_BUFFER_LEN, format, args);
232 return string (buffer);
236 Convert a double to a string.
238 @param #fmt# is a printf style format, default assumes "%lf" as format
241 String_convert::double_string (double f, char const *fmt)
243 char buf[STRING_BUFFER_LEN];
245 snprintf (buf, STRING_BUFFER_LEN, fmt ? fmt : "%f", f);
250 Make a string from a single character.
253 #n# is a repetition count, default value is 1
256 String_convert::char_string (char c, int n)
259 char *ch = new char[ n ];
268 String_convert::rational_string (Rational r)
270 return r.to_string ();
274 String_convert::pointer_string (void const *l)
276 char buffer[STRING_BUFFER_LEN];
277 snprintf (buffer, STRING_BUFFER_LEN, "%p", l); // assume radix 10
278 return string (buffer);
282 Convert a double to a string.
285 #n# is the number of nonzero digits
288 String_convert::precision_string (double x, int n)
290 string format = "%." + to_string (max (0, n - 1)) + "e";
291 string str = double_string (abs (x), format.c_str ());
293 int exp = dec2int (str.substr (str.length () - 3));
294 str = str.substr (0, str.length () - 4);
296 while (str[str.length () - 1] == '0')
297 str = str.substr (0, str.length () - 1);
298 if (str[str.length () - 1] == '.')
299 str = str.substr (0, str.length () - 1);
302 return (sign (x) > 0 ? str : "-" + str);
304 str = str.substr (0, 1) + str.substr (2);
307 str = "0." + to_string ('0', -dot) + str;
308 else if (dot >= str.length ())
309 str += to_string ('0', dot - str.length ());
310 else if ((dot > 0) && (dot < str.length ()))
311 str = str.substr (0, dot) + "." + str.substr (dot);
315 return (sign (x) > 0 ? str : "-" + str);
320 String_convert::long_string (long l)
322 char s[STRING_BUFFER_LEN];
323 sprintf (s, "%ld", l);
328 String_convert::unsigned_string (unsigned u)
330 char s[STRING_BUFFER_LEN];
331 sprintf (s, "%u", u);
336 String_convert::pad_to (string s, int n)
338 return s + string (max (int(n - s.length ()), 0), ' ');
342 String_convert::to_upper (string s)
344 return strnupr ((char *)s.c_str (), s.length ());
348 String_convert::to_lower (string s)
350 return strnlwr ((char *)s.c_str (), s.length ());
354 String_convert::reverse (string s)
356 return (char*) memrev ((unsigned char *)s.data (), s.length ());