Class: Rattler::Util::LineCounter

Inherits:
Object
  • Object
show all
Defined in:
lib/rattler/util/line_counter.rb

Overview

A LineCounter takes a linear index into text and calculates line and column numbers.

Constant Summary collapse

@@newline =
"\n"
@@tab =
"\t"

Instance Method Summary collapse

Constructor Details

#initialize(source, options = {}) ⇒ LineCounter

Create a LineCounter object for source.

Parameters:

  • source (String)

    the text in which to count lines

  • options (Hash) (defaults to: {})

    any optional options for the line counter

Options Hash (options):

  • :tab_size (Integer) — default: 8

    the tab size to use for calculating the column number when tab characters are encountered



21
22
23
24
# File 'lib/rattler/util/line_counter.rb', line 21

def initialize(source, options={})
  @source = source
  @tab_size = options[:tab_size] || 8
end

Instance Method Details

#column(index) ⇒ Integer

Return the column number of the character at index. When a tab character is encountered the next tab stop is used for the column number of the character following the tab character.

Parameters:

  • index (Integer)

    the (0-based) index into the text

Returns:

  • (Integer)

    the (1-based) column number at index



41
42
43
44
# File 'lib/rattler/util/line_counter.rb', line 41

def column(index)
  count(index)
  return @column
end

#line(index) ⇒ Integer

Return the line number of the character at index.

Parameters:

  • index (Integer)

    the (0-based) index into the text

Returns:

  • (Integer)

    the (1-based) line number at index



30
31
32
33
# File 'lib/rattler/util/line_counter.rb', line 30

def line(index)
  count(index)
  return @line
end