diff options
| author | Ariel Davis <ariel.z.davis@icloud.com> | 2023-05-06 00:56:30 -0700 |
|---|---|---|
| committer | Ariel Davis <ariel.z.davis@icloud.com> | 2023-05-06 00:56:30 -0700 |
| commit | 9de213c4fe10a001d1f55c76f157c1d9173694bc (patch) | |
| tree | 84e0a8eb9f7c1cea61bbd0fd421fc0eb54141bb1 | |
| parent | fcbe73ec1caad761eb6cf1a641fa667ac920076b (diff) | |
| download | rust-9de213c4fe10a001d1f55c76f157c1d9173694bc.tar.gz rust-9de213c4fe10a001d1f55c76f157c1d9173694bc.zip | |
Swap, tweak comments
| -rw-r--r-- | lib/line-index/src/lib.rs | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/lib/line-index/src/lib.rs b/lib/line-index/src/lib.rs index 91b7faa78ad..39ae99fbd69 100644 --- a/lib/line-index/src/lib.rs +++ b/lib/line-index/src/lib.rs @@ -93,29 +93,30 @@ pub struct LineIndex { impl LineIndex { /// Returns a `LineIndex` for the `text`. pub fn new(text: &str) -> LineIndex { + let mut newlines = Vec::with_capacity(16); let mut line_wide_chars = IntMap::default(); + let mut wide_chars = Vec::new(); + let mut cur_row = TextSize::from(0); + let mut cur_col = TextSize::from(0); + let mut line = 0; - let mut newlines = Vec::with_capacity(16); newlines.push(TextSize::from(0)); - let mut cur_row = 0.into(); - let mut cur_col = 0.into(); - let mut line = 0; for c in text.chars() { let c_len = TextSize::of(c); cur_row += c_len; if c == '\n' { newlines.push(cur_row); - // Save any utf-16 characters seen in the previous line + // Save any wide characters seen in the previous line if !wide_chars.is_empty() { - line_wide_chars - .insert(line, std::mem::take(&mut wide_chars).into_boxed_slice()); + let cs = std::mem::take(&mut wide_chars).into_boxed_slice(); + line_wide_chars.insert(line, cs); } // Prepare for processing the next line - cur_col = 0.into(); + cur_col = TextSize::from(0); line += 1; continue; } @@ -127,7 +128,7 @@ impl LineIndex { cur_col += c_len; } - // Save any utf-16 characters seen in the last line + // Save any wide characters seen in the last line if !wide_chars.is_empty() { line_wide_chars.insert(line, wide_chars.into_boxed_slice()); } @@ -136,6 +137,10 @@ impl LineIndex { } /// Transforms the `TextSize` into a `LineCol`. + /// + /// # Panics + /// + /// If the offset is invalid. pub fn line_col(&self, offset: TextSize) -> LineCol { let line = self.newlines.partition_point(|&it| it <= offset) - 1; let line_start_offset = self.newlines[line]; |
