Doc: CG: Reorder programming sections
[lilypond/mpolesky.git] / flower / string-convert.cc
blob03bb05b319c57b291c11d2146170c630569957fc
1 /*
2 PROJECT: FlowerSoft C++ library
3 FILE : string-convert.cc
5 --*/
7 #include "string-convert.hh"
9 #include <cstring>
10 #include <cstdio>
11 using namespace std;
13 #include "libc-extension.hh"
14 #include "rational.hh"
15 #include "std-vector.hh"
17 /**
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.
25 @see
26 man 3 snprintf
28 static const int STRING_BUFFER_LEN = 1024;
30 string
31 String_convert::bool_string (bool b)
33 return string (b ? "true" : "false");
36 string
37 String_convert::bin2hex (Byte bin_char)
39 string str;
40 str += to_string ((char) nibble2hex_byte (bin_char >> 4));
41 str += to_string ((char) nibble2hex_byte (bin_char++));
42 return str;
45 string
46 String_convert::bin2hex (string bin_string)
48 string str;
49 Byte const *byte = (Byte const*)bin_string.data ();
50 for (ssize i = 0; i < bin_string.length (); i++)
52 str += to_string ((char)nibble2hex_byte (*byte >> 4));
53 str += to_string ((char)nibble2hex_byte (*byte++));
55 return str;
58 int
59 String_convert::bin2int (string bin_string)
61 return bin2unsigned (bin_string);
64 unsigned
65 String_convert::bin2unsigned (string bin_string)
67 assert (bin_string.length () <= (int)sizeof (unsigned));
69 unsigned result_u = 0;
70 for (ssize i = 0; i < bin_string.length (); i++)
72 result_u <<= 8;
73 result_u += (Byte)bin_string[ i ];
75 return result_u;
78 int
79 String_convert::dec2int (string dec_string)
81 if (!dec_string.length ())
82 return 0;
84 long l = 0;
85 if (!sscanf (dec_string.c_str (), "%ld", &l))
86 assert (false);
88 assert (form_string ("%ld", l) == dec_string);
90 return (int)l;
93 string
94 String_convert::i64_string (I64 i64, char const *fmt)
96 char buffer[STRING_BUFFER_LEN];
97 snprintf (buffer, STRING_BUFFER_LEN,
98 (fmt ? fmt : "%Ld"), i64); // assume radix 10
99 return string (buffer);
101 // breendet imp from string
102 double
103 String_convert::dec2double (string dec_string)
105 if (!dec_string.length ())
106 return 0;
108 double d = 0.0;
109 if (!sscanf (dec_string.c_str (), "%lf", &d))
110 assert (false);
112 return d;
116 String_convert::hex2bin (string hex_string, string &bin_string_r)
118 if (hex_string.length () % 2)
119 hex_string = "0" + hex_string;
121 bin_string_r = "";
122 Byte const *byte = (Byte const*) hex_string.data ();
123 ssize i = 0;
124 while (i < hex_string.length ())
126 int high_i = hex2nibble (*byte++);
127 int low_i = hex2nibble (*byte++);
128 if (high_i < 0 || low_i < 0)
129 return 1; // invalid char
130 bin_string_r += to_string ((char) (high_i << 4 | low_i), 1);
131 i += 2;
133 return 0;
136 string
137 String_convert::hex2bin (string hex_string)
139 string str;
141 if (hex2bin (hex_string, str))
142 assert (false);
144 return str;
148 String_convert::hex2nibble (Byte byte)
150 if (byte >= '0' && byte <= '9')
151 return byte - '0';
152 if (byte >= 'A' && byte <= 'F')
153 return byte - 'A' + 10;
154 if (byte >= 'a' && byte <= 'f')
155 return byte - 'a' + 10;
156 return -1;
159 // stupido. Should use int_string ()
160 string
161 String_convert::int2dec (int i, int length_i, char ch)
163 char fill_char = ch;
164 if (fill_char)
165 fill_char = '0';
167 // ugh
168 string dec_string = to_string (i);
170 // ugh
171 return to_string (fill_char, length_i - dec_string.length ()) + dec_string;
174 // stupido. Should use int_string ()
175 string
176 String_convert::unsigned2hex (unsigned u, ssize length, char fill_char)
178 string str;
179 if (!u)
180 str = "0";
182 #if 1 // both go...
183 while (u)
185 str = to_string ((char) ((u % 16)["0123456789abcdef"])) + str;
186 u /= 16;
188 #else
189 str += int_string (u, "%x"); // hmm. %lx vs. %x -> portability?
190 #endif
192 str = to_string (fill_char, length - str.length ()) + str;
193 while ((str.length () > length) && (str[ 0 ] == 'f'))
194 str = str.substr (2);
196 return str;
199 string
200 String_convert::int2hex (int i, int length_i, char fill_char)
202 return unsigned2hex ((unsigned)i, length_i, fill_char);
205 Byte
206 String_convert::nibble2hex_byte (Byte byte)
208 if ((byte & 0x0f) <= 9)
209 return (byte & 0x0f) + '0';
210 else
211 return (byte & 0x0f) - 10 + 'a';
214 Convert an integer to a string
216 @param
217 #fmt# is a printf style format, default assumes "%d" as format.
219 string
220 String_convert::int_string (int i, char const *fmt)
222 char buffer[STRING_BUFFER_LEN];
223 snprintf (buffer, STRING_BUFFER_LEN,
224 (fmt ? fmt : "%d"), i); // assume radix 10
225 return string (buffer);
228 string
229 String_convert::form_string (char const *format, ...)
231 va_list args;
232 va_start (args, format);
233 char buffer[STRING_BUFFER_LEN];
234 vsnprintf (buffer, STRING_BUFFER_LEN, format, args);
235 va_end (args);
236 return string (buffer);
239 string
240 String_convert::vform_string (char const *format, va_list args)
242 char buffer[STRING_BUFFER_LEN];
243 vsnprintf (buffer, STRING_BUFFER_LEN, format, args);
244 return string (buffer);
248 Convert a double to a string.
250 @param #fmt# is a printf style format, default assumes "%lf" as format
252 string
253 String_convert::double_string (double f, char const *fmt)
255 char buf[STRING_BUFFER_LEN];
257 snprintf (buf, STRING_BUFFER_LEN, fmt ? fmt : "%f", f);
258 return string (buf);
262 Make a string from a single character.
264 @param
265 #n# is a repetition count, default value is 1
267 string
268 String_convert::char_string (char c, int n)
270 n = n >= 0 ? n : 0;
271 char *ch = new char[ n ];
272 memset (ch, c, n);
273 string s (ch, n);
275 delete[] ch;
276 return s;
279 string
280 String_convert::rational_string (Rational r)
282 return r.to_string ();
285 string
286 String_convert::pointer_string (void const *l)
288 char buffer[STRING_BUFFER_LEN];
289 snprintf (buffer, STRING_BUFFER_LEN, "%p", l); // assume radix 10
290 return string (buffer);
294 Convert a double to a string.
296 @param
297 #n# is the number of nonzero digits
299 string
300 String_convert::precision_string (double x, int n)
302 string format = "%." + to_string (max (0, n - 1)) + "e";
303 string str = double_string (abs (x), format.c_str ());
305 int exp = dec2int (str.substr (str.length () - 3));
306 str = str.substr (0, str.length () - 4);
308 while (str[str.length () - 1] == '0')
309 str = str.substr (0, str.length () - 1);
310 if (str[str.length () - 1] == '.')
311 str = str.substr (0, str.length () - 1);
313 if (exp == 0)
314 return (sign (x) > 0 ? str : "-" + str);
316 str = str.substr (0, 1) + str.substr (2);
317 ssize dot = 1 + exp;
318 if (dot <= 0)
319 str = "0." + to_string ('0', -dot) + str;
320 else if (dot >= str.length ())
321 str += to_string ('0', dot - str.length ());
322 else if ((dot > 0) && (dot < str.length ()))
323 str = str.substr (0, dot) + "." + str.substr (dot);
324 else
325 assert (0);
327 return (sign (x) > 0 ? str : "-" + str);
331 string
332 String_convert::long_string (long l)
334 char s[STRING_BUFFER_LEN];
335 sprintf (s, "%ld", l);
336 return s;
339 string
340 String_convert::unsigned_string (unsigned u)
342 char s[STRING_BUFFER_LEN];
343 sprintf (s, "%u", u);
344 return s;
347 string
348 String_convert::pad_to (string s, int n)
350 return s + string (max (int(n - s.length ()), 0), ' ');
353 string
354 String_convert::to_upper (string s)
356 return strnupr ((char *)s.c_str (), s.length ());
359 string
360 String_convert::to_lower (string s)
362 return strnlwr ((char *)s.c_str (), s.length ());
365 string
366 String_convert::reverse (string s)
368 return (char*) memrev ((unsigned char *)s.data (), s.length ());