Created
March 14, 2015 17:01
-
-
Save outoftime/5935f42f81d11ae49771 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_support/all' | |
class FakeRegexp | |
def initialize(pattern) | |
@pattern = pattern | |
end | |
def matches?(str) | |
case | |
when pattern.empty? | |
str.empty? | |
when head_char_star? | |
with_head_char_repeated.matches?(str) || tail.matches?(str) | |
when head_char_matches?(str) | |
tail.matches?(str[1..-1]) | |
else | |
false | |
end | |
end | |
def to_s | |
pattern | |
end | |
def inspect | |
"/#{pattern}/" | |
end | |
protected | |
attr_reader :pattern | |
def head_char | |
@head_char ||= pattern[0] | |
end | |
def head_char_matches?(str) | |
(head_char == '.' && !str.empty?) || head_char == str[0] | |
end | |
def head_char_star? | |
pattern[1] == '*' | |
end | |
def with_head_char_repeated | |
self.class.new("#{head_char}#{pattern}") | |
end | |
def tail | |
head_length = head_char_star? ? 2 : 1 | |
@tail ||= self.class.new(pattern[head_length..-1]) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def assert_match(pattern, string) | |
regexp = FakeRegexp.new(pattern) | |
STDERR.puts "#{regexp.inspect} does not match #{string.inspect}" unless regexp.matches?(string) | |
end | |
def assert_no_match(pattern, string) | |
regexp = FakeRegexp.new(pattern) | |
STDERR.puts "#{regexp.inspect} matches #{string.inspect}" if regexp.matches?(string) | |
end | |
assert_match 'a', 'a' | |
assert_match 'aa', 'aa' | |
assert_match '', '' | |
assert_match '.', 'a' | |
assert_match '..', 'aa' | |
assert_match 'a.', 'ab' | |
assert_match '.a', 'ba' | |
assert_match 'a.c', 'abc' | |
assert_match 'a*', '' | |
assert_match 'a*', 'a' | |
assert_match 'a*', 'aaa' | |
assert_match 'ab*', 'ab' | |
assert_match 'ab*', 'a' | |
assert_match 'a*a', 'a' | |
assert_match 'a*a', 'aa' | |
assert_match 'a*a', 'aaa' | |
assert_match 'a*b*', '' | |
assert_match 'a*b*', 'a' | |
assert_match 'a*b*', 'b' | |
assert_match 'a*b*', 'aa' | |
assert_match 'a*b*', 'ab' | |
assert_match 'a*b*', 'bb' | |
assert_match 'a*b*', 'aabb' | |
assert_no_match 'a', 'aa' | |
assert_no_match 'aa', 'a' | |
assert_no_match '', 'a' | |
assert_no_match 'a', '' | |
assert_no_match '.', '' | |
assert_no_match 'a.', '' | |
assert_no_match 'a.', 'abc' | |
assert_no_match 'a.', 'ba' | |
assert_no_match '.a', 'ab' | |
assert_no_match 'a.c', 'ac' | |
assert_no_match 'a.c', 'acb' | |
assert_no_match 'a*', 'b' | |
assert_no_match 'a*', 'ba' | |
assert_no_match 'a*', 'aaab' | |
assert_no_match 'ab*', 'aba' | |
assert_no_match 'ab*', 'ba' | |
assert_no_match 'a*a', '' | |
assert_no_match 'a*b*', 'ba' | |
assert_no_match 'a*b*', 'abba' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment