jsonxs_inline.h

filter: Statementstmt 60.3% 67 / 111 Branchbran 66.6% 56 / 84 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 63.0% 123 / 195 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« srcout/option_accessors.xs ↑ Summary jsonsl/jsonsl.c »
1 /**
2 * This header file contains static functions adapted from
3 * Marc Lehmanns' JSON::XS, particularly because it's fast,
4 * doesn't seem to rely on any external structures, and because
5 * numeric conversions aren't my ideas of fun.
6 */
7  
8 #ifndef JSONXS_INLINE_H_
9 #define JSONXS_INLINE_H_
10  
11 #include "perl-jsonsl.h"
12 #if __GNUC__ >= 3
13 # define expect(expr,value) __builtin_expect ((expr), (value))
14 # define INLINE static inline
15 #else
16 # define expect(expr,value) (expr)
17 # define INLINE static
18 #endif
19  
20 #define ERR die
21 #define expect_false(expr) expect ((expr) != 0, 0)
22 #define expect_true(expr) expect ((expr) != 0, 1)
23  
24 #define jsonxs__atof_scan1(s,a,e,p,m) jsonxs__atof_scan1_THX(aTHX_ s,a,e,p,m)
25 INLINE void
26 20 jsonxs__atof_scan1_THX(pTHX_ const char *s,
27 NV *accum, int *expo, int postdp,
28 int maxdepth)
29 {
30 20 UV uaccum = 0;
31 20 int eaccum = 0;
32  
33 /* if we recurse too deep, skip all remaining digits
34 to avoid a stack overflow attack */
35 20 ▶ if (expect_false (--maxdepth <= 0))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false (--maxdepth <= 0)) 20 0 20
36 0 ▶ while (((U8)*s - '0') < 10)
Branch: 1 branch
BranchTrueFalseTotal
while (((U8)*s - '0') < 10) 0 0 0
37 0 ++s;
38  
39 for (;;)
40 41 {
41 61 U8 dig = (U8)*s - '0';
42  
43 61 ▶ if (expect_false (dig >= 10))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false (dig >= 10)) 20 41 61
44 {
45 20 ▶ if (dig == (U8)((U8)'.' - (U8)'0'))
Branch: 1 branch
BranchTrueFalseTotal
if (dig == (U8)((U8)'.' - (U8)'0')) 8 12 20
46 {
47 8 ++s;
48 8 jsonxs__atof_scan1(s, accum, expo, 1, maxdepth);
49 }
50 12 ▶ else if ((dig | ' ') == 'e' - '0')
Branch: 1 branch
BranchTrueFalseTotal
else if ((dig | ' ') == 'e' - '0') 8 4 12
51 {
52 8 int exp2 = 0;
53 8 int neg = 0;
54  
55 8 ++s;
56  
57 8 ▶ if (*s == '-')
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '-') 2 6 8
58 {
59 2 ++s;
60 2 neg = 1;
61 }
62 6 ▶ else if (*s == '+')
Branch: 1 branch
BranchTrueFalseTotal
else if (*s == '+') 2 4 6
63 2 ++s;
64  
65 17 ▶ while ((dig = (U8)*s - '0') < 10)
Branch: 1 branch
BranchTrueFalseTotal
while ((dig = (U8)*s - '0') < 10) 9 8 17
66 9 exp2 = exp2 * 10 + *s++ - '0';
67  
68 8 ▶ *expo += neg ? -exp2 : exp2;
Branch: 1 branch
BranchTrueFalseTotal
*expo += neg ? -exp2 : exp2; 2 6 8
69 }
70  
71 20 break;
72 }
73  
74 41 ++s;
75  
76 41 uaccum = uaccum * 10 + dig;
77 41 ++eaccum;
78  
79 /*
80 if we have too many digits, then recurse for more
81 we actually do this for rather few digits
82 */
83 41 ▶ if (uaccum >= (UV_MAX - 9) / 10)
Branch: 1 branch
BranchTrueFalseTotal
if (uaccum >= (UV_MAX - 9) / 10) 0 41 41
84 {
85 0 ▶ if (postdp) *expo -= eaccum;
Branch: 1 branch
BranchTrueFalseTotal
if (postdp) *expo -= eaccum; 0 0 0
86 0 jsonxs__atof_scan1 (s, accum, expo, postdp, maxdepth);
87 0 ▶ if (postdp) *expo += eaccum;
Branch: 1 branch
BranchTrueFalseTotal
if (postdp) *expo += eaccum; 0 0 0
88  
89 0 break;
90 }
91 }
92  
93 /*
94 // this relies greatly on the quality of the pow ()
95 // implementation of the platform, but a good
96 // implementation is hard to beat.
97 // (IEEE 754 conformant ones are required to be exact)
98 *
99 */
100 20 ▶ if (postdp) *expo -= eaccum;
Branch: 1 branch
BranchTrueFalseTotal
if (postdp) *expo -= eaccum; 8 12 20
101 20 *accum += uaccum * Perl_pow (10., *expo);
102 20 *expo += eaccum;
103 20 }
104  
105 #define jsonxs__atof(s) jsonxs__atof_THX(aTHX_ s)
106 INLINE NV
107 12 jsonxs__atof_THX (pTHX_ const char *s)
108 {
109 12 NV accum = 0.;
110 12 int expo = 0;
111 12 int neg = 0;
112  
113 12 ▶ if (*s == '-')
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '-') 2 10 12
114 {
115 2 ++s;
116 2 neg = 1;
117 }
118  
119 /* a recursion depth of ten gives us >>500 bits */
120 12 jsonxs__atof_scan1(s, &accum, &expo, 0, 10);
121  
122 12 ▶ return neg ? -accum : accum;
Branch: 1 branch
BranchTrueFalseTotal
return neg ? -accum : accum; 2 10 12
123 }
124  
125 #define jsonxs_inline_process_number(s) jsonxs_inline_process_number_THX(aTHX_ s)
126  
127 INLINE SV *
128 12 jsonxs_inline_process_number_THX(pTHX_ const char *start)
129 {
130  
131 12 int is_nv = 0;
132 12 const char *c = start;
133  
134 12 ▶ if (*c == '-')
Branch: 1 branch
BranchTrueFalseTotal
if (*c == '-') 2 10 12
135 2 ++c;
136  
137 12 ▶ if (*c == '0') {
Branch: 1 branch
BranchTrueFalseTotal
if (*c == '0') { 3 9 12
138 3 ++c;
139 3 ▶ if (*c >= '0' && *c <= '9') {
Branch: 2 branches
BranchTrueFalseTotal
if (*c >= '0' && *c <= '9') { 0 3 3
if (*c >= '0' && *c <= '9') { 0 0 0
140 0 ERR("malformed number (leading zero must not be followed by another digit)");
141 }
142 9 ▶ } else if (*c < '0' || *c > '9') {
Branch: 2 branches
BranchTrueFalseTotal
} else if (*c < '0' || *c > '9') { 9 0 9
} else if (*c < '0' || *c > '9') { 0 9 9
143 0 ERR("malformed number (no digits after initial minus)");
144 } else {
145 do {
146 10 ++c;
147 10 ▶ } while (*c >= '0' && *c <= '9');
Branch: 2 branches
BranchTrueFalseTotal
} while (*c >= '0' && *c <= '9'); 5 5 10
} while (*c >= '0' && *c <= '9'); 1 4 5
148 }
149  
150 12 ▶ if (*c == '.') {
Branch: 1 branch
BranchTrueFalseTotal
if (*c == '.') { 8 4 12
151 8 ++c;
152  
153 8 ▶ if (*c < '0' || *c > '9')
Branch: 2 branches
BranchTrueFalseTotal
if (*c < '0' || *c > '9') 8 0 8
if (*c < '0' || *c > '9') 0 8 8
154 0 ERR("malformed number (no digits after decimal point)");
155  
156 do {
157 28 ++c;
158 28 ▶ } while (*c >= '0' && *c <= '9');
Branch: 2 branches
BranchTrueFalseTotal
} while (*c >= '0' && *c <= '9'); 27 1 28
} while (*c >= '0' && *c <= '9'); 20 7 27
159  
160 8 is_nv = 1;
161 }
162  
163 12 ▶ if (*c == 'e' || *c == 'E') {
Branch: 2 branches
BranchTrueFalseTotal
if (*c == 'e' || *c == 'E') { 6 6 12
if (*c == 'e' || *c == 'E') { 2 4 6
164 8 ++c;
165  
166 8 ▶ if (*c == '-' || *c == '+')
Branch: 2 branches
BranchTrueFalseTotal
if (*c == '-' || *c == '+') 6 2 8
if (*c == '-' || *c == '+') 2 4 6
167 4 ++c;
168  
169 8 ▶ if (*c < '0' || *c > '9')
Branch: 2 branches
BranchTrueFalseTotal
if (*c < '0' || *c > '9') 8 0 8
if (*c < '0' || *c > '9') 0 8 8
170 0 ERR("malformed number (no digits after exp sign)");
171  
172 do {
173 9 ++c;
174 9 ▶ } while (*c >= '0' && *c <= '9');
Branch: 2 branches
BranchTrueFalseTotal
} while (*c >= '0' && *c <= '9'); 8 1 9
} while (*c >= '0' && *c <= '9'); 1 7 8
175  
176 8 is_nv = 1;
177 }
178  
179 12 ▶ if (!is_nv) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_nv) { 0 12 12
180 0 int len = c - start;
181  
182 /* special case the rather common 1..5-digit-int case */
183 0 ▶ if (*start == '-')
Branch: 1 branch
BranchTrueFalseTotal
if (*start == '-') 0 0 0
184 0 switch (len) {
185 0 case 2:
186 0 return newSViv (-(IV)( start [1] - '0' * 1));
187 0 case 3:
188 0 return newSViv (-(IV)( start [1] * 10 + start [2] - '0' * 11));
189 0 case 4:
190 0 return newSViv (-(IV)( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111));
191 0 case 5:
192 0 return newSViv (-(IV)( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
193 0 case 6:
194 0 return newSViv (-(IV)(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111));
195 }
196 else
197 0 switch (len) {
198 0 case 1:
199 0 return newSViv ( start [0] - '0' * 1);
200 0 case 2:
201 0 return newSViv ( start [0] * 10 + start [1] - '0' * 11);
202 0 case 3:
203 0 return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111);
204 0 case 4:
205 0 return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
206 0 case 5:
207 0 return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111);
208 }
209  
210 {
211 UV uv;
212 0 int numtype = grok_number (start, len, &uv);
213 0 ▶ if (numtype & IS_NUMBER_IN_UV
Branch: 1 branch
BranchTrueFalseTotal
if (numtype & IS_NUMBER_IN_UV 0 0 0
214 ) {
215 0 ▶ if (numtype & IS_NUMBER_NEG)
Branch: 1 branch
BranchTrueFalseTotal
if (numtype & IS_NUMBER_NEG) 0 0 0
216 {
217 0 ▶ if (uv < (UV) IV_MIN
Branch: 1 branch
BranchTrueFalseTotal
if (uv < (UV) IV_MIN 0 0 0
218 )
219 0 return newSViv (-(IV)uv);
220 } else
221 0 return newSVuv (uv);
222 }
223 }
224  
225 0 len -= *start == '-' ? 1 : 0;
226  
227 /* does not fit into IV or UV, try NV */
228 0 ▶ if (len <= NV_DIG
Branch: 1 branch
BranchTrueFalseTotal
if (len <= NV_DIG 0 0 0
229 )
230 /* fits into NV without loss of precision */
231 0 return newSVnv (jsonxs__atof (start));
232  
233 /* everything else fails, convert it to a string */
234 0 return newSVpvn (start, c - start);
235 }
236  
237 /* loss of precision here */
238 12 return newSVnv (jsonxs__atof (start));
239 }
240  
241 #undef ERR
242 #undef expect_false
243 #undef expect_true
244  
245 #endif /* JSONXS_INLINE_H_ */
« srcout/option_accessors.xs ↑ Summary jsonsl/jsonsl.c »