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

fix: Span.trim_remainder gives incorrect result in some cases #147

Merged
merged 1 commit into from
Oct 28, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/span/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,13 @@ impl<'src> Span<'src> {
///
/// Note that the trailing remainder condition is not enforced.
pub(crate) fn trim_remainder(self, after: Span<'src>) -> Span<'src> {
// Sanity check: If after is longer than source, we can't trim.
let rlen = after.len();
let slen = self.len();
let offset = (self.offset + self.len()).min(after.offset);

if rlen >= slen {
if offset <= self.offset {
// Invalid input: We'll respond with an empty slice.
self.slice(0..0)
} else {
let trim_len = slen - rlen;
let trim_len = offset - self.offset;
self.slice(0..trim_len)
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/tests/span/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,4 +567,21 @@ mod trim_remainder {
}
);
}

#[test]
fn rem_is_incomplete_subset_of_source() {
let source = Span::new("abc\ndef\n");
let line1 = source.take_normalized_line();
let line2 = line1.after.take_line();

assert_eq!(
source.trim_remainder(line2.item),
TSpan {
data: "abc\n",
line: 1,
col: 1,
offset: 0
}
);
}
}
Loading