Module: Lorentz::Glob
- Included in:
- Keys
- Defined in:
- lib/lorentz/glob.rb
Class Method Summary collapse
-
.compile(source, case_sensitive = false) ⇒ Object
jacked from github.com/karottenreibe/joker & specifically karottenreibe.github.com/2009/12/03/inside-the-joker/.
Class Method Details
.compile(source, case_sensitive = false) ⇒ Object
jacked from github.com/karottenreibe/joker & specifically karottenreibe.github.com/2009/12/03/inside-the-joker/
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/lorentz/glob.rb', line 4 def compile(source, case_sensitive=false) ptr = 0 compiled = '^' while ptr < source.length snip = source[ptr..ptr] case snip when '\\' lookahead = source[ptr+1..ptr+1] case snip when '\\\\', '\\?', '\\*' compiled << snip << lookahead else compiled << Regexp.quote(lookahead) end ptr += 1 when '?' then compiled << '.' when '*' then compiled << '.*' when '[' then compiled << '[' when ']' then compiled << ']' else compiled << Regexp.quote(snip) end ptr += 1 end compiled << '$' Regexp.compile(compiled, case_sensitive) end |