5 /* _PDCLIB_strtox_main( const char * *, int, _PDCLIB_uintmax_t, _PDCLIB_uintmax_t, int )
7 This file is part of the Public Domain C Library (PDCLib).
8 Permission is granted to use, modify, and / or redistribute at will.
11 #include <_PDCLIB_int.h>
16 _PDCLIB_uintmax_t _PDCLIB_strtox_main( const char ** p, int base, _PDCLIB_uintmax_t error, _PDCLIB_uintmax_t limval, _PDCLIB_uintmax_t limdigit, char * sign )
18 _PDCLIB_uintmax_t rc = 0;
21 while ( ( x = memchr( _PDCLIB_digits, toupper(**p), base ) ) != NULL )
23 digit = x - _PDCLIB_digits;
24 if ( ( rc < limval ) || ( ( rc == limval ) && ( digit <= limdigit ) ) )
26 rc = rc * base + ( x - _PDCLIB_digits );
32 /* TODO: Only if endptr != NULL */
33 while ( memchr( _PDCLIB_digits, **p, base ) != NULL ) ++(*p);
34 /* TODO: This is ugly, but keeps caller from negating the error value */
48 #include <_PDCLIB_test.h>
58 /* basic functionality */
61 TESTCASE( _PDCLIB_strtox_main( &p, 10, 999, 12, 3, &sign ) == 123 );
62 TESTCASE( errno == 0 );
63 TESTCASE( p == &test[3] );
64 /* proper functioning to smaller base */
66 TESTCASE( _PDCLIB_strtox_main( &p, 8, 999, 12, 3, &sign ) == 0123 );
67 TESTCASE( errno == 0 );
68 TESTCASE( p == &test[3] );
69 /* overflowing subject sequence must still return proper endptr */
71 TESTCASE( _PDCLIB_strtox_main( &p, 4, 999, 1, 2, &sign ) == 999 );
72 TESTCASE( errno == ERANGE );
73 TESTCASE( p == &test[3] );
74 TESTCASE( sign == '+' );
75 /* testing conversion failure */
79 TESTCASE( _PDCLIB_strtox_main( &p, 10, 999, 99, 8, &sign ) == 0 );
80 TESTCASE( p == NULL );