Sha256: 9c2782ed443040a1a52052b3307b563489a9e1147dc60833363c19684df4107d

Contents?: true

Size: 1.53 KB

Versions: 25

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true
require 'mustermann'
require 'mustermann/regexp_based'
require 'strscan'

module Mustermann
  # Regexp pattern implementation.
  #
  # @example
  #   Mustermann.new('/.*', type: :regexp) === '/bar' # => true
  #
  # @see Mustermann::Pattern
  # @see file:README.md#simple Syntax description in the README
  class Regular < RegexpBased
    include Concat::Native
    register :regexp, :regular
    supported_options :check_anchors

    # @param (see Mustermann::Pattern#initialize)
    # @return (see Mustermann::Pattern#initialize)
    # @see (see Mustermann::Pattern#initialize)
    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

    def compile(**options)
      if @check_anchors
        scanner = ::StringScanner.new(@string)
        check_anchors(scanner) until scanner.eos?
      end

      /#{@string}/
    end

    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

    def extended_regexp?(string)
      not (Regexp.new(string).options & Regexp::EXTENDED).zero?
    end

    private :compile, :check_anchors, :extended_regexp?
  end
end

Version data entries

25 entries across 22 versions & 4 rubygems

Version Path
mustermann-3.0.3 lib/mustermann/regular.rb
mustermann-3.0.2 lib/mustermann/regular.rb
mustermann-3.0.1 lib/mustermann/regular.rb
rubypitaya-3.12.5 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mustermann-3.0.0/lib/mustermann/regular.rb
rubypitaya-3.12.4 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mustermann-3.0.0/lib/mustermann/regular.rb
rubypitaya-3.12.3 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mustermann-3.0.0/lib/mustermann/regular.rb
rubypitaya-3.12.2 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/mustermann-3.0.0/lib/mustermann/regular.rb
mustermann-3.0.0 lib/mustermann/regular.rb
mustermann-2.0.2 lib/mustermann/regular.rb
mustermann-2.0.1 lib/mustermann/regular.rb
mustermann-2.0.0 lib/mustermann/regular.rb
mustermann-1.1.2 lib/mustermann/regular.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/3.1.0/gems/mustermann-1.1.1/lib/mustermann/regular.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/3.0.0/gems/mustermann-1.1.1/lib/mustermann/regular.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/2.6.0/gems/mustermann-1.1.1/lib/mustermann/regular.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/mustermann-1.0.3/lib/mustermann/regular.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/mustermann-1.1.1/lib/mustermann/regular.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/mustermann-1.0.3/lib/mustermann/regular.rb
mustermann-1.1.1 lib/mustermann/regular.rb
mustermann-1.1.0 lib/mustermann/regular.rb