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