Skip to content

Commit 85b96cf

Browse files
committed
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.
1 parent d0560c7 commit 85b96cf

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

src/range.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,23 @@ impl TextRange {
8888

8989
/// Manipulation methods.
9090
impl TextRange {
91+
/// Check if this range contains an offset.
92+
///
93+
/// The end index is considered excluded.
94+
pub fn contains(self, offset: TextSize) -> bool {
95+
self.start() <= offset && offset < self.end()
96+
}
97+
98+
/// Check if this range contains an offset.
99+
///
100+
/// The end index is considered included.
101+
pub fn contains_inclusive(self, offset: TextSize) -> bool {
102+
let point = offset.into();
103+
self.start() <= point && point <= self.end()
104+
}
105+
91106
/// Check if this range completely contains another range.
92-
pub fn contains(self, other: TextRange) -> bool {
107+
pub fn contains_range(self, other: TextRange) -> bool {
93108
self.start() <= other.start() && other.end() <= self.end()
94109
}
95110

@@ -110,21 +125,6 @@ impl TextRange {
110125
let end = cmp::max(lhs.end(), rhs.end());
111126
TextRange(start, end)
112127
}
113-
114-
/// Check if this range contains an offset.
115-
///
116-
/// The end index is considered excluded.
117-
pub fn contains_exclusive(self, offset: TextSize) -> bool {
118-
self.start() <= offset && offset < self.end()
119-
}
120-
121-
/// Check if this range contains an offset.
122-
///
123-
/// The end index is considered included.
124-
pub fn contains_inclusive(self, offset: TextSize) -> bool {
125-
let point = offset.into();
126-
self.start() <= point && point <= self.end()
127-
}
128128
}
129129

130130
fn ix(size: TextSize) -> usize {

tests/main.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ fn checked_math() {
3232
#[test]
3333
#[rustfmt::skip]
3434
fn contains() {
35-
assert!( range(2..4).contains(range(2..3)));
36-
assert!( ! range(2..4).contains(range(1..3)));
35+
assert!( range(2..4).contains_range(range(2..3)));
36+
assert!( ! range(2..4).contains_range(range(1..3)));
3737
}
3838

3939
#[test]
@@ -59,11 +59,11 @@ fn covering() {
5959
#[test]
6060
#[rustfmt::skip]
6161
fn contains_point() {
62-
assert!( ! range(1..3).contains_exclusive(size(0)));
63-
assert!( range(1..3).contains_exclusive(size(1)));
64-
assert!( range(1..3).contains_exclusive(size(2)));
65-
assert!( ! range(1..3).contains_exclusive(size(3)));
66-
assert!( ! range(1..3).contains_exclusive(size(4)));
62+
assert!( ! range(1..3).contains(size(0)));
63+
assert!( range(1..3).contains(size(1)));
64+
assert!( range(1..3).contains(size(2)));
65+
assert!( ! range(1..3).contains(size(3)));
66+
assert!( ! range(1..3).contains(size(4)));
6767

6868
assert!( ! range(1..3).contains_inclusive(size(0)));
6969
assert!( range(1..3).contains_inclusive(size(1)));

0 commit comments

Comments
 (0)