Initial Commit
This commit is contained in:
		
							
								
								
									
										42
									
								
								src/span.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								src/span.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,42 @@
 | 
			
		||||
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}]")
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[allow(non_snake_case)]
 | 
			
		||||
/// Stores the start and end byte position
 | 
			
		||||
pub 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}")
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user