Sha256: b55a4950d35c1a5e061d2fcd31bd3c63f2e455ba19970b3e574dc92b6cc985e8

Contents?: true

Size: 1.84 KB

Versions: 98

Compression:

Stored size: 1.84 KB

Contents

module Recog
  class Fingerprint

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

      # Currently, only options relating to case insensitivity and
      # multiline/newline are supported.  Because Recog's data is used by tools
      # written in different languages like Ruby and Java, we currently support
      # specifying them in a variety of ways.  This map controls how they can
      # be specified.
      #
      # TODO: consider supporting only a simpler variant and require that tools
      # that use Recog data translate accordingly
      FLAG_MAP = {
        # multiline variations
        'REG_DOT_NEWLINE'   => Regexp::MULTILINE,
        'REG_LINE_ANY_CRLF' => Regexp::MULTILINE,
        'REG_MULTILINE'     => Regexp::MULTILINE,
        # case variations
        'REG_ICASE'         => Regexp::IGNORECASE,
        'IGNORECASE'        => Regexp::IGNORECASE
      }

      DEFAULT_FLAGS = 0

      # @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)
        unsupported_flags = flags.select { |flag| !FLAG_MAP.key?(flag) }
        unless unsupported_flags.empty?
          fail "Unsupported regular expression flags found: #{unsupported_flags.join(',')}. Must be one of: #{FLAG_MAP.keys.join(',')}"
        end
        flags.reduce(DEFAULT_FLAGS) do |sum, flag|
          sum |= (FLAG_MAP[flag] || 0)
        end
      end
    end
  end
end

Version data entries

98 entries across 98 versions & 2 rubygems

Version Path
recog-3.1.1 lib/recog/fingerprint/regexp_factory.rb
recog-3.1.0 lib/recog/fingerprint/regexp_factory.rb
recog-3.0.3 lib/recog/fingerprint/regexp_factory.rb
recog-3.0.2 lib/recog/fingerprint/regexp_factory.rb
recog-3.0.1 lib/recog/fingerprint/regexp_factory.rb
recog-2.3.23 lib/recog/fingerprint/regexp_factory.rb
recog-2.3.22 lib/recog/fingerprint/regexp_factory.rb
recog-2.3.21 lib/recog/fingerprint/regexp_factory.rb
recog-2.3.20 lib/recog/fingerprint/regexp_factory.rb
recog-2.3.19 lib/recog/fingerprint/regexp_factory.rb
recog-2.3.18 lib/recog/fingerprint/regexp_factory.rb
recog-2.3.17 lib/recog/fingerprint/regexp_factory.rb
recog-2.3.16 lib/recog/fingerprint/regexp_factory.rb
recog-2.3.15 lib/recog/fingerprint/regexp_factory.rb
recog-intrigue-2.3.14 lib/recog/fingerprint/regexp_factory.rb
recog-2.3.14 lib/recog/fingerprint/regexp_factory.rb
recog-2.3.13 lib/recog/fingerprint/regexp_factory.rb
recog-2.3.12 lib/recog/fingerprint/regexp_factory.rb
recog-2.3.11 lib/recog/fingerprint/regexp_factory.rb
recog-2.3.10 lib/recog/fingerprint/regexp_factory.rb