/rust/registry/src/index.crates.io-1949cf8c6b5b557f/regex-syntax-0.8.10/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 | 1.87k | fn eq(&self, other: &IntervalSet<I>) -> bool { |
63 | 1.87k | self.ranges.eq(&other.ranges) |
64 | 1.87k | } <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange> as core::cmp::PartialEq>::eq Line | Count | Source | 62 | 734 | fn eq(&self, other: &IntervalSet<I>) -> bool { | 63 | 734 | self.ranges.eq(&other.ranges) | 64 | 734 | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange> as core::cmp::PartialEq>::eq Line | Count | Source | 62 | 1.13k | fn eq(&self, other: &IntervalSet<I>) -> bool { | 63 | 1.13k | self.ranges.eq(&other.ranges) | 64 | 1.13k | } |
|
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 | 2.11M | pub fn new<T: IntoIterator<Item = I>>(intervals: T) -> IntervalSet<I> { |
74 | 2.11M | let ranges: Vec<I> = intervals.into_iter().collect(); |
75 | | // An empty set is case folded. |
76 | 2.11M | let folded = ranges.is_empty(); |
77 | 2.11M | let mut set = IntervalSet { ranges, folded }; |
78 | 2.11M | set.canonicalize(); |
79 | 2.11M | set |
80 | 2.11M | } <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::new::<[regex_syntax::hir::ClassBytesRange; 1]> Line | Count | Source | 73 | 52.9k | pub fn new<T: IntoIterator<Item = I>>(intervals: T) -> IntervalSet<I> { | 74 | 52.9k | let ranges: Vec<I> = intervals.into_iter().collect(); | 75 | | // An empty set is case folded. | 76 | 52.9k | let folded = ranges.is_empty(); | 77 | 52.9k | let mut set = IntervalSet { ranges, folded }; | 78 | 52.9k | set.canonicalize(); | 79 | 52.9k | set | 80 | 52.9k | } |
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]> <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::new::<alloc::vec::Vec<regex_syntax::hir::ClassBytesRange>> Line | Count | Source | 73 | 894k | pub fn new<T: IntoIterator<Item = I>>(intervals: T) -> IntervalSet<I> { | 74 | 894k | let ranges: Vec<I> = intervals.into_iter().collect(); | 75 | | // An empty set is case folded. | 76 | 894k | let folded = ranges.is_empty(); | 77 | 894k | let mut set = IntervalSet { ranges, folded }; | 78 | 894k | set.canonicalize(); | 79 | 894k | set | 80 | 894k | } |
<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 | 9 | pub fn new<T: IntoIterator<Item = I>>(intervals: T) -> IntervalSet<I> { | 74 | 9 | let ranges: Vec<I> = intervals.into_iter().collect(); | 75 | | // An empty set is case folded. | 76 | 9 | let folded = ranges.is_empty(); | 77 | 9 | let mut set = IntervalSet { ranges, folded }; | 78 | 9 | set.canonicalize(); | 79 | 9 | set | 80 | 9 | } |
<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 | 1.67k | pub fn new<T: IntoIterator<Item = I>>(intervals: T) -> IntervalSet<I> { | 74 | 1.67k | let ranges: Vec<I> = intervals.into_iter().collect(); | 75 | | // An empty set is case folded. | 76 | 1.67k | let folded = ranges.is_empty(); | 77 | 1.67k | let mut set = IntervalSet { ranges, folded }; | 78 | 1.67k | set.canonicalize(); | 79 | 1.67k | set | 80 | 1.67k | } |
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}>><regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::new::<[regex_syntax::hir::ClassUnicodeRange; 1]> Line | Count | Source | 73 | 121k | pub fn new<T: IntoIterator<Item = I>>(intervals: T) -> IntervalSet<I> { | 74 | 121k | let ranges: Vec<I> = intervals.into_iter().collect(); | 75 | | // An empty set is case folded. | 76 | 121k | let folded = ranges.is_empty(); | 77 | 121k | let mut set = IntervalSet { ranges, folded }; | 78 | 121k | set.canonicalize(); | 79 | 121k | set | 80 | 121k | } |
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 | 171 | pub fn new<T: IntoIterator<Item = I>>(intervals: T) -> IntervalSet<I> { | 74 | 171 | let ranges: Vec<I> = intervals.into_iter().collect(); | 75 | | // An empty set is case folded. | 76 | 171 | let folded = ranges.is_empty(); | 77 | 171 | let mut set = IntervalSet { ranges, folded }; | 78 | 171 | set.canonicalize(); | 79 | 171 | set | 80 | 171 | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::new::<alloc::vec::Vec<regex_syntax::hir::ClassUnicodeRange>> Line | Count | Source | 73 | 1.04M | pub fn new<T: IntoIterator<Item = I>>(intervals: T) -> IntervalSet<I> { | 74 | 1.04M | let ranges: Vec<I> = intervals.into_iter().collect(); | 75 | | // An empty set is case folded. | 76 | 1.04M | let folded = ranges.is_empty(); | 77 | 1.04M | let mut set = IntervalSet { ranges, folded }; | 78 | 1.04M | set.canonicalize(); | 79 | 1.04M | set | 80 | 1.04M | } |
<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 | 355 | pub fn new<T: IntoIterator<Item = I>>(intervals: T) -> IntervalSet<I> { | 74 | 355 | let ranges: Vec<I> = intervals.into_iter().collect(); | 75 | | // An empty set is case folded. | 76 | 355 | let folded = ranges.is_empty(); | 77 | 355 | let mut set = IntervalSet { ranges, folded }; | 78 | 355 | set.canonicalize(); | 79 | 355 | set | 80 | 355 | } |
<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 | 174 | pub fn new<T: IntoIterator<Item = I>>(intervals: T) -> IntervalSet<I> { | 74 | 174 | let ranges: Vec<I> = intervals.into_iter().collect(); | 75 | | // An empty set is case folded. | 76 | 174 | let folded = ranges.is_empty(); | 77 | 174 | let mut set = IntervalSet { ranges, folded }; | 78 | 174 | set.canonicalize(); | 79 | 174 | set | 80 | 174 | } |
<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 | 660 | pub fn new<T: IntoIterator<Item = I>>(intervals: T) -> IntervalSet<I> { | 74 | 660 | let ranges: Vec<I> = intervals.into_iter().collect(); | 75 | | // An empty set is case folded. | 76 | 660 | let folded = ranges.is_empty(); | 77 | 660 | let mut set = IntervalSet { ranges, folded }; | 78 | 660 | set.canonicalize(); | 79 | 660 | set | 80 | 660 | } |
|
81 | | |
82 | | /// Add a new interval to this set. |
83 | 3.18M | 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 | 3.18M | self.ranges.push(interval); |
87 | 3.18M | 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 | 3.18M | self.folded = false; |
92 | 3.18M | } <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::push Line | Count | Source | 83 | 1.50M | 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 | 1.50M | self.ranges.push(interval); | 87 | 1.50M | 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 | 1.50M | self.folded = false; | 92 | 1.50M | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::push Line | Count | Source | 83 | 1.67M | 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 | 1.67M | self.ranges.push(interval); | 87 | 1.67M | 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 | 1.67M | self.folded = false; | 92 | 1.67M | } |
|
93 | | |
94 | | /// Return an iterator over all intervals in this set. |
95 | | /// |
96 | | /// The iterator yields intervals in ascending order. |
97 | 3.02M | pub fn iter(&self) -> IntervalSetIter<'_, I> { |
98 | 3.02M | IntervalSetIter(self.ranges.iter()) |
99 | 3.02M | } <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::iter Line | Count | Source | 97 | 880k | pub fn iter(&self) -> IntervalSetIter<'_, I> { | 98 | 880k | IntervalSetIter(self.ranges.iter()) | 99 | 880k | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::iter Line | Count | Source | 97 | 2.14M | pub fn iter(&self) -> IntervalSetIter<'_, I> { | 98 | 2.14M | IntervalSetIter(self.ranges.iter()) | 99 | 2.14M | } |
|
100 | | |
101 | | /// Return an immutable slice of intervals in this set. |
102 | | /// |
103 | | /// The sequence returned is in canonical ordering. |
104 | 11.7M | pub fn intervals(&self) -> &[I] { |
105 | 11.7M | &self.ranges |
106 | 11.7M | } <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::intervals Line | Count | Source | 104 | 4.53M | pub fn intervals(&self) -> &[I] { | 105 | 4.53M | &self.ranges | 106 | 4.53M | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::intervals Line | Count | Source | 104 | 7.22M | pub fn intervals(&self) -> &[I] { | 105 | 7.22M | &self.ranges | 106 | 7.22M | } |
|
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 | 1.61M | pub fn case_fold_simple(&mut self) -> Result<(), unicode::CaseFoldError> { |
116 | 1.61M | if self.folded { |
117 | 54.6k | return Ok(()); |
118 | 1.56M | } |
119 | 1.56M | let len = self.ranges.len(); |
120 | 3.34M | for i in 0..len { |
121 | 3.34M | let range = self.ranges[i]; |
122 | 3.34M | if let Err(err) = range.case_fold_simple(&mut self.ranges) { |
123 | 0 | self.canonicalize(); |
124 | 0 | return Err(err); |
125 | 3.34M | } |
126 | | } |
127 | 1.56M | self.canonicalize(); |
128 | 1.56M | self.folded = true; |
129 | 1.56M | Ok(()) |
130 | 1.61M | } <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::case_fold_simple Line | Count | Source | 115 | 770k | pub fn case_fold_simple(&mut self) -> Result<(), unicode::CaseFoldError> { | 116 | 770k | if self.folded { | 117 | 48.9k | return Ok(()); | 118 | 722k | } | 119 | 722k | let len = self.ranges.len(); | 120 | 2.44M | for i in 0..len { | 121 | 2.44M | let range = self.ranges[i]; | 122 | 2.44M | if let Err(err) = range.case_fold_simple(&mut self.ranges) { | 123 | 0 | self.canonicalize(); | 124 | 0 | return Err(err); | 125 | 2.44M | } | 126 | | } | 127 | 722k | self.canonicalize(); | 128 | 722k | self.folded = true; | 129 | 722k | Ok(()) | 130 | 770k | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::case_fold_simple Line | Count | Source | 115 | 848k | pub fn case_fold_simple(&mut self) -> Result<(), unicode::CaseFoldError> { | 116 | 848k | if self.folded { | 117 | 5.72k | return Ok(()); | 118 | 843k | } | 119 | 843k | let len = self.ranges.len(); | 120 | 891k | for i in 0..len { | 121 | 891k | let range = self.ranges[i]; | 122 | 891k | if let Err(err) = range.case_fold_simple(&mut self.ranges) { | 123 | 0 | self.canonicalize(); | 124 | 0 | return Err(err); | 125 | 891k | } | 126 | | } | 127 | 843k | self.canonicalize(); | 128 | 843k | self.folded = true; | 129 | 843k | Ok(()) | 130 | 848k | } |
|
131 | | |
132 | | /// Union this set with the given set, in place. |
133 | 289k | pub fn union(&mut self, other: &IntervalSet<I>) { |
134 | 289k | if other.ranges.is_empty() || self.ranges == other.ranges { |
135 | 48.0k | return; |
136 | 241k | } |
137 | | // This could almost certainly be done more efficiently. |
138 | 241k | self.ranges.extend(&other.ranges); |
139 | 241k | self.canonicalize(); |
140 | 241k | self.folded = self.folded && other.folded; |
141 | 289k | } <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::union Line | Count | Source | 133 | 196k | pub fn union(&mut self, other: &IntervalSet<I>) { | 134 | 196k | if other.ranges.is_empty() || self.ranges == other.ranges { | 135 | 14.3k | return; | 136 | 181k | } | 137 | | // This could almost certainly be done more efficiently. | 138 | 181k | self.ranges.extend(&other.ranges); | 139 | 181k | self.canonicalize(); | 140 | 181k | self.folded = self.folded && other.folded; | 141 | 196k | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::union Line | Count | Source | 133 | 93.2k | pub fn union(&mut self, other: &IntervalSet<I>) { | 134 | 93.2k | if other.ranges.is_empty() || self.ranges == other.ranges { | 135 | 33.6k | return; | 136 | 59.6k | } | 137 | | // This could almost certainly be done more efficiently. | 138 | 59.6k | self.ranges.extend(&other.ranges); | 139 | 59.6k | self.canonicalize(); | 140 | 59.6k | self.folded = self.folded && other.folded; | 141 | 93.2k | } |
|
142 | | |
143 | | /// Intersect this set with the given set, in place. |
144 | 66.6k | pub fn intersect(&mut self, other: &IntervalSet<I>) { |
145 | 66.6k | if self.ranges.is_empty() { |
146 | 3.29k | return; |
147 | 63.3k | } |
148 | 63.3k | if other.ranges.is_empty() { |
149 | 37.1k | self.ranges.clear(); |
150 | | // An empty set is case folded. |
151 | 37.1k | self.folded = true; |
152 | 37.1k | return; |
153 | 26.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 | 26.2k | let drain_end = self.ranges.len(); |
160 | | |
161 | 26.2k | let mut ita = 0..drain_end; |
162 | 26.2k | let mut itb = 0..other.ranges.len(); |
163 | 26.2k | let mut a = ita.next().unwrap(); |
164 | 26.2k | let mut b = itb.next().unwrap(); |
165 | | loop { |
166 | 905k | if let Some(ab) = self.ranges[a].intersect(&other.ranges[b]) { |
167 | 269k | self.ranges.push(ab); |
168 | 635k | } |
169 | 905k | let (it, aorb) = |
170 | 905k | if self.ranges[a].upper() < other.ranges[b].upper() { |
171 | 535k | (&mut ita, &mut a) |
172 | | } else { |
173 | 369k | (&mut itb, &mut b) |
174 | | }; |
175 | 905k | match it.next() { |
176 | 878k | Some(v) => *aorb = v, |
177 | 26.2k | None => break, |
178 | | } |
179 | | } |
180 | 26.2k | self.ranges.drain(..drain_end); |
181 | 26.2k | self.folded = self.folded && other.folded; |
182 | 66.6k | } <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::intersect Line | Count | Source | 144 | 33.0k | pub fn intersect(&mut self, other: &IntervalSet<I>) { | 145 | 33.0k | if self.ranges.is_empty() { | 146 | 259 | return; | 147 | 32.8k | } | 148 | 32.8k | if other.ranges.is_empty() { | 149 | 11.8k | self.ranges.clear(); | 150 | | // An empty set is case folded. | 151 | 11.8k | self.folded = true; | 152 | 11.8k | return; | 153 | 21.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 | 21.0k | let drain_end = self.ranges.len(); | 160 | | | 161 | 21.0k | let mut ita = 0..drain_end; | 162 | 21.0k | let mut itb = 0..other.ranges.len(); | 163 | 21.0k | let mut a = ita.next().unwrap(); | 164 | 21.0k | let mut b = itb.next().unwrap(); | 165 | | loop { | 166 | 731k | if let Some(ab) = self.ranges[a].intersect(&other.ranges[b]) { | 167 | 210k | self.ranges.push(ab); | 168 | 521k | } | 169 | 731k | let (it, aorb) = | 170 | 731k | if self.ranges[a].upper() < other.ranges[b].upper() { | 171 | 448k | (&mut ita, &mut a) | 172 | | } else { | 173 | 283k | (&mut itb, &mut b) | 174 | | }; | 175 | 731k | match it.next() { | 176 | 710k | Some(v) => *aorb = v, | 177 | 21.0k | None => break, | 178 | | } | 179 | | } | 180 | 21.0k | self.ranges.drain(..drain_end); | 181 | 21.0k | self.folded = self.folded && other.folded; | 182 | 33.0k | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::intersect Line | Count | Source | 144 | 33.5k | pub fn intersect(&mut self, other: &IntervalSet<I>) { | 145 | 33.5k | if self.ranges.is_empty() { | 146 | 3.03k | return; | 147 | 30.5k | } | 148 | 30.5k | if other.ranges.is_empty() { | 149 | 25.3k | self.ranges.clear(); | 150 | | // An empty set is case folded. | 151 | 25.3k | self.folded = true; | 152 | 25.3k | return; | 153 | 5.19k | } | 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 | 5.19k | let drain_end = self.ranges.len(); | 160 | | | 161 | 5.19k | let mut ita = 0..drain_end; | 162 | 5.19k | let mut itb = 0..other.ranges.len(); | 163 | 5.19k | let mut a = ita.next().unwrap(); | 164 | 5.19k | let mut b = itb.next().unwrap(); | 165 | | loop { | 166 | 173k | if let Some(ab) = self.ranges[a].intersect(&other.ranges[b]) { | 167 | 58.8k | self.ranges.push(ab); | 168 | 114k | } | 169 | 173k | let (it, aorb) = | 170 | 173k | if self.ranges[a].upper() < other.ranges[b].upper() { | 171 | 86.8k | (&mut ita, &mut a) | 172 | | } else { | 173 | 86.2k | (&mut itb, &mut b) | 174 | | }; | 175 | 173k | match it.next() { | 176 | 167k | Some(v) => *aorb = v, | 177 | 5.19k | None => break, | 178 | | } | 179 | | } | 180 | 5.19k | self.ranges.drain(..drain_end); | 181 | 5.19k | self.folded = self.folded && other.folded; | 182 | 33.5k | } |
|
183 | | |
184 | | /// Subtract the given set from this set, in place. |
185 | 91.9k | pub fn difference(&mut self, other: &IntervalSet<I>) { |
186 | 91.9k | if self.ranges.is_empty() || other.ranges.is_empty() { |
187 | 52.0k | return; |
188 | 39.8k | } |
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 | 39.8k | let drain_end = self.ranges.len(); |
203 | 39.8k | let (mut a, mut b) = (0, 0); |
204 | 826k | '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 | 786k | if other.ranges[b].upper() < self.ranges[a].lower() { |
209 | 203k | b += 1; |
210 | 203k | continue; |
211 | 582k | } |
212 | | // ... similarly for the `a` range. If it's less than the smallest |
213 | | // `b` range, then we can add it as-is. |
214 | 582k | if self.ranges[a].upper() < other.ranges[b].lower() { |
215 | 328k | let range = self.ranges[a]; |
216 | 328k | self.ranges.push(range); |
217 | 328k | a += 1; |
218 | 328k | continue; |
219 | 254k | } |
220 | | // Otherwise, we have overlapping ranges. |
221 | 254k | 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 | 254k | let mut range = self.ranges[a]; |
235 | 381k | while b < other.ranges.len() |
236 | 370k | && !range.is_intersection_empty(&other.ranges[b]) |
237 | | { |
238 | 324k | let old_range = range; |
239 | 324k | 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 | 196k | a += 1; |
244 | 196k | continue 'LOOP; |
245 | | } |
246 | 61.4k | (Some(range1), None) | (None, Some(range1)) => range1, |
247 | 66.3k | (Some(range1), Some(range2)) => { |
248 | 66.3k | self.ranges.push(range1); |
249 | 66.3k | 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 | 127k | if other.ranges[b].upper() > old_range.upper() { |
259 | 347 | break; |
260 | 127k | } |
261 | | // Otherwise, the next `b` range might apply to the current |
262 | | // `a` range. |
263 | 127k | b += 1; |
264 | | } |
265 | 58.1k | self.ranges.push(range); |
266 | 58.1k | a += 1; |
267 | | } |
268 | 144k | while a < drain_end { |
269 | 104k | let range = self.ranges[a]; |
270 | 104k | self.ranges.push(range); |
271 | 104k | a += 1; |
272 | 104k | } |
273 | 39.8k | self.ranges.drain(..drain_end); |
274 | 39.8k | self.folded = self.folded && other.folded; |
275 | 91.9k | } <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::difference Line | Count | Source | 185 | 41.3k | pub fn difference(&mut self, other: &IntervalSet<I>) { | 186 | 41.3k | if self.ranges.is_empty() || other.ranges.is_empty() { | 187 | 15.3k | return; | 188 | 26.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 | 26.0k | let drain_end = self.ranges.len(); | 203 | 26.0k | let (mut a, mut b) = (0, 0); | 204 | 666k | '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 | 639k | if other.ranges[b].upper() < self.ranges[a].lower() { | 209 | 151k | b += 1; | 210 | 151k | continue; | 211 | 488k | } | 212 | | // ... similarly for the `a` range. If it's less than the smallest | 213 | | // `b` range, then we can add it as-is. | 214 | 488k | if self.ranges[a].upper() < other.ranges[b].lower() { | 215 | 286k | let range = self.ranges[a]; | 216 | 286k | self.ranges.push(range); | 217 | 286k | a += 1; | 218 | 286k | continue; | 219 | 202k | } | 220 | | // Otherwise, we have overlapping ranges. | 221 | 202k | 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 | 202k | let mut range = self.ranges[a]; | 235 | 292k | while b < other.ranges.len() | 236 | 283k | && !range.is_intersection_empty(&other.ranges[b]) | 237 | | { | 238 | 242k | let old_range = range; | 239 | 242k | 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 | 151k | a += 1; | 244 | 151k | continue 'LOOP; | 245 | | } | 246 | 55.2k | (Some(range1), None) | (None, Some(range1)) => range1, | 247 | 35.6k | (Some(range1), Some(range2)) => { | 248 | 35.6k | self.ranges.push(range1); | 249 | 35.6k | 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 | 90.9k | if other.ranges[b].upper() > old_range.upper() { | 259 | 134 | break; | 260 | 90.7k | } | 261 | | // Otherwise, the next `b` range might apply to the current | 262 | | // `a` range. | 263 | 90.7k | b += 1; | 264 | | } | 265 | 50.3k | self.ranges.push(range); | 266 | 50.3k | a += 1; | 267 | | } | 268 | 72.3k | while a < drain_end { | 269 | 46.2k | let range = self.ranges[a]; | 270 | 46.2k | self.ranges.push(range); | 271 | 46.2k | a += 1; | 272 | 46.2k | } | 273 | 26.0k | self.ranges.drain(..drain_end); | 274 | 26.0k | self.folded = self.folded && other.folded; | 275 | 41.3k | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::difference Line | Count | Source | 185 | 50.5k | pub fn difference(&mut self, other: &IntervalSet<I>) { | 186 | 50.5k | if self.ranges.is_empty() || other.ranges.is_empty() { | 187 | 36.7k | return; | 188 | 13.8k | } | 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 | 13.8k | let drain_end = self.ranges.len(); | 203 | 13.8k | let (mut a, mut b) = (0, 0); | 204 | 160k | '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 | 146k | if other.ranges[b].upper() < self.ranges[a].lower() { | 209 | 51.7k | b += 1; | 210 | 51.7k | continue; | 211 | 94.5k | } | 212 | | // ... similarly for the `a` range. If it's less than the smallest | 213 | | // `b` range, then we can add it as-is. | 214 | 94.5k | if self.ranges[a].upper() < other.ranges[b].lower() { | 215 | 42.2k | let range = self.ranges[a]; | 216 | 42.2k | self.ranges.push(range); | 217 | 42.2k | a += 1; | 218 | 42.2k | continue; | 219 | 52.2k | } | 220 | | // Otherwise, we have overlapping ranges. | 221 | 52.2k | 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 | 52.2k | let mut range = self.ranges[a]; | 235 | 88.9k | while b < other.ranges.len() | 236 | 87.3k | && !range.is_intersection_empty(&other.ranges[b]) | 237 | | { | 238 | 81.3k | let old_range = range; | 239 | 81.3k | 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 | 44.4k | a += 1; | 244 | 44.4k | continue 'LOOP; | 245 | | } | 246 | 6.22k | (Some(range1), None) | (None, Some(range1)) => range1, | 247 | 30.7k | (Some(range1), Some(range2)) => { | 248 | 30.7k | self.ranges.push(range1); | 249 | 30.7k | 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 | 36.9k | if other.ranges[b].upper() > old_range.upper() { | 259 | 213 | break; | 260 | 36.7k | } | 261 | | // Otherwise, the next `b` range might apply to the current | 262 | | // `a` range. | 263 | 36.7k | b += 1; | 264 | | } | 265 | 7.80k | self.ranges.push(range); | 266 | 7.80k | a += 1; | 267 | | } | 268 | 71.8k | while a < drain_end { | 269 | 57.9k | let range = self.ranges[a]; | 270 | 57.9k | self.ranges.push(range); | 271 | 57.9k | a += 1; | 272 | 57.9k | } | 273 | 13.8k | self.ranges.drain(..drain_end); | 274 | 13.8k | self.folded = self.folded && other.folded; | 275 | 50.5k | } |
|
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 | 66.0k | pub fn symmetric_difference(&mut self, other: &IntervalSet<I>) { |
285 | | // TODO(burntsushi): Fix this so that it amortizes allocation. |
286 | 66.0k | let mut intersection = self.clone(); |
287 | 66.0k | intersection.intersect(other); |
288 | 66.0k | self.union(other); |
289 | 66.0k | self.difference(&intersection); |
290 | 66.0k | } <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::symmetric_difference Line | Count | Source | 284 | 32.9k | pub fn symmetric_difference(&mut self, other: &IntervalSet<I>) { | 285 | | // TODO(burntsushi): Fix this so that it amortizes allocation. | 286 | 32.9k | let mut intersection = self.clone(); | 287 | 32.9k | intersection.intersect(other); | 288 | 32.9k | self.union(other); | 289 | 32.9k | self.difference(&intersection); | 290 | 32.9k | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::symmetric_difference Line | Count | Source | 284 | 33.1k | pub fn symmetric_difference(&mut self, other: &IntervalSet<I>) { | 285 | | // TODO(burntsushi): Fix this so that it amortizes allocation. | 286 | 33.1k | let mut intersection = self.clone(); | 287 | 33.1k | intersection.intersect(other); | 288 | 33.1k | self.union(other); | 289 | 33.1k | self.difference(&intersection); | 290 | 33.1k | } |
|
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 | 123k | pub fn negate(&mut self) { |
297 | 123k | if self.ranges.is_empty() { |
298 | 261 | let (min, max) = (I::Bound::min_value(), I::Bound::max_value()); |
299 | 261 | self.ranges.push(I::create(min, max)); |
300 | | // The set containing everything must case folded. |
301 | 261 | self.folded = true; |
302 | 261 | return; |
303 | 122k | } |
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 | 122k | let drain_end = self.ranges.len(); |
310 | | |
311 | | // We do checked arithmetic below because of the canonical ordering |
312 | | // invariant. |
313 | 122k | if self.ranges[0].lower() > I::Bound::min_value() { |
314 | 122k | let upper = self.ranges[0].lower().decrement(); |
315 | 122k | self.ranges.push(I::create(I::Bound::min_value(), upper)); |
316 | 122k | } |
317 | 306k | for i in 1..drain_end { |
318 | 306k | let lower = self.ranges[i - 1].upper().increment(); |
319 | 306k | let upper = self.ranges[i].lower().decrement(); |
320 | 306k | self.ranges.push(I::create(lower, upper)); |
321 | 306k | } |
322 | 122k | if self.ranges[drain_end - 1].upper() < I::Bound::max_value() { |
323 | 122k | let lower = self.ranges[drain_end - 1].upper().increment(); |
324 | 122k | self.ranges.push(I::create(lower, I::Bound::max_value())); |
325 | 122k | } |
326 | 122k | 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 | 123k | } <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::negate Line | Count | Source | 296 | 199 | pub fn negate(&mut self) { | 297 | 199 | if self.ranges.is_empty() { | 298 | 5 | let (min, max) = (I::Bound::min_value(), I::Bound::max_value()); | 299 | 5 | self.ranges.push(I::create(min, max)); | 300 | | // The set containing everything must case folded. | 301 | 5 | self.folded = true; | 302 | 5 | return; | 303 | 194 | } | 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 | 194 | let drain_end = self.ranges.len(); | 310 | | | 311 | | // We do checked arithmetic below because of the canonical ordering | 312 | | // invariant. | 313 | 194 | if self.ranges[0].lower() > I::Bound::min_value() { | 314 | 185 | let upper = self.ranges[0].lower().decrement(); | 315 | 185 | self.ranges.push(I::create(I::Bound::min_value(), upper)); | 316 | 185 | } | 317 | 2.30k | for i in 1..drain_end { | 318 | 2.30k | let lower = self.ranges[i - 1].upper().increment(); | 319 | 2.30k | let upper = self.ranges[i].lower().decrement(); | 320 | 2.30k | self.ranges.push(I::create(lower, upper)); | 321 | 2.30k | } | 322 | 194 | if self.ranges[drain_end - 1].upper() < I::Bound::max_value() { | 323 | 194 | let lower = self.ranges[drain_end - 1].upper().increment(); | 324 | 194 | self.ranges.push(I::create(lower, I::Bound::max_value())); | 325 | 194 | } | 326 | 194 | 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 | 199 | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::negate Line | Count | Source | 296 | 122k | pub fn negate(&mut self) { | 297 | 122k | if self.ranges.is_empty() { | 298 | 256 | let (min, max) = (I::Bound::min_value(), I::Bound::max_value()); | 299 | 256 | self.ranges.push(I::create(min, max)); | 300 | | // The set containing everything must case folded. | 301 | 256 | self.folded = true; | 302 | 256 | return; | 303 | 122k | } | 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 | 122k | let drain_end = self.ranges.len(); | 310 | | | 311 | | // We do checked arithmetic below because of the canonical ordering | 312 | | // invariant. | 313 | 122k | if self.ranges[0].lower() > I::Bound::min_value() { | 314 | 122k | let upper = self.ranges[0].lower().decrement(); | 315 | 122k | self.ranges.push(I::create(I::Bound::min_value(), upper)); | 316 | 122k | } | 317 | 304k | for i in 1..drain_end { | 318 | 304k | let lower = self.ranges[i - 1].upper().increment(); | 319 | 304k | let upper = self.ranges[i].lower().decrement(); | 320 | 304k | self.ranges.push(I::create(lower, upper)); | 321 | 304k | } | 322 | 122k | if self.ranges[drain_end - 1].upper() < I::Bound::max_value() { | 323 | 122k | let lower = self.ranges[drain_end - 1].upper().increment(); | 324 | 122k | self.ranges.push(I::create(lower, I::Bound::max_value())); | 325 | 122k | } | 326 | 122k | 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 | 122k | } |
|
341 | | |
342 | | /// Converts this set into a canonical ordering. |
343 | 7.10M | fn canonicalize(&mut self) { |
344 | 7.10M | if self.is_canonical() { |
345 | 2.92M | return; |
346 | 4.17M | } |
347 | 4.17M | self.ranges.sort(); |
348 | 4.17M | 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 | 4.17M | let drain_end = self.ranges.len(); |
354 | 52.4M | 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 | 52.4M | if self.ranges.len() > drain_end { |
358 | 48.3M | let (last, rest) = self.ranges.split_last_mut().unwrap(); |
359 | 48.3M | if let Some(union) = last.union(&rest[oldi]) { |
360 | 5.42M | *last = union; |
361 | 5.42M | continue; |
362 | 42.8M | } |
363 | 4.17M | } |
364 | 47.0M | let range = self.ranges[oldi]; |
365 | 47.0M | self.ranges.push(range); |
366 | | } |
367 | 4.17M | self.ranges.drain(..drain_end); |
368 | 7.10M | } <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::canonicalize Line | Count | Source | 343 | 3.35M | fn canonicalize(&mut self) { | 344 | 3.35M | if self.is_canonical() { | 345 | 1.47M | return; | 346 | 1.87M | } | 347 | 1.87M | self.ranges.sort(); | 348 | 1.87M | 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.87M | let drain_end = self.ranges.len(); | 354 | 27.8M | 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 | 27.8M | if self.ranges.len() > drain_end { | 358 | 25.9M | let (last, rest) = self.ranges.split_last_mut().unwrap(); | 359 | 25.9M | if let Some(union) = last.union(&rest[oldi]) { | 360 | 2.57M | *last = union; | 361 | 2.57M | continue; | 362 | 23.3M | } | 363 | 1.87M | } | 364 | 25.2M | let range = self.ranges[oldi]; | 365 | 25.2M | self.ranges.push(range); | 366 | | } | 367 | 1.87M | self.ranges.drain(..drain_end); | 368 | 3.35M | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::canonicalize Line | Count | Source | 343 | 3.74M | fn canonicalize(&mut self) { | 344 | 3.74M | if self.is_canonical() { | 345 | 1.44M | return; | 346 | 2.29M | } | 347 | 2.29M | self.ranges.sort(); | 348 | 2.29M | 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 | 2.29M | let drain_end = self.ranges.len(); | 354 | 24.6M | 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 | 24.6M | if self.ranges.len() > drain_end { | 358 | 22.3M | let (last, rest) = self.ranges.split_last_mut().unwrap(); | 359 | 22.3M | if let Some(union) = last.union(&rest[oldi]) { | 360 | 2.84M | *last = union; | 361 | 2.84M | continue; | 362 | 19.5M | } | 363 | 2.29M | } | 364 | 21.8M | let range = self.ranges[oldi]; | 365 | 21.8M | self.ranges.push(range); | 366 | | } | 367 | 2.29M | self.ranges.drain(..drain_end); | 368 | 3.74M | } |
|
369 | | |
370 | | /// Returns true if and only if this class is in a canonical ordering. |
371 | 7.10M | fn is_canonical(&self) -> bool { |
372 | 51.4M | for pair in self.ranges.windows(2) { |
373 | 51.4M | if pair[0] >= pair[1] { |
374 | 4.08M | return false; |
375 | 47.4M | } |
376 | 47.4M | if pair[0].is_contiguous(&pair[1]) { |
377 | 83.5k | return false; |
378 | 47.3M | } |
379 | | } |
380 | 2.92M | true |
381 | 7.10M | } <regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassBytesRange>>::is_canonical Line | Count | Source | 371 | 3.35M | fn is_canonical(&self) -> bool { | 372 | 25.2M | for pair in self.ranges.windows(2) { | 373 | 25.2M | if pair[0] >= pair[1] { | 374 | 1.80M | return false; | 375 | 23.4M | } | 376 | 23.4M | if pair[0].is_contiguous(&pair[1]) { | 377 | 72.7k | return false; | 378 | 23.3M | } | 379 | | } | 380 | 1.47M | true | 381 | 3.35M | } |
<regex_syntax::hir::interval::IntervalSet<regex_syntax::hir::ClassUnicodeRange>>::is_canonical Line | Count | Source | 371 | 3.74M | fn is_canonical(&self) -> bool { | 372 | 26.2M | for pair in self.ranges.windows(2) { | 373 | 26.2M | if pair[0] >= pair[1] { | 374 | 2.28M | return false; | 375 | 23.9M | } | 376 | 23.9M | if pair[0].is_contiguous(&pair[1]) { | 377 | 10.8k | return false; | 378 | 23.9M | } | 379 | | } | 380 | 1.44M | true | 381 | 3.74M | } |
|
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 | 18.1M | fn next(&mut self) -> Option<&'a I> { |
392 | 18.1M | self.0.next() |
393 | 18.1M | } <regex_syntax::hir::interval::IntervalSetIter<regex_syntax::hir::ClassBytesRange> as core::iter::traits::iterator::Iterator>::next Line | Count | Source | 391 | 1.73M | fn next(&mut self) -> Option<&'a I> { | 392 | 1.73M | self.0.next() | 393 | 1.73M | } |
<regex_syntax::hir::interval::IntervalSetIter<regex_syntax::hir::ClassUnicodeRange> as core::iter::traits::iterator::Iterator>::next Line | Count | Source | 391 | 16.4M | fn next(&mut self) -> Option<&'a I> { | 392 | 16.4M | self.0.next() | 393 | 16.4M | } |
|
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 | 24.7M | fn create(lower: Self::Bound, upper: Self::Bound) -> Self { |
412 | 24.7M | let mut int = Self::default(); |
413 | 24.7M | if lower <= upper { |
414 | 24.7M | int.set_lower(lower); |
415 | 24.7M | int.set_upper(upper); |
416 | 24.7M | } else { |
417 | 0 | int.set_lower(upper); |
418 | 0 | int.set_upper(lower); |
419 | 0 | } |
420 | 24.7M | int |
421 | 24.7M | } <regex_syntax::hir::ClassBytesRange as regex_syntax::hir::interval::Interval>::create Line | Count | Source | 411 | 11.9M | fn create(lower: Self::Bound, upper: Self::Bound) -> Self { | 412 | 11.9M | let mut int = Self::default(); | 413 | 11.9M | if lower <= upper { | 414 | 11.9M | int.set_lower(lower); | 415 | 11.9M | int.set_upper(upper); | 416 | 11.9M | } else { | 417 | 0 | int.set_lower(upper); | 418 | 0 | int.set_upper(lower); | 419 | 0 | } | 420 | 11.9M | int | 421 | 11.9M | } |
<regex_syntax::hir::ClassUnicodeRange as regex_syntax::hir::interval::Interval>::create Line | Count | Source | 411 | 12.7M | fn create(lower: Self::Bound, upper: Self::Bound) -> Self { | 412 | 12.7M | let mut int = Self::default(); | 413 | 12.7M | if lower <= upper { | 414 | 12.7M | int.set_lower(lower); | 415 | 12.7M | int.set_upper(upper); | 416 | 12.7M | } else { | 417 | 0 | int.set_lower(upper); | 418 | 0 | int.set_upper(lower); | 419 | 0 | } | 420 | 12.7M | int | 421 | 12.7M | } |
|
422 | | |
423 | | /// Union the given overlapping range into this range. |
424 | | /// |
425 | | /// If the two ranges aren't contiguous, then this returns `None`. |
426 | 48.3M | fn union(&self, other: &Self) -> Option<Self> { |
427 | 48.3M | if !self.is_contiguous(other) { |
428 | 42.8M | return None; |
429 | 5.42M | } |
430 | 5.42M | let lower = cmp::min(self.lower(), other.lower()); |
431 | 5.42M | let upper = cmp::max(self.upper(), other.upper()); |
432 | 5.42M | Some(Self::create(lower, upper)) |
433 | 48.3M | } <regex_syntax::hir::ClassBytesRange as regex_syntax::hir::interval::Interval>::union Line | Count | Source | 426 | 25.9M | fn union(&self, other: &Self) -> Option<Self> { | 427 | 25.9M | if !self.is_contiguous(other) { | 428 | 23.3M | return None; | 429 | 2.57M | } | 430 | 2.57M | let lower = cmp::min(self.lower(), other.lower()); | 431 | 2.57M | let upper = cmp::max(self.upper(), other.upper()); | 432 | 2.57M | Some(Self::create(lower, upper)) | 433 | 25.9M | } |
<regex_syntax::hir::ClassUnicodeRange as regex_syntax::hir::interval::Interval>::union Line | Count | Source | 426 | 22.3M | fn union(&self, other: &Self) -> Option<Self> { | 427 | 22.3M | if !self.is_contiguous(other) { | 428 | 19.5M | return None; | 429 | 2.84M | } | 430 | 2.84M | let lower = cmp::min(self.lower(), other.lower()); | 431 | 2.84M | let upper = cmp::max(self.upper(), other.upper()); | 432 | 2.84M | Some(Self::create(lower, upper)) | 433 | 22.3M | } |
|
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 | 905k | fn intersect(&self, other: &Self) -> Option<Self> { |
439 | 905k | let lower = cmp::max(self.lower(), other.lower()); |
440 | 905k | let upper = cmp::min(self.upper(), other.upper()); |
441 | 905k | if lower <= upper { |
442 | 269k | Some(Self::create(lower, upper)) |
443 | | } else { |
444 | 635k | None |
445 | | } |
446 | 905k | } <regex_syntax::hir::ClassBytesRange as regex_syntax::hir::interval::Interval>::intersect Line | Count | Source | 438 | 731k | fn intersect(&self, other: &Self) -> Option<Self> { | 439 | 731k | let lower = cmp::max(self.lower(), other.lower()); | 440 | 731k | let upper = cmp::min(self.upper(), other.upper()); | 441 | 731k | if lower <= upper { | 442 | 210k | Some(Self::create(lower, upper)) | 443 | | } else { | 444 | 521k | None | 445 | | } | 446 | 731k | } |
<regex_syntax::hir::ClassUnicodeRange as regex_syntax::hir::interval::Interval>::intersect Line | Count | Source | 438 | 173k | fn intersect(&self, other: &Self) -> Option<Self> { | 439 | 173k | let lower = cmp::max(self.lower(), other.lower()); | 440 | 173k | let upper = cmp::min(self.upper(), other.upper()); | 441 | 173k | if lower <= upper { | 442 | 58.8k | Some(Self::create(lower, upper)) | 443 | | } else { | 444 | 114k | None | 445 | | } | 446 | 173k | } |
|
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 | 324k | fn difference(&self, other: &Self) -> (Option<Self>, Option<Self>) { |
454 | 324k | if self.is_subset(other) { |
455 | 196k | return (None, None); |
456 | 127k | } |
457 | 127k | if self.is_intersection_empty(other) { |
458 | 0 | return (Some(self.clone()), None); |
459 | 127k | } |
460 | 127k | let add_lower = other.lower() > self.lower(); |
461 | 127k | 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 | 127k | assert!(add_lower || add_upper); |
465 | 127k | let mut ret = (None, None); |
466 | 127k | if add_lower { |
467 | 97.1k | let upper = other.lower().decrement(); |
468 | 97.1k | ret.0 = Some(Self::create(self.lower(), upper)); |
469 | 97.1k | } |
470 | 127k | if add_upper { |
471 | 97.0k | let lower = other.upper().increment(); |
472 | 97.0k | let range = Self::create(lower, self.upper()); |
473 | 97.0k | if ret.0.is_none() { |
474 | 30.6k | ret.0 = Some(range); |
475 | 66.3k | } else { |
476 | 66.3k | ret.1 = Some(range); |
477 | 66.3k | } |
478 | 30.8k | } |
479 | 127k | ret |
480 | 324k | } <regex_syntax::hir::ClassBytesRange as regex_syntax::hir::interval::Interval>::difference Line | Count | Source | 453 | 242k | fn difference(&self, other: &Self) -> (Option<Self>, Option<Self>) { | 454 | 242k | if self.is_subset(other) { | 455 | 151k | return (None, None); | 456 | 90.9k | } | 457 | 90.9k | if self.is_intersection_empty(other) { | 458 | 0 | return (Some(self.clone()), None); | 459 | 90.9k | } | 460 | 90.9k | let add_lower = other.lower() > self.lower(); | 461 | 90.9k | 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 | 90.9k | assert!(add_lower || add_upper); | 465 | 90.9k | let mut ret = (None, None); | 466 | 90.9k | if add_lower { | 467 | 63.5k | let upper = other.lower().decrement(); | 468 | 63.5k | ret.0 = Some(Self::create(self.lower(), upper)); | 469 | 63.5k | } | 470 | 90.9k | if add_upper { | 471 | 63.0k | let lower = other.upper().increment(); | 472 | 63.0k | let range = Self::create(lower, self.upper()); | 473 | 63.0k | if ret.0.is_none() { | 474 | 27.3k | ret.0 = Some(range); | 475 | 35.6k | } else { | 476 | 35.6k | ret.1 = Some(range); | 477 | 35.6k | } | 478 | 27.9k | } | 479 | 90.9k | ret | 480 | 242k | } |
<regex_syntax::hir::ClassUnicodeRange as regex_syntax::hir::interval::Interval>::difference Line | Count | Source | 453 | 81.3k | fn difference(&self, other: &Self) -> (Option<Self>, Option<Self>) { | 454 | 81.3k | if self.is_subset(other) { | 455 | 44.4k | return (None, None); | 456 | 36.9k | } | 457 | 36.9k | if self.is_intersection_empty(other) { | 458 | 0 | return (Some(self.clone()), None); | 459 | 36.9k | } | 460 | 36.9k | let add_lower = other.lower() > self.lower(); | 461 | 36.9k | 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 | 36.9k | assert!(add_lower || add_upper); | 465 | 36.9k | let mut ret = (None, None); | 466 | 36.9k | if add_lower { | 467 | 33.6k | let upper = other.lower().decrement(); | 468 | 33.6k | ret.0 = Some(Self::create(self.lower(), upper)); | 469 | 33.6k | } | 470 | 36.9k | if add_upper { | 471 | 34.0k | let lower = other.upper().increment(); | 472 | 34.0k | let range = Self::create(lower, self.upper()); | 473 | 34.0k | if ret.0.is_none() { | 474 | 3.32k | ret.0 = Some(range); | 475 | 30.7k | } else { | 476 | 30.7k | ret.1 = Some(range); | 477 | 30.7k | } | 478 | 2.89k | } | 479 | 36.9k | ret | 480 | 81.3k | } |
|
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 | 95.7M | fn is_contiguous(&self, other: &Self) -> bool { |
486 | 95.7M | let lower1 = self.lower().as_u32(); |
487 | 95.7M | let upper1 = self.upper().as_u32(); |
488 | 95.7M | let lower2 = other.lower().as_u32(); |
489 | 95.7M | let upper2 = other.upper().as_u32(); |
490 | 95.7M | cmp::max(lower1, lower2) <= cmp::min(upper1, upper2).saturating_add(1) |
491 | 95.7M | } <regex_syntax::hir::ClassBytesRange as regex_syntax::hir::interval::Interval>::is_contiguous Line | Count | Source | 485 | 49.4M | fn is_contiguous(&self, other: &Self) -> bool { | 486 | 49.4M | let lower1 = self.lower().as_u32(); | 487 | 49.4M | let upper1 = self.upper().as_u32(); | 488 | 49.4M | let lower2 = other.lower().as_u32(); | 489 | 49.4M | let upper2 = other.upper().as_u32(); | 490 | 49.4M | cmp::max(lower1, lower2) <= cmp::min(upper1, upper2).saturating_add(1) | 491 | 49.4M | } |
<regex_syntax::hir::ClassUnicodeRange as regex_syntax::hir::interval::Interval>::is_contiguous Line | Count | Source | 485 | 46.2M | fn is_contiguous(&self, other: &Self) -> bool { | 486 | 46.2M | let lower1 = self.lower().as_u32(); | 487 | 46.2M | let upper1 = self.upper().as_u32(); | 488 | 46.2M | let lower2 = other.lower().as_u32(); | 489 | 46.2M | let upper2 = other.upper().as_u32(); | 490 | 46.2M | cmp::max(lower1, lower2) <= cmp::min(upper1, upper2).saturating_add(1) | 491 | 46.2M | } |
|
492 | | |
493 | | /// Returns true if and only if the intersection of this range and the |
494 | | /// other range is empty. |
495 | 5.65M | fn is_intersection_empty(&self, other: &Self) -> bool { |
496 | 5.65M | let (lower1, upper1) = (self.lower(), self.upper()); |
497 | 5.65M | let (lower2, upper2) = (other.lower(), other.upper()); |
498 | 5.65M | cmp::max(lower1, lower2) > cmp::min(upper1, upper2) |
499 | 5.65M | } <regex_syntax::hir::ClassBytesRange as regex_syntax::hir::interval::Interval>::is_intersection_empty Line | Count | Source | 495 | 5.47M | fn is_intersection_empty(&self, other: &Self) -> bool { | 496 | 5.47M | let (lower1, upper1) = (self.lower(), self.upper()); | 497 | 5.47M | let (lower2, upper2) = (other.lower(), other.upper()); | 498 | 5.47M | cmp::max(lower1, lower2) > cmp::min(upper1, upper2) | 499 | 5.47M | } |
<regex_syntax::hir::ClassUnicodeRange as regex_syntax::hir::interval::Interval>::is_intersection_empty Line | Count | Source | 495 | 176k | fn is_intersection_empty(&self, other: &Self) -> bool { | 496 | 176k | let (lower1, upper1) = (self.lower(), self.upper()); | 497 | 176k | let (lower2, upper2) = (other.lower(), other.upper()); | 498 | 176k | cmp::max(lower1, lower2) > cmp::min(upper1, upper2) | 499 | 176k | } |
|
500 | | |
501 | | /// Returns true if and only if this range is a subset of the other range. |
502 | 324k | fn is_subset(&self, other: &Self) -> bool { |
503 | 324k | let (lower1, upper1) = (self.lower(), self.upper()); |
504 | 324k | let (lower2, upper2) = (other.lower(), other.upper()); |
505 | 324k | (lower2 <= lower1 && lower1 <= upper2) |
506 | 226k | && (lower2 <= upper1 && upper1 <= upper2) |
507 | 324k | } <regex_syntax::hir::ClassBytesRange as regex_syntax::hir::interval::Interval>::is_subset Line | Count | Source | 502 | 242k | fn is_subset(&self, other: &Self) -> bool { | 503 | 242k | let (lower1, upper1) = (self.lower(), self.upper()); | 504 | 242k | let (lower2, upper2) = (other.lower(), other.upper()); | 505 | 242k | (lower2 <= lower1 && lower1 <= upper2) | 506 | 179k | && (lower2 <= upper1 && upper1 <= upper2) | 507 | 242k | } |
<regex_syntax::hir::ClassUnicodeRange as regex_syntax::hir::interval::Interval>::is_subset Line | Count | Source | 502 | 81.3k | fn is_subset(&self, other: &Self) -> bool { | 503 | 81.3k | let (lower1, upper1) = (self.lower(), self.upper()); | 504 | 81.3k | let (lower2, upper2) = (other.lower(), other.upper()); | 505 | 81.3k | (lower2 <= lower1 && lower1 <= upper2) | 506 | 47.7k | && (lower2 <= upper1 && upper1 <= upper2) | 507 | 81.3k | } |
|
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 | 384 | fn min_value() -> Self { |
522 | 384 | u8::MIN |
523 | 384 | } |
524 | 393 | fn max_value() -> Self { |
525 | 393 | u8::MAX |
526 | 393 | } |
527 | 197M | fn as_u32(self) -> u32 { |
528 | 197M | u32::from(self) |
529 | 197M | } |
530 | 65.4k | fn increment(self) -> Self { |
531 | 65.4k | self.checked_add(1).unwrap() |
532 | 65.4k | } |
533 | 66.0k | fn decrement(self) -> Self { |
534 | 66.0k | self.checked_sub(1).unwrap() |
535 | 66.0k | } |
536 | | } |
537 | | |
538 | | impl Bound for char { |
539 | 245k | fn min_value() -> Self { |
540 | 245k | '\x00' |
541 | 245k | } |
542 | 245k | fn max_value() -> Self { |
543 | 245k | '\u{10FFFF}' |
544 | 245k | } |
545 | 185M | fn as_u32(self) -> u32 { |
546 | 185M | u32::from(self) |
547 | 185M | } |
548 | | |
549 | 460k | fn increment(self) -> Self { |
550 | 460k | match self { |
551 | 35 | '\u{D7FF}' => '\u{E000}', |
552 | 460k | c => char::from_u32(u32::from(c).checked_add(1).unwrap()).unwrap(), |
553 | | } |
554 | 460k | } |
555 | | |
556 | 460k | fn decrement(self) -> Self { |
557 | 460k | match self { |
558 | 0 | '\u{E000}' => '\u{D7FF}', |
559 | 460k | c => char::from_u32(u32::from(c).checked_sub(1).unwrap()).unwrap(), |
560 | | } |
561 | 460k | } |
562 | | } |
563 | | |
564 | | // Tests for interval sets are written in src/hir.rs against the public API. |