Skip to main content

vibe/
span.rs

1//! UTF-8 byte spans. Canonical source positions for diagnostics.
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
4pub struct Span {
5    pub start: u32,
6    pub end: u32,
7}
8
9impl Span {
10    /// Creates a new `Span`.
11    pub const fn new(start: u32, end: u32) -> Self {
12        Self { start, end }
13    }
14
15    /// Point span.
16    pub const fn point(at: u32) -> Self {
17        Self { start: at, end: at }
18    }
19
20    /// Merges two spans.
21    pub fn merge(self, other: Span) -> Span {
22        Span {
23            start: self.start.min(other.start),
24            end: self.end.max(other.end),
25        }
26    }
27
28    /// Slices the span.
29    pub fn slice<'a>(&self, src: &'a str) -> &'a str {
30        let start = self.start as usize;
31        let end = (self.end as usize).min(src.len());
32        if start >= src.len() || start > end {
33            return "";
34        }
35        &src[start..end]
36    }
37}