Skip to content

Commit d17edf3

Browse files
committed
[Bug #20705] Update strtod implementation
The absence of either the integer or fractional part should be allowed.
1 parent f37e6d7 commit d17edf3

3 files changed

Lines changed: 20 additions & 26 deletions

File tree

‎missing/dtoa.c‎

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,7 +1552,7 @@ strtod(const char *s00, char **se)
15521552
aadj = 1.0;
15531553
nd0 = -4;
15541554

1555-
if (!*++s || !(s1 = strchr(hexdigit, *s))) goto ret0;
1555+
if (!*++s || (!(s1 = strchr(hexdigit, *s)) && *s != '.')) goto ret0;
15561556
if (*s == '0') {
15571557
while (*++s == '0');
15581558
if (!*s) goto ret;
@@ -1566,9 +1566,7 @@ strtod(const char *s00, char **se)
15661566
} while (*++s && (s1 = strchr(hexdigit, *s)));
15671567
}
15681568

1569-
if (*s == '.') {
1570-
dsign = 1;
1571-
if (!*++s || !(s1 = strchr(hexdigit, *s))) goto ret0;
1569+
if ((*s == '.') && *++s && (s1 = strchr(hexdigit, *s))) {
15721570
if (nd0 < 0) {
15731571
while (*s == '0') {
15741572
s++;
@@ -1583,9 +1581,6 @@ strtod(const char *s00, char **se)
15831581
}
15841582
}
15851583
}
1586-
else {
1587-
dsign = 0;
1588-
}
15891584

15901585
if (*s == 'P' || *s == 'p') {
15911586
dsign = 0x2C - *++s; /* +: 2B, -: 2D */
@@ -1608,9 +1603,6 @@ strtod(const char *s00, char **se)
16081603
} while ('0' <= c && c <= '9');
16091604
nd0 += nd * dsign;
16101605
}
1611-
else {
1612-
if (dsign) goto ret0;
1613-
}
16141606
dval(rv) = ldexp(adj, nd0);
16151607
goto ret;
16161608
}
@@ -1647,9 +1639,9 @@ strtod(const char *s00, char **se)
16471639
}
16481640
#endif
16491641
if (c == '.') {
1650-
if (!ISDIGIT(s[1]))
1651-
goto dig_done;
16521642
c = *++s;
1643+
if (!ISDIGIT(c))
1644+
goto dig_done;
16531645
if (!nd) {
16541646
for (; c == '0'; c = *++s)
16551647
nz++;

‎spec/ruby/core/string/modulo_spec.rb‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,9 @@ def obj.to_s() "obj" end
749749
(format % "-10.4e-20").should == (format % -10.4e-20)
750750
(format % ".5").should == (format % 0.5)
751751
(format % "-.5").should == (format % -0.5)
752+
ruby_bug("#20705", ""..."3.4") do
753+
(format % "10.").should == (format % 10)
754+
end
752755
# Something's strange with this spec:
753756
# it works just fine in individual mode, but not when run as part of a group
754757
(format % "10_1_0.5_5_5").should == (format % 1010.555)
@@ -758,7 +761,6 @@ def obj.to_s() "obj" end
758761
-> { format % "" }.should raise_error(ArgumentError)
759762
-> { format % "x" }.should raise_error(ArgumentError)
760763
-> { format % "." }.should raise_error(ArgumentError)
761-
-> { format % "10." }.should raise_error(ArgumentError)
762764
-> { format % "5x" }.should raise_error(ArgumentError)
763765
-> { format % "0b1" }.should raise_error(ArgumentError)
764766
-> { format % "10e10.5" }.should raise_error(ArgumentError)

‎test/ruby/test_float.rb‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -129,22 +129,22 @@ def test_strtod
129129
assert_in_delta(a, 0, Float::EPSILON)
130130
a = Float("-.0")
131131
assert_in_delta(a, 0, Float::EPSILON)
132-
assert_raise(ArgumentError){Float("0.")}
133-
assert_raise(ArgumentError){Float("+0.")}
134-
assert_raise(ArgumentError){Float("-0.")}
132+
assert_equal(0.0, Float("0."))
133+
assert_equal(0.0, Float("+0."))
134+
assert_equal(0.0, Float("-0."))
135135
assert_raise(ArgumentError){Float(".")}
136136
assert_raise(ArgumentError){Float("+")}
137137
assert_raise(ArgumentError){Float("+.")}
138138
assert_raise(ArgumentError){Float("-")}
139139
assert_raise(ArgumentError){Float("-.")}
140140
assert_raise(ArgumentError){Float("1e")}
141141
assert_raise(ArgumentError){Float("1__1")}
142-
assert_raise(ArgumentError){Float("1.")}
143-
assert_raise(ArgumentError){Float("1.e+00")}
144-
assert_raise(ArgumentError){Float("0x.1")}
145-
assert_raise(ArgumentError){Float("0x1.")}
146-
assert_raise(ArgumentError){Float("0x1.0")}
147-
assert_raise(ArgumentError){Float("0x1.p+0")}
142+
assert_equal(1.0, Float("1."))
143+
assert_equal(1.0, Float("1.e+00"))
144+
assert_equal(0.0625, Float("0x.1"))
145+
assert_equal(1.0, Float("0x1."))
146+
assert_equal(1.0, Float("0x1.0"))
147+
assert_equal(1.0, Float("0x1.p+0"))
148148
# add expected behaviour here.
149149
assert_equal(10, Float("1_0"))
150150

@@ -191,7 +191,7 @@ def test_strtod
191191
break
192192
end
193193
end
194-
assert_nil(x, ->{"%a" % x})
194+
assert_equal(1.0, x, ->{"%a" % x})
195195
end
196196

197197
def test_divmod
@@ -833,10 +833,10 @@ def test_Float
833833
assert_equal(15, Float('0xf'))
834834
assert_equal(15, Float('0xfp0'))
835835
assert_raise(ArgumentError) { Float('0xfp') }
836-
assert_raise(ArgumentError) { Float('0xf.') }
836+
assert_equal(15, Float('0xf.'))
837837
assert_raise(ArgumentError) { Float('0xf.p') }
838-
assert_raise(ArgumentError) { Float('0xf.p0') }
839-
assert_raise(ArgumentError) { Float('0xf.f') }
838+
assert_equal(15, Float('0xf.p0'))
839+
assert_equal(15.9375, Float('0xf.f'))
840840
assert_raise(ArgumentError) { Float('0xf.fp') }
841841
begin
842842
verbose_bak, $VERBOSE = $VERBOSE, nil

0 commit comments

Comments
 (0)