Skip to content

Commit c7a6ad6

Browse files
Merge #1
1: Add extend_to for TextRange r=edwin0cheng a=edwin0cheng ~~This PR added a `convex_hull` function for joining to two `TextUnit` together.~~ This PR added a `extend_to` function for joining to two `TextUnit` together. Co-authored-by: Edwin Cheng <[email protected]>
2 parents 0258341 + 8d0d3e9 commit c7a6ad6

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,15 @@ impl TextRange {
299299
}
300300
}
301301

302+
// BREAK: pass by value
303+
#[inline(always)]
304+
/// The smallest range that contains both ranges
305+
pub fn extend_to(&self, other: &TextRange) -> TextRange {
306+
let start = self.start().min(other.start());
307+
let end = self.end().max(other.end());
308+
TextRange::from_to(start, end)
309+
}
310+
302311
// BREAK: pass by value
303312
#[inline(always)]
304313
pub fn contains(&self, offset: TextUnit) -> bool {
@@ -433,6 +442,13 @@ mod tests {
433442
assert_eq!(r(1, 2).intersection(&r(3, 4)), None);
434443
}
435444

445+
#[test]
446+
fn check_extend_to() {
447+
assert_eq!(r(1, 2).extend_to(&r(2, 3)), r(1, 3));
448+
assert_eq!(r(1, 5).extend_to(&r(2, 3)), r(1, 5));
449+
assert_eq!(r(1, 2).extend_to(&r(4, 5)), r(1, 5));
450+
}
451+
436452
#[test]
437453
fn check_contains() {
438454
assert!(!r(1, 3).contains(0.into()));

0 commit comments

Comments
 (0)