Skip to content

Commit

Permalink
[Bug #19087] Disallow successive underscores in Complex string
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Dec 1, 2022
1 parent 914cf26 commit c0dc717
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
6 changes: 3 additions & 3 deletions complex.c
Original file line number Diff line number Diff line change
Expand Up @@ -1751,9 +1751,9 @@ read_digits(const char **s, int strict,

while (isdecimal(**s) || **s == '_') {
if (**s == '_') {
if (strict) {
if (us)
return 0;
if (us) {
if (strict) return 0;
break;
}
us = 1;
}
Expand Down
22 changes: 22 additions & 0 deletions test/ruby/test_complex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -853,20 +853,42 @@ def test_parse
assert_equal(Complex(0), '_5'.to_c)
assert_equal(Complex(5), '5_'.to_c)
assert_equal(Complex(5), '5x'.to_c)
assert_equal(Complex(51), '5_1'.to_c)
assert_equal(Complex(5), '5__1'.to_c)
assert_equal(Complex(5), '5+_3i'.to_c)
assert_equal(Complex(5), '5+3_i'.to_c)
assert_equal(Complex(5,3), '5+3i_'.to_c)
assert_equal(Complex(5,3), '5+3ix'.to_c)
assert_equal(Complex(5,31), '5+3_1i'.to_c)
assert_equal(Complex(5), '5+3__1i'.to_c)
assert_equal(Complex(51), Complex('5_1'))
assert_equal(Complex(5,31), Complex('5+3_1i'))
assert_equal(Complex(5,31), Complex('5+3_1I'))
assert_equal(Complex(5,31), Complex('5+3_1j'))
assert_equal(Complex(5,31), Complex('5+3_1J'))
assert_equal(Complex(0,31), Complex('3_1i'))
assert_equal(Complex(0,31), Complex('3_1I'))
assert_equal(Complex(0,31), Complex('3_1j'))
assert_equal(Complex(0,31), Complex('3_1J'))
assert_raise(ArgumentError){ Complex('')}
assert_raise(ArgumentError){ Complex('_')}
assert_raise(ArgumentError){ Complex("\f\n\r\t\v5\0")}
assert_raise(ArgumentError){ Complex('_5')}
assert_raise(ArgumentError){ Complex('5_')}
assert_raise(ArgumentError){ Complex('5__1')}
assert_raise(ArgumentError){ Complex('5x')}
assert_raise(ArgumentError){ Complex('5+_3i')}
assert_raise(ArgumentError){ Complex('5+3_i')}
assert_raise(ArgumentError){ Complex('5+3i_')}
assert_raise(ArgumentError){ Complex('5+3ix')}
assert_raise(ArgumentError){ Complex('5+3__1i')}
assert_raise(ArgumentError){ Complex('5+3__1I')}
assert_raise(ArgumentError){ Complex('5+3__1j')}
assert_raise(ArgumentError){ Complex('5+3__1J')}
assert_raise(ArgumentError){ Complex('3__1i')}
assert_raise(ArgumentError){ Complex('3__1I')}
assert_raise(ArgumentError){ Complex('3__1j')}
assert_raise(ArgumentError){ Complex('3__1J')}

assert_equal(Complex(Rational(1,5)), '1/5'.to_c)
assert_equal(Complex(Rational(-1,5)), '-1/5'.to_c)
Expand Down

0 comments on commit c0dc717

Please sign in to comment.