]> pd.if.org Git - pdclib/blob - functions/_PDCLIB/_PDCLIB_atomax.c
PDCLib includes with quotes, not <>.
[pdclib] / functions / _PDCLIB / _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 #ifndef REGTEST
8 #include "_PDCLIB_int.h"
9 #include <string.h>
10 #include <ctype.h>
11
12 _PDCLIB_intmax_t _PDCLIB_atomax( const char * s )
13 {
14     _PDCLIB_intmax_t rc = 0;
15     char sign = '+';
16     const char * x;
17     /* TODO: In other than "C" locale, additional patterns may be defined     */
18     while ( isspace( *s ) ) ++s;
19     if ( *s == '+' ) ++s;
20     else if ( *s == '-' ) sign = *(s++);
21     /* TODO: Earlier version was missing tolower() but was not caught by tests */
22     while ( ( x = memchr( _PDCLIB_digits, tolower(*(s++)), 10 ) ) != NULL )
23     {
24         rc = rc * 10 + ( x - _PDCLIB_digits );
25     }
26     return ( sign == '+' ) ? rc : -rc;
27 }
28 #endif
29
30 #ifdef TEST
31 #include "_PDCLIB_test.h"
32
33 int main( void )
34 {
35 #ifndef REGTEST
36     /* basic functionality */
37     TESTCASE( _PDCLIB_atomax( "123" ) == 123 );
38     /* testing skipping of leading whitespace and trailing garbage */
39     TESTCASE( _PDCLIB_atomax( " \n\v\t\f123xyz" ) == 123 );
40 #endif
41     return TEST_RESULTS;
42 }
43
44 #endif