Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5ad5820

Browse files
feat: support flags for hybrid regex (#1915)
<!-- Thank you for contributing! --> ### Description closes #1911 <!-- Please insert your description here and provide especially info about the "what" this PR is solving --> --------- Co-authored-by: IWANABETHATGUY <[email protected]>
1 parent 13f4adc commit 5ad5820

1 file changed

Lines changed: 25 additions & 3 deletions

File tree

crates/rolldown_common/src/types/js_regex.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,21 @@ pub enum HybridRegex {
88
}
99

1010
impl HybridRegex {
11-
pub fn new(source: &str) -> anyhow::Result<Self> {
12-
match regex::Regex::new(source).map(HybridRegex::Optimize) {
11+
pub fn new(pattern: &str) -> anyhow::Result<Self> {
12+
match regex::Regex::new(pattern).map(HybridRegex::Optimize) {
1313
Ok(reg) => Ok(reg),
14-
Err(_) => regress::Regex::new(source).map(HybridRegex::Ecma).map_err(anyhow::Error::from),
14+
Err(_) => regress::Regex::new(pattern).map(HybridRegex::Ecma).map_err(anyhow::Error::from),
15+
}
16+
}
17+
18+
pub fn with_flags(pattern: &str, flags: &str) -> anyhow::Result<Self> {
19+
let regex_pattern = if flags.is_empty() { pattern } else { &format!("(?{flags}){pattern}") };
20+
21+
match regex::Regex::new(regex_pattern).map(HybridRegex::Optimize) {
22+
Ok(reg) => Ok(reg),
23+
Err(_) => regress::Regex::with_flags(pattern, flags)
24+
.map(HybridRegex::Ecma)
25+
.map_err(anyhow::Error::from),
1526
}
1627
}
1728

@@ -22,3 +33,14 @@ impl HybridRegex {
2233
}
2334
}
2435
}
36+
37+
mod test {
38+
#[test]
39+
fn with_flags() {
40+
let reg = super::HybridRegex::with_flags("a", "i").unwrap();
41+
assert!(reg.matches("A"));
42+
43+
let reg = super::HybridRegex::new("a").unwrap();
44+
assert!(!reg.matches("A"));
45+
}
46+
}

0 commit comments

Comments
 (0)