Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
Prism::Source#find_line
find_line(byte_offset)
lib/prism/parse_result.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.4.1/lib/prism/parse_result.rb, line 139
def find_line(byte_offset)
left = 0
right = offsets.length - 1
while left <= right
mid = left + (right - left) / 2
return mid if (offset = offsets[mid]) == byte_offset
if offset < byte_offset
left = mid + 1
else
right = mid - 1
end
end
left - 1
end
Binary search through the offsets to find the line number for the given byte offset.