Skip to content

Commit df96cba

Browse files
committed
Add Span::trim_end
This is the counterpart of `Span::trim_start`.
1 parent e609c9b commit df96cba

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

compiler/rustc_span/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,13 @@ impl Span {
682682
if span.hi > other.hi { Some(span.with_lo(cmp::max(span.lo, other.hi))) } else { None }
683683
}
684684

685+
/// Returns `Some(span)`, where the end is trimmed by the start of `other`.
686+
pub fn trim_end(self, other: Span) -> Option<Span> {
687+
let span = self.data();
688+
let other = other.data();
689+
if span.lo < other.lo { Some(span.with_hi(cmp::min(span.hi, other.lo))) } else { None }
690+
}
691+
685692
/// Returns the source span -- this is either the supplied span, or the span for
686693
/// the macro callsite that expanded to it.
687694
pub fn source_callsite(self) -> Span {

compiler/rustc_span/src/tests.rs

+19
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,25 @@ fn test_trim() {
6161
// The resulting span's context should be that of `self`, not `other`.
6262
let other = span(start, end).with_ctxt(SyntaxContext::from_u32(999));
6363

64+
// Test cases for `trim_end`.
65+
66+
assert_eq!(span(well_before, before).trim_end(other), Some(span(well_before, before)));
67+
assert_eq!(span(well_before, start).trim_end(other), Some(span(well_before, start)));
68+
assert_eq!(span(well_before, mid).trim_end(other), Some(span(well_before, start)));
69+
assert_eq!(span(well_before, end).trim_end(other), Some(span(well_before, start)));
70+
assert_eq!(span(well_before, after).trim_end(other), Some(span(well_before, start)));
71+
72+
assert_eq!(span(start, mid).trim_end(other), None);
73+
assert_eq!(span(start, end).trim_end(other), None);
74+
assert_eq!(span(start, after).trim_end(other), None);
75+
76+
assert_eq!(span(mid, end).trim_end(other), None);
77+
assert_eq!(span(mid, after).trim_end(other), None);
78+
79+
assert_eq!(span(end, after).trim_end(other), None);
80+
81+
assert_eq!(span(after, well_after).trim_end(other), None);
82+
6483
// Test cases for `trim_start`.
6584

6685
assert_eq!(span(after, well_after).trim_start(other), Some(span(after, well_after)));

0 commit comments

Comments
 (0)