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.

Author:

  • Jason Arhart

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

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

Create a Rattler::Util::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



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

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



50
51
52
53
# File 'lib/rattler/util/line_counter.rb', line 50

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



39
40
41
42
# File 'lib/rattler/util/line_counter.rb', line 39

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