Skip to content

Commit

Permalink
Use static regex for simple codespan results
Browse files Browse the repository at this point in the history
  • Loading branch information
ashmaroli committed May 6, 2019
1 parent 68d44ec commit 9867a3b
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/kramdown/parser/kramdown/codespan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,18 @@ def parse_codespan
return
end

if (text = @src.scan_until(/#{result}/))
text.sub!(/#{result}\Z/, '')
# assign static regex to avoid allocating the same on every instance
# where +result+ equals a single-backtick. Interpolate otherwise.
if result == '`'
scan_pattern = /`/
str_sub_pattern = /`\Z/
else
scan_pattern = /#{result}/
str_sub_pattern = /#{result}\Z/
end

if (text = @src.scan_until(scan_pattern))
text.sub!(str_sub_pattern, '')
unless simple
text = text[1..-1] if text[0..0] == ' '
text = text[0..-2] if text[-1..-1] == ' '
Expand Down

0 comments on commit 9867a3b

Please sign in to comment.