class Puppet::Pops::Parser::Locator::Locator19

This implementation is for Ruby19 and Ruby20. It uses byteslice to get strings from byte based offsets. For Ruby20 this is faster than using the Stringscanner.charpos method (byteslice outperforms it, when strings are frozen).

Public Instance Methods

char_length(offset, end_offset) click to toggle source

Returns the length measured in number of characters from the given start and end byte offseta

# File lib/puppet/pops/parser/locator.rb, line 287
def char_length(offset, end_offset)
  string.byteslice(offset, end_offset - offset).length
end
char_offset(byte_offset) click to toggle source

Returns the character offset for a given byte offset Ruby 19 is multibyte but has no character position methods, must use byteslice

# File lib/puppet/pops/parser/locator.rb, line 282
def char_offset(byte_offset)
  string.byteslice(0, byte_offset).length
end
offset_on_line(offset) click to toggle source

Returns the offset on line (first offset on a line is 0). Ruby 19 is multibyte but has no character position methods, must use byteslice

# File lib/puppet/pops/parser/locator.rb, line 275
def offset_on_line(offset)
  line_offset = line_index[ line_for_offset(offset)-1 ]
  string.byteslice(line_offset, offset-line_offset).length
end