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