File: numparser2.h

package info (click to toggle)
notion 4.0.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,656 kB
  • sloc: ansic: 47,365; sh: 2,093; makefile: 594; perl: 270
file content (272 lines) | stat: -rw-r--r-- 5,134 bytes parent folder | download | duplicates (4)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
 * libtu/numparser2.h
 *
 * Copyright (c) Tuomo Valkonen 1999-2002.
 *
 * You may distribute and modify this library under the terms of either
 * the Clarified Artistic License or the GNU LGPL, version 2.1 or later.
 */

#define MAX_MANTISSA 10  /* should be enough for our needs */
#define ULONG_SIZE (sizeof(ulong)*8)

enum{
    NPNUM_INT,
    NPNUM_FLOAT
};

#ifdef NP_SIMPLE_IMPL

typedef struct _NPNum{
    int type;
    int base;
     bool negative;
    double fval;
    ulong ival;
} NPNum;

#define NUM_INIT {0, 0, 0, 0.0, 0}

static int npnum_mulbase_add(NPNum *num, long base, long v)
{
    double iold=num->ival;

    num->fval=num->fval*base+(double)v;

    num->ival*=base;

    if(num->ival<iold)
        num->type=NPNUM_FLOAT;

    num->ival+=v;

    return 0;
}

#else /* NP_SIMPLE_IMPL */

typedef struct _NPNum{
    unsigned char nmantissa;
    int type;
    int base;
     bool negative;
    ulong mantissa[MAX_MANTISSA];
    long exponent;
} NPNum;

#define NUM_INIT {0, 0, 0, 0, {0,}, 0}

#define ADD_EXP(NUM, X) (NUM)->exponent+=(X);

#if defined(__GNUG__) && defined(i386) && !defined(NP_NO_I386_ASM)
 #define NP_I386_ASM
#endif

static int npnum_mulbase_add(NPNum *num, long base, long v)
{
    long i, j;
    ulong overflow;
#ifndef NP_I386_ASM
    ulong val;
#endif

    for(i=num->nmantissa;i>=0;i--){
#ifdef NP_I386_ASM
        __asm__("mul %4\n"
                : "=a" (num->mantissa[i]), "=d" (overflow)
                : "0" (num->mantissa[i]), "1" (0), "q" (base)
                : "eax", "edx");
#else
        overflow=0;
        val=num->mantissa[i];

        if(val<ULONG_MAX/base){
            val*=base;
        }else if(val){
            ulong tmp=val;
            ulong old=val;
            for(j=0; j<base; j++){
                val+=tmp;
                if(val<=old)
                    overflow++;
                old=val;
            }
        }
        num->mantissa[i]=val;
#endif
        if(overflow){
            if(i==num->nmantissa){
                if(num->nmantissa==MAX_MANTISSA)
                    return E_TOKZ_TOOBIG;
                num->nmantissa++;
            }
            num->mantissa[i+1]+=overflow;
        }
    }
    num->mantissa[0]+=v;

    return 0;
}

#undef NP_I386_ASM

#endif /* NP_SIMPLE_IMPL */


/* */


static bool is_end(int c)
{
    /* oops... EOF was missing */
    return (c==EOF || (c!='.' && ispunct(c)) || isspace(c) || iscntrl(c));
}


/* */


static int parse_exponent(NPNum *num, Tokenizer *tokz, int c)
{
    long exp=0;
    bool neg=FALSE;
    int err=0;

    c=GETCH();

    if(c=='-' || c=='+'){
        if(c=='-')
            neg=TRUE;
        c=GETCH();
    }

    for(; 1; c=GETCH()){
        if(isdigit(c)){
            exp*=10;
            exp+=c-'0';
        }else if(is_end(c)){
            UNGETCH(c);
            break;
        }else{
            err=E_TOKZ_NUMFMT;
        }
    }

    if(neg)
        exp*=-1;

#ifndef NP_SIMPLE_IMPL
    ADD_EXP(num, exp);
#else
    num->fval*=pow(num->base, exp);
#endif
    return err;
}


static int parse_number(NPNum *num, Tokenizer *tokz, int c)
{
    int base=10;
    int dm=1;
    int err=0;
    int tmp;
#ifdef NP_SIMPLE_IMPL
    double divisor=base;
#endif

    if(c=='-' || c=='+'){
        if(c=='-')
            num->negative=TRUE;
        c=GETCH();
        if(!isdigit(c))
            err=E_TOKZ_NUMFMT;
    }

    if(c=='0'){
        dm=0;
        c=GETCH();
        if(c=='x' || c=='X'){
            base=16;
            c=GETCH();
        }else if(c=='b' || c=='B'){
            base=2;
            c=GETCH();
        }else if('0'<=c && c<='7'){
            base=8;
        }else{
            dm=2;
        }
    }

    num->base=base;

    for(; 1; c=GETCH()){
        if((c=='e' || c=='E') && dm!=0){
            if(dm<2){
                err=E_TOKZ_NUMFMT;
                continue;
            }
            tmp=parse_exponent(num, tokz, c);
            if(err==0)
                err=tmp;
            break;
        }

        if(isxdigit(c)){
            if('0'<=c && c<='9')
                c-='0';
            else if(isupper(c))
                c-='A'-10;
            else
                c-='a'-10;

            if(c>=base)
                err=E_TOKZ_NUMFMT;

#ifdef NP_SIMPLE_IMPL
            if(dm==3){
                num->fval+=(double)c/divisor;
                divisor*=base;
            }else
#endif
            {
                tmp=npnum_mulbase_add(num, base, c);
                if(err==0)
                    err=tmp;
            }

            if(dm==1)
                dm=2;
#ifndef NP_SIMPLE_IMPL
            else if(dm==3)
                ADD_EXP(num, -1);
#endif
            continue;
        }

        if(c=='.'){
            if(dm!=2){
                err=E_TOKZ_NUMFMT;
            }
            dm=3;
#ifdef NP_SIMPLE_IMPL
            num->type=NPNUM_FLOAT;
            divisor=base;
#endif
            continue;
        }

        if(is_end(c)){
            UNGETCH(c);
            break;
        }

        err=E_TOKZ_NUMFMT;
    }

#ifndef NP_SIMPLE_IMPL
    num->type=(num->exponent==0 ? NPNUM_INT : NPNUM_FLOAT);
#endif

    return err;
}