diff options
| author | Jean Boussier <jean.boussier@gmail.com> | 2024-05-09 11:56:51 +0200 |
|---|---|---|
| committer | Benoit Daloze <eregontp@gmail.com> | 2024-05-14 09:29:21 +0200 |
| commit | 449899b38314d0ecbe61e42d34398bdf2561b8e9 (patch) | |
| tree | 4fc45c44200c8cfda309056081ae61fc43a8c855 /spec/ruby/core/string | |
| parent | a10a0d0c77826b97ec246274adcc80952049688b (diff) | |
Fix `String#index` to clear MatchData when a regexp is passed
[Bug #20421]
The bug was fixed in Ruby 3.3 via 9dcdffb8bf8a3654fd78bf1a58b30c8e13888a7a
Diffstat (limited to 'spec/ruby/core/string')
| -rw-r--r-- | spec/ruby/core/string/byteindex_spec.rb | 16 | ||||
| -rw-r--r-- | spec/ruby/core/string/index_spec.rb | 11 | ||||
| -rw-r--r-- | spec/ruby/core/string/rindex_spec.rb | 9 |
3 files changed, 36 insertions, 0 deletions
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 |
