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