Skip to content

Commit 08d1dd0

Browse files
committed
Cache span end positions
1 parent 8c4135f commit 08d1dd0

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

src/fallback.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -375,37 +375,34 @@ impl FileInfo {
375375
}
376376

377377
fn byte_range(&mut self, span: Span) -> Range<usize> {
378-
let lo_char = (span.lo - self.span.lo) as usize;
378+
self.byte(span.lo)..self.byte(span.hi)
379+
}
380+
381+
fn byte(&mut self, ch: u32) -> usize {
382+
let char_index = (ch - self.span.lo) as usize;
379383

380384
// Look up offset of the largest already-computed char index that is
381385
// less than or equal to the current requested one. We resume counting
382386
// chars from that point.
383387
let (&last_char_index, &last_byte_offset) = self
384388
.char_index_to_byte_offset
385-
.range(..=lo_char)
389+
.range(..=char_index)
386390
.next_back()
387391
.unwrap_or((&0, &0));
388392

389-
let lo_byte = if last_char_index == lo_char {
393+
if last_char_index == char_index {
390394
last_byte_offset
391395
} else {
392396
let total_byte_offset = match self.source_text[last_byte_offset..]
393397
.char_indices()
394-
.nth(lo_char - last_char_index)
398+
.nth(char_index - last_char_index)
395399
{
396400
Some((additional_offset, _ch)) => last_byte_offset + additional_offset,
397401
None => self.source_text.len(),
398402
};
399403
self.char_index_to_byte_offset
400-
.insert(lo_char, total_byte_offset);
404+
.insert(char_index, total_byte_offset);
401405
total_byte_offset
402-
};
403-
404-
let trunc_lo = &self.source_text[lo_byte..];
405-
let char_len = (span.hi - span.lo) as usize;
406-
lo_byte..match trunc_lo.char_indices().nth(char_len) {
407-
Some((offset, _ch)) => lo_byte + offset,
408-
None => self.source_text.len(),
409406
}
410407
}
411408

0 commit comments

Comments
 (0)