pub struct Span {
pub offset: usize,
pub len: usize,
}Expand description
Represents a span in the SourceMap, bounded by an offset and a len
§About offset
The offset of this Span is an absolute offset inside the SourceMap
It doesn’t represent the offset relative to the file it was created from.
This is done to avoid havind an additional field to identify the source file.
To get the base offset of a Span you can use the
SourceMap::get_base_offset_of_span function
Fields§
§offset: usizeOffset of the span
len: usizeLength of the span
Implementations§
Source§impl Span
impl Span
pub const fn dummy() -> Span
Sourcepub const fn join(&self, other: &Span) -> Span
pub const fn join(&self, other: &Span) -> Span
Joins two spans together. Returns the smallest Span that covers both.
Sourcepub fn slice<'a>(&self, base_offset: usize, src: &'a str) -> &'a str
pub fn slice<'a>(&self, base_offset: usize, src: &'a str) -> &'a str
Slices the given string with this span
base_offset is the base offset of the corresponding
SourceFile for this Span
You can also use the SourceMap::slice function
See: about offset
§Examples
Simple example with a single source file
use span::Span;
let src = "abcdefg";
let span = Span {
offset: 1,
len: 5,
};
let slice = span.slice(0, src);
assert_eq!("bcdef", slice);Example with multiple source files
use span::{Span, source::SourceMap};
let src1 = "abcdefghi";
let src2 = "abcdefhij";
let mut source = SourceMap::default();
let offset1 = source.add_file_annon(src1.into()).offset();
let offset2 = source.add_file_annon(src2.into()).offset();
let span1 = Span {
offset: offset1 + 3,
len: 4,
};
let span2 = Span {
offset: offset2 + 4,
len: 2,
};
let slice1 = span1.slice(offset1, src1);
let slice2 = span2.slice(offset2, src1);
assert_eq!(slice1, "defg");
assert_eq!(slice2, "ef");
Sourcepub fn file_position(&self, base_offset: usize, src: &str) -> FilePosition
pub fn file_position(&self, base_offset: usize, src: &str) -> FilePosition
Gets the file position of this span in the given string slice
base_offset is the base offset of the corresponding
SourceFile for this Span
You can also use the SourceMap::file_position function
See: about offset
Sourcepub const fn end_offset(&self) -> usize
pub const fn end_offset(&self) -> usize
Returns the end offset of the span. This is, the offset of the span plus it’s length