1 /***********************************************************************
3 * This software is part of the ast package *
4 * Copyright (c) 1985-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
13 * Information and Software Systems Research *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
19 * Phong Vo <kpv@research.att.com> *
21 ***********************************************************************/
27 * common header and implementation for
29 * strtol strtoul strton
30 * strtoll strtoull strtonll
31 * strntol strntoul strnton
32 * strntoll strntoull strntonll
34 * define these macros to instantiate an implementation:
36 * S2I_function the function name
37 * S2I_number the signed number type
38 * S2I_unumber the unsigned number type
39 * S2I_unsigned 1 for unsigned, 0 for signed
40 * S2I_qualifier 1 for optional qualifier suffix, 0 otherwise
41 * S2I_multiplier 1 for optional multiplier suffix, 0 otherwise
42 * S2I_size the second argument is the input string size
44 * convert string to number
45 * errno=ERANGE on overflow (LONG_MAX) or underflow (LONG_MIN)
46 * if non-null e will point to first unrecognized char in s
47 * if basep!=0 it points to the default base on input and
48 * will point to the explicit base on return
49 * a default base of 0 will determine the base from the input
50 * a default base of 1 will determine the base from the input using bb#*
51 * a base prefix in the string overrides *b
52 * *b will not be set if the string has no base prefix
53 * if m>1 and no multipler was specified then the result is multiplied by m
54 * if m<0 then multipliers are not consumed
55 * if a base arg or prefix is specified then multiplier is not consumed
57 * integer numbers are of the form:
59 * [sign][base][number[qualifier]][multiplier]
66 * number: [0-9a-zA-Z]*
75 * multiplier: . pseudo-float if m>1
78 * [gG] giga (1024*1024*1024)
80 * [mM] mega (1024*1024)
88 #if !__STD_C && !defined(const)
99 #define S2I_umax (~((S2I_unumber)0))
102 #define S2I_type S2I_unumber
104 #define S2I_max S2I_umax
106 #define S2I_type S2I_number
107 #define S2I_min (-S2I_max-1)
108 #define S2I_max (S2I_umax>>1)
112 #define S2I_valid(s) ((s)<(z))
114 #define S2I_valid(s) 1
117 #define ADDOVER(n,c,s) ((S2I_umax-(n))<((S2I_unumber)((c)+(s))))
118 #define MPYOVER(n,c) (((S2I_unumber)(n))>(S2I_umax/(c)))
120 static const S2I_unumber mm
[] =
189 #if defined(__EXPORT__)
190 #define extern __EXPORT__
197 S2I_function(const char* a
, size_t size
, char** e
, char* basep
, int m
)
199 S2I_function(a
, size
, e
, basep
, m
) const char* a
; size_t size
; char** e
; char* basep
; int m
;
203 S2I_function(const char* a
, size_t size
, char** e
, int base
)
205 S2I_function(a
, size
, e
, base
) const char* a
; size_t size
; char** e
; int base
;
211 S2I_function(const char* a
, char** e
, char* basep
, int m
)
213 S2I_function(a
, e
, basep
, m
) const char* a
; char** e
; char* basep
; int m
;
217 S2I_function(const char* a
, char** e
, int base
)
219 S2I_function(a
, e
, base
) const char* a
; char** e
; int base
;
224 register unsigned char* s
= (unsigned char*)a
;
226 register unsigned char* z
= s
+ size
;
228 register S2I_unumber n
;
229 register S2I_unumber x
;
232 register unsigned char* p
;
233 register unsigned char* cv
;
249 base
= basep
? *((unsigned char*)basep
) : 0;
251 if (base
> 36 && base
<= SF_RADIX
)
253 static int conformance
= -1;
256 conformance
= !strcmp(astconf("CONFORMANCE", NiL
, NiL
), "standard");
261 if (base
&& (base
< 2 || base
> SF_RADIX
))
266 while (S2I_valid(s
) && isspace(*s
))
268 if ((negative
= S2I_valid(s
) && (*s
== '-')) || S2I_valid(s
) && *s
== '+')
275 if (S2I_valid(p
) && (c
= *p
++) >= '0' && c
<= '9')
278 if (S2I_valid(p
) && (c
= *p
) >= '0' && c
<= '9')
280 n
= (n
<< 3) + (n
<< 1) + c
- '0';
283 if (S2I_valid(p
) && *p
== '#')
285 if (n
>= 2 && n
<= 64)
293 else if (S2I_valid(s
) && *s
== '0' && S2I_valid(s
+ 1))
295 if ((c
= *(s
+ 1)) == 'x' || c
== 'X')
300 else if (c
>= '0' && c
<= '7')
309 else if (base
< 2 || base
> SF_RADIX
)
329 * this part transcribed from sfvscanf()
332 SFSETLOCALE(&decimal
, &thousand
);
341 if (S2I_valid(s
) && (c
= *s
++) >= '0' && c
<= '9')
347 n
= (n
<< 3) + (n
<< 1);
349 if (ADDOVER(n
, c
, negative
))
354 else if (p
&& (s
- p
) != (3 + S2I_valid(s
)))
361 else if (!S2I_valid(s
) || c
!= thousand
)
363 else if (!p
&& (s
- b
) > 4)
373 n
= negative
? S2I_min
: S2I_max
;
388 cv
= base
<= 36 ? _Sfcv36
: _Sfcv64
;
389 if ((base
& ~(base
- 1)) == base
)
395 shift
= base
< 4 ? 1 : 2;
397 shift
= base
< 16 ? 3 : 4;
399 shift
= base
< 64 ? 5 : 6;
400 while (S2I_valid(s
) && (c
= cv
[*s
++]) < base
)
407 if (ADDOVER(n
, c
, negative
))
414 while (S2I_valid(s
) && (c
= cv
[*s
++]) < base
)
421 if (ADDOVER(n
, c
, negative
))
432 * optional qualifier suffix
435 if (S2I_valid(s
) && s
> (unsigned char*)(a
+ 1))
440 if (!(base
& QL
) && (c
== 'l' || c
== 'L'))
446 if (c
== 'l' || c
== 'L')
453 else if (!(base
& QU
) && (c
== 'u' || c
== 'U'))
472 * optional multiplier suffix
475 if (m
< 0 || s
== (unsigned char*)(a
+ 1))
512 else if (c
== decimal
&& S2I_valid(s
))
518 while (S2I_valid(s
) && (c
= *s
++) >= '0' && c
<= '9')
519 v
+= (m
/= 10) * (c
- '0');
520 if (ADDOVER(n
, v
, negative
))
544 if (shift
>= (sizeof(S2I_type
) * CHAR_BIT
))
546 if (shift
>= (sizeof(S2I_type
) * CHAR_BIT
- 1))
553 v
= ((S2I_unumber
)1) << shift
;
575 else if (!(qualifier
& QU
))
589 } while (*b
++ == '0');
591 if (negative
&& (n
- 1) > S2I_max
)
594 else if (n
> S2I_max
)
607 return (S2I_type
)S2I_min
;
611 return (S2I_type
)S2I_max
;
613 return negative
? -n
: n
;