1 /* strtoull( 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.
14 unsigned long long int strtoull( const char * s, char ** endptr, int base )
16 unsigned long long int rc;
18 const char * p = _PDCLIB_strtox_prelim( s, &sign, &base );
19 if ( base < 2 || base > 36 ) return 0;
20 rc = _PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)ULLONG_MAX, (uintmax_t)( ULLONG_MAX / base ), (int)( ULLONG_MAX % base ), &sign );
21 if ( endptr != NULL ) *endptr = ( p != NULL ) ? (char *) p : (char *) s;
22 return ( sign == '+' ) ? rc : -rc;
28 #include <_PDCLIB_test.h>
34 /* this, to base 36, overflows even a 256 bit integer */
35 char overflow[] = "-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ_";
36 /* tricky border case */
37 char tricky[] = "+0xz";
39 /* basic functionality */
40 TESTCASE( strtoull( "123", NULL, 10 ) == 123 );
41 /* proper detecting of default base 10 */
42 TESTCASE( strtoull( "456", NULL, 0 ) == 456 );
43 /* proper functioning to smaller base */
44 TESTCASE( strtoull( "14", NULL, 8 ) == 12 );
45 /* proper autodetecting of octal */
46 TESTCASE( strtoull( "016", NULL, 0 ) == 14 );
47 /* proper autodetecting of hexadecimal, lowercase 'x' */
48 TESTCASE( strtoull( "0xFF", NULL, 0 ) == 255 );
49 /* proper autodetecting of hexadecimal, uppercase 'X' */
50 TESTCASE( strtoull( "0Xa1", NULL, 0 ) == 161 );
51 /* proper handling of border case: 0x followed by non-hexdigit */
52 TESTCASE( strtoull( tricky, &endptr, 0 ) == 0 );
53 TESTCASE( endptr == tricky + 2 );
54 /* proper handling of border case: 0 followed by non-octdigit */
55 TESTCASE( strtoull( tricky, &endptr, 8 ) == 0 );
56 TESTCASE( endptr == tricky + 2 );
57 /* errno should still be 0 */
58 TESTCASE( errno == 0 );
59 /* overflowing subject sequence must still return proper endptr */
60 TESTCASE( strtoull( overflow, &endptr, 36 ) == ULLONG_MAX );
61 TESTCASE( errno == ERANGE );
62 TESTCASE( ( endptr - overflow ) == 53 );
63 /* same for positive */
65 TESTCASE( strtoull( overflow + 1, &endptr, 36 ) == ULLONG_MAX );
66 TESTCASE( errno == ERANGE );
67 TESTCASE( ( endptr - overflow ) == 53 );
68 /* testing skipping of leading whitespace */
69 TESTCASE( strtoull( " \n\v\t\f789", NULL, 0 ) == 789 );
70 /* testing conversion failure */
71 TESTCASE( strtoull( overflow, &endptr, 10 ) == 0 );
72 TESTCASE( endptr == overflow );
74 TESTCASE( strtoull( overflow, &endptr, 0 ) == 0 );
75 TESTCASE( endptr == overflow );
77 /* long long -> 64 bit */
78 #if ULLONG_MAX >> 63 == 1
79 /* testing "even" overflow, i.e. base is power of two */
80 TESTCASE( strtoull( "18446744073709551615", NULL, 0 ) == ULLONG_MAX );
81 TESTCASE( errno == 0 );
82 TESTCASE( strtoull( "18446744073709551616", NULL, 0 ) == ULLONG_MAX );
83 TESTCASE( errno == ERANGE );
84 /* TODO: test "odd" overflow, i.e. base is not power of two */
85 /* long long -> 128 bit */
86 #elif ULLONG_MAX >> 127 == 1
87 /* testing "even" overflow, i.e. base is power of two */
88 TESTCASE( strtoull( "340282366920938463463374607431768211455", NULL, 0 ) == ULLONG_MAX );
89 TESTCASE( errno == 0 );
90 TESTCASE( strtoull( "340282366920938463463374607431768211456", NULL, 0 ) == ULLONG_MAX );
91 TESTCASE( errno == ERANGE );
92 /* TODO: test "odd" overflow, i.e. base is not power of two */
94 #error Unsupported width of 'long long' (neither 64 nor 128 bit).