Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 245 lines (201 sloc) 6.387 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
/**
* This header file contains static functions adapted from
* Marc Lehmanns' JSON::XS, particularly because it's fast,
* doesn't seem to rely on any external structures, and because
* numeric conversions aren't my ideas of fun.
*/

#ifndef JSONXS_INLINE_H_
#define JSONXS_INLINE_H_

#include "perl-jsonsl.h"
#if __GNUC__ >= 3
# define expect(expr,value) __builtin_expect ((expr), (value))
# define INLINE static inline
#else
# define expect(expr,value) (expr)
# define INLINE static
#endif

#define ERR die
#define expect_false(expr) expect ((expr) != 0, 0)
#define expect_true(expr) expect ((expr) != 0, 1)

#define jsonxs__atof_scan1(s,a,e,p,m) jsonxs__atof_scan1_THX(aTHX_ s,a,e,p,m)
INLINE void
jsonxs__atof_scan1_THX(pTHX_ const char *s,
                        NV *accum, int *expo, int postdp,
                        int maxdepth)
{
  UV uaccum = 0;
  int eaccum = 0;

  /* if we recurse too deep, skip all remaining digits
to avoid a stack overflow attack */
  if (expect_false (--maxdepth <= 0))
    while (((U8)*s - '0') < 10)
      ++s;

  for (;;)
    {
      U8 dig = (U8)*s - '0';

      if (expect_false (dig >= 10))
        {
          if (dig == (U8)((U8)'.' - (U8)'0'))
            {
              ++s;
              jsonxs__atof_scan1(s, accum, expo, 1, maxdepth);
            }
          else if ((dig | ' ') == 'e' - '0')
            {
              int exp2 = 0;
              int neg = 0;

              ++s;

              if (*s == '-')
                {
                  ++s;
                  neg = 1;
                }
              else if (*s == '+')
                ++s;

              while ((dig = (U8)*s - '0') < 10)
                exp2 = exp2 * 10 + *s++ - '0';

              *expo += neg ? -exp2 : exp2;
            }

          break;
        }

      ++s;

      uaccum = uaccum * 10 + dig;
      ++eaccum;

      /*
if we have too many digits, then recurse for more
we actually do this for rather few digits
*/
      if (uaccum >= (UV_MAX - 9) / 10)
        {
          if (postdp) *expo -= eaccum;
          jsonxs__atof_scan1 (s, accum, expo, postdp, maxdepth);
          if (postdp) *expo += eaccum;

          break;
        }
    }

  /*
// this relies greatly on the quality of the pow ()
// implementation of the platform, but a good
// implementation is hard to beat.
// (IEEE 754 conformant ones are required to be exact)
*
*/
  if (postdp) *expo -= eaccum;
  *accum += uaccum * Perl_pow (10., *expo);
  *expo += eaccum;
}

#define jsonxs__atof(s) jsonxs__atof_THX(aTHX_ s)
INLINE NV
jsonxs__atof_THX (pTHX_ const char *s)
{
  NV accum = 0.;
  int expo = 0;
  int neg = 0;

  if (*s == '-')
    {
      ++s;
      neg = 1;
    }

  /* a recursion depth of ten gives us >>500 bits */
  jsonxs__atof_scan1(s, &accum, &expo, 0, 10);

  return neg ? -accum : accum;
}

#define jsonxs_inline_process_number(s) jsonxs_inline_process_number_THX(aTHX_ s)

INLINE SV *
jsonxs_inline_process_number_THX(pTHX_ const char *start)
{

    int is_nv = 0;
    const char *c = start;

    if (*c == '-')
        ++c;

    if (*c == '0') {
        ++c;
        if (*c >= '0' && *c <= '9') {
            ERR("malformed number (leading zero must not be followed by another digit)");
        }
    } else if (*c < '0' || *c > '9') {
        ERR("malformed number (no digits after initial minus)");
    } else {
        do {
            ++c;
        } while (*c >= '0' && *c <= '9');
    }

    if (*c == '.') {
        ++c;

        if (*c < '0' || *c > '9')
            ERR("malformed number (no digits after decimal point)");

        do {
            ++c;
        } while (*c >= '0' && *c <= '9');

        is_nv = 1;
    }

    if (*c == 'e' || *c == 'E') {
        ++c;

        if (*c == '-' || *c == '+')
            ++c;

        if (*c < '0' || *c > '9')
            ERR("malformed number (no digits after exp sign)");

        do {
            ++c;
        } while (*c >= '0' && *c <= '9');

        is_nv = 1;
    }

    if (!is_nv) {
        int len = c - start;

        /* special case the rather common 1..5-digit-int case */
        if (*start == '-')
            switch (len) {
            case 2:
                return newSViv (-(IV)( start [1] - '0' * 1));
            case 3:
                return newSViv (-(IV)( start [1] * 10 + start [2] - '0' * 11));
            case 4:
                return newSViv (-(IV)( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111));
            case 5:
                return newSViv (-(IV)( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
            case 6:
                return newSViv (-(IV)(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111));
            }
        else
            switch (len) {
            case 1:
                return newSViv ( start [0] - '0' * 1);
            case 2:
                return newSViv ( start [0] * 10 + start [1] - '0' * 11);
            case 3:
                return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111);
            case 4:
                return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
            case 5:
                return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111);
            }

        {
            UV uv;
            int numtype = grok_number (start, len, &uv);
            if (numtype & IS_NUMBER_IN_UV
                ) {
                if (numtype & IS_NUMBER_NEG)
                {
                    if (uv < (UV) IV_MIN
                        )
                        return newSViv (-(IV)uv);
                } else
                    return newSVuv (uv);
            }
        }

        len -= *start == '-' ? 1 : 0;

        /* does not fit into IV or UV, try NV */
        if (len <= NV_DIG
            )
            /* fits into NV without loss of precision */
            return newSVnv (jsonxs__atof (start));

        /* everything else fails, convert it to a string */
        return newSVpvn (start, c - start);
    }

    /* loss of precision here */
    return newSVnv (jsonxs__atof (start));
}

#undef ERR
#undef expect_false
#undef expect_true

#endif /* JSONXS_INLINE_H_ */
Something went wrong with that request. Please try again.