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 STN_DECLARE(STN_TYPE, STN_SUFFIX)
28 const char *err = NULL;
31 err = str_to_num_init(&p, flags, &sign, &base);
33 const char *parse_string(void)
35 const STN_TYPE max = STN_MAX;
36 const STN_TYPE top = S(tops)[base];
39 return err_unknown_base;
42 const STN_TYPE sign_max = ((flags & STN_SIGNED) || sign) ? max/2 + sign : max;
49 const uns c = (byte)*p;
53 if (flags & STN_UNDERSCORE)
59 const uns d = get_digit(c);
67 if ( (overflow = (v > top || (v *= base) > sign_max - d)) )
86 if (flags & STN_TRUNC)
90 return err_numeric_overflow;
94 if ((flags & STN_ZCHAR) && *p)
96 return err_invalid_character;
106 err = parse_string();