]> pd.if.org Git - pdclib/blob - functions/_PDCLIB/atomax.c
Added internal headerguard defines that were missing.
[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 #define _PDCLIB_INT_H _PDCLIB_INT_H
12 #include <_PDCLIB_int.h>
13 #include <string.h>
14 #include <ctype.h>
15
16 _PDCLIB_intmax_t _PDCLIB_atomax( const char * s )
17 {
18     _PDCLIB_intmax_t rc = 0;
19     char sign = '+';
20     const char * x;
21     /* TODO: In other than "C" locale, additional patterns may be defined     */
22     while ( isspace( *s ) ) ++s;
23     if ( *s == '+' ) ++s;
24     else if ( *s == '-' ) sign = *(s++);
25     while ( ( x = memchr( _PDCLIB_digits, *(s++), 10 ) ) != NULL )
26     {
27         rc = rc * 10 + ( x - _PDCLIB_digits );
28     }
29     return ( sign == '+' ) ? rc : -rc;
30 }
31
32 #ifdef TEST
33 #include <_PDCLIB_test.h>
34
35 int main()
36 {
37     BEGIN_TESTS;
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     return TEST_RESULTS;
43 }
44
45 #endif