mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +01:00
Add utility methods to Range
This commit is contained in:
parent
39c3a6ff1d
commit
0ac520631a
1 changed files with 16 additions and 0 deletions
|
@ -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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue