Skip to content

Commit

Permalink
Eliminate excess Regexp creation due to capture counting
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy committed Nov 11, 2008
1 parent 5db9f9b commit 278b6cd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion actionpack/lib/action_controller/routing/route.rb
Expand Up @@ -219,7 +219,7 @@ def recognition_extraction
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
Expand Down
28 changes: 25 additions & 3 deletions actionpack/lib/action_controller/routing/segments.rb
Expand Up @@ -13,6 +13,10 @@ def initialize
@is_optional = false
end

def number_of_captures
Regexp.new(regexp_chunk).number_of_captures
end

def extraction_code
nil
end
Expand Down Expand Up @@ -84,6 +88,10 @@ def regexp_chunk
optional? ? Regexp.optionalize(chunk) : chunk
end

def number_of_captures
0
end

def build_pattern(pattern)
escaped = Regexp.escape(value)
if optional? && ! pattern.empty?
Expand Down Expand Up @@ -194,10 +202,16 @@ def regexp_chunk
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

Expand Down Expand Up @@ -230,6 +244,10 @@ def regexp_chunk
"(?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}"
Expand Down Expand Up @@ -275,6 +293,10 @@ def regexp_chunk
regexp || "(.*)"
end

def number_of_captures
regexp ? regexp.number_of_captures : 1
end

def optionality_implied?
true
end
Expand Down

0 comments on commit 278b6cd

Please sign in to comment.