Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Do not create @named_captures hash if there are no named groups for r…

…egexp -- somewhat improve speed for repetive calls
  • Loading branch information...
commit 1108b46b1d491cd17b7056ab67acb6803b025672 1 parent 0962aff
Nikolai Lugovoi authored
Showing with 20 additions and 5 deletions.
  1. +7 −5 ext/oregexp.c
  2. +13 −0 test/test_oniguruma.rb
View
12 ext/oregexp.c
@@ -111,8 +111,7 @@ static int name_callback(
regex_t* reg,
struct callback_packet* arg
) {
- int i, gn, ref;
- OnigRegion *region = arg->region;
+ int i, gn;
VALUE nameHash = arg->hash;
for (i = 0; i < ngroup_num; i++) {
@@ -157,6 +156,7 @@ struct RMatch {
struct re_registers *regs;
};
#define RMATCH(obj) (R_CAST(RMatch)(obj))
+void rb_match_busy _((VALUE));
static VALUE oregexp_make_match_data(ORegexp * oregexp, OnigRegion * region, VALUE string_str) {
VALUE rb_cMatch = rb_const_get(rb_cObject, rb_intern("MatchData")) ;
@@ -179,9 +179,11 @@ 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();
- onig_foreach_name(oregexp->reg, name_callback, &packet);
- rb_iv_set((VALUE)match, "@named_captures", packet.hash);
+ if( onig_number_of_names( oregexp->reg ) > 0 ) {
+ packet.hash = rb_hash_new();
+ onig_foreach_name(oregexp->reg, name_callback, &packet);
+ rb_iv_set((VALUE)match, "@named_captures", packet.hash);
+ }
return (VALUE)match;
}
View
13 test/test_oniguruma.rb
@@ -268,6 +268,7 @@ def test_group_by_name
reg = Oniguruma::ORegexp.new( '(?<begin>\()(?<body>.*)(?<end>\))', :options => Oniguruma::OPTION_MULTILINE )
matches = reg.match( "blah (content) blah" )
assert_not_nil( matches )
+ assert_equal $~, matches
assert_equal( '(', matches[:begin] )
assert_equal( 'content', matches[:body] )
assert_equal( ')', matches[:end] )
@@ -279,6 +280,18 @@ def test_multibyte_named_backrefs
assert_equal "should TEST", r.sub("should test this damned test") {|m| m[:"имя"].upcase }
end
+ def test_no_named_backrefs
+ r = Oniguruma::ORegexp.new('(.+).+(.+)')
+ r.match("text")
+ assert_not_nil $~
+ assert_equal 0, $~.instance_variables.size
+ r = Oniguruma::ORegexp.new('(?<a>.+).+(?<b>.+)')
+ r.match("text")
+ assert_not_nil $~
+ assert_equal 1, $~.instance_variables.size
+
+ end
+
# casefolding for full Unicode set is not present in versions prior to 5.
if Oniguruma::VERSION >= '5.0.0'
def test_utf8_ignore_case
Please sign in to comment.
Something went wrong with that request. Please try again.