Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

* Modified begin, end and offset methods in MatchData to handle named…

… groups and default to group 0.

* Exception is not longer thrown when in oregexp_make_match_data.
  • Loading branch information...
commit 7c58f43e493d197eb419a86b5b1e512d58a798ac 1 parent 5e16348
@dichodaemon dichodaemon authored
Showing with 53 additions and 14 deletions.
  1. +1 −5 ext/oregexp.c
  2. +49 −9 lib/oniguruma.rb
  3. +3 −0  test/test_oniguruma.rb
View
6 ext/oregexp.c
@@ -117,9 +117,6 @@ static int name_callback(
for (i = 0; i < ngroup_num; i++) {
gn = group_nums[i];
- ref = onig_name_to_backref_number(reg, name, name_end, region);
- if (ref != gn )
- return 1;
rb_hash_aset( nameHash, ID2SYM(rb_intern(name)), INT2FIX( gn ) );
}
return 0;
@@ -181,8 +178,7 @@ static VALUE oregexp_make_match_data(ORegexp * oregexp, OnigRegion * region, VAL
rb_cv_set( kORegexp, "@@last_match", (VALUE)match );
packet.region = region;
packet.hash = rb_hash_new();
- if( onig_foreach_name(oregexp->reg, name_callback, &packet) )
- rb_raise(rb_eException, "Oniguruma Error: group and backreference names are different");
+ onig_foreach_name(oregexp->reg, name_callback, &packet);
rb_iv_set((VALUE)match, "@named_captures", packet.hash);
return (VALUE)match;
}
View
58 lib/oniguruma.rb
@@ -321,13 +321,53 @@ def source
end
class ::MatchData
- alias old_aref :[]
- def [](*idx)
- if idx[0].is_a?(Symbol)
- k = @named_captures && @named_captures[idx[0]]
- k && old_aref(k)
- else
- old_aref(*idx)
- end
- end
+ def to_index symbol
+ @named_captures && @named_captures[symbol]
+ end
+
+ alias old_aref :[]
+ def [](*idx)
+ if idx[0].is_a?(Symbol)
+ k = to_index( idx[0] )
+ k && old_aref(k)
+ else
+ old_aref(*idx)
+ end
+ end
+
+ alias old_begin :begin
+ def begin(*idx)
+ if idx[0].is_a?(Symbol)
+ k = to_index( idx[0] )
+ k && old_begin(k)
+ elsif idx.empty?
+ old_begin( 0 )
+ else
+ old_begin(*idx)
+ end
+ end
+
+ alias old_end :end
+ def end(*idx)
+ if idx[0].is_a?(Symbol)
+ k = to_index( idx[0] )
+ k && old_end(k)
+ elsif idx.empty?
+ old_end( 0 )
+ else
+ old_end(*idx)
+ end
+ end
+
+ alias old_offset :offset
+ def offset(*idx)
+ if idx[0].is_a?(Symbol)
+ k = to_index( idx[0] )
+ k && old_offset(k)
+ elsif idx.empty?
+ old_offset( 0 )
+ else
+ old_offset(*idx)
+ end
+ end
end
View
3  test/test_oniguruma.rb
@@ -132,6 +132,7 @@ def test_square_brackets
def test_begin
matches = @reg.match( "THX1138." )
+ assert_equal( 1, matches.begin )
assert_equal( 1, matches.begin(0) )
assert_equal( 2, matches.begin(2) )
end
@@ -143,6 +144,7 @@ def test_captures
def test_end
matches = @reg.match( "THX1138." )
+ assert_equal( 7, matches.end )
assert_equal( 7, matches.end(0) )
assert_equal( 3, matches.end(2) )
end
@@ -155,6 +157,7 @@ def test_size
def test_offset
matches = @reg.match( "THX1138." )
+ assert_equal( [1, 7], matches.offset )
assert_equal( [1, 7], matches.offset(0) )
assert_equal( [6, 7], matches.offset(4) )
end
Please sign in to comment.
Something went wrong with that request. Please try again.