]> pd.if.org Git - pdclib/blob - functions/_PDCLIB/atomax.c
Added atoi, atol, atoll plus internal helpers.
[pdclib] / functions / _PDCLIB / atomax.c
1 /* $Id$ */
2
3 /* Release $Name$ */
4
5 /* _PDCLIB_atomax( const char * )
6
7    This file is part of the Public Domain C Library (PDCLib).
8    Permission is granted to use, modify, and / or redistribute at will.
9 */
10
11 #include <_PDCLIB_int.h>
12 #include <string.h>
13 #include <ctype.h>
14
15 _PDCLIB_intmax_t _PDCLIB_atomax( const char * s )
16 {
17     _PDCLIB_intmax_t rc = 0;
18     char sign = '+';
19     const char * x;
20     /* TODO: In other than "C" locale, additional patterns may be defined     */
21     while ( isspace( *s ) ) ++s;
22     if ( *s == '+' ) ++s;
23     else if ( *s == '-' ) sign = *(s++);
24     while ( ( x = memchr( _PDCLIB_digits, *(s++), 10 ) ) != NULL )
25     {
26         rc = rc * 10 + ( x - _PDCLIB_digits );
27     }
28     return ( sign == '+' ) ? rc : -rc;
29 }
30
31 #ifdef TEST
32 #include <_PDCLIB_test.h>
33
34 int main()
35 {
36     BEGIN_TESTS;
37     /* basic functionality */
38     TESTCASE( _PDCLIB_atomax( "123" ) == 123 );
39     /* testing skipping of leading whitespace and trailing garbage */
40     TESTCASE( _PDCLIB_atomax( " \n\v\t\f123xyz" ) == 123 );
41     return TEST_RESULTS;
42 }
43
44 #endif