Class: SyntaxTree::MultiByteString
- Inherits:
-
Object
- Object
- SyntaxTree::MultiByteString
- Defined in:
- lib/syntax_tree.rb
Overview
Represents a line in the source. If this class is being used, it means that there are characters in the string that are multi-byte, so we will build up an array of indices, such that array will be equal to the index of the character within the string.
Instance Attribute Summary collapse
-
#indices ⇒ Object
readonly
Returns the value of attribute indices.
-
#start ⇒ Object
readonly
Returns the value of attribute start.
Instance Method Summary collapse
-
#[](byteindex) ⇒ Object
Technically it’s possible for the column index to be a negative value if there’s a BOM at the beginning of the file, which is the reason we need to compare it to 0 here.
-
#initialize(start, line) ⇒ MultiByteString
constructor
A new instance of MultiByteString.
Constructor Details
#initialize(start, line) ⇒ MultiByteString
Returns a new instance of MultiByteString.
47 48 49 50 51 52 53 54 |
# File 'lib/syntax_tree.rb', line 47 def initialize(start, line) @start = start @indices = [] line.each_char.with_index(start) do |char, index| char.bytesize.times { @indices << index } end end |
Instance Attribute Details
#indices ⇒ Object (readonly)
Returns the value of attribute indices.
45 46 47 |
# File 'lib/syntax_tree.rb', line 45 def indices @indices end |
#start ⇒ Object (readonly)
Returns the value of attribute start.
45 46 47 |
# File 'lib/syntax_tree.rb', line 45 def start @start end |
Instance Method Details
#[](byteindex) ⇒ Object
Technically it’s possible for the column index to be a negative value if there’s a BOM at the beginning of the file, which is the reason we need to compare it to 0 here.
59 60 61 |
# File 'lib/syntax_tree.rb', line 59 def [](byteindex) indices[byteindex < 0 ? 0 : byteindex] end |