Sha256: bd358d9db2962c8fc5db510d12d9699068674cb3423aa0d3d19e2a2fa7bd2084

Contents?: true

Size: 1.03 KB

Versions: 17

Compression:

Stored size: 1.03 KB

Contents

module Recog
  class Fingerprint

    #
    # @example
    #   r = RegexpFactory.build("^Apache[ -]Coyote/(\d\.\d)$", "REG_ICASE")
    #   r.match("Apache-Coyote/1.1")
    #
    module RegexpFactory

      # Map strings as used in Recog XML to Fixnum values used by Regexp
      FLAG_MAP = {
        'REG_DOT_NEWLINE'   => Regexp::MULTILINE,
        'REG_LINE_ANY_CRLF' => Regexp::MULTILINE,
        'REG_ICASE'         => Regexp::IGNORECASE,
      }

      # @return [Regexp]
      def self.build(pattern, flags)
        options = build_options(flags)
        Regexp.new(pattern, options)
      end

      # Convert string flag names as used in Recog XML into a Fixnum suitable for
      # passing as the `options` parameter to `Regexp.new`
      #
      # @see FLAG_MAP
      # @param flags [Array<String>]
      # @return [Fixnum] Flags for creating a regular expression object
      def self.build_options(flags)
        flags.reduce(Regexp::NOENCODING) do |sum, flag|
          sum |= (FLAG_MAP[flag] || 0)
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
recog-1.0.16 lib/recog/fingerprint/regexp_factory.rb
recog-1.0.15 lib/recog/fingerprint/regexp_factory.rb
recog-1.0.14 lib/recog/fingerprint/regexp_factory.rb
recog-1.0.13 lib/recog/fingerprint/regexp_factory.rb
recog-1.0.12 lib/recog/fingerprint/regexp_factory.rb
recog-1.0.11 lib/recog/fingerprint/regexp_factory.rb
recog-1.0.10 lib/recog/fingerprint/regexp_factory.rb
recog-1.0.9 lib/recog/fingerprint/regexp_factory.rb
recog-1.0.8 lib/recog/fingerprint/regexp_factory.rb
recog-1.0.7 lib/recog/fingerprint/regexp_factory.rb
recog-1.0.6 lib/recog/fingerprint/regexp_factory.rb
recog-1.0.5 lib/recog/fingerprint/regexp_factory.rb
recog-1.0.4 lib/recog/fingerprint/regexp_factory.rb
recog-1.0.3 lib/recog/fingerprint/regexp_factory.rb
recog-1.0.2 lib/recog/fingerprint/regexp_factory.rb
recog-1.0.1 lib/recog/fingerprint/regexp_factory.rb
recog-1.0.0 lib/recog/fingerprint/regexp_factory.rb