tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

hb-number-parser.hh (5390B)


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