Skip to content
This repository was archived by the owner on May 29, 2023. It is now read-only.

Commit e9013e0

Browse files
authored
Merge pull request #15 from robinst/bump-regex
Update regex to get character class nesting & intersections
2 parents de912aa + b869e0a commit e9013e0

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ description = "An implementation of regexes, supporting a relatively rich set of
77
repository = "https://github.com/google/fancy-regex"
88

99
[dependencies]
10-
regex = "0.2.1"
10+
regex = "0.2.2"
1111
bit-set = "0.4"
1212
memchr = "1.0.1"

tests/matching.rs

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,67 @@ use fancy_regex::Regex;
55

66
#[test]
77
fn control_character_escapes() {
8-
assert_matches(r"\a", "\x07");
9-
assert_matches(r"\e", "\x1B");
10-
assert_matches(r"\f", "\x0C");
11-
assert_matches(r"\n", "\x0A");
12-
assert_matches(r"\r", "\x0D");
13-
assert_matches(r"\t", "\x09");
14-
assert_matches(r"\v", "\x0B");
8+
assert_match(r"\a", "\x07");
9+
assert_match(r"\e", "\x1B");
10+
assert_match(r"\f", "\x0C");
11+
assert_match(r"\n", "\x0A");
12+
assert_match(r"\r", "\x0D");
13+
assert_match(r"\t", "\x09");
14+
assert_match(r"\v", "\x0B");
1515
}
1616

1717
#[test]
1818
fn character_class_escapes() {
19-
assert_matches(r"[\[]", "[");
20-
assert_matches(r"[\^]", "^");
19+
assert_match(r"[\[]", "[");
20+
assert_match(r"[\^]", "^");
2121

2222
// The regex crate would reject the following because it's not necessary to escape them.
2323
// Other engines allow to escape any non-alphanumeric character.
24-
assert_matches(r"[\<]", "<");
25-
assert_matches(r"[\>]", ">");
26-
assert_matches(r"[\.]", ".");
24+
assert_match(r"[\<]", "<");
25+
assert_match(r"[\>]", ">");
26+
assert_match(r"[\.]", ".");
2727

2828
// Character class escape
29-
assert_matches(r"[\d]", "1");
29+
assert_match(r"[\d]", "1");
3030

3131
// Control characters
32-
assert_matches(r"[\e]", "\x1B");
33-
assert_matches(r"[\n]", "\x0A");
32+
assert_match(r"[\e]", "\x1B");
33+
assert_match(r"[\n]", "\x0A");
3434
}
3535

36+
#[test]
37+
fn character_class_nested() {
38+
assert_match(r"[[a][bc]]", "c");
39+
assert_match(r"[a[^b]]", "c");
40+
}
41+
42+
#[test]
43+
fn character_class_intersection() {
44+
assert_match(r"[\w&&a-c]", "c");
45+
assert_no_match(r"[\w&&a-c]", "d");
46+
47+
assert_match(r"[[0-9]&&[^4]]", "1");
48+
assert_no_match(r"[[0-9]&&[^4]]", "4");
49+
}
50+
51+
52+
fn assert_match(re: &str, text: &str) {
53+
let result = match_text(re, text);
54+
assert_eq!(result, true, "Expected regex '{}' to match text '{}'", re, text);
55+
}
56+
57+
fn assert_no_match(re: &str, text: &str) {
58+
let result = match_text(re, text);
59+
assert_eq!(result, false, "Expected regex '{}' to not match text '{}'", re, text);
60+
}
3661

37-
fn assert_matches(re: &str, text: &str) {
62+
fn match_text(re: &str, text: &str) -> bool {
3863
let parse_result = Regex::new(re);
3964
assert!(parse_result.is_ok(),
4065
"Expected regex '{}' to be compiled successfully, got {:?}", re, parse_result.err());
4166

4267
let regex = parse_result.unwrap();
4368
let match_result = regex.is_match(text);
4469
assert!(match_result.is_ok(), "Expected match to succeed, but was {:?}", match_result);
45-
assert_eq!(match_result.ok(), Some(true), "Expected regex '{}' to match text '{}'", re, text);
70+
match_result.unwrap()
4671
}

0 commit comments

Comments
 (0)