class

Noir::CharOffsets

Inherits Reference < Object

Char <-> byte offsets and line numbers for one string, built in one pass.

Analyzers that keep CHAR indices (to agree with the char-indexed masking lexers) pay O(offset) for every String#[], String#match(re, pos) and MatchData#begin/#end once the string holds a single multi-byte char, and Analyzer#line_number_for_index is O(offset) even on ASCII. Done per route that is quadratic in the route count. These forms keep the char semantics at O(1) / O(log n) per call.

Constructors

new(content : String)
Source

Instance methods

ascii?

True when every char is one byte, so char and byte offsets coincide.

Source
ascii_whitespace?(char_pos : Int32) : Bool

Whether the char at char_pos is ASCII whitespace (false past the end).

Source
begin(match : Regex::MatchData, n : Int32 = 0) : Int32

match.begin(n) / match.end(n) as char indices.

Source
byte(char_pos : Int32) : Int32

Byte offset of char index char_pos.

Source
char(byte_pos : Int32) : Int32

Char index of byte offset byte_pos (a char boundary).

Source
char_at(char_pos : Int32) : Char | Nil

content[char_pos]?.

Source
chars

content.chars, built on first use, for char-array scanners.

Source
content
Source
end(match : Regex::MatchData, n : Int32 = 0) : Int32
Source
index(regex : Regex, char_pos : Int32, options : Regex::MatchOptions = Regex::MatchOptions::None) : Int32 | Nil

content.index(regex, char_pos).

Source
index(char : Char, char_pos : Int32) : Int32 | Nil

content.index(char, char_pos) for an ASCII char.

Source
line(char_pos : Int32) : Int32

1-based line of char_pos; same answer as Analyzer#line_number_for_index.

Source
match(regex : Regex, char_pos : Int32, options : Regex::MatchOptions = Regex::MatchOptions::None) : Regex::MatchData | Nil

content.match(regex, char_pos).

Source
slice(from : Int32, to : Int32) : String

content[from...to].

Source