diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2008-11-10 19:53:53 -0800 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2008-11-10 22:21:09 -0800 |
commit | 278b6cd9529f33286449a9be18f1903687814d3f (patch) | |
tree | 7ecff6aeef0e2b9b956e8d2b9902cc21aa872183 /actionpack | |
parent | 5db9f9b3ad47fadf0b3f12ada1c2ea7b9c15ded5 (diff) | |
download | rails-278b6cd9529f33286449a9be18f1903687814d3f.tar.gz rails-278b6cd9529f33286449a9be18f1903687814d3f.tar.bz2 rails-278b6cd9529f33286449a9be18f1903687814d3f.zip |
Eliminate excess Regexp creation due to capture counting
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_controller/routing/route.rb | 2 | ||||
-rw-r--r-- | actionpack/lib/action_controller/routing/segments.rb | 28 |
2 files changed, 26 insertions, 4 deletions
diff --git a/actionpack/lib/action_controller/routing/route.rb b/actionpack/lib/action_controller/routing/route.rb index 3b2cb28545..a610ec7e84 100644 --- a/actionpack/lib/action_controller/routing/route.rb +++ b/actionpack/lib/action_controller/routing/route.rb @@ -219,7 +219,7 @@ module ActionController next_capture = 1 extraction = segments.collect do |segment| x = segment.match_extraction(next_capture) - next_capture += Regexp.new(segment.regexp_chunk).number_of_captures + next_capture += segment.number_of_captures x end extraction.compact diff --git a/actionpack/lib/action_controller/routing/segments.rb b/actionpack/lib/action_controller/routing/segments.rb index e5f174ae2c..f6b03edcca 100644 --- a/actionpack/lib/action_controller/routing/segments.rb +++ b/actionpack/lib/action_controller/routing/segments.rb @@ -13,6 +13,10 @@ module ActionController @is_optional = false end + def number_of_captures + Regexp.new(regexp_chunk).number_of_captures + end + def extraction_code nil end @@ -84,6 +88,10 @@ module ActionController optional? ? Regexp.optionalize(chunk) : chunk end + def number_of_captures + 0 + end + def build_pattern(pattern) escaped = Regexp.escape(value) if optional? && ! pattern.empty? @@ -194,10 +202,16 @@ module ActionController end end + def number_of_captures + if regexp + regexp.number_of_captures + 1 + else + 1 + end + end + def build_pattern(pattern) - chunk = regexp_chunk - chunk = "(#{chunk})" if Regexp.new(chunk).number_of_captures == 0 - pattern = "#{chunk}#{pattern}" + pattern = "#{regexp_chunk}#{pattern}" optional? ? Regexp.optionalize(pattern) : pattern end @@ -230,6 +244,10 @@ module ActionController "(?i-:(#{(regexp || Regexp.union(*possible_names)).source}))" end + def number_of_captures + 1 + end + # Don't URI.escape the controller name since it may contain slashes. def interpolation_chunk(value_code = local_name) "\#{#{value_code}.to_s}" @@ -275,6 +293,10 @@ module ActionController regexp || "(.*)" end + def number_of_captures + regexp ? regexp.number_of_captures : 1 + end + def optionality_implied? true end |