Skip to content

Commit

Permalink
Merge pull request #41 from korny/speedup
Browse files Browse the repository at this point in the history
Performance improvements
  • Loading branch information
jneen committed Oct 21, 2012
2 parents 37d3d8a + cda8bfc commit 9343724
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/rouge/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def option(k, v=:absent)
# @example
# debug { "hello, world!" }
def debug(&b)
puts(b.call) if option :debug
puts(b.call) if defined?(@debug) ? @debug : @debug ||= option(:debug)
end

# @abstract
Expand Down
16 changes: 15 additions & 1 deletion lib/rouge/regex_lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def scan(scanner, re, &b)
# StringScanner's implementation of ^ is b0rken.
# TODO: this doesn't cover cases like /(a|^b)/, but it's
# the most common, for now...
return false if re.source[0] == ?^ && !scanner.beginning_of_line?
return false if re.beginning_of_line? && !scanner.beginning_of_line?

@null_steps ||= 0

Expand Down Expand Up @@ -398,3 +398,17 @@ def with_output_stream(output_stream, &b)
end
end
end

class Regexp
# Does this expression start with a ^?
#
# Since Regexps are immuntable, this method is cached to avoid
# calling Regexp#source more than once.
def beginning_of_line?
if defined? @beginning_of_line
@beginning_of_line
else
@beginning_of_line = source[0] == ?^
end
end
end

0 comments on commit 9343724

Please sign in to comment.