/src/harfbuzz/src/hb-number-parser.hh
Line | Count | Source (jump to first uncovered line) |
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 | | |
34 | | #line 35 "hb-number-parser.hh" |
35 | | static const unsigned char _double_parser_trans_keys[] = { |
36 | | 0u, 0u, 43u, 57u, 46u, 57u, 48u, 57u, 43u, 57u, 48u, 57u, 48u, 101u, 48u, 57u, |
37 | | 46u, 101u, 0 |
38 | | }; |
39 | | |
40 | | static const char _double_parser_key_spans[] = { |
41 | | 0, 15, 12, 10, 15, 10, 54, 10, |
42 | | 56 |
43 | | }; |
44 | | |
45 | | static const unsigned char _double_parser_index_offsets[] = { |
46 | | 0, 0, 16, 29, 40, 56, 67, 122, |
47 | | 133 |
48 | | }; |
49 | | |
50 | | static const char _double_parser_indicies[] = { |
51 | | 0, 1, 2, 3, 1, 4, 4, |
52 | | 4, 4, 4, 4, 4, 4, 4, 4, |
53 | | 1, 3, 1, 4, 4, 4, 4, 4, |
54 | | 4, 4, 4, 4, 4, 1, 5, 5, |
55 | | 5, 5, 5, 5, 5, 5, 5, 5, |
56 | | 1, 6, 1, 7, 1, 1, 8, 8, |
57 | | 8, 8, 8, 8, 8, 8, 8, 8, |
58 | | 1, 8, 8, 8, 8, 8, 8, 8, |
59 | | 8, 8, 8, 1, 5, 5, 5, 5, |
60 | | 5, 5, 5, 5, 5, 5, 1, 1, |
61 | | 1, 1, 1, 1, 1, 1, 1, 1, |
62 | | 1, 9, 1, 1, 1, 1, 1, 1, |
63 | | 1, 1, 1, 1, 1, 1, 1, 1, |
64 | | 1, 1, 1, 1, 1, 1, 1, 1, |
65 | | 1, 1, 1, 1, 1, 1, 1, 1, |
66 | | 1, 9, 1, 8, 8, 8, 8, 8, |
67 | | 8, 8, 8, 8, 8, 1, 3, 1, |
68 | | 4, 4, 4, 4, 4, 4, 4, 4, |
69 | | 4, 4, 1, 1, 1, 1, 1, 1, |
70 | | 1, 1, 1, 1, 1, 9, 1, 1, |
71 | | 1, 1, 1, 1, 1, 1, 1, 1, |
72 | | 1, 1, 1, 1, 1, 1, 1, 1, |
73 | | 1, 1, 1, 1, 1, 1, 1, 1, |
74 | | 1, 1, 1, 1, 1, 9, 1, 0 |
75 | | }; |
76 | | |
77 | | static const char _double_parser_trans_targs[] = { |
78 | | 2, 0, 2, 3, 8, 6, 5, 5, |
79 | | 7, 4 |
80 | | }; |
81 | | |
82 | | static const char _double_parser_trans_actions[] = { |
83 | | 0, 0, 1, 0, 2, 3, 0, 4, |
84 | | 5, 0 |
85 | | }; |
86 | | |
87 | | static const int double_parser_start = 1; |
88 | | static const int double_parser_first_final = 6; |
89 | | static const int double_parser_error = 0; |
90 | | |
91 | | static const int double_parser_en_main = 1; |
92 | | |
93 | | |
94 | | #line 68 "hb-number-parser.rl" |
95 | | |
96 | | |
97 | | /* Works only for n < 512 */ |
98 | | static inline double |
99 | | _pow10 (unsigned exponent) |
100 | 0 | { |
101 | 0 | static const double _powers_of_10[] = |
102 | 0 | { |
103 | 0 | 1.0e+256, |
104 | 0 | 1.0e+128, |
105 | 0 | 1.0e+64, |
106 | 0 | 1.0e+32, |
107 | 0 | 1.0e+16, |
108 | 0 | 1.0e+8, |
109 | 0 | 10000., |
110 | 0 | 100., |
111 | 0 | 10. |
112 | 0 | }; |
113 | 0 | unsigned mask = 1 << (ARRAY_LENGTH (_powers_of_10) - 1); |
114 | 0 | double result = 1; |
115 | 0 | for (const double *power = _powers_of_10; mask; ++power, mask >>= 1) |
116 | 0 | if (exponent & mask) result *= *power; |
117 | 0 | return result; |
118 | 0 | } |
119 | | |
120 | | /* a variant of strtod that also gets end of buffer in its second argument */ |
121 | | static inline double |
122 | | strtod_rl (const char *p, const char **end_ptr /* IN/OUT */) |
123 | 0 | { |
124 | 0 | double value = 0; |
125 | 0 | double frac = 0; |
126 | 0 | double frac_count = 0; |
127 | 0 | unsigned exp = 0; |
128 | 0 | bool neg = false, exp_neg = false, exp_overflow = false; |
129 | 0 | const unsigned long long MAX_FRACT = 0xFFFFFFFFFFFFFull; /* 2^52-1 */ |
130 | 0 | const unsigned MAX_EXP = 0x7FFu; /* 2^11-1 */ |
131 | |
|
132 | 0 | const char *pe = *end_ptr; |
133 | 0 | while (p < pe && ISSPACE (*p)) |
134 | 0 | p++; |
135 | |
|
136 | 0 | int cs; |
137 | | |
138 | 0 | #line 139 "hb-number-parser.hh" |
139 | 0 | { |
140 | 0 | cs = double_parser_start; |
141 | 0 | } |
142 | |
|
143 | 0 | #line 144 "hb-number-parser.hh" |
144 | 0 | { |
145 | 0 | int _slen; |
146 | 0 | int _trans; |
147 | 0 | const unsigned char *_keys; |
148 | 0 | const char *_inds; |
149 | 0 | if ( p == pe ) |
150 | 0 | goto _test_eof; |
151 | 0 | if ( cs == 0 ) |
152 | 0 | goto _out; |
153 | 0 | _resume: |
154 | 0 | _keys = _double_parser_trans_keys + (cs<<1); |
155 | 0 | _inds = _double_parser_indicies + _double_parser_index_offsets[cs]; |
156 | |
|
157 | 0 | _slen = _double_parser_key_spans[cs]; |
158 | 0 | _trans = _inds[ _slen > 0 && _keys[0] <=(*p) && |
159 | 0 | (*p) <= _keys[1] ? |
160 | 0 | (*p) - _keys[0] : _slen ]; |
161 | |
|
162 | 0 | cs = _double_parser_trans_targs[_trans]; |
163 | |
|
164 | 0 | if ( _double_parser_trans_actions[_trans] == 0 ) |
165 | 0 | goto _again; |
166 | | |
167 | 0 | switch ( _double_parser_trans_actions[_trans] ) { |
168 | 0 | case 1: |
169 | 0 | #line 37 "hb-number-parser.rl" |
170 | 0 | { neg = true; } |
171 | 0 | break; |
172 | 0 | case 4: |
173 | 0 | #line 38 "hb-number-parser.rl" |
174 | 0 | { exp_neg = true; } |
175 | 0 | break; |
176 | 0 | case 2: |
177 | 0 | #line 40 "hb-number-parser.rl" |
178 | 0 | { |
179 | 0 | value = value * 10. + ((*p) - '0'); |
180 | 0 | } |
181 | 0 | break; |
182 | 0 | case 3: |
183 | 0 | #line 43 "hb-number-parser.rl" |
184 | 0 | { |
185 | 0 | if (likely (frac <= MAX_FRACT / 10)) |
186 | 0 | { |
187 | 0 | frac = frac * 10. + ((*p) - '0'); |
188 | 0 | ++frac_count; |
189 | 0 | } |
190 | 0 | } |
191 | 0 | break; |
192 | 0 | case 5: |
193 | 0 | #line 50 "hb-number-parser.rl" |
194 | 0 | { |
195 | 0 | if (likely (exp * 10 + ((*p) - '0') <= MAX_EXP)) |
196 | 0 | exp = exp * 10 + ((*p) - '0'); |
197 | 0 | else |
198 | 0 | exp_overflow = true; |
199 | 0 | } |
200 | 0 | break; |
201 | 0 | #line 202 "hb-number-parser.hh" |
202 | 0 | } |
203 | | |
204 | 0 | _again: |
205 | 0 | if ( cs == 0 ) |
206 | 0 | goto _out; |
207 | 0 | if ( ++p != pe ) |
208 | 0 | goto _resume; |
209 | 0 | _test_eof: {} |
210 | 0 | _out: {} |
211 | 0 | } |
212 | | |
213 | 0 | #line 113 "hb-number-parser.rl" |
214 | | |
215 | | |
216 | 0 | *end_ptr = p; |
217 | |
|
218 | 0 | if (frac_count) value += frac / _pow10 (frac_count); |
219 | 0 | if (neg) value *= -1.; |
220 | |
|
221 | 0 | if (unlikely (exp_overflow)) |
222 | 0 | { |
223 | 0 | if (value == 0) return value; |
224 | 0 | if (exp_neg) return neg ? -DBL_MIN : DBL_MIN; |
225 | 0 | else return neg ? -DBL_MAX : DBL_MAX; |
226 | 0 | } |
227 | | |
228 | 0 | if (exp) |
229 | 0 | { |
230 | 0 | if (exp_neg) value /= _pow10 (exp); |
231 | 0 | else value *= _pow10 (exp); |
232 | 0 | } |
233 | |
|
234 | 0 | return value; |
235 | 0 | } |
236 | | |
237 | | #endif /* HB_NUMBER_PARSER_HH */ |