use std::cmp::Ordering;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::ptr;
use position;
pub struct Span<'i> {
input: &'i str,
start: usize,
end: usize,
}
#[inline]
pub fn new(input: &str, start: usize, end: usize) -> Span {
Span { input, start, end }
}
impl<'i> Span<'i> {
#[inline]
pub fn start(&self) -> usize {
self.start
}
#[inline]
pub fn end(&self) -> usize {
self.end
}
#[inline]
pub fn start_pos(&self) -> position::Position<'i> {
unsafe { position::new(self.input, self.start) }
}
#[inline]
pub fn end_pos(&self) -> position::Position<'i> {
unsafe { position::new(self.input, self.end) }
}
#[inline]
pub fn split(self) -> (position::Position<'i>, position::Position<'i>) {
let pos1 = unsafe { position::new(self.input, self.start) };
let pos2 = unsafe { position::new(self.input, self.end) };
(pos1, pos2)
}
#[inline]
pub fn as_str(&self) -> &'i str {
unsafe { self.input.slice_unchecked(self.start, self.end) }
}
}
impl<'i> fmt::Debug for Span<'i> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Span {{ start: {}, end: {} }}", self.start, self.end)
}
}
impl<'i> Clone for Span<'i> {
fn clone(&self) -> Span<'i> {
new(self.input, self.start, self.end)
}
}
impl<'i> PartialEq for Span<'i> {
fn eq(&self, other: &Span<'i>) -> bool {
ptr::eq(self.input, other.input) && self.start == other.start && self.end == other.end
}
}
impl<'i> Eq for Span<'i> {}
impl<'i> PartialOrd for Span<'i> {
fn partial_cmp(&self, other: &Span<'i>) -> Option<Ordering> {
if ptr::eq(self.input, other.input) {
match self.start.partial_cmp(&other.start) {
Some(Ordering::Equal) => self.end.partial_cmp(&other.end),
ordering => ordering
}
} else {
None
}
}
}
impl<'i> Ord for Span<'i> {
fn cmp(&self, other: &Span<'i>) -> Ordering {
self.partial_cmp(other).expect(
"cannot compare spans from \
different inputs"
)
}
}
impl<'i> Hash for Span<'i> {
fn hash<H: Hasher>(&self, state: &mut H) {
(self.input as *const str).hash(state);
self.start.hash(state);
self.end.hash(state);
}
}