Add utility methods to Range

This commit is contained in:
Seth Fowler 2013-06-26 15:45:03 -07:00
parent 39c3a6ff1d
commit 0ac520631a

View file

@ -3,6 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use std::uint;
use std::cmp::{max, min};
enum RangeRelation {
OverlapsBegin(/* overlap */ uint),
@ -51,6 +52,10 @@ impl Range {
self.begin() < s.len() && self.end() <= s.len() && self.length() <= s.len()
}
pub fn is_empty(&self) -> bool {
self.len == 0
}
pub fn shift_by(&mut self, i: int) {
self.off = ((self.off as int) + i) as uint;
}
@ -73,6 +78,17 @@ impl Range {
self.len = len_i;
}
pub fn intersect(&self, other: &Range) -> Range {
let begin = max(self.begin(), other.begin());
let end = min(self.end(), other.end());
if end < begin {
Range::empty()
} else {
Range::new(begin, end - begin)
}
}
/// Computes the relationship between two ranges (`self` and `other`),
/// from the point of view of `self`. So, 'EntirelyBefore' means
/// that the `self` range is entirely before `other` range.