Sha256: 7ff7edc88e461cfabfff5e4adfda4c9b430057137e6fdae18f2400a013a554c0
Contents?: true
Size: 1.47 KB
Versions: 3
Compression:
Stored size: 1.47 KB
Contents
module Goodcheck class Pattern attr_reader :source attr_reader :regexp def initialize(source:, regexp:) @source = source @regexp = regexp end def self.literal(literal, case_insensitive:) new(source: literal, regexp: Regexp.compile(Regexp.escape(literal), case_insensitive)) end def self.regexp(regexp, case_insensitive:, multiline:) options = 0 options |= Regexp::IGNORECASE if case_insensitive options |= Regexp::MULTILINE if multiline new(source: regexp, regexp: Regexp.compile(regexp, options)) end def self.token(tokens) new(source: tokens, regexp: compile_tokens(tokens)) end def self.compile_tokens(source) tokens = [] s = StringScanner.new(source) until s.eos? case when s.scan(/\(|\)|\{|\}|\[|\]|\<|\>/) tokens << Regexp.escape(s.matched) when s.scan(/\s+/) tokens << '\s+' when s.scan(/(\p{Letter}|\w)+/) tokens << Regexp.escape(s.matched) when s.scan(%r{[!"#$%&'=\-^~¥\\|`@*:+;/?.,]+}) tokens << Regexp.escape(s.matched.rstrip) when s.scan(/./) tokens << Regexp.escape(s.matched) end end if tokens.first =~ /\A\p{Letter}/ tokens.first.prepend('\b') end if tokens.last =~ /\p{Letter}\Z/ tokens.last << '\b' end Regexp.new(tokens.join('\s*').gsub(/\\s\*(\\s\+\\s\*)+/, '\s+'), Regexp::MULTILINE) end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
goodcheck-0.3.0 | lib/goodcheck/pattern.rb |
goodcheck-0.2.0 | lib/goodcheck/pattern.rb |
goodcheck-0.1.0 | lib/goodcheck/pattern.rb |