Skip to content

Commit da5c63c

Browse files
committed
Use boxed slice
As well as doing the shrink_to_fit, we also don't have to keep track of the capacity anymore.
1 parent d9c8846 commit da5c63c

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

lib/line-index/src/lib.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ pub use text_size::{TextRange, TextSize};
1313
#[derive(Clone, Debug, PartialEq, Eq)]
1414
pub struct LineIndex {
1515
/// Offset the beginning of each line, zero-based.
16-
newlines: Vec<TextSize>,
16+
newlines: Box<[TextSize]>,
1717
/// List of non-ASCII characters on each line.
18-
line_wide_chars: IntMap<u32, Vec<WideChar>>,
18+
line_wide_chars: IntMap<u32, Box<[WideChar]>>,
1919
}
2020

2121
/// Line/Column information in native, utf8 format.
@@ -97,7 +97,8 @@ impl LineIndex {
9797

9898
// Save any utf-16 characters seen in the previous line
9999
if !wide_chars.is_empty() {
100-
line_wide_chars.insert(line, std::mem::take(&mut wide_chars));
100+
line_wide_chars
101+
.insert(line, std::mem::take(&mut wide_chars).into_boxed_slice());
101102
}
102103

103104
// Prepare for processing the next line
@@ -115,13 +116,10 @@ impl LineIndex {
115116

116117
// Save any utf-16 characters seen in the last line
117118
if !wide_chars.is_empty() {
118-
line_wide_chars.insert(line, wide_chars);
119+
line_wide_chars.insert(line, wide_chars.into_boxed_slice());
119120
}
120121

121-
newlines.shrink_to_fit();
122-
line_wide_chars.shrink_to_fit();
123-
124-
LineIndex { newlines, line_wide_chars }
122+
LineIndex { newlines: newlines.into_boxed_slice(), line_wide_chars }
125123
}
126124

127125
/// Transforms the `TextSize` into a `LineCol`.
@@ -168,7 +166,7 @@ impl LineIndex {
168166
fn utf8_to_wide_col(&self, enc: WideEncoding, line: u32, col: TextSize) -> usize {
169167
let mut res: usize = col.into();
170168
if let Some(wide_chars) = self.line_wide_chars.get(&line) {
171-
for c in wide_chars {
169+
for c in wide_chars.iter() {
172170
if c.end <= col {
173171
res -= usize::from(c.len()) - c.wide_len(enc);
174172
} else {
@@ -183,7 +181,7 @@ impl LineIndex {
183181

184182
fn wide_to_utf8_col(&self, enc: WideEncoding, line: u32, mut col: u32) -> TextSize {
185183
if let Some(wide_chars) = self.line_wide_chars.get(&line) {
186-
for c in wide_chars {
184+
for c in wide_chars.iter() {
187185
if col > u32::from(c.start) {
188186
col += u32::from(c.len()) - c.wide_len(enc) as u32;
189187
} else {

0 commit comments

Comments
 (0)