[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[bug] cases where regexp should raise

Simon Strandgaard

10/31/2003 8:00:00 PM

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.

--
Simon Strandgaard
4 Answers

ts

11/1/2003 1:01:00 PM

0

>>>>> "S" == Simon Strandgaard <qj5nd7l02@sneakemail.com> writes:

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

No, why ?

--

Guy Decoux

Simon Strandgaard

11/1/2003 3:32:00 PM

0

On Sat, 01 Nov 2003 14:01:29 +0100, ts wrote:

>>>>>> "S" == Simon Strandgaard <qj5nd7l02@sneakemail.com> writes:
>
> 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 ?

--
Simon Strandgaard

ts

11/1/2003 4:22:00 PM

0

>>>>> "S" == Simon Strandgaard <qj5nd7l02@sneakemail.com> writes:

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


--

Guy Decoux

Simon Strandgaard

11/1/2003 4:42:00 PM

0

On Sat, 01 Nov 2003 17:21:39 +0100, ts wrote:

>>>>>> "S" == Simon Strandgaard <qj5nd7l02@sneakemail.com> writes:
>
> 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.

--
Simon Strandgaard