blob: c6c4a3bab88d997f2df5ed99f9c017f60d371504 [file] [log] [blame]
Ebrahim Byagowi47d82712019-09-06 20:33:10 +04301/*
2 * Copyright © 2019 Ebrahim Byagowi
3 *
4 * This is part of HarfBuzz, a text shaping library.
5 *
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
11 *
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16 * DAMAGE.
17 *
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23 *
24 */
25
26#ifndef HB_NUMBER_PARSER_HH
27#define HB_NUMBER_PARSER_HH
28
29#include "hb.hh"
30
31%%{
32
33machine double_parser;
34alphtype unsigned char;
35write data;
36
37action see_neg { neg = true; }
38action see_exp_neg { exp_neg = true; }
39
Ebrahim Byagowi39cf8e22019-09-07 15:27:30 +043040action add_int {
41 value = value * 10. + (fc - '0');
42}
43action add_frac {
44 if (likely (frac <= MAX_FRACT / 10))
45 {
46 frac = frac * 10. + (fc - '0');
47 ++frac_count;
48 }
49}
50action add_exp {
51 if (likely (exp * 10 + (fc - '0') <= MAX_EXP))
52 exp = exp * 10 + (fc - '0');
53 else
54 exp_overflow = true;
55}
Ebrahim Byagowi47d82712019-09-06 20:33:10 +043056
57num = [0-9]+;
58
59main := (
60 (
61 (('+'|'-'@see_neg)? num @add_int) ('.' num @add_frac)?
62 |
63 (('+'|'-'@see_neg)? '.' num @add_frac)
64 )
65 (('e'|'E') (('+'|'-'@see_exp_neg)? num @add_exp))?
66);
67
68}%%
69
Ebrahim Byagowi47d82712019-09-06 20:33:10 +043070/* Works only for n < 512 */
Ebrahim Byagowi4905a212019-09-09 12:36:12 +043071static inline double
Ebrahim Byagowidca8ba62020-05-10 16:57:45 +043072_pow10 (unsigned exponent)
Ebrahim Byagowi47d82712019-09-06 20:33:10 +043073{
Ebrahim Byagowi30e5cdf2019-09-09 00:28:16 +043074 static const double _powers_of_10[] =
75 {
76 1.0e+256,
77 1.0e+128,
78 1.0e+64,
79 1.0e+32,
80 1.0e+16,
81 1.0e+8,
82 10000.,
83 100.,
84 10.
85 };
Ebrahim Byagowidca8ba62020-05-10 16:57:45 +043086 unsigned mask = 1 << (ARRAY_LENGTH (_powers_of_10) - 1);
Ebrahim Byagowi47d82712019-09-06 20:33:10 +043087 double result = 1;
88 for (const double *power = _powers_of_10; mask; ++power, mask >>= 1)
89 if (exponent & mask) result *= *power;
90 return result;
91}
92
Ebrahim Byagowidca8ba62020-05-10 16:57:45 +043093/* a variant of strtod that also gets end of buffer in its second argument */
Ebrahim Byagowi4905a212019-09-09 12:36:12 +043094static inline double
Ebrahim Byagowi2cc78a52020-05-11 15:56:54 +043095strtod_rl (const char *p, const char **end_ptr /* IN/OUT */)
Ebrahim Byagowi47d82712019-09-06 20:33:10 +043096{
Ebrahim Byagowi47d82712019-09-06 20:33:10 +043097 double value = 0;
98 double frac = 0;
99 double frac_count = 0;
Ebrahim Byagowidca8ba62020-05-10 16:57:45 +0430100 unsigned exp = 0;
Ebrahim Byagowi39cf8e22019-09-07 15:27:30 +0430101 bool neg = false, exp_neg = false, exp_overflow = false;
Ebrahim Byagowia46ba772020-02-22 17:15:00 +0330102 const unsigned long long MAX_FRACT = 0xFFFFFFFFFFFFFull; /* 2^52-1 */
Ebrahim Byagowidca8ba62020-05-10 16:57:45 +0430103 const unsigned MAX_EXP = 0x7FFu; /* 2^11-1 */
Ebrahim Byagowi47d82712019-09-06 20:33:10 +0430104
Ebrahim Byagowi2cc78a52020-05-11 15:56:54 +0430105 const char *pe = *end_ptr;
Ebrahim Byagowi47d82712019-09-06 20:33:10 +0430106 while (p < pe && ISSPACE (*p))
107 p++;
108
109 int cs;
110 %%{
111 write init;
112 write exec;
113 }%%
114
Ebrahim Byagowi2cc78a52020-05-11 15:56:54 +0430115 *end_ptr = p;
Ebrahim Byagowi47d82712019-09-06 20:33:10 +0430116
117 if (frac_count) value += frac / _pow10 (frac_count);
118 if (neg) value *= -1.;
119
Ebrahim Byagowi39cf8e22019-09-07 15:27:30 +0430120 if (unlikely (exp_overflow))
121 {
122 if (value == 0) return value;
123 if (exp_neg) return neg ? -DBL_MIN : DBL_MIN;
124 else return neg ? -DBL_MAX : DBL_MAX;
125 }
126
Ebrahim Byagowi47d82712019-09-06 20:33:10 +0430127 if (exp)
128 {
Ebrahim Byagowi39cf8e22019-09-07 15:27:30 +0430129 if (exp_neg) value /= _pow10 (exp);
130 else value *= _pow10 (exp);
Ebrahim Byagowi47d82712019-09-06 20:33:10 +0430131 }
132
133 return value;
134}
135
136#endif /* HB_NUMBER_PARSER_HH */