Commit | Line | Data |
---|---|---|
98994639 HS |
1 | /* numeric.c |
2 | * | |
663f364b | 3 | * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, |
1129b882 | 4 | * 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others |
98994639 HS |
5 | * |
6 | * You may distribute under the terms of either the GNU General Public | |
7 | * License or the Artistic License, as specified in the README file. | |
8 | * | |
9 | */ | |
10 | ||
11 | /* | |
4ac71550 TC |
12 | * "That only makes eleven (plus one mislaid) and not fourteen, |
13 | * unless wizards count differently to other people." --Beorn | |
14 | * | |
15 | * [p.115 of _The Hobbit_: "Queer Lodgings"] | |
98994639 HS |
16 | */ |
17 | ||
ccfc67b7 JH |
18 | /* |
19 | =head1 Numeric functions | |
166f8a29 | 20 | |
7fefc6c1 KW |
21 | =cut |
22 | ||
166f8a29 DM |
23 | This file contains all the stuff needed by perl for manipulating numeric |
24 | values, including such things as replacements for the OS's atof() function | |
25 | ||
ccfc67b7 JH |
26 | */ |
27 | ||
98994639 HS |
28 | #include "EXTERN.h" |
29 | #define PERL_IN_NUMERIC_C | |
30 | #include "perl.h" | |
31 | ||
9ec8aea5 KW |
32 | #ifdef Perl_strtod |
33 | ||
34 | PERL_STATIC_INLINE NV | |
35 | S_strtod(pTHX_ const char * const s, char ** e) | |
36 | { | |
37 | DECLARATION_FOR_LC_NUMERIC_MANIPULATION; | |
38 | NV result; | |
39 | ||
40 | STORE_LC_NUMERIC_SET_TO_NEEDED(); | |
41 | ||
42 | # ifdef USE_QUADMATH | |
43 | ||
44 | result = strtoflt128(s, e); | |
45 | ||
46 | # elif defined(HAS_STRTOLD) && defined(HAS_LONG_DOUBLE) \ | |
47 | && defined(USE_LONG_DOUBLE) | |
48 | # if defined(__MINGW64_VERSION_MAJOR) | |
49 | /*********************************************** | |
50 | We are unable to use strtold because of | |
51 | https://sourceforge.net/p/mingw-w64/bugs/711/ | |
52 | & | |
53 | https://sourceforge.net/p/mingw-w64/bugs/725/ | |
54 | ||
55 | but __mingw_strtold is fine. | |
56 | ***********************************************/ | |
57 | ||
58 | result = __mingw_strtold(s, e); | |
59 | ||
60 | # else | |
61 | ||
62 | result = strtold(s, e); | |
63 | ||
64 | # endif | |
65 | # elif defined(HAS_STRTOD) | |
66 | ||
67 | result = strtod(s, e); | |
68 | ||
02fd9d54 KW |
69 | # else |
70 | # error No strtod() equivalent found | |
9ec8aea5 KW |
71 | # endif |
72 | ||
73 | RESTORE_LC_NUMERIC(); | |
74 | ||
75 | return result; | |
76 | } | |
77 | ||
78 | #endif /* #ifdef Perl_strtod */ | |
79 | ||
80 | /* | |
81 | ||
82 | =for apidoc my_strtod | |
83 | ||
84 | This function is equivalent to the libc strtod() function, and is available | |
85 | even on platforms that lack plain strtod(). Its return value is the best | |
86 | available precision depending on platform capabilities and F<Configure> | |
87 | options. | |
88 | ||
89 | It properly handles the locale radix character, meaning it expects a dot except | |
90 | when called from within the scope of S<C<use locale>>, in which case the radix | |
91 | character should be that specified by the current locale. | |
92 | ||
e7a3fd45 | 93 | The synonym Strtod() may be used instead. |
9ec8aea5 KW |
94 | |
95 | =cut | |
96 | ||
97 | */ | |
98 | ||
99 | NV | |
0f3d8cd4 | 100 | Perl_my_strtod(const char * const s, char **e) |
9ec8aea5 KW |
101 | { |
102 | dTHX; | |
103 | ||
104 | PERL_ARGS_ASSERT_MY_STRTOD; | |
105 | ||
106 | #ifdef Perl_strtod | |
107 | ||
108 | return S_strtod(aTHX_ s, e); | |
109 | ||
110 | #else | |
111 | ||
112 | { | |
113 | NV result; | |
114 | char ** end_ptr = NULL; | |
115 | ||
116 | *end_ptr = my_atof2(s, &result); | |
117 | if (e) { | |
118 | *e = *end_ptr; | |
119 | } | |
120 | ||
121 | if (! *end_ptr) { | |
122 | result = 0.0; | |
123 | } | |
124 | ||
125 | return result; | |
126 | } | |
127 | ||
128 | #endif | |
129 | ||
130 | } | |
131 | ||
132 | ||
98994639 | 133 | U32 |
ddeaf645 | 134 | Perl_cast_ulong(NV f) |
98994639 HS |
135 | { |
136 | if (f < 0.0) | |
137 | return f < I32_MIN ? (U32) I32_MIN : (U32)(I32) f; | |
138 | if (f < U32_MAX_P1) { | |
139 | #if CASTFLAGS & 2 | |
140 | if (f < U32_MAX_P1_HALF) | |
141 | return (U32) f; | |
142 | f -= U32_MAX_P1_HALF; | |
071db91b | 143 | return ((U32) f) | (1 + (U32_MAX >> 1)); |
98994639 HS |
144 | #else |
145 | return (U32) f; | |
146 | #endif | |
147 | } | |
148 | return f > 0 ? U32_MAX : 0 /* NaN */; | |
149 | } | |
150 | ||
151 | I32 | |
ddeaf645 | 152 | Perl_cast_i32(NV f) |
98994639 HS |
153 | { |
154 | if (f < I32_MAX_P1) | |
155 | return f < I32_MIN ? I32_MIN : (I32) f; | |
156 | if (f < U32_MAX_P1) { | |
157 | #if CASTFLAGS & 2 | |
158 | if (f < U32_MAX_P1_HALF) | |
159 | return (I32)(U32) f; | |
160 | f -= U32_MAX_P1_HALF; | |
071db91b | 161 | return (I32)(((U32) f) | (1 + (U32_MAX >> 1))); |
98994639 HS |
162 | #else |
163 | return (I32)(U32) f; | |
164 | #endif | |
165 | } | |
166 | return f > 0 ? (I32)U32_MAX : 0 /* NaN */; | |
167 | } | |
168 | ||
169 | IV | |
ddeaf645 | 170 | Perl_cast_iv(NV f) |
98994639 HS |
171 | { |
172 | if (f < IV_MAX_P1) | |
173 | return f < IV_MIN ? IV_MIN : (IV) f; | |
174 | if (f < UV_MAX_P1) { | |
175 | #if CASTFLAGS & 2 | |
176 | /* For future flexibility allowing for sizeof(UV) >= sizeof(IV) */ | |
177 | if (f < UV_MAX_P1_HALF) | |
178 | return (IV)(UV) f; | |
179 | f -= UV_MAX_P1_HALF; | |
071db91b | 180 | return (IV)(((UV) f) | (1 + (UV_MAX >> 1))); |
98994639 HS |
181 | #else |
182 | return (IV)(UV) f; | |
183 | #endif | |
184 | } | |
185 | return f > 0 ? (IV)UV_MAX : 0 /* NaN */; | |
186 | } | |
187 | ||
188 | UV | |
ddeaf645 | 189 | Perl_cast_uv(NV f) |
98994639 HS |
190 | { |
191 | if (f < 0.0) | |
192 | return f < IV_MIN ? (UV) IV_MIN : (UV)(IV) f; | |
193 | if (f < UV_MAX_P1) { | |
194 | #if CASTFLAGS & 2 | |
195 | if (f < UV_MAX_P1_HALF) | |
196 | return (UV) f; | |
197 | f -= UV_MAX_P1_HALF; | |
071db91b | 198 | return ((UV) f) | (1 + (UV_MAX >> 1)); |
98994639 HS |
199 | #else |
200 | return (UV) f; | |
201 | #endif | |
202 | } | |
203 | return f > 0 ? UV_MAX : 0 /* NaN */; | |
204 | } | |
205 | ||
53305cf1 NC |
206 | /* |
207 | =for apidoc grok_bin | |
98994639 | 208 | |
53305cf1 NC |
209 | converts a string representing a binary number to numeric form. |
210 | ||
2d7f6611 | 211 | On entry C<start> and C<*len> give the string to scan, C<*flags> gives |
796b6530 | 212 | conversion flags, and C<result> should be C<NULL> or a pointer to an NV. |
53305cf1 | 213 | The scan stops at the end of the string, or the first invalid character. |
2d7f6611 | 214 | Unless C<PERL_SCAN_SILENT_ILLDIGIT> is set in C<*flags>, encountering an |
7b667b5f | 215 | invalid character will also trigger a warning. |
2d7f6611 KW |
216 | On return C<*len> is set to the length of the scanned string, |
217 | and C<*flags> gives output flags. | |
53305cf1 | 218 | |
7fc63493 | 219 | If the value is <= C<UV_MAX> it is returned as a UV, the output flags are clear, |
796b6530 KW |
220 | and nothing is written to C<*result>. If the value is > C<UV_MAX>, C<grok_bin> |
221 | returns C<UV_MAX>, sets C<PERL_SCAN_GREATER_THAN_UV_MAX> in the output flags, | |
2d7f6611 | 222 | and writes the value to C<*result> (or the value is discarded if C<result> |
53305cf1 NC |
223 | is NULL). |
224 | ||
796b6530 | 225 | The binary number may optionally be prefixed with C<"0b"> or C<"b"> unless |
2d7f6611 KW |
226 | C<PERL_SCAN_DISALLOW_PREFIX> is set in C<*flags> on entry. If |
227 | C<PERL_SCAN_ALLOW_UNDERSCORES> is set in C<*flags> then the binary | |
796b6530 | 228 | number may use C<"_"> characters to separate digits. |
53305cf1 | 229 | |
ceea512d KW |
230 | =for apidoc Amnh||PERL_SCAN_ALLOW_UNDERSCORES |
231 | =for apidoc Amnh||PERL_SCAN_DISALLOW_PREFIX | |
232 | =for apidoc Amnh||PERL_SCAN_GREATER_THAN_UV_MAX | |
233 | =for apidoc Amnh||PERL_SCAN_SILENT_ILLDIGIT | |
234 | =for apidoc Amnh||PERL_SCAN_TRAILING | |
235 | ||
53305cf1 | 236 | =cut |
02470786 KW |
237 | |
238 | Not documented yet because experimental is C<PERL_SCAN_SILENT_NON_PORTABLE | |
239 | which suppresses any message for non-portable numbers that are still valid | |
240 | on this platform. | |
53305cf1 NC |
241 | */ |
242 | ||
243 | UV | |
7918f24d NC |
244 | Perl_grok_bin(pTHX_ const char *start, STRLEN *len_p, I32 *flags, NV *result) |
245 | { | |
53305cf1 NC |
246 | const char *s = start; |
247 | STRLEN len = *len_p; | |
248 | UV value = 0; | |
249 | NV value_nv = 0; | |
250 | ||
251 | const UV max_div_2 = UV_MAX / 2; | |
f2338a2e | 252 | const bool allow_underscores = cBOOL(*flags & PERL_SCAN_ALLOW_UNDERSCORES); |
53305cf1 | 253 | bool overflowed = FALSE; |
7fc63493 | 254 | char bit; |
53305cf1 | 255 | |
7918f24d NC |
256 | PERL_ARGS_ASSERT_GROK_BIN; |
257 | ||
a4c04bdc NC |
258 | if (!(*flags & PERL_SCAN_DISALLOW_PREFIX)) { |
259 | /* strip off leading b or 0b. | |
260 | for compatibility silently suffer "b" and "0b" as valid binary | |
261 | numbers. */ | |
262 | if (len >= 1) { | |
305b8651 | 263 | if (isALPHA_FOLD_EQ(s[0], 'b')) { |
a4c04bdc NC |
264 | s++; |
265 | len--; | |
266 | } | |
305b8651 | 267 | else if (len >= 2 && s[0] == '0' && (isALPHA_FOLD_EQ(s[1], 'b'))) { |
a4c04bdc NC |
268 | s+=2; |
269 | len-=2; | |
270 | } | |
271 | } | |
53305cf1 NC |
272 | } |
273 | ||
7fc63493 | 274 | for (; len-- && (bit = *s); s++) { |
53305cf1 NC |
275 | if (bit == '0' || bit == '1') { |
276 | /* Write it in this wonky order with a goto to attempt to get the | |
277 | compiler to make the common case integer-only loop pretty tight. | |
278 | With gcc seems to be much straighter code than old scan_bin. */ | |
279 | redo: | |
280 | if (!overflowed) { | |
281 | if (value <= max_div_2) { | |
282 | value = (value << 1) | (bit - '0'); | |
283 | continue; | |
284 | } | |
285 | /* Bah. We're just overflowed. */ | |
dcbac5bb | 286 | /* diag_listed_as: Integer overflow in %s number */ |
9b387841 NC |
287 | Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW), |
288 | "Integer overflow in binary number"); | |
53305cf1 NC |
289 | overflowed = TRUE; |
290 | value_nv = (NV) value; | |
291 | } | |
292 | value_nv *= 2.0; | |
98994639 | 293 | /* If an NV has not enough bits in its mantissa to |
d1be9408 | 294 | * represent a UV this summing of small low-order numbers |
98994639 HS |
295 | * is a waste of time (because the NV cannot preserve |
296 | * the low-order bits anyway): we could just remember when | |
53305cf1 | 297 | * did we overflow and in the end just multiply value_nv by the |
98994639 | 298 | * right amount. */ |
53305cf1 NC |
299 | value_nv += (NV)(bit - '0'); |
300 | continue; | |
301 | } | |
302 | if (bit == '_' && len && allow_underscores && (bit = s[1]) | |
303 | && (bit == '0' || bit == '1')) | |
98994639 HS |
304 | { |
305 | --len; | |
306 | ++s; | |
53305cf1 | 307 | goto redo; |
98994639 | 308 | } |
a2a5de95 NC |
309 | if (!(*flags & PERL_SCAN_SILENT_ILLDIGIT)) |
310 | Perl_ck_warner(aTHX_ packWARN(WARN_DIGIT), | |
311 | "Illegal binary digit '%c' ignored", *s); | |
53305cf1 | 312 | break; |
98994639 | 313 | } |
19c1206d | 314 | |
53305cf1 | 315 | if ( ( overflowed && value_nv > 4294967295.0) |
98994639 | 316 | #if UVSIZE > 4 |
02470786 KW |
317 | || (!overflowed && value > 0xffffffff |
318 | && ! (*flags & PERL_SCAN_SILENT_NON_PORTABLE)) | |
98994639 HS |
319 | #endif |
320 | ) { | |
a2a5de95 NC |
321 | Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE), |
322 | "Binary number > 0b11111111111111111111111111111111 non-portable"); | |
53305cf1 NC |
323 | } |
324 | *len_p = s - start; | |
325 | if (!overflowed) { | |
326 | *flags = 0; | |
327 | return value; | |
98994639 | 328 | } |
53305cf1 NC |
329 | *flags = PERL_SCAN_GREATER_THAN_UV_MAX; |
330 | if (result) | |
331 | *result = value_nv; | |
332 | return UV_MAX; | |
98994639 HS |
333 | } |
334 | ||
53305cf1 NC |
335 | /* |
336 | =for apidoc grok_hex | |
337 | ||
338 | converts a string representing a hex number to numeric form. | |
339 | ||
2d7f6611 | 340 | On entry C<start> and C<*len_p> give the string to scan, C<*flags> gives |
796b6530 | 341 | conversion flags, and C<result> should be C<NULL> or a pointer to an NV. |
7b667b5f | 342 | The scan stops at the end of the string, or the first invalid character. |
2d7f6611 | 343 | Unless C<PERL_SCAN_SILENT_ILLDIGIT> is set in C<*flags>, encountering an |
7b667b5f | 344 | invalid character will also trigger a warning. |
2d7f6611 KW |
345 | On return C<*len> is set to the length of the scanned string, |
346 | and C<*flags> gives output flags. | |
53305cf1 | 347 | |
796b6530 KW |
348 | If the value is <= C<UV_MAX> it is returned as a UV, the output flags are clear, |
349 | and nothing is written to C<*result>. If the value is > C<UV_MAX>, C<grok_hex> | |
350 | returns C<UV_MAX>, sets C<PERL_SCAN_GREATER_THAN_UV_MAX> in the output flags, | |
2d7f6611 | 351 | and writes the value to C<*result> (or the value is discarded if C<result> |
796b6530 | 352 | is C<NULL>). |
53305cf1 | 353 | |
796b6530 | 354 | The hex number may optionally be prefixed with C<"0x"> or C<"x"> unless |
2d7f6611 KW |
355 | C<PERL_SCAN_DISALLOW_PREFIX> is set in C<*flags> on entry. If |
356 | C<PERL_SCAN_ALLOW_UNDERSCORES> is set in C<*flags> then the hex | |
796b6530 | 357 | number may use C<"_"> characters to separate digits. |
53305cf1 NC |
358 | |
359 | =cut | |
02470786 KW |
360 | |
361 | Not documented yet because experimental is C<PERL_SCAN_SILENT_NON_PORTABLE | |
baf48926 | 362 | which suppresses any message for non-portable numbers, but which are valid |
02470786 | 363 | on this platform. |
53305cf1 NC |
364 | */ |
365 | ||
366 | UV | |
7918f24d NC |
367 | Perl_grok_hex(pTHX_ const char *start, STRLEN *len_p, I32 *flags, NV *result) |
368 | { | |
53305cf1 NC |
369 | const char *s = start; |
370 | STRLEN len = *len_p; | |
371 | UV value = 0; | |
372 | NV value_nv = 0; | |
53305cf1 | 373 | const UV max_div_16 = UV_MAX / 16; |
f2338a2e | 374 | const bool allow_underscores = cBOOL(*flags & PERL_SCAN_ALLOW_UNDERSCORES); |
53305cf1 | 375 | bool overflowed = FALSE; |
98994639 | 376 | |
7918f24d NC |
377 | PERL_ARGS_ASSERT_GROK_HEX; |
378 | ||
a4c04bdc NC |
379 | if (!(*flags & PERL_SCAN_DISALLOW_PREFIX)) { |
380 | /* strip off leading x or 0x. | |
381 | for compatibility silently suffer "x" and "0x" as valid hex numbers. | |
382 | */ | |
383 | if (len >= 1) { | |
305b8651 | 384 | if (isALPHA_FOLD_EQ(s[0], 'x')) { |
a4c04bdc NC |
385 | s++; |
386 | len--; | |
387 | } | |
305b8651 | 388 | else if (len >= 2 && s[0] == '0' && (isALPHA_FOLD_EQ(s[1], 'x'))) { |
a4c04bdc NC |
389 | s+=2; |
390 | len-=2; | |
391 | } | |
392 | } | |
98994639 HS |
393 | } |
394 | ||
395 | for (; len-- && *s; s++) { | |
626ef089 | 396 | if (isXDIGIT(*s)) { |
53305cf1 NC |
397 | /* Write it in this wonky order with a goto to attempt to get the |
398 | compiler to make the common case integer-only loop pretty tight. | |
399 | With gcc seems to be much straighter code than old scan_hex. */ | |
400 | redo: | |
401 | if (!overflowed) { | |
402 | if (value <= max_div_16) { | |
626ef089 | 403 | value = (value << 4) | XDIGIT_VALUE(*s); |
53305cf1 NC |
404 | continue; |
405 | } | |
406 | /* Bah. We're just overflowed. */ | |
dcbac5bb | 407 | /* diag_listed_as: Integer overflow in %s number */ |
9b387841 NC |
408 | Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW), |
409 | "Integer overflow in hexadecimal number"); | |
53305cf1 NC |
410 | overflowed = TRUE; |
411 | value_nv = (NV) value; | |
412 | } | |
413 | value_nv *= 16.0; | |
414 | /* If an NV has not enough bits in its mantissa to | |
d1be9408 | 415 | * represent a UV this summing of small low-order numbers |
53305cf1 NC |
416 | * is a waste of time (because the NV cannot preserve |
417 | * the low-order bits anyway): we could just remember when | |
418 | * did we overflow and in the end just multiply value_nv by the | |
419 | * right amount of 16-tuples. */ | |
626ef089 | 420 | value_nv += (NV) XDIGIT_VALUE(*s); |
53305cf1 NC |
421 | continue; |
422 | } | |
423 | if (*s == '_' && len && allow_underscores && s[1] | |
626ef089 | 424 | && isXDIGIT(s[1])) |
98994639 HS |
425 | { |
426 | --len; | |
427 | ++s; | |
53305cf1 | 428 | goto redo; |
98994639 | 429 | } |
a2a5de95 NC |
430 | if (!(*flags & PERL_SCAN_SILENT_ILLDIGIT)) |
431 | Perl_ck_warner(aTHX_ packWARN(WARN_DIGIT), | |
53305cf1 NC |
432 | "Illegal hexadecimal digit '%c' ignored", *s); |
433 | break; | |
434 | } | |
19c1206d | 435 | |
53305cf1 NC |
436 | if ( ( overflowed && value_nv > 4294967295.0) |
437 | #if UVSIZE > 4 | |
02470786 KW |
438 | || (!overflowed && value > 0xffffffff |
439 | && ! (*flags & PERL_SCAN_SILENT_NON_PORTABLE)) | |
53305cf1 NC |
440 | #endif |
441 | ) { | |
a2a5de95 NC |
442 | Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE), |
443 | "Hexadecimal number > 0xffffffff non-portable"); | |
53305cf1 NC |
444 | } |
445 | *len_p = s - start; | |
446 | if (!overflowed) { | |
447 | *flags = 0; | |
448 | return value; | |
449 | } | |
450 | *flags = PERL_SCAN_GREATER_THAN_UV_MAX; | |
451 | if (result) | |
452 | *result = value_nv; | |
453 | return UV_MAX; | |
454 | } | |
455 | ||
456 | /* | |
457 | =for apidoc grok_oct | |
458 | ||
7b667b5f MHM |
459 | converts a string representing an octal number to numeric form. |
460 | ||
2d7f6611 | 461 | On entry C<start> and C<*len> give the string to scan, C<*flags> gives |
796b6530 | 462 | conversion flags, and C<result> should be C<NULL> or a pointer to an NV. |
7b667b5f | 463 | The scan stops at the end of the string, or the first invalid character. |
2d7f6611 | 464 | Unless C<PERL_SCAN_SILENT_ILLDIGIT> is set in C<*flags>, encountering an |
154bd527 | 465 | 8 or 9 will also trigger a warning. |
2d7f6611 KW |
466 | On return C<*len> is set to the length of the scanned string, |
467 | and C<*flags> gives output flags. | |
7b667b5f | 468 | |
796b6530 KW |
469 | If the value is <= C<UV_MAX> it is returned as a UV, the output flags are clear, |
470 | and nothing is written to C<*result>. If the value is > C<UV_MAX>, C<grok_oct> | |
471 | returns C<UV_MAX>, sets C<PERL_SCAN_GREATER_THAN_UV_MAX> in the output flags, | |
2d7f6611 | 472 | and writes the value to C<*result> (or the value is discarded if C<result> |
796b6530 | 473 | is C<NULL>). |
7b667b5f | 474 | |
2d7f6611 | 475 | If C<PERL_SCAN_ALLOW_UNDERSCORES> is set in C<*flags> then the octal |
796b6530 | 476 | number may use C<"_"> characters to separate digits. |
53305cf1 NC |
477 | |
478 | =cut | |
02470786 | 479 | |
333ae27c KW |
480 | Not documented yet because experimental is C<PERL_SCAN_SILENT_NON_PORTABLE> |
481 | which suppresses any message for non-portable numbers, but which are valid | |
02470786 | 482 | on this platform. |
53305cf1 NC |
483 | */ |
484 | ||
485 | UV | |
7918f24d NC |
486 | Perl_grok_oct(pTHX_ const char *start, STRLEN *len_p, I32 *flags, NV *result) |
487 | { | |
53305cf1 NC |
488 | const char *s = start; |
489 | STRLEN len = *len_p; | |
490 | UV value = 0; | |
491 | NV value_nv = 0; | |
53305cf1 | 492 | const UV max_div_8 = UV_MAX / 8; |
f2338a2e | 493 | const bool allow_underscores = cBOOL(*flags & PERL_SCAN_ALLOW_UNDERSCORES); |
53305cf1 NC |
494 | bool overflowed = FALSE; |
495 | ||
7918f24d NC |
496 | PERL_ARGS_ASSERT_GROK_OCT; |
497 | ||
53305cf1 | 498 | for (; len-- && *s; s++) { |
626ef089 | 499 | if (isOCTAL(*s)) { |
53305cf1 NC |
500 | /* Write it in this wonky order with a goto to attempt to get the |
501 | compiler to make the common case integer-only loop pretty tight. | |
502 | */ | |
503 | redo: | |
504 | if (!overflowed) { | |
505 | if (value <= max_div_8) { | |
626ef089 | 506 | value = (value << 3) | OCTAL_VALUE(*s); |
53305cf1 NC |
507 | continue; |
508 | } | |
509 | /* Bah. We're just overflowed. */ | |
dcbac5bb | 510 | /* diag_listed_as: Integer overflow in %s number */ |
9b387841 NC |
511 | Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW), |
512 | "Integer overflow in octal number"); | |
53305cf1 NC |
513 | overflowed = TRUE; |
514 | value_nv = (NV) value; | |
515 | } | |
516 | value_nv *= 8.0; | |
98994639 | 517 | /* If an NV has not enough bits in its mantissa to |
d1be9408 | 518 | * represent a UV this summing of small low-order numbers |
98994639 HS |
519 | * is a waste of time (because the NV cannot preserve |
520 | * the low-order bits anyway): we could just remember when | |
53305cf1 NC |
521 | * did we overflow and in the end just multiply value_nv by the |
522 | * right amount of 8-tuples. */ | |
626ef089 | 523 | value_nv += (NV) OCTAL_VALUE(*s); |
53305cf1 NC |
524 | continue; |
525 | } | |
626ef089 KW |
526 | if (*s == '_' && len && allow_underscores && isOCTAL(s[1])) { |
527 | --len; | |
528 | ++s; | |
529 | goto redo; | |
530 | } | |
53305cf1 | 531 | /* Allow \octal to work the DWIM way (that is, stop scanning |
7b667b5f | 532 | * as soon as non-octal characters are seen, complain only if |
626ef089 KW |
533 | * someone seems to want to use the digits eight and nine. Since we |
534 | * know it is not octal, then if isDIGIT, must be an 8 or 9). */ | |
535 | if (isDIGIT(*s)) { | |
a2a5de95 NC |
536 | if (!(*flags & PERL_SCAN_SILENT_ILLDIGIT)) |
537 | Perl_ck_warner(aTHX_ packWARN(WARN_DIGIT), | |
538 | "Illegal octal digit '%c' ignored", *s); | |
53305cf1 NC |
539 | } |
540 | break; | |
98994639 | 541 | } |
19c1206d | 542 | |
53305cf1 | 543 | if ( ( overflowed && value_nv > 4294967295.0) |
98994639 | 544 | #if UVSIZE > 4 |
02470786 KW |
545 | || (!overflowed && value > 0xffffffff |
546 | && ! (*flags & PERL_SCAN_SILENT_NON_PORTABLE)) | |
98994639 HS |
547 | #endif |
548 | ) { | |
a2a5de95 NC |
549 | Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE), |
550 | "Octal number > 037777777777 non-portable"); | |
53305cf1 NC |
551 | } |
552 | *len_p = s - start; | |
553 | if (!overflowed) { | |
554 | *flags = 0; | |
555 | return value; | |
98994639 | 556 | } |
53305cf1 NC |
557 | *flags = PERL_SCAN_GREATER_THAN_UV_MAX; |
558 | if (result) | |
559 | *result = value_nv; | |
560 | return UV_MAX; | |
561 | } | |
562 | ||
563 | /* | |
564 | =for apidoc scan_bin | |
565 | ||
72d33970 | 566 | For backwards compatibility. Use C<grok_bin> instead. |
53305cf1 NC |
567 | |
568 | =for apidoc scan_hex | |
569 | ||
72d33970 | 570 | For backwards compatibility. Use C<grok_hex> instead. |
53305cf1 NC |
571 | |
572 | =for apidoc scan_oct | |
573 | ||
72d33970 | 574 | For backwards compatibility. Use C<grok_oct> instead. |
53305cf1 NC |
575 | |
576 | =cut | |
577 | */ | |
578 | ||
579 | NV | |
73d840c0 | 580 | Perl_scan_bin(pTHX_ const char *start, STRLEN len, STRLEN *retlen) |
53305cf1 NC |
581 | { |
582 | NV rnv; | |
583 | I32 flags = *retlen ? PERL_SCAN_ALLOW_UNDERSCORES : 0; | |
73d840c0 | 584 | const UV ruv = grok_bin (start, &len, &flags, &rnv); |
53305cf1 | 585 | |
7918f24d NC |
586 | PERL_ARGS_ASSERT_SCAN_BIN; |
587 | ||
53305cf1 NC |
588 | *retlen = len; |
589 | return (flags & PERL_SCAN_GREATER_THAN_UV_MAX) ? rnv : (NV)ruv; | |
590 | } | |
591 | ||
592 | NV | |
73d840c0 | 593 | Perl_scan_oct(pTHX_ const char *start, STRLEN len, STRLEN *retlen) |
53305cf1 NC |
594 | { |
595 | NV rnv; | |
596 | I32 flags = *retlen ? PERL_SCAN_ALLOW_UNDERSCORES : 0; | |
73d840c0 | 597 | const UV ruv = grok_oct (start, &len, &flags, &rnv); |
53305cf1 | 598 | |
7918f24d NC |
599 | PERL_ARGS_ASSERT_SCAN_OCT; |
600 | ||
53305cf1 NC |
601 | *retlen = len; |
602 | return (flags & PERL_SCAN_GREATER_THAN_UV_MAX) ? rnv : (NV)ruv; | |
603 | } | |
604 | ||
605 | NV | |
73d840c0 | 606 | Perl_scan_hex(pTHX_ const char *start, STRLEN len, STRLEN *retlen) |
53305cf1 NC |
607 | { |
608 | NV rnv; | |
609 | I32 flags = *retlen ? PERL_SCAN_ALLOW_UNDERSCORES : 0; | |
73d840c0 | 610 | const UV ruv = grok_hex (start, &len, &flags, &rnv); |
53305cf1 | 611 | |
7918f24d NC |
612 | PERL_ARGS_ASSERT_SCAN_HEX; |
613 | ||
53305cf1 NC |
614 | *retlen = len; |
615 | return (flags & PERL_SCAN_GREATER_THAN_UV_MAX) ? rnv : (NV)ruv; | |
98994639 HS |
616 | } |
617 | ||
618 | /* | |
619 | =for apidoc grok_numeric_radix | |
620 | ||
621 | Scan and skip for a numeric decimal separator (radix). | |
622 | ||
623 | =cut | |
624 | */ | |
625 | bool | |
626 | Perl_grok_numeric_radix(pTHX_ const char **sp, const char *send) | |
627 | { | |
7918f24d NC |
628 | PERL_ARGS_ASSERT_GROK_NUMERIC_RADIX; |
629 | ||
7ea85fa8 KW |
630 | #ifdef USE_LOCALE_NUMERIC |
631 | ||
d6ded950 | 632 | if (IN_LC(LC_NUMERIC)) { |
f0dafd73 KW |
633 | STRLEN len; |
634 | char * radix; | |
635 | bool matches_radix = FALSE; | |
67d796ae | 636 | DECLARATION_FOR_LC_NUMERIC_MANIPULATION; |
f0dafd73 | 637 | |
a1395eaf | 638 | STORE_LC_NUMERIC_FORCE_TO_UNDERLYING(); |
f0dafd73 KW |
639 | |
640 | radix = SvPV(PL_numeric_radix_sv, len); | |
641 | radix = savepvn(radix, len); | |
642 | ||
21431899 | 643 | RESTORE_LC_NUMERIC(); |
f0dafd73 KW |
644 | |
645 | if (*sp + len <= send) { | |
646 | matches_radix = memEQ(*sp, radix, len); | |
647 | } | |
648 | ||
649 | Safefree(radix); | |
650 | ||
651 | if (matches_radix) { | |
652 | *sp += len; | |
653 | return TRUE; | |
654 | } | |
98994639 | 655 | } |
f0dafd73 | 656 | |
98994639 | 657 | #endif |
7918f24d | 658 | |
f0dafd73 KW |
659 | /* always try "." if numeric radix didn't match because |
660 | * we may have data from different locales mixed */ | |
98994639 HS |
661 | if (*sp < send && **sp == '.') { |
662 | ++*sp; | |
663 | return TRUE; | |
664 | } | |
f0dafd73 | 665 | |
98994639 HS |
666 | return FALSE; |
667 | } | |
668 | ||
569f27e5 | 669 | /* |
ff4eb398 JH |
670 | =for apidoc grok_infnan |
671 | ||
796b6530 | 672 | Helper for C<grok_number()>, accepts various ways of spelling "infinity" |
ff4eb398 JH |
673 | or "not a number", and returns one of the following flag combinations: |
674 | ||
5962c2f6 | 675 | IS_NUMBER_INFINITY |
ff4eb398 | 676 | IS_NUMBER_NAN |
5962c2f6 | 677 | IS_NUMBER_INFINITY | IS_NUMBER_NEG |
ff4eb398 JH |
678 | IS_NUMBER_NAN | IS_NUMBER_NEG |
679 | 0 | |
680 | ||
796b6530 | 681 | possibly |-ed with C<IS_NUMBER_TRAILING>. |
b489e20f | 682 | |
796b6530 | 683 | If an infinity or a not-a-number is recognized, C<*sp> will point to |
62bdc035 | 684 | one byte past the end of the recognized string. If the recognition fails, |
796b6530 | 685 | zero is returned, and C<*sp> will not move. |
ff4eb398 | 686 | |
ceea512d KW |
687 | =for apidoc Amn|bool|IS_NUMBER_GREATER_THAN_UV_MAX |
688 | =for apidoc Amn|bool|IS_NUMBER_INFINITY | |
689 | =for apidoc Amn|bool|IS_NUMBER_IN_UV | |
690 | =for apidoc Amn|bool|IS_NUMBER_NAN | |
691 | =for apidoc Amn|bool|IS_NUMBER_NEG | |
692 | =for apidoc Amn|bool|IS_NUMBER_NOT_INT | |
693 | ||
ff4eb398 JH |
694 | =cut |
695 | */ | |
696 | ||
697 | int | |
3823048b | 698 | Perl_grok_infnan(pTHX_ const char** sp, const char* send) |
ff4eb398 JH |
699 | { |
700 | const char* s = *sp; | |
701 | int flags = 0; | |
a5dc2484 | 702 | #if defined(NV_INF) || defined(NV_NAN) |
62bdc035 | 703 | bool odh = FALSE; /* one-dot-hash: 1.#INF */ |
ff4eb398 JH |
704 | |
705 | PERL_ARGS_ASSERT_GROK_INFNAN; | |
706 | ||
8c12dc63 JH |
707 | if (*s == '+') { |
708 | s++; if (s == send) return 0; | |
709 | } | |
710 | else if (*s == '-') { | |
ff4eb398 JH |
711 | flags |= IS_NUMBER_NEG; /* Yes, -NaN happens. Incorrect but happens. */ |
712 | s++; if (s == send) return 0; | |
713 | } | |
714 | ||
715 | if (*s == '1') { | |
62bdc035 JH |
716 | /* Visual C: 1.#SNAN, -1.#QNAN, 1#INF, 1.#IND (maybe also 1.#NAN) |
717 | * Let's keep the dot optional. */ | |
ff4eb398 JH |
718 | s++; if (s == send) return 0; |
719 | if (*s == '.') { | |
720 | s++; if (s == send) return 0; | |
721 | } | |
722 | if (*s == '#') { | |
723 | s++; if (s == send) return 0; | |
724 | } else | |
725 | return 0; | |
e855f543 | 726 | odh = TRUE; |
ff4eb398 JH |
727 | } |
728 | ||
305b8651 | 729 | if (isALPHA_FOLD_EQ(*s, 'I')) { |
62bdc035 JH |
730 | /* INF or IND (1.#IND is "indeterminate", a certain type of NAN) */ |
731 | ||
305b8651 | 732 | s++; if (s == send || isALPHA_FOLD_NE(*s, 'N')) return 0; |
ff4eb398 | 733 | s++; if (s == send) return 0; |
305b8651 | 734 | if (isALPHA_FOLD_EQ(*s, 'F')) { |
ff4eb398 | 735 | s++; |
b8974fcb JH |
736 | if (s < send && (isALPHA_FOLD_EQ(*s, 'I'))) { |
737 | int fail = | |
738 | flags | IS_NUMBER_INFINITY | IS_NUMBER_NOT_INT | IS_NUMBER_TRAILING; | |
739 | s++; if (s == send || isALPHA_FOLD_NE(*s, 'N')) return fail; | |
740 | s++; if (s == send || isALPHA_FOLD_NE(*s, 'I')) return fail; | |
741 | s++; if (s == send || isALPHA_FOLD_NE(*s, 'T')) return fail; | |
742 | s++; if (s == send || isALPHA_FOLD_NE(*s, 'Y')) return fail; | |
3396ed30 | 743 | s++; |
b8974fcb JH |
744 | } else if (odh) { |
745 | while (*s == '0') { /* 1.#INF00 */ | |
746 | s++; | |
747 | } | |
3396ed30 | 748 | } |
b489e20f JH |
749 | while (s < send && isSPACE(*s)) |
750 | s++; | |
751 | if (s < send && *s) { | |
3396ed30 | 752 | flags |= IS_NUMBER_TRAILING; |
fae4db12 | 753 | } |
ff4eb398 JH |
754 | flags |= IS_NUMBER_INFINITY | IS_NUMBER_NOT_INT; |
755 | } | |
e855f543 | 756 | else if (isALPHA_FOLD_EQ(*s, 'D') && odh) { /* 1.#IND */ |
ff4eb398 JH |
757 | s++; |
758 | flags |= IS_NUMBER_NAN | IS_NUMBER_NOT_INT; | |
fae4db12 JH |
759 | while (*s == '0') { /* 1.#IND00 */ |
760 | s++; | |
761 | } | |
1e9aa12f JH |
762 | if (*s) { |
763 | flags |= IS_NUMBER_TRAILING; | |
764 | } | |
ff4eb398 JH |
765 | } else |
766 | return 0; | |
ff4eb398 JH |
767 | } |
768 | else { | |
62bdc035 | 769 | /* Maybe NAN of some sort */ |
3823048b JH |
770 | |
771 | if (isALPHA_FOLD_EQ(*s, 'S') || isALPHA_FOLD_EQ(*s, 'Q')) { | |
772 | /* snan, qNaN */ | |
773 | /* XXX do something with the snan/qnan difference */ | |
774 | s++; if (s == send) return 0; | |
775 | } | |
776 | ||
777 | if (isALPHA_FOLD_EQ(*s, 'N')) { | |
778 | s++; if (s == send || isALPHA_FOLD_NE(*s, 'A')) return 0; | |
779 | s++; if (s == send || isALPHA_FOLD_NE(*s, 'N')) return 0; | |
780 | s++; | |
781 | ||
782 | flags |= IS_NUMBER_NAN | IS_NUMBER_NOT_INT; | |
783 | ||
784 | /* NaN can be followed by various stuff (NaNQ, NaNS), but | |
785 | * there are also multiple different NaN values, and some | |
786 | * implementations output the "payload" values, | |
787 | * e.g. NaN123, NAN(abc), while some legacy implementations | |
788 | * have weird stuff like NaN%. */ | |
789 | if (isALPHA_FOLD_EQ(*s, 'q') || | |
790 | isALPHA_FOLD_EQ(*s, 's')) { | |
791 | /* "nanq" or "nans" are ok, though generating | |
792 | * these portably is tricky. */ | |
793 | s++; | |
794 | } | |
795 | if (*s == '(') { | |
796 | /* C99 style "nan(123)" or Perlish equivalent "nan($uv)". */ | |
797 | const char *t; | |
798 | s++; | |
799 | if (s == send) { | |
800 | return flags | IS_NUMBER_TRAILING; | |
801 | } | |
802 | t = s + 1; | |
803 | while (t < send && *t && *t != ')') { | |
804 | t++; | |
805 | } | |
806 | if (t == send) { | |
807 | return flags | IS_NUMBER_TRAILING; | |
808 | } | |
809 | if (*t == ')') { | |
810 | int nantype; | |
811 | UV nanval; | |
812 | if (s[0] == '0' && s + 2 < t && | |
813 | isALPHA_FOLD_EQ(s[1], 'x') && | |
814 | isXDIGIT(s[2])) { | |
815 | STRLEN len = t - s; | |
816 | I32 flags = PERL_SCAN_ALLOW_UNDERSCORES; | |
817 | nanval = grok_hex(s, &len, &flags, NULL); | |
818 | if ((flags & PERL_SCAN_GREATER_THAN_UV_MAX)) { | |
819 | nantype = 0; | |
820 | } else { | |
821 | nantype = IS_NUMBER_IN_UV; | |
822 | } | |
823 | s += len; | |
824 | } else if (s[0] == '0' && s + 2 < t && | |
825 | isALPHA_FOLD_EQ(s[1], 'b') && | |
826 | (s[2] == '0' || s[2] == '1')) { | |
827 | STRLEN len = t - s; | |
828 | I32 flags = PERL_SCAN_ALLOW_UNDERSCORES; | |
829 | nanval = grok_bin(s, &len, &flags, NULL); | |
830 | if ((flags & PERL_SCAN_GREATER_THAN_UV_MAX)) { | |
831 | nantype = 0; | |
832 | } else { | |
833 | nantype = IS_NUMBER_IN_UV; | |
834 | } | |
835 | s += len; | |
836 | } else { | |
837 | const char *u; | |
838 | nantype = | |
839 | grok_number_flags(s, t - s, &nanval, | |
840 | PERL_SCAN_TRAILING | | |
841 | PERL_SCAN_ALLOW_UNDERSCORES); | |
842 | /* Unfortunately grok_number_flags() doesn't | |
843 | * tell how far we got and the ')' will always | |
844 | * be "trailing", so we need to double-check | |
845 | * whether we had something dubious. */ | |
846 | for (u = s; u < t; u++) { | |
847 | if (!isDIGIT(*u)) { | |
848 | flags |= IS_NUMBER_TRAILING; | |
849 | break; | |
850 | } | |
851 | } | |
852 | s = u; | |
853 | } | |
854 | ||
855 | /* XXX Doesn't do octal: nan("0123"). | |
856 | * Probably not a big loss. */ | |
857 | ||
858 | if ((nantype & IS_NUMBER_NOT_INT) || | |
859 | !(nantype && IS_NUMBER_IN_UV)) { | |
860 | /* XXX the nanval is currently unused, that is, | |
861 | * not inserted as the NaN payload of the NV. | |
862 | * But the above code already parses the C99 | |
863 | * nan(...) format. See below, and see also | |
864 | * the nan() in POSIX.xs. | |
865 | * | |
866 | * Certain configuration combinations where | |
867 | * NVSIZE is greater than UVSIZE mean that | |
868 | * a single UV cannot contain all the possible | |
869 | * NaN payload bits. There would need to be | |
870 | * some more generic syntax than "nan($uv)". | |
871 | * | |
872 | * Issues to keep in mind: | |
873 | * | |
874 | * (1) In most common cases there would | |
875 | * not be an integral number of bytes that | |
876 | * could be set, only a certain number of bits. | |
877 | * For example for the common case of | |
878 | * NVSIZE == UVSIZE == 8 there is room for 52 | |
879 | * bits in the payload, but the most significant | |
880 | * bit is commonly reserved for the | |
881 | * signaling/quiet bit, leaving 51 bits. | |
882 | * Furthermore, the C99 nan() is supposed | |
883 | * to generate quiet NaNs, so it is doubtful | |
884 | * whether it should be able to generate | |
885 | * signaling NaNs. For the x86 80-bit doubles | |
886 | * (if building a long double Perl) there would | |
887 | * be 62 bits (s/q bit being the 63rd). | |
888 | * | |
889 | * (2) Endianness of the payload bits. If the | |
890 | * payload is specified as an UV, the low-order | |
891 | * bits of the UV are naturally little-endianed | |
892 | * (rightmost) bits of the payload. The endianness | |
893 | * of UVs and NVs can be different. */ | |
894 | return 0; | |
895 | } | |
896 | if (s < t) { | |
897 | flags |= IS_NUMBER_TRAILING; | |
898 | } | |
899 | } else { | |
900 | /* Looked like nan(...), but no close paren. */ | |
901 | flags |= IS_NUMBER_TRAILING; | |
902 | } | |
903 | } else { | |
904 | while (s < send && isSPACE(*s)) | |
905 | s++; | |
906 | if (s < send && *s) { | |
907 | /* Note that we here implicitly accept (parse as | |
908 | * "nan", but with warnings) also any other weird | |
909 | * trailing stuff for "nan". In the above we just | |
910 | * check that if we got the C99-style "nan(...)", | |
911 | * the "..." looks sane. | |
912 | * If in future we accept more ways of specifying | |
913 | * the nan payload, the accepting would happen around | |
914 | * here. */ | |
915 | flags |= IS_NUMBER_TRAILING; | |
916 | } | |
917 | } | |
918 | s = send; | |
919 | } | |
920 | else | |
921 | return 0; | |
ff4eb398 JH |
922 | } |
923 | ||
b489e20f JH |
924 | while (s < send && isSPACE(*s)) |
925 | s++; | |
926 | ||
a5dc2484 JH |
927 | #else |
928 | PERL_UNUSED_ARG(send); | |
929 | #endif /* #if defined(NV_INF) || defined(NV_NAN) */ | |
a1fe7cea JH |
930 | *sp = s; |
931 | return flags; | |
ff4eb398 JH |
932 | } |
933 | ||
13393a5e | 934 | /* |
3823048b | 935 | =for apidoc grok_number_flags |
13393a5e JH |
936 | |
937 | Recognise (or not) a number. The type of the number is returned | |
938 | (0 if unrecognised), otherwise it is a bit-ORed combination of | |
796b6530 KW |
939 | C<IS_NUMBER_IN_UV>, C<IS_NUMBER_GREATER_THAN_UV_MAX>, C<IS_NUMBER_NOT_INT>, |
940 | C<IS_NUMBER_NEG>, C<IS_NUMBER_INFINITY>, C<IS_NUMBER_NAN> (defined in perl.h). | |
941 | ||
942 | If the value of the number can fit in a UV, it is returned in C<*valuep>. | |
943 | C<IS_NUMBER_IN_UV> will be set to indicate that C<*valuep> is valid, C<IS_NUMBER_IN_UV> | |
944 | will never be set unless C<*valuep> is valid, but C<*valuep> may have been assigned | |
945 | to during processing even though C<IS_NUMBER_IN_UV> is not set on return. | |
946 | If C<valuep> is C<NULL>, C<IS_NUMBER_IN_UV> will be set for the same cases as when | |
947 | C<valuep> is non-C<NULL>, but no actual assignment (or SEGV) will occur. | |
948 | ||
949 | C<IS_NUMBER_NOT_INT> will be set with C<IS_NUMBER_IN_UV> if trailing decimals were | |
950 | seen (in which case C<*valuep> gives the true value truncated to an integer), and | |
951 | C<IS_NUMBER_NEG> if the number is negative (in which case C<*valuep> holds the | |
952 | absolute value). C<IS_NUMBER_IN_UV> is not set if e notation was used or the | |
13393a5e JH |
953 | number is larger than a UV. |
954 | ||
955 | C<flags> allows only C<PERL_SCAN_TRAILING>, which allows for trailing | |
956 | non-numeric text on an otherwise successful I<grok>, setting | |
957 | C<IS_NUMBER_TRAILING> on the result. | |
958 | ||
959 | =for apidoc grok_number | |
960 | ||
796b6530 | 961 | Identical to C<grok_number_flags()> with C<flags> set to zero. |
13393a5e JH |
962 | |
963 | =cut | |
964 | */ | |
965 | int | |
966 | Perl_grok_number(pTHX_ const char *pv, STRLEN len, UV *valuep) | |
967 | { | |
968 | PERL_ARGS_ASSERT_GROK_NUMBER; | |
969 | ||
970 | return grok_number_flags(pv, len, valuep, 0); | |
971 | } | |
972 | ||
945b524a JH |
973 | static const UV uv_max_div_10 = UV_MAX / 10; |
974 | static const U8 uv_max_mod_10 = UV_MAX % 10; | |
975 | ||
3f7602fa | 976 | int |
3823048b | 977 | Perl_grok_number_flags(pTHX_ const char *pv, STRLEN len, UV *valuep, U32 flags) |
3f7602fa | 978 | { |
60939fb8 | 979 | const char *s = pv; |
c4420975 | 980 | const char * const send = pv + len; |
ae776a2c | 981 | const char *d; |
60939fb8 | 982 | int numtype = 0; |
60939fb8 | 983 | |
3823048b | 984 | PERL_ARGS_ASSERT_GROK_NUMBER_FLAGS; |
7918f24d | 985 | |
60939fb8 NC |
986 | while (s < send && isSPACE(*s)) |
987 | s++; | |
988 | if (s == send) { | |
989 | return 0; | |
990 | } else if (*s == '-') { | |
991 | s++; | |
992 | numtype = IS_NUMBER_NEG; | |
993 | } | |
994 | else if (*s == '+') | |
aa42a541 | 995 | s++; |
60939fb8 NC |
996 | |
997 | if (s == send) | |
998 | return 0; | |
999 | ||
ae776a2c | 1000 | /* The first digit (after optional sign): note that might |
8c12dc63 | 1001 | * also point to "infinity" or "nan", or "1.#INF". */ |
ae776a2c JH |
1002 | d = s; |
1003 | ||
8c12dc63 | 1004 | /* next must be digit or the radix separator or beginning of infinity/nan */ |
60939fb8 NC |
1005 | if (isDIGIT(*s)) { |
1006 | /* UVs are at least 32 bits, so the first 9 decimal digits cannot | |
1007 | overflow. */ | |
1008 | UV value = *s - '0'; | |
1009 | /* This construction seems to be more optimiser friendly. | |
1010 | (without it gcc does the isDIGIT test and the *s - '0' separately) | |
1011 | With it gcc on arm is managing 6 instructions (6 cycles) per digit. | |
1012 | In theory the optimiser could deduce how far to unroll the loop | |
1013 | before checking for overflow. */ | |
58bb9ec3 NC |
1014 | if (++s < send) { |
1015 | int digit = *s - '0'; | |
f85b645f | 1016 | if (inRANGE(digit, 0, 9)) { |
60939fb8 | 1017 | value = value * 10 + digit; |
58bb9ec3 NC |
1018 | if (++s < send) { |
1019 | digit = *s - '0'; | |
f85b645f | 1020 | if (inRANGE(digit, 0, 9)) { |
60939fb8 | 1021 | value = value * 10 + digit; |
58bb9ec3 NC |
1022 | if (++s < send) { |
1023 | digit = *s - '0'; | |
f85b645f | 1024 | if (inRANGE(digit, 0, 9)) { |
60939fb8 | 1025 | value = value * 10 + digit; |
58bb9ec3 NC |
1026 | if (++s < send) { |
1027 | digit = *s - '0'; | |
f85b645f | 1028 | if (inRANGE(digit, 0, 9)) { |
60939fb8 | 1029 | value = value * 10 + digit; |
58bb9ec3 NC |
1030 | if (++s < send) { |
1031 | digit = *s - '0'; | |
f85b645f | 1032 | if (inRANGE(digit, 0, 9)) { |
60939fb8 | 1033 | value = value * 10 + digit; |
58bb9ec3 NC |
1034 | if (++s < send) { |
1035 | digit = *s - '0'; | |
f85b645f | 1036 | if (inRANGE(digit, 0, 9)) { |
60939fb8 | 1037 | value = value * 10 + digit; |
58bb9ec3 NC |
1038 | if (++s < send) { |
1039 | digit = *s - '0'; | |
f85b645f | 1040 | if (inRANGE(digit, 0, 9)) { |
60939fb8 | 1041 | value = value * 10 + digit; |
58bb9ec3 NC |
1042 | if (++s < send) { |
1043 | digit = *s - '0'; | |
f85b645f | 1044 | if (inRANGE(digit, 0, 9)) { |
60939fb8 | 1045 | value = value * 10 + digit; |
58bb9ec3 | 1046 | if (++s < send) { |
60939fb8 NC |
1047 | /* Now got 9 digits, so need to check |
1048 | each time for overflow. */ | |
58bb9ec3 | 1049 | digit = *s - '0'; |
f85b645f | 1050 | while ( inRANGE(digit, 0, 9) |
945b524a JH |
1051 | && (value < uv_max_div_10 |
1052 | || (value == uv_max_div_10 | |
1053 | && digit <= uv_max_mod_10))) { | |
60939fb8 | 1054 | value = value * 10 + digit; |
58bb9ec3 NC |
1055 | if (++s < send) |
1056 | digit = *s - '0'; | |
60939fb8 NC |
1057 | else |
1058 | break; | |
1059 | } | |
f85b645f | 1060 | if (inRANGE(digit, 0, 9) |
51bd16da | 1061 | && (s < send)) { |
60939fb8 NC |
1062 | /* value overflowed. |
1063 | skip the remaining digits, don't | |
1064 | worry about setting *valuep. */ | |
1065 | do { | |
1066 | s++; | |
1067 | } while (s < send && isDIGIT(*s)); | |
1068 | numtype |= | |
1069 | IS_NUMBER_GREATER_THAN_UV_MAX; | |
1070 | goto skip_value; | |
1071 | } | |
1072 | } | |
1073 | } | |
98994639 | 1074 | } |
60939fb8 NC |
1075 | } |
1076 | } | |
1077 | } | |
1078 | } | |
1079 | } | |
1080 | } | |
1081 | } | |
1082 | } | |
1083 | } | |
1084 | } | |
1085 | } | |
98994639 | 1086 | } |
60939fb8 | 1087 | } |
98994639 | 1088 | } |
60939fb8 NC |
1089 | numtype |= IS_NUMBER_IN_UV; |
1090 | if (valuep) | |
1091 | *valuep = value; | |
1092 | ||
1093 | skip_value: | |
1094 | if (GROK_NUMERIC_RADIX(&s, send)) { | |
1095 | numtype |= IS_NUMBER_NOT_INT; | |
1096 | while (s < send && isDIGIT(*s)) /* optional digits after the radix */ | |
1097 | s++; | |
98994639 | 1098 | } |
60939fb8 NC |
1099 | } |
1100 | else if (GROK_NUMERIC_RADIX(&s, send)) { | |
1101 | numtype |= IS_NUMBER_NOT_INT | IS_NUMBER_IN_UV; /* valuep assigned below */ | |
1102 | /* no digits before the radix means we need digits after it */ | |
1103 | if (s < send && isDIGIT(*s)) { | |
1104 | do { | |
1105 | s++; | |
1106 | } while (s < send && isDIGIT(*s)); | |
1107 | if (valuep) { | |
1108 | /* integer approximation is valid - it's 0. */ | |
1109 | *valuep = 0; | |
1110 | } | |
98994639 | 1111 | } |
60939fb8 | 1112 | else |
ae776a2c | 1113 | return 0; |
ff4eb398 | 1114 | } |
60939fb8 | 1115 | |
926f5fc6 | 1116 | if (s > d && s < send) { |
60939fb8 | 1117 | /* we can have an optional exponent part */ |
305b8651 | 1118 | if (isALPHA_FOLD_EQ(*s, 'e')) { |
60939fb8 NC |
1119 | s++; |
1120 | if (s < send && (*s == '-' || *s == '+')) | |
1121 | s++; | |
1122 | if (s < send && isDIGIT(*s)) { | |
1123 | do { | |
1124 | s++; | |
1125 | } while (s < send && isDIGIT(*s)); | |
1126 | } | |
3f7602fa TC |
1127 | else if (flags & PERL_SCAN_TRAILING) |
1128 | return numtype | IS_NUMBER_TRAILING; | |
60939fb8 | 1129 | else |
3f7602fa TC |
1130 | return 0; |
1131 | ||
1132 | /* The only flag we keep is sign. Blow away any "it's UV" */ | |
1133 | numtype &= IS_NUMBER_NEG; | |
1134 | numtype |= IS_NUMBER_NOT_INT; | |
60939fb8 NC |
1135 | } |
1136 | } | |
1137 | while (s < send && isSPACE(*s)) | |
1138 | s++; | |
1139 | if (s >= send) | |
aa8b85de | 1140 | return numtype; |
b59bf0b2 | 1141 | if (memEQs(pv, len, "0 but true")) { |
60939fb8 NC |
1142 | if (valuep) |
1143 | *valuep = 0; | |
1144 | return IS_NUMBER_IN_UV; | |
1145 | } | |
8c12dc63 JH |
1146 | /* We could be e.g. at "Inf" or "NaN", or at the "#" of "1.#INF". */ |
1147 | if ((s + 2 < send) && strchr("inqs#", toFOLD(*s))) { | |
1148 | /* Really detect inf/nan. Start at d, not s, since the above | |
1149 | * code might have already consumed the "1." or "1". */ | |
7eff3d39 | 1150 | const int infnan = Perl_grok_infnan(aTHX_ &d, send); |
8c12dc63 JH |
1151 | if ((infnan & IS_NUMBER_INFINITY)) { |
1152 | return (numtype | infnan); /* Keep sign for infinity. */ | |
1153 | } | |
1154 | else if ((infnan & IS_NUMBER_NAN)) { | |
1155 | return (numtype | infnan) & ~IS_NUMBER_NEG; /* Clear sign for nan. */ | |
1156 | } | |
1157 | } | |
3f7602fa TC |
1158 | else if (flags & PERL_SCAN_TRAILING) { |
1159 | return numtype | IS_NUMBER_TRAILING; | |
1160 | } | |
1161 | ||
60939fb8 | 1162 | return 0; |
98994639 HS |
1163 | } |
1164 | ||
6313e544 | 1165 | /* |
5d4a52b5 | 1166 | =for apidoc grok_atoUV |
6313e544 | 1167 | |
5d4a52b5 | 1168 | parse a string, looking for a decimal unsigned integer. |
338aa8b0 | 1169 | |
5d4a52b5 KW |
1170 | On entry, C<pv> points to the beginning of the string; |
1171 | C<valptr> points to a UV that will receive the converted value, if found; | |
1172 | C<endptr> is either NULL or points to a variable that points to one byte | |
1173 | beyond the point in C<pv> that this routine should examine. | |
1174 | If C<endptr> is NULL, C<pv> is assumed to be NUL-terminated. | |
f4379102 | 1175 | |
5d4a52b5 KW |
1176 | Returns FALSE if C<pv> doesn't represent a valid unsigned integer value (with |
1177 | no leading zeros). Otherwise it returns TRUE, and sets C<*valptr> to that | |
1178 | value. | |
6313e544 | 1179 | |
5d4a52b5 KW |
1180 | If you constrain the portion of C<pv> that is looked at by this function (by |
1181 | passing a non-NULL C<endptr>), and if the intial bytes of that portion form a | |
1182 | valid value, it will return TRUE, setting C<*endptr> to the byte following the | |
1183 | final digit of the value. But if there is no constraint at what's looked at, | |
1184 | all of C<pv> must be valid in order for TRUE to be returned. | |
6313e544 | 1185 | |
5d4a52b5 | 1186 | The only characters this accepts are the decimal digits '0'..'9'. |
338aa8b0 | 1187 | |
5d4a52b5 KW |
1188 | As opposed to L<atoi(3)> or L<strtol(3)>, C<grok_atoUV> does NOT allow optional |
1189 | leading whitespace, nor negative inputs. If such features are required, the | |
1190 | calling code needs to explicitly implement those. | |
6313e544 | 1191 | |
5d4a52b5 KW |
1192 | Note that this function returns FALSE for inputs that would overflow a UV, |
1193 | or have leading zeros. Thus a single C<0> is accepted, but not C<00> nor | |
1194 | C<01>, C<002>, I<etc>. | |
1195 | ||
1196 | Background: C<atoi> has severe problems with illegal inputs, it cannot be | |
d62b8c6a | 1197 | used for incremental parsing, and therefore should be avoided |
5d4a52b5 | 1198 | C<atoi> and C<strtol> are also affected by locale settings, which can also be |
d62b8c6a JH |
1199 | seen as a bug (global state controlled by user environment). |
1200 | ||
238217e5 JK |
1201 | =cut |
1202 | ||
6313e544 JH |
1203 | */ |
1204 | ||
22ff3130 HS |
1205 | bool |
1206 | Perl_grok_atoUV(const char *pv, UV *valptr, const char** endptr) | |
6313e544 JH |
1207 | { |
1208 | const char* s = pv; | |
1209 | const char** eptr; | |
1210 | const char* end2; /* Used in case endptr is NULL. */ | |
22ff3130 | 1211 | UV val = 0; /* The parsed value. */ |
6313e544 | 1212 | |
22ff3130 | 1213 | PERL_ARGS_ASSERT_GROK_ATOUV; |
6313e544 | 1214 | |
5d4a52b5 KW |
1215 | if (endptr) { |
1216 | eptr = endptr; | |
1217 | } | |
1218 | else { | |
1219 | end2 = s + strlen(s); | |
1220 | eptr = &end2; | |
1221 | } | |
1222 | ||
1223 | if ( *eptr <= s | |
1224 | || ! isDIGIT(*s)) | |
1225 | { | |
1226 | return FALSE; | |
1227 | } | |
1228 | ||
97d95d46 KW |
1229 | /* Single-digit inputs are quite common. */ |
1230 | val = *s++ - '0'; | |
1231 | if (s < *eptr && isDIGIT(*s)) { | |
1232 | /* Fail on extra leading zeros. */ | |
1233 | if (val == 0) | |
1234 | return FALSE; | |
1235 | while (s < *eptr && isDIGIT(*s)) { | |
1236 | /* This could be unrolled like in grok_number(), but | |
1237 | * the expected uses of this are not speed-needy, and | |
1238 | * unlikely to need full 64-bitness. */ | |
1239 | const U8 digit = *s++ - '0'; | |
1240 | if (val < uv_max_div_10 || | |
1241 | (val == uv_max_div_10 && digit <= uv_max_mod_10)) { | |
1242 | val = val * 10 + digit; | |
1243 | } else { | |
22ff3130 | 1244 | return FALSE; |
6313e544 JH |
1245 | } |
1246 | } | |
97d95d46 KW |
1247 | } |
1248 | ||
5d4a52b5 KW |
1249 | if (endptr == NULL) { |
1250 | if (*s) { | |
1251 | return FALSE; /* If endptr is NULL, no trailing non-digits allowed. */ | |
1252 | } | |
1253 | } | |
1254 | else { | |
1255 | *endptr = s; | |
75feedba | 1256 | } |
97d95d46 | 1257 | |
22ff3130 HS |
1258 | *valptr = val; |
1259 | return TRUE; | |
6313e544 JH |
1260 | } |
1261 | ||
ce6f496d | 1262 | #ifndef Perl_strtod |
4801ca72 | 1263 | STATIC NV |
98994639 HS |
1264 | S_mulexp10(NV value, I32 exponent) |
1265 | { | |
1266 | NV result = 1.0; | |
1267 | NV power = 10.0; | |
1268 | bool negative = 0; | |
1269 | I32 bit; | |
1270 | ||
1271 | if (exponent == 0) | |
1272 | return value; | |
659c4b96 DM |
1273 | if (value == 0) |
1274 | return (NV)0; | |
87032ba1 | 1275 | |
24866caa | 1276 | /* On OpenVMS VAX we by default use the D_FLOAT double format, |
67597c89 | 1277 | * and that format does not have *easy* capabilities [1] for |
19c1206d KW |
1278 | * overflowing doubles 'silently' as IEEE fp does. We also need |
1279 | * to support G_FLOAT on both VAX and Alpha, and though the exponent | |
1280 | * range is much larger than D_FLOAT it still doesn't do silent | |
1281 | * overflow. Therefore we need to detect early whether we would | |
1282 | * overflow (this is the behaviour of the native string-to-float | |
24866caa | 1283 | * conversion routines, and therefore of native applications, too). |
67597c89 | 1284 | * |
24866caa CB |
1285 | * [1] Trying to establish a condition handler to trap floating point |
1286 | * exceptions is not a good idea. */ | |
87032ba1 JH |
1287 | |
1288 | /* In UNICOS and in certain Cray models (such as T90) there is no | |
1289 | * IEEE fp, and no way at all from C to catch fp overflows gracefully. | |
1290 | * There is something you can do if you are willing to use some | |
1291 | * inline assembler: the instruction is called DFI-- but that will | |
1292 | * disable *all* floating point interrupts, a little bit too large | |
1293 | * a hammer. Therefore we need to catch potential overflows before | |
1294 | * it's too late. */ | |
353813d9 | 1295 | |
a7157111 | 1296 | #if ((defined(VMS) && !defined(_IEEE_FP)) || defined(_UNICOS) || defined(DOUBLE_IS_VAX_FLOAT)) && defined(NV_MAX_10_EXP) |
353813d9 | 1297 | STMT_START { |
c4420975 | 1298 | const NV exp_v = log10(value); |
353813d9 HS |
1299 | if (exponent >= NV_MAX_10_EXP || exponent + exp_v >= NV_MAX_10_EXP) |
1300 | return NV_MAX; | |
1301 | if (exponent < 0) { | |
1302 | if (-(exponent + exp_v) >= NV_MAX_10_EXP) | |
1303 | return 0.0; | |
1304 | while (-exponent >= NV_MAX_10_EXP) { | |
1305 | /* combination does not overflow, but 10^(-exponent) does */ | |
1306 | value /= 10; | |
1307 | ++exponent; | |
1308 | } | |
1309 | } | |
1310 | } STMT_END; | |
87032ba1 JH |
1311 | #endif |
1312 | ||
353813d9 HS |
1313 | if (exponent < 0) { |
1314 | negative = 1; | |
1315 | exponent = -exponent; | |
b27804d8 DM |
1316 | #ifdef NV_MAX_10_EXP |
1317 | /* for something like 1234 x 10^-309, the action of calculating | |
1318 | * the intermediate value 10^309 then returning 1234 / (10^309) | |
1319 | * will fail, since 10^309 becomes infinity. In this case try to | |
1320 | * refactor it as 123 / (10^308) etc. | |
1321 | */ | |
1322 | while (value && exponent > NV_MAX_10_EXP) { | |
1323 | exponent--; | |
1324 | value /= 10; | |
1325 | } | |
48853916 JH |
1326 | if (value == 0.0) |
1327 | return value; | |
b27804d8 | 1328 | #endif |
353813d9 | 1329 | } |
c62e754c JH |
1330 | #if defined(__osf__) |
1331 | /* Even with cc -ieee + ieee_set_fp_control(IEEE_TRAP_ENABLE_INV) | |
1332 | * Tru64 fp behavior on inf/nan is somewhat broken. Another way | |
1333 | * to do this would be ieee_set_fp_control(IEEE_TRAP_ENABLE_OVF) | |
1334 | * but that breaks another set of infnan.t tests. */ | |
1335 | # define FP_OVERFLOWS_TO_ZERO | |
1336 | #endif | |
98994639 HS |
1337 | for (bit = 1; exponent; bit <<= 1) { |
1338 | if (exponent & bit) { | |
1339 | exponent ^= bit; | |
1340 | result *= power; | |
c62e754c JH |
1341 | #ifdef FP_OVERFLOWS_TO_ZERO |
1342 | if (result == 0) | |
a7157111 | 1343 | # ifdef NV_INF |
c62e754c | 1344 | return value < 0 ? -NV_INF : NV_INF; |
a7157111 JH |
1345 | # else |
1346 | return value < 0 ? -FLT_MAX : FLT_MAX; | |
1347 | # endif | |
c62e754c | 1348 | #endif |
236f0012 | 1349 | /* Floating point exceptions are supposed to be turned off, |
19c1206d | 1350 | * but if we're obviously done, don't risk another iteration. |
236f0012 CB |
1351 | */ |
1352 | if (exponent == 0) break; | |
98994639 HS |
1353 | } |
1354 | power *= power; | |
1355 | } | |
1356 | return negative ? value / result : value * result; | |
1357 | } | |
ce6f496d | 1358 | #endif /* #ifndef Perl_strtod */ |
98994639 | 1359 | |
ce6f496d | 1360 | #ifdef Perl_strtod |
b93d1309 | 1361 | # define ATOF(s, x) my_atof2(s, &x) |
f7b64c80 | 1362 | #else |
b93d1309 | 1363 | # define ATOF(s, x) Perl_atof2(s, x) |
f7b64c80 | 1364 | #endif |
b93d1309 | 1365 | |
98994639 HS |
1366 | NV |
1367 | Perl_my_atof(pTHX_ const char* s) | |
1368 | { | |
f720c878 KW |
1369 | /* 's' must be NUL terminated */ |
1370 | ||
98994639 | 1371 | NV x = 0.0; |
9eda1ea6 KW |
1372 | |
1373 | PERL_ARGS_ASSERT_MY_ATOF; | |
1374 | ||
b93d1309 | 1375 | #if ! defined(USE_LOCALE_NUMERIC) |
9eda1ea6 | 1376 | |
b93d1309 | 1377 | ATOF(s, x); |
9eda1ea6 KW |
1378 | |
1379 | #else | |
7918f24d | 1380 | |
a2287a13 | 1381 | { |
67d796ae KW |
1382 | DECLARATION_FOR_LC_NUMERIC_MANIPULATION; |
1383 | STORE_LC_NUMERIC_SET_TO_NEEDED(); | |
fdf55d20 KW |
1384 | if (! (PL_numeric_radix_sv && IN_LC(LC_NUMERIC))) { |
1385 | ATOF(s,x); | |
1386 | } | |
1387 | else { | |
19c1206d | 1388 | |
e4850248 KW |
1389 | /* Look through the string for the first thing that looks like a |
1390 | * decimal point: either the value in the current locale or the | |
1391 | * standard fallback of '.'. The one which appears earliest in the | |
1392 | * input string is the one that we should have atof look for. Note | |
1393 | * that we have to determine this beforehand because on some | |
1394 | * systems, Perl_atof2 is just a wrapper around the system's atof. | |
1395 | * */ | |
1ae85f6c KW |
1396 | const char * const standard_pos = strchr(s, '.'); |
1397 | const char * const local_pos | |
1398 | = strstr(s, SvPV_nolen(PL_numeric_radix_sv)); | |
1399 | const bool use_standard_radix | |
1400 | = standard_pos && (!local_pos || standard_pos < local_pos); | |
78787052 | 1401 | |
665873e9 | 1402 | if (use_standard_radix) { |
e4850248 | 1403 | SET_NUMERIC_STANDARD(); |
665873e9 KW |
1404 | LOCK_LC_NUMERIC_STANDARD(); |
1405 | } | |
78787052 | 1406 | |
b93d1309 | 1407 | ATOF(s,x); |
78787052 | 1408 | |
665873e9 KW |
1409 | if (use_standard_radix) { |
1410 | UNLOCK_LC_NUMERIC_STANDARD(); | |
67d796ae | 1411 | SET_NUMERIC_UNDERLYING(); |
665873e9 | 1412 | } |
e4850248 | 1413 | } |
a2287a13 KW |
1414 | RESTORE_LC_NUMERIC(); |
1415 | } | |
9eda1ea6 | 1416 | |
98994639 | 1417 | #endif |
9eda1ea6 | 1418 | |
98994639 HS |
1419 | return x; |
1420 | } | |
1421 | ||
a7157111 | 1422 | #if defined(NV_INF) || defined(NV_NAN) |
3c81f0b3 DD |
1423 | |
1424 | #ifdef USING_MSVC6 | |
1425 | # pragma warning(push) | |
1426 | # pragma warning(disable:4756;disable:4056) | |
1427 | #endif | |
829757a4 | 1428 | static char* |
5563f457 | 1429 | S_my_atof_infnan(pTHX_ const char* s, bool negative, const char* send, NV* value) |
829757a4 JH |
1430 | { |
1431 | const char *p0 = negative ? s - 1 : s; | |
1432 | const char *p = p0; | |
7eff3d39 | 1433 | const int infnan = grok_infnan(&p, send); |
829757a4 JH |
1434 | if (infnan && p != p0) { |
1435 | /* If we can generate inf/nan directly, let's do so. */ | |
1436 | #ifdef NV_INF | |
1437 | if ((infnan & IS_NUMBER_INFINITY)) { | |
3823048b | 1438 | *value = (infnan & IS_NUMBER_NEG) ? -NV_INF: NV_INF; |
829757a4 JH |
1439 | return (char*)p; |
1440 | } | |
1441 | #endif | |
1442 | #ifdef NV_NAN | |
1443 | if ((infnan & IS_NUMBER_NAN)) { | |
3823048b | 1444 | *value = NV_NAN; |
829757a4 JH |
1445 | return (char*)p; |
1446 | } | |
1447 | #endif | |
1448 | #ifdef Perl_strtod | |
68611e6f | 1449 | /* If still here, we didn't have either NV_INF or NV_NAN, |
829757a4 JH |
1450 | * and can try falling back to native strtod/strtold. |
1451 | * | |
1452 | * The native interface might not recognize all the possible | |
1453 | * inf/nan strings Perl recognizes. What we can try | |
1454 | * is to try faking the input. We will try inf/-inf/nan | |
1455 | * as the most promising/portable input. */ | |
1456 | { | |
6d37e916 | 1457 | const char* fake = "silence compiler warning"; |
829757a4 JH |
1458 | char* endp; |
1459 | NV nv; | |
a7157111 | 1460 | #ifdef NV_INF |
829757a4 JH |
1461 | if ((infnan & IS_NUMBER_INFINITY)) { |
1462 | fake = ((infnan & IS_NUMBER_NEG)) ? "-inf" : "inf"; | |
1463 | } | |
a7157111 JH |
1464 | #endif |
1465 | #ifdef NV_NAN | |
1466 | if ((infnan & IS_NUMBER_NAN)) { | |
829757a4 JH |
1467 | fake = "nan"; |
1468 | } | |
a7157111 | 1469 | #endif |
6d37e916 | 1470 | assert(strNE(fake, "silence compiler warning")); |
9ec8aea5 | 1471 | nv = S_strtod(aTHX_ fake, &endp); |
829757a4 | 1472 | if (fake != endp) { |
a7157111 | 1473 | #ifdef NV_INF |
829757a4 | 1474 | if ((infnan & IS_NUMBER_INFINITY)) { |
a7157111 | 1475 | # ifdef Perl_isinf |
829757a4 JH |
1476 | if (Perl_isinf(nv)) |
1477 | *value = nv; | |
a7157111 | 1478 | # else |
829757a4 JH |
1479 | /* last resort, may generate SIGFPE */ |
1480 | *value = Perl_exp((NV)1e9); | |
1481 | if ((infnan & IS_NUMBER_NEG)) | |
1482 | *value = -*value; | |
a7157111 | 1483 | # endif |
829757a4 JH |
1484 | return (char*)p; /* p, not endp */ |
1485 | } | |
a7157111 JH |
1486 | #endif |
1487 | #ifdef NV_NAN | |
1488 | if ((infnan & IS_NUMBER_NAN)) { | |
1489 | # ifdef Perl_isnan | |
829757a4 JH |
1490 | if (Perl_isnan(nv)) |
1491 | *value = nv; | |
a7157111 | 1492 | # else |
829757a4 JH |
1493 | /* last resort, may generate SIGFPE */ |
1494 | *value = Perl_log((NV)-1.0); | |
a7157111 | 1495 | # endif |
829757a4 | 1496 | return (char*)p; /* p, not endp */ |
a7157111 | 1497 | #endif |
829757a4 JH |
1498 | } |
1499 | } | |
1500 | } | |
1501 | #endif /* #ifdef Perl_strtod */ | |
1502 | } | |
1503 | return NULL; | |
1504 | } | |
3c81f0b3 DD |
1505 | #ifdef USING_MSVC6 |
1506 | # pragma warning(pop) | |
1507 | #endif | |
829757a4 | 1508 | |
a7157111 JH |
1509 | #endif /* if defined(NV_INF) || defined(NV_NAN) */ |
1510 | ||
98994639 HS |
1511 | char* |
1512 | Perl_my_atof2(pTHX_ const char* orig, NV* value) | |
1513 | { | |
6928bedc KW |
1514 | PERL_ARGS_ASSERT_MY_ATOF2; |
1515 | return my_atof3(orig, value, 0); | |
1516 | } | |
1517 | ||
1518 | char* | |
16411967 | 1519 | Perl_my_atof3(pTHX_ const char* orig, NV* value, const STRLEN len) |
6928bedc | 1520 | { |
e1ec3a88 | 1521 | const char* s = orig; |
a4eca1d4 | 1522 | NV result[3] = {0.0, 0.0, 0.0}; |
ce6f496d | 1523 | #if defined(USE_PERL_ATOF) || defined(Perl_strtod) |
6928bedc KW |
1524 | const char* send = s + ((len != 0) |
1525 | ? len | |
1526 | : strlen(orig)); /* one past the last */ | |
a4eca1d4 JH |
1527 | bool negative = 0; |
1528 | #endif | |
ce6f496d | 1529 | #if defined(USE_PERL_ATOF) && !defined(Perl_strtod) |
a4eca1d4 | 1530 | UV accumulator[2] = {0,0}; /* before/after dp */ |
8194bf88 | 1531 | bool seen_digit = 0; |
20f6aaab AS |
1532 | I32 exp_adjust[2] = {0,0}; |
1533 | I32 exp_acc[2] = {-1, -1}; | |
1534 | /* the current exponent adjust for the accumulators */ | |
98994639 | 1535 | I32 exponent = 0; |
8194bf88 | 1536 | I32 seen_dp = 0; |
20f6aaab AS |
1537 | I32 digit = 0; |
1538 | I32 old_digit = 0; | |
8194bf88 | 1539 | I32 sig_digits = 0; /* noof significant digits seen so far */ |
a4eca1d4 | 1540 | #endif |
8194bf88 | 1541 | |
ce6f496d | 1542 | #if defined(USE_PERL_ATOF) || defined(Perl_strtod) |
6928bedc | 1543 | PERL_ARGS_ASSERT_MY_ATOF3; |
7918f24d | 1544 | |
a4eca1d4 | 1545 | /* leading whitespace */ |
6928bedc | 1546 | while (s < send && isSPACE(*s)) |
a4eca1d4 JH |
1547 | ++s; |
1548 | ||
1549 | /* sign */ | |
1550 | switch (*s) { | |
1551 | case '-': | |
1552 | negative = 1; | |
1553 | /* FALLTHROUGH */ | |
1554 | case '+': | |
1555 | ++s; | |
1556 | } | |
1557 | #endif | |
1558 | ||
ce6f496d | 1559 | #ifdef Perl_strtod |
a4eca1d4 JH |
1560 | { |
1561 | char* endp; | |
d94e901a KW |
1562 | char* copy = NULL; |
1563 | ||
adc55e02 | 1564 | if ((endp = S_my_atof_infnan(aTHX_ s, negative, send, value))) |
a4eca1d4 | 1565 | return endp; |
d94e901a | 1566 | |
14d26b44 TC |
1567 | /* strtold() accepts 0x-prefixed hex and in POSIX implementations, |
1568 | 0b-prefixed binary numbers, which is backward incompatible | |
1569 | */ | |
1570 | if ((len == 0 || len >= 2) && *s == '0' && | |
1571 | (isALPHA_FOLD_EQ(s[1], 'x') || isALPHA_FOLD_EQ(s[1], 'b'))) { | |
1572 | *value = 0; | |
1573 | return (char *)s+1; | |
1574 | } | |
1575 | ||
d94e901a KW |
1576 | /* If the length is passed in, the input string isn't NUL-terminated, |
1577 | * and in it turns out the function below assumes it is; therefore we | |
1578 | * create a copy and NUL-terminate that */ | |
1579 | if (len) { | |
1580 | Newx(copy, len + 1, char); | |
1581 | Copy(orig, copy, len, char); | |
1582 | copy[len] = '\0'; | |
1583 | s = copy + (s - orig); | |
1584 | } | |
1585 | ||
9ec8aea5 | 1586 | result[2] = S_strtod(aTHX_ s, &endp); |
d94e901a KW |
1587 | |
1588 | /* If we created a copy, 'endp' is in terms of that. Convert back to | |
1589 | * the original */ | |
1590 | if (copy) { | |
aac39b03 | 1591 | s = (s - copy) + (char *) orig; |
d94e901a KW |
1592 | endp = (endp - copy) + (char *) orig; |
1593 | Safefree(copy); | |
1594 | } | |
1595 | ||
a4eca1d4 JH |
1596 | if (s != endp) { |
1597 | *value = negative ? -result[2] : result[2]; | |
1598 | return endp; | |
1599 | } | |
1600 | return NULL; | |
1601 | } | |
1602 | #elif defined(USE_PERL_ATOF) | |
1603 | ||
8194bf88 DM |
1604 | /* There is no point in processing more significant digits |
1605 | * than the NV can hold. Note that NV_DIG is a lower-bound value, | |
1606 | * while we need an upper-bound value. We add 2 to account for this; | |
1607 | * since it will have been conservative on both the first and last digit. | |
1608 | * For example a 32-bit mantissa with an exponent of 4 would have | |
1609 | * exact values in the set | |
1610 | * 4 | |
1611 | * 8 | |
1612 | * .. | |
1613 | * 17179869172 | |
1614 | * 17179869176 | |
1615 | * 17179869180 | |
1616 | * | |
1617 | * where for the purposes of calculating NV_DIG we would have to discount | |
1618 | * both the first and last digit, since neither can hold all values from | |
1619 | * 0..9; but for calculating the value we must examine those two digits. | |
1620 | */ | |
ffa277e5 AS |
1621 | #ifdef MAX_SIG_DIG_PLUS |
1622 | /* It is not necessarily the case that adding 2 to NV_DIG gets all the | |
1623 | possible digits in a NV, especially if NVs are not IEEE compliant | |
1624 | (e.g., long doubles on IRIX) - Allen <allens@cpan.org> */ | |
1625 | # define MAX_SIG_DIGITS (NV_DIG+MAX_SIG_DIG_PLUS) | |
1626 | #else | |
1627 | # define MAX_SIG_DIGITS (NV_DIG+2) | |
1628 | #endif | |
8194bf88 DM |
1629 | |
1630 | /* the max number we can accumulate in a UV, and still safely do 10*N+9 */ | |
1631 | #define MAX_ACCUMULATE ( (UV) ((UV_MAX - 9)/10)) | |
98994639 | 1632 | |
a5dc2484 | 1633 | #if defined(NV_INF) || defined(NV_NAN) |
ae776a2c | 1634 | { |
7eff3d39 | 1635 | char* endp; |
5563f457 | 1636 | if ((endp = S_my_atof_infnan(aTHX_ s, negative, send, value))) |
7eff3d39 | 1637 | return endp; |
ae776a2c | 1638 | } |
a5dc2484 | 1639 | #endif |
2b54f59f | 1640 | |
8194bf88 DM |
1641 | /* we accumulate digits into an integer; when this becomes too |
1642 | * large, we add the total to NV and start again */ | |
98994639 | 1643 | |
6928bedc | 1644 | while (s < send) { |
8194bf88 DM |
1645 | if (isDIGIT(*s)) { |
1646 | seen_digit = 1; | |
20f6aaab | 1647 | old_digit = digit; |
8194bf88 | 1648 | digit = *s++ - '0'; |
20f6aaab AS |
1649 | if (seen_dp) |
1650 | exp_adjust[1]++; | |
98994639 | 1651 | |
8194bf88 DM |
1652 | /* don't start counting until we see the first significant |
1653 | * digit, eg the 5 in 0.00005... */ | |
1654 | if (!sig_digits && digit == 0) | |
1655 | continue; | |
1656 | ||
1657 | if (++sig_digits > MAX_SIG_DIGITS) { | |
98994639 | 1658 | /* limits of precision reached */ |
20f6aaab AS |
1659 | if (digit > 5) { |
1660 | ++accumulator[seen_dp]; | |
1661 | } else if (digit == 5) { | |
1662 | if (old_digit % 2) { /* round to even - Allen */ | |
1663 | ++accumulator[seen_dp]; | |
1664 | } | |
1665 | } | |
1666 | if (seen_dp) { | |
1667 | exp_adjust[1]--; | |
1668 | } else { | |
1669 | exp_adjust[0]++; | |
1670 | } | |
8194bf88 | 1671 | /* skip remaining digits */ |
6928bedc | 1672 | while (s < send && isDIGIT(*s)) { |
98994639 | 1673 | ++s; |
20f6aaab AS |
1674 | if (! seen_dp) { |
1675 | exp_adjust[0]++; | |
1676 | } | |
98994639 HS |
1677 | } |
1678 | /* warn of loss of precision? */ | |
98994639 | 1679 | } |
8194bf88 | 1680 | else { |
20f6aaab | 1681 | if (accumulator[seen_dp] > MAX_ACCUMULATE) { |
8194bf88 | 1682 | /* add accumulator to result and start again */ |
20f6aaab AS |
1683 | result[seen_dp] = S_mulexp10(result[seen_dp], |
1684 | exp_acc[seen_dp]) | |
1685 | + (NV)accumulator[seen_dp]; | |
1686 | accumulator[seen_dp] = 0; | |
1687 | exp_acc[seen_dp] = 0; | |
98994639 | 1688 | } |
20f6aaab AS |
1689 | accumulator[seen_dp] = accumulator[seen_dp] * 10 + digit; |
1690 | ++exp_acc[seen_dp]; | |
98994639 | 1691 | } |
8194bf88 | 1692 | } |
e1ec3a88 | 1693 | else if (!seen_dp && GROK_NUMERIC_RADIX(&s, send)) { |
8194bf88 | 1694 | seen_dp = 1; |
20f6aaab | 1695 | if (sig_digits > MAX_SIG_DIGITS) { |
6928bedc | 1696 | while (s < send && isDIGIT(*s)) { |
20f6aaab | 1697 | ++s; |
9604fbf0 | 1698 | } |
20f6aaab AS |
1699 | break; |
1700 | } | |
8194bf88 DM |
1701 | } |
1702 | else { | |
1703 | break; | |
98994639 HS |
1704 | } |
1705 | } | |
1706 | ||
20f6aaab AS |
1707 | result[0] = S_mulexp10(result[0], exp_acc[0]) + (NV)accumulator[0]; |
1708 | if (seen_dp) { | |
1709 | result[1] = S_mulexp10(result[1], exp_acc[1]) + (NV)accumulator[1]; | |
1710 | } | |
98994639 | 1711 | |
6928bedc | 1712 | if (s < send && seen_digit && (isALPHA_FOLD_EQ(*s, 'e'))) { |
98994639 HS |
1713 | bool expnegative = 0; |
1714 | ||
1715 | ++s; | |
1716 | switch (*s) { | |
1717 | case '-': | |
1718 | expnegative = 1; | |
924ba076 | 1719 | /* FALLTHROUGH */ |
98994639 HS |
1720 | case '+': |
1721 | ++s; | |
1722 | } | |
6928bedc | 1723 | while (s < send && isDIGIT(*s)) |
98994639 HS |
1724 | exponent = exponent * 10 + (*s++ - '0'); |
1725 | if (expnegative) | |
1726 | exponent = -exponent; | |
1727 | } | |
1728 | ||
1729 | /* now apply the exponent */ | |
20f6aaab AS |
1730 | |
1731 | if (seen_dp) { | |
1732 | result[2] = S_mulexp10(result[0],exponent+exp_adjust[0]) | |
1733 | + S_mulexp10(result[1],exponent-exp_adjust[1]); | |
1734 | } else { | |
1735 | result[2] = S_mulexp10(result[0],exponent+exp_adjust[0]); | |
1736 | } | |
98994639 HS |
1737 | |
1738 | /* now apply the sign */ | |
1739 | if (negative) | |
20f6aaab | 1740 | result[2] = -result[2]; |
a36244b7 | 1741 | #endif /* USE_PERL_ATOF */ |
20f6aaab | 1742 | *value = result[2]; |
73d840c0 | 1743 | return (char *)s; |
98994639 HS |
1744 | } |
1745 | ||
5d34af89 | 1746 | /* |
3d9d9213 | 1747 | =for apidoc isinfnan |
5d34af89 | 1748 | |
796b6530 KW |
1749 | C<Perl_isinfnan()> is utility function that returns true if the NV |
1750 | argument is either an infinity or a C<NaN>, false otherwise. To test | |
1751 | in more detail, use C<Perl_isinf()> and C<Perl_isnan()>. | |
5d34af89 | 1752 | |
68611e6f JH |
1753 | This is also the logical inverse of Perl_isfinite(). |
1754 | ||
5d34af89 JH |
1755 | =cut |
1756 | */ | |
1cd88304 JH |
1757 | bool |
1758 | Perl_isinfnan(NV nv) | |
1759 | { | |
a5dc2484 | 1760 | PERL_UNUSED_ARG(nv); |
1cd88304 JH |
1761 | #ifdef Perl_isinf |
1762 | if (Perl_isinf(nv)) | |
1763 | return TRUE; | |
1764 | #endif | |
1765 | #ifdef Perl_isnan | |
1766 | if (Perl_isnan(nv)) | |
1767 | return TRUE; | |
1768 | #endif | |
1769 | return FALSE; | |
1770 | } | |
1771 | ||
354b74ae FC |
1772 | /* |
1773 | =for apidoc | |
1774 | ||
796b6530 | 1775 | Checks whether the argument would be either an infinity or C<NaN> when used |
354b74ae | 1776 | as a number, but is careful not to trigger non-numeric or uninitialized |
796b6530 | 1777 | warnings. it assumes the caller has done C<SvGETMAGIC(sv)> already. |
354b74ae FC |
1778 | |
1779 | =cut | |
1780 | */ | |
1781 | ||
1782 | bool | |
1783 | Perl_isinfnansv(pTHX_ SV *sv) | |
1784 | { | |
1785 | PERL_ARGS_ASSERT_ISINFNANSV; | |
1786 | if (!SvOK(sv)) | |
1787 | return FALSE; | |
1788 | if (SvNOKp(sv)) | |
1789 | return Perl_isinfnan(SvNVX(sv)); | |
1790 | if (SvIOKp(sv)) | |
1791 | return FALSE; | |
1792 | { | |
1793 | STRLEN len; | |
1794 | const char *s = SvPV_nomg_const(sv, len); | |
3823048b | 1795 | return cBOOL(grok_infnan(&s, s+len)); |
354b74ae FC |
1796 | } |
1797 | } | |
1798 | ||
d67dac15 | 1799 | #ifndef HAS_MODFL |
68611e6f JH |
1800 | /* C99 has truncl, pre-C99 Solaris had aintl. We can use either with |
1801 | * copysignl to emulate modfl, which is in some platforms missing or | |
1802 | * broken. */ | |
d67dac15 JH |
1803 | # if defined(HAS_TRUNCL) && defined(HAS_COPYSIGNL) |
1804 | long double | |
1805 | Perl_my_modfl(long double x, long double *ip) | |
1806 | { | |
68611e6f JH |
1807 | *ip = truncl(x); |
1808 | return (x == *ip ? copysignl(0.0L, x) : x - *ip); | |
d67dac15 JH |
1809 | } |
1810 | # elif defined(HAS_AINTL) && defined(HAS_COPYSIGNL) | |
55954f19 JH |
1811 | long double |
1812 | Perl_my_modfl(long double x, long double *ip) | |
1813 | { | |
68611e6f JH |
1814 | *ip = aintl(x); |
1815 | return (x == *ip ? copysignl(0.0L, x) : x - *ip); | |
55954f19 | 1816 | } |
d67dac15 | 1817 | # endif |
55954f19 JH |
1818 | #endif |
1819 | ||
7b9b7dff | 1820 | /* Similarly, with ilogbl and scalbnl we can emulate frexpl. */ |
55954f19 JH |
1821 | #if ! defined(HAS_FREXPL) && defined(HAS_ILOGBL) && defined(HAS_SCALBNL) |
1822 | long double | |
1823 | Perl_my_frexpl(long double x, int *e) { | |
68611e6f JH |
1824 | *e = x == 0.0L ? 0 : ilogbl(x) + 1; |
1825 | return (scalbnl(x, -*e)); | |
55954f19 JH |
1826 | } |
1827 | #endif | |
66610fdd RGS |
1828 | |
1829 | /* | |
ed140128 AD |
1830 | =for apidoc Perl_signbit |
1831 | ||
1832 | Return a non-zero integer if the sign bit on an NV is set, and 0 if | |
19c1206d | 1833 | it is not. |
ed140128 | 1834 | |
796b6530 KW |
1835 | If F<Configure> detects this system has a C<signbit()> that will work with |
1836 | our NVs, then we just use it via the C<#define> in F<perl.h>. Otherwise, | |
8b7fad81 | 1837 | fall back on this implementation. The main use of this function |
796b6530 | 1838 | is catching C<-0.0>. |
ed140128 | 1839 | |
796b6530 KW |
1840 | C<Configure> notes: This function is called C<'Perl_signbit'> instead of a |
1841 | plain C<'signbit'> because it is easy to imagine a system having a C<signbit()> | |
ed140128 | 1842 | function or macro that doesn't happen to work with our particular choice |
796b6530 | 1843 | of NVs. We shouldn't just re-C<#define> C<signbit> as C<Perl_signbit> and expect |
ed140128 | 1844 | the standard system headers to be happy. Also, this is a no-context |
796b6530 KW |
1845 | function (no C<pTHX_>) because C<Perl_signbit()> is usually re-C<#defined> in |
1846 | F<perl.h> as a simple macro call to the system's C<signbit()>. | |
1847 | Users should just always call C<Perl_signbit()>. | |
ed140128 AD |
1848 | |
1849 | =cut | |
1850 | */ | |
1851 | #if !defined(HAS_SIGNBIT) | |
1852 | int | |
1853 | Perl_signbit(NV x) { | |
8b7fad81 | 1854 | # ifdef Perl_fp_class_nzero |
406d5545 JH |
1855 | return Perl_fp_class_nzero(x); |
1856 | /* Try finding the high byte, and assume it's highest bit | |
1857 | * is the sign. This assumption is probably wrong somewhere. */ | |
572cd850 JH |
1858 | # elif defined(USE_LONG_DOUBLE) && LONG_DOUBLEKIND == LONG_DOUBLE_IS_X86_80_BIT_LITTLE_ENDIAN |
1859 | return (((unsigned char *)&x)[9] & 0x80); | |
1860 | # elif defined(NV_LITTLE_ENDIAN) | |
1861 | /* Note that NVSIZE is sizeof(NV), which would make the below be | |
1862 | * wrong if the end bytes are unused, which happens with the x86 | |
1863 | * 80-bit long doubles, which is why take care of that above. */ | |
1864 | return (((unsigned char *)&x)[NVSIZE - 1] & 0x80); | |
1865 | # elif defined(NV_BIG_ENDIAN) | |
1866 | return (((unsigned char *)&x)[0] & 0x80); | |
1867 | # else | |
406d5545 | 1868 | /* This last resort fallback is wrong for the negative zero. */ |
3585840c | 1869 | return (x < 0.0) ? 1 : 0; |
572cd850 | 1870 | # endif |
ed140128 AD |
1871 | } |
1872 | #endif | |
1873 | ||
1874 | /* | |
14d04a33 | 1875 | * ex: set ts=8 sts=4 sw=4 et: |
37442d52 | 1876 | */ |