]> granicus.if.org Git - libass/blob - libass/ass_strtod.c
Fix whitespace trimming
[libass] / libass / ass_strtod.c
1 /*
2  * Copyright (c) 1988-1993 The Regents of the University of California.
3  * Copyright (c) 1994 Sun Microsystems, Inc.
4  *
5  * Permission to use, copy, modify, and distribute this
6  * software and its documentation for any purpose and without
7  * fee is hereby granted, provided that the above copyright
8  * notice appear in all copies.  The University of California
9  * makes no representations about the suitability of this
10  * software for any purpose.  It is provided "as is" without
11  * express or implied warranty.
12  *
13  */
14
15 #include <stdlib.h>
16 #include <ctype.h>
17 #include <errno.h>
18
19 const
20 static int maxExponent = 511;   /* Largest possible base 10 exponent.  Any
21                                  * exponent larger than this will already
22                                  * produce underflow or overflow, so there's
23                                  * no need to worry about additional digits.
24                                  */
25
26 const
27 static double powersOf10[] = {  /* Table giving binary powers of 10.  Entry */
28     10.,                        /* is 10^2^i.  Used to convert decimal */
29     100.,                       /* exponents into floating-point numbers. */
30     1.0e4,
31     1.0e8,
32     1.0e16,
33     1.0e32,
34     1.0e64,
35     1.0e128,
36     1.0e256
37 };
38
39 /*
40  *----------------------------------------------------------------------
41  *
42  * strtod --
43  *
44  * This procedure converts a floating-point number from an ASCII
45  * decimal representation to internal double-precision format.
46  *
47  * Results:
48  * The return value is the double-precision floating-point
49  * representation of the characters in string.  If endPtr isn't
50  * NULL, then *endPtr is filled in with the address of the
51  * next character after the last one that was part of the
52  * floating-point number.
53  *
54  * Side effects:
55  * None.
56  *
57  *----------------------------------------------------------------------
58  */
59
60 double
61 ass_strtod(string, endPtr)
62     const char *string;     /* A decimal ASCII floating-point number,
63                              * optionally preceded by white space.
64                              * Must have form "-I.FE-X", where I is the
65                              * integer part of the mantissa, F is the
66                              * fractional part of the mantissa, and X
67                              * is the exponent.  Either of the signs
68                              * may be "+", "-", or omitted.  Either I
69                              * or F may be omitted, or both.  The decimal
70                              * point isn't necessary unless F is present.
71                              * The "E" may actually be an "e".  E and X
72                              * may both be omitted (but not just one).
73                              */
74     char **endPtr;          /* If non-NULL, store terminating character's
75                              * address here. */
76 {
77     int sign, expSign = 0;
78     double fraction, dblExp, *d;
79     register const char *p;
80     register int c;
81     int exp = 0;            /* Exponent read from "EX" field. */
82     int fracExp = 0;        /* Exponent that derives from the fractional
83                              * part.  Under normal circumstatnces, it is
84                              * the negative of the number of digits in F.
85                              * However, if I is very long, the last digits
86                              * of I get dropped (otherwise a long I with a
87                              * large negative exponent could cause an
88                              * unnecessary overflow on I alone).  In this
89                              * case, fracExp is incremented one for each
90                              * dropped digit. */
91     int mantSize;       /* Number of digits in mantissa. */
92     int decPt;          /* Number of mantissa digits BEFORE decimal
93                          * point. */
94     const char *pExp;       /* Temporarily holds location of exponent
95                              * in string. */
96
97     /*
98      * Strip off leading blanks and check for a sign.
99      */
100
101     p = string;
102     while (isspace(*p)) {
103         p += 1;
104     }
105     if (*p == '-') {
106         sign = 1;
107         p += 1;
108     } else {
109         if (*p == '+') {
110             p += 1;
111         }
112         sign = 0;
113     }
114
115     /*
116      * Count the number of digits in the mantissa (including the decimal
117      * point), and also locate the decimal point.
118      */
119
120     decPt = -1;
121     for (mantSize = 0; ; mantSize += 1)
122     {
123         c = *p;
124         if (!isdigit(c)) {
125             if ((c != '.') || (decPt >= 0)) {
126                 break;
127             }
128             decPt = mantSize;
129         }
130         p += 1;
131     }
132
133     /*
134      * Now suck up the digits in the mantissa.  Use two integers to
135      * collect 9 digits each (this is faster than using floating-point).
136      * If the mantissa has more than 18 digits, ignore the extras, since
137      * they can't affect the value anyway.
138      */
139
140     pExp  = p;
141     p -= mantSize;
142     if (decPt < 0) {
143         decPt = mantSize;
144     } else {
145         mantSize -= 1;      /* One of the digits was the point. */
146     }
147     if (mantSize > 18) {
148         fracExp = decPt - 18;
149         mantSize = 18;
150     } else {
151         fracExp = decPt - mantSize;
152     }
153     if (mantSize == 0) {
154         fraction = 0.0;
155         p = string;
156         goto done;
157     } else {
158         int frac1, frac2;
159         frac1 = 0;
160         for ( ; mantSize > 9; mantSize -= 1)
161         {
162             c = *p;
163             p += 1;
164             if (c == '.') {
165                 c = *p;
166                 p += 1;
167             }
168             frac1 = 10*frac1 + (c - '0');
169         }
170         frac2 = 0;
171         for (; mantSize > 0; mantSize -= 1)
172         {
173             c = *p;
174             p += 1;
175             if (c == '.') {
176                 c = *p;
177                 p += 1;
178             }
179             frac2 = 10*frac2 + (c - '0');
180         }
181         fraction = (1.0e9 * frac1) + frac2;
182     }
183
184     /*
185      * Skim off the exponent.
186      */
187
188     p = pExp;
189     if ((*p == 'E') || (*p == 'e')) {
190         p += 1;
191         if (*p == '-') {
192             expSign = 1;
193             p += 1;
194         } else {
195             if (*p == '+') {
196                 p += 1;
197             }
198             expSign = 0;
199         }
200         while (isdigit(*p)) {
201             exp = exp * 10 + (*p - '0');
202             p += 1;
203         }
204     }
205     if (expSign) {
206         exp = fracExp - exp;
207     } else {
208         exp = fracExp + exp;
209     }
210
211     /*
212      * Generate a floating-point number that represents the exponent.
213      * Do this by processing the exponent one bit at a time to combine
214      * many powers of 2 of 10. Then combine the exponent with the
215      * fraction.
216      */
217
218     if (exp < 0) {
219         expSign = 1;
220         exp = -exp;
221     } else {
222         expSign = 0;
223     }
224     if (exp > maxExponent) {
225         exp = maxExponent;
226         errno = ERANGE;
227     }
228     dblExp = 1.0;
229     for (d = (double *) powersOf10; exp != 0; exp >>= 1, d += 1) {
230         if (exp & 01) {
231             dblExp *= *d;
232         }
233     }
234     if (expSign) {
235         fraction /= dblExp;
236     } else {
237         fraction *= dblExp;
238     }
239
240 done:
241     if (endPtr != NULL) {
242         *endPtr = (char *) p;
243     }
244
245     if (sign) {
246         return -fraction;
247     }
248     return fraction;
249 }