diff options
| author | Benoit Daloze <eregontp@gmail.com> | 2025-12-15 10:46:24 +0100 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2026-01-19 12:03:33 +0000 |
| commit | 859920dfd272bc22670d779129a04cdf07632192 (patch) | |
| tree | 791b5fa3e21a8e788d3930aa651a0a95b526e28e /lib | |
| parent | ae5efb55d1bd52e3a08c9ffb6ab4e0cef74cef12 (diff) | |
[ruby/prism] Add Prism::Source#line_to_byte_offset and replace direct accesses to offsets
https://github.com/ruby/prism/commit/ff81a29ba5
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/prism/parse_result.rb | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/lib/prism/parse_result.rb b/lib/prism/parse_result.rb index 3570af136a..12d19da562 100644 --- a/lib/prism/parse_result.rb +++ b/lib/prism/parse_result.rb @@ -76,6 +76,15 @@ module Prism source.byteslice(byte_offset, length) or raise end + # Converts the line number to a byte offset corresponding to the start of that line + def line_to_byte_offset(line) + l = line - @start_line + if l < 0 || l >= offsets.size + raise ArgumentError, "line #{line} is out of range" + end + offsets[l] + end + # Binary search through the offsets to find the line number for the given # byte offset. def line(byte_offset) |
