[bug] cases where regexp should raise

S

Simon Strandgaard

While working on my own regexp engine, I found out that
there is many cases where a range doesn't raise an exception.

Shouldn't Ruby's Regexp class raise an exception here ???


def test_illegal_range1
input = [
"a{3", # premature end of input
"a{,}b", # neither min or max specified
"a{,3}b", # min expected, but got none
"a{43x3}b", # either ',' or '}' expected, got 'x'
"a{43,333", # premature end of input
"a{43,333x", # expected '}', got 'x'
"a{999,666}" # expected (min <= max), got (min > max)
]
res = input.map{|str|
ok = false
begin
Regexp.new(str)
rescue RegexpError
ok = true
end
ok
}
assert_equal([true] * input.size, res)
end

Output when running via Ruby-1.8.1

1) Failure:
test_illegal_range1(TestRubyRegex) [test_ruby_regex.rb:135]:
<[true, true, true, true, true, true, true]> expected but was
<[false, false, false, false, false, false, true]>.

As you can see only the last case is OK, the others are broken.
 
S

Simon Strandgaard

S> Shouldn't Ruby's Regexp class raise an exception here ???

No, why ?

It seems very strange that the Regexp class let itself initialize
with a string which cannot match anything (In my opinion they are illegal
regexp constructions).

For instance Ruby raises exceptions if one tries to initialize with
either of these strings:
'a\\',
")a",
"a(",
"*a",
"+a",
"?a",
"|*",
"x?+",
"x???+",
"x?++",
"x?+?+",

Ruby raises exceptions to almost any illegal string..
except if the string contains an illegal range.

It seems inconsistent to me that Ruby doesn't raise in when encountering
an illegal range ?

BTW: do you agree with me that these ranges really are illegal ?
 
T

ts

S> It seems very strange that the Regexp class let itself initialize
S> with a string which cannot match anything (In my opinion they are illegal
S> regexp constructions).

Why it cannot match anything ?

svg% cat b.rb
#!/usr/bin/ruby
["a{3", "a{,}b", "a{,3}b", "a{43x3}b", "a{43,333", "a{43,333x"].each do |x|
p Regexp.new(x) =~ x
end
svg%

svg% b.rb
0
0
0
0
0
0
svg%

it work just fine
 
S

Simon Strandgaard

S> It seems very strange that the Regexp class let itself initialize
S> with a string which cannot match anything (In my opinion they are illegal
S> regexp constructions).

Why it cannot match anything ?

svg% cat b.rb
#!/usr/bin/ruby
["a{3", "a{,}b", "a{,3}b", "a{43x3}b", "a{43,333", "a{43,333x"].each do |x|
p Regexp.new(x) =~ x
end
svg%

svg% b.rb
0
0
0
0
0
0
svg%

it work just fine

Admitted this is embarrassing for me. Silly me :)
Thanks Guy, this is educational.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top