Class: Prism::CodeUnitsCache

Inherits:
Object
  • Object
show all
Defined in:
lib/prism/parse_result.rb,
sig/generated/prism/parse_result.rbs

Overview

A cache that can be used to quickly compute code unit offsets from byte offsets. It purposefully provides only a single #[] method to access the cache in order to minimize surface area.

Note that there are some known issues here that may or may not be addressed in the future:

  • The first is that there are issues when the cache computes values that are not on character boundaries. This can result in subsequent computations being off by one or more code units.
  • The second is that this cache is currently unbounded. In theory we could introduce some kind of LRU cache to limit the number of entries, but this has not yet been implemented.

Instance Method Summary collapse

Constructor Details

#initialize(source, encoding) ⇒ CodeUnitsCache

Initialize a new cache with the given source and encoding.

: (String source, Encoding encoding) -> void

Parameters:

  • source (String)
  • encoding (Encoding)


361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/prism/parse_result.rb', line 361

def initialize(source, encoding)
  @source = source
  @counter =
    case encoding
    when Encoding::UTF_8
      UTF8Counter.new
    when Encoding::UTF_16LE, Encoding::UTF_16BE
      UTF16Counter.new(source, encoding)
    else
      UTF32Counter.new(source, encoding)
    end

  @cache = {} #: Hash[Integer, Integer]
  @offsets = [] #: Array[Integer]
end

Instance Method Details

#[](byte_offset) ⇒ Integer

Retrieve the code units offset from the given byte offset.

: (Integer byte_offset) -> Integer

Parameters:

  • byte_offset (Integer)

Returns:

  • (Integer)


380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/prism/parse_result.rb', line 380

def [](byte_offset)
  @cache[byte_offset] ||=
    if (index = @offsets.bsearch_index { |offset| offset > byte_offset }).nil?
      @offsets << byte_offset
      @counter.count(0, byte_offset)
    elsif index == 0
      @offsets.unshift(byte_offset)
      @counter.count(0, byte_offset)
    else
      @offsets.insert(index, byte_offset)
      offset = @offsets[index - 1]
      @cache[offset] + @counter.count(offset, byte_offset - offset)
    end
end