3 /* strtol( 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 long int strtol( const char * s, char ** endptr, int base )
20 const char * p = _PDCLIB_strtox_prelim( s, &sign, &base );
21 if ( base < 2 || base > 36 ) return 0;
24 rc = (long int)_PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)LONG_MAX, (uintmax_t)( LONG_MAX / base ), (int)( LONG_MAX % base ), &sign );
28 /* FIXME: This breaks on some machines that round negatives wrongly */
29 rc = (long int)_PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)LONG_MIN, (uintmax_t)( LONG_MIN / -base ), (int)( -( LONG_MIN % base ) ), &sign );
31 if ( endptr != NULL ) *endptr = ( p != NULL ) ? (char *) p : (char *) s;
32 return ( sign == '+' ) ? rc : -rc;
38 #include <_PDCLIB_test.h>
42 #include <_PDCLIB_int.h>
50 /* this, to base 36, overflows even a 256 bit integer */
51 char overflow[] = "-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ_";
52 /* tricky border case */
53 char tricky[] = "+0xz";
55 /* basic functionality */
56 TESTCASE( strtol( "123", NULL, 10 ) == 123 );
57 /* proper detecting of default base 10 */
58 TESTCASE( strtol( "456", NULL, 0 ) == 456 );
59 /* proper functioning to smaller base */
60 TESTCASE( strtol( "14", NULL, 8 ) == 12 );
61 /* proper autodetecting of octal */
62 TESTCASE( strtol( "016", NULL, 0 ) == 14 );
63 /* proper autodetecting of hexadecimal, lowercase 'x' */
64 TESTCASE( strtol( "0xFF", NULL, 0 ) == 255 );
65 /* proper autodetecting of hexadecimal, uppercase 'X' */
66 TESTCASE( strtol( "0Xa1", NULL, 0 ) == 161 );
67 /* proper handling of border case: 0x followed by non-hexdigit */
68 TESTCASE( strtol( tricky, &endptr, 0 ) == 0 );
69 TESTCASE( endptr == tricky + 2 );
70 /* proper handling of border case: 0 followed by non-octdigit */
71 TESTCASE( strtol( tricky, &endptr, 8 ) == 0 );
72 TESTCASE( endptr == tricky + 2 );
73 /* errno should still be 0 */
74 TESTCASE( errno == 0 );
75 /* overflowing subject sequence must still return proper endptr */
76 TESTCASE( strtol( overflow, &endptr, 36 ) == LONG_MIN );
77 TESTCASE( errno == ERANGE );
78 TESTCASE( ( endptr - overflow ) == 53 );
79 /* same for positive */
81 TESTCASE( strtol( overflow + 1, &endptr, 36 ) == LONG_MAX );
82 TESTCASE( errno == ERANGE );
83 TESTCASE( ( endptr - overflow ) == 53 );
84 /* testing skipping of leading whitespace */
85 TESTCASE( strtol( " \n\v\t\f789", NULL, 0 ) == 789 );
86 /* testing conversion failure */
87 TESTCASE( strtol( overflow, &endptr, 10 ) == 0 );
88 TESTCASE( endptr == overflow );
90 TESTCASE( strtol( overflow, &endptr, 0 ) == 0 );
91 TESTCASE( endptr == overflow );
92 /* These tests assume two-complement, but conversion should work for */
93 /* one-complement and signed magnitude just as well. Anyone having a */
94 /* platform to test this on? */
96 #if _PDCLIB_LONG_BYTES == 4
97 /* testing "even" overflow, i.e. base is power of two */
98 TESTCASE( strtol( "0x7FFFFFFF", NULL, 0 ) == 0x7fffffff );
99 TESTCASE( errno == 0 );
100 TESTCASE( strtol( "0x80000000", NULL, 0 ) == LONG_MAX );
101 TESTCASE( errno == ERANGE );
103 TESTCASE( strtol( "-0x7FFFFFFF", NULL, 0 ) == (long)0x80000001 );
104 TESTCASE( errno == 0 );
105 TESTCASE( strtol( "-0x80000000", NULL, 0 ) == LONG_MIN );
106 TESTCASE( errno == 0 );
107 TESTCASE( strtol( "-0x80000001", NULL, 0 ) == LONG_MIN );
108 TESTCASE( errno == ERANGE );
109 /* TODO: test "odd" overflow, i.e. base is not power of two */
110 #elif _PDCLIB_LONG_BYTES == 8
111 /* testing "even" overflow, i.e. base is power of two */
112 TESTCASE( strtol( "0x7FFFFFFFFFFFFFFF", NULL, 0 ) == 0x7fffffffffffffff );
113 TESTCASE( errno == 0 );
114 TESTCASE( strtol( "0x8000000000000000", NULL, 0 ) == LONG_MAX );
115 TESTCASE( errno == ERANGE );
117 TESTCASE( strtol( "-0x7FFFFFFFFFFFFFFF", NULL, 0 ) == (long)0x8000000000000001 );
118 TESTCASE( errno == 0 );
119 TESTCASE( strtol( "-0x8000000000000000", NULL, 0 ) == LONG_MIN );
120 TESTCASE( errno == 0 );
121 TESTCASE( strtol( "-0x8000000000000001", NULL, 0 ) == LONG_MIN );
122 TESTCASE( errno == ERANGE );
123 /* TODO: test "odd" overflow, i.e. base is not power of two */
125 #error Unsupported width of 'long' (neither 32 nor 64 bit).