1 /* strtoll( 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 long long int strtoll( const char * s, char ** endptr, int base )
18 const char * p = _PDCLIB_strtox_prelim( s, &sign, &base );
19 if ( base < 2 || base > 36 ) return 0;
22 rc = (long long int)_PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)LLONG_MAX, (uintmax_t)( LLONG_MAX / base ), (int)( LLONG_MAX % base ), &sign );
26 rc = (long long int)_PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)LLONG_MIN, (uintmax_t)( LLONG_MIN / -base ), (int)( -( LLONG_MIN % base ) ), &sign );
28 if ( endptr != NULL ) *endptr = ( p != NULL ) ? (char *) p : (char *) s;
29 return ( sign == '+' ) ? rc : -rc;
36 #include "_PDCLIB_test.h"
43 /* this, to base 36, overflows even a 256 bit integer */
44 char overflow[] = "-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ_";
45 /* tricky border case */
46 char tricky[] = "+0xz";
48 /* basic functionality */
49 TESTCASE( strtoll( "123", NULL, 10 ) == 123 );
50 /* proper detecting of default base 10 */
51 TESTCASE( strtoll( "456", NULL, 0 ) == 456 );
52 /* proper functioning to smaller base */
53 TESTCASE( strtoll( "14", NULL, 8 ) == 12 );
54 /* proper autodetecting of octal */
55 TESTCASE( strtoll( "016", NULL, 0 ) == 14 );
56 /* proper autodetecting of hexadecimal, lowercase 'x' */
57 TESTCASE( strtoll( "0xFF", NULL, 0 ) == 255 );
58 /* proper autodetecting of hexadecimal, uppercase 'X' */
59 TESTCASE( strtoll( "0Xa1", NULL, 0 ) == 161 );
60 /* proper handling of border case: 0x followed by non-hexdigit */
61 TESTCASE( strtoll( tricky, &endptr, 0 ) == 0 );
62 TESTCASE( endptr == tricky + 2 );
63 /* proper handling of border case: 0 followed by non-octdigit */
64 TESTCASE( strtoll( tricky, &endptr, 8 ) == 0 );
65 TESTCASE( endptr == tricky + 2 );
66 /* errno should still be 0 */
67 TESTCASE( errno == 0 );
68 /* overflowing subject sequence must still return proper endptr */
69 TESTCASE( strtoll( overflow, &endptr, 36 ) == LLONG_MIN );
70 TESTCASE( errno == ERANGE );
71 TESTCASE( ( endptr - overflow ) == 53 );
72 /* same for positive */
74 TESTCASE( strtoll( overflow + 1, &endptr, 36 ) == LLONG_MAX );
75 TESTCASE( errno == ERANGE );
76 TESTCASE( ( endptr - overflow ) == 53 );
77 /* testing skipping of leading whitespace */
78 TESTCASE( strtoll( " \n\v\t\f789", NULL, 0 ) == 789 );
79 /* testing conversion failure */
80 TESTCASE( strtoll( overflow, &endptr, 10 ) == 0 );
81 TESTCASE( endptr == overflow );
83 TESTCASE( strtoll( overflow, &endptr, 0 ) == 0 );
84 TESTCASE( endptr == overflow );
85 /* TODO: These tests assume two-complement, but conversion should work */
86 /* for one-complement and signed magnitude just as well. Anyone having */
87 /* a platform to test this on? */
89 #if LLONG_MAX >> 62 == 1
90 /* testing "even" overflow, i.e. base is power of two */
91 TESTCASE( strtoll( "9223372036854775807", NULL, 0 ) == 0x7fffffffffffffff );
92 TESTCASE( errno == 0 );
93 TESTCASE( strtoll( "9223372036854775808", NULL, 0 ) == LLONG_MAX );
94 TESTCASE( errno == ERANGE );
96 TESTCASE( strtoll( "-9223372036854775807", NULL, 0 ) == (long long)0x8000000000000001 );
97 TESTCASE( errno == 0 );
98 TESTCASE( strtoll( "-9223372036854775808", NULL, 0 ) == LLONG_MIN );
99 TESTCASE( errno == 0 );
100 TESTCASE( strtoll( "-9223372036854775809", NULL, 0 ) == LLONG_MIN );
101 TESTCASE( errno == ERANGE );
102 /* TODO: test "odd" overflow, i.e. base is not power of two */
103 #elif LLONG_MAX >> 126 == 1
104 /* testing "even" overflow, i.e. base is power of two */
105 TESTCASE( strtoll( "170141183460469231731687303715884105728", NULL, 0 ) == 0x7fffffffffffffffffffffffffffffff );
106 TESTCASE( errno == 0 );
107 TESTCASE( strtoll( "170141183460469231731687303715884105729", NULL, 0 ) == LLONG_MAX );
108 TESTCASE( errno == ERANGE );
110 TESTCASE( strtoll( "-170141183460469231731687303715884105728", NULL, 0 ) == -0x80000000000000000000000000000001 );
111 TESTCASE( errno == 0 );
112 TESTCASE( strtoll( "-170141183460469231731687303715884105729", NULL, 0 ) == LLONG_MIN );
113 TESTCASE( errno == 0 );
114 TESTCASE( strtoll( "-170141183460469231731687303715884105730", NULL, 0 ) == LLONG_MIN );
115 TESTCASE( errno == ERANGE );
116 /* TODO: test "odd" overflow, i.e. base is not power of two */
118 #error Unsupported width of 'long long' (neither 64 nor 128 bit).