Skip to content

Commit

Permalink
Account for multibyte chars in source_text() computation
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Oct 6, 2023
1 parent 07bb590 commit 124fba6
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,13 @@ impl FileInfo {

fn source_text(&self, span: Span) -> String {
let lo = (span.lo - self.span.lo) as usize;
let hi = (span.hi - self.span.lo) as usize;
self.source_text[lo..hi].to_owned()
let trunc_lo = &self.source_text[lo..];
let char_len = (span.hi - span.lo) as usize;
let source_text = match trunc_lo.char_indices().nth(char_len) {
Some((offset, _ch)) => &trunc_lo[..offset],
None => trunc_lo,
};
source_text.to_owned()
}
}

Expand Down

0 comments on commit 124fba6

Please sign in to comment.