summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--string.c19
-rw-r--r--test/ruby/test_string.rb4
2 files changed, 18 insertions, 5 deletions
diff --git a/string.c b/string.c
index e1c6e54456..e984330b9a 100644
--- a/string.c
+++ b/string.c
@@ -3676,8 +3676,14 @@ rb_str_index_m(int argc, VALUE *argv, VALUE str)
pos = str_offset(RSTRING_PTR(str), RSTRING_END(str), pos,
rb_enc_check(str, sub), single_byte_optimizable(str));
- pos = rb_reg_search(sub, str, pos, 0);
- pos = rb_str_sublen(str, pos);
+ if (rb_reg_search(sub, str, pos, 0) < 0) {
+ return Qnil;
+ } else {
+ VALUE match = rb_backref_get();
+ struct re_registers *regs = RMATCH_REGS(match);
+ pos = rb_str_sublen(str, BEG(0));
+ return LONG2NUM(pos);
+ }
}
else {
StringValue(sub);
@@ -3827,9 +3833,12 @@ rb_str_rindex_m(int argc, VALUE *argv, VALUE str)
pos = str_offset(RSTRING_PTR(str), RSTRING_END(str), pos,
enc, single_byte_optimizable(str));
- pos = rb_reg_search(sub, str, pos, 1);
- pos = rb_str_sublen(str, pos);
- if (pos >= 0) return LONG2NUM(pos);
+ if (rb_reg_search(sub, str, pos, 1) >= 0) {
+ VALUE match = rb_backref_get();
+ struct re_registers *regs = RMATCH_REGS(match);
+ pos = rb_str_sublen(str, BEG(0));
+ return LONG2NUM(pos);
+ }
}
else {
StringValue(sub);
diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb
index 308ba80373..489eee33dc 100644
--- a/test/ruby/test_string.rb
+++ b/test/ruby/test_string.rb
@@ -1353,6 +1353,8 @@ CODE
assert_nil("foo".index(//, -100))
assert_nil($~)
+
+ assert_equal(2, S("abcdbce").index(/b\Kc/))
end
def test_insert
@@ -1525,6 +1527,8 @@ CODE
assert_equal(3, "foo".rindex(//))
assert_equal([3, 3], $~.offset(0))
+
+ assert_equal(5, S("abcdbce").rindex(/b\Kc/))
end
def test_rjust