Skip to content

Commit aede507

Browse files
hhugovouillon
authored andcommitted
Runtime: make float_of_string strict
1 parent 7a1d85c commit aede507

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

runtime/ieee_754.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -567,13 +567,16 @@ function caml_format_float(fmt, x) {
567567
//Requires: caml_failwith, caml_jsbytes_of_string
568568
function caml_float_of_string(s) {
569569
var res;
570+
var r_float = /^ *[-+]?(?:\d*\.?\d+|\d+\.?\d*)(?:[eE][-+]?\d+)?$/;
570571
s = caml_jsbytes_of_string(s);
571572
res = +s;
572-
if (s.length > 0 && res === res) return res;
573+
//Fast path
574+
if (!Number.isNaN(res) && r_float.test(s)) return res;
573575
s = s.replace(/_/g, "");
574576
res = +s;
575-
if ((s.length > 0 && res === res) || /^[+-]?nan$/i.test(s)) return res;
576-
var m = /^ *([+-]?)0x([0-9a-f]+)\.?([0-9a-f]*)(p([+-]?[0-9]+))?/i.exec(s);
577+
if ((!Number.isNaN(res) && r_float.test(s)) || /^[+-]?nan$/i.test(s))
578+
return res;
579+
var m = /^ *([+-]?)0x([0-9a-f]+)\.?([0-9a-f]*)(p([+-]?[0-9]+))?$/i.exec(s);
577580
// 1 2 3 5
578581
if (m) {
579582
var m3 = m[3].replace(/0+$/, "");

0 commit comments

Comments
 (0)