Class: Prism::Translation::Ripper::LineAndColumnCache
- Inherits:
-
Object
- Object
- Prism::Translation::Ripper::LineAndColumnCache
- Defined in:
- lib/prism/translation/ripper.rb,
sig/generated/prism/lex_compat.rbs
Overview
Provides optimized access to line and column information. Ripper bounds are mostly accessed in a linear fashion, so we can try a linear scan first and fall back to binary search.
Instance Method Summary collapse
-
#initialize(source) ⇒ LineAndColumnCache
constructor
: (Source source) -> void.
-
#line_and_column(byte_offset) ⇒ [ Integer, Integer ]
: (Integer byte_offset) -> [Integer, Integer].
Constructor Details
#initialize(source) ⇒ LineAndColumnCache
: (Source source) -> void
458 459 460 461 462 |
# File 'lib/prism/translation/ripper.rb', line 458 def initialize(source) @source = source @offsets = source.offsets @hint = 0 end |
Instance Method Details
#line_and_column(byte_offset) ⇒ [ Integer, Integer ]
: (Integer byte_offset) -> [Integer, Integer]
465 466 467 468 |
# File 'lib/prism/translation/ripper.rb', line 465 def line_and_column(byte_offset) @hint = new_hint(byte_offset) || @source.find_line(byte_offset) return [@hint + @source.start_line, byte_offset - @offsets[@hint]] end |