Skip to content

Commit

Permalink
Explain source_text implementation approach
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Oct 9, 2023
1 parent 31b14c3 commit c4c3251
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,11 +368,16 @@ impl FileInfo {

fn source_text(&mut self, span: Span) -> String {
let lo_char = (span.lo - self.span.lo) as usize;

// Look up offset of the largest already-computed char index that is
// less than or equal to the current requested one. We resume counting
// chars from that point.
let (&last_char_index, &last_byte_offset) = self
.char_index_to_byte_offset
.range(..=lo_char)
.next_back()
.unwrap_or((&0, &0));

let lo_byte = if last_char_index == lo_char {
last_byte_offset
} else {
Expand All @@ -387,6 +392,7 @@ impl FileInfo {
.insert(lo_char, total_byte_offset);
total_byte_offset
};

let trunc_lo = &self.source_text[lo_byte..];
let char_len = (span.hi - span.lo) as usize;
let source_text = match trunc_lo.char_indices().nth(char_len) {
Expand Down Expand Up @@ -441,6 +447,7 @@ impl SourceMap {
source_text: src.to_owned(),
span,
lines,
// Populated lazily by source_text().
char_index_to_byte_offset: BTreeMap::new(),
});

Expand Down

0 comments on commit c4c3251

Please sign in to comment.