File: strtol.c

package info (click to toggle)
cc65 2.19-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,268 kB
  • sloc: ansic: 117,151; asm: 66,339; pascal: 4,248; makefile: 1,009; perl: 607
file content (128 lines) | stat: -rw-r--r-- 2,960 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <limits.h>
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>



long __fastcall__ strtol (const char* nptr, char** endptr, int base)
/* Convert a string to a long int */
{
    register const char* S          = nptr;
    unsigned long        Val        = 0;
    unsigned char        Minus      = 0;
    unsigned char        Ovf        = 0;
    unsigned             CvtCount   = 0;
    unsigned char        DigitVal;
    unsigned long        MaxVal;
    unsigned char        MaxDigit;


    /* Skip white space */
    while (isspace (*S)) {
        ++S;
    }

    /* Check for leading + or - sign */
    switch (*S) {
        case '-':
            Minus = 1;
            /* FALLTHROUGH */
        case '+':
            ++S;
    }

    /* If base is zero, we may have a 0 or 0x prefix. If base is 16, we may
    ** have a 0x prefix.
    */
    if (base == 0) {
        if (*S == '0') {
            ++S;
            if (*S == 'x' || *S == 'X') {
                ++S;
                base = 16;
            } else {
                base = 8;
            }
        } else {
            base = 10;
        }
    } else if (base == 16 && *S == '0' && (S[1] == 'x' || S[1] == 'X')) {
        S += 2;
    }

    /* Determine the maximum valid number and (if the number is equal to this
    ** value) the maximum valid digit.
    */
    if (Minus) {
        MaxVal = LONG_MIN;
    } else {
        MaxVal = LONG_MAX;
    }
    MaxDigit = MaxVal % base;
    MaxVal  /= base;

    /* Convert the number */
    while (1) {

        /* Convert the digit into a numeric value */
        if (isdigit (*S)) {
            DigitVal = *S - '0';
        } else if (isupper (*S)) {
            DigitVal = *S - ('A' - 10);
        } else if (islower (*S)) {
            DigitVal = *S - ('a' - 10);
        } else {
            /* Unknown character */
            break;
        }

        /* Don't accept a character that doesn't match base */
        if (DigitVal >= base) {
            break;
        }

        /* Don't accept anything that makes the final value invalid */
        if (Val > MaxVal || (Val == MaxVal && DigitVal > MaxDigit)) {
            Ovf = 1;
        }

        /* Calculate the next value if digit is not invalid */
        if (Ovf == 0) {
            Val = (Val * base) + DigitVal;
            ++CvtCount;
        }

        /* Next character from input */
        ++S;
    }

    /* Store the end pointer. If no conversion was performed, the value of
    ** nptr is returned in endptr.
    */
    if (endptr) {
        if (CvtCount > 0) {
            *endptr = (char*) S;
        } else {
            *endptr = (char*) nptr;
        }
    }

    /* Handle overflow */
    if (Ovf) {
        _seterrno (ERANGE);
        if (Minus) {
            return LONG_MIN;
        } else {
            return LONG_MAX;
        }
    }

    /* Return the result */
    if (Minus) {
        return -(long)Val;
    } else {
        return Val;
    }
}