Clears the cache such that the backtracking engine can be executed on some input of fixed length.
(&mut self)
| 116 | /// Clears the cache such that the backtracking engine can be executed |
| 117 | /// on some input of fixed length. |
| 118 | fn clear(&mut self) { |
| 119 | // Reset the job memory so that we start fresh. |
| 120 | self.m.jobs.clear(); |
| 121 | |
| 122 | // Now we need to clear the bit state set. |
| 123 | // We do this by figuring out how much space we need to keep track |
| 124 | // of the states we've visited. |
| 125 | // Then we reset all existing allocated space to 0. |
| 126 | // Finally, we request more space if we need it. |
| 127 | // |
| 128 | // This is all a little circuitous, but doing this unsafely |
| 129 | // doesn't seem to have a measurable impact on performance. |
| 130 | // (Probably because backtracking is limited to such small |
| 131 | // inputs/regexes in the first place.) |
| 132 | let visited_len = |
| 133 | (self.prog.len() * (self.input.len() + 1) + BIT_SIZE - 1) |
| 134 | / |
| 135 | BIT_SIZE; |
| 136 | self.m.visited.truncate(visited_len); |
| 137 | for v in &mut self.m.visited { |
| 138 | *v = 0; |
| 139 | } |
| 140 | if visited_len > self.m.visited.len() { |
| 141 | let len = self.m.visited.len(); |
| 142 | self.m.visited.reserve_exact(visited_len - len); |
| 143 | for _ in 0..(visited_len - len) { |
| 144 | self.m.visited.push(0); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /// Start backtracking at the given position in the input, but also look |
| 150 | /// for literal prefixes. |