Permalink
Browse files

Merge branch 'rmc'

* rmc:
  push move_string in to `move`
  combine move_regexp and move_string so we only loop over states once
  do not create memo objects since we'll just throw them away
  only ask if `t` is empty once.
  • Loading branch information...
2 parents 9aa7c25 + 6c6dc32 commit 5fa65f94b8c16019d2ddd55f2e59b1bf1c87db89 @tenderlove tenderlove committed Apr 1, 2014
@@ -19,6 +19,14 @@ def initialize(transition_table)
end
def simulate(string)
+ ms = memos(string) { return }
+ MatchData.new(ms)
+ end
+
+ alias :=~ :simulate
+ alias :match :simulate
+
+ def memos(string)
input = StringScanner.new(string)
state = [0]
while sym = input.scan(%r([/.?]|[^/.?]+))
@@ -29,15 +37,10 @@ def simulate(string)
tt.accepting? s
}
- return if acceptance_states.empty?
+ return yield if acceptance_states.empty?
- memos = acceptance_states.flat_map { |x| tt.memo(x) }.compact
-
- MatchData.new(memos)
+ acceptance_states.flat_map { |x| tt.memo(x) }.compact
end
-
- alias :=~ :simulate
- alias :match :simulate
end
end
end
@@ -40,7 +40,19 @@ def eclosure(t)
end
def move(t, a)
- move_string(t, a).concat(move_regexp(t, a))
+ return [] if t.empty?
+
+ regexps = []
+
+ t.map { |s|
+ if states = @regexp_states[s]
+ regexps.concat states.map { |re, v| re === a ? v : nil }
+ end
+
+ if states = @string_states[s]
+ states[a]
+ end
+ }.compact.concat regexps
end
def as_json(options = nil)
@@ -139,26 +151,6 @@ def states_hash_for(sym)
raise ArgumentError, 'unknown symbol: %s' % sym.class
end
end
-
- def move_regexp(t, a)
- return [] if t.empty?
-
- t.flat_map { |s|
- if states = @regexp_states[s]
- states.map { |re, v| re === a ? v : nil }
- end
- }.compact.uniq
- end
-
- def move_string(t, a)
- return [] if t.empty?
-
- t.map do |s|
- if states = @string_states[s]
- states[a]
- end
- end.compact
- end
end
end
end
@@ -121,8 +121,7 @@ def custom_routes
def filter_routes(path)
return [] unless ast
- data = simulator.match(path)
- data ? data.memos : []
+ simulator.memos(path) { [] }
end
def find_routes env

0 comments on commit 5fa65f9

Please sign in to comment.