use crate::*;
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Span {
pub start: usize,
pub end: usize,
}
impl Span {
pub const fn new(start: usize, end: usize) -> Self {
assert!(start <= end);
Self { start, end }
}
pub fn merge(self, rhs: Span) -> Self {
let start = self.start.min(rhs.start);
let end = self.end.max(rhs.end);
Span::new(start, end)
}
pub fn subset(self, rhs: Span) -> Self {
let start = self.start.max(rhs.start);
let end = self.end.min(rhs.end);
assert!(start < end, "those two span have no subset");
Span::new(start, end)
}
pub const fn len(&self) -> usize {
self.end - self.start
}
pub const fn is_empty(&self) -> bool {
self.len() == 0
}
}
impl std::ops::Add for Span {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
self.merge(rhs)
}
}
impl std::ops::Mul for Span {
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
self.subset(rhs)
}
}
impl std::fmt::Debug for Span {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "@{}..{}", self.start, self.end)
}
}
impl WithSpan for Span {
fn get_span(&self) -> Span {
*self
}
}