Unnamed repository; edit this file 'description' to name the repository.
Alternative set of contains function
Motivation: TextRange is a set, it contains elements (TextSize). For this reason, for range-range op we use a more verbose `contains_range` name. In stdlib, there's `HashSet::is_subset`. We used a similar design with `is_subrage` before, but it was very confusing in practice -- you'll have to lookup docs for which of lhs and rhs is sub and super set. Additionally, exclusive semantics is a clear default with better properties (if you have a partitioning of a range into subranges, only one of the parts contains any given offset), so it make sense to call it `contains` and reserve `contains_inclusive` for another op.
Aleksey Kladov 2020-03-12
parent c44a24e · commit 894afd0
-rw-r--r--lib/text-size/src/range.rs32
-rw-r--r--lib/text-size/tests/main.rs14
2 files changed, 23 insertions, 23 deletions
diff --git a/lib/text-size/src/range.rs b/lib/text-size/src/range.rs
index c06c19dfe4..4fba210cfb 100644
--- a/lib/text-size/src/range.rs
+++ b/lib/text-size/src/range.rs
@@ -88,8 +88,23 @@ impl TextRange {
/// Manipulation methods.
impl TextRange {
+ /// Check if this range contains an offset.
+ ///
+ /// The end index is considered excluded.
+ pub fn contains(self, offset: TextSize) -> bool {
+ self.start() <= offset && offset < self.end()
+ }
+
+ /// Check if this range contains an offset.
+ ///
+ /// The end index is considered included.
+ pub fn contains_inclusive(self, offset: TextSize) -> bool {
+ let point = offset.into();
+ self.start() <= point && point <= self.end()
+ }
+
/// Check if this range completely contains another range.
- pub fn contains(self, other: TextRange) -> bool {
+ pub fn contains_range(self, other: TextRange) -> bool {
self.start() <= other.start() && other.end() <= self.end()
}
@@ -110,21 +125,6 @@ impl TextRange {
let end = cmp::max(lhs.end(), rhs.end());
TextRange(start, end)
}
-
- /// Check if this range contains an offset.
- ///
- /// The end index is considered excluded.
- pub fn contains_exclusive(self, offset: TextSize) -> bool {
- self.start() <= offset && offset < self.end()
- }
-
- /// Check if this range contains an offset.
- ///
- /// The end index is considered included.
- pub fn contains_inclusive(self, offset: TextSize) -> bool {
- let point = offset.into();
- self.start() <= point && point <= self.end()
- }
}
fn ix(size: TextSize) -> usize {
diff --git a/lib/text-size/tests/main.rs b/lib/text-size/tests/main.rs
index 9a20cf9819..cd299d0b66 100644
--- a/lib/text-size/tests/main.rs
+++ b/lib/text-size/tests/main.rs
@@ -32,8 +32,8 @@ fn checked_math() {
#[test]
#[rustfmt::skip]
fn contains() {
- assert!( range(2..4).contains(range(2..3)));
- assert!( ! range(2..4).contains(range(1..3)));
+ assert!( range(2..4).contains_range(range(2..3)));
+ assert!( ! range(2..4).contains_range(range(1..3)));
}
#[test]
@@ -59,11 +59,11 @@ fn covering() {
#[test]
#[rustfmt::skip]
fn contains_point() {
- assert!( ! range(1..3).contains_exclusive(size(0)));
- assert!( range(1..3).contains_exclusive(size(1)));
- assert!( range(1..3).contains_exclusive(size(2)));
- assert!( ! range(1..3).contains_exclusive(size(3)));
- assert!( ! range(1..3).contains_exclusive(size(4)));
+ assert!( ! range(1..3).contains(size(0)));
+ assert!( range(1..3).contains(size(1)));
+ assert!( range(1..3).contains(size(2)));
+ assert!( ! range(1..3).contains(size(3)));
+ assert!( ! range(1..3).contains(size(4)));
assert!( ! range(1..3).contains_inclusive(size(0)));
assert!( range(1..3).contains_inclusive(size(1)));