<commit_before>class Regexp
  `def._isRegexp = true`

  def self.escape(string)
    `string.replace(/[\\-\\[\\]\\/\\{\\}\\(\\)\\*\\+\\?\\.\\\\\^\\$\\|]/g, '\\\\$&')`
  end

  def self.union(*parts)
    `new RegExp(parts.join(''))`
  end

  def self.new(regexp, options = undefined)
    `new RegExp(regexp, options)`
  end

  def ==(other)
    `other.constructor == RegExp && #{self}.toString() === other.toString()`
  end

  def ===(str)
    if `str._isString == null` && str.respond_to?(:to_str)
      str = str.to_str
    end

    if `str._isString == null`
      return false
    end

    `self.test(str)`
  end

  def =~(string)
    if `string === nil`
      $~ = $` = $' = nil

      return
    end

    if `string._isString == null`
      unless string.respond_to? :to_str
        raise TypeError, "no implicit conversion of #{string.class} into String"
      end

      string = string.to_str
    end

    %x{
      var re = self;

      if (re.global) {
        // should we clear it afterwards too?
        re.lastIndex = 0;
      }
      else {
        // rewrite regular expression to add the global flag to capture pre/post match
        re = new RegExp(re.source, 'g' + (re.multiline ? 'm' : '') + (re.ignoreCase ? 'i' : ''));
      }

      var result = re.exec(string);

      if (result) {
        #{$~ = MatchData.new(`re`, `result`)};
      }
      else {
        #{$~ = $` = $' = nil};
      }

      return result ? result.index : nil;
    }
  end

  alias eql? ==

  def inspect
    `self.toString()`
  end

  def match(string, pos = undefined)
    if `string._isString == null`
      unless string.respond_to? :to_str
        raise TypeError, "no implicit conversion of #{other.class.name} into String"
      end

      string = string.to_str
    end

    %x{
      var re = self;

      if (re.global) {
        // should we clear it afterwards too?
        re.lastIndex = 0;
      }
      else {
        re = new RegExp(re.source, 'g' + (re.multiline ? 'm' : '') + (re.ignoreCase ? 'i' : ''));
      }

      var result = re.exec(string);

      if (result) {
        return #{$~ = MatchData.new(`re`, `result`)};
      }
      else {
        return #{$~ = $` = $' = nil};
      }
    }
  end

  def source
    `self.source`
  end

  alias to_s source

  def to_n
    `self.valueOf()`
  end
end
<commit_msg>Set special vars to nil when the parameter is nil in Regexp#match<commit_after>class Regexp
  `def._isRegexp = true`

  def self.escape(string)
    `string.replace(/[\\-\\[\\]\\/\\{\\}\\(\\)\\*\\+\\?\\.\\\\\^\\$\\|]/g, '\\\\$&')`
  end

  def self.union(*parts)
    `new RegExp(parts.join(''))`
  end

  def self.new(regexp, options = undefined)
    `new RegExp(regexp, options)`
  end

  def ==(other)
    `other.constructor == RegExp && #{self}.toString() === other.toString()`
  end

  def ===(str)
    if `str._isString == null` && str.respond_to?(:to_str)
      str = str.to_str
    end

    if `str._isString == null`
      return false
    end

    `self.test(str)`
  end

  def =~(string)
    if `string === nil`
      $~ = $` = $' = nil

      return
    end

    if `string._isString == null`
      unless string.respond_to? :to_str
        raise TypeError, "no implicit conversion of #{string.class} into String"
      end

      string = string.to_str
    end

    %x{
      var re = self;

      if (re.global) {
        // should we clear it afterwards too?
        re.lastIndex = 0;
      }
      else {
        // rewrite regular expression to add the global flag to capture pre/post match
        re = new RegExp(re.source, 'g' + (re.multiline ? 'm' : '') + (re.ignoreCase ? 'i' : ''));
      }

      var result = re.exec(string);

      if (result) {
        #{$~ = MatchData.new(`re`, `result`)};
      }
      else {
        #{$~ = $` = $' = nil};
      }

      return result ? result.index : nil;
    }
  end

  alias eql? ==

  def inspect
    `self.toString()`
  end

  def match(string, pos = undefined)
    if `string === nil`
      $~ = $` = $' = nil

      return
    end

    if `string._isString == null`
      unless string.respond_to? :to_str
        raise TypeError, "no implicit conversion of #{string.class} into String"
      end

      string = string.to_str
    end

    %x{
      var re = self;

      if (re.global) {
        // should we clear it afterwards too?
        re.lastIndex = 0;
      }
      else {
        re = new RegExp(re.source, 'g' + (re.multiline ? 'm' : '') + (re.ignoreCase ? 'i' : ''));
      }

      var result = re.exec(string);

      if (result) {
        return #{$~ = MatchData.new(`re`, `result`)};
      }
      else {
        return #{$~ = $` = $' = nil};
      }
    }
  end

  def source
    `self.source`
  end

  alias to_s source

  def to_n
    `self.valueOf()`
  end
end

{'fraction_non_alphanumeric': 0.17063989962358847, 'fraction_numerical': 0.000836470096194061, 'mean_word_length': 1.8234946871310507, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4465629', 'n_tokens_mistral': 1722, 'n_tokens_neox': 1639, 'n_words': 411}