8 long __fastcall__ strtol (const char* nptr, char** endptr, int base)
9 /* Convert a string to a long int */
11 register const char* S = nptr;
12 unsigned long Val = 0;
13 unsigned char Minus = 0;
14 unsigned char Ovf = 0;
15 unsigned CvtCount = 0;
16 unsigned char DigitVal;
18 unsigned char MaxDigit;
21 /* Skip white space */
22 while (isspace (*S)) {
26 /* Check for leading + or - sign */
35 /* If base is zero, we may have a 0 or 0x prefix. If base is 16, we may
41 if (*S == 'x' || *S == 'X') {
50 } else if (base == 16 && *S == '0' && (S[1] == 'x' || S[1] == 'X')) {
54 /* Determine the maximum valid number and (if the number is equal to this
55 * value) the maximum valid digit.
62 MaxDigit = MaxVal % base;
65 /* Convert the number */
68 /* Convert the digit into a numeric value */
71 } else if (isupper (*S)) {
72 DigitVal = *S - ('A' - 10);
73 } else if (islower (*S)) {
74 DigitVal = *S - ('a' - 10);
76 /* Unknown character */
80 /* Don't accept a character that doesn't match base */
81 if (DigitVal >= base) {
85 /* Don't accept anything that makes the final value invalid */
86 if (Val > MaxVal || (Val == MaxVal && DigitVal > MaxDigit)) {
90 /* Calculate the next value if digit is not invalid */
92 Val = (Val * base) + DigitVal;
96 /* Next character from input */
100 /* Store the end pointer. If no conversion was performed, the value of
101 * nptr is returned in endptr.
105 *endptr = (char*) S - 1;
107 *endptr = (char*) nptr;
111 /* Handle overflow */
121 /* Return the result */