Skip to content

Commit df21150

Browse files
committed
speedup/tweak debug encoder
1 parent 108a5c9 commit df21150

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

lib/coderay/encoders/debug.rb

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,34 +25,37 @@ def initialize options = {}
2525

2626
def text_token text, kind
2727
raise 'empty token' if $CODERAY_DEBUG && text.empty?
28+
2829
if kind == :space
2930
@out << text
3031
else
31-
# TODO: Escape (
32-
text = text.gsub(/[)\\]/, '\\\\\0') if text.index(/[)\\]/)
33-
@out << kind.to_s << '(' << text << ')'
32+
text = text.gsub('\\', '\\\\\\\\') if text.index('\\')
33+
text = text.gsub(')', '\\\\)') if text.index(')')
34+
@out << "#{kind}(#{text})"
3435
end
3536
end
3637

3738
def begin_group kind
38-
@opened << kind
39-
@out << kind.to_s << '<'
39+
@opened << kind if $CODERAY_DEBUG
40+
41+
@out << "#{kind}<"
4042
end
4143

4244
def end_group kind
43-
if @opened.last != kind
44-
puts @out
45-
raise "we are inside #{@opened.inspect}, not #{kind}"
46-
end
47-
@opened.pop
45+
raise "We are inside #{@opened.inspect}, not #{kind}" if $CODERAY_DEBUG && @opened.pop != kind
46+
4847
@out << '>'
4948
end
5049

5150
def begin_line kind
52-
@out << kind.to_s << '['
51+
@opened << kind if $CODERAY_DEBUG
52+
53+
@out << "#{kind}["
5354
end
5455

5556
def end_line kind
57+
raise "We are inside #{@opened.inspect}, not #{kind}" if $CODERAY_DEBUG && @opened.pop != kind
58+
5659
@out << ']'
5760
end
5861

0 commit comments

Comments
 (0)