/src/regex/regex-syntax/src/hir/interval.rs
Line | Count | Source |
1 | | use core::{char, cmp, fmt::Debug, slice}; |
2 | | |
3 | | use alloc::vec::Vec; |
4 | | |
5 | | use crate::unicode; |
6 | | |
7 | | // This module contains an *internal* implementation of interval sets. |
8 | | // |
9 | | // The primary invariant that interval sets guards is canonical ordering. That |
10 | | // is, every interval set contains an ordered sequence of intervals where |
11 | | // no two intervals are overlapping or adjacent. While this invariant is |
12 | | // occasionally broken within the implementation, it should be impossible for |
13 | | // callers to observe it. |
14 | | // |
15 | | // Since case folding (as implemented below) breaks that invariant, we roll |
16 | | // that into this API even though it is a little out of place in an otherwise |
17 | | // generic interval set. (Hence the reason why the `unicode` module is imported |
18 | | // here.) |
19 | | // |
20 | | // Some of the implementation complexity here is a result of me wanting to |
21 | | // preserve the sequential representation without using additional memory. |
22 | | // In many cases, we do use linear extra memory, but it is at most 2x and it |
23 | | // is amortized. If we relaxed the memory requirements, this implementation |
24 | | // could become much simpler. The extra memory is honestly probably OK, but |
25 | | // character classes (especially of the Unicode variety) can become quite |
26 | | // large, and it would be nice to keep regex compilation snappy even in debug |
27 | | // builds. (In the past, I have been careless with this area of code and it has |
28 | | // caused slow regex compilations in debug mode, so this isn't entirely |
29 | | // unwarranted.) |
30 | | // |
31 | | // Tests on this are relegated to the public API of HIR in src/hir.rs. |
32 | | |
33 | | #[derive(Clone, Debug)] |
34 | | pub struct IntervalSet<I> { |
35 | | /// A sorted set of non-overlapping ranges. |
36 | | ranges: Vec<I>, |
37 | | /// While not required at all for correctness, we keep track of whether an |
38 | | /// interval set has been case folded or not. This helps us avoid doing |
39 | | /// redundant work if, for example, a set has already been cased folded. |
40 | | /// And note that whether a set is folded or not is preserved through |
41 | | /// all of the pairwise set operations. That is, if both interval sets |
42 | | /// have been case folded, then any of difference, union, intersection or |
43 | | /// symmetric difference all produce a case folded set. |
44 | | /// |
45 | | /// Note that when this is true, it *must* be the case that the set is case |
46 | | /// folded. But when it's false, the set *may* be case folded. In other |
47 | | /// words, we only set this to true when we know it to be case, but we're |
48 | | /// okay with it being false if it would otherwise be costly to determine |
49 | | /// whether it should be true. This means code cannot assume that a false |
50 | | /// value necessarily indicates that the set is not case folded. |
51 | | /// |
52 | | /// Bottom line: this is a performance optimization. |
53 | | folded: bool, |
54 | | } |
55 | | |
56 | | impl<I: Interval> Eq for IntervalSet<I> {} |
57 | | |
58 | | // We implement PartialEq manually so that we don't consider the set's internal |
59 | | // 'folded' property to be part of its identity. The 'folded' property is |
60 | | // strictly an optimization. |
61 | | impl<I: Interval> PartialEq for IntervalSet<I> { |
62 | 27.0k | fn eq(&self, other: &IntervalSet<I>) -> bool { |
63 | 27.0k | self.ranges.eq(&other.ranges) |
64 | 27.0k | } <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange> as core::cmp::PartialEq>::eq Line | Count | Source | 62 | 5.24k | fn eq(&self, other: &IntervalSet<I>) -> bool { | 63 | 5.24k | self.ranges.eq(&other.ranges) | 64 | 5.24k | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange> as core::cmp::PartialEq>::eq Line | Count | Source | 62 | 21.8k | fn eq(&self, other: &IntervalSet<I>) -> bool { | 63 | 21.8k | self.ranges.eq(&other.ranges) | 64 | 21.8k | } |
|
65 | | } |
66 | | |
67 | | impl<I: Interval> IntervalSet<I> { |
68 | | /// Create a new set from a sequence of intervals. Each interval is |
69 | | /// specified as a pair of bounds, where both bounds are inclusive. |
70 | | /// |
71 | | /// The given ranges do not need to be in any specific order, and ranges |
72 | | /// may overlap. |
73 | 1.53M | pub fn new<T: IntoIterator<Item = I>>(intervals: T) -> IntervalSet<I> { |
74 | 1.53M | let ranges: Vec<I> = intervals.into_iter().collect(); |
75 | | // An empty set is case folded. |
76 | 1.53M | let folded = ranges.is_empty(); |
77 | 1.53M | let mut set = IntervalSet { ranges, folded }; |
78 | 1.53M | set.canonicalize(); |
79 | 1.53M | set |
80 | 1.53M | } <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::new::<[regex_syntax::hir::ClassBytesRange; 1]> Line | Count | Source | 73 | 6.25k | pub fn new<T: IntoIterator<Item = I>>(intervals: T) -> IntervalSet<I> { | 74 | 6.25k | let ranges: Vec<I> = intervals.into_iter().collect(); | 75 | | // An empty set is case folded. | 76 | 6.25k | let folded = ranges.is_empty(); | 77 | 6.25k | let mut set = IntervalSet { ranges, folded }; | 78 | 6.25k | set.canonicalize(); | 79 | 6.25k | set | 80 | 6.25k | } |
Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::new::<[regex_syntax::hir::ClassBytesRange; 2]> <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::new::<[regex_syntax::hir::ClassBytesRange; 3]> Line | Count | Source | 73 | 572 | pub fn new<T: IntoIterator<Item = I>>(intervals: T) -> IntervalSet<I> { | 74 | 572 | let ranges: Vec<I> = intervals.into_iter().collect(); | 75 | | // An empty set is case folded. | 76 | 572 | let folded = ranges.is_empty(); | 77 | 572 | let mut set = IntervalSet { ranges, folded }; | 78 | 572 | set.canonicalize(); | 79 | 572 | set | 80 | 572 | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::new::<alloc::vec::Vec<regex_syntax::hir::ClassBytesRange>> Line | Count | Source | 73 | 409k | pub fn new<T: IntoIterator<Item = I>>(intervals: T) -> IntervalSet<I> { | 74 | 409k | let ranges: Vec<I> = intervals.into_iter().collect(); | 75 | | // An empty set is case folded. | 76 | 409k | let folded = ranges.is_empty(); | 77 | 409k | let mut set = IntervalSet { ranges, folded }; | 78 | 409k | set.canonicalize(); | 79 | 409k | set | 80 | 409k | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::new::<core::iter::adapters::map::Map<core::iter::adapters::copied::Copied<core::slice::iter::Iter<(u8, u8)>>, <regex_syntax::hir::translate::TranslatorI>::hir_ascii_byte_class::{closure#0}>>Line | Count | Source | 73 | 26.5k | pub fn new<T: IntoIterator<Item = I>>(intervals: T) -> IntervalSet<I> { | 74 | 26.5k | let ranges: Vec<I> = intervals.into_iter().collect(); | 75 | | // An empty set is case folded. | 76 | 26.5k | let folded = ranges.is_empty(); | 77 | 26.5k | let mut set = IntervalSet { ranges, folded }; | 78 | 26.5k | set.canonicalize(); | 79 | 26.5k | set | 80 | 26.5k | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::new::<core::iter::adapters::map::Map<core::slice::iter::Iter<regex_syntax::hir::ClassUnicodeRange>, <regex_syntax::hir::ClassUnicode>::to_byte_class::{closure#0}>>Line | Count | Source | 73 | 3.03k | pub fn new<T: IntoIterator<Item = I>>(intervals: T) -> IntervalSet<I> { | 74 | 3.03k | let ranges: Vec<I> = intervals.into_iter().collect(); | 75 | | // An empty set is case folded. | 76 | 3.03k | let folded = ranges.is_empty(); | 77 | 3.03k | let mut set = IntervalSet { ranges, folded }; | 78 | 3.03k | set.canonicalize(); | 79 | 3.03k | set | 80 | 3.03k | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::new::<core::iter::adapters::map::Map<alloc::vec::into_iter::IntoIter<u8>, <regex_syntax::hir::Hir>::alternation::{closure#1}>>Line | Count | Source | 73 | 6 | pub fn new<T: IntoIterator<Item = I>>(intervals: T) -> IntervalSet<I> { | 74 | 6 | let ranges: Vec<I> = intervals.into_iter().collect(); | 75 | | // An empty set is case folded. | 76 | 6 | let folded = ranges.is_empty(); | 77 | 6 | let mut set = IntervalSet { ranges, folded }; | 78 | 6 | set.canonicalize(); | 79 | 6 | set | 80 | 6 | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::new::<[regex_syntax::hir::ClassUnicodeRange; 1]> Line | Count | Source | 73 | 45.4k | pub fn new<T: IntoIterator<Item = I>>(intervals: T) -> IntervalSet<I> { | 74 | 45.4k | let ranges: Vec<I> = intervals.into_iter().collect(); | 75 | | // An empty set is case folded. | 76 | 45.4k | let folded = ranges.is_empty(); | 77 | 45.4k | let mut set = IntervalSet { ranges, folded }; | 78 | 45.4k | set.canonicalize(); | 79 | 45.4k | set | 80 | 45.4k | } |
Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::new::<[regex_syntax::hir::ClassUnicodeRange; 2]> <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::new::<[regex_syntax::hir::ClassUnicodeRange; 3]> Line | Count | Source | 73 | 3.53k | pub fn new<T: IntoIterator<Item = I>>(intervals: T) -> IntervalSet<I> { | 74 | 3.53k | let ranges: Vec<I> = intervals.into_iter().collect(); | 75 | | // An empty set is case folded. | 76 | 3.53k | let folded = ranges.is_empty(); | 77 | 3.53k | let mut set = IntervalSet { ranges, folded }; | 78 | 3.53k | set.canonicalize(); | 79 | 3.53k | set | 80 | 3.53k | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::new::<alloc::vec::Vec<regex_syntax::hir::ClassUnicodeRange>> Line | Count | Source | 73 | 843k | pub fn new<T: IntoIterator<Item = I>>(intervals: T) -> IntervalSet<I> { | 74 | 843k | let ranges: Vec<I> = intervals.into_iter().collect(); | 75 | | // An empty set is case folded. | 76 | 843k | let folded = ranges.is_empty(); | 77 | 843k | let mut set = IntervalSet { ranges, folded }; | 78 | 843k | set.canonicalize(); | 79 | 843k | set | 80 | 843k | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::new::<core::iter::adapters::map::Map<core::iter::adapters::map::Map<core::iter::adapters::copied::Copied<core::slice::iter::Iter<(u8, u8)>>, regex_syntax::hir::translate::ascii_class_as_chars::{closure#0}>, <regex_syntax::hir::translate::TranslatorI>::hir_ascii_unicode_class::{closure#0}>>Line | Count | Source | 73 | 54.7k | pub fn new<T: IntoIterator<Item = I>>(intervals: T) -> IntervalSet<I> { | 74 | 54.7k | let ranges: Vec<I> = intervals.into_iter().collect(); | 75 | | // An empty set is case folded. | 76 | 54.7k | let folded = ranges.is_empty(); | 77 | 54.7k | let mut set = IntervalSet { ranges, folded }; | 78 | 54.7k | set.canonicalize(); | 79 | 54.7k | set | 80 | 54.7k | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::new::<core::iter::adapters::map::Map<core::slice::iter::Iter<regex_syntax::hir::ClassBytesRange>, <regex_syntax::hir::ClassBytes>::to_unicode_class::{closure#0}>>Line | Count | Source | 73 | 7.18k | pub fn new<T: IntoIterator<Item = I>>(intervals: T) -> IntervalSet<I> { | 74 | 7.18k | let ranges: Vec<I> = intervals.into_iter().collect(); | 75 | | // An empty set is case folded. | 76 | 7.18k | let folded = ranges.is_empty(); | 77 | 7.18k | let mut set = IntervalSet { ranges, folded }; | 78 | 7.18k | set.canonicalize(); | 79 | 7.18k | set | 80 | 7.18k | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::new::<core::iter::adapters::map::Map<alloc::vec::into_iter::IntoIter<char>, <regex_syntax::hir::Hir>::alternation::{closure#0}>>Line | Count | Source | 73 | 1.68k | pub fn new<T: IntoIterator<Item = I>>(intervals: T) -> IntervalSet<I> { | 74 | 1.68k | let ranges: Vec<I> = intervals.into_iter().collect(); | 75 | | // An empty set is case folded. | 76 | 1.68k | let folded = ranges.is_empty(); | 77 | 1.68k | let mut set = IntervalSet { ranges, folded }; | 78 | 1.68k | set.canonicalize(); | 79 | 1.68k | set | 80 | 1.68k | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::new::<[regex_syntax::hir::ClassBytesRange; 1]> Line | Count | Source | 73 | 136k | pub fn new<T: IntoIterator<Item = I>>(intervals: T) -> IntervalSet<I> { | 74 | 136k | let ranges: Vec<I> = intervals.into_iter().collect(); | 75 | | // An empty set is case folded. | 76 | 136k | let folded = ranges.is_empty(); | 77 | 136k | let mut set = IntervalSet { ranges, folded }; | 78 | 136k | set.canonicalize(); | 79 | 136k | set | 80 | 136k | } |
Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::new::<[regex_syntax::hir::ClassBytesRange; 2]> Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::new::<[regex_syntax::hir::ClassBytesRange; 3]> Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::new::<[regex_syntax::hir::ClassUnicodeRange; 1]> Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::new::<[regex_syntax::hir::ClassUnicodeRange; 2]> Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::new::<[regex_syntax::hir::ClassUnicodeRange; 3]> |
81 | | |
82 | | /// Add a new interval to this set. |
83 | 992k | pub fn push(&mut self, interval: I) { |
84 | | // TODO: This could be faster. e.g., Push the interval such that |
85 | | // it preserves canonicalization. |
86 | 992k | self.ranges.push(interval); |
87 | 992k | self.canonicalize(); |
88 | | // We don't know whether the new interval added here is considered |
89 | | // case folded, so we conservatively assume that the entire set is |
90 | | // no longer case folded if it was previously. |
91 | 992k | self.folded = false; |
92 | 992k | } <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::push Line | Count | Source | 83 | 545k | pub fn push(&mut self, interval: I) { | 84 | | // TODO: This could be faster. e.g., Push the interval such that | 85 | | // it preserves canonicalization. | 86 | 545k | self.ranges.push(interval); | 87 | 545k | self.canonicalize(); | 88 | | // We don't know whether the new interval added here is considered | 89 | | // case folded, so we conservatively assume that the entire set is | 90 | | // no longer case folded if it was previously. | 91 | 545k | self.folded = false; | 92 | 545k | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::push Line | Count | Source | 83 | 447k | pub fn push(&mut self, interval: I) { | 84 | | // TODO: This could be faster. e.g., Push the interval such that | 85 | | // it preserves canonicalization. | 86 | 447k | self.ranges.push(interval); | 87 | 447k | self.canonicalize(); | 88 | | // We don't know whether the new interval added here is considered | 89 | | // case folded, so we conservatively assume that the entire set is | 90 | | // no longer case folded if it was previously. | 91 | 447k | self.folded = false; | 92 | 447k | } |
|
93 | | |
94 | | /// Return an iterator over all intervals in this set. |
95 | | /// |
96 | | /// The iterator yields intervals in ascending order. |
97 | 10.0M | pub fn iter(&self) -> IntervalSetIter<'_, I> { |
98 | 10.0M | IntervalSetIter(self.ranges.iter()) |
99 | 10.0M | } <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::iter Line | Count | Source | 97 | 4.26M | pub fn iter(&self) -> IntervalSetIter<'_, I> { | 98 | 4.26M | IntervalSetIter(self.ranges.iter()) | 99 | 4.26M | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::iter Line | Count | Source | 97 | 5.73M | pub fn iter(&self) -> IntervalSetIter<'_, I> { | 98 | 5.73M | IntervalSetIter(self.ranges.iter()) | 99 | 5.73M | } |
|
100 | | |
101 | | /// Return an immutable slice of intervals in this set. |
102 | | /// |
103 | | /// The sequence returned is in canonical ordering. |
104 | 15.9M | pub fn intervals(&self) -> &[I] { |
105 | 15.9M | &self.ranges |
106 | 15.9M | } <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::intervals Line | Count | Source | 104 | 5.82M | pub fn intervals(&self) -> &[I] { | 105 | 5.82M | &self.ranges | 106 | 5.82M | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::intervals Line | Count | Source | 104 | 10.1M | pub fn intervals(&self) -> &[I] { | 105 | 10.1M | &self.ranges | 106 | 10.1M | } |
|
107 | | |
108 | | /// Expand this interval set such that it contains all case folded |
109 | | /// characters. For example, if this class consists of the range `a-z`, |
110 | | /// then applying case folding will result in the class containing both the |
111 | | /// ranges `a-z` and `A-Z`. |
112 | | /// |
113 | | /// This returns an error if the necessary case mapping data is not |
114 | | /// available. |
115 | 583k | pub fn case_fold_simple(&mut self) -> Result<(), unicode::CaseFoldError> { |
116 | 583k | if self.folded { |
117 | 91.7k | return Ok(()); |
118 | 492k | } |
119 | 492k | let len = self.ranges.len(); |
120 | 5.23M | for i in 0..len { |
121 | 5.23M | let range = self.ranges[i]; |
122 | 5.23M | if let Err(err) = range.case_fold_simple(&mut self.ranges) { |
123 | 0 | self.canonicalize(); |
124 | 0 | return Err(err); |
125 | 5.23M | } |
126 | | } |
127 | 492k | self.canonicalize(); |
128 | 492k | self.folded = true; |
129 | 492k | Ok(()) |
130 | 583k | } <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::case_fold_simple Line | Count | Source | 115 | 224k | pub fn case_fold_simple(&mut self) -> Result<(), unicode::CaseFoldError> { | 116 | 224k | if self.folded { | 117 | 61.6k | return Ok(()); | 118 | 163k | } | 119 | 163k | let len = self.ranges.len(); | 120 | 885k | for i in 0..len { | 121 | 885k | let range = self.ranges[i]; | 122 | 885k | if let Err(err) = range.case_fold_simple(&mut self.ranges) { | 123 | 0 | self.canonicalize(); | 124 | 0 | return Err(err); | 125 | 885k | } | 126 | | } | 127 | 163k | self.canonicalize(); | 128 | 163k | self.folded = true; | 129 | 163k | Ok(()) | 130 | 224k | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::case_fold_simple Line | Count | Source | 115 | 358k | pub fn case_fold_simple(&mut self) -> Result<(), unicode::CaseFoldError> { | 116 | 358k | if self.folded { | 117 | 30.0k | return Ok(()); | 118 | 328k | } | 119 | 328k | let len = self.ranges.len(); | 120 | 4.34M | for i in 0..len { | 121 | 4.34M | let range = self.ranges[i]; | 122 | 4.34M | if let Err(err) = range.case_fold_simple(&mut self.ranges) { | 123 | 0 | self.canonicalize(); | 124 | 0 | return Err(err); | 125 | 4.34M | } | 126 | | } | 127 | 328k | self.canonicalize(); | 128 | 328k | self.folded = true; | 129 | 328k | Ok(()) | 130 | 358k | } |
|
131 | | |
132 | | /// Union this set with the given set, in place. |
133 | 576k | pub fn union(&mut self, other: &IntervalSet<I>) { |
134 | 576k | if other.ranges.is_empty() || self.ranges == other.ranges { |
135 | 126k | return; |
136 | 450k | } |
137 | | // This could almost certainly be done more efficiently. |
138 | 450k | self.ranges.extend(&other.ranges); |
139 | 450k | self.canonicalize(); |
140 | 450k | self.folded = self.folded && other.folded; |
141 | 576k | } <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::union Line | Count | Source | 133 | 203k | pub fn union(&mut self, other: &IntervalSet<I>) { | 134 | 203k | if other.ranges.is_empty() || self.ranges == other.ranges { | 135 | 46.9k | return; | 136 | 156k | } | 137 | | // This could almost certainly be done more efficiently. | 138 | 156k | self.ranges.extend(&other.ranges); | 139 | 156k | self.canonicalize(); | 140 | 156k | self.folded = self.folded && other.folded; | 141 | 203k | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::union Line | Count | Source | 133 | 373k | pub fn union(&mut self, other: &IntervalSet<I>) { | 134 | 373k | if other.ranges.is_empty() || self.ranges == other.ranges { | 135 | 79.8k | return; | 136 | 293k | } | 137 | | // This could almost certainly be done more efficiently. | 138 | 293k | self.ranges.extend(&other.ranges); | 139 | 293k | self.canonicalize(); | 140 | 293k | self.folded = self.folded && other.folded; | 141 | 373k | } |
|
142 | | |
143 | | /// Intersect this set with the given set, in place. |
144 | 92.0k | pub fn intersect(&mut self, other: &IntervalSet<I>) { |
145 | 92.0k | if self.ranges.is_empty() { |
146 | 38.7k | return; |
147 | 53.2k | } |
148 | 53.2k | if other.ranges.is_empty() { |
149 | 14.0k | self.ranges.clear(); |
150 | | // An empty set is case folded. |
151 | 14.0k | self.folded = true; |
152 | 14.0k | return; |
153 | 39.2k | } |
154 | | |
155 | | // There should be a way to do this in-place with constant memory, |
156 | | // but I couldn't figure out a simple way to do it. So just append |
157 | | // the intersection to the end of this range, and then drain it before |
158 | | // we're done. |
159 | 39.2k | let drain_end = self.ranges.len(); |
160 | | |
161 | 39.2k | let mut ita = 0..drain_end; |
162 | 39.2k | let mut itb = 0..other.ranges.len(); |
163 | 39.2k | let mut a = ita.next().unwrap(); |
164 | 39.2k | let mut b = itb.next().unwrap(); |
165 | | loop { |
166 | 5.31M | if let Some(ab) = self.ranges[a].intersect(&other.ranges[b]) { |
167 | 2.45M | self.ranges.push(ab); |
168 | 2.86M | } |
169 | 5.31M | let (it, aorb) = |
170 | 5.31M | if self.ranges[a].upper() < other.ranges[b].upper() { |
171 | 2.65M | (&mut ita, &mut a) |
172 | | } else { |
173 | 2.65M | (&mut itb, &mut b) |
174 | | }; |
175 | 5.31M | match it.next() { |
176 | 5.27M | Some(v) => *aorb = v, |
177 | 39.2k | None => break, |
178 | | } |
179 | | } |
180 | 39.2k | self.ranges.drain(..drain_end); |
181 | 39.2k | self.folded = self.folded && other.folded; |
182 | 92.0k | } <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::intersect Line | Count | Source | 144 | 48.1k | pub fn intersect(&mut self, other: &IntervalSet<I>) { | 145 | 48.1k | if self.ranges.is_empty() { | 146 | 16.6k | return; | 147 | 31.4k | } | 148 | 31.4k | if other.ranges.is_empty() { | 149 | 7.45k | self.ranges.clear(); | 150 | | // An empty set is case folded. | 151 | 7.45k | self.folded = true; | 152 | 7.45k | return; | 153 | 24.0k | } | 154 | | | 155 | | // There should be a way to do this in-place with constant memory, | 156 | | // but I couldn't figure out a simple way to do it. So just append | 157 | | // the intersection to the end of this range, and then drain it before | 158 | | // we're done. | 159 | 24.0k | let drain_end = self.ranges.len(); | 160 | | | 161 | 24.0k | let mut ita = 0..drain_end; | 162 | 24.0k | let mut itb = 0..other.ranges.len(); | 163 | 24.0k | let mut a = ita.next().unwrap(); | 164 | 24.0k | let mut b = itb.next().unwrap(); | 165 | | loop { | 166 | 281k | if let Some(ab) = self.ranges[a].intersect(&other.ranges[b]) { | 167 | 106k | self.ranges.push(ab); | 168 | 175k | } | 169 | 281k | let (it, aorb) = | 170 | 281k | if self.ranges[a].upper() < other.ranges[b].upper() { | 171 | 166k | (&mut ita, &mut a) | 172 | | } else { | 173 | 115k | (&mut itb, &mut b) | 174 | | }; | 175 | 281k | match it.next() { | 176 | 257k | Some(v) => *aorb = v, | 177 | 24.0k | None => break, | 178 | | } | 179 | | } | 180 | 24.0k | self.ranges.drain(..drain_end); | 181 | 24.0k | self.folded = self.folded && other.folded; | 182 | 48.1k | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::intersect Line | Count | Source | 144 | 43.9k | pub fn intersect(&mut self, other: &IntervalSet<I>) { | 145 | 43.9k | if self.ranges.is_empty() { | 146 | 22.1k | return; | 147 | 21.8k | } | 148 | 21.8k | if other.ranges.is_empty() { | 149 | 6.59k | self.ranges.clear(); | 150 | | // An empty set is case folded. | 151 | 6.59k | self.folded = true; | 152 | 6.59k | return; | 153 | 15.2k | } | 154 | | | 155 | | // There should be a way to do this in-place with constant memory, | 156 | | // but I couldn't figure out a simple way to do it. So just append | 157 | | // the intersection to the end of this range, and then drain it before | 158 | | // we're done. | 159 | 15.2k | let drain_end = self.ranges.len(); | 160 | | | 161 | 15.2k | let mut ita = 0..drain_end; | 162 | 15.2k | let mut itb = 0..other.ranges.len(); | 163 | 15.2k | let mut a = ita.next().unwrap(); | 164 | 15.2k | let mut b = itb.next().unwrap(); | 165 | | loop { | 166 | 5.03M | if let Some(ab) = self.ranges[a].intersect(&other.ranges[b]) { | 167 | 2.34M | self.ranges.push(ab); | 168 | 2.68M | } | 169 | 5.03M | let (it, aorb) = | 170 | 5.03M | if self.ranges[a].upper() < other.ranges[b].upper() { | 171 | 2.49M | (&mut ita, &mut a) | 172 | | } else { | 173 | 2.54M | (&mut itb, &mut b) | 174 | | }; | 175 | 5.03M | match it.next() { | 176 | 5.01M | Some(v) => *aorb = v, | 177 | 15.2k | None => break, | 178 | | } | 179 | | } | 180 | 15.2k | self.ranges.drain(..drain_end); | 181 | 15.2k | self.folded = self.folded && other.folded; | 182 | 43.9k | } |
|
183 | | |
184 | | /// Subtract the given set from this set, in place. |
185 | 117k | pub fn difference(&mut self, other: &IntervalSet<I>) { |
186 | 117k | if self.ranges.is_empty() || other.ranges.is_empty() { |
187 | 68.7k | return; |
188 | 48.3k | } |
189 | | |
190 | | // This algorithm is (to me) surprisingly complex. A search of the |
191 | | // interwebs indicate that this is a potentially interesting problem. |
192 | | // Folks seem to suggest interval or segment trees, but I'd like to |
193 | | // avoid the overhead (both runtime and conceptual) of that. |
194 | | // |
195 | | // The following is basically my Shitty First Draft. Therefore, in |
196 | | // order to grok it, you probably need to read each line carefully. |
197 | | // Simplifications are most welcome! |
198 | | // |
199 | | // Remember, we can assume the canonical format invariant here, which |
200 | | // says that all ranges are sorted, not overlapping and not adjacent in |
201 | | // each class. |
202 | 48.3k | let drain_end = self.ranges.len(); |
203 | 48.3k | let (mut a, mut b) = (0, 0); |
204 | 2.97M | 'LOOP: while a < drain_end && b < other.ranges.len() { |
205 | | // Basically, the easy cases are when neither range overlaps with |
206 | | // each other. If the `b` range is less than our current `a` |
207 | | // range, then we can skip it and move on. |
208 | 2.92M | if other.ranges[b].upper() < self.ranges[a].lower() { |
209 | 1.06M | b += 1; |
210 | 1.06M | continue; |
211 | 1.86M | } |
212 | | // ... similarly for the `a` range. If it's less than the smallest |
213 | | // `b` range, then we can add it as-is. |
214 | 1.86M | if self.ranges[a].upper() < other.ranges[b].lower() { |
215 | 861k | let range = self.ranges[a]; |
216 | 861k | self.ranges.push(range); |
217 | 861k | a += 1; |
218 | 861k | continue; |
219 | 1.00M | } |
220 | | // Otherwise, we have overlapping ranges. |
221 | 1.00M | assert!(!self.ranges[a].is_intersection_empty(&other.ranges[b])); |
222 | | |
223 | | // This part is tricky and was non-obvious to me without looking |
224 | | // at explicit examples (see the tests). The trickiness stems from |
225 | | // two things: 1) subtracting a range from another range could |
226 | | // yield two ranges and 2) after subtracting a range, it's possible |
227 | | // that future ranges can have an impact. The loop below advances |
228 | | // the `b` ranges until they can't possible impact the current |
229 | | // range. |
230 | | // |
231 | | // For example, if our `a` range is `a-t` and our next three `b` |
232 | | // ranges are `a-c`, `g-i`, `r-t` and `x-z`, then we need to apply |
233 | | // subtraction three times before moving on to the next `a` range. |
234 | 1.00M | let mut range = self.ranges[a]; |
235 | 2.57M | while b < other.ranges.len() |
236 | 2.55M | && !range.is_intersection_empty(&other.ranges[b]) |
237 | | { |
238 | 2.46M | let old_range = range; |
239 | 2.46M | range = match range.difference(&other.ranges[b]) { |
240 | | (None, None) => { |
241 | | // We lost the entire range, so move on to the next |
242 | | // without adding this one. |
243 | 893k | a += 1; |
244 | 893k | continue 'LOOP; |
245 | | } |
246 | 100k | (Some(range1), None) | (None, Some(range1)) => range1, |
247 | 1.47M | (Some(range1), Some(range2)) => { |
248 | 1.47M | self.ranges.push(range1); |
249 | 1.47M | range2 |
250 | | } |
251 | | }; |
252 | | // It's possible that the `b` range has more to contribute |
253 | | // here. In particular, if it is greater than the original |
254 | | // range, then it might impact the next `a` range *and* it |
255 | | // has impacted the current `a` range as much as possible, |
256 | | // so we can quit. We don't bump `b` so that the next `a` |
257 | | // range can apply it. |
258 | 1.57M | if other.ranges[b].upper() > old_range.upper() { |
259 | 3.61k | break; |
260 | 1.57M | } |
261 | | // Otherwise, the next `b` range might apply to the current |
262 | | // `a` range. |
263 | 1.57M | b += 1; |
264 | | } |
265 | 109k | self.ranges.push(range); |
266 | 109k | a += 1; |
267 | | } |
268 | 1.17M | while a < drain_end { |
269 | 1.12M | let range = self.ranges[a]; |
270 | 1.12M | self.ranges.push(range); |
271 | 1.12M | a += 1; |
272 | 1.12M | } |
273 | 48.3k | self.ranges.drain(..drain_end); |
274 | 48.3k | self.folded = self.folded && other.folded; |
275 | 117k | } <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::difference Line | Count | Source | 185 | 60.8k | pub fn difference(&mut self, other: &IntervalSet<I>) { | 186 | 60.8k | if self.ranges.is_empty() || other.ranges.is_empty() { | 187 | 32.5k | return; | 188 | 28.3k | } | 189 | | | 190 | | // This algorithm is (to me) surprisingly complex. A search of the | 191 | | // interwebs indicate that this is a potentially interesting problem. | 192 | | // Folks seem to suggest interval or segment trees, but I'd like to | 193 | | // avoid the overhead (both runtime and conceptual) of that. | 194 | | // | 195 | | // The following is basically my Shitty First Draft. Therefore, in | 196 | | // order to grok it, you probably need to read each line carefully. | 197 | | // Simplifications are most welcome! | 198 | | // | 199 | | // Remember, we can assume the canonical format invariant here, which | 200 | | // says that all ranges are sorted, not overlapping and not adjacent in | 201 | | // each class. | 202 | 28.3k | let drain_end = self.ranges.len(); | 203 | 28.3k | let (mut a, mut b) = (0, 0); | 204 | 233k | 'LOOP: while a < drain_end && b < other.ranges.len() { | 205 | | // Basically, the easy cases are when neither range overlaps with | 206 | | // each other. If the `b` range is less than our current `a` | 207 | | // range, then we can skip it and move on. | 208 | 205k | if other.ranges[b].upper() < self.ranges[a].lower() { | 209 | 36.7k | b += 1; | 210 | 36.7k | continue; | 211 | 168k | } | 212 | | // ... similarly for the `a` range. If it's less than the smallest | 213 | | // `b` range, then we can add it as-is. | 214 | 168k | if self.ranges[a].upper() < other.ranges[b].lower() { | 215 | 102k | let range = self.ranges[a]; | 216 | 102k | self.ranges.push(range); | 217 | 102k | a += 1; | 218 | 102k | continue; | 219 | 65.9k | } | 220 | | // Otherwise, we have overlapping ranges. | 221 | 65.9k | assert!(!self.ranges[a].is_intersection_empty(&other.ranges[b])); | 222 | | | 223 | | // This part is tricky and was non-obvious to me without looking | 224 | | // at explicit examples (see the tests). The trickiness stems from | 225 | | // two things: 1) subtracting a range from another range could | 226 | | // yield two ranges and 2) after subtracting a range, it's possible | 227 | | // that future ranges can have an impact. The loop below advances | 228 | | // the `b` ranges until they can't possible impact the current | 229 | | // range. | 230 | | // | 231 | | // For example, if our `a` range is `a-t` and our next three `b` | 232 | | // ranges are `a-c`, `g-i`, `r-t` and `x-z`, then we need to apply | 233 | | // subtraction three times before moving on to the next `a` range. | 234 | 65.9k | let mut range = self.ranges[a]; | 235 | 156k | while b < other.ranges.len() | 236 | 141k | && !range.is_intersection_empty(&other.ranges[b]) | 237 | | { | 238 | 121k | let old_range = range; | 239 | 121k | range = match range.difference(&other.ranges[b]) { | 240 | | (None, None) => { | 241 | | // We lost the entire range, so move on to the next | 242 | | // without adding this one. | 243 | 29.7k | a += 1; | 244 | 29.7k | continue 'LOOP; | 245 | | } | 246 | 25.0k | (Some(range1), None) | (None, Some(range1)) => range1, | 247 | 66.4k | (Some(range1), Some(range2)) => { | 248 | 66.4k | self.ranges.push(range1); | 249 | 66.4k | range2 | 250 | | } | 251 | | }; | 252 | | // It's possible that the `b` range has more to contribute | 253 | | // here. In particular, if it is greater than the original | 254 | | // range, then it might impact the next `a` range *and* it | 255 | | // has impacted the current `a` range as much as possible, | 256 | | // so we can quit. We don't bump `b` so that the next `a` | 257 | | // range can apply it. | 258 | 91.5k | if other.ranges[b].upper() > old_range.upper() { | 259 | 612 | break; | 260 | 90.9k | } | 261 | | // Otherwise, the next `b` range might apply to the current | 262 | | // `a` range. | 263 | 90.9k | b += 1; | 264 | | } | 265 | 36.1k | self.ranges.push(range); | 266 | 36.1k | a += 1; | 267 | | } | 268 | 63.3k | while a < drain_end { | 269 | 35.0k | let range = self.ranges[a]; | 270 | 35.0k | self.ranges.push(range); | 271 | 35.0k | a += 1; | 272 | 35.0k | } | 273 | 28.3k | self.ranges.drain(..drain_end); | 274 | 28.3k | self.folded = self.folded && other.folded; | 275 | 60.8k | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::difference Line | Count | Source | 185 | 56.2k | pub fn difference(&mut self, other: &IntervalSet<I>) { | 186 | 56.2k | if self.ranges.is_empty() || other.ranges.is_empty() { | 187 | 36.1k | return; | 188 | 20.0k | } | 189 | | | 190 | | // This algorithm is (to me) surprisingly complex. A search of the | 191 | | // interwebs indicate that this is a potentially interesting problem. | 192 | | // Folks seem to suggest interval or segment trees, but I'd like to | 193 | | // avoid the overhead (both runtime and conceptual) of that. | 194 | | // | 195 | | // The following is basically my Shitty First Draft. Therefore, in | 196 | | // order to grok it, you probably need to read each line carefully. | 197 | | // Simplifications are most welcome! | 198 | | // | 199 | | // Remember, we can assume the canonical format invariant here, which | 200 | | // says that all ranges are sorted, not overlapping and not adjacent in | 201 | | // each class. | 202 | 20.0k | let drain_end = self.ranges.len(); | 203 | 20.0k | let (mut a, mut b) = (0, 0); | 204 | 2.74M | 'LOOP: while a < drain_end && b < other.ranges.len() { | 205 | | // Basically, the easy cases are when neither range overlaps with | 206 | | // each other. If the `b` range is less than our current `a` | 207 | | // range, then we can skip it and move on. | 208 | 2.72M | if other.ranges[b].upper() < self.ranges[a].lower() { | 209 | 1.02M | b += 1; | 210 | 1.02M | continue; | 211 | 1.69M | } | 212 | | // ... similarly for the `a` range. If it's less than the smallest | 213 | | // `b` range, then we can add it as-is. | 214 | 1.69M | if self.ranges[a].upper() < other.ranges[b].lower() { | 215 | 759k | let range = self.ranges[a]; | 216 | 759k | self.ranges.push(range); | 217 | 759k | a += 1; | 218 | 759k | continue; | 219 | 937k | } | 220 | | // Otherwise, we have overlapping ranges. | 221 | 937k | assert!(!self.ranges[a].is_intersection_empty(&other.ranges[b])); | 222 | | | 223 | | // This part is tricky and was non-obvious to me without looking | 224 | | // at explicit examples (see the tests). The trickiness stems from | 225 | | // two things: 1) subtracting a range from another range could | 226 | | // yield two ranges and 2) after subtracting a range, it's possible | 227 | | // that future ranges can have an impact. The loop below advances | 228 | | // the `b` ranges until they can't possible impact the current | 229 | | // range. | 230 | | // | 231 | | // For example, if our `a` range is `a-t` and our next three `b` | 232 | | // ranges are `a-c`, `g-i`, `r-t` and `x-z`, then we need to apply | 233 | | // subtraction three times before moving on to the next `a` range. | 234 | 937k | let mut range = self.ranges[a]; | 235 | 2.41M | while b < other.ranges.len() | 236 | 2.40M | && !range.is_intersection_empty(&other.ranges[b]) | 237 | | { | 238 | 2.34M | let old_range = range; | 239 | 2.34M | range = match range.difference(&other.ranges[b]) { | 240 | | (None, None) => { | 241 | | // We lost the entire range, so move on to the next | 242 | | // without adding this one. | 243 | 863k | a += 1; | 244 | 863k | continue 'LOOP; | 245 | | } | 246 | 75.0k | (Some(range1), None) | (None, Some(range1)) => range1, | 247 | 1.40M | (Some(range1), Some(range2)) => { | 248 | 1.40M | self.ranges.push(range1); | 249 | 1.40M | range2 | 250 | | } | 251 | | }; | 252 | | // It's possible that the `b` range has more to contribute | 253 | | // here. In particular, if it is greater than the original | 254 | | // range, then it might impact the next `a` range *and* it | 255 | | // has impacted the current `a` range as much as possible, | 256 | | // so we can quit. We don't bump `b` so that the next `a` | 257 | | // range can apply it. | 258 | 1.48M | if other.ranges[b].upper() > old_range.upper() { | 259 | 3.00k | break; | 260 | 1.48M | } | 261 | | // Otherwise, the next `b` range might apply to the current | 262 | | // `a` range. | 263 | 1.48M | b += 1; | 264 | | } | 265 | 73.1k | self.ranges.push(range); | 266 | 73.1k | a += 1; | 267 | | } | 268 | 1.11M | while a < drain_end { | 269 | 1.09M | let range = self.ranges[a]; | 270 | 1.09M | self.ranges.push(range); | 271 | 1.09M | a += 1; | 272 | 1.09M | } | 273 | 20.0k | self.ranges.drain(..drain_end); | 274 | 20.0k | self.folded = self.folded && other.folded; | 275 | 56.2k | } |
|
276 | | |
277 | | /// Compute the symmetric difference of the two sets, in place. |
278 | | /// |
279 | | /// This computes the symmetric difference of two interval sets. This |
280 | | /// removes all elements in this set that are also in the given set, |
281 | | /// but also adds all elements from the given set that aren't in this |
282 | | /// set. That is, the set will contain all elements in either set, |
283 | | /// but will not contain any elements that are in both sets. |
284 | 72.3k | pub fn symmetric_difference(&mut self, other: &IntervalSet<I>) { |
285 | | // TODO(burntsushi): Fix this so that it amortizes allocation. |
286 | 72.3k | let mut intersection = self.clone(); |
287 | 72.3k | intersection.intersect(other); |
288 | 72.3k | self.union(other); |
289 | 72.3k | self.difference(&intersection); |
290 | 72.3k | } <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::symmetric_difference Line | Count | Source | 284 | 41.3k | pub fn symmetric_difference(&mut self, other: &IntervalSet<I>) { | 285 | | // TODO(burntsushi): Fix this so that it amortizes allocation. | 286 | 41.3k | let mut intersection = self.clone(); | 287 | 41.3k | intersection.intersect(other); | 288 | 41.3k | self.union(other); | 289 | 41.3k | self.difference(&intersection); | 290 | 41.3k | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::symmetric_difference Line | Count | Source | 284 | 31.0k | pub fn symmetric_difference(&mut self, other: &IntervalSet<I>) { | 285 | | // TODO(burntsushi): Fix this so that it amortizes allocation. | 286 | 31.0k | let mut intersection = self.clone(); | 287 | 31.0k | intersection.intersect(other); | 288 | 31.0k | self.union(other); | 289 | 31.0k | self.difference(&intersection); | 290 | 31.0k | } |
|
291 | | |
292 | | /// Negate this interval set. |
293 | | /// |
294 | | /// For all `x` where `x` is any element, if `x` was in this set, then it |
295 | | /// will not be in this set after negation. |
296 | 233k | pub fn negate(&mut self) { |
297 | 233k | if self.ranges.is_empty() { |
298 | 5.38k | let (min, max) = (I::Bound::min_value(), I::Bound::max_value()); |
299 | 5.38k | self.ranges.push(I::create(min, max)); |
300 | | // The set containing everything must case folded. |
301 | 5.38k | self.folded = true; |
302 | 5.38k | return; |
303 | 228k | } |
304 | | |
305 | | // There should be a way to do this in-place with constant memory, |
306 | | // but I couldn't figure out a simple way to do it. So just append |
307 | | // the negation to the end of this range, and then drain it before |
308 | | // we're done. |
309 | 228k | let drain_end = self.ranges.len(); |
310 | | |
311 | | // We do checked arithmetic below because of the canonical ordering |
312 | | // invariant. |
313 | 228k | if self.ranges[0].lower() > I::Bound::min_value() { |
314 | 191k | let upper = self.ranges[0].lower().decrement(); |
315 | 191k | self.ranges.push(I::create(I::Bound::min_value(), upper)); |
316 | 191k | } |
317 | 15.7M | for i in 1..drain_end { |
318 | 15.7M | let lower = self.ranges[i - 1].upper().increment(); |
319 | 15.7M | let upper = self.ranges[i].lower().decrement(); |
320 | 15.7M | self.ranges.push(I::create(lower, upper)); |
321 | 15.7M | } |
322 | 228k | if self.ranges[drain_end - 1].upper() < I::Bound::max_value() { |
323 | 194k | let lower = self.ranges[drain_end - 1].upper().increment(); |
324 | 194k | self.ranges.push(I::create(lower, I::Bound::max_value())); |
325 | 194k | } |
326 | 228k | self.ranges.drain(..drain_end); |
327 | | // We don't need to update whether this set is folded or not, because |
328 | | // it is conservatively preserved through negation. Namely, if a set |
329 | | // is not folded, then it is possible that its negation is folded, for |
330 | | // example, [^☃]. But we're fine with assuming that the set is not |
331 | | // folded in that case. (`folded` permits false negatives but not false |
332 | | // positives.) |
333 | | // |
334 | | // But what about when a set is folded, is its negation also |
335 | | // necessarily folded? Yes. Because if a set is folded, then for every |
336 | | // character in the set, it necessarily included its equivalence class |
337 | | // of case folded characters. Negating it in turn means that all |
338 | | // equivalence classes in the set are negated, and any equivalence |
339 | | // class that was previously not in the set is now entirely in the set. |
340 | 233k | } <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::negate Line | Count | Source | 296 | 27.1k | pub fn negate(&mut self) { | 297 | 27.1k | if self.ranges.is_empty() { | 298 | 1.03k | let (min, max) = (I::Bound::min_value(), I::Bound::max_value()); | 299 | 1.03k | self.ranges.push(I::create(min, max)); | 300 | | // The set containing everything must case folded. | 301 | 1.03k | self.folded = true; | 302 | 1.03k | return; | 303 | 26.1k | } | 304 | | | 305 | | // There should be a way to do this in-place with constant memory, | 306 | | // but I couldn't figure out a simple way to do it. So just append | 307 | | // the negation to the end of this range, and then drain it before | 308 | | // we're done. | 309 | 26.1k | let drain_end = self.ranges.len(); | 310 | | | 311 | | // We do checked arithmetic below because of the canonical ordering | 312 | | // invariant. | 313 | 26.1k | if self.ranges[0].lower() > I::Bound::min_value() { | 314 | 22.9k | let upper = self.ranges[0].lower().decrement(); | 315 | 22.9k | self.ranges.push(I::create(I::Bound::min_value(), upper)); | 316 | 22.9k | } | 317 | 37.4k | for i in 1..drain_end { | 318 | 37.4k | let lower = self.ranges[i - 1].upper().increment(); | 319 | 37.4k | let upper = self.ranges[i].lower().decrement(); | 320 | 37.4k | self.ranges.push(I::create(lower, upper)); | 321 | 37.4k | } | 322 | 26.1k | if self.ranges[drain_end - 1].upper() < I::Bound::max_value() { | 323 | 23.3k | let lower = self.ranges[drain_end - 1].upper().increment(); | 324 | 23.3k | self.ranges.push(I::create(lower, I::Bound::max_value())); | 325 | 23.3k | } | 326 | 26.1k | self.ranges.drain(..drain_end); | 327 | | // We don't need to update whether this set is folded or not, because | 328 | | // it is conservatively preserved through negation. Namely, if a set | 329 | | // is not folded, then it is possible that its negation is folded, for | 330 | | // example, [^☃]. But we're fine with assuming that the set is not | 331 | | // folded in that case. (`folded` permits false negatives but not false | 332 | | // positives.) | 333 | | // | 334 | | // But what about when a set is folded, is its negation also | 335 | | // necessarily folded? Yes. Because if a set is folded, then for every | 336 | | // character in the set, it necessarily included its equivalence class | 337 | | // of case folded characters. Negating it in turn means that all | 338 | | // equivalence classes in the set are negated, and any equivalence | 339 | | // class that was previously not in the set is now entirely in the set. | 340 | 27.1k | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::negate Line | Count | Source | 296 | 206k | pub fn negate(&mut self) { | 297 | 206k | if self.ranges.is_empty() { | 298 | 4.34k | let (min, max) = (I::Bound::min_value(), I::Bound::max_value()); | 299 | 4.34k | self.ranges.push(I::create(min, max)); | 300 | | // The set containing everything must case folded. | 301 | 4.34k | self.folded = true; | 302 | 4.34k | return; | 303 | 202k | } | 304 | | | 305 | | // There should be a way to do this in-place with constant memory, | 306 | | // but I couldn't figure out a simple way to do it. So just append | 307 | | // the negation to the end of this range, and then drain it before | 308 | | // we're done. | 309 | 202k | let drain_end = self.ranges.len(); | 310 | | | 311 | | // We do checked arithmetic below because of the canonical ordering | 312 | | // invariant. | 313 | 202k | if self.ranges[0].lower() > I::Bound::min_value() { | 314 | 168k | let upper = self.ranges[0].lower().decrement(); | 315 | 168k | self.ranges.push(I::create(I::Bound::min_value(), upper)); | 316 | 168k | } | 317 | 15.7M | for i in 1..drain_end { | 318 | 15.7M | let lower = self.ranges[i - 1].upper().increment(); | 319 | 15.7M | let upper = self.ranges[i].lower().decrement(); | 320 | 15.7M | self.ranges.push(I::create(lower, upper)); | 321 | 15.7M | } | 322 | 202k | if self.ranges[drain_end - 1].upper() < I::Bound::max_value() { | 323 | 171k | let lower = self.ranges[drain_end - 1].upper().increment(); | 324 | 171k | self.ranges.push(I::create(lower, I::Bound::max_value())); | 325 | 171k | } | 326 | 202k | self.ranges.drain(..drain_end); | 327 | | // We don't need to update whether this set is folded or not, because | 328 | | // it is conservatively preserved through negation. Namely, if a set | 329 | | // is not folded, then it is possible that its negation is folded, for | 330 | | // example, [^☃]. But we're fine with assuming that the set is not | 331 | | // folded in that case. (`folded` permits false negatives but not false | 332 | | // positives.) | 333 | | // | 334 | | // But what about when a set is folded, is its negation also | 335 | | // necessarily folded? Yes. Because if a set is folded, then for every | 336 | | // character in the set, it necessarily included its equivalence class | 337 | | // of case folded characters. Negating it in turn means that all | 338 | | // equivalence classes in the set are negated, and any equivalence | 339 | | // class that was previously not in the set is now entirely in the set. | 340 | 206k | } |
|
341 | | |
342 | | /// Converts this set into a canonical ordering. |
343 | 3.47M | fn canonicalize(&mut self) { |
344 | 3.47M | if self.is_canonical() { |
345 | 2.09M | return; |
346 | 1.37M | } |
347 | 1.37M | self.ranges.sort(); |
348 | 1.37M | assert!(!self.ranges.is_empty()); |
349 | | |
350 | | // Is there a way to do this in-place with constant memory? I couldn't |
351 | | // figure out a way to do it. So just append the canonicalization to |
352 | | // the end of this range, and then drain it before we're done. |
353 | 1.37M | let drain_end = self.ranges.len(); |
354 | 108M | for oldi in 0..drain_end { |
355 | | // If we've added at least one new range, then check if we can |
356 | | // merge this range in the previously added range. |
357 | 108M | if self.ranges.len() > drain_end { |
358 | 107M | let (last, rest) = self.ranges.split_last_mut().unwrap(); |
359 | 107M | if let Some(union) = last.union(&rest[oldi]) { |
360 | 31.0M | *last = union; |
361 | 31.0M | continue; |
362 | 75.9M | } |
363 | 1.37M | } |
364 | 77.3M | let range = self.ranges[oldi]; |
365 | 77.3M | self.ranges.push(range); |
366 | | } |
367 | 1.37M | self.ranges.drain(..drain_end); |
368 | 3.47M | } <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::canonicalize Line | Count | Source | 343 | 1.31M | fn canonicalize(&mut self) { | 344 | 1.31M | if self.is_canonical() { | 345 | 680k | return; | 346 | 630k | } | 347 | 630k | self.ranges.sort(); | 348 | 630k | assert!(!self.ranges.is_empty()); | 349 | | | 350 | | // Is there a way to do this in-place with constant memory? I couldn't | 351 | | // figure out a way to do it. So just append the canonicalization to | 352 | | // the end of this range, and then drain it before we're done. | 353 | 630k | let drain_end = self.ranges.len(); | 354 | 8.26M | for oldi in 0..drain_end { | 355 | | // If we've added at least one new range, then check if we can | 356 | | // merge this range in the previously added range. | 357 | 8.26M | if self.ranges.len() > drain_end { | 358 | 7.63M | let (last, rest) = self.ranges.split_last_mut().unwrap(); | 359 | 7.63M | if let Some(union) = last.union(&rest[oldi]) { | 360 | 973k | *last = union; | 361 | 973k | continue; | 362 | 6.66M | } | 363 | 630k | } | 364 | 7.29M | let range = self.ranges[oldi]; | 365 | 7.29M | self.ranges.push(range); | 366 | | } | 367 | 630k | self.ranges.drain(..drain_end); | 368 | 1.31M | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::canonicalize Line | Count | Source | 343 | 2.02M | fn canonicalize(&mut self) { | 344 | 2.02M | if self.is_canonical() { | 345 | 1.28M | return; | 346 | 742k | } | 347 | 742k | self.ranges.sort(); | 348 | 742k | assert!(!self.ranges.is_empty()); | 349 | | | 350 | | // Is there a way to do this in-place with constant memory? I couldn't | 351 | | // figure out a way to do it. So just append the canonicalization to | 352 | | // the end of this range, and then drain it before we're done. | 353 | 742k | let drain_end = self.ranges.len(); | 354 | 100M | for oldi in 0..drain_end { | 355 | | // If we've added at least one new range, then check if we can | 356 | | // merge this range in the previously added range. | 357 | 100M | if self.ranges.len() > drain_end { | 358 | 99.3M | let (last, rest) = self.ranges.split_last_mut().unwrap(); | 359 | 99.3M | if let Some(union) = last.union(&rest[oldi]) { | 360 | 30.0M | *last = union; | 361 | 30.0M | continue; | 362 | 69.3M | } | 363 | 742k | } | 364 | 70.0M | let range = self.ranges[oldi]; | 365 | 70.0M | self.ranges.push(range); | 366 | | } | 367 | 742k | self.ranges.drain(..drain_end); | 368 | 2.02M | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::canonicalize Line | Count | Source | 343 | 136k | fn canonicalize(&mut self) { | 344 | 136k | if self.is_canonical() { | 345 | 136k | return; | 346 | 0 | } | 347 | 0 | self.ranges.sort(); | 348 | 0 | assert!(!self.ranges.is_empty()); | 349 | | | 350 | | // Is there a way to do this in-place with constant memory? I couldn't | 351 | | // figure out a way to do it. So just append the canonicalization to | 352 | | // the end of this range, and then drain it before we're done. | 353 | 0 | let drain_end = self.ranges.len(); | 354 | 0 | for oldi in 0..drain_end { | 355 | | // If we've added at least one new range, then check if we can | 356 | | // merge this range in the previously added range. | 357 | 0 | if self.ranges.len() > drain_end { | 358 | 0 | let (last, rest) = self.ranges.split_last_mut().unwrap(); | 359 | 0 | if let Some(union) = last.union(&rest[oldi]) { | 360 | 0 | *last = union; | 361 | 0 | continue; | 362 | 0 | } | 363 | 0 | } | 364 | 0 | let range = self.ranges[oldi]; | 365 | 0 | self.ranges.push(range); | 366 | | } | 367 | 0 | self.ranges.drain(..drain_end); | 368 | 136k | } |
Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::canonicalize |
369 | | |
370 | | /// Returns true if and only if this class is in a canonical ordering. |
371 | 3.47M | fn is_canonical(&self) -> bool { |
372 | 114M | for pair in self.ranges.windows(2) { |
373 | 114M | if pair[0] >= pair[1] { |
374 | 1.30M | return false; |
375 | 113M | } |
376 | 113M | if pair[0].is_contiguous(&pair[1]) { |
377 | 65.9k | return false; |
378 | 113M | } |
379 | | } |
380 | 2.09M | true |
381 | 3.47M | } <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::is_canonical Line | Count | Source | 371 | 1.31M | fn is_canonical(&self) -> bool { | 372 | 7.52M | for pair in self.ranges.windows(2) { | 373 | 7.52M | if pair[0] >= pair[1] { | 374 | 604k | return false; | 375 | 6.91M | } | 376 | 6.91M | if pair[0].is_contiguous(&pair[1]) { | 377 | 25.4k | return false; | 378 | 6.89M | } | 379 | | } | 380 | 680k | true | 381 | 1.31M | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::is_canonical Line | Count | Source | 371 | 2.02M | fn is_canonical(&self) -> bool { | 372 | 107M | for pair in self.ranges.windows(2) { | 373 | 107M | if pair[0] >= pair[1] { | 374 | 702k | return false; | 375 | 106M | } | 376 | 106M | if pair[0].is_contiguous(&pair[1]) { | 377 | 40.4k | return false; | 378 | 106M | } | 379 | | } | 380 | 1.28M | true | 381 | 2.02M | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::is_canonical Line | Count | Source | 371 | 136k | fn is_canonical(&self) -> bool { | 372 | 136k | for pair in self.ranges.windows(2) { | 373 | 0 | if pair[0] >= pair[1] { | 374 | 0 | return false; | 375 | 0 | } | 376 | 0 | if pair[0].is_contiguous(&pair[1]) { | 377 | 0 | return false; | 378 | 0 | } | 379 | | } | 380 | 136k | true | 381 | 136k | } |
Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::is_canonical |
382 | | } |
383 | | |
384 | | /// An iterator over intervals. |
385 | | #[derive(Debug)] |
386 | | pub struct IntervalSetIter<'a, I>(slice::Iter<'a, I>); |
387 | | |
388 | | impl<'a, I> Iterator for IntervalSetIter<'a, I> { |
389 | | type Item = &'a I; |
390 | | |
391 | 62.3M | fn next(&mut self) -> Option<&'a I> { |
392 | 62.3M | self.0.next() |
393 | 62.3M | } <regex_syntax::hir::interval::IntervalSetIter<regex_syntax::hir::ClassBytesRange> as core::iter::traits::iterator::Iterator>::next Line | Count | Source | 391 | 10.7M | fn next(&mut self) -> Option<&'a I> { | 392 | 10.7M | self.0.next() | 393 | 10.7M | } |
<regex_syntax::hir::interval::IntervalSetIter<regex_syntax::hir::ClassUnicodeRange> as core::iter::traits::iterator::Iterator>::next Line | Count | Source | 391 | 51.5M | fn next(&mut self) -> Option<&'a I> { | 392 | 51.5M | self.0.next() | 393 | 51.5M | } |
|
394 | | } |
395 | | |
396 | | pub trait Interval: |
397 | | Clone + Copy + Debug + Default + Eq + PartialEq + PartialOrd + Ord |
398 | | { |
399 | | type Bound: Bound; |
400 | | |
401 | | fn lower(&self) -> Self::Bound; |
402 | | fn upper(&self) -> Self::Bound; |
403 | | fn set_lower(&mut self, bound: Self::Bound); |
404 | | fn set_upper(&mut self, bound: Self::Bound); |
405 | | fn case_fold_simple( |
406 | | &self, |
407 | | intervals: &mut Vec<Self>, |
408 | | ) -> Result<(), unicode::CaseFoldError>; |
409 | | |
410 | | /// Create a new interval. |
411 | 98.2M | fn create(lower: Self::Bound, upper: Self::Bound) -> Self { |
412 | 98.2M | let mut int = Self::default(); |
413 | 98.2M | if lower <= upper { |
414 | 98.2M | int.set_lower(lower); |
415 | 98.2M | int.set_upper(upper); |
416 | 98.2M | } else { |
417 | 71 | int.set_lower(upper); |
418 | 71 | int.set_upper(lower); |
419 | 71 | } |
420 | 98.2M | int |
421 | 98.2M | } <regex_syntax::hir::ClassBytesRange as regex_syntax::hir::interval::Interval>::create Line | Count | Source | 411 | 4.58M | fn create(lower: Self::Bound, upper: Self::Bound) -> Self { | 412 | 4.58M | let mut int = Self::default(); | 413 | 4.58M | if lower <= upper { | 414 | 4.58M | int.set_lower(lower); | 415 | 4.58M | int.set_upper(upper); | 416 | 4.58M | } else { | 417 | 0 | int.set_lower(upper); | 418 | 0 | int.set_upper(lower); | 419 | 0 | } | 420 | 4.58M | int | 421 | 4.58M | } |
<regex_syntax::hir::ClassUnicodeRange as regex_syntax::hir::interval::Interval>::create Line | Count | Source | 411 | 93.7M | fn create(lower: Self::Bound, upper: Self::Bound) -> Self { | 412 | 93.7M | let mut int = Self::default(); | 413 | 93.7M | if lower <= upper { | 414 | 93.7M | int.set_lower(lower); | 415 | 93.7M | int.set_upper(upper); | 416 | 93.7M | } else { | 417 | 71 | int.set_lower(upper); | 418 | 71 | int.set_upper(lower); | 419 | 71 | } | 420 | 93.7M | int | 421 | 93.7M | } |
Unexecuted instantiation: <regex_syntax::hir::ClassBytesRange as regex_syntax::hir::interval::Interval>::create Unexecuted instantiation: <regex_syntax::hir::ClassUnicodeRange as regex_syntax::hir::interval::Interval>::create |
422 | | |
423 | | /// Union the given overlapping range into this range. |
424 | | /// |
425 | | /// If the two ranges aren't contiguous, then this returns `None`. |
426 | 107M | fn union(&self, other: &Self) -> Option<Self> { |
427 | 107M | if !self.is_contiguous(other) { |
428 | 75.9M | return None; |
429 | 31.0M | } |
430 | 31.0M | let lower = cmp::min(self.lower(), other.lower()); |
431 | 31.0M | let upper = cmp::max(self.upper(), other.upper()); |
432 | 31.0M | Some(Self::create(lower, upper)) |
433 | 107M | } <regex_syntax::hir::ClassBytesRange as regex_syntax::hir::interval::Interval>::union Line | Count | Source | 426 | 7.63M | fn union(&self, other: &Self) -> Option<Self> { | 427 | 7.63M | if !self.is_contiguous(other) { | 428 | 6.66M | return None; | 429 | 973k | } | 430 | 973k | let lower = cmp::min(self.lower(), other.lower()); | 431 | 973k | let upper = cmp::max(self.upper(), other.upper()); | 432 | 973k | Some(Self::create(lower, upper)) | 433 | 7.63M | } |
<regex_syntax::hir::ClassUnicodeRange as regex_syntax::hir::interval::Interval>::union Line | Count | Source | 426 | 99.3M | fn union(&self, other: &Self) -> Option<Self> { | 427 | 99.3M | if !self.is_contiguous(other) { | 428 | 69.3M | return None; | 429 | 30.0M | } | 430 | 30.0M | let lower = cmp::min(self.lower(), other.lower()); | 431 | 30.0M | let upper = cmp::max(self.upper(), other.upper()); | 432 | 30.0M | Some(Self::create(lower, upper)) | 433 | 99.3M | } |
Unexecuted instantiation: <regex_syntax::hir::ClassBytesRange as regex_syntax::hir::interval::Interval>::union Unexecuted instantiation: <regex_syntax::hir::ClassUnicodeRange as regex_syntax::hir::interval::Interval>::union |
434 | | |
435 | | /// Intersect this range with the given range and return the result. |
436 | | /// |
437 | | /// If the intersection is empty, then this returns `None`. |
438 | 5.31M | fn intersect(&self, other: &Self) -> Option<Self> { |
439 | 5.31M | let lower = cmp::max(self.lower(), other.lower()); |
440 | 5.31M | let upper = cmp::min(self.upper(), other.upper()); |
441 | 5.31M | if lower <= upper { |
442 | 2.45M | Some(Self::create(lower, upper)) |
443 | | } else { |
444 | 2.86M | None |
445 | | } |
446 | 5.31M | } <regex_syntax::hir::ClassBytesRange as regex_syntax::hir::interval::Interval>::intersect Line | Count | Source | 438 | 281k | fn intersect(&self, other: &Self) -> Option<Self> { | 439 | 281k | let lower = cmp::max(self.lower(), other.lower()); | 440 | 281k | let upper = cmp::min(self.upper(), other.upper()); | 441 | 281k | if lower <= upper { | 442 | 106k | Some(Self::create(lower, upper)) | 443 | | } else { | 444 | 175k | None | 445 | | } | 446 | 281k | } |
<regex_syntax::hir::ClassUnicodeRange as regex_syntax::hir::interval::Interval>::intersect Line | Count | Source | 438 | 5.03M | fn intersect(&self, other: &Self) -> Option<Self> { | 439 | 5.03M | let lower = cmp::max(self.lower(), other.lower()); | 440 | 5.03M | let upper = cmp::min(self.upper(), other.upper()); | 441 | 5.03M | if lower <= upper { | 442 | 2.34M | Some(Self::create(lower, upper)) | 443 | | } else { | 444 | 2.68M | None | 445 | | } | 446 | 5.03M | } |
|
447 | | |
448 | | /// Subtract the given range from this range and return the resulting |
449 | | /// ranges. |
450 | | /// |
451 | | /// If subtraction would result in an empty range, then no ranges are |
452 | | /// returned. |
453 | 2.46M | fn difference(&self, other: &Self) -> (Option<Self>, Option<Self>) { |
454 | 2.46M | if self.is_subset(other) { |
455 | 893k | return (None, None); |
456 | 1.57M | } |
457 | 1.57M | if self.is_intersection_empty(other) { |
458 | 0 | return (Some(self.clone()), None); |
459 | 1.57M | } |
460 | 1.57M | let add_lower = other.lower() > self.lower(); |
461 | 1.57M | let add_upper = other.upper() < self.upper(); |
462 | | // We know this because !self.is_subset(other) and the ranges have |
463 | | // a non-empty intersection. |
464 | 1.57M | assert!(add_lower || add_upper); |
465 | 1.57M | let mut ret = (None, None); |
466 | 1.57M | if add_lower { |
467 | 1.52M | let upper = other.lower().decrement(); |
468 | 1.52M | ret.0 = Some(Self::create(self.lower(), upper)); |
469 | 1.52M | } |
470 | 1.57M | if add_upper { |
471 | 1.52M | let lower = other.upper().increment(); |
472 | 1.52M | let range = Self::create(lower, self.upper()); |
473 | 1.52M | if ret.0.is_none() { |
474 | 50.5k | ret.0 = Some(range); |
475 | 1.47M | } else { |
476 | 1.47M | ret.1 = Some(range); |
477 | 1.47M | } |
478 | 49.5k | } |
479 | 1.57M | ret |
480 | 2.46M | } <regex_syntax::hir::ClassBytesRange as regex_syntax::hir::interval::Interval>::difference Line | Count | Source | 453 | 121k | fn difference(&self, other: &Self) -> (Option<Self>, Option<Self>) { | 454 | 121k | if self.is_subset(other) { | 455 | 29.7k | return (None, None); | 456 | 91.5k | } | 457 | 91.5k | if self.is_intersection_empty(other) { | 458 | 0 | return (Some(self.clone()), None); | 459 | 91.5k | } | 460 | 91.5k | let add_lower = other.lower() > self.lower(); | 461 | 91.5k | let add_upper = other.upper() < self.upper(); | 462 | | // We know this because !self.is_subset(other) and the ranges have | 463 | | // a non-empty intersection. | 464 | 91.5k | assert!(add_lower || add_upper); | 465 | 91.5k | let mut ret = (None, None); | 466 | 91.5k | if add_lower { | 467 | 78.4k | let upper = other.lower().decrement(); | 468 | 78.4k | ret.0 = Some(Self::create(self.lower(), upper)); | 469 | 78.4k | } | 470 | 91.5k | if add_upper { | 471 | 79.6k | let lower = other.upper().increment(); | 472 | 79.6k | let range = Self::create(lower, self.upper()); | 473 | 79.6k | if ret.0.is_none() { | 474 | 13.1k | ret.0 = Some(range); | 475 | 66.4k | } else { | 476 | 66.4k | ret.1 = Some(range); | 477 | 66.4k | } | 478 | 11.9k | } | 479 | 91.5k | ret | 480 | 121k | } |
<regex_syntax::hir::ClassUnicodeRange as regex_syntax::hir::interval::Interval>::difference Line | Count | Source | 453 | 2.34M | fn difference(&self, other: &Self) -> (Option<Self>, Option<Self>) { | 454 | 2.34M | if self.is_subset(other) { | 455 | 863k | return (None, None); | 456 | 1.48M | } | 457 | 1.48M | if self.is_intersection_empty(other) { | 458 | 0 | return (Some(self.clone()), None); | 459 | 1.48M | } | 460 | 1.48M | let add_lower = other.lower() > self.lower(); | 461 | 1.48M | let add_upper = other.upper() < self.upper(); | 462 | | // We know this because !self.is_subset(other) and the ranges have | 463 | | // a non-empty intersection. | 464 | 1.48M | assert!(add_lower || add_upper); | 465 | 1.48M | let mut ret = (None, None); | 466 | 1.48M | if add_lower { | 467 | 1.44M | let upper = other.lower().decrement(); | 468 | 1.44M | ret.0 = Some(Self::create(self.lower(), upper)); | 469 | 1.44M | } | 470 | 1.48M | if add_upper { | 471 | 1.44M | let lower = other.upper().increment(); | 472 | 1.44M | let range = Self::create(lower, self.upper()); | 473 | 1.44M | if ret.0.is_none() { | 474 | 37.4k | ret.0 = Some(range); | 475 | 1.40M | } else { | 476 | 1.40M | ret.1 = Some(range); | 477 | 1.40M | } | 478 | 37.6k | } | 479 | 1.48M | ret | 480 | 2.34M | } |
|
481 | | |
482 | | /// Returns true if and only if the two ranges are contiguous. Two ranges |
483 | | /// are contiguous if and only if the ranges are either overlapping or |
484 | | /// adjacent. |
485 | 220M | fn is_contiguous(&self, other: &Self) -> bool { |
486 | 220M | let lower1 = self.lower().as_u32(); |
487 | 220M | let upper1 = self.upper().as_u32(); |
488 | 220M | let lower2 = other.lower().as_u32(); |
489 | 220M | let upper2 = other.upper().as_u32(); |
490 | 220M | cmp::max(lower1, lower2) <= cmp::min(upper1, upper2).saturating_add(1) |
491 | 220M | } <regex_syntax::hir::ClassBytesRange as regex_syntax::hir::interval::Interval>::is_contiguous Line | Count | Source | 485 | 14.5M | fn is_contiguous(&self, other: &Self) -> bool { | 486 | 14.5M | let lower1 = self.lower().as_u32(); | 487 | 14.5M | let upper1 = self.upper().as_u32(); | 488 | 14.5M | let lower2 = other.lower().as_u32(); | 489 | 14.5M | let upper2 = other.upper().as_u32(); | 490 | 14.5M | cmp::max(lower1, lower2) <= cmp::min(upper1, upper2).saturating_add(1) | 491 | 14.5M | } |
<regex_syntax::hir::ClassUnicodeRange as regex_syntax::hir::interval::Interval>::is_contiguous Line | Count | Source | 485 | 205M | fn is_contiguous(&self, other: &Self) -> bool { | 486 | 205M | let lower1 = self.lower().as_u32(); | 487 | 205M | let upper1 = self.upper().as_u32(); | 488 | 205M | let lower2 = other.lower().as_u32(); | 489 | 205M | let upper2 = other.upper().as_u32(); | 490 | 205M | cmp::max(lower1, lower2) <= cmp::min(upper1, upper2).saturating_add(1) | 491 | 205M | } |
Unexecuted instantiation: <regex_syntax::hir::ClassBytesRange as regex_syntax::hir::interval::Interval>::is_contiguous Unexecuted instantiation: <regex_syntax::hir::ClassUnicodeRange as regex_syntax::hir::interval::Interval>::is_contiguous |
492 | | |
493 | | /// Returns true if and only if the intersection of this range and the |
494 | | /// other range is empty. |
495 | 6.89M | fn is_intersection_empty(&self, other: &Self) -> bool { |
496 | 6.89M | let (lower1, upper1) = (self.lower(), self.upper()); |
497 | 6.89M | let (lower2, upper2) = (other.lower(), other.upper()); |
498 | 6.89M | cmp::max(lower1, lower2) > cmp::min(upper1, upper2) |
499 | 6.89M | } <regex_syntax::hir::ClassBytesRange as regex_syntax::hir::interval::Interval>::is_intersection_empty Line | Count | Source | 495 | 2.06M | fn is_intersection_empty(&self, other: &Self) -> bool { | 496 | 2.06M | let (lower1, upper1) = (self.lower(), self.upper()); | 497 | 2.06M | let (lower2, upper2) = (other.lower(), other.upper()); | 498 | 2.06M | cmp::max(lower1, lower2) > cmp::min(upper1, upper2) | 499 | 2.06M | } |
<regex_syntax::hir::ClassUnicodeRange as regex_syntax::hir::interval::Interval>::is_intersection_empty Line | Count | Source | 495 | 4.82M | fn is_intersection_empty(&self, other: &Self) -> bool { | 496 | 4.82M | let (lower1, upper1) = (self.lower(), self.upper()); | 497 | 4.82M | let (lower2, upper2) = (other.lower(), other.upper()); | 498 | 4.82M | cmp::max(lower1, lower2) > cmp::min(upper1, upper2) | 499 | 4.82M | } |
|
500 | | |
501 | | /// Returns true if and only if this range is a subset of the other range. |
502 | 2.46M | fn is_subset(&self, other: &Self) -> bool { |
503 | 2.46M | let (lower1, upper1) = (self.lower(), self.upper()); |
504 | 2.46M | let (lower2, upper2) = (other.lower(), other.upper()); |
505 | 2.46M | (lower2 <= lower1 && lower1 <= upper2) |
506 | 944k | && (lower2 <= upper1 && upper1 <= upper2) |
507 | 2.46M | } <regex_syntax::hir::ClassBytesRange as regex_syntax::hir::interval::Interval>::is_subset Line | Count | Source | 502 | 121k | fn is_subset(&self, other: &Self) -> bool { | 503 | 121k | let (lower1, upper1) = (self.lower(), self.upper()); | 504 | 121k | let (lower2, upper2) = (other.lower(), other.upper()); | 505 | 121k | (lower2 <= lower1 && lower1 <= upper2) | 506 | 42.9k | && (lower2 <= upper1 && upper1 <= upper2) | 507 | 121k | } |
<regex_syntax::hir::ClassUnicodeRange as regex_syntax::hir::interval::Interval>::is_subset Line | Count | Source | 502 | 2.34M | fn is_subset(&self, other: &Self) -> bool { | 503 | 2.34M | let (lower1, upper1) = (self.lower(), self.upper()); | 504 | 2.34M | let (lower2, upper2) = (other.lower(), other.upper()); | 505 | 2.34M | (lower2 <= lower1 && lower1 <= upper2) | 506 | 901k | && (lower2 <= upper1 && upper1 <= upper2) | 507 | 2.34M | } |
|
508 | | } |
509 | | |
510 | | pub trait Bound: |
511 | | Copy + Clone + Debug + Eq + PartialEq + PartialOrd + Ord |
512 | | { |
513 | | fn min_value() -> Self; |
514 | | fn max_value() -> Self; |
515 | | fn as_u32(self) -> u32; |
516 | | fn increment(self) -> Self; |
517 | | fn decrement(self) -> Self; |
518 | | } |
519 | | |
520 | | impl Bound for u8 { |
521 | 50.1k | fn min_value() -> Self { |
522 | 50.1k | u8::MIN |
523 | 50.1k | } |
524 | 50.5k | fn max_value() -> Self { |
525 | 50.5k | u8::MAX |
526 | 50.5k | } |
527 | 58.2M | fn as_u32(self) -> u32 { |
528 | 58.2M | u32::from(self) |
529 | 58.2M | } |
530 | 140k | fn increment(self) -> Self { |
531 | 140k | self.checked_add(1).unwrap() |
532 | 140k | } |
533 | 138k | fn decrement(self) -> Self { |
534 | 138k | self.checked_sub(1).unwrap() |
535 | 138k | } |
536 | | } |
537 | | |
538 | | impl Bound for char { |
539 | 375k | fn min_value() -> Self { |
540 | 375k | '\x00' |
541 | 375k | } |
542 | 377k | fn max_value() -> Self { |
543 | 377k | '\u{10FFFF}' |
544 | 377k | } |
545 | 822M | fn as_u32(self) -> u32 { |
546 | 822M | u32::from(self) |
547 | 822M | } |
548 | | |
549 | 17.3M | fn increment(self) -> Self { |
550 | 17.3M | match self { |
551 | 1.59k | '\u{D7FF}' => '\u{E000}', |
552 | 17.3M | c => char::from_u32(u32::from(c).checked_add(1).unwrap()).unwrap(), |
553 | | } |
554 | 17.3M | } |
555 | | |
556 | 17.3M | fn decrement(self) -> Self { |
557 | 17.3M | match self { |
558 | 7.52k | '\u{E000}' => '\u{D7FF}', |
559 | 17.3M | c => char::from_u32(u32::from(c).checked_sub(1).unwrap()).unwrap(), |
560 | | } |
561 | 17.3M | } |
562 | | } |
563 | | |
564 | | // Tests for interval sets are written in src/hir.rs against the public API. |