use std::fmt::{self, Debug, Formatter};
use std::num::{NonZeroU16, NonZeroU64};
use std::ops::Range;
use ecow::EcoString;
use crate::FileId;
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct Span(NonZeroU64);
impl Span {
pub(crate) const FULL: Range<u64> = 2..(1 << 47);
const DETACHED: u64 = 1;
const NUMBER_BITS: usize = 48;
const FILE_ID_SHIFT: usize = Self::NUMBER_BITS;
const NUMBER_MASK: u64 = (1 << Self::NUMBER_BITS) - 1;
const RANGE_BASE: u64 = Self::FULL.end;
const RANGE_PART_BITS: usize = 23;
const RANGE_PART_SHIFT: usize = Self::RANGE_PART_BITS;
const RANGE_PART_MASK: u64 = (1 << Self::RANGE_PART_BITS) - 1;
pub const fn detached() -> Self {
Self(NonZeroU64::new(Self::DETACHED).unwrap())
}
pub(crate) const fn from_number(id: FileId, number: u64) -> Option<Self> {
if number < Self::FULL.start || number >= Self::FULL.end {
return None;
}
Some(Self::pack(id, number))
}
pub const fn from_range(id: FileId, range: Range<usize>) -> Self {
let max = 1 << Self::RANGE_PART_BITS;
let start = if range.start > max { max } else { range.start } as u64;
let end = if range.end > max { max } else { range.end } as u64;
let number = (start << Self::RANGE_PART_SHIFT) | end;
Self::pack(id, Self::RANGE_BASE + number)
}
pub const fn from_raw(v: NonZeroU64) -> Self {
Self(v)
}
const fn pack(id: FileId, low: u64) -> Self {
let bits = ((id.into_raw().get() as u64) << Self::FILE_ID_SHIFT) | low;
Self(NonZeroU64::new(bits).unwrap())
}
pub const fn is_detached(self) -> bool {
self.0.get() == Self::DETACHED
}
pub const fn id(self) -> Option<FileId> {
match NonZeroU16::new((self.0.get() >> Self::FILE_ID_SHIFT) as u16) {
Some(v) => Some(FileId::from_raw(v)),
None => None,
}
}
pub(crate) const fn number(self) -> u64 {
self.0.get() & Self::NUMBER_MASK
}
pub const fn range(self) -> Option<Range<usize>> {
let Some(number) = self.number().checked_sub(Self::RANGE_BASE) else {
return None;
};
let start = (number >> Self::RANGE_PART_SHIFT) as usize;
let end = (number & Self::RANGE_PART_MASK) as usize;
Some(start..end)
}
pub const fn into_raw(self) -> NonZeroU64 {
self.0
}
pub fn or(self, other: Self) -> Self {
if self.is_detached() { other } else { self }
}
pub fn find(iter: impl IntoIterator<Item = Self>) -> Self {
iter.into_iter()
.find(|span| !span.is_detached())
.unwrap_or(Span::detached())
}
pub fn resolve_path(self, path: &str) -> Result<FileId, EcoString> {
let Some(file) = self.id() else {
return Err("cannot access file system from here".into());
};
Ok(file.join(path))
}
}
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
pub struct Spanned<T> {
pub v: T,
pub span: Span,
}
impl<T> Spanned<T> {
pub fn new(v: T, span: Span) -> Self {
Self { v, span }
}
pub fn as_ref(&self) -> Spanned<&T> {
Spanned { v: &self.v, span: self.span }
}
pub fn map<F, U>(self, f: F) -> Spanned<U>
where
F: FnOnce(T) -> U,
{
Spanned { v: f(self.v), span: self.span }
}
}
impl<T: Debug> Debug for Spanned<T> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
self.v.fmt(f)
}
}
#[cfg(test)]
mod tests {
use std::num::NonZeroU16;
use std::ops::Range;
use crate::{FileId, Span};
#[test]
fn test_span_detached() {
let span = Span::detached();
assert!(span.is_detached());
assert_eq!(span.id(), None);
assert_eq!(span.range(), None);
}
#[test]
fn test_span_number_encoding() {
let id = FileId::from_raw(NonZeroU16::new(5).unwrap());
let span = Span::from_number(id, 10).unwrap();
assert_eq!(span.id(), Some(id));
assert_eq!(span.number(), 10);
assert_eq!(span.range(), None);
}
#[test]
fn test_span_range_encoding() {
let id = FileId::from_raw(NonZeroU16::new(u16::MAX).unwrap());
let roundtrip = |range: Range<usize>| {
let span = Span::from_range(id, range.clone());
assert_eq!(span.id(), Some(id));
assert_eq!(span.range(), Some(range));
};
roundtrip(0..0);
roundtrip(177..233);
roundtrip(0..8388607);
roundtrip(8388606..8388607);
}
}