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;
55 #include <_PDCLIB_test.h>
61 char test1[] = " 123";
62 char test2[] = "\t+0123";
63 char test3[] = "\v-0x123";
64 TESTCASE( _PDCLIB_strtox_prelim( test1, &sign, &base ) == &test1[2] );
65 TESTCASE( sign == '+' );
66 TESTCASE( base == 10 );
69 TESTCASE( _PDCLIB_strtox_prelim( test2, &sign, &base ) == &test2[3] );
70 TESTCASE( sign == '+' );
71 TESTCASE( base == 8 );
74 TESTCASE( _PDCLIB_strtox_prelim( test3, &sign, &base ) == &test3[4] );
75 TESTCASE( sign == '-' );
76 TESTCASE( base == 16 );
79 TESTCASE( _PDCLIB_strtox_prelim( test3, &sign, &base ) == &test3[2] );
80 TESTCASE( sign == '-' );
81 TESTCASE( base == 10 );
83 TESTCASE( _PDCLIB_strtox_prelim( test3, &sign, &base ) == NULL );
85 TESTCASE( _PDCLIB_strtox_prelim( test3, &sign, &base ) == NULL );