[MacRuby] #1012: Inconsistent Regex behaviour

MacRuby ruby-noreply at macosforge.org
Mon Nov 29 15:54:07 PST 2010


#1012: Inconsistent Regex behaviour
-------------------------------------------+--------------------------------
 Reporter:  harry@…                        |       Owner:  lsansonetti@…        
     Type:  defect                         |      Status:  new                  
 Priority:  minor                          |   Milestone:                       
Component:  MacRuby                        |    Keywords:                       
-------------------------------------------+--------------------------------

Comment(by watson1978@…):

 It seems to have to use uregex_findNext() to support \G.

 {{{
 #!diff
 diff --git a/re.c b/re.c
 index afbed36..49cbdb8 100644
 --- a/re.c
 +++ b/re.c
 @@ -785,10 +785,13 @@ rb_reg_matcher_search(VALUE re, VALUE matcher, int
 pos, bool reverse)
             return -1;
         }
      }
 -    else if (!uregex_find(re_matcher->pattern, pos, &status)) {
 -       // No match.
 -       rb_backref_set(Qnil);
 -       return -1;
 +    else {
 +       uregex_setRegion(re_matcher->pattern, pos, chars_len, &status);
 +       if (!uregex_findNext(re_matcher->pattern, &status)) {
 +           // No match.
 +           rb_backref_set(Qnil);
 +           return -1;
 +       }
      }

      // Match found.
 }}}

 Test Script:
 {{{
 require 'tempfile'
 require 'test/unit/assertions.rb'
 include Test::Unit::Assertions

 str = "0123456789"
 assert_equal(["01", "23", "45", "67", "89"], str.scan(/\G\d\d/))
 assert_equal("x23456789", str.sub(/\G\d\d/, "x"))
 assert_equal("xxxxx", str.gsub(/\G\d\d/, "x"))
 assert_equal(2, str.index(/\G\d\d/, 2))

 # not yet
 #assert_equal(2, str.rindex(/\G\d\d/, 2))

 puts :ok
 }}}

 I think that many methods can support \G by an above change.
 but does not supported yet with String#rindex and String#rpartition.

-- 
Ticket URL: <http://www.macruby.org/trac/ticket/1012#comment:3>
MacRuby <http://macruby.org/>



More information about the macruby-tickets mailing list