Skip to content
Permalink
Browse files

Add utility methods to Range

  • Loading branch information...
sethfowler committed Jun 26, 2013
1 parent 39c3a6f commit 0ac520631a861e2ce5e652ac5413035f5913d138
Showing with 16 additions and 0 deletions.
  1. +16 −0 src/components/util/range.rs
@@ -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.

0 comments on commit 0ac5206

Please sign in to comment.
You can’t perform that action at this time.