Skip to content

Commit 4dbdb94

Browse files
committed
[Bug #20697] Parse r suffix at Rational
1 parent 57583cb commit 4dbdb94

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

rational.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2304,6 +2304,12 @@ islettere(int c)
23042304
return (c == 'e' || c == 'E');
23052305
}
23062306

2307+
inline static int
2308+
isletterr(int c)
2309+
{
2310+
return (c == 'r' || c == 'R');
2311+
}
2312+
23072313
static VALUE
23082314
negate_num(VALUE num)
23092315
{
@@ -2353,7 +2359,13 @@ read_num(const char **s, const char *const end, VALUE *num, VALUE *nexp)
23532359
ok = 1;
23542360
}
23552361

2356-
if (ok && *s + 1 < end && islettere(**s)) {
2362+
if (!ok || *s >= end) {
2363+
/* failed or finish */
2364+
}
2365+
else if (isletterr(**s)) {
2366+
(*s)++;
2367+
}
2368+
else if (*s + 1 < end && islettere(**s)) {
23572369
(*s)++;
23582370
expsign = read_sign(s, end);
23592371
exp = rb_int_parse_cstr(*s, end-*s, &e, NULL,

test/ruby/test_rational.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ def test_conv_string
130130
assert_equal(Rational(111, 10), Rational('1.11e1'))
131131
assert_equal(Rational(111, 100), Rational('1.11e0'))
132132
assert_equal(Rational(111, 1000), Rational('1.11e-1'))
133+
assert_equal(Rational(5, 4), Rational('3.0r','2.4R'))
133134
end
134135

135136
def test_conv_error
@@ -842,6 +843,10 @@ def test_parse
842843
ng[5, 3, '5/3x']
843844

844845
ng[5, 1, '5/-3']
846+
847+
ok[30, 24, '3.0r/2.4R']
848+
ng[30, 24, '3.0r/2.4re1']
849+
ng[30, 240, '3.0r/2.4e1r']
845850
end
846851

847852
def test_parse_zero_denominator

0 commit comments

Comments
 (0)