| 1 |
use std::error::Error; |
| 2 |
|
| 3 |
use regex_automata::{ |
| 4 |
hybrid::dfa::{OverlappingState, DFA}, |
| 5 |
nfa::thompson, |
| 6 |
HalfMatch, Input, MatchError, |
| 7 |
}; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#[test] |
| 25 |
#[cfg(target_pointer_width = "64")] |
| 26 |
#[cfg(not(miri))] |
| 27 |
fn too_many_cache_resets_cause_quit() -> Result<(), Box<dyn Error>> { |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
e |
| 34 |
. |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
. |
| 43 |
let pattern = r"[aβ]{99}"; |
| 44 |
let dfa = DFA::builder() |
| 45 |
.configure( |
| 46 |
|
| 47 |
|
| 48 |
DFA::config() |
| 49 |
.skip_cache_capacity_check(true) |
| 50 |
.cache_capacity(0) |
| 51 |
.minimum_cache_clear_count(Some(0)), |
| 52 |
) |
| 53 |
.thompson(thompson::NFA::config()) |
| 54 |
.build(pattern)?; |
| 55 |
let mut cache = dfa.create_cache(); |
| 56 |
|
| 57 |
let haystack = "a".repeat(101).into_bytes(); |
| 58 |
let err = MatchError::gave_up(25); |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
assert_eq!( |
| 63 |
Err(err.clone()), |
| 64 |
dfa.try_search_fwd(&mut cache, &Input::new(&haystack)) |
| 65 |
); |
| 66 |
assert_eq!( |
| 67 |
Err(err.clone()), |
| 68 |
dfa.try_search_overlapping_fwd( |
| 69 |
&mut cache, |
| 70 |
&Input::new(&haystack), |
| 71 |
&mut OverlappingState::start() |
| 72 |
), |
| 73 |
); |
| 74 |
|
| 75 |
let haystack = "β".repeat(101).into_bytes(); |
| 76 |
let err = MatchError::gave_up(2); |
| 77 |
assert_eq!( |
| 78 |
Err(err), |
| 79 |
dfa.try_search_fwd(&mut cache, &Input::new(&haystack)) |
| 80 |
); |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
cache.reset(&dfa); |
| 86 |
let err = MatchError::gave_up(27); |
| 87 |
assert_eq!( |
| 88 |
Err(err), |
| 89 |
dfa.try_search_fwd(&mut cache, &Input::new(&haystack)) |
| 90 |
); |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
let haystack = "a".repeat(101).into_bytes(); |
| 95 |
let err = MatchError::gave_up(13); |
| 96 |
assert_eq!( |
| 97 |
Err(err), |
| 98 |
dfa.try_search_fwd(&mut cache, &Input::new(&haystack)) |
| 99 |
); |
| 100 |
|
| 101 |
Ok(()) |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
#[test] |
| 106 |
fn quit_fwd() -> Result<(), Box<dyn Error>> { |
| 107 |
let dfa = DFA::builder() |
| 108 |
.configure(DFA::config().quit(b'x', true)) |
| 109 |
.build("[[:word:]]+$")?; |
| 110 |
let mut cache = dfa.create_cache(); |
| 111 |
|
| 112 |
assert_eq!( |
| 113 |
dfa.try_search_fwd(&mut cache, &Input::new("abcxyz")), |
| 114 |
Err(MatchError::quit(b'x', 3)), |
| 115 |
); |
| 116 |
assert_eq!( |
| 117 |
dfa.try_search_overlapping_fwd( |
| 118 |
&mut cache, |
| 119 |
&Input::new(b"abcxyz"), |
| 120 |
&mut OverlappingState::start() |
| 121 |
), |
| 122 |
Err(MatchError::quit(b'x', 3)), |
| 123 |
); |
| 124 |
|
| 125 |
Ok(()) |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
#[test] |
| 130 |
fn quit_rev() -> Result<(), Box<dyn Error>> { |
| 131 |
let dfa = DFA::builder() |
| 132 |
.configure(DFA::config().quit(b'x', true)) |
| 133 |
.thompson(thompson::Config::new().reverse(true)) |
| 134 |
.build("^[[:word:]]+")?; |
| 135 |
let mut cache = dfa.create_cache(); |
| 136 |
|
| 137 |
assert_eq!( |
| 138 |
dfa.try_search_rev(&mut cache, &Input::new("abcxyz")), |
| 139 |
Err(MatchError::quit(b'x', 3)), |
| 140 |
); |
| 141 |
|
| 142 |
Ok(()) |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
#[test] |
| 149 |
#[should_panic] |
| 150 |
fn quit_panics() { |
| 151 |
DFA::config().unicode_word_boundary(true).quit(b'\xFF', false); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
#[test] |
| 158 |
fn unicode_word_implicitly_works() -> Result<(), Box<dyn Error>> { |
| 159 |
let mut config = DFA::config(); |
| 160 |
for b in 0x80..=0xFF { |
| 161 |
config = config.quit(b, true); |
| 162 |
} |
| 163 |
let dfa = DFA::builder().configure(config).build(r"\b")?; |
| 164 |
let mut cache = dfa.create_cache(); |
| 165 |
let expected = HalfMatch::must(0, 1); |
| 166 |
assert_eq!( |
| 167 |
Ok(Some(expected)), |
| 168 |
dfa.try_search_fwd(&mut cache, &Input::new(" a")), |
| 169 |
); |
| 170 |
Ok(()) |
| 171 |
} |