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!(Box<T>);
60impl_indexable!(Rc<T>);
61impl_indexable!(Arc<T>);
62
63/// An [`Indexer`] that stores ending positions of every line (including trailing newlines).
64///
65/// The line and column numbers are zero-based.
66///
67/// And note that the `LineIndexer` works as if there is an implicit newline at the end of the text.
68#[derive(Debug, PartialEq, Eq)]
69#[repr(transparent)]
70pub struct LineIndexer([usize]);
71
72impl LineIndexer {
73    /// Create an [`LineIndexer`].
74    pub fn new(s: &str) -> Box<Self> {
75        let mut line_starts = Vec::new();
76        let mut cur = 0usize;
77        let mut slice = s.as_bytes();
78        while let Some(index) = find_newline_utf8(slice) {
79            line_starts.push(cur + index.end());
80            cur += index.end();
81            slice = &slice[index.end()..]
82        }
83        line_starts.push(s.len());
84        let line_starts = line_starts.into_boxed_slice();
85        unsafe { std::mem::transmute(line_starts) }
86    }
87}
88
89impl LineIndexer {
90    /// Get the line number and the starting position of the line at `pos`.
91    fn line_start_at(&self, pos: usize) -> usize {
92        match self.0.binary_search(&pos) {
93            Ok(i) => self.0[i],
94            Err(0) => 0,
95            Err(i) => self.0[i.saturating_sub(1)],
96        }
97    }
98    /// Get the line number and the starting position of the line at `pos`.
99    fn line_and_start_at(&self, pos: usize) -> (usize, usize) {
100        match self.0.binary_search(&pos) {
101            Ok(i) => (i + 1, self.0[i]),
102            Err(0) => (0, 0),
103            Err(i) => (i, self.0[i.saturating_sub(1)]),
104        }
105    }
106    /// Get the line number at `pos`.
107    fn line_at(&self, pos: usize) -> usize {
108        match self.0.binary_search(&pos) {
109            Ok(i) => i + 1,
110            Err(i) => i,
111        }
112    }
113}
114
115impl Indexer for LineIndexer {
116    fn line_col_at(&self, pos: usize) -> (usize, usize) {
117        let (line, line_start) = self.line_and_start_at(pos);
118        debug_assert!(pos >= line_start);
119        (line, pos - line_start)
120    }
121
122    fn line_span_at(&self, pos: usize) -> (usize, usize) {
123        match self.0.binary_search(&pos) {
124            Ok(i) if i + 1 == self.0.len() => (self.0[i], self.0[i]),
125            Ok(i) => (self.0[i], self.0[i + 1]),
126            Err(0) => (0, self.0[0]),
127            Err(i) if i == self.0.len() => {
128                let j = i.saturating_sub(1);
129                (self.0[j], self.0[j])
130            }
131            Err(i) => (self.0[i.saturating_sub(1)], self.0[i]),
132        }
133    }
134
135    fn span_with_context_lines(
136        &self,
137        start: usize,
138        end: usize,
139        context_lines_before: usize,
140        context_lines_after: usize,
141    ) -> (usize, usize) {
142        let start = if context_lines_before == 0 {
143            self.line_start_at(start)
144        } else {
145            self.line_at(start)
146                .saturating_sub(context_lines_before)
147                .checked_sub(1)
148                .map_or_else(|| 0, |i| self.0[i])
149        };
150        let end = if context_lines_after == 0 {
151            self.line_span_at(end).1
152        } else {
153            self.0[self
154                .line_at(end)
155                .saturating_add(context_lines_after)
156                .min(self.0.len() - 1)]
157        };
158        (start, end)
159    }
160}