Skip to content

Commit

Permalink
Speedup Lotus::Utils::String#tokenize
Browse files Browse the repository at this point in the history
Use MatchData with its {pre,post}_match information which is twice
as fast as the previous implementation.
  • Loading branch information
splattael committed Jul 29, 2014
1 parent 8b9194f commit 520c021
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/lotus/utils/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,14 @@ def namespace
# 'Lotus::Utils'
# 'Lotus::App'
def tokenize(&blk)
template = @string.gsub(/\((.*)\)/, "%{token}")
tokens = Array(( $1 || @string ).split('|'))

tokens.each do |token|
blk.call(self.class.new(template % {token: token}))
if match = /\((.*)\)/.match(@string)
pre, post = match.pre_match, match.post_match
tokens = match[1].split('|')
tokens.each do |token|
blk.call(self.class.new("#{pre}#{token}#{post}"))
end
else
blk.call(self.class.new(@string))
end

nil
Expand Down

0 comments on commit 520c021

Please sign in to comment.