Class: Parslet::Source::LineCache

### Home Classes Methods

### In Files

- lib/parslet/source/line_cache.rb

### Parent

Object

### Methods

- ::new
- #line_and_column
- #scan_for_line_endings

### Files

- README

### Class/Module Index

Quicksearch

- Parslet
- Parslet::Atoms
- Parslet::Atoms::Alternative
- Parslet::Atoms::Base
- Parslet::Atoms::CanFlatten
- Parslet::Atoms::Capture
- Parslet::Atoms::Context
- Parslet::Atoms::DSL
- Parslet::Atoms::Dynamic
- Parslet::Atoms::Entity
- Parslet::Atoms::Lookahead
- Parslet::Atoms::Named
- Parslet::Atoms::Precedence
- Parslet::Atoms::Re
- Parslet::Atoms::Repetition
- Parslet::Atoms::Scope
- Parslet::Atoms::Sequence
- Parslet::Atoms::Str
- Parslet::Cause
- Parslet::ClassMethods
- Parslet::DelayedMatchConstructor
- Parslet::ErrorReporter
- Parslet::ErrorReporter::Deepest
- Parslet::ErrorReporter::Tree
- Parslet::Expression
- Parslet::Expression::Treetop
- Parslet::Expression::Treetop::Parser
- Parslet::Expression::Treetop::Transform
- Parslet::ParseFailed
- Parslet::Parser
- Parslet::Parser::PrettyPrinter
- Parslet::Parser::Visitors
- Parslet::Parser::Visitors::Citrus
- Parslet::Parser::Visitors::Treetop
- Parslet::Pattern
- Parslet::Pattern::SequenceBind
- Parslet::Pattern::SimpleBind
- Parslet::Pattern::SubtreeBind
- Parslet::Scope
- Parslet::Scope::Binding
- Parslet::Scope::NotFound
- Parslet::Slice
- Parslet::Source
- Parslet::Source::LineCache
- Parslet::Source::RangeSearch
- Parslet::Transform
- Parslet::Transform::Context

No matching classes.

# Parslet::Source::LineCache

A cache for line start positions.

### Public Class Methods

new() click to toggle source

    # File lib/parslet/source/line_cache.rb, line 7
    def initialize
      # Stores line endings as a simple position number. The first line always
      # starts at 0; numbers beyond the biggest entry are on any line > size,
      # but probably make a scan to that position neccessary.
      @line_ends = []
      @line_ends.extend RangeSearch
    end

### Public Instance Methods

line_and_column(pos) click to toggle source

Returns a <line, column> tuple for the given input position.

    # File lib/parslet/source/line_cache.rb, line 17
    def line_and_column(pos)
      eol_idx = @line_ends.lbound(pos)

      if eol_idx
        # eol_idx points to the offset that ends the current line.
        # Let's try to find the offset that starts it:
        offset = eol_idx>0 && @line_ends[eol_idx-1] || 0
        return [eol_idx+1, pos-offset+1]
      else
        # eol_idx is nil, that means that we're beyond the last line end that
        # we know about. Pretend for now that we're just on the last line.
        offset = @line_ends.last || 0
        return [@line_ends.size+1, pos-offset+1]
      end
    end

scan_for_line_endings(start_pos, buf) click to toggle source

    # File lib/parslet/source/line_cache.rb, line 33
    def scan_for_line_endings(start_pos, buf)
      return unless buf
      return unless buf.index("\n")
      cur = -1

      # If we have already read part or all of buf, we already know about
      # line ends in that portion. remove it and correct cur (search index)
      if @last_line_end && start_pos < @last_line_end
        # Let's not search the range from start_pos to last_line_end again.
        cur = @last_line_end - start_pos -1
      end

      # Scan the string for line endings; store the positions of all endings
      # in @line_ends.
      while buf && cur = buf.index("\n", cur+1)
        @last_line_end = (start_pos + cur+1)
        @line_ends << @last_line_end
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.