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.
11 const char * _PDCLIB_strtox_prelim( const char * p, char * sign, int * base )
13 /* skipping leading whitespace */
14 while ( isspace( *p ) ) ++p;
15 /* determining / skipping sign */
16 if ( *p != '+' && *p != '-' ) *sign = '+';
18 /* determining base */
22 if ( ( *base == 0 || *base == 16 ) && ( *p == 'x' || *p == 'X' ) )
26 /* catching a border case here: "0x" followed by a non-digit should
27 be parsed as the unprefixed zero.
28 We have to "rewind" the parsing; having the base set to 16 if it
29 was zero previously does not hurt, as the result is zero anyway.
31 if ( memchr( _PDCLIB_digits, tolower(*p), *base ) == NULL )
36 else if ( *base == 0 )
49 return ( ( *base >= 2 ) && ( *base <= 36 ) ) ? p : NULL;
53 #include "_PDCLIB_test.h"
59 char test1[] = " 123";
60 char test2[] = "\t+0123";
61 char test3[] = "\v-0x123";
62 TESTCASE( _PDCLIB_strtox_prelim( test1, &sign, &base ) == &test1[2] );
63 TESTCASE( sign == '+' );
64 TESTCASE( base == 10 );
67 TESTCASE( _PDCLIB_strtox_prelim( test2, &sign, &base ) == &test2[3] );
68 TESTCASE( sign == '+' );
69 TESTCASE( base == 8 );
72 TESTCASE( _PDCLIB_strtox_prelim( test3, &sign, &base ) == &test3[4] );
73 TESTCASE( sign == '-' );
74 TESTCASE( base == 16 );
77 TESTCASE( _PDCLIB_strtox_prelim( test3, &sign, &base ) == &test3[2] );
78 TESTCASE( sign == '-' );
79 TESTCASE( base == 10 );
81 TESTCASE( _PDCLIB_strtox_prelim( test3, &sign, &base ) == NULL );
83 TESTCASE( _PDCLIB_strtox_prelim( test3, &sign, &base ) == NULL );