/rust/registry/src/index.crates.io-1949cf8c6b5b557f/regex-syntax-0.8.11/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 | 0 | fn eq(&self, other: &IntervalSet<I>) -> bool { |
63 | 0 | self.ranges.eq(&other.ranges) |
64 | 0 | } Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange> as core::cmp::PartialEq>::eq Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange> as core::cmp::PartialEq>::eq |
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 | 0 | pub fn new<T: IntoIterator<Item = I>>(intervals: T) -> IntervalSet<I> { |
74 | 0 | let ranges: Vec<I> = intervals.into_iter().collect(); |
75 | | // An empty set is case folded. |
76 | 0 | let folded = ranges.is_empty(); |
77 | 0 | let mut set = IntervalSet { ranges, folded }; |
78 | 0 | set.canonicalize(); |
79 | 0 | set |
80 | 0 | } Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::new::<[regex_syntax::hir::ClassBytesRange; 1]> 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::ClassBytesRange>>::new::<alloc::vec::Vec<regex_syntax::hir::ClassBytesRange>> Unexecuted instantiation: <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}>>Unexecuted instantiation: <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}>>Unexecuted instantiation: <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}>>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]> Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::new::<alloc::vec::Vec<regex_syntax::hir::ClassUnicodeRange>> Unexecuted instantiation: <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}>>Unexecuted instantiation: <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}>>Unexecuted instantiation: <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}>> |
81 | | |
82 | | /// Add a new interval to this set. |
83 | 0 | pub fn push(&mut self, mut interval: I) { |
84 | 0 | let Err(i) = self.ranges.binary_search(&interval) else { |
85 | | // Exact match, `interval` is already in the set. |
86 | 0 | return; |
87 | | }; |
88 | | |
89 | | // The search finds us the first index where the previous interval |
90 | | // start is less than or equal to the new interval start. Since the |
91 | | // existing intervals are non-overlapping we only need to try to union |
92 | | // this single preceding interval |
93 | 0 | let mut start = i; |
94 | 0 | if let Some(before_i) = i.checked_sub(1) { |
95 | 0 | let before = &self.ranges[before_i]; |
96 | 0 | if let Some(union) = before.union(&interval) { |
97 | 0 | interval = union; |
98 | 0 | start = before_i; |
99 | 0 | } |
100 | 0 | } |
101 | | // `interval` may overlap any number of intervals following the |
102 | | // insertion point so will union each of them until we reach the |
103 | | // first non-overlapping interval |
104 | 0 | let mut end = i; |
105 | 0 | for after_i in i..self.ranges.len() { |
106 | 0 | let after = &self.ranges[after_i]; |
107 | 0 | let Some(union) = interval.union(after) else { |
108 | 0 | break; |
109 | | }; |
110 | 0 | interval = union; |
111 | 0 | end = after_i + 1; |
112 | | } |
113 | 0 | self.ranges.splice(start..end, core::iter::once(interval)); |
114 | | |
115 | | // We don't know whether the new interval added here is considered |
116 | | // case folded, so we conservatively assume that the entire set is |
117 | | // no longer case folded if it was previously. |
118 | 0 | self.folded = false; |
119 | 0 | } Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::push Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::push |
120 | | |
121 | | /// Return an iterator over all intervals in this set. |
122 | | /// |
123 | | /// The iterator yields intervals in ascending order. |
124 | 0 | pub fn iter(&self) -> IntervalSetIter<'_, I> { |
125 | 0 | IntervalSetIter(self.ranges.iter()) |
126 | 0 | } Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::iter Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::iter |
127 | | |
128 | | /// Return an immutable slice of intervals in this set. |
129 | | /// |
130 | | /// The sequence returned is in canonical ordering. |
131 | 0 | pub fn intervals(&self) -> &[I] { |
132 | 0 | &self.ranges |
133 | 0 | } Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::intervals Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::intervals |
134 | | |
135 | | /// Expand this interval set such that it contains all case folded |
136 | | /// characters. For example, if this class consists of the range `a-z`, |
137 | | /// then applying case folding will result in the class containing both the |
138 | | /// ranges `a-z` and `A-Z`. |
139 | | /// |
140 | | /// This returns an error if the necessary case mapping data is not |
141 | | /// available. |
142 | 0 | pub fn case_fold_simple(&mut self) -> Result<(), unicode::CaseFoldError> { |
143 | 0 | if self.folded { |
144 | 0 | return Ok(()); |
145 | 0 | } |
146 | 0 | let len = self.ranges.len(); |
147 | 0 | for i in 0..len { |
148 | 0 | let range = self.ranges[i]; |
149 | 0 | if let Err(err) = range.case_fold_simple(&mut self.ranges) { |
150 | 0 | self.canonicalize(); |
151 | 0 | return Err(err); |
152 | 0 | } |
153 | | } |
154 | 0 | self.canonicalize(); |
155 | 0 | self.folded = true; |
156 | 0 | Ok(()) |
157 | 0 | } Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::case_fold_simple Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::case_fold_simple |
158 | | |
159 | | /// Union this set with the given set, in place. |
160 | 0 | pub fn union(&mut self, other: &IntervalSet<I>) { |
161 | 0 | if other.ranges.is_empty() || self.ranges == other.ranges { |
162 | 0 | return; |
163 | 0 | } |
164 | | // This could almost certainly be done more efficiently. |
165 | 0 | self.ranges.extend(&other.ranges); |
166 | 0 | self.canonicalize(); |
167 | 0 | self.folded = self.folded && other.folded; |
168 | 0 | } Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::union Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::union |
169 | | |
170 | | /// Intersect this set with the given set, in place. |
171 | 0 | pub fn intersect(&mut self, other: &IntervalSet<I>) { |
172 | 0 | if self.ranges.is_empty() { |
173 | 0 | return; |
174 | 0 | } |
175 | 0 | if other.ranges.is_empty() { |
176 | 0 | self.ranges.clear(); |
177 | | // An empty set is case folded. |
178 | 0 | self.folded = true; |
179 | 0 | return; |
180 | 0 | } |
181 | | |
182 | | // There should be a way to do this in-place with constant memory, |
183 | | // but I couldn't figure out a simple way to do it. So just append |
184 | | // the intersection to the end of this range, and then drain it before |
185 | | // we're done. |
186 | 0 | let drain_end = self.ranges.len(); |
187 | | |
188 | 0 | let mut ita = 0..drain_end; |
189 | 0 | let mut itb = 0..other.ranges.len(); |
190 | 0 | let mut a = ita.next().unwrap(); |
191 | 0 | let mut b = itb.next().unwrap(); |
192 | | loop { |
193 | 0 | if let Some(ab) = self.ranges[a].intersect(&other.ranges[b]) { |
194 | 0 | self.ranges.push(ab); |
195 | 0 | } |
196 | 0 | let (it, aorb) = |
197 | 0 | if self.ranges[a].upper() < other.ranges[b].upper() { |
198 | 0 | (&mut ita, &mut a) |
199 | | } else { |
200 | 0 | (&mut itb, &mut b) |
201 | | }; |
202 | 0 | match it.next() { |
203 | 0 | Some(v) => *aorb = v, |
204 | 0 | None => break, |
205 | | } |
206 | | } |
207 | 0 | self.ranges.drain(..drain_end); |
208 | 0 | self.folded = self.folded && other.folded; |
209 | 0 | } Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::intersect Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::intersect |
210 | | |
211 | | /// Subtract the given set from this set, in place. |
212 | 0 | pub fn difference(&mut self, other: &IntervalSet<I>) { |
213 | 0 | if self.ranges.is_empty() || other.ranges.is_empty() { |
214 | 0 | return; |
215 | 0 | } |
216 | | |
217 | | // This algorithm is (to me) surprisingly complex. A search of the |
218 | | // interwebs indicate that this is a potentially interesting problem. |
219 | | // Folks seem to suggest interval or segment trees, but I'd like to |
220 | | // avoid the overhead (both runtime and conceptual) of that. |
221 | | // |
222 | | // The following is basically my Shitty First Draft. Therefore, in |
223 | | // order to grok it, you probably need to read each line carefully. |
224 | | // Simplifications are most welcome! |
225 | | // |
226 | | // Remember, we can assume the canonical format invariant here, which |
227 | | // says that all ranges are sorted, not overlapping and not adjacent in |
228 | | // each class. |
229 | 0 | let drain_end = self.ranges.len(); |
230 | 0 | let (mut a, mut b) = (0, 0); |
231 | 0 | 'LOOP: while a < drain_end && b < other.ranges.len() { |
232 | | // Basically, the easy cases are when neither range overlaps with |
233 | | // each other. If the `b` range is less than our current `a` |
234 | | // range, then we can skip it and move on. |
235 | 0 | if other.ranges[b].upper() < self.ranges[a].lower() { |
236 | 0 | b += 1; |
237 | 0 | continue; |
238 | 0 | } |
239 | | // ... similarly for the `a` range. If it's less than the smallest |
240 | | // `b` range, then we can add it as-is. |
241 | 0 | if self.ranges[a].upper() < other.ranges[b].lower() { |
242 | 0 | let range = self.ranges[a]; |
243 | 0 | self.ranges.push(range); |
244 | 0 | a += 1; |
245 | 0 | continue; |
246 | 0 | } |
247 | | // Otherwise, we have overlapping ranges. |
248 | 0 | assert!(!self.ranges[a].is_intersection_empty(&other.ranges[b])); |
249 | | |
250 | | // This part is tricky and was non-obvious to me without looking |
251 | | // at explicit examples (see the tests). The trickiness stems from |
252 | | // two things: 1) subtracting a range from another range could |
253 | | // yield two ranges and 2) after subtracting a range, it's possible |
254 | | // that future ranges can have an impact. The loop below advances |
255 | | // the `b` ranges until they can't possible impact the current |
256 | | // range. |
257 | | // |
258 | | // For example, if our `a` range is `a-t` and our next three `b` |
259 | | // ranges are `a-c`, `g-i`, `r-t` and `x-z`, then we need to apply |
260 | | // subtraction three times before moving on to the next `a` range. |
261 | 0 | let mut range = self.ranges[a]; |
262 | 0 | while b < other.ranges.len() |
263 | 0 | && !range.is_intersection_empty(&other.ranges[b]) |
264 | | { |
265 | 0 | let old_range = range; |
266 | 0 | range = match range.difference(&other.ranges[b]) { |
267 | | (None, None) => { |
268 | | // We lost the entire range, so move on to the next |
269 | | // without adding this one. |
270 | 0 | a += 1; |
271 | 0 | continue 'LOOP; |
272 | | } |
273 | 0 | (Some(range1), None) | (None, Some(range1)) => range1, |
274 | 0 | (Some(range1), Some(range2)) => { |
275 | 0 | self.ranges.push(range1); |
276 | 0 | range2 |
277 | | } |
278 | | }; |
279 | | // It's possible that the `b` range has more to contribute |
280 | | // here. In particular, if it is greater than the original |
281 | | // range, then it might impact the next `a` range *and* it |
282 | | // has impacted the current `a` range as much as possible, |
283 | | // so we can quit. We don't bump `b` so that the next `a` |
284 | | // range can apply it. |
285 | 0 | if other.ranges[b].upper() > old_range.upper() { |
286 | 0 | break; |
287 | 0 | } |
288 | | // Otherwise, the next `b` range might apply to the current |
289 | | // `a` range. |
290 | 0 | b += 1; |
291 | | } |
292 | 0 | self.ranges.push(range); |
293 | 0 | a += 1; |
294 | | } |
295 | 0 | while a < drain_end { |
296 | 0 | let range = self.ranges[a]; |
297 | 0 | self.ranges.push(range); |
298 | 0 | a += 1; |
299 | 0 | } |
300 | 0 | self.ranges.drain(..drain_end); |
301 | 0 | self.folded = self.folded && other.folded; |
302 | 0 | } Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::difference Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::difference |
303 | | |
304 | | /// Compute the symmetric difference of the two sets, in place. |
305 | | /// |
306 | | /// This computes the symmetric difference of two interval sets. This |
307 | | /// removes all elements in this set that are also in the given set, |
308 | | /// but also adds all elements from the given set that aren't in this |
309 | | /// set. That is, the set will contain all elements in either set, |
310 | | /// but will not contain any elements that are in both sets. |
311 | 0 | pub fn symmetric_difference(&mut self, other: &IntervalSet<I>) { |
312 | | // TODO(burntsushi): Fix this so that it amortizes allocation. |
313 | 0 | let mut intersection = self.clone(); |
314 | 0 | intersection.intersect(other); |
315 | 0 | self.union(other); |
316 | 0 | self.difference(&intersection); |
317 | 0 | } Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::symmetric_difference Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::symmetric_difference |
318 | | |
319 | | /// Negate this interval set. |
320 | | /// |
321 | | /// For all `x` where `x` is any element, if `x` was in this set, then it |
322 | | /// will not be in this set after negation. |
323 | 0 | pub fn negate(&mut self) { |
324 | 0 | if self.ranges.is_empty() { |
325 | 0 | let (min, max) = (I::Bound::min_value(), I::Bound::max_value()); |
326 | 0 | self.ranges.push(I::create(min, max)); |
327 | | // The set containing everything must case folded. |
328 | 0 | self.folded = true; |
329 | 0 | return; |
330 | 0 | } |
331 | | |
332 | | // There should be a way to do this in-place with constant memory, |
333 | | // but I couldn't figure out a simple way to do it. So just append |
334 | | // the negation to the end of this range, and then drain it before |
335 | | // we're done. |
336 | 0 | let drain_end = self.ranges.len(); |
337 | | |
338 | | // We do checked arithmetic below because of the canonical ordering |
339 | | // invariant. |
340 | 0 | if self.ranges[0].lower() > I::Bound::min_value() { |
341 | 0 | let upper = self.ranges[0].lower().decrement(); |
342 | 0 | self.ranges.push(I::create(I::Bound::min_value(), upper)); |
343 | 0 | } |
344 | 0 | for i in 1..drain_end { |
345 | 0 | let lower = self.ranges[i - 1].upper().increment(); |
346 | 0 | let upper = self.ranges[i].lower().decrement(); |
347 | 0 | self.ranges.push(I::create(lower, upper)); |
348 | 0 | } |
349 | 0 | if self.ranges[drain_end - 1].upper() < I::Bound::max_value() { |
350 | 0 | let lower = self.ranges[drain_end - 1].upper().increment(); |
351 | 0 | self.ranges.push(I::create(lower, I::Bound::max_value())); |
352 | 0 | } |
353 | 0 | self.ranges.drain(..drain_end); |
354 | | // We don't need to update whether this set is folded or not, because |
355 | | // it is conservatively preserved through negation. Namely, if a set |
356 | | // is not folded, then it is possible that its negation is folded, for |
357 | | // example, [^☃]. But we're fine with assuming that the set is not |
358 | | // folded in that case. (`folded` permits false negatives but not false |
359 | | // positives.) |
360 | | // |
361 | | // But what about when a set is folded, is its negation also |
362 | | // necessarily folded? Yes. Because if a set is folded, then for every |
363 | | // character in the set, it necessarily included its equivalence class |
364 | | // of case folded characters. Negating it in turn means that all |
365 | | // equivalence classes in the set are negated, and any equivalence |
366 | | // class that was previously not in the set is now entirely in the set. |
367 | 0 | } Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::negate Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::negate |
368 | | |
369 | | /// Converts this set into a canonical ordering. |
370 | 0 | fn canonicalize(&mut self) { |
371 | 0 | if self.is_canonical() { |
372 | 0 | return; |
373 | 0 | } |
374 | 0 | self.ranges.sort(); |
375 | 0 | assert!(!self.ranges.is_empty()); |
376 | | |
377 | | // Is there a way to do this in-place with constant memory? I couldn't |
378 | | // figure out a way to do it. So just append the canonicalization to |
379 | | // the end of this range, and then drain it before we're done. |
380 | 0 | let drain_end = self.ranges.len(); |
381 | 0 | for oldi in 0..drain_end { |
382 | | // If we've added at least one new range, then check if we can |
383 | | // merge this range in the previously added range. |
384 | 0 | if self.ranges.len() > drain_end { |
385 | 0 | let (last, rest) = self.ranges.split_last_mut().unwrap(); |
386 | 0 | if let Some(union) = last.union(&rest[oldi]) { |
387 | 0 | *last = union; |
388 | 0 | continue; |
389 | 0 | } |
390 | 0 | } |
391 | 0 | let range = self.ranges[oldi]; |
392 | 0 | self.ranges.push(range); |
393 | | } |
394 | 0 | self.ranges.drain(..drain_end); |
395 | 0 | } Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::canonicalize Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::canonicalize |
396 | | |
397 | | /// Returns true if and only if this class is in a canonical ordering. |
398 | 0 | fn is_canonical(&self) -> bool { |
399 | 0 | for pair in self.ranges.windows(2) { |
400 | 0 | if pair[0] >= pair[1] { |
401 | 0 | return false; |
402 | 0 | } |
403 | 0 | if pair[0].is_contiguous(&pair[1]) { |
404 | 0 | return false; |
405 | 0 | } |
406 | | } |
407 | 0 | true |
408 | 0 | } Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::is_canonical Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::is_canonical |
409 | | } |
410 | | |
411 | | /// An iterator over intervals. |
412 | | #[derive(Debug)] |
413 | | pub struct IntervalSetIter<'a, I>(slice::Iter<'a, I>); |
414 | | |
415 | | impl<'a, I> Iterator for IntervalSetIter<'a, I> { |
416 | | type Item = &'a I; |
417 | | |
418 | 0 | fn next(&mut self) -> Option<&'a I> { |
419 | 0 | self.0.next() |
420 | 0 | } Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSetIter<regex_syntax::hir::ClassBytesRange> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <regex_syntax::hir::interval::IntervalSetIter<regex_syntax::hir::ClassUnicodeRange> as core::iter::traits::iterator::Iterator>::next |
421 | | } |
422 | | |
423 | | pub trait Interval: |
424 | | Clone + Copy + Debug + Default + Eq + PartialEq + PartialOrd + Ord |
425 | | { |
426 | | type Bound: Bound; |
427 | | |
428 | | fn lower(&self) -> Self::Bound; |
429 | | fn upper(&self) -> Self::Bound; |
430 | | fn set_lower(&mut self, bound: Self::Bound); |
431 | | fn set_upper(&mut self, bound: Self::Bound); |
432 | | fn case_fold_simple( |
433 | | &self, |
434 | | intervals: &mut Vec<Self>, |
435 | | ) -> Result<(), unicode::CaseFoldError>; |
436 | | |
437 | | /// Create a new interval. |
438 | 0 | fn create(lower: Self::Bound, upper: Self::Bound) -> Self { |
439 | 0 | let mut int = Self::default(); |
440 | 0 | if lower <= upper { |
441 | 0 | int.set_lower(lower); |
442 | 0 | int.set_upper(upper); |
443 | 0 | } else { |
444 | 0 | int.set_lower(upper); |
445 | 0 | int.set_upper(lower); |
446 | 0 | } |
447 | 0 | int |
448 | 0 | } 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 |
449 | | |
450 | | /// Union the given overlapping range into this range. |
451 | | /// |
452 | | /// If the two ranges aren't contiguous, then this returns `None`. |
453 | 0 | fn union(&self, other: &Self) -> Option<Self> { |
454 | 0 | if !self.is_contiguous(other) { |
455 | 0 | return None; |
456 | 0 | } |
457 | 0 | let lower = cmp::min(self.lower(), other.lower()); |
458 | 0 | let upper = cmp::max(self.upper(), other.upper()); |
459 | 0 | Some(Self::create(lower, upper)) |
460 | 0 | } 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 |
461 | | |
462 | | /// Intersect this range with the given range and return the result. |
463 | | /// |
464 | | /// If the intersection is empty, then this returns `None`. |
465 | 0 | fn intersect(&self, other: &Self) -> Option<Self> { |
466 | 0 | let lower = cmp::max(self.lower(), other.lower()); |
467 | 0 | let upper = cmp::min(self.upper(), other.upper()); |
468 | 0 | if lower <= upper { |
469 | 0 | Some(Self::create(lower, upper)) |
470 | | } else { |
471 | 0 | None |
472 | | } |
473 | 0 | } Unexecuted instantiation: <regex_syntax::hir::ClassBytesRange as regex_syntax::hir::interval::Interval>::intersect Unexecuted instantiation: <regex_syntax::hir::ClassUnicodeRange as regex_syntax::hir::interval::Interval>::intersect |
474 | | |
475 | | /// Subtract the given range from this range and return the resulting |
476 | | /// ranges. |
477 | | /// |
478 | | /// If subtraction would result in an empty range, then no ranges are |
479 | | /// returned. |
480 | 0 | fn difference(&self, other: &Self) -> (Option<Self>, Option<Self>) { |
481 | 0 | if self.is_subset(other) { |
482 | 0 | return (None, None); |
483 | 0 | } |
484 | 0 | if self.is_intersection_empty(other) { |
485 | 0 | return (Some(self.clone()), None); |
486 | 0 | } |
487 | 0 | let add_lower = other.lower() > self.lower(); |
488 | 0 | let add_upper = other.upper() < self.upper(); |
489 | | // We know this because !self.is_subset(other) and the ranges have |
490 | | // a non-empty intersection. |
491 | 0 | assert!(add_lower || add_upper); |
492 | 0 | let mut ret = (None, None); |
493 | 0 | if add_lower { |
494 | 0 | let upper = other.lower().decrement(); |
495 | 0 | ret.0 = Some(Self::create(self.lower(), upper)); |
496 | 0 | } |
497 | 0 | if add_upper { |
498 | 0 | let lower = other.upper().increment(); |
499 | 0 | let range = Self::create(lower, self.upper()); |
500 | 0 | if ret.0.is_none() { |
501 | 0 | ret.0 = Some(range); |
502 | 0 | } else { |
503 | 0 | ret.1 = Some(range); |
504 | 0 | } |
505 | 0 | } |
506 | 0 | ret |
507 | 0 | } Unexecuted instantiation: <regex_syntax::hir::ClassBytesRange as regex_syntax::hir::interval::Interval>::difference Unexecuted instantiation: <regex_syntax::hir::ClassUnicodeRange as regex_syntax::hir::interval::Interval>::difference |
508 | | |
509 | | /// Returns true if and only if the two ranges are contiguous. Two ranges |
510 | | /// are contiguous if and only if the ranges are either overlapping or |
511 | | /// adjacent. |
512 | 0 | fn is_contiguous(&self, other: &Self) -> bool { |
513 | 0 | let lower1 = self.lower().as_u32(); |
514 | 0 | let upper1 = self.upper().as_u32(); |
515 | 0 | let lower2 = other.lower().as_u32(); |
516 | 0 | let upper2 = other.upper().as_u32(); |
517 | 0 | cmp::max(lower1, lower2) <= cmp::min(upper1, upper2).saturating_add(1) |
518 | 0 | } 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 |
519 | | |
520 | | /// Returns true if and only if the intersection of this range and the |
521 | | /// other range is empty. |
522 | 0 | fn is_intersection_empty(&self, other: &Self) -> bool { |
523 | 0 | let (lower1, upper1) = (self.lower(), self.upper()); |
524 | 0 | let (lower2, upper2) = (other.lower(), other.upper()); |
525 | 0 | cmp::max(lower1, lower2) > cmp::min(upper1, upper2) |
526 | 0 | } Unexecuted instantiation: <regex_syntax::hir::ClassBytesRange as regex_syntax::hir::interval::Interval>::is_intersection_empty Unexecuted instantiation: <regex_syntax::hir::ClassUnicodeRange as regex_syntax::hir::interval::Interval>::is_intersection_empty |
527 | | |
528 | | /// Returns true if and only if this range is a subset of the other range. |
529 | 0 | fn is_subset(&self, other: &Self) -> bool { |
530 | 0 | let (lower1, upper1) = (self.lower(), self.upper()); |
531 | 0 | let (lower2, upper2) = (other.lower(), other.upper()); |
532 | 0 | (lower2 <= lower1 && lower1 <= upper2) |
533 | 0 | && (lower2 <= upper1 && upper1 <= upper2) |
534 | 0 | } Unexecuted instantiation: <regex_syntax::hir::ClassBytesRange as regex_syntax::hir::interval::Interval>::is_subset Unexecuted instantiation: <regex_syntax::hir::ClassUnicodeRange as regex_syntax::hir::interval::Interval>::is_subset |
535 | | } |
536 | | |
537 | | pub trait Bound: |
538 | | Copy + Clone + Debug + Eq + PartialEq + PartialOrd + Ord |
539 | | { |
540 | | fn min_value() -> Self; |
541 | | fn max_value() -> Self; |
542 | | fn as_u32(self) -> u32; |
543 | | fn increment(self) -> Self; |
544 | | fn decrement(self) -> Self; |
545 | | } |
546 | | |
547 | | impl Bound for u8 { |
548 | 0 | fn min_value() -> Self { |
549 | 0 | u8::MIN |
550 | 0 | } |
551 | 0 | fn max_value() -> Self { |
552 | 0 | u8::MAX |
553 | 0 | } |
554 | 0 | fn as_u32(self) -> u32 { |
555 | 0 | u32::from(self) |
556 | 0 | } |
557 | 0 | fn increment(self) -> Self { |
558 | 0 | self.checked_add(1).unwrap() |
559 | 0 | } |
560 | 0 | fn decrement(self) -> Self { |
561 | 0 | self.checked_sub(1).unwrap() |
562 | 0 | } |
563 | | } |
564 | | |
565 | | impl Bound for char { |
566 | 0 | fn min_value() -> Self { |
567 | 0 | '\x00' |
568 | 0 | } |
569 | 0 | fn max_value() -> Self { |
570 | 0 | '\u{10FFFF}' |
571 | 0 | } |
572 | 0 | fn as_u32(self) -> u32 { |
573 | 0 | u32::from(self) |
574 | 0 | } |
575 | | |
576 | 0 | fn increment(self) -> Self { |
577 | 0 | match self { |
578 | 0 | '\u{D7FF}' => '\u{E000}', |
579 | 0 | c => char::from_u32(u32::from(c).checked_add(1).unwrap()).unwrap(), |
580 | | } |
581 | 0 | } |
582 | | |
583 | 0 | fn decrement(self) -> Self { |
584 | 0 | match self { |
585 | 0 | '\u{E000}' => '\u{D7FF}', |
586 | 0 | c => char::from_u32(u32::from(c).checked_sub(1).unwrap()).unwrap(), |
587 | | } |
588 | 0 | } |
589 | | } |
590 | | |
591 | | // Tests for interval sets are written in src/hir.rs against the public API. |