2 * UCW Library -- Conversions of Strings to Numbers
4 * (c) 2010 Daniel Fiala <danfiala@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 * This is not a normal header file, it is a generator of a function for
12 * converting strings to integers of a certain type. This file should be used
13 * by ucw/stronum.c only.
16 #define STN_DECLARE(type, suffix) STN_DECLARE_CONVERTOR(type, suffix)
18 #define S_HELPER2(name, suffix) name##suffix
19 #define S_HELPER1(name, suffix) S_HELPER2(name, suffix)
20 #define S(name) S_HELPER1(name, STN_SUFFIX)
22 #define STN_MAX ((STN_TYPE)(-1))
23 static const STN_TYPE S(tops)[STN_DBASES_MASK+1] = { [2] = STN_MAX/2, [8] = STN_MAX/8, [10] = STN_MAX/10, [16] = STN_MAX/16 };
25 static const char *S(parse_string)(const char **pp, const uns flags, const uns sign, const uns base, STN_TYPE *num)
27 const STN_TYPE max = STN_MAX;
28 const STN_TYPE top = S(tops)[base];
30 return "Unknown base";
32 const STN_TYPE sign_max = ((flags & STN_SIGNED) || sign) ? max/2 + sign : max;
39 const uns c = (byte)**pp;
43 if (flags & STN_UNDERSCORE)
49 const uns d = get_digit(c);
59 if ( (overflow = (v > top || (v *= base) > sign_max - d)) )
67 return "Number contains no digits";
74 if (flags & STN_TRUNC)
77 return "Numeric overflow";
80 if ((flags & STN_WHOLE) && **pp)
81 return "Invalid character";
89 STN_DECLARE(STN_TYPE, STN_SUFFIX)
91 const char *err = NULL;
94 err = str_to_num_init(&str, flags, &sign, &base);
97 err = S(parse_string)(&str, flags, sign, base, num);