summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorKevin Newton <kddnewton@gmail.com>2026-01-27 14:55:09 -0500
committergit <svn-admin@ruby-lang.org>2026-01-27 20:06:22 +0000
commitc983b7aee631db46002ba7438089d792c2b0298b (patch)
tree5cf1863fad7e37daac12a1f7b625f141de61f435 /lib
parent39b28e67a6363f9ffe1f478298984414083c96d4 (diff)
[ruby/prism] Rename line_to_byte_offset -> byte_offset
Also, include the column in here. Hopefully we can do some additional optimizations later. https://github.com/ruby/prism/commit/7759acdd26
Diffstat (limited to 'lib')
-rw-r--r--lib/prism/lex_compat.rb8
-rw-r--r--lib/prism/parse_result.rb14
2 files changed, 12 insertions, 10 deletions
diff --git a/lib/prism/lex_compat.rb b/lib/prism/lex_compat.rb
index 523ad39586..4960230bcf 100644
--- a/lib/prism/lex_compat.rb
+++ b/lib/prism/lex_compat.rb
@@ -816,7 +816,7 @@ module Prism
# Manually implemented instead of `sort_by!(&:location)` for performance.
tokens.sort_by! do |token|
line, column = token.location
- source.line_to_byte_offset(line) + column
+ source.byte_offset(line, column)
end
# Add :on_sp tokens
@@ -833,8 +833,10 @@ module Prism
tokens.each do |token|
line, column = token.location
- start_offset = source.line_to_byte_offset(line) + column
- # Ripper reports columns on line 1 without counting the BOM, so we adjust to get the real offset
+ start_offset = source.byte_offset(line, column)
+
+ # Ripper reports columns on line 1 without counting the BOM, so we
+ # adjust to get the real offset
start_offset += 3 if line == 1 && bom
if start_offset > prev_token_end
diff --git a/lib/prism/parse_result.rb b/lib/prism/parse_result.rb
index 12d19da562..be1c13f97c 100644
--- a/lib/prism/parse_result.rb
+++ b/lib/prism/parse_result.rb
@@ -76,13 +76,13 @@ 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]
+ # Converts the line number and column in bytes to a byte offset.
+ def byte_offset(line, column)
+ normal = line - @start_line
+ raise IndexError if normal < 0
+ offsets.fetch(normal) + column
+ rescue IndexError
+ raise ArgumentError, "line #{line} is out of range"
end
# Binary search through the offsets to find the line number for the given