Skip to content

Commit 2947aa4

Browse files
kddnewtonmatzbot
authored andcommitted
[ruby/prism] Use each_line to avoid allocating array
Though very unlikely, it could potentially allocate a large array of whitespace. ruby/prism@3389947819
1 parent 5f25420 commit 2947aa4

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

lib/prism/lex_compat.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -659,13 +659,14 @@ def result
659659
IgnoreStateToken.new([[lineno, column], event, value, lex_state])
660660
when :on_words_sep
661661
# Ripper emits one token each per line.
662-
lines = value.lines
663-
lines[0...-1].each do |whitespace|
664-
tokens << Token.new([[lineno, column], event, whitespace, lex_state])
665-
lineno += 1
666-
column = 0
662+
value.each_line.with_index do |line, index|
663+
if index > 0
664+
lineno += 1
665+
column = 0
666+
end
667+
tokens << Token.new([[lineno, column], event, line, lex_state])
667668
end
668-
Token.new([[lineno, column], event, lines.last, lex_state])
669+
tokens.pop
669670
when :on_regexp_end
670671
# On regex end, Ripper scans and then sets end state, so the ripper
671672
# lexed output is begin, when it should be end. prism sets lex state

0 commit comments

Comments
 (0)