pub struct ByteSpan {
pub start: usize,
pub end: usize,
}Expand description
A byte-based span representing a range in source text.
ByteSpan uses byte offsets (not character or line positions) for precise
and efficient source location tracking. For LSP communication, use
WireRange or convert via LineStartsCache.
§Invariants
start <= end(enforced by constructors, but not at type level for Copy)- Both
startandendare valid byte offsets in the source text - Spans are half-open intervals:
[start, end)
§Example
use perl_position_tracking::ByteSpan;
let span = ByteSpan::new(0, 10);
assert_eq!(span.len(), 10);
assert!(!span.is_empty());
// Extract the spanned text
let source = "hello world";
let text = span.slice(source);
assert_eq!(text, "hello worl");Fields§
§start: usizeStarting byte offset in the source text (inclusive)
end: usizeEnding byte offset in the source text (exclusive)
Implementations§
Source§impl ByteSpan
impl ByteSpan
Sourcepub fn new(start: usize, end: usize) -> Self
pub fn new(start: usize, end: usize) -> Self
Creates a new ByteSpan with the given start and end offsets.
§Panics
Panics in debug mode if start > end.
Sourcepub const fn contains(&self, offset: usize) -> bool
pub const fn contains(&self, offset: usize) -> bool
Returns true if this span contains the given byte offset.
Sourcepub const fn contains_span(&self, other: ByteSpan) -> bool
pub const fn contains_span(&self, other: ByteSpan) -> bool
Returns true if this span contains the given span entirely.
Sourcepub const fn overlaps(&self, other: ByteSpan) -> bool
pub const fn overlaps(&self, other: ByteSpan) -> bool
Returns true if this span overlaps with the given span.
Sourcepub fn intersection(&self, other: ByteSpan) -> Option<ByteSpan>
pub fn intersection(&self, other: ByteSpan) -> Option<ByteSpan>
Returns the intersection of two spans, or None if they don’t overlap.
Sourcepub fn union(&self, other: ByteSpan) -> ByteSpan
pub fn union(&self, other: ByteSpan) -> ByteSpan
Returns a new span that covers both this span and the given span.
Sourcepub fn slice<'a>(&self, source: &'a str) -> &'a str
pub fn slice<'a>(&self, source: &'a str) -> &'a str
Extracts the slice of source text covered by this span.
§Panics
Panics if the span is out of bounds for the source text.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for ByteSpan
impl<'de> Deserialize<'de> for ByteSpan
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl From<ByteSpan> for Range
Convert old SourceLocation to Range (for migration)
impl From<ByteSpan> for Range
Convert old SourceLocation to Range (for migration)