From 449899b38314d0ecbe61e42d34398bdf2561b8e9 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Thu, 9 May 2024 11:56:51 +0200 Subject: Fix `String#index` to clear MatchData when a regexp is passed [Bug #20421] The bug was fixed in Ruby 3.3 via 9dcdffb8bf8a3654fd78bf1a58b30c8e13888a7a --- spec/ruby/core/string/byteindex_spec.rb | 16 ++++++++++++++++ spec/ruby/core/string/index_spec.rb | 11 +++++++++++ spec/ruby/core/string/rindex_spec.rb | 9 +++++++++ 3 files changed, 36 insertions(+) create mode 100644 spec/ruby/core/string/byteindex_spec.rb (limited to 'spec/ruby/core/string') diff --git a/spec/ruby/core/string/byteindex_spec.rb b/spec/ruby/core/string/byteindex_spec.rb new file mode 100644 index 0000000000..af70c729e8 --- /dev/null +++ b/spec/ruby/core/string/byteindex_spec.rb @@ -0,0 +1,16 @@ +# -*- encoding: utf-8 -*- +require_relative '../../spec_helper' +require_relative 'fixtures/classes' + +describe "String#byteindex with Regexp" do + ruby_version_is "3.2" do + it "always clear $~" do + "a".byteindex(/a/) + $~.should_not == nil + + string = "blablabla" + string.byteindex(/bla/, string.bytesize + 1) + $~.should == nil + end + end +end diff --git a/spec/ruby/core/string/index_spec.rb b/spec/ruby/core/string/index_spec.rb index 2eeee9be87..93153e65f0 100644 --- a/spec/ruby/core/string/index_spec.rb +++ b/spec/ruby/core/string/index_spec.rb @@ -224,6 +224,17 @@ describe "String#index with Regexp" do $~.should == nil end + ruby_bug "#20421", ""..."3.2" do + it "always clear $~" do + "a".index(/a/) + $~.should_not == nil + + string = "blablabla" + string.index(/bla/, string.length + 1) + $~.should == nil + end + end + it "starts the search at the given offset" do "blablabla".index(/.{0}/, 5).should == 5 "blablabla".index(/.{1}/, 5).should == 5 diff --git a/spec/ruby/core/string/rindex_spec.rb b/spec/ruby/core/string/rindex_spec.rb index e795105e1d..cd2aabba34 100644 --- a/spec/ruby/core/string/rindex_spec.rb +++ b/spec/ruby/core/string/rindex_spec.rb @@ -265,6 +265,15 @@ describe "String#rindex with Regexp" do $~.should == nil end + it "always clear $~" do + "a".rindex(/a/) + $~.should_not == nil + + string = "blablabla" + string.rindex(/bla/, -(string.length + 1)) + $~.should == nil + end + it "starts the search at the given offset" do "blablabla".rindex(/.{0}/, 5).should == 5 "blablabla".rindex(/.{1}/, 5).should == 5 -- cgit v1.2.3