Skip to content

Commit

Permalink
Add utility methods to Range
Browse files Browse the repository at this point in the history
  • Loading branch information
sethfowler committed Jun 27, 2013
1 parent 39c3a6f commit 0ac5206
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/components/util/range.rs
Expand Up @@ -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),
Expand Down Expand Up @@ -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;
}
Expand All @@ -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.
Expand Down

0 comments on commit 0ac5206

Please sign in to comment.