diff options
| author | Kevin Newton <kddnewton@gmail.com> | 2026-01-26 09:08:27 -0500 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2026-01-26 14:43:01 +0000 |
| commit | 2947aa41d4e6aa340edb0979c3e59bb6b6aca3a4 (patch) | |
| tree | c9b816e386ef6dc753f0c9b27eed09fc9515f760 | |
| parent | 5f254209184f4bcddc3155a85da7dc970f5708bf (diff) | |
[ruby/prism] Use each_line to avoid allocating array
Though very unlikely, it could potentially allocate a large array
of whitespace.
https://github.com/ruby/prism/commit/3389947819
| -rw-r--r-- | lib/prism/lex_compat.rb | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/lib/prism/lex_compat.rb b/lib/prism/lex_compat.rb index 775be2759a..523ad39586 100644 --- a/lib/prism/lex_compat.rb +++ b/lib/prism/lex_compat.rb @@ -659,13 +659,14 @@ module Prism IgnoreStateToken.new([[lineno, column], event, value, lex_state]) when :on_words_sep # Ripper emits one token each per line. - lines = value.lines - lines[0...-1].each do |whitespace| - tokens << Token.new([[lineno, column], event, whitespace, lex_state]) - lineno += 1 - column = 0 + value.each_line.with_index do |line, index| + if index > 0 + lineno += 1 + column = 0 + end + tokens << Token.new([[lineno, column], event, line, lex_state]) end - Token.new([[lineno, column], event, lines.last, lex_state]) + tokens.pop when :on_regexp_end # On regex end, Ripper scans and then sets end state, so the ripper # lexed output is begin, when it should be end. prism sets lex state |
