1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use std::ops::Range;

/// Utility function to get the width of a character at a given byte index
/// # Panic
/// Panics if the index is not the start of a character
pub fn character_width_at_index(index: usize, value: &str) -> usize {
    let mut current_index = 0;
    for char in value.chars() {
        if current_index == index {
            return char.len_utf8();
        }
        assert!(
            current_index < index,
            "Index was not at the start of a character"
        );
        current_index += char.len_utf8();
    }
    panic!("Index out of bounds")
}

/// A span which uniquely specifies a span of characters in their corresponding file
/// All spans are dependent on the [`InputFiles`](super::InputFiles) which contains the file
/// that spans can refer to.
#[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)]
pub struct Span {
    start: usize,
    len: usize,
}

impl Span {
    pub const EMPTY: Span = Span::new(0, 0);

    /// Creates a new `Span` from the starting character and its length
    pub const fn new(start: usize, len: usize) -> Self {
        Span { start, len }
    }

    /// Returns a span with the same start and a length of one
    pub fn at_start(&self) -> Span {
        Span::new(self.start(), 1)
    }

    /// Returns the start of this span
    pub fn start(&self) -> usize {
        self.start
    }

    /// Returns the end of this span
    pub fn end(&self) -> usize {
        self.start + self.len
    }

    /// Returns the length of this span
    pub fn len(&self) -> usize {
        self.len
    }

    /// Returns, whether this span is empty
    ///
    /// Thanks, clippy.
    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// Constructs a new span which ranges from the start of this span to the end of the other span
    pub fn until(&self, other: Span) -> Self {
        assert!(
            self.start <= other.end(),
            "Span length must not be negative"
        );
        Span::new(self.start, other.end() - self.start)
    }

    pub fn as_slice(&self) -> Range<usize> {
        self.start()..self.end()
    }

    /// Since ranges are used to index into a str on a byte level,
    /// a span starting at index 10 is **not** necessarily the character at index 10.
    /// This methods iterates over the source chars until it finds the character at the
    /// byte positions marked by this span. This methods panics if the span is out of bounds.
    /// # Returns:
    ///  The returned tuple has the shape (`start_character_index`, `end_character_index`)
    pub fn char_bounds(&self, source: &str) -> (usize, usize) {
        let mut byte_idx = 0;

        let mut start_idx = None;
        let mut end_idx = None;

        if self.start() == 0 {
            start_idx = Some(0);
        }
        if self.end() == 0 {
            end_idx = Some(0);
        }
        for (idx, char) in source.chars().enumerate() {
            byte_idx += char.len_utf8();
            if self.start() == byte_idx {
                start_idx = Some(idx + 1);
            }
            if self.end() == byte_idx {
                end_idx = Some(idx + 1);
                break;
            }
        }
        let start_idx = start_idx.expect("Span start is out of bounds");
        let end_idx = end_idx.expect("Span end is out of bounds");
        (start_idx, end_idx)
    }
}

impl From<Range<usize>> for Span {
    fn from(value: Range<usize>) -> Self {
        Span {
            start: value.start,
            len: value
                .end
                .checked_sub(value.start)
                .expect("End must be greater than start"),
        }
    }
}

impl std::ops::Add<usize> for Span {
    type Output = Self;

    fn add(self, rhs: usize) -> Self::Output {
        Span {
            start: self.start + rhs,
            len: self.len,
        }
    }
}

impl std::ops::Add<Span> for usize {
    type Output = Span;

    fn add(self, rhs: Span) -> Self::Output {
        rhs + self
    }
}

#[cfg(test)]
mod tests {

    use crate::Span;

    #[test]
    fn local_span_correct() {
        let span = Span::new(0, 1);

        assert_eq!(span.start(), 0);
        assert_eq!(span.end(), 1);
        assert_eq!(span.len(), 1);
    }

    #[test]
    fn span_char_bounds() {
        let span = Span::new(2, 3);
        let text = "öaöa";
        assert_eq!(span.char_bounds(text), (1, 3));
    }
}