summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--re.c9
-rw-r--r--test/ruby/test_regexp.rb22
2 files changed, 26 insertions, 5 deletions
diff --git a/re.c b/re.c
index 87df1a6ef0..f8682e23e1 100644
--- a/re.c
+++ b/re.c
@@ -1089,6 +1089,8 @@ static VALUE
match_names(VALUE match)
{
match_check(match);
+ if (NIL_P(RMATCH(match)->regexp))
+ return rb_ary_new_capa(0);
return rb_reg_names(RMATCH(match)->regexp);
}
@@ -2078,6 +2080,8 @@ match_named_captures(VALUE match)
struct MEMO *memo;
match_check(match);
+ if (NIL_P(RMATCH(match)->regexp))
+ return rb_hash_new();
hash = rb_hash_new();
memo = MEMO_NEW(hash, match, 0);
@@ -2948,7 +2952,7 @@ match_hash(VALUE match)
match_check(match);
hashval = rb_hash_start(rb_str_hash(RMATCH(match)->str));
- hashval = rb_hash_uint(hashval, reg_hash(RMATCH(match)->regexp));
+ hashval = rb_hash_uint(hashval, reg_hash(match_regexp(match)));
regs = RMATCH_REGS(match);
hashval = rb_hash_uint(hashval, regs->num_regs);
hashval = rb_hash_uint(hashval, rb_memhash(regs->beg, regs->num_regs * sizeof(*regs->beg)));
@@ -2970,11 +2974,12 @@ static VALUE
match_equal(VALUE match1, VALUE match2)
{
const struct re_registers *regs1, *regs2;
+
if (match1 == match2) return Qtrue;
if (!RB_TYPE_P(match2, T_MATCH)) return Qfalse;
if (!RMATCH(match1)->regexp || !RMATCH(match2)->regexp) return Qfalse;
if (!rb_str_equal(RMATCH(match1)->str, RMATCH(match2)->str)) return Qfalse;
- if (!rb_reg_equal(RMATCH(match1)->regexp, RMATCH(match2)->regexp)) return Qfalse;
+ if (!rb_reg_equal(match_regexp(match1), match_regexp(match2))) return Qfalse;
regs1 = RMATCH_REGS(match1);
regs2 = RMATCH_REGS(match2);
if (regs1->num_regs != regs2->num_regs) return Qfalse;
diff --git a/test/ruby/test_regexp.rb b/test/ruby/test_regexp.rb
index bdf8e48720..a9120149c5 100644
--- a/test/ruby/test_regexp.rb
+++ b/test/ruby/test_regexp.rb
@@ -644,14 +644,30 @@ class TestRegexp < Test::Unit::TestCase
end
def test_match_without_regexp
+ # create a MatchData for each assertion because the internal state may change
+ test = proc {|&blk| "abc".sub("a", ""); blk.call($~) }
+
bug10877 = '[ruby-core:68209] [Bug #10877]'
- "abc".sub("a", "")
- assert_raise_with_message(IndexError, /foo/, bug10877) {$~["foo"]}
+ test.call {|m| assert_raise_with_message(IndexError, /foo/, bug10877) {m["foo"]} }
key = "\u{3042}"
[Encoding::UTF_8, Encoding::Shift_JIS, Encoding::EUC_JP].each do |enc|
idx = key.encode(enc)
- assert_raise_with_message(IndexError, /#{idx}/, bug10877) {$~[idx]}
+ test.call {|m| assert_raise_with_message(IndexError, /#{idx}/, bug10877) {m[idx]} }
end
+ test.call {|m| assert_equal(/a/, m.regexp) }
+ test.call {|m| assert_equal("abc", m.string) }
+ test.call {|m| assert_equal(1, m.size) }
+ test.call {|m| assert_equal(0, m.begin(0)) }
+ test.call {|m| assert_equal(1, m.end(0)) }
+ test.call {|m| assert_equal([0, 1], m.offset(0)) }
+ test.call {|m| assert_equal([], m.captures) }
+ test.call {|m| assert_equal([], m.names) }
+ test.call {|m| assert_equal({}, m.named_captures) }
+ test.call {|m| assert_equal(/a/.match("abc"), m) }
+ test.call {|m| assert_equal(/a/.match("abc").hash, m.hash) }
+ test.call {|m| assert_equal("bc", m.post_match) }
+ test.call {|m| assert_equal("", m.pre_match) }
+ test.call {|m| assert_equal(["a", nil], m.values_at(0, 1)) }
end
def test_last_match