diff options
| author | Martin Haug <mhaug@live.de> | 2022-01-03 23:18:21 +0100 |
|---|---|---|
| committer | Martin Haug <mhaug@live.de> | 2022-01-04 00:21:33 +0100 |
| commit | c994cfa7d814e3909682b19322867ed5c676c453 (patch) | |
| tree | 03349230f74786c7128876889c07a31a4932f108 /src/parse/scanner.rs | |
| parent | 98c96ba1cb8a46e327de313118e4ce1a84795ae9 (diff) | |
Code Review: Your parsers were so preoccupied with whether they could
Diffstat (limited to 'src/parse/scanner.rs')
| -rw-r--r-- | src/parse/scanner.rs | 23 |
1 files changed, 19 insertions, 4 deletions
diff --git a/src/parse/scanner.rs b/src/parse/scanner.rs index c735be40..6db89132 100644 --- a/src/parse/scanner.rs +++ b/src/parse/scanner.rs @@ -162,11 +162,26 @@ impl<'s> Scanner<'s> { /// The column index of a given index in the source string. #[inline] pub fn column(&self, index: usize) -> usize { - self.src[.. index] - .chars() + self.column_offset(index, 0) + } + + /// The column index of a given index in the source string when an offset is + /// applied to the first line of the string. + #[inline] + pub fn column_offset(&self, index: usize, offset: usize) -> usize { + let mut apply_offset = false; + let res = self.src[.. index] + .char_indices() .rev() - .take_while(|&c| !is_newline(c)) - .count() + .take_while(|&(_, c)| !is_newline(c)) + .inspect(|&(i, _)| { + if i == 0 { + apply_offset = true + } + }) + .count(); + + if apply_offset { res + offset } else { res } } } |
