Skip to content

Commit 7f5533d

Browse files
committed
Account for multibyte chars in source_text() computation
1 parent 4fe2326 commit 7f5533d

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/fallback.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,13 @@ impl FileInfo {
364364

365365
fn source_text(&self, span: Span) -> String {
366366
let lo = (span.lo - self.span.lo) as usize;
367-
let hi = (span.hi - self.span.lo) as usize;
368-
self.source_text[lo..hi].to_owned()
367+
let trunc_lo = &self.source_text[lo..];
368+
let char_len = (span.hi - span.lo) as usize;
369+
let source_text = match trunc_lo.char_indices().nth(char_len) {
370+
Some((offset, _ch)) => &trunc_lo[..offset],
371+
None => trunc_lo,
372+
};
373+
source_text.to_owned()
369374
}
370375
}
371376

tests/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ fn source_text() {
331331
let input = " 𓀕 ";
332332
let tokens = input.parse::<proc_macro2::TokenStream>().unwrap();
333333
let ident = tokens.into_iter().next().unwrap();
334-
assert_eq!("𓀕", ident.span().source_text().unwrap()); // FIXME
334+
assert_eq!("𓀕", ident.span().source_text().unwrap());
335335
}
336336

337337
#[test]

0 commit comments

Comments
 (0)