Class: Prism::ASCIISource

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

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

#checksum, #comment_attachments, #comment_ranges, #comments, #source, #start_line

Instance Method Summary collapse

Methods inherited from Source

#byte_offset, #column, #comments_for, #deep_freeze, #encoding, #find_line, for, #initialize, #line, #line_end, #line_start, #lines, #offsets, #replace_checksum, #replace_offsets, #replace_start_line, #slice

Constructor Details

This class inherits a constructor from Prism::Source

Instance Method Details

#character_column(byte_offset) ⇒ Integer

Return the column in characters for the given byte offset.

: (Integer byte_offset) -> Integer

Parameters:

  • byte_offset (Integer)

Returns:

  • (Integer)


415
416
417
# File 'lib/prism/parse_result.rb', line 415

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

#character_offset(byte_offset) ⇒ Integer

Return the character offset for the given byte offset.

: (Integer byte_offset) -> Integer

Parameters:

  • byte_offset (Integer)

Returns:

  • (Integer)


408
409
410
# File 'lib/prism/parse_result.rb', line 408

def character_offset(byte_offset)
  byte_offset
end

#code_units_cache(encoding) ⇒ _CodeUnitsCache

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.

: (Encoding encoding) -> _CodeUnitsCache

Parameters:

  • encoding (Encoding)

Returns:



436
437
438
# File 'lib/prism/parse_result.rb', line 436

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

#code_units_column(byte_offset, encoding) ⇒ Integer

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.

: (Integer byte_offset, Encoding encoding) -> Integer

Parameters:

  • byte_offset (Integer)
  • encoding (Encoding)

Returns:

  • (Integer)


445
446
447
# File 'lib/prism/parse_result.rb', line 445

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

#code_units_offset(byte_offset, encoding) ⇒ Integer

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.

: (Integer byte_offset, Encoding encoding) -> Integer

Parameters:

  • byte_offset (Integer)
  • encoding (Encoding)

Returns:

  • (Integer)


427
428
429
# File 'lib/prism/parse_result.rb', line 427

def code_units_offset(byte_offset, encoding)
  byte_offset
end