pub struct Span { /* private fields */ }Expand description
A span is a range of positions in the input.
A span is composed of a start and an end position. The start position is included in the span but the end position is not. The end position is the position of the first character after the fragment.
If the span is empty, the start and end positions are the same. The span contains no character.
Implementations§
Source§impl Span
impl Span
Sourcepub fn new(start: impl Into<Position>, end: impl Into<Position>) -> Self
pub fn new(start: impl Into<Position>, end: impl Into<Position>) -> Self
Creates a new span from the start and end positions. The start position must be less than or equal to the end position.
§Panics
This function panics if the start position is greater than the end position.
§Examples
let start = Position::new(0, 1, 1);
let end = Position::new(3, 1, 4);
let span = Span::new(start, end);
assert_eq!(span.start(), start);
assert_eq!(span.end(), end);Sourcepub fn new_at(pos: impl Into<Position>) -> Self
pub fn new_at(pos: impl Into<Position>) -> Self
Creates a new span at the given position. The start and end positions are the same.
§Examples
let pos = Position::new(0, 1, 1);
let span = Span::new_at(pos);
assert_eq!(span.start(), pos);
assert_eq!(span.end(), pos);Sourcepub const fn offset(&self) -> usize
pub const fn offset(&self) -> usize
Returns the byte offset of the start of the fragment relatively to the input. It starts at 0.
Sourcepub const fn line(&self) -> usize
pub const fn line(&self) -> usize
Returns the line number on which the span starts. It starts at 1.
Sourcepub const fn column(&self) -> usize
pub const fn column(&self) -> usize
Returns the charcater (utf-8 encoded) column the span start. It starts at 1.
Sourcepub fn line_str<'a>(&self, source: &'a str) -> &'a str
pub fn line_str<'a>(&self, source: &'a str) -> &'a str
Returns the first line that contains the fragment. The source must be
the original string the span was created from otherwise an arbitrarily
string will be returned.
§Examples
let pos = Position::new(0, 1, 1);
let span = Span::new_at(pos);
assert_eq!(span.line_str("abc"), "abc");
let pos = Position::new(5, 2, 2);
let span = Span::new_at(pos);
assert_eq!(span.line_str("abc\ndef"), "def");Sourcepub fn fragment<'a>(&self, source: &'a str) -> &'a str
pub fn fragment<'a>(&self, source: &'a str) -> &'a str
Returns the spanned fragement in the given source. The source must be
the original string the span was created from otherwise an arbitrarily
string will be returned.
§Examples
let start = Position::new(0, 1, 1);
let end = Position::new(2, 1, 3);
let span = Span::new(start, end);
assert_eq!(span.fragment("abc"), "ab");