1 | |
2 | #line 1 "hb-number-parser.rl" |
3 | /* |
4 | * Copyright © 2019 Ebrahim Byagowi |
5 | * |
6 | * This is part of HarfBuzz, a text shaping library. |
7 | * |
8 | * Permission is hereby granted, without written agreement and without |
9 | * license or royalty fees, to use, copy, modify, and distribute this |
10 | * software and its documentation for any purpose, provided that the |
11 | * above copyright notice and the following two paragraphs appear in |
12 | * all copies of this software. |
13 | * |
14 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
15 | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
16 | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
17 | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
18 | * DAMAGE. |
19 | * |
20 | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
21 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
22 | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
23 | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
24 | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
25 | * |
26 | */ |
27 | |
28 | #ifndef HB_NUMBER_PARSER_HH |
29 | #define HB_NUMBER_PARSER_HH |
30 | |
31 | #include "hb.hh" |
32 | |
33 | #include <float.h> |
34 | |
35 | |
36 | #line 37 "hb-number-parser.hh" |
37 | static const unsigned char _double_parser_trans_keys[] = { |
38 | 0u, 0u, 43u, 57u, 46u, 57u, 48u, 57u, 43u, 57u, 48u, 57u, 48u, 101u, 48u, 57u, |
39 | 46u, 101u, 0 |
40 | }; |
41 | |
42 | static const char _double_parser_key_spans[] = { |
43 | 0, 15, 12, 10, 15, 10, 54, 10, |
44 | 56 |
45 | }; |
46 | |
47 | static const unsigned char _double_parser_index_offsets[] = { |
48 | 0, 0, 16, 29, 40, 56, 67, 122, |
49 | 133 |
50 | }; |
51 | |
52 | static const char _double_parser_indicies[] = { |
53 | 0, 1, 2, 3, 1, 4, 4, |
54 | 4, 4, 4, 4, 4, 4, 4, 4, |
55 | 1, 3, 1, 4, 4, 4, 4, 4, |
56 | 4, 4, 4, 4, 4, 1, 5, 5, |
57 | 5, 5, 5, 5, 5, 5, 5, 5, |
58 | 1, 6, 1, 7, 1, 1, 8, 8, |
59 | 8, 8, 8, 8, 8, 8, 8, 8, |
60 | 1, 8, 8, 8, 8, 8, 8, 8, |
61 | 8, 8, 8, 1, 5, 5, 5, 5, |
62 | 5, 5, 5, 5, 5, 5, 1, 1, |
63 | 1, 1, 1, 1, 1, 1, 1, 1, |
64 | 1, 9, 1, 1, 1, 1, 1, 1, |
65 | 1, 1, 1, 1, 1, 1, 1, 1, |
66 | 1, 1, 1, 1, 1, 1, 1, 1, |
67 | 1, 1, 1, 1, 1, 1, 1, 1, |
68 | 1, 9, 1, 8, 8, 8, 8, 8, |
69 | 8, 8, 8, 8, 8, 1, 3, 1, |
70 | 4, 4, 4, 4, 4, 4, 4, 4, |
71 | 4, 4, 1, 1, 1, 1, 1, 1, |
72 | 1, 1, 1, 1, 1, 9, 1, 1, |
73 | 1, 1, 1, 1, 1, 1, 1, 1, |
74 | 1, 1, 1, 1, 1, 1, 1, 1, |
75 | 1, 1, 1, 1, 1, 1, 1, 1, |
76 | 1, 1, 1, 1, 1, 9, 1, 0 |
77 | }; |
78 | |
79 | static const char _double_parser_trans_targs[] = { |
80 | 2, 0, 2, 3, 8, 6, 5, 5, |
81 | 7, 4 |
82 | }; |
83 | |
84 | static const char _double_parser_trans_actions[] = { |
85 | 0, 0, 1, 0, 2, 3, 0, 4, |
86 | 5, 0 |
87 | }; |
88 | |
89 | static const int double_parser_start = 1; |
90 | static const int double_parser_first_final = 6; |
91 | static const int double_parser_error = 0; |
92 | |
93 | static const int double_parser_en_main = 1; |
94 | |
95 | |
96 | #line 70 "hb-number-parser.rl" |
97 | |
98 | |
99 | /* Works only for n < 512 */ |
100 | static inline double |
101 | _pow10 (unsigned int exponent) |
102 | { |
103 | static const double _powers_of_10[] = |
104 | { |
105 | 1.0e+256, |
106 | 1.0e+128, |
107 | 1.0e+64, |
108 | 1.0e+32, |
109 | 1.0e+16, |
110 | 1.0e+8, |
111 | 10000., |
112 | 100., |
113 | 10. |
114 | }; |
115 | unsigned int mask = 1 << (ARRAY_LENGTH (_powers_of_10) - 1); |
116 | double result = 1; |
117 | for (const double *power = _powers_of_10; mask; ++power, mask >>= 1) |
118 | if (exponent & mask) result *= *power; |
119 | return result; |
120 | } |
121 | |
122 | static inline double |
123 | strtod_rl (const char *buf, char **end_ptr) |
124 | { |
125 | const char *p, *pe; |
126 | double value = 0; |
127 | double frac = 0; |
128 | double frac_count = 0; |
129 | unsigned int exp = 0; |
130 | bool neg = false, exp_neg = false, exp_overflow = false; |
131 | const unsigned long long MAX_FRACT = 0xFFFFFFFFFFFFFull; /* 1^52-1 */ |
132 | const unsigned int MAX_EXP = 0x7FFu; /* 1^11-1 */ |
133 | p = buf; |
134 | pe = p + strlen (p); |
135 | |
136 | while (p < pe && ISSPACE (*p)) |
137 | p++; |
138 | |
139 | int cs; |
140 | |
141 | #line 142 "hb-number-parser.hh" |
142 | { |
143 | cs = double_parser_start; |
144 | } |
145 | |
146 | #line 147 "hb-number-parser.hh" |
147 | { |
148 | int _slen; |
149 | int _trans; |
150 | const unsigned char *_keys; |
151 | const char *_inds; |
152 | if ( p == pe ) |
153 | goto _test_eof; |
154 | if ( cs == 0 ) |
155 | goto _out; |
156 | _resume: |
157 | _keys = _double_parser_trans_keys + (cs<<1); |
158 | _inds = _double_parser_indicies + _double_parser_index_offsets[cs]; |
159 | |
160 | _slen = _double_parser_key_spans[cs]; |
161 | _trans = _inds[ _slen > 0 && _keys[0] <=(*p) && |
162 | (*p) <= _keys[1] ? |
163 | (*p) - _keys[0] : _slen ]; |
164 | |
165 | cs = _double_parser_trans_targs[_trans]; |
166 | |
167 | if ( _double_parser_trans_actions[_trans] == 0 ) |
168 | goto _again; |
169 | |
170 | switch ( _double_parser_trans_actions[_trans] ) { |
171 | case 1: |
172 | #line 39 "hb-number-parser.rl" |
173 | { neg = true; } |
174 | break; |
175 | case 4: |
176 | #line 40 "hb-number-parser.rl" |
177 | { exp_neg = true; } |
178 | break; |
179 | case 2: |
180 | #line 42 "hb-number-parser.rl" |
181 | { |
182 | value = value * 10. + ((*p) - '0'); |
183 | } |
184 | break; |
185 | case 3: |
186 | #line 45 "hb-number-parser.rl" |
187 | { |
188 | if (likely (frac <= MAX_FRACT / 10)) |
189 | { |
190 | frac = frac * 10. + ((*p) - '0'); |
191 | ++frac_count; |
192 | } |
193 | } |
194 | break; |
195 | case 5: |
196 | #line 52 "hb-number-parser.rl" |
197 | { |
198 | if (likely (exp * 10 + ((*p) - '0') <= MAX_EXP)) |
199 | exp = exp * 10 + ((*p) - '0'); |
200 | else |
201 | exp_overflow = true; |
202 | } |
203 | break; |
204 | #line 205 "hb-number-parser.hh" |
205 | } |
206 | |
207 | _again: |
208 | if ( cs == 0 ) |
209 | goto _out; |
210 | if ( ++p != pe ) |
211 | goto _resume; |
212 | _test_eof: {} |
213 | _out: {} |
214 | } |
215 | |
216 | #line 116 "hb-number-parser.rl" |
217 | |
218 | |
219 | *end_ptr = (char *) p; |
220 | |
221 | if (frac_count) value += frac / _pow10 (frac_count); |
222 | if (neg) value *= -1.; |
223 | |
224 | if (unlikely (exp_overflow)) |
225 | { |
226 | if (value == 0) return value; |
227 | if (exp_neg) return neg ? -DBL_MIN : DBL_MIN; |
228 | else return neg ? -DBL_MAX : DBL_MAX; |
229 | } |
230 | |
231 | if (exp) |
232 | { |
233 | if (exp_neg) value /= _pow10 (exp); |
234 | else value *= _pow10 (exp); |
235 | } |
236 | |
237 | return value; |
238 | } |
239 | |
240 | #endif /* HB_NUMBER_PARSER_HH */ |
241 | |