From 859920dfd272bc22670d779129a04cdf07632192 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Mon, 15 Dec 2025 10:46:24 +0100 Subject: [ruby/prism] Add Prism::Source#line_to_byte_offset and replace direct accesses to offsets https://github.com/ruby/prism/commit/ff81a29ba5 --- lib/prism/parse_result.rb | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'lib') 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) -- cgit v1.2.3