Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account for multibyte chars in source_text() computation #409

Merged
merged 2 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/fallback.rs
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
9 changes: 9 additions & 0 deletions tests/test.rs
Expand Up @@ -325,6 +325,15 @@ fn literal_span() {
assert!(positive.subspan(1..4).is_none());
}

#[cfg(span_locations)]
#[test]
fn source_text() {
let input = " 𓀕 ";
let tokens = input.parse::<proc_macro2::TokenStream>().unwrap();
let ident = tokens.into_iter().next().unwrap();
assert_eq!("𓀕", ident.span().source_text().unwrap());
}

#[test]
fn roundtrip() {
fn roundtrip(p: &str) {
Expand Down