From f326eef2c9c827935a27de16748abe5e253b9979 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Sun, 4 Apr 2021 11:57:50 -0600 Subject: force patterns to match full text --- src/makefile/pattern.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/makefile/pattern.rs b/src/makefile/pattern.rs index b47c75d..9c5a0fa 100644 --- a/src/makefile/pattern.rs +++ b/src/makefile/pattern.rs @@ -2,7 +2,7 @@ use eyre::Result; use regex::{Captures, Regex}; fn compile_pattern(pattern: &str) -> Result { - let mut result = String::new(); + let mut result = "^".to_owned(); for c in pattern.chars() { if c == '%' { if let Some(real_result) = result.strip_suffix(r"\\\\") { @@ -22,6 +22,7 @@ fn compile_pattern(pattern: &str) -> Result { result.push_str(®ex::escape(&c.to_string())); } } + result.push('$'); Ok(Regex::new(&result)?) } @@ -38,10 +39,10 @@ mod test { #[test] fn pattern_backslashes() -> R { let test_case = compile_pattern(r"the\%weird\\%pattern\\")?; - assert_eq!(test_case.to_string(), r"the%weird\\(\w*)pattern\\\\"); + assert_eq!(test_case.to_string(), r"^the%weird\\(.*)pattern\\\\$"); let hell = compile_pattern(r"\\\\%")?; - assert_eq!(hell.to_string(), r"\\\\\\(\w*)"); + assert_eq!(hell.to_string(), r"^\\\\\\(.*)$"); Ok(()) } } -- cgit v1.2.3