Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance improvements #41

Merged
merged 2 commits into from
Oct 21, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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