From da5c63c8f90b7cf66d94626352141dc3f0585b6e Mon Sep 17 00:00:00 2001 From: Ariel Davis Date: Thu, 4 May 2023 16:40:41 -0700 Subject: [PATCH] Use boxed slice As well as doing the shrink_to_fit, we also don't have to keep track of the capacity anymore. --- lib/line-index/src/lib.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/lib/line-index/src/lib.rs b/lib/line-index/src/lib.rs index 1a4753cbd6..e31f3006e2 100644 --- a/lib/line-index/src/lib.rs +++ b/lib/line-index/src/lib.rs @@ -13,9 +13,9 @@ pub use text_size::{TextRange, TextSize}; #[derive(Clone, Debug, PartialEq, Eq)] pub struct LineIndex { /// Offset the beginning of each line, zero-based. - newlines: Vec, + newlines: Box<[TextSize]>, /// List of non-ASCII characters on each line. - line_wide_chars: IntMap>, + line_wide_chars: IntMap>, } /// Line/Column information in native, utf8 format. @@ -97,7 +97,8 @@ impl LineIndex { // Save any utf-16 characters seen in the previous line if !wide_chars.is_empty() { - line_wide_chars.insert(line, std::mem::take(&mut wide_chars)); + line_wide_chars + .insert(line, std::mem::take(&mut wide_chars).into_boxed_slice()); } // Prepare for processing the next line @@ -115,13 +116,10 @@ impl LineIndex { // Save any utf-16 characters seen in the last line if !wide_chars.is_empty() { - line_wide_chars.insert(line, wide_chars); + line_wide_chars.insert(line, wide_chars.into_boxed_slice()); } - newlines.shrink_to_fit(); - line_wide_chars.shrink_to_fit(); - - LineIndex { newlines, line_wide_chars } + LineIndex { newlines: newlines.into_boxed_slice(), line_wide_chars } } /// Transforms the `TextSize` into a `LineCol`. @@ -168,7 +166,7 @@ impl LineIndex { fn utf8_to_wide_col(&self, enc: WideEncoding, line: u32, col: TextSize) -> usize { let mut res: usize = col.into(); if let Some(wide_chars) = self.line_wide_chars.get(&line) { - for c in wide_chars { + for c in wide_chars.iter() { if c.end <= col { res -= usize::from(c.len()) - c.wide_len(enc); } else { @@ -183,7 +181,7 @@ impl LineIndex { fn wide_to_utf8_col(&self, enc: WideEncoding, line: u32, mut col: u32) -> TextSize { if let Some(wide_chars) = self.line_wide_chars.get(&line) { - for c in wide_chars { + for c in wide_chars.iter() { if col > u32::from(c.start) { col += u32::from(c.len()) - c.wide_len(enc) as u32; } else {