diff options
| author | Benoit Daloze <eregontp@gmail.com> | 2024-02-08 16:26:27 +0100 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2024-02-14 15:48:32 +0000 |
| commit | f0f6ffef4252fcc899fe2f039b910fc7613d00aa (patch) | |
| tree | 1fd688e83b79528d07cd38e55917057f4a9d43dd /lib | |
| parent | 65f54355406a25f352241406967d038fc72d4737 (diff) | |
[ruby/prism] Serialize the newline_list to avoid recomputing it again later
* Fixes https://github.com/ruby/prism/issues/2380
https://github.com/ruby/prism/commit/4eaaa90114
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/prism/parse_result.rb | 20 |
1 files changed, 5 insertions, 15 deletions
diff --git a/lib/prism/parse_result.rb b/lib/prism/parse_result.rb index 4b0c57ed4b..785d2acf35 100644 --- a/lib/prism/parse_result.rb +++ b/lib/prism/parse_result.rb @@ -12,15 +12,13 @@ module Prism attr_accessor :start_line # The list of newline byte offsets in the source code. - attr_reader :offsets + attr_accessor :offsets - # Create a new source object with the given source code and newline byte - # offsets. If no newline byte offsets are given, they will be computed from - # the source code. - def initialize(source, start_line = 1, offsets = compute_offsets(source)) + # Create a new source object with the given source code. + def initialize(source) @source = source - @start_line = start_line - @offsets = offsets + @start_line = 1 # set after parsing is done + @offsets = [] # set after parsing is done end # Perform a byteslice on the source code using the given byte offset and @@ -94,14 +92,6 @@ module Prism left - 1 end - - # Find all of the newlines in the source code and return their byte offsets - # from the start of the string an array. - def compute_offsets(code) - offsets = [0] - code.b.scan("\n") { offsets << $~.end(0) } - offsets - end end # This represents a location in the source. |
