123456789_123456789_123456789_123456789_123456789_

Module: RuboCop::Cop::RegexpMetacharacter

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/rubocop/cop/mixin/regexp_metacharacter.rb

Overview

Common functionality for handling regexp metacharacters.

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#safe_multiline?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/regexp_metacharacter.rb', line 71

def safe_multiline?
  cop_config.fetch('SafeMultiline', true)
end

Instance Method Details

#drop_end_metacharacter(regexp_string) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/regexp_metacharacter.rb', line 63

def drop_end_metacharacter(regexp_string)
  if regexp_string.end_with?('\\z')
    regexp_string.chomp('\z') # drop `\z` anchor
  else
    regexp_string.chop # drop `$` anchor
  end
end

#drop_start_metacharacter(regexp_string) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/regexp_metacharacter.rb', line 55

def drop_start_metacharacter(regexp_string)
  if regexp_string.start_with?('\\A')
    regexp_string[2..] # drop `\A` anchor
  else
    regexp_string[1..] # drop `^` anchor
  end
end

#literal_at_end?(regexp) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/regexp_metacharacter.rb', line 15

def literal_at_end?(regexp)
  return true if literal_at_end_with_backslash_z?(regexp)

  !safe_multiline? && literal_at_end_with_dollar?(regexp)
end

#literal_at_end_with_backslash_z?(regex_str) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/regexp_metacharacter.rb', line 41

def literal_at_end_with_backslash_z?(regex_str)
  # is this regexp 'literal' in the sense of only matching literal
  # chars, rather than using metachars like . and * and so on?
  # also, is it anchored at the end of the string?
  /\A(?:#{Util::LITERAL_REGEX})+\\z\z/o.match?(regex_str)
end

#literal_at_end_with_dollar?(regex_str) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/regexp_metacharacter.rb', line 48

def literal_at_end_with_dollar?(regex_str)
  # is this regexp 'literal' in the sense of only matching literal
  # chars, rather than using metachars like . and * and so on?
  # also, is it anchored at the end of the string?
  /\A(?:#{Util::LITERAL_REGEX})+\$\z/o.match?(regex_str)
end

#literal_at_start?(regexp) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/regexp_metacharacter.rb', line 9

def literal_at_start?(regexp)
  return true if literal_at_start_with_backslash_a?(regexp)

  !safe_multiline? && literal_at_start_with_caret?(regexp)
end

#literal_at_start_with_backslash_a?(regex_str) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/regexp_metacharacter.rb', line 21

def literal_at_start_with_backslash_a?(regex_str)
  # is this regexp 'literal' in the sense of only matching literal
  # chars, rather than using metachars like `.` and `*` and so on?
  # also, is it anchored at the start of the string?
  # (tricky: \s, \d, and so on are metacharacters, but other characters
  #  escaped with a slash are just literals. LITERAL_REGEX takes all
  #  that into account.)
  /\A\\A(?:#{Util::LITERAL_REGEX})+\z/o.match?(regex_str)
end

#literal_at_start_with_caret?(regex_str) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/regexp_metacharacter.rb', line 31

def literal_at_start_with_caret?(regex_str)
  # is this regexp 'literal' in the sense of only matching literal
  # chars, rather than using metachars like `.` and `*` and so on?
  # also, is it anchored at the start of the string?
  # (tricky: \s, \d, and so on are metacharacters, but other characters
  #  escaped with a slash are just literals. LITERAL_REGEX takes all
  #  that into account.)
  /\A\^(?:#{Util::LITERAL_REGEX})+\z/o.match?(regex_str)
end