Files
Doughlang/src/span.rs

43 lines
1.0 KiB
Rust

use std::ops::Range;
/// Stores the start and end byte position
#[derive(Clone, Copy, Default, PartialEq, Eq)]
pub struct Span {
pub head: u32,
pub tail: u32,
}
impl std::fmt::Debug for Span {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { head, tail } = self;
write!(f, "[{head}:{tail}]")
}
}
#[expect(non_snake_case)]
/// Stores the start and end byte position
pub const fn Span(head: u32, tail: u32) -> Span {
Span { head, tail }
}
impl Span {
/// Updates `self` to include all but the last byte in `other`
pub fn merge(self, other: Span) -> Span {
Span { head: self.head.min(other.head), tail: self.tail.max(other.head) }
}
}
impl From<Span> for Range<usize> {
fn from(value: Span) -> Self {
let Span { head, tail } = value;
(head as usize)..(tail as usize)
}
}
impl std::fmt::Display for Span {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { head, tail } = self;
write!(f, "{head}:{tail}")
}
}