use core::fmt;
use core::ops::Range;
use crate::{ByteIndex, RawIndex};
#[cfg(feature = "serialization")]
use serde::{Deserialize, Serialize};
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))]
pub struct Span {
start: ByteIndex,
end: ByteIndex,
}
impl Span {
#[must_use]
pub fn new(start: impl Into<ByteIndex>, end: impl Into<ByteIndex>) -> Span {
let start = start.into();
let end = end.into();
assert!(end >= start);
Span { start, end }
}
#[must_use]
pub const fn initial() -> Span {
Span {
start: ByteIndex(0),
end: ByteIndex(0),
}
}
#[allow(clippy::should_implement_trait)]
#[must_use]
pub fn from_str(s: &str) -> Span {
Span::new(0, s.len() as u32)
}
#[must_use]
pub fn merge(self, other: Span) -> Span {
use core::cmp::{max, min};
let start = min(self.start, other.start);
let end = max(self.end, other.end);
Span::new(start, end)
}
#[must_use]
pub fn disjoint(self, other: Span) -> bool {
let (first, last) = if self.end < other.end {
(self, other)
} else {
(other, self)
};
first.end <= last.start
}
pub fn start(self) -> ByteIndex {
self.start
}
pub fn end(self) -> ByteIndex {
self.end
}
}
impl Default for Span {
fn default() -> Span {
Span::initial()
}
}
impl fmt::Display for Span {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"[{start}, {end})",
start = self.start(),
end = self.end(),
)
}
}
impl<I> From<Range<I>> for Span
where
I: Into<ByteIndex>,
{
fn from(range: Range<I>) -> Span {
Span::new(range.start, range.end)
}
}
impl From<Span> for Range<usize> {
fn from(span: Span) -> Range<usize> {
span.start.into()..span.end.into()
}
}
impl From<Span> for Range<RawIndex> {
fn from(span: Span) -> Range<RawIndex> {
span.start.0..span.end.0
}
}
#[cfg(test)]
mod test {
#[test]
fn test_merge() {
use super::Span;
let a = Span::from(1..5);
let b = Span::from(3..10);
assert_eq!(a.merge(b), Span::from(1..10));
assert_eq!(b.merge(a), Span::from(1..10));
let two_four = (2..4).into();
assert_eq!(a.merge(two_four), (1..5).into());
assert_eq!(two_four.merge(a), (1..5).into());
let ten_twenty = (10..20).into();
assert_eq!(a.merge(ten_twenty), (1..20).into());
assert_eq!(ten_twenty.merge(a), (1..20).into());
assert_eq!(a.merge(a), a);
}
#[test]
fn test_disjoint() {
use super::Span;
let a = Span::from(1..5);
let b = Span::from(3..10);
assert!(!a.disjoint(b));
assert!(!b.disjoint(a));
let two_four = (2..4).into();
assert!(!a.disjoint(two_four));
assert!(!two_four.disjoint(a));
let ten_twenty = (10..20).into();
assert!(a.disjoint(ten_twenty));
assert!(ten_twenty.disjoint(a));
assert!(!a.disjoint(a));
let c = Span::from(5..10);
assert!(a.disjoint(c));
assert!(c.disjoint(a));
let d = Span::from(0..1);
assert!(a.disjoint(d));
assert!(d.disjoint(a));
}
}