1 /* _PDCLIB_strtox_prelim( const char *, char *, int * )
3 This file is part of the Public Domain C Library (PDCLib).
4 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;
58 #include "_PDCLIB_test.h"
65 char test1[] = " 123";
66 char test2[] = "\t+0123";
67 char test3[] = "\v-0x123";
68 TESTCASE( _PDCLIB_strtox_prelim( test1, &sign, &base ) == &test1[2] );
69 TESTCASE( sign == '+' );
70 TESTCASE( base == 10 );
73 TESTCASE( _PDCLIB_strtox_prelim( test2, &sign, &base ) == &test2[3] );
74 TESTCASE( sign == '+' );
75 TESTCASE( base == 8 );
78 TESTCASE( _PDCLIB_strtox_prelim( test3, &sign, &base ) == &test3[4] );
79 TESTCASE( sign == '-' );
80 TESTCASE( base == 16 );
83 TESTCASE( _PDCLIB_strtox_prelim( test3, &sign, &base ) == &test3[2] );
84 TESTCASE( sign == '-' );
85 TESTCASE( base == 10 );
87 TESTCASE( _PDCLIB_strtox_prelim( test3, &sign, &base ) == NULL );
89 TESTCASE( _PDCLIB_strtox_prelim( test3, &sign, &base ) == NULL );