Unnamed repository; edit this file 'description' to name the repository.
Add convex_hull for TextRange
Edwin Cheng 2020-02-25
parent 1b67164 · commit 3d9bbe7
-rw-r--r--lib/text-size/src/lib.rs16
1 files changed, 16 insertions, 0 deletions
diff --git a/lib/text-size/src/lib.rs b/lib/text-size/src/lib.rs
index c00954c55c..2ca54859fe 100644
--- a/lib/text-size/src/lib.rs
+++ b/lib/text-size/src/lib.rs
@@ -301,6 +301,15 @@ impl TextRange {
// BREAK: pass by value
#[inline(always)]
+ /// The smallest convex set that contains both ranges
+ pub fn convex_hull(&self, other: &TextRange) -> TextRange {
+ let start = self.start().min(other.start());
+ let end = self.end().max(other.end());
+ TextRange::from_to(start, end)
+ }
+
+ // BREAK: pass by value
+ #[inline(always)]
pub fn contains(&self, offset: TextUnit) -> bool {
self.start() <= offset && offset < self.end()
}
@@ -434,6 +443,13 @@ mod tests {
}
#[test]
+ fn check_convex_hull() {
+ assert_eq!(r(1, 2).convex_hull(&r(2, 3)), r(1, 3));
+ assert_eq!(r(1, 5).convex_hull(&r(2, 3)), r(1, 5));
+ assert_eq!(r(1, 2).convex_hull(&r(4, 5)), r(1, 5));
+ }
+
+ #[test]
fn check_contains() {
assert!(!r(1, 3).contains(0.into()));
assert!(r(1, 3).contains(1.into()));