Skip to content

Commit 1d25dd1

Browse files
bors[bot]matklad
andauthored
Merge #22
22: Make intersect and cover into methods r=matklad a=matklad r? @CAD97 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 3bae1bd + 8c16c6a commit 1d25dd1

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

src/range.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,22 +118,27 @@ impl TextRange {
118118

119119
/// The range covered by both ranges, if it exists.
120120
/// If the ranges touch but do not overlap, the output range is empty.
121-
pub fn intersection(lhs: TextRange, rhs: TextRange) -> Option<TextRange> {
122-
let start = cmp::max(lhs.start(), rhs.start());
123-
let end = cmp::min(lhs.end(), rhs.end());
121+
pub fn intersect(self, other: TextRange) -> Option<TextRange> {
122+
let start = cmp::max(self.start(), other.start());
123+
let end = cmp::min(self.end(), other.end());
124124
if end < start {
125125
return None;
126126
}
127127
Some(TextRange(start, end))
128128
}
129129

130-
/// The smallest range that completely contains both ranges.
131-
pub fn covering(lhs: TextRange, rhs: TextRange) -> TextRange {
132-
let start = cmp::min(lhs.start(), rhs.start());
133-
let end = cmp::max(lhs.end(), rhs.end());
130+
/// Extends the range to cover `other` as well.
131+
pub fn cover(self, other: TextRange) -> TextRange {
132+
let start = cmp::min(self.start(), other.start());
133+
let end = cmp::max(self.end(), other.end());
134134
TextRange(start, end)
135135
}
136136

137+
/// Extends the range to cover `other` offsets as well.
138+
pub fn cover_offset(self, offset: TextSize) -> TextRange {
139+
self.cover(TextRange::empty(offset))
140+
}
141+
137142
/// Add an offset to this range.
138143
///
139144
/// Note that this is not appropriate for changing where a `TextRange` is

tests/main.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,26 @@ fn contains() {
3737
}
3838

3939
#[test]
40-
fn intersection() {
41-
assert_eq!(
42-
TextRange::intersection(range(1..2), range(2..3)),
43-
Some(range(2..2))
44-
);
45-
assert_eq!(
46-
TextRange::intersection(range(1..5), range(2..3)),
47-
Some(range(2..3))
48-
);
49-
assert_eq!(TextRange::intersection(range(1..2), range(3..4)), None);
40+
fn intersect() {
41+
assert_eq!(range(1..2).intersect(range(2..3)), Some(range(2..2)));
42+
assert_eq!(range(1..5).intersect(range(2..3)), Some(range(2..3)));
43+
assert_eq!(range(1..2).intersect(range(3..4)), None);
5044
}
5145

5246
#[test]
53-
fn covering() {
54-
assert_eq!(TextRange::covering(range(1..2), range(2..3)), range(1..3));
55-
assert_eq!(TextRange::covering(range(1..5), range(2..3)), range(1..5));
56-
assert_eq!(TextRange::covering(range(1..2), range(4..5)), range(1..5));
47+
fn cover() {
48+
assert_eq!(range(1..2).cover(range(2..3)), range(1..3));
49+
assert_eq!(range(1..5).cover(range(2..3)), range(1..5));
50+
assert_eq!(range(1..2).cover(range(4..5)), range(1..5));
51+
}
52+
53+
#[test]
54+
fn cover_offset() {
55+
assert_eq!(range(1..3).cover_offset(size(0)), range(0..3));
56+
assert_eq!(range(1..3).cover_offset(size(1)), range(1..3));
57+
assert_eq!(range(1..3).cover_offset(size(2)), range(1..3));
58+
assert_eq!(range(1..3).cover_offset(size(3)), range(1..3));
59+
assert_eq!(range(1..3).cover_offset(size(4)), range(1..4));
5760
}
5861

5962
#[test]

0 commit comments

Comments
 (0)