class Mustermann::Regular

Regexp pattern implementation.

@example

Mustermann.new('/.*', type: :regexp) === '/bar' # => true

@see Mustermann::Pattern @see file:README.md#simple Syntax description in the README

Public Class Methods

new(string, check_anchors: true, **options) click to toggle source

@param (see Mustermann::Pattern#initialize) @return (see Mustermann::Pattern#initialize) @see (see Mustermann::Pattern#initialize)

Calls superclass method Mustermann::RegexpBased.new
# File lib/mustermann/regular.rb, line 21
def initialize(string, check_anchors: true, **options)
  string = $1 if string.to_s =~ /\A\(\?\-mix\:(.*)\)\Z/ && string.inspect == "/#$1/"
  string = string.source.gsub!(/(?<!\\)(?:\s|#.*$)/, '') if extended_regexp?(string)
  @check_anchors = check_anchors
  super(string, **options)
end

Private Instance Methods

check_anchors(scanner) click to toggle source
# File lib/mustermann/regular.rb, line 37
def check_anchors(scanner)
  return scanner.scan_until(/\]/) if scanner.scan(/\[/)
  return scanner.scan(/\\?./) unless illegal = scanner.scan(/\\[AzZ]|[\^\$]/)
  raise CompileError, "regular expression should not contain %s: %p" % [illegal.to_s, @string]
end
compile(**options) click to toggle source
# File lib/mustermann/regular.rb, line 28
def compile(**options)
  if @check_anchors
    scanner = ::StringScanner.new(@string)
    check_anchors(scanner) until scanner.eos?
  end

  /#{@string}/
end
extended_regexp?(string) click to toggle source
# File lib/mustermann/regular.rb, line 43
def extended_regexp?(string)
  not (Regexp.new(string).options & Regexp::EXTENDED).zero?
end