diff options
| author | Ariel Davis <ariel.z.davis@icloud.com> | 2023-05-06 15:10:35 -0700 |
|---|---|---|
| committer | Ariel Davis <ariel.z.davis@icloud.com> | 2023-05-06 15:10:35 -0700 |
| commit | cc2936b93ebeadceb1dfd0b3aa6194d95a5cb5a6 (patch) | |
| tree | 15593a922240a82878625316cd2c2678916dadf6 | |
| parent | 510050ecdc5d7eede55533246e713fe847038af8 (diff) | |
| download | rust-cc2936b93ebeadceb1dfd0b3aa6194d95a5cb5a6.tar.gz rust-cc2936b93ebeadceb1dfd0b3aa6194d95a5cb5a6.zip | |
Use size field
| -rw-r--r-- | lib/line-index/src/lib.rs | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/lib/line-index/src/lib.rs b/lib/line-index/src/lib.rs index 370bbf68f38..b29717e0a7b 100644 --- a/lib/line-index/src/lib.rs +++ b/lib/line-index/src/lib.rs @@ -84,11 +84,11 @@ impl WideChar { #[derive(Debug, Clone, PartialEq, Eq)] pub struct LineIndex { /// Offset the beginning of each line (except the first, which always has offset 0). - /// - /// Invariant: Always non-empty and the last element holds the length of the original text. newlines: Box<[TextSize]>, /// List of non-ASCII characters on each line. line_wide_chars: IntMap<u32, Box<[WideChar]>>, + /// The size of the entire text. + size: TextSize, } impl LineIndex { @@ -127,14 +127,16 @@ impl LineIndex { cur_col += c_len; } - newlines.push(TextSize::of(text)); - // Save any wide characters seen in the last line if !wide_chars.is_empty() { line_wide_chars.insert(line, wide_chars.into_boxed_slice()); } - LineIndex { newlines: newlines.into_boxed_slice(), line_wide_chars } + LineIndex { + newlines: newlines.into_boxed_slice(), + line_wide_chars, + size: TextSize::of(text), + } } /// Transforms the `TextSize` into a `LineCol`. @@ -150,7 +152,7 @@ impl LineIndex { /// e.g. if it extends past the end of the text or points to the middle of a multi-byte /// character. pub fn try_line_col(&self, offset: TextSize) -> Option<LineCol> { - if offset > *self.newlines.last().unwrap() { + if offset > self.size { return None; } let line = self.newlines.partition_point(|&it| it <= offset); |
