Class: Prism::ASCIISource

Inherits:
Source
  • Object
show all
Defined in:
lib/prism/parse_result.rb

Overview

Specialized version of Prism::Source for source code that includes ASCII characters only. This class is used to apply performance optimizations that cannot be applied to sources that include multibyte characters.

In the extremely rare case that a source includes multi-byte characters but is marked as binary because of a magic encoding comment and it cannot be eagerly converted to UTF-8, this class will be used as well. This is because at that point we will treat everything as single-byte characters.

Instance Attribute Summary

Attributes inherited from Source

#offsets, #source, #start_line

Instance Method Summary collapse

Methods inherited from Source

#column, #deep_freeze, #encoding, for, #initialize, #line, #line_end, #line_start, #lines, #replace_offsets, #replace_start_line, #slice

Constructor Details

This class inherits a constructor from Prism::Source

Instance Method Details

#character_column(byte_offset) ⇒ Object

Return the column number in characters for the given byte offset.



261
262
263
# File 'lib/prism/parse_result.rb', line 261

def character_column(byte_offset)
  byte_offset - line_start(byte_offset)
end

#character_offset(byte_offset) ⇒ Object

Return the character offset for the given byte offset.



256
257
258
# File 'lib/prism/parse_result.rb', line 256

def character_offset(byte_offset)
  byte_offset
end

#code_units_cache(encoding) ⇒ Object

Returns a cache that is the identity function in order to maintain the same interface. We can do this because code units are always equivalent to byte offsets for ASCII-only sources.



278
279
280
# File 'lib/prism/parse_result.rb', line 278

def code_units_cache(encoding)
  ->(byte_offset) { byte_offset }
end

#code_units_column(byte_offset, encoding) ⇒ Object

Specialized version of ‘code_units_column` that does not depend on `code_units_offset`, which is a more expensive operation. This is essentially the same as `Prism::Source#column`.



285
286
287
# File 'lib/prism/parse_result.rb', line 285

def code_units_column(byte_offset, encoding)
  byte_offset - line_start(byte_offset)
end

#code_units_offset(byte_offset, encoding) ⇒ Object

Returns the offset from the start of the file for the given byte offset counting in code units for the given encoding.

This method is tested with UTF-8, UTF-16, and UTF-32. If there is the concept of code units that differs from the number of characters in other encodings, it is not captured here.



271
272
273
# File 'lib/prism/parse_result.rb', line 271

def code_units_offset(byte_offset, encoding)
  byte_offset
end