3 /* _PDCLIB_strtox_prelim( const char *, char *, int * )
5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
13 const char * _PDCLIB_strtox_prelim( const char * p, char * sign, int * base )
15 /* skipping leading whitespace */
16 while ( isspace( *p ) ) ++p;
17 /* determining / skipping sign */
18 if ( *p != '+' && *p != '-' ) *sign = '+';
20 /* determining base */
24 if ( ( *base == 0 || *base == 16 ) && ( *p == 'x' || *p == 'X' ) )
28 /* catching a border case here: "0x" followed by a non-digit should
29 be parsed as the unprefixed zero.
30 We have to "rewind" the parsing; having the base set to 16 if it
31 was zero previously does not hurt, as the result is zero anyway.
33 if ( memchr( _PDCLIB_digits, tolower(*p), *base ) == NULL )
38 else if ( *base == 0 )
51 return ( ( *base >= 2 ) && ( *base <= 36 ) ) ? p : NULL;
56 #include <_PDCLIB_test.h>
63 char test1[] = " 123";
64 char test2[] = "\t+0123";
65 char test3[] = "\v-0x123";
66 TESTCASE( _PDCLIB_strtox_prelim( test1, &sign, &base ) == &test1[2] );
67 TESTCASE( sign == '+' );
68 TESTCASE( base == 10 );
71 TESTCASE( _PDCLIB_strtox_prelim( test2, &sign, &base ) == &test2[3] );
72 TESTCASE( sign == '+' );
73 TESTCASE( base == 8 );
76 TESTCASE( _PDCLIB_strtox_prelim( test3, &sign, &base ) == &test3[4] );
77 TESTCASE( sign == '-' );
78 TESTCASE( base == 16 );
81 TESTCASE( _PDCLIB_strtox_prelim( test3, &sign, &base ) == &test3[2] );
82 TESTCASE( sign == '-' );
83 TESTCASE( base == 10 );
85 TESTCASE( _PDCLIB_strtox_prelim( test3, &sign, &base ) == NULL );
87 TESTCASE( _PDCLIB_strtox_prelim( test3, &sign, &base ) == NULL );