3 /* strtoimax( 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.
16 intmax_t strtoimax( const char * _PDCLIB_restrict nptr, char ** _PDCLIB_restrict endptr, int base )
20 const char * p = _PDCLIB_strtox_prelim( nptr, &sign, &base );
21 if ( base < 2 || base > 36 ) return 0;
24 rc = (intmax_t)_PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)INTMAX_MAX, (uintmax_t)( INTMAX_MAX / base ), (int)( INTMAX_MAX % base ), &sign );
28 rc = (intmax_t)_PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)INTMAX_MIN, (uintmax_t)( INTMAX_MIN / -base ), (int)( -( INTMAX_MIN % base ) ), &sign );
30 if ( endptr != NULL ) *endptr = ( p != NULL ) ? (char *) p : (char *) nptr;
31 return ( sign == '+' ) ? rc : -rc;
37 #include <_PDCLIB_test.h>
44 /* this, to base 36, overflows even a 256 bit integer */
45 char overflow[] = "-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ_";
46 /* tricky border case */
47 char tricky[] = "+0xz";
49 /* basic functionality */
50 TESTCASE( strtoimax( "123", NULL, 10 ) == 123 );
51 /* proper detecting of default base 10 */
52 TESTCASE( strtoimax( "456", NULL, 0 ) == 456 );
53 /* proper functioning to smaller base */
54 TESTCASE( strtoimax( "14", NULL, 8 ) == 12 );
55 /* proper autodetecting of octal */
56 TESTCASE( strtoimax( "016", NULL, 0 ) == 14 );
57 /* proper autodetecting of hexadecimal, lowercase 'x' */
58 TESTCASE( strtoimax( "0xFF", NULL, 0 ) == 255 );
59 /* proper autodetecting of hexadecimal, uppercase 'X' */
60 TESTCASE( strtoimax( "0Xa1", NULL, 0 ) == 161 );
61 /* proper handling of border case: 0x followed by non-hexdigit */
62 TESTCASE( strtoimax( tricky, &endptr, 0 ) == 0 );
63 TESTCASE( endptr == tricky + 2 );
64 /* proper handling of border case: 0 followed by non-octdigit */
65 TESTCASE( strtoimax( tricky, &endptr, 8 ) == 0 );
66 TESTCASE( endptr == tricky + 2 );
67 /* errno should still be 0 */
68 TESTCASE( errno == 0 );
69 /* overflowing subject sequence must still return proper endptr */
70 TESTCASE( strtoimax( overflow, &endptr, 36 ) == INTMAX_MIN );
71 TESTCASE( errno == ERANGE );
72 TESTCASE( ( endptr - overflow ) == 53 );
73 /* same for positive */
75 TESTCASE( strtoimax( overflow + 1, &endptr, 36 ) == INTMAX_MAX );
76 TESTCASE( errno == ERANGE );
77 TESTCASE( ( endptr - overflow ) == 53 );
78 /* testing skipping of leading whitespace */
79 TESTCASE( strtoimax( " \n\v\t\f789", NULL, 0 ) == 789 );
80 /* testing conversion failure */
81 TESTCASE( strtoimax( overflow, &endptr, 10 ) == 0 );
82 TESTCASE( endptr == overflow );
84 TESTCASE( strtoimax( overflow, &endptr, 0 ) == 0 );
85 TESTCASE( endptr == overflow );
86 /* These tests assume two-complement, but conversion should work for */
87 /* one-complement and signed magnitude just as well. Anyone having a */
88 /* platform to test this on? */
90 #if INTMAX_MAX >> 62 == 1
91 /* testing "odd" overflow, i.e. base is not a power of two */
92 TESTCASE( strtoimax( "9223372036854775807", NULL, 0 ) == INTMAX_MAX );
93 TESTCASE( errno == 0 );
94 TESTCASE( strtoimax( "9223372036854775808", NULL, 0 ) == INTMAX_MAX );
95 TESTCASE( errno == ERANGE );
97 TESTCASE( strtoimax( "-9223372036854775807", NULL, 0 ) == (INTMAX_MIN + 1) );
98 TESTCASE( errno == 0 );
99 TESTCASE( strtoimax( "-9223372036854775808", NULL, 0 ) == INTMAX_MIN );
100 TESTCASE( errno == 0 );
101 TESTCASE( strtoimax( "-9223372036854775809", NULL, 0 ) == INTMAX_MIN );
102 TESTCASE( errno == ERANGE );
103 /* testing "even" overflow, i.e. base is power of two */
105 TESTCASE( strtoimax( "0x7fffffffffffffff", NULL, 0 ) == INTMAX_MAX );
106 TESTCASE( errno == 0 );
107 TESTCASE( strtoimax( "0x8000000000000000", NULL, 0 ) == INTMAX_MAX );
108 TESTCASE( errno == ERANGE );
110 TESTCASE( strtoimax( "-0x7fffffffffffffff", NULL, 0 ) == (INTMAX_MIN + 1) );
111 TESTCASE( errno == 0 );
112 TESTCASE( strtoimax( "-0x8000000000000000", NULL, 0 ) == INTMAX_MIN );
113 TESTCASE( errno == 0 );
114 TESTCASE( strtoimax( "-0x8000000000000001", NULL, 0 ) == INTMAX_MIN );
115 TESTCASE( errno == ERANGE );
116 #elif LLONG_MAX >> 126 == 1
117 /* testing "odd" overflow, i.e. base is not a power of two */
118 TESTCASE( strtoimax( "170141183460469231731687303715884105728", NULL, 0 ) == INTMAX_MAX );
119 TESTCASE( errno == 0 );
120 TESTCASE( strtoimax( "170141183460469231731687303715884105729", NULL, 0 ) == INTMAX_MAX );
121 TESTCASE( errno == ERANGE );
123 TESTCASE( strtoimax( "-170141183460469231731687303715884105728", NULL, 0 ) == (INTMAX_MIN + 1) );
124 TESTCASE( errno == 0 );
125 TESTCASE( strtoimax( "-170141183460469231731687303715884105729", NULL, 0 ) == INTMAX_MIN );
126 TESTCASE( errno == 0 );
127 TESTCASE( strtoimax( "-170141183460469231731687303715884105730", NULL, 0 ) == INTMAX_MIN );
128 TESTCASE( errno == ERANGE );
129 /* testing "even" overflow, i.e. base is power of two */
131 TESTCASE( strtoimax( "0x7fffffffffffffffffffffffffffffff", NULL, 0 ) == INTMAX_MAX );
132 TESTCASE( errno == 0 );
133 TESTCASE( strtoimax( "0x80000000000000000000000000000000", NULL, 0 ) == INTMAX_MAX );
134 TESTCASE( errno == ERANGE );
136 TESTCASE( strtoimax( "-0x7fffffffffffffffffffffffffffffff", NULL, 0 ) == (INTMAX_MIN + 1) );
137 TESTCASE( errno == 0 );
138 TESTCASE( strtoimax( "-0x80000000000000000000000000000000", NULL, 0 ) == INTMAX_MIN );
139 TESTCASE( errno == 0 );
140 TESTCASE( strtoimax( "-0x80000000000000000000000000000001", NULL, 0 ) == INTMAX_MIN );
141 TESTCASE( errno == ERANGE );
143 #error Unsupported width of 'intmax_t' (neither 64 nor 128 bit).