error_enum_core/
indexer.rs

1use std::{rc::Rc, sync::Arc};
2use stringzilla::sz::find_newline_utf8;
3
4/// A indexable string.
5pub trait Indexer {
6    /// Returns the line and column number of this `Position`.
7    fn line_col_at(&self, pos: usize) -> (usize, usize);
8
9    /// Returns the start and the end of the line that contains the position at `pos`.
10    fn line_span_at(&self, pos: usize) -> (usize, usize);
11
12    /// Returns the start and the end of the `(context_lines_before + n + context_lines_after)`
13    /// lines that contains the span from `start` to `end`.
14    ///
15    /// `context_lines_before` and `context_lines_after` specify how many lines before and after
16    /// the span to include.
17    ///
18    /// If there are not enough lines before or after, it will include as many as possible.
19    /// And if `context_lines_before` or `context_lines_after` is zero, no extra lines will be included.
20    fn span_with_context_lines(
21        &self,
22        start: usize,
23        end: usize,
24        context_lines_before: usize,
25        context_lines_after: usize,
26    ) -> (usize, usize);
27}
28
29macro_rules! impl_indexable {
30    ($T:ty) => {
31        impl<T: Indexer + ?Sized> Indexer for $T {
32            fn line_col_at(&self, pos: usize) -> (usize, usize) {
33                T::line_col_at(self, pos)
34            }
35
36            fn line_span_at(&self, pos: usize) -> (usize, usize) {
37                T::line_span_at(self, pos)
38            }
39
40            fn span_with_context_lines(
41                &self,
42                start: usize,
43                end: usize,
44                context_lines_before: usize,
45                context_lines_after: usize,
46            ) -> (usize, usize) {
47                T::span_with_context_lines(
48                    self,
49                    start,
50                    end,
51                    context_lines_before,
52                    context_lines_after,
53                )
54            }
55        }
56    };
57}
58
59impl_indexable!(&T);
60impl_indexable!(Box<T>);
61impl_indexable!(Rc<T>);
62impl_indexable!(Arc<T>);
63
64/// An [`Indexer`] that stores ending positions of every line (including trailing newlines).
65///
66/// The line and column numbers are zero-based.
67///
68/// And note that the `LineIndexer` works as if there is an implicit newline at the end of the text.
69#[derive(Debug, PartialEq, Eq)]
70#[repr(transparent)]
71pub struct LineIndexer([usize]);
72
73impl LineIndexer {
74    /// Create an [`LineIndexer`].
75    pub fn new(s: &str) -> Box<Self> {
76        let mut line_starts = Vec::new();
77        let mut cur = 0usize;
78        let mut slice = s.as_bytes();
79        while let Some(index) = find_newline_utf8(slice) {
80            line_starts.push(cur + index.end());
81            cur += index.end();
82            slice = &slice[index.end()..]
83        }
84        line_starts.push(s.len());
85        let line_starts = line_starts.into_boxed_slice();
86        unsafe { std::mem::transmute(line_starts) }
87    }
88    /// Allocate the [`LineIndexer`] on a custom lifetime.
89    pub fn alloc_on<'a>(self: Box<Self>, f: impl FnOnce(Box<[usize]>) -> &'a [usize]) -> &'a Self {
90        let slice: Box<[usize]> = unsafe { std::mem::transmute(self) };
91        let slice_ref: &'a [usize] = f(slice);
92        unsafe { std::mem::transmute(slice_ref) }
93    }
94    /// Create an [`LineIndexer`] from a boxed slice.
95    pub fn from_boxed_slice(slice: Box<[usize]>) -> Box<Self> {
96        unsafe { std::mem::transmute(slice) }
97    }
98    /// Convert the [`LineIndexer`] into a boxed slice.
99    pub fn into_boxed_slice(self: Box<Self>) -> Box<[usize]> {
100        unsafe { std::mem::transmute(self) }
101    }
102}
103
104impl LineIndexer {
105    /// Get the line number and the starting position of the line at `pos`.
106    fn line_start_at(&self, pos: usize) -> usize {
107        match self.0.binary_search(&pos) {
108            Ok(i) => self.0[i],
109            Err(0) => 0,
110            Err(i) => self.0[i.saturating_sub(1)],
111        }
112    }
113    /// Get the line number and the starting position of the line at `pos`.
114    fn line_and_start_at(&self, pos: usize) -> (usize, usize) {
115        match self.0.binary_search(&pos) {
116            Ok(i) => (i + 1, self.0[i]),
117            Err(0) => (0, 0),
118            Err(i) => (i, self.0[i.saturating_sub(1)]),
119        }
120    }
121    /// Get the line number at `pos`.
122    fn line_at(&self, pos: usize) -> usize {
123        match self.0.binary_search(&pos) {
124            Ok(i) => i + 1,
125            Err(i) => i,
126        }
127    }
128}
129
130impl Indexer for LineIndexer {
131    fn line_col_at(&self, pos: usize) -> (usize, usize) {
132        let (line, line_start) = self.line_and_start_at(pos);
133        debug_assert!(pos >= line_start);
134        (line, pos - line_start)
135    }
136
137    fn line_span_at(&self, pos: usize) -> (usize, usize) {
138        match self.0.binary_search(&pos) {
139            Ok(i) if i + 1 == self.0.len() => (self.0[i], self.0[i]),
140            Ok(i) => (self.0[i], self.0[i + 1]),
141            Err(0) => (0, self.0[0]),
142            Err(i) if i == self.0.len() => {
143                let j = i.saturating_sub(1);
144                (self.0[j], self.0[j])
145            }
146            Err(i) => (self.0[i.saturating_sub(1)], self.0[i]),
147        }
148    }
149
150    fn span_with_context_lines(
151        &self,
152        start: usize,
153        end: usize,
154        context_lines_before: usize,
155        context_lines_after: usize,
156    ) -> (usize, usize) {
157        let start = if context_lines_before == 0 {
158            self.line_start_at(start)
159        } else {
160            self.line_at(start)
161                .saturating_sub(context_lines_before)
162                .checked_sub(1)
163                .map_or_else(|| 0, |i| self.0[i])
164        };
165        let end = if context_lines_after == 0 {
166            self.line_span_at(end).1
167        } else {
168            self.0[self
169                .line_at(end)
170                .saturating_add(context_lines_after)
171                .min(self.0.len() - 1)]
172        };
173        (start, end)
174    }
175}