Projects

Ticket #749: regex2.rb

File regex2.rb, 1.0 KB (added by martinlagardette@…, 2 years ago)

Segfaulting

Line 
1require 'strscan'
2require "test/unit"
3
4class TestStringScanner < Test::Unit::TestCase
5  def test_lookahead
6    match = []
7    ss = StringScanner.new($str)
8
9    # while ss.skip_until(/class(?![^ \n])|def(?![^ \n])|end(?![^ \n])|if(?![^ \n])|then(?![^ \n])|else(?![^ \n])/)
10    # while ss.skip_until(/(class|def|end|if|then|else)(?![^ \n])/)
11    while ss.skip_until(/(class(?![^ \n])|def(?![^ \n]))/)
12      a = ss.pos - ss.matched_size
13      b = ss.matched_size
14
15      warn "[#{a}, #{b}] = \"#{ss.matched}\""
16      x = [a,b,ss.matched]
17      match << x
18    end
19
20    assert_equal(match[0], [0, 5, "class"])
21    assert_equal(match[1], [120, 3, "def"])
22  end
23 
24end
25
26$str = <<EOT
27class PrintProxy
28  # instance_methods.each {
29  #   |m| undef_method m unless m =~ /(^__|^send$|^object_id$)/
30  # }
31 
32  def initialize(obj, &block)
33    @obj = obj
34  end
35
36  protected
37
38  def method_missing(symbol, *args, &block)
39    print "foo"
40    if @block then print "foo\n" else print "\n" end
41   
42    @obj.send(symbol, *args, &block)
43  end
44end
45EOT