@@ -140,6 +140,11 @@ impl Range {
140 self.from() == other.from() || (self.to() > other.from() && other.to() > self.from())
140 self.from() == other.from() || (self.to() > other.from() && other.to() > self.from())
144+ pub fn contains_range(&self, other: &Self) -> bool {
145+ self.from() <= other.from() && self.to() >= other.to()
143 pub fn contains(&self, pos: usize) -> bool {
148 pub fn contains(&self, pos: usize) -> bool {
144 self.from() <= pos && pos < self.to()
149 self.from() <= pos && pos < self.to()
@@ -544,6 +549,39 @@ impl Selection {
544 pub fn len(&self) -> usize {
549 pub fn len(&self) -> usize {
545 self.ranges.len()
550 self.ranges.len()
553+ // returns true if self ⊇ other
554+ pub fn contains(&self, other: &Selection) -> bool {
555+ // can't contain other if it is larger
556+ if other.len() > self.len() {
560+ let (mut iter_self, mut iter_other) = (self.iter(), other.iter());
561+ let (mut ele_self, mut ele_other) = (iter_self.next(), iter_other.next());
564+ match (ele_self, ele_other) {
565+ (Some(ra), Some(rb)) => {
566+ if !ra.contains_range(rb) {
567+ // `self` doesn't contain next element from `other`, advance `self`, we need to match all from `other`
568+ ele_self = iter_self.next();
570+ // matched element from `other`, advance `other`
571+ ele_other = iter_other.next();
574+ (None, Some(_)) => {
575+ // exhausted `self`, we can't match the reminder of `other`
579+ // no elements from `other` left to match, `self` contains `other`
549impl<'a> IntoIterator for &'a Selection {
587impl<'a> IntoIterator for &'a Selection {
@@ -982,4 +1020,30 @@ mod test {
982 &["", "abcd", "efg", "rs", "xyz"]
1020 &["", "abcd", "efg", "rs", "xyz"]
1024+ fn test_selection_contains() {
1025+ fn contains(a: Vec<(usize, usize)>, b: Vec<(usize, usize)>) -> bool {
1026+ let sela = Selection::new(a.iter().map(|a| Range::new(a.0, a.1)).collect(), 0);
1027+ let selb = Selection::new(b.iter().map(|b| Range::new(b.0, b.1)).collect(), 0);
1028+ sela.contains(&selb)
1032+ assert!(contains(vec!((1, 1)), vec!((1, 1))));
1034+ // larger set contains smaller
1035+ assert!(contains(vec!((1, 1), (2, 2), (3, 3)), vec!((2, 2))));
1037+ // multiple matches
1038+ assert!(contains(vec!((1, 1), (2, 2)), vec!((1, 1), (2, 2))));
1040+ // smaller set can't contain bigger
1041+ assert!(!contains(vec!((1, 1)), vec!((1, 1), (2, 2))));
1044+ vec!((1, 1), (2, 4), (5, 6), (7, 9), (10, 13)),
1045+ vec!((3, 4), (7, 9))
1047+ assert!(!contains(vec!((1, 1), (5, 6)), vec!((1, 6))));