Sha256: e9e8491e061e20e8236117a3c0ef28a7932a6759ca9beee1754704495f4df218
Contents?: true
Size: 1.82 KB
Versions: 18
Compression:
Stored size: 1.82 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, 'MULTILINE' => Regexp::MULTILINE, # case variations 'REG_ICASE' => Regexp::IGNORECASE, 'IGNORECASE' => 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) 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(Regexp::NOENCODING) do |sum, flag| sum |= (FLAG_MAP[flag] || 0) end end end end end
Version data entries
18 entries across 18 versions & 1 rubygems