| 1 |
use fancy_regex::{BytesMode, Error, RegexBuilder, RegexInput, RuntimeError}; |
| 2 |
|
| 3 |
mod common; |
| 4 |
|
| 5 |
#[test] |
| 6 |
fn control_character_escapes() { |
| 7 |
common::assert_is_match(r"\a", "\x07"); |
| 8 |
common::assert_is_match(r"\e", "\x1B"); |
| 9 |
common::assert_is_match(r"\f", "\x0C"); |
| 10 |
common::assert_is_match(r"\n", "\x0A"); |
| 11 |
common::assert_is_match(r"\r", "\x0D"); |
| 12 |
common::assert_is_match(r"\t", "\x09"); |
| 13 |
common::assert_is_match(r"\v", "\x0B"); |
| 14 |
} |
| 15 |
|
| 16 |
#[test] |
| 17 |
fn character_class_escapes() { |
| 18 |
common::assert_is_match(r"[\[]", "["); |
| 19 |
common::assert_is_match(r"[\^]", "^"); |
| 20 |
|
| 21 |
// The regex crate would reject the following because it's not necessary to escape them. |
| 22 |
// Other engines allow to escape any non-alphanumeric character. |
| 23 |
common::assert_is_match(r"[\<]", "<"); |
| 24 |
common::assert_is_match(r"[\>]", ">"); |
| 25 |
assert_match(r"[\.]", "."); |
| 26 |
common::assert_is_match(r"[\ ]", " "); |
| 27 |
|
| 28 |
// Character class escape |
| 29 |
common::assert_is_match(r"[\d]", "1"); |
| 30 |
|
| 31 |
// Control characters |
| 32 |
common::assert_is_match(r"[\e]", "\x1B"); |
| 33 |
common::assert_is_match(r"[\n]", "\x0A"); |
| 34 |
|
| 35 |
// `]` can be unescaped if it's right after `[` |
| 36 |
common::assert_is_match(r"[]]", "]"); |
| 37 |
// `]` can be unescaped even after `[^` |
| 38 |
common::assert_is_match(r"[^]]", "a"); |
| 39 |
} |
| 40 |
|
| 41 |
#[test] |
| 42 |
fn character_class_nested() { |
| 43 |
assert_match(r"[[a][bc]]", "c"); |
| 44 |
assert_match(r"[a[^b]]", "c"); |
| 45 |
} |
| 46 |
|
| 47 |
#[test] |
| 48 |
fn character_class_intersection() { |
| 49 |
let pattern = r"[\w&&a-c]"; |
| 50 |
assert_match(pattern, "c"); |
| 51 |
assert_no_match(pattern, "d"); |
| 52 |
|
| 53 |
let pattern = r"[[0-9]&&[^4]]"; |
| 54 |
assert_match(pattern, "1"); |
| 55 |
assert_no_match(pattern, "4"); |
| 56 |
|
| 57 |
// Test nested classes with [^]...] pattern where the first ] is a literal character |
| 58 |
let pattern = r"[[a]&&[^]b]]"; |
| 59 |
assert_match(pattern, "a"); |
| 60 |
assert_no_match(pattern, "]"); |
| 61 |
assert_no_match(pattern, "b"); |
| 62 |
|
| 63 |
// Test with unicode properties and intersection |
| 64 |
let pattern = r#"[[\p{L}]&&[^]abc]]"#; |
| 65 |
assert_match(pattern, "x"); |
| 66 |
assert_no_match(pattern, "]"); |
| 67 |
assert_no_match(pattern, "a"); |
| 68 |
|
| 69 |
let pattern = r#"[[\p{S}\p{P}]&&[^]"'(),;\[_`{}]]"#; |
| 70 |
assert_match(pattern, "!"); |
| 71 |
assert_no_match(pattern, "]"); |
| 72 |
assert_no_match(pattern, ","); |
| 73 |
} |
| 74 |
|
| 75 |
#[test] |
| 76 |
fn alternation_with_empty_arm() { |
| 77 |
common::assert_is_match(r"^(a|)$", "a"); |
| 78 |
common::assert_is_match(r"^(a|)$", ""); |
| 79 |
common::assert_is_match(r"^(|a)$", "a"); |
| 80 |
common::assert_is_match(r"^(|a)$", ""); |
| 81 |
common::assert_is_match(r"a|", "a"); |
| 82 |
common::assert_is_match(r"a|", ""); |
| 83 |
common::assert_is_match(r"|a", "a"); |
| 84 |
common::assert_is_match(r"|a", ""); |
| 85 |
common::assert_no_match(r"^(a|)$", "b"); |
| 86 |
} |
| 87 |
|
| 88 |
#[test] |
| 89 |
fn nested_repeats_match_same_language() { |
| 90 |
fn assert_oniguruma_is_match(pattern: &str, text: &str) { |
| 91 |
let re = RegexBuilder::new(pattern) |
| 92 |
.oniguruma_mode(true) |
| 93 |
.build() |
| 94 |
.unwrap(); |
| 95 |
assert!(re.is_match(text).unwrap()); |
| 96 |
} |
| 97 |
|
| 98 |
fn assert_oniguruma_no_match(pattern: &str, text: &str) { |
| 99 |
let re = RegexBuilder::new(pattern) |
| 100 |
.oniguruma_mode(true) |
| 101 |
.build() |
| 102 |
.unwrap(); |
| 103 |
assert!(!re.is_match(text).unwrap()); |
| 104 |
} |
| 105 |
|
| 106 |
assert_oniguruma_is_match(r"^(x+){1,}$", "xxx"); |
| 107 |
assert_oniguruma_no_match(r"^(x+){1,}$", "xxy"); |
| 108 |
assert_oniguruma_is_match(r"^(x*){1,}$", ""); |
| 109 |
assert_oniguruma_is_match(r"^(x*){1,}$", "xxx"); |
| 110 |
assert_oniguruma_is_match(r"^(x?){1,}$", ""); |
| 111 |
assert_oniguruma_is_match(r"^(x?){1,}$", "xxx"); |
| 112 |
} |
| 113 |
|
| 114 |
#[test] |
| 115 |
fn case_insensitive_character_class() { |
| 116 |
common::assert_is_match(r"^(?i)[a-z]+$", "aB"); |
| 117 |
} |
| 118 |
|
| 119 |
#[test] |
| 120 |
fn case_insensitive_escape() { |
| 121 |
// `\x61` is lowercase `a` |
| 122 |
assert_match(r"(?i)\x61", "A"); |
| 123 |
|
| 124 |
// `\p{Ll}` is the "Letter, lowercase" category |
| 125 |
assert_match(r"(?i)\p{Ll}", "A"); |
| 126 |
} |
| 127 |
|
| 128 |
#[test] |
| 129 |
fn atomic_group() { |
| 130 |
common::assert_is_match(r"^a(?>bc|b)c$", "abcc"); |
| 131 |
common::assert_no_match(r"^a(?>bc|b)c$", "abc"); |
| 132 |
|
| 133 |
// Look-ahead forces use of VM |
| 134 |
common::assert_is_match(r"^a(bc(?=d)|b)cd$", "abcd"); |
| 135 |
common::assert_no_match(r"^a(?>bc(?=d)|b)cd$", "abcd"); |
| 136 |
} |
| 137 |
|
| 138 |
#[test] |
| 139 |
fn is_match_input_respects_range_without_slicing() { |
| 140 |
let re = RegexBuilder::new(r"\bat\b").build().unwrap(); |
| 141 |
assert!(!re |
| 142 |
.is_match_input(RegexInput::new("batter").range(1..3)) |
| 143 |
.unwrap()); |
| 144 |
|
| 145 |
let re = RegexBuilder::new(r"(?=(foo))\1(?=bar)").build().unwrap(); |
| 146 |
assert!(re |
| 147 |
.is_match_input(RegexInput::new("foobar").range(0..3)) |
| 148 |
.unwrap()); |
| 149 |
} |
| 150 |
|
| 151 |
#[test] |
| 152 |
fn backtrack_limit_hit_when_not_seeking() { |
| 153 |
// Disable the seek pre-filter so the backtracking VM actually explores all positions and |
| 154 |
// hits the limit. With seek enabled the engine would correctly determine there is no `c` |
| 155 |
// in the haystack and return `None` immediately without backtracking. |
| 156 |
let re = RegexBuilder::new(r"(?i)(a|b|ab)*(?>c)") |
| 157 |
.backtrack_limit(100_000) |
| 158 |
.seek(false) |
| 159 |
.build() |
| 160 |
.expect("regex to compile successfully"); |
| 161 |
let s = "abababababababababababababababababababababababababababab"; |
| 162 |
let result = re.is_match(s); |
| 163 |
assert!(result.is_err()); |
| 164 |
match result.err() { |
| 165 |
Some(Error::RuntimeError(RuntimeError::BacktrackLimitExceeded)) => {} |
| 166 |
_ => panic!("Expected RuntimeError::BacktrackLimitExceeded"), |
| 167 |
} |
| 168 |
|
| 169 |
// The same behaviour must hold in bytes mode. |
| 170 |
let re = RegexBuilder::new(r"(?i)(a|b|ab)*(?>c)") |
| 171 |
.bytes_mode(BytesMode::Ascii) |
| 172 |
.backtrack_limit(100_000) |
| 173 |
.build() |
| 174 |
.expect("regex to compile successfully"); |
| 175 |
let s = b"abababababababababababababababababababababababababababab"; |
| 176 |
let result = re.is_match(s); |
| 177 |
assert!(result.is_err()); |
| 178 |
match result.err() { |
| 179 |
Some(Error::RuntimeError(RuntimeError::BacktrackLimitExceeded)) => {} |
| 180 |
_ => panic!("Expected RuntimeError::BacktrackLimitExceeded"), |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
#[test] |
| 185 |
fn backtrack_limit_not_applicable_when_seeking() { |
| 186 |
// Enable the seek pre-filter so the backtracking VM would only explore all positions |
| 187 |
// which could match. With seek enabled the engine will correctly determine there is no `c` |
| 188 |
// in the haystack and return `None` immediately without backtracking. |
| 189 |
let re = RegexBuilder::new(r"(?i)(a|b|ab)*(?>c)") |
| 190 |
.backtrack_limit(1) |
| 191 |
.seek(true) |
| 192 |
.build() |
| 193 |
.expect("regex to compile successfully"); |
| 194 |
let s = "abababababababababababababababababababababababababababab"; |
| 195 |
let result = re.is_match(s); |
| 196 |
assert!(result.is_ok()); |
| 197 |
assert!(!result.unwrap()); |
| 198 |
} |
| 199 |
|
| 200 |
#[test] |
| 201 |
fn can_find_matches_when_seeking() { |
| 202 |
let re = RegexBuilder::new(r"(abc|def)\1") |
| 203 |
.seek(true) |
| 204 |
.build() |
| 205 |
.expect("regex to compile successfully"); |
| 206 |
let s = "abcdefabcdefabcdefdef"; |
| 207 |
let result = re.is_match(s); |
| 208 |
assert!(result.is_ok()); |
| 209 |
assert!(result.unwrap()); |
| 210 |
} |
| 211 |
|
| 212 |
#[test] |
| 213 |
fn end_of_hard_expression_cannot_be_delegated() { |
| 214 |
common::assert_is_match(r"(?!x)(?:a|ab)c", "abc"); |
| 215 |
// If `(?:a|ab)` is delegated, there's no backtracking and `a` matches and `ab` is never tried. |
| 216 |
common::assert_is_match(r"((?!x)(?:a|ab))c", "abc"); |
| 217 |
} |
| 218 |
|
| 219 |
#[test] |
| 220 |
fn issue103() { |
| 221 |
assert_no_match(r"(([ab]+)\1b)", "babab"); |
| 222 |
} |
| 223 |
|
| 224 |
#[test] |
| 225 |
fn backreference_validity_checker() { |
| 226 |
common::assert_is_match(r"(a)(?(1))", "a"); |
| 227 |
common::assert_is_match(r"(?<group1>a)(?('group1'))b", "ab"); |
| 228 |
common::assert_is_match(r"(a)(b)?(?(2))", "ab"); |
| 229 |
common::assert_no_match(r"(a)(b)?(?(2))", "a"); |
| 230 |
common::assert_is_match(r"(a)(b?)(?(2))", "a"); |
| 231 |
} |
| 232 |
|
| 233 |
#[test] |
| 234 |
fn conditional_with_backref_validity() { |
| 235 |
assert_match(r"(a)?b(?(1)c|d)", "bd"); |
| 236 |
assert_match(r"(a)?b(?(1)c|d)", "abc"); |
| 237 |
assert_match(r"(?<group1>a)?b(?(<group1>)c|d)", "abc"); |
| 238 |
assert_no_match(r"(a)?b(?(1)c|d)", "bc"); |
| 239 |
assert_no_match(r"^(a)?b(?(1)c|d)$", "abd"); |
| 240 |
} |
| 241 |
|
| 242 |
#[test] |
| 243 |
fn conditional_with_consuming_condition() { |
| 244 |
assert_match(r"^(?(ab)c|d)$", "abc"); |
| 245 |
assert_no_match(r"^(?(ab)c|d)$", "abd"); |
| 246 |
assert_no_match(r"^(?(ab)c|d)$", "ad"); |
| 247 |
|
| 248 |
assert_match(r"^(?(\d)abc|\d!)$", "5abc"); |
| 249 |
assert_no_match(r"^(?(\d)abc|\d!)$", "5!"); |
| 250 |
} |
| 251 |
|
| 252 |
#[test] |
| 253 |
fn conditional_with_lookaround_condition() { |
| 254 |
assert_match(r"^(?((?=\d))\w+|!)$", "!"); |
| 255 |
assert_match(r"^(?((?=\d))\w+|!)$", "5abc"); |
| 256 |
assert_match(r"^(?((?=\d))abc)$", ""); |
| 257 |
assert_match(r"^(?((?=\w))abc)$", "abc"); |
| 258 |
assert_no_match(r"^(?((?=\d))\wabc|\d!)$", "5!"); |
| 259 |
} |
| 260 |
|
| 261 |
#[test] |
| 262 |
fn backrefs() { |
| 263 |
common::assert_is_match(r"(abc)\1", "abcabc"); |
| 264 |
common::assert_is_match(r"(abc|def)\1", "abcabc"); |
| 265 |
common::assert_no_match(r"(abc|def)\1", "abcdef"); |
| 266 |
common::assert_is_match(r"(abc|def)\1", "defdef"); |
| 267 |
|
| 268 |
common::assert_no_match(r"(abc|def)\1", "abcABC"); |
| 269 |
common::assert_is_match(r"(abc|def)(?i:\1)", "abcABC"); |
| 270 |
common::assert_is_match(r"(abc|def)(?i:\1)", "abcAbc"); |
| 271 |
common::assert_no_match(r"(abc|def)(?i:\1)", "abcAB"); |
| 272 |
common::assert_no_match(r"(abc|def)(?i:\1)", "abcdef"); |
| 273 |
|
| 274 |
assert_match(r"(δ)(?i:\1)", "δΔ"); |
| 275 |
assert_no_match(r"(δ)\1", "δΔ"); |
| 276 |
assert_no_match(r"(δδ)\1", "δΔfoo"); |
| 277 |
assert_no_match(r"(δδ)\1", "δΔ"); |
| 278 |
|
| 279 |
common::assert_is_match(r"(.)(?i:\1)", "\\\\"); |
| 280 |
common::assert_is_match(r"(.)(?i:\1)", "(("); |
| 281 |
|
| 282 |
assert_match(r"(.)(?i:\1)", "įĮ"); |
| 283 |
assert_no_match(r"(.)(?i:\1)", "įi"); |
| 284 |
assert_no_match(r"(.)(?i:\1)", "įĖ"); |
| 285 |
|
| 286 |
assert_match(r"(?i)(?<word>\w+)\s+\k<word>", "Greek : δ Δ"); |
| 287 |
} |
| 288 |
|
| 289 |
#[test] |
| 290 |
fn backrefs_casei_unicode_fold_orbits() { |
| 291 |
// Simple case folding relates all of σ/ς/Σ; same UTF-8 length, so the |
| 292 |
// backref window lines up and these match. |
| 293 |
assert_match(r"(σ)(?i:\1)", "σς"); |
| 294 |
assert_match(r"(Σ)(?i:\1)", "Σς"); |
| 295 |
// Simple folding is not full folding: ß does not fold to ss. |
| 296 |
assert_no_match(r"(ß)(?i:\1)", "ßss"); |
| 297 |
// The backref must match the folded text exactly; a folded prefix of the |
| 298 |
// window (previously accepted via a substring search) is not a match. |
| 299 |
assert_no_match(r"(ſa)(?i:\1)", "ſasab"); |
| 300 |
// Fold orbits whose members differ in UTF-8 length (ſ U+017F vs s, |
| 301 |
// K U+212A vs k, Å U+212B vs å) cannot match through a backref because |
| 302 |
// the window is sized in bytes from the captured text. |
| 303 |
assert_no_match(r"(å)(?i:\1)", "å\u{212B}"); |
| 304 |
assert_no_match(r"(s)(?i:\1)", "sſ"); |
| 305 |
} |
| 306 |
|
| 307 |
#[test] |
| 308 |
fn easy_trailing_positive_lookaheads() { |
| 309 |
common::assert_is_match(r"(?=c)", "abcabc"); |
| 310 |
common::assert_is_match(r"abc(?=abc)", "abcabc"); |
| 311 |
common::assert_no_match(r"abc(?=abc)", "abcdef"); |
| 312 |
common::assert_is_match(r"abc(?=a|b)", "abcabc"); |
| 313 |
common::assert_no_match(r"abc(?=a|f)", "f"); |
| 314 |
} |
| 315 |
|
| 316 |
#[test] |
| 317 |
fn hard_trailing_positive_lookaheads() { |
| 318 |
common::assert_is_match(r"(abc|def)(?=\1)", "defdef"); |
| 319 |
common::assert_is_match(r"(abc|def)(?=a(?!b))", "abca"); |
| 320 |
common::assert_is_match(r"(abc|def)(?=a(?!b))", "abcaa"); |
| 321 |
common::assert_no_match(r"(abc|def)(?=a(?!b))", "abcabc"); |
| 322 |
} |
| 323 |
|
| 324 |
#[test] |
| 325 |
fn lookbehind_with_emoji_in_char_class() { |
| 326 |
// the Delegate Expr which gets parsed is analyzed as having min_size 1. |
| 327 |
// Emojis typically consist of multiple bytes, but the lookbehind |
| 328 |
// still behaves correctly |
| 329 |
assert_match(r"(?<=[🎯])\s+z", "🎯 z"); |
| 330 |
} |
| 331 |
|
| 332 |
#[test] |
| 333 |
fn word_boundary_brace_syntax() { |
| 334 |
// \b{start} |
| 335 |
assert_match(r"\b{start}world", "hello world"); |
| 336 |
assert_no_match(r"\b{start}world", "helloworld"); |
| 337 |
assert_match(r"\b{start}test", "test case"); |
| 338 |
assert_no_match(r"\b{start}test", "contest"); |
| 339 |
assert_no_match(r"\b{start}-test", "run --test"); |
| 340 |
|
| 341 |
// \b{start-half} |
| 342 |
assert_match(r"\b{start-half}world", "hello world"); |
| 343 |
assert_no_match(r"\b{start-half}world", "helloworld"); |
| 344 |
assert_match(r"\b{start-half}test", "test case"); |
| 345 |
assert_no_match(r"\b{start-half}test", "contest"); |
| 346 |
assert_match(r"\b{start-half}-test", "run --test"); // different from \b{start} |
| 347 |
|
| 348 |
// \b{end} |
| 349 |
assert_match(r"world\b{end}", "hello world"); |
| 350 |
assert_no_match(r"world\b{end}", "worldhello"); |
| 351 |
assert_match(r"test\b{end}", "run test"); |
| 352 |
assert_no_match(r"test\b{end}", "testing"); |
| 353 |
assert_no_match(r"-1-\b{end}", "chapter -1-"); |
| 354 |
|
| 355 |
// \b{end-half} |
| 356 |
assert_match(r"world\b{end-half}", "hello world"); |
| 357 |
assert_no_match(r"world\b{end-half}", "worldhello"); |
| 358 |
assert_match(r"test\b{end-half}", "run test"); |
| 359 |
assert_no_match(r"test\b{end-half}", "testing"); |
| 360 |
assert_match(r"-1-\b{end-half}", "chapter -1-"); |
| 361 |
|
| 362 |
// repetition |
| 363 |
assert_match(r"\b\bworld", "hello world"); |
| 364 |
assert_match(r"\b{2}world", "hello world"); |
| 365 |
assert_no_match(r"\b{2}orld", "hello world"); |
| 366 |
assert_match(r"world\b{,3}", "hello world"); |
| 367 |
assert_match(r"\B{5}est", "run test"); |
| 368 |
assert_no_match(r"\B{2}test", "run test"); |
| 369 |
|
| 370 |
// `\b{start}` <=> `\<` and `\b{end}` <=> `\>` |
| 371 |
let test_cases = [ |
| 372 |
("hello world", r"\b{start}world", r"\<world"), |
| 373 |
("hello world", r"world\b{end}", r"world\>"), |
| 374 |
("hello world there", r"\b{start}world\b{end}", r"\<world\>"), |
| 375 |
]; |
| 376 |
|
| 377 |
for (text, b_pattern, angle_pattern) in test_cases { |
| 378 |
let b_result = match_text(b_pattern, text); |
| 379 |
let angle_result = match_text(angle_pattern, text); |
| 380 |
assert_eq!( |
| 381 |
b_result, angle_result, |
| 382 |
"Pattern '{}' and '{}' should match '{}' equivalently", |
| 383 |
b_pattern, angle_pattern, text |
| 384 |
); |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
#[test] |
| 389 |
fn general_newline_escape() { |
| 390 |
// Test \R matching \r\n (two characters) |
| 391 |
common::assert_is_match(r"\R", "\r\n"); |
| 392 |
|
| 393 |
// Test \R matching single newline characters |
| 394 |
common::assert_is_match(r"\R", "\n"); |
| 395 |
common::assert_is_match(r"\R", "\r"); |
| 396 |
common::assert_is_match(r"\R", "\x0B"); // \v |
| 397 |
common::assert_is_match(r"\R", "\x0C"); // \f |
| 398 |
|
| 399 |
// Test \R in patterns |
| 400 |
common::assert_is_match(r"a\Rb", "a\r\nb"); |
| 401 |
common::assert_is_match(r"a\Rb", "a\nb"); |
| 402 |
common::assert_is_match(r"a\Rb", "a\rb"); |
| 403 |
|
| 404 |
// Test that \R doesn't backtrack from \r\n to \r |
| 405 |
// If we have \r\n followed by something that must match \n, |
| 406 |
// \R should match \r\n as a unit and fail, not backtrack to match just \r |
| 407 |
common::assert_no_match(r"\R\n", "\r\n"); |
| 408 |
|
| 409 |
// Test multiple \R in a pattern |
| 410 |
common::assert_is_match(r"\R\R", "\n\r"); |
| 411 |
common::assert_is_match(r"\R\R", "\r\n\n"); |
| 412 |
|
| 413 |
// Test \R doesn't match other characters |
| 414 |
common::assert_no_match(r"^\R$", "a"); |
| 415 |
common::assert_no_match(r"^\R$", " "); |
| 416 |
common::assert_no_match(r"^\R$", ""); |
| 417 |
} |
| 418 |
|
| 419 |
#[test] |
| 420 |
fn general_newline_escape_unicode() { |
| 421 |
// Test Unicode newline characters (U+0085, U+2028, U+2029) |
| 422 |
assert_match(r"\R", "\u{0085}"); // NEL (Next Line) |
| 423 |
assert_match(r"\R", "\u{2028}"); // LS (Line Separator) |
| 424 |
assert_match(r"\R", "\u{2029}"); // PS (Paragraph Separator) |
| 425 |
|
| 426 |
// Test in patterns |
| 427 |
assert_match(r"a\Rb", "a\u{0085}b"); |
| 428 |
assert_match(r"a\Rb", "a\u{2028}b"); |
| 429 |
} |
| 430 |
|
| 431 |
#[test] |
| 432 |
fn general_newline_not_in_character_class() { |
| 433 |
// \R inside character class should be treated as literal R |
| 434 |
assert_match(r"[\R]", "R"); |
| 435 |
assert_no_match(r"[\R]", "\n"); |
| 436 |
assert_no_match(r"[\R]", "\r\n"); |
| 437 |
} |
| 438 |
|
| 439 |
#[test] |
| 440 |
fn crlf_flag_multiline() { |
| 441 |
// (?mR) treats \r\n as a single line ending for ^ and $ |
| 442 |
let pattern = r"(?mR)^test$"; |
| 443 |
assert_match(pattern, "test"); |
| 444 |
assert_match(pattern, "\r\ntest\r\n"); |
| 445 |
assert_match(pattern, "test\r\n"); |
| 446 |
assert_match(pattern, "\r\ntest"); |
| 447 |
// In CRLF mode bare \r is also treated as a line ending |
| 448 |
assert_match(pattern, "\rtest\r"); |
| 449 |
// ^ should not match between \r and \n (they form a single unit) |
| 450 |
assert_no_match(r"(?mR)^\ntest$", "\r\ntest"); |
| 451 |
// In non-CRLF multiline mode, \r is not a line ending |
| 452 |
assert_no_match(r"(?m)^test$", "\rtest\r"); |
| 453 |
// Works with a lookahead (hard/fancy regex) |
| 454 |
assert_match(r"(?mR)(?=^test$)^\w+$", "\r\ntest\r\n"); |
| 455 |
} |
| 456 |
|
| 457 |
#[test] |
| 458 |
fn crlf_flag_dot() { |
| 459 |
// In CRLF mode, `.` does not match `\r` or `\n` |
| 460 |
assert_no_match(r"(?R).", "\r"); |
| 461 |
assert_no_match(r"(?R).", "\n"); |
| 462 |
assert_no_match(r"(?R).", "\r\n"); |
| 463 |
// But it still matches other characters |
| 464 |
assert_match(r"(?R).", "a"); |
| 465 |
assert_match(r"(?R).", " "); |
| 466 |
// Same via the backtracking VM (hard/fancy path) |
| 467 |
assert_no_match(r"(?R)(?=).", "\r"); |
| 468 |
assert_no_match(r"(?R)(?=).", "\n"); |
| 469 |
// Without CRLF mode, `.` matches `\r` but not `\n` |
| 470 |
assert_match(r".", "\r"); |
| 471 |
assert_no_match(r".", "\n"); |
| 472 |
} |
| 473 |
|
| 474 |
#[test] |
| 475 |
fn crlf_flag_oniguruma_any_char() { |
| 476 |
// Regardless of mode, \O always matches any character |
| 477 |
// including newline and carriage return |
| 478 |
let test_pattern = |pattern: &str| { |
| 479 |
assert_match(pattern, "\r"); |
| 480 |
assert_match(pattern, "\n"); |
| 481 |
assert_match(pattern, "\t"); |
| 482 |
assert_match(pattern, "╔"); |
| 483 |
assert_match(pattern, "a"); |
| 484 |
assert_match(pattern, "š"); |
| 485 |
assert_no_match(pattern, ""); |
| 486 |
}; |
| 487 |
|
| 488 |
test_pattern(r"(?R)\O"); |
| 489 |
test_pattern(r"\O"); |
| 490 |
} |
| 491 |
|
| 492 |
#[cfg_attr(feature = "track_caller", track_caller)] |
| 493 |
fn assert_match(re: &str, text: &str) { |
| 494 |
let result = match_text(re, text); |
| 495 |
assert_eq!( |
| 496 |
result, true, |
| 497 |
"Expected regex '{}' to match text '{}'", |
| 498 |
re, text |
| 499 |
); |
| 500 |
} |
| 501 |
|
| 502 |
#[cfg_attr(feature = "track_caller", track_caller)] |
| 503 |
fn assert_no_match(re: &str, text: &str) { |
| 504 |
let result = match_text(re, text); |
| 505 |
assert_eq!( |
| 506 |
result, false, |
| 507 |
"Expected regex '{}' to not match text '{}'", |
| 508 |
re, text |
| 509 |
); |
| 510 |
} |
| 511 |
|
| 512 |
#[test] |
| 513 |
fn unicode_property_remapping_outside_char_class() { |
| 514 |
// Test \p{alnum} - should match alphanumeric characters |
| 515 |
assert_match(r"\p{alnum}", "a"); |
| 516 |
assert_match(r"\p{alnum}", "1"); |
| 517 |
assert_match(r"\p{alnum}", "š"); |
| 518 |
assert_no_match(r"\p{alnum}", " "); |
| 519 |
assert_no_match(r"\p{alnum}", "_"); |
| 520 |
assert_no_match(r"\p{alnum}", "-"); |
| 521 |
|
| 522 |
// Test \p{blank} - should match space and tab |
| 523 |
assert_match(r"\p{blank}", " "); |
| 524 |
assert_match(r"\p{blank}", "\t"); |
| 525 |
assert_no_match(r"\p{blank}", "a"); |
| 526 |
assert_no_match(r"\p{blank}", "\n"); |
| 527 |
assert_no_match(r"\p{blank}", "1"); |
| 528 |
assert_no_match(r"\p{blank}", "_"); |
| 529 |
assert_no_match(r"\p{blank}", "-"); |
| 530 |
|
| 531 |
// Test \p{word} - should match word characters |
| 532 |
assert_match(r"\p{word}", "a"); |
| 533 |
assert_match(r"\p{word}", "_"); |
| 534 |
assert_match(r"\p{alnum}", "1"); |
| 535 |
assert_match(r"\p{alnum}", "š"); |
| 536 |
assert_no_match(r"\p{word}", " "); |
| 537 |
assert_no_match(r"\p{word}", "-"); |
| 538 |
|
| 539 |
// Test non-remapped properties still work. L matches letters if unnegated. |
| 540 |
assert_match(r"\p{L}", "a"); |
| 541 |
assert_match(r"\p{L}", "š"); |
| 542 |
assert_no_match(r"\p{L}", "1"); |
| 543 |
assert_no_match(r"\p{L}", "_"); |
| 544 |
assert_no_match(r"\p{L}", "-"); |
| 545 |
assert_no_match(r"\p{L}", " "); |
| 546 |
|
| 547 |
assert_no_match(r"\P{L}", "a"); |
| 548 |
assert_no_match(r"\P{L}", "š"); |
| 549 |
assert_match(r"\P{L}", "1"); |
| 550 |
assert_match(r"\P{L}", " "); |
| 551 |
assert_match(r"\P{L}", "-"); |
| 552 |
|
| 553 |
// Test \p{cntrl} - control characters (U+0000-U+001F, U+007F-U+009F) |
| 554 |
assert_match(r"\p{cntrl}", "\x00"); // NULL |
| 555 |
assert_match(r"\p{cntrl}", "\x1F"); // Unit Separator |
| 556 |
assert_match(r"\p{cntrl}", "\x7F"); // DEL |
| 557 |
assert_match(r"\p{cntrl}", "\u{0080}"); // Control character |
| 558 |
assert_match(r"\p{cntrl}", "\u{009F}"); // Control character |
| 559 |
assert_no_match(r"\p{cntrl}", "a"); |
| 560 |
assert_no_match(r"\p{cntrl}", " "); |
| 561 |
assert_no_match(r"\p{cntrl}", "0"); |
| 562 |
assert_no_match(r"\p{cntrl}", "\u{00A0}"); // Non-breaking space (not control) |
| 563 |
|
| 564 |
// Test \P{cntrl} - non-control characters |
| 565 |
assert_no_match(r"\P{cntrl}", "\x00"); |
| 566 |
assert_no_match(r"\P{cntrl}", "\x1F"); |
| 567 |
assert_no_match(r"\P{cntrl}", "\x7F"); |
| 568 |
assert_match(r"\P{cntrl}", "a"); |
| 569 |
assert_match(r"\P{cntrl}", " "); |
| 570 |
assert_match(r"\P{cntrl}", "0"); |
| 571 |
} |
| 572 |
|
| 573 |
#[test] |
| 574 |
fn unicode_property_remapping_inside_char_class() { |
| 575 |
// Test \p{alnum} - should match alphanumeric characters |
| 576 |
let alnum_pattern = r"[\p{alnum}]"; |
| 577 |
assert_match(alnum_pattern, "a"); |
| 578 |
assert_match(alnum_pattern, "1"); |
| 579 |
assert_match(alnum_pattern, "š"); |
| 580 |
assert_no_match(alnum_pattern, " "); |
| 581 |
assert_no_match(alnum_pattern, "]"); |
| 582 |
|
| 583 |
// Test \p{blank} - should match space and tab |
| 584 |
let blank_pattern = r"[\p{blank}]"; |
| 585 |
assert_match(blank_pattern, " "); |
| 586 |
assert_match(blank_pattern, "\t"); |
| 587 |
assert_no_match(blank_pattern, "a"); |
| 588 |
assert_no_match(blank_pattern, "\n"); |
| 589 |
assert_no_match(blank_pattern, "]"); |
| 590 |
|
| 591 |
let bang_dash_hash_blank_pattern = r"[!-#\p{blank}]"; |
| 592 |
assert_match(bang_dash_hash_blank_pattern, "!"); |
| 593 |
assert_match(bang_dash_hash_blank_pattern, " "); |
| 594 |
assert_match(bang_dash_hash_blank_pattern, "\t"); |
| 595 |
assert_no_match(bang_dash_hash_blank_pattern, "a"); |
| 596 |
|
| 597 |
// Test \p{word} - should match word characters |
| 598 |
let word_pattern = r"[\p{word}]"; |
| 599 |
assert_match(word_pattern, "a"); |
| 600 |
assert_match(word_pattern, "_"); |
| 601 |
assert_no_match(word_pattern, " "); |
| 602 |
assert_no_match(word_pattern, "-"); |
| 603 |
assert_no_match(word_pattern, "]"); |
| 604 |
|
| 605 |
// Test non-remapped properties still work. L matches letters if unnegated. |
| 606 |
let l_pattern = r"[\p{L}]"; |
| 607 |
assert_match(l_pattern, "a"); |
| 608 |
assert_no_match(l_pattern, "1"); |
| 609 |
assert_no_match(l_pattern, " "); |
| 610 |
|
| 611 |
let not_l_pattern = r"[\P{L}]"; |
| 612 |
assert_no_match(not_l_pattern, "a"); |
| 613 |
assert_match(not_l_pattern, "1"); |
| 614 |
assert_match(not_l_pattern, " "); |
| 615 |
|
| 616 |
let slash_not_alnum_pattern = r"[/\P{alnum}]"; |
| 617 |
assert_no_match(slash_not_alnum_pattern, "a"); |
| 618 |
assert_match(slash_not_alnum_pattern, "/"); |
| 619 |
|
| 620 |
// Test \p{cntrl} |
| 621 |
let cntrl_pattern = r"[\p{cntrl}]"; |
| 622 |
assert_match(cntrl_pattern, "\x00"); |
| 623 |
assert_match(cntrl_pattern, "\x7F"); |
| 624 |
assert_no_match(cntrl_pattern, "a"); |
| 625 |
|
| 626 |
let not_cntrl_pattern = r"[\P{cntrl}]"; |
| 627 |
assert_match(not_cntrl_pattern, "a"); |
| 628 |
assert_no_match(not_cntrl_pattern, "\x00"); |
| 629 |
} |
| 630 |
|
| 631 |
#[test] |
| 632 |
fn char_class_negated_property_as_only_item() { |
| 633 |
// Test negated properties as the first (and only) item in a char class |
| 634 |
|
| 635 |
// \P{alnum} as only content - should match non-alphanumeric characters |
| 636 |
let not_alnum_pattern = r"[\P{alnum}]"; |
| 637 |
assert_no_match(not_alnum_pattern, "a"); |
| 638 |
assert_no_match(not_alnum_pattern, "Z"); |
| 639 |
assert_no_match(not_alnum_pattern, "š"); |
| 640 |
assert_no_match(not_alnum_pattern, "0"); |
| 641 |
assert_no_match(not_alnum_pattern, "9"); |
| 642 |
assert_match(not_alnum_pattern, " "); |
| 643 |
assert_match(not_alnum_pattern, "!"); |
| 644 |
assert_match(not_alnum_pattern, "-"); |
| 645 |
assert_match(not_alnum_pattern, "_"); |
| 646 |
|
| 647 |
// \P{blank} as only content - should match non-blank characters |
| 648 |
let not_blank_pattern = r"[\P{blank}]"; |
| 649 |
assert_match(not_blank_pattern, "a"); |
| 650 |
assert_match(not_blank_pattern, "!"); |
| 651 |
assert_no_match(not_blank_pattern, " "); |
| 652 |
assert_no_match(not_blank_pattern, "\t"); |
| 653 |
|
| 654 |
// \P{word} as only content - should match non-word characters |
| 655 |
let not_word_pattern = r"[\P{word}]"; |
| 656 |
assert_no_match(not_word_pattern, "a"); |
| 657 |
assert_no_match(not_word_pattern, "š"); |
| 658 |
assert_no_match(not_word_pattern, "_"); |
| 659 |
assert_no_match(not_word_pattern, "0"); |
| 660 |
assert_match(not_word_pattern, " "); |
| 661 |
assert_match(not_word_pattern, "-"); |
| 662 |
} |
| 663 |
|
| 664 |
#[test] |
| 665 |
fn char_class_negated_property_after_range() { |
| 666 |
// Test negated properties after ranges |
| 667 |
|
| 668 |
// Range followed by negated property |
| 669 |
let a_z_not_alnum_pattern = r"[a-z\P{alnum}]"; |
| 670 |
assert_match(a_z_not_alnum_pattern, "a"); |
| 671 |
assert_match(a_z_not_alnum_pattern, "z"); |
| 672 |
assert_match(a_z_not_alnum_pattern, "!"); // non-alphanumeric |
| 673 |
assert_no_match(a_z_not_alnum_pattern, "A"); |
| 674 |
assert_no_match(a_z_not_alnum_pattern, "0"); |
| 675 |
|
| 676 |
// Another test |
| 677 |
let zero_nine_not_blank_pattern = r"[0-9\P{blank}]"; |
| 678 |
assert_match(zero_nine_not_blank_pattern, "5"); |
| 679 |
assert_match(zero_nine_not_blank_pattern, "a"); // non-blank |
| 680 |
assert_no_match(zero_nine_not_blank_pattern, " "); |
| 681 |
assert_no_match(zero_nine_not_blank_pattern, "\t"); |
| 682 |
} |
| 683 |
|
| 684 |
#[test] |
| 685 |
fn char_class_negated_property_after_intersection() { |
| 686 |
// Test negated properties after && operator |
| 687 |
|
| 688 |
// \w intersected with negated blank - should match word chars as usual |
| 689 |
let word_and_not_blank_pattern = r"[\w&&\P{blank}]"; |
| 690 |
assert_match(word_and_not_blank_pattern, "a"); |
| 691 |
assert_match(word_and_not_blank_pattern, "š"); |
| 692 |
assert_match(word_and_not_blank_pattern, "0"); |
| 693 |
assert_match(word_and_not_blank_pattern, "_"); |
| 694 |
assert_no_match(word_and_not_blank_pattern, " "); |
| 695 |
assert_no_match(word_and_not_blank_pattern, "\t"); |
| 696 |
assert_no_match(word_and_not_blank_pattern, "-"); |
| 697 |
} |
| 698 |
|
| 699 |
#[test] |
| 700 |
fn char_class_negated_in_negated_class() { |
| 701 |
// Test negated properties in a negated char class |
| 702 |
// [^\P{alnum}] should match alphanumeric (double negation) |
| 703 |
|
| 704 |
let not_not_alnum_pattern = r"[^\P{alnum}]"; |
| 705 |
assert_match(not_not_alnum_pattern, "a"); |
| 706 |
assert_match(not_not_alnum_pattern, "š"); |
| 707 |
assert_match(not_not_alnum_pattern, "Z"); |
| 708 |
assert_match(not_not_alnum_pattern, "0"); |
| 709 |
assert_match(not_not_alnum_pattern, "9"); |
| 710 |
assert_no_match(not_not_alnum_pattern, " "); |
| 711 |
assert_no_match(not_not_alnum_pattern, "!"); |
| 712 |
assert_no_match(not_not_alnum_pattern, "_"); |
| 713 |
|
| 714 |
// [^\P{word}] should match word characters |
| 715 |
let not_not_word_pattern = r"[^\P{word}]"; |
| 716 |
assert_match(not_not_word_pattern, "a"); |
| 717 |
assert_match(not_not_word_pattern, "š"); |
| 718 |
assert_match(not_not_word_pattern, "_"); |
| 719 |
assert_match(not_not_word_pattern, "0"); |
| 720 |
assert_no_match(not_not_word_pattern, " "); |
| 721 |
assert_no_match(not_not_word_pattern, "-"); |
| 722 |
} |
| 723 |
|
| 724 |
#[test] |
| 725 |
fn char_class_negated_property_mixed() { |
| 726 |
// Test various combinations |
| 727 |
|
| 728 |
// Multiple items with negated property in the middle |
| 729 |
let mixed_pattern = r"[a-z0-9\P{blank}_\n]"; |
| 730 |
assert_match(mixed_pattern, "a"); |
| 731 |
assert_match(mixed_pattern, "5"); |
| 732 |
assert_match(mixed_pattern, "_"); |
| 733 |
assert_match(mixed_pattern, "!"); // non-blank |
| 734 |
assert_match(mixed_pattern, "A"); // non-blank (even though not in a-z or 0-9) |
| 735 |
assert_match(mixed_pattern, "š"); // non-blank (even though not in a-z or 0-9) |
| 736 |
assert_no_match(mixed_pattern, " "); // blank |
| 737 |
assert_no_match(mixed_pattern, "\t"); // blank |
| 738 |
|
| 739 |
// Multiple negated properties |
| 740 |
// [\P{alnum}\P{blank}] - matches non-alphanumeric intersected with non-blank |
| 741 |
let not_alnum_and_not_blank_pattern = r"[\P{alnum}\P{blank}]"; |
| 742 |
assert_match(not_alnum_and_not_blank_pattern, "!"); |
| 743 |
assert_match(not_alnum_and_not_blank_pattern, "-"); |
| 744 |
assert_match(not_alnum_and_not_blank_pattern, "_"); |
| 745 |
assert_no_match(not_alnum_and_not_blank_pattern, "a"); |
| 746 |
assert_no_match(not_alnum_and_not_blank_pattern, "š"); |
| 747 |
assert_no_match(not_alnum_and_not_blank_pattern, "0"); |
| 748 |
assert_no_match(not_alnum_and_not_blank_pattern, " "); // blank |
| 749 |
assert_no_match(not_alnum_and_not_blank_pattern, "\t"); // blank |
| 750 |
} |
| 751 |
|
| 752 |
#[cfg_attr(feature = "track_caller", track_caller)] |
| 753 |
fn match_text(re: &str, text: &str) -> bool { |
| 754 |
let regex = common::regex(re); |
| 755 |
let result = regex.is_match(text); |
| 756 |
assert!( |
| 757 |
result.is_ok(), |
| 758 |
"Expected match to succeed, but was {:?}", |
| 759 |
result |
| 760 |
); |
| 761 |
result.unwrap() |
| 762 |
} |
| 763 |
|
| 764 |
#[test] |
| 765 |
fn unicode_property_graph() { |
| 766 |
// Test \p{graph} - graphical/visible characters (not whitespace, control, unassigned, surrogate) |
| 767 |
assert_match(r"\p{graph}", "a"); |
| 768 |
assert_match(r"\p{graph}", "Z"); |
| 769 |
assert_match(r"\p{graph}", "0"); |
| 770 |
assert_match(r"\p{graph}", "!"); |
| 771 |
assert_match(r"\p{graph}", "š"); |
| 772 |
assert_no_match(r"\p{graph}", " "); |
| 773 |
assert_no_match(r"\p{graph}", "\t"); |
| 774 |
assert_no_match(r"\p{graph}", "\n"); |
| 775 |
assert_no_match(r"\p{graph}", "\x00"); // control character |
| 776 |
assert_no_match(r"\p{graph}", "\x7F"); // control character |
| 777 |
|
| 778 |
// Test \P{graph} - non-graphical characters |
| 779 |
assert_no_match(r"\P{graph}", "a"); |
| 780 |
assert_no_match(r"\P{graph}", "0"); |
| 781 |
assert_match(r"\P{graph}", " "); |
| 782 |
assert_match(r"\P{graph}", "\t"); |
| 783 |
assert_match(r"\P{graph}", "\n"); |
| 784 |
assert_match(r"\P{graph}", "\x00"); |
| 785 |
|
| 786 |
// Test in character class |
| 787 |
assert_match(r"[\p{graph}]", "a"); |
| 788 |
assert_match(r"[\p{graph}]", "!"); |
| 789 |
assert_no_match(r"[\p{graph}]", " "); |
| 790 |
|
| 791 |
assert_match(r"[\P{graph}]", " "); |
| 792 |
assert_no_match(r"[\P{graph}]", "a"); |
| 793 |
|
| 794 |
// Test combining graph with other characters in a class |
| 795 |
assert_match(r"[\p{graph}0-9]", "a"); |
| 796 |
assert_match(r"[\p{graph}0-9]", "5"); |
| 797 |
assert_no_match(r"[\p{graph}0-9]", " "); |
| 798 |
} |
| 799 |
|
| 800 |
#[test] |
| 801 |
fn unicode_property_print() { |
| 802 |
// Test \p{print} - printable characters (graph + space separators) |
| 803 |
assert_match(r"\p{print}", "a"); |
| 804 |
assert_match(r"\p{print}", "Z"); |
| 805 |
assert_match(r"\p{print}", "0"); |
| 806 |
assert_match(r"\p{print}", "!"); |
| 807 |
assert_match(r"\p{print}", "š"); |
| 808 |
assert_match(r"\p{print}", " "); // space separator |
| 809 |
assert_no_match(r"\p{print}", "\t"); // tab is not a space separator |
| 810 |
assert_no_match(r"\p{print}", "\n"); |
| 811 |
assert_no_match(r"\p{print}", "\x00"); // control character |
| 812 |
assert_no_match(r"\p{print}", "\x7F"); // control character |
| 813 |
|
| 814 |
// Test \P{print} - non-printable characters |
| 815 |
assert_no_match(r"\P{print}", "a"); |
| 816 |
assert_no_match(r"\P{print}", "0"); |
| 817 |
assert_no_match(r"\P{print}", " "); // space is printable |
| 818 |
assert_match(r"\P{print}", "\t"); |
| 819 |
assert_match(r"\P{print}", "\n"); |
| 820 |
assert_match(r"\P{print}", "\x00"); |
| 821 |
|
| 822 |
// Test in character class |
| 823 |
assert_match(r"[\p{print}]", "a"); |
| 824 |
assert_match(r"[\p{print}]", " "); |
| 825 |
assert_no_match(r"[\p{print}]", "\t"); |
| 826 |
|
| 827 |
assert_match(r"[\P{print}]", "\t"); |
| 828 |
assert_no_match(r"[\P{print}]", "a"); |
| 829 |
|
| 830 |
// Test combining print with other characters in a class |
| 831 |
assert_match(r"[\p{print}A-Z]", "a"); |
| 832 |
assert_match(r"[\p{print}A-Z]", "A"); |
| 833 |
assert_match(r"[\p{print}A-Z]", " "); |
| 834 |
assert_no_match(r"[\p{print}A-Z]", "\t"); |
| 835 |
} |
| 836 |
|
| 837 |
#[test] |
| 838 |
fn unicode_property_cntrl_graph_print_combined() { |
| 839 |
// Test combinations to ensure they work as expected |
| 840 |
|
| 841 |
// Test that each property works individually in character classes |
| 842 |
assert_match(r"[\p{cntrl}]", "\x00"); // cntrl |
| 843 |
assert_match(r"[\p{graph}]", "a"); // graph |
| 844 |
assert_match(r"[\p{print}]", " "); // print |
| 845 |
|
| 846 |
// Verify that cntrl and graph are distinct |
| 847 |
assert_match(r"\p{cntrl}", "\x00"); |
| 848 |
assert_no_match(r"\p{graph}", "\x00"); |
| 849 |
|
| 850 |
// Verify that graph is a subset of print |
| 851 |
assert_match(r"\p{graph}", "a"); |
| 852 |
assert_match(r"\p{print}", "a"); |
| 853 |
|
| 854 |
// Space is in print but not graph |
| 855 |
assert_no_match(r"\p{graph}", " "); |
| 856 |
assert_match(r"\p{print}", " "); |
| 857 |
|
| 858 |
// Test that cntrl and graph can be combined in a character class |
| 859 |
// This is tricky because graph is a negated class |
| 860 |
// We expect: matches cntrl OR graph, but not space |
| 861 |
assert_match(r"[\p{cntrl}\x21-\x7E]", "\x00"); // control char |
| 862 |
assert_match(r"[\p{cntrl}\x21-\x7E]", "a"); // visible ASCII |
| 863 |
assert_no_match(r"[\p{cntrl}\x21-\x7E]", " "); // space (not in cntrl or \x21-\x7E) |
| 864 |
} |
| 865 |
|
| 866 |
#[test] |
| 867 |
fn test_basic_subroutine() { |
| 868 |
common::assert_is_match(r"^(a)\g<1>$", "aa"); |
| 869 |
common::assert_no_match(r"^(a)\g<1>$", "ab"); |
| 870 |
common::assert_no_match(r"^(a)\g<1>$", "aab"); |
| 871 |
|
| 872 |
// Named group subroutine |
| 873 |
common::assert_is_match(r"(?<name>ab)\g<name>", "abab"); |
| 874 |
common::assert_no_match(r"(?<name>ab)\g<name>", "abcd"); |
| 875 |
} |
| 876 |
|
| 877 |
#[test] |
| 878 |
fn test_recursive_subroutine() { |
| 879 |
let balanced_parens = r"^(?<foo>a|\(\g<foo>\))$"; |
| 880 |
common::assert_is_match(balanced_parens, "a"); |
| 881 |
common::assert_is_match(balanced_parens, "(a)"); |
| 882 |
common::assert_is_match(balanced_parens, "(((((a)))))"); |
| 883 |
common::assert_no_match(balanced_parens, "(a"); |
| 884 |
common::assert_no_match(balanced_parens, "((a)"); |
| 885 |
} |
| 886 |
|
| 887 |
#[test] |
| 888 |
fn test_forward_reference_subroutine() { |
| 889 |
// Forward reference: subroutine call before group definition |
| 890 |
assert_match(r"\g<1>(a)", "aa"); |
| 891 |
assert_match(r"\g<name>(?<name>a)", "aa"); |
| 892 |
} |
| 893 |
|
| 894 |
#[test] |
| 895 |
fn test_forward_refrence_subroutine_zero_repetition_capture_group() { |
| 896 |
// This has a forward reference to group n, which is defined as . with {0} repetition |
| 897 |
assert_match(r"\g<n>(?<n>.){0}", "X"); |
| 898 |
} |
| 899 |
|
| 900 |
#[test] |
| 901 |
fn absent_repeater_with_hard_inner_backref() { |
| 902 |
// Hard absent repeater: (?~\1) where \1 is a backreference. |
| 903 |
// The pattern matches: capture 1+ backticks, then absent-repeat while those backticks |
| 904 |
// are absent, then match those backticks again as the closing delimiter. |
| 905 |
assert_match(r"(`+)(?~\1)\1", "```code```"); |
| 906 |
assert_match(r"(`+)(?~\1)\1", "``code``"); |
| 907 |
assert_match(r"(`+)(?~\1)\1", "`code`"); |
| 908 |
// Require at least 3 backticks: "```abc```" matches, "```code``" has no 3-backtick close. |
| 909 |
assert_match(r"^(`{3,})(?~\1)\1$", "```abc```"); |
| 910 |
assert_no_match(r"^(`{3,})(?~\1)\1$", "```code``"); |
| 911 |
// Just backticks alone without a body cannot form a valid open+close fence |
| 912 |
assert_no_match(r"^(`{3,})(?~\1)\1$", "```"); |
| 913 |
// A 4-backtick fence can contain 3-backtick sequences |
| 914 |
assert_match(r"^(`{3,})(?~\1)\1$", "````has ``` inside````"); |
| 915 |
} |
| 916 |
|
| 917 |
#[test] |
| 918 |
fn absent_repeater_with_lookahead_inner() { |
| 919 |
// Hard absent repeater: (?~(?=end)) where the inner is a lookahead (hard). |
| 920 |
// This matches everything until "end" appears. |
| 921 |
assert_match(r"(?~(?=end))end", "endend"); |
| 922 |
assert_match(r"(?~(?=end))end", "abcend"); |
| 923 |
assert_no_match(r"^(?~(?=end))$", "abcend"); |
| 924 |
} |
| 925 |
|
| 926 |
#[test] |
| 927 |
fn test_define_group_subroutine_call() { |
| 928 |
// Basic DEFINE block: the definition does not match, the subroutine call does |
| 929 |
assert_match(r"(?(DEFINE)(?<word>\w+))\g<word>", "hello"); |
| 930 |
assert_no_match(r"^(?(DEFINE)(?<word>\w+))\g<word>$", "hello world"); |
| 931 |
} |
| 932 |
|
| 933 |
#[test] |
| 934 |
fn test_define_group_multiple_definitions() { |
| 935 |
// DEFINE block with multiple named groups used as subroutines |
| 936 |
let pattern = |
| 937 |
r"^(?(DEFINE)(?<year>\d{4})(?<month>\d{2})(?<day>\d{2}))\g<year>-\g<month>-\g<day>$"; |
| 938 |
assert_match(pattern, "2024-03-17"); |
| 939 |
assert_no_match(pattern, "1-2-3"); |
| 940 |
} |
| 941 |
|
| 942 |
#[test] |
| 943 |
fn test_define_group_does_not_consume_input() { |
| 944 |
// The DEFINE block itself must not consume any input; only the subroutine call does |
| 945 |
let pattern = r"^(?(DEFINE)(?<digit>\d)ignored_literal)X\g<digit>Y$"; |
| 946 |
assert_match(pattern, "X5Y"); |
| 947 |
assert_no_match(pattern, "X Y"); |
| 948 |
assert_no_match(pattern, "X8ignored_literalY"); |
| 949 |
} |
| 950 |
|
| 951 |
#[test] |
| 952 |
fn test_unrestricted_group_names() { |
| 953 |
// Hyphenated group names |
| 954 |
assert_match(r"(?<foo-bar>a)b", "ab"); |
| 955 |
assert_match(r"(?'foo-bar'a)b", "ab"); |
| 956 |
assert_match(r"(?P<foo-bar>a)b", "ab"); |
| 957 |
|
| 958 |
// CSS-style names |
| 959 |
assert_match(r"(?<data-value-1>\d+)", "42"); |
| 960 |
|
| 961 |
// Special characters in names |
| 962 |
assert_match(r"(?<#tag>\w+)", "hello"); |
| 963 |
assert_match(r"(?<hello!>\w+)", "world"); |
| 964 |
} |
| 965 |
|
| 966 |
#[test] |
| 967 |
fn test_subroutine_calls_with_unrestricted_names() { |
| 968 |
// Subroutine call with hyphenated name via \g<> |
| 969 |
assert_match(r"(?<foo-bar>a)\g<foo-bar>", "aa"); |
| 970 |
assert_no_match(r"(?<foo-bar>a)\g<foo-bar>", "ab"); |
| 971 |
|
| 972 |
// Subroutine call with hyphenated name via \g'' |
| 973 |
assert_match(r"(?<foo-bar>a)\g'foo-bar'", "aa"); |
| 974 |
|
| 975 |
// Subroutine call with hyphenated name via (?P>) |
| 976 |
assert_match(r"(?<foo-bar>a)(?P>foo-bar)", "aa"); |
| 977 |
assert_no_match(r"(?<foo-bar>a)(?P>foo-bar)", "ab"); |
| 978 |
|
| 979 |
// Forward reference with hyphenated name |
| 980 |
assert_match(r"\g<foo-bar>(?<foo-bar>a)", "aa"); |
| 981 |
|
| 982 |
// Recursive subroutine with hyphenated name |
| 983 |
let balanced = r"^(?<my-group>a|\(\g<my-group>\))$"; |
| 984 |
assert_match(balanced, "a"); |
| 985 |
assert_match(balanced, "(a)"); |
| 986 |
assert_match(balanced, "((a))"); |
| 987 |
assert_no_match(balanced, "(a"); |
| 988 |
} |
| 989 |
|
| 990 |
/// A single character class sitting next to a "fancy" feature is compiled to the |
| 991 |
/// native single-character-class instruction (`Insn::CharClass`) rather than a |
| 992 |
/// delegated regex-automata engine. These cases verify it still matches exactly |
| 993 |
/// what the engine would, in both str (Unicode) and ASCII bytes modes. The |
| 994 |
/// leading look-behind forces the class through the backtracking VM (a trailing |
| 995 |
/// look-ahead would be optimized away). |
| 996 |
#[test] |
| 997 |
fn native_char_class_matches_like_engine() { |
| 998 |
// Builtin classes. |
| 999 |
common::assert_is_match(r"(?<=a)\d", "a5"); |
| 1000 |
common::assert_no_match(r"(?<=a)\d", "ab"); |
| 1001 |
common::assert_is_match(r"(?<=a)\w", "a_"); |
| 1002 |
common::assert_no_match(r"(?<=a)\w", "a-"); |
| 1003 |
|
| 1004 |
// Explicit ranges, including multiple ranges in one class. |
| 1005 |
common::assert_is_match(r"(?<=a)[xyz]", "ay"); |
| 1006 |
common::assert_no_match(r"(?<=a)[xyz]", "aw"); |
| 1007 |
common::assert_is_match(r"(?<=a)[a-f0-9]", "a3"); |
| 1008 |
common::assert_no_match(r"(?<=a)[a-f0-9]", "ag"); |
| 1009 |
|
| 1010 |
// Negated class. |
| 1011 |
common::assert_is_match(r"(?<=a)[^0-9]", "ab"); |
| 1012 |
common::assert_no_match(r"(?<=a)[^0-9]", "a5"); |
| 1013 |
|
| 1014 |
// Case-insensitive class (ASCII case folding applies in both modes). |
| 1015 |
common::assert_is_match(r"(?<=a)(?i:[p-t])", "aR"); |
| 1016 |
common::assert_no_match(r"(?<=a)(?i:[p-t])", "aZ"); |
| 1017 |
} |
| 1018 |
|
| 1019 |
/// The native codepoint matcher must decode multi-byte UTF-8 in Unicode (str) |
| 1020 |
/// mode. ASCII bytes mode is covered by `assert_is_match` above for ASCII inputs. |
| 1021 |
#[test] |
| 1022 |
fn native_char_class_matches_multibyte_unicode() { |
| 1023 |
// `\w` is Unicode-aware in str mode, so it matches non-ASCII letters. |
| 1024 |
let re = fancy_regex::Regex::new(r"(?<=a)\w").unwrap(); |
| 1025 |
assert!(re.is_match("aé").unwrap()); |
| 1026 |
assert!(re.is_match("añ").unwrap()); |
| 1027 |
assert!(!re.is_match("a.").unwrap()); |
| 1028 |
|
| 1029 |
// Case-insensitive Unicode folding (ſ folds to s). |
| 1030 |
let re = fancy_regex::Regex::new(r"(?<=a)(?i:s)").unwrap(); |
| 1031 |
assert!(re.is_match("a\u{17F}").unwrap()); |
| 1032 |
|
| 1033 |
// Negated class still consumes exactly one (multi-byte) codepoint. |
| 1034 |
let re = fancy_regex::Regex::new(r"(?<=a)[^\d]").unwrap(); |
| 1035 |
assert!(re.is_match("aé").unwrap()); |
| 1036 |
} |
| 1037 |
|
| 1038 |
#[test] |
| 1039 |
fn ambiguous_concat_repeat_optimization_not_more_permissive() { |
| 1040 |
// \w*\.?\w+ requires at least one word character (from \w+). |
| 1041 |
// After optimization to (?:\w*\.{1})?\w+, it must not match an empty string. |
| 1042 |
common::assert_is_match(r"\w*\.?\w+", "a"); |
| 1043 |
common::assert_is_match(r"\w*\.?\w+", "ab"); |
| 1044 |
common::assert_is_match(r"\w*\.?\w+", "a.b"); |
| 1045 |
common::assert_no_match(r"^\w*\.?\w+$", ""); |
| 1046 |
common::assert_no_match(r"^\w*\.?\w+$", "."); |
| 1047 |
|
| 1048 |
// \w+\.?\w* requires at least one word character (from \w+). |
| 1049 |
// After optimization to \w+(?:\.{1}\w*)?, it must not match an empty string. |
| 1050 |
common::assert_is_match(r"\w+\.?\w*", "a"); |
| 1051 |
common::assert_is_match(r"\w+\.?\w*", "ab"); |
| 1052 |
common::assert_is_match(r"\w+\.?\w*", "a.b"); |
| 1053 |
common::assert_no_match(r"^\w+\.?\w*$", ""); |
| 1054 |
common::assert_no_match(r"^\w+\.?\w*$", "."); |
| 1055 |
|
| 1056 |
// \s+\w?\s* requires at least one whitespace character (from \s+). |
| 1057 |
// After optimization to \s+(?:\w{1}\s*)?, it must not match an empty string. |
| 1058 |
common::assert_is_match(r"\s+\w?\s*", " "); |
| 1059 |
common::assert_is_match(r"\s+\w?\s*", " a "); |
| 1060 |
common::assert_no_match(r"^\s+\w?\s*$", ""); |
| 1061 |
common::assert_no_match(r"^\s+\w?\s*$", "a"); |
| 1062 |
} |
| 1063 |
|
| 1064 |
#[test] |
| 1065 |
fn casei_literal_in_hard_pattern() { |
| 1066 |
// Lookbehind forces these onto the VM, where casei literals are matched |
| 1067 |
// by the native LitCasei instruction. Semantics must equal a delegated |
| 1068 |
// (?i) literal: Unicode simple case folding. |
| 1069 |
common::assert_is_match(r"(?<=x)(?i)kelvin", "xKELVIN"); |
| 1070 |
common::assert_is_match(r"(?<=x)(?i)KELVIN", "xkelvin"); |
| 1071 |
common::assert_no_match(r"(?<=x)(?i)kelvin", "xKELVIM"); |
| 1072 |
|
| 1073 |
// Mixed casei and case-sensitive chars in one literal batch |
| 1074 |
common::assert_is_match(r"(?<=x)(?i:ab)cd", "xABcd"); |
| 1075 |
common::assert_no_match(r"(?<=x)(?i:ab)cd", "xABCD"); |
| 1076 |
|
| 1077 |
// Unicode folding applies in str mode only (the shared both-modes helpers |
| 1078 |
// also run in ASCII bytes mode, where (?i) folds ASCII only). |
| 1079 |
let is_match = |
| 1080 |
|re: &str, text: &str| fancy_regex::Regex::new(re).unwrap().is_match(text).unwrap(); |
| 1081 |
|
| 1082 |
// Cyrillic folding |
| 1083 |
assert!(is_match(r"(?<=x)(?i)привет", "xПРИВЕТ")); |
| 1084 |
assert!(is_match(r"(?<=x)(?i)ПРИВЕТ", "xпривет")); |
| 1085 |
|
| 1086 |
// Width-changing fold variants: U+212A KELVIN SIGN (3 bytes) matches |
| 1087 |
// literal `k` (1 byte), and U+017F LATIN SMALL LETTER LONG S matches `s`. |
| 1088 |
assert!(is_match(r"(?<=x)(?i)k", "x\u{212A}")); |
| 1089 |
assert!(is_match(r"(?<=x)(?i)s", "x\u{17F}")); |
| 1090 |
} |
| 1091 |
|
| 1092 |
#[test] |
| 1093 |
fn casei_keyword_alternation_in_hard_pattern() { |
| 1094 |
let haystack = "if ABSENT then"; |
| 1095 |
let m = fancy_regex::Regex::new(r"\b(?i)(abort|absent|zzz)\b") |
| 1096 |
.unwrap() |
| 1097 |
.find(haystack) |
| 1098 |
.unwrap() |
| 1099 |
.unwrap(); |
| 1100 |
assert_eq!(&haystack[m.start()..m.end()], "ABSENT"); |
| 1101 |
} |
| 1102 |
|
| 1103 |
#[test] |
| 1104 |
fn leftmost_longest_default_behavior_unchanged() { |
| 1105 |
let re = RegexBuilder::new(r"(a|ab)c").build().unwrap(); |
| 1106 |
assert_eq!(re.find("xabc").unwrap().unwrap().as_str(), "abc"); |
| 1107 |
} |