pub struct Span {
pub start: usize,
pub end: usize,
}Expand description
Span type that tracks a byte range.
§Example
use sipha_core::span::Span;
let span = Span::new(10, 20);
assert_eq!(span.start(), 10);
assert_eq!(span.end(), 20);Fields§
§start: usizeStart byte offset (inclusive).
end: usizeEnd byte offset (exclusive).
Implementations§
Source§impl Span
impl Span
Sourcepub fn from_range(range: Range<usize>) -> Self
pub fn from_range(range: Range<usize>) -> Self
Create a span from a range.
Sourcepub fn contains(&self, pos: usize) -> bool
pub fn contains(&self, pos: usize) -> bool
Check if this span contains the given position.
A position is contained if it’s >= start and < end.
Sourcepub fn contains_span(&self, other: &Self) -> bool
pub fn contains_span(&self, other: &Self) -> bool
Check if this span contains another span entirely.
Sourcepub fn is_adjacent_to(&self, other: &Self) -> bool
pub fn is_adjacent_to(&self, other: &Self) -> bool
Check if this span is adjacent to another span (touching but not overlapping).
Sourcepub fn join(self, other: Self) -> Option<Self>
pub fn join(self, other: Self) -> Option<Self>
Join two adjacent or overlapping spans into one.
Returns None if the spans are not adjacent/overlapping.
Sourcepub fn merge(self, other: Self) -> Self
pub fn merge(self, other: Self) -> Self
Merge two spans into one, creating the smallest span that contains both.
Sourcepub fn intersection(self, other: Self) -> Option<Self>
pub fn intersection(self, other: Self) -> Option<Self>
Get the intersection of two spans, if they overlap.
Returns None if the spans don’t overlap.
Sourcepub fn before(&self, start_pos: usize) -> Option<Self>
pub fn before(&self, start_pos: usize) -> Option<Self>
Get the span that comes before this span, from start_pos to this span’s start.
Returns None if start_pos >= self.start().
§Example
use sipha_core::span::Span;
let span = Span::new(10, 20);
let before = span.before(5);
assert_eq!(before, Some(Span::new(5, 10)));Sourcepub fn after(&self, end_pos: usize) -> Option<Self>
pub fn after(&self, end_pos: usize) -> Option<Self>
Get the span that comes after this span, from this span’s end to end_pos.
Returns None if end_pos <= self.end().
§Example
use sipha_core::span::Span;
let span = Span::new(10, 20);
let after = span.after(25);
assert_eq!(after, Some(Span::new(20, 25)));Sourcepub fn expand_to_include(&mut self, other: &Self)
pub fn expand_to_include(&mut self, other: &Self)
Expand this span to include another span.
This is equivalent to self.merge(other) but modifies self in place.
§Example
use sipha_core::span::Span;
let mut span = Span::new(10, 20);
span.expand_to_include(&Span::new(25, 30));
assert_eq!(span, Span::new(10, 30));Sourcepub fn split_at(&self, pos: usize) -> Option<(Self, Self)>
pub fn split_at(&self, pos: usize) -> Option<(Self, Self)>
Split this span at the given position, returning two spans.
Returns (before, after) where:
beforeis the span fromself.start()topos(exclusive)afteris the span frompostoself.end()
Returns None if pos is not within this span.
§Example
use sipha_core::span::Span;
let span = Span::new(10, 20);
let (before, after) = span.split_at(15).unwrap();
assert_eq!(before, Span::new(10, 15));
assert_eq!(after, Span::new(15, 20));