Class: Kramdown::Utils::StringScanner

### Home Classes Methods

### In Files

- lib/kramdown/utils/string_scanner.rb

### Parent

StringScanner

### Methods

- ::new
- #current_line_number
- #pos=
- #revert_pos
- #save_pos

### Class/Module Index

Quicksearch

- Kramdown
- Kramdown::Converter
- Kramdown::Converter::Base
- Kramdown::Converter::Html
- Kramdown::Converter::Kramdown
- Kramdown::Converter::Latex
- Kramdown::Converter::MathEngine
- Kramdown::Converter::MathEngine::Itex2MML
- Kramdown::Converter::MathEngine::Mathjax
- Kramdown::Converter::MathEngine::Ritex
- Kramdown::Converter::RemoveHtmlTags
- Kramdown::Converter::SyntaxHighlighter
- Kramdown::Converter::SyntaxHighlighter::Coderay
- Kramdown::Converter::SyntaxHighlighter::Rouge
- Kramdown::Converter::Toc
- Kramdown::Document
- Kramdown::Element
- Kramdown::Error
- Kramdown::Options
- Kramdown::Options::Boolean
- Kramdown::Parser
- Kramdown::Parser::Base
- Kramdown::Parser::GFM
- Kramdown::Parser::Html
- Kramdown::Parser::Html::Constants
- Kramdown::Parser::Html::ElementConverter
- Kramdown::Parser::Html::Parser
- Kramdown::Parser::Kramdown
- Kramdown::Parser::Markdown
- Kramdown::Utils
- Kramdown::Utils::Configurable
- Kramdown::Utils::Entities
- Kramdown::Utils::Entities::Entity
- Kramdown::Utils::Html
- Kramdown::Utils::OrderedHash
- Kramdown::Utils::StringScanner
- Kramdown::Utils::Unidecoder

No matching classes.

# Kramdown::Utils::StringScanner

This patched StringScanner adds line number information for current scan position and a start_line_number override for nested StringScanners.

### Attributes

start_line_number[R]

The start line number. Used for nested StringScanners that scan a sub-string of the source document. The kramdown parser uses this, e.g., for span level parsers.

### Public Class Methods

new(string, start_line_number = 1) click to toggle source

Takes the start line number as optional second argument.

Note: The original second argument is no longer used so this should be safe.

    # File lib/kramdown/utils/string_scanner.rb, line 18
    def initialize(string, start_line_number = 1)
      super(string)
      @start_line_number = start_line_number || 1
      @previous_pos = 0
      @previous_line_number = @start_line_number
    end

### Public Instance Methods

current_line_number() click to toggle source

Returns the line number for current charpos.

NOTE: Requires that all line endings are normalized to 'n'

NOTE: Normally we'd have to add one to the count of newlines to get the correct line number. However we add the one indirectly by using a one-based start_line_number.

    # File lib/kramdown/utils/string_scanner.rb, line 59
    def current_line_number
      # Not using string[@previous_pos..best_pos].count('\n') because it is slower
      strscan = ::StringScanner.new(string)
      strscan.pos = @previous_pos
      old_pos = pos + 1
      @previous_line_number += 1 while strscan.skip_until(/\n/) && strscan.pos <= old_pos

      @previous_pos = (eos? ? pos : pos + 1)
      @previous_line_number
    end

pos=(pos) click to toggle source

Sets the byte position of the scan pointer.

Note: This also resets some internal variables, so always use pos= when setting the position and don't use any other method for that!

    # File lib/kramdown/utils/string_scanner.rb, line 29
    def pos=(pos)
      if self.pos > pos
        @previous_line_number = @start_line_number
        @previous_pos = 0
      end
      super
    end

revert_pos(data) click to toggle source

Revert the position to one saved by save_pos.

    # File lib/kramdown/utils/string_scanner.rb, line 48
    def revert_pos(data)
      self.pos = data[0]
      @previous_pos, @previous_line_number = data[1], data[2]
    end

save_pos() click to toggle source

Return information needed to revert the byte position of the string scanner in a performant way.

The returned data can be fed to revert_pos to revert the position to the saved one.

Note: Just saving pos won't be enough.

    # File lib/kramdown/utils/string_scanner.rb, line 43
    def save_pos
      [pos, @previous_pos, @previous_line_number]
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.