<commit_before>require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe "MatchData#[]" do
  it "acts as normal array indexing [index]" do
    md = /(.)(.)(\d+)(\d)/.match("THX1138.")

    md[0].should == 'HX1138'
    md[1].should == 'H'
    md[2].should == 'X'
    md[-3].should == 'X'
    md[10000].should == nil
    md[-10000].should == nil
  end

  it "supports accessors [start, length]" do
    /(.)(.)(\d+)(\d)/.match("THX1138.")[1, 2].should == %w|H X|
    /(.)(.)(\d+)(\d)/.match("THX1138.")[-3, 2].should == %w|X 113|
  end

  it "supports ranges [start..end]" do
    /(.)(.)(\d+)(\d)/.match("THX1138.")[1..3].should == %w|H X 113|
  end

  ruby_version_is "3.0" do
    it "returns instances of String when given a String subclass" do
      str = MatchDataSpecs::MyString.new("THX1138.")
      /(.)(.)(\d+)(\d)/.match(str)[0..-1].each { |m| m.should be_an_instance_of(String) }
    end
  end
end

describe "MatchData#[Symbol]" do
  it "returns the corresponding named match when given a Symbol" do
    md = 'haystack'.match(/(?<t>t(?<a>ack))/)
    md[:a].should == 'ack'
    md[:t].should == 'tack'
  end

  it "returns the corresponding named match when given a String" do
    md = 'haystack'.match(/(?<t>t(?<a>ack))/)
    md['a'].should == 'ack'
    md['t'].should == 'tack'
  end<commit_msg>Fix bounds checking issues with `MatchData#[Integer, Integer]`.<commit_after>require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe "MatchData#[]" do
  it "acts as normal array indexing [index]" do
    md = /(.)(.)(\d+)(\d)/.match("THX1138.")

    md[0].should == 'HX1138'
    md[1].should == 'H'
    md[2].should == 'X'
    md[-3].should == 'X'
    md[10000].should == nil
    md[-10000].should == nil
  end

  it "supports accessors [start, length]" do
    /(.)(.)(\d+)(\d)/.match("THX1138.")[1, 2].should == %w|H X|
    /(.)(.)(\d+)(\d)/.match("THX1138.")[-3, 2].should == %w|X 113|

    # negative index is larger than the number of match values
    /(.)(.)(\d+)(\d)/.match("THX1138.")[-30, 2].should == nil

    # length argument larger than number of match values is capped to match value length
    /(.)(.)(\d+)(\d)/.match("THX1138.")[3, 10].should == %w|113 8|
  end

  it "supports ranges [start..end]" do
    /(.)(.)(\d+)(\d)/.match("THX1138.")[1..3].should == %w|H X 113|
  end

  ruby_version_is "3.0" do
    it "returns instances of String when given a String subclass" do
      str = MatchDataSpecs::MyString.new("THX1138.")
      /(.)(.)(\d+)(\d)/.match(str)[0..-1].each { |m| m.should be_an_instance_of(String) }
    end
  end
end

describe "MatchData#[Symbol]" do
  it "returns the corresponding named match when given a Symbol" do
    md = 'haystack'.match(/(?<t>t(?<a>ack))/)
    md[:a].should == 'ack'
    md[:t].should == 'tack'
  end

  it "returns the corresponding named match when given a String" do
    md = 'haystack'.match(/(?<t>t(?<a>ack))/)
    md['a'].should == 'ack'
    md['t'].should == 'tack'
  end
{'fraction_non_alphanumeric': 0.23118099698895953, 'fraction_numerical': 0.04215456674473068, 'mean_word_length': 3.0405405405405403, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7378229', 'n_tokens_mistral': 1250, 'n_tokens_neox': 1126, 'n_words': 270}