Skip to content

Commit

Permalink
fix: Fix and improve regex for sensitive and flexible command matchin…
Browse files Browse the repository at this point in the history
…g, add tests

- Fixed a bug where regex incorrectly matched "/cfg" with "/g" using a Sensitive filter.
- Improved Flexible command matching.
- Added new tests for command matching functionality.
- Bump version to 1.2.2.
  • Loading branch information
Azumi4han committed Apr 12, 2024
1 parent 36c8102 commit 2af43cc
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "safe-vk"
version = "1.2.1"
version = "1.2.2"
edition = "2021"
authors = ["Azumi4han"]
homepage = "https://github.com/Azumi4han/safe-vk"
Expand Down
8 changes: 3 additions & 5 deletions src/util/matchit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ pub enum Filter {
pub fn matchit(message: &str, command: &str, filter: &Filter) -> bool {
let pattern = match filter {
Filter::Strict => format!(r"^{}$", regex::escape(command)),
Filter::Flexible => format!(r"(?si).*{}", regex::escape(command)),
Filter::Flexible => format!(r"(?i)^\s*[^\w\s]?{}\s*$", regex::escape(command)),
Filter::Sensitive => format!(
r"(?si)[^\w\s]*({}?{})",
regex::escape(&command[..1]),
regex::escape(&command[1..])
r"(?i)(?:^|[\W_]){}(?:[\W_]|$)",
regex::escape(&command.trim_start_matches(|c: char| !c.is_alphanumeric()))
),
};

Regex::new(&pattern).unwrap().is_match(message)
}
4 changes: 3 additions & 1 deletion tests/test_command_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ fn flexible() {
assert!(!matchit("This is a command1 2 3 message", "!command", filter));
assert!(!matchit("This is a $command 123message", "!command", filter));
assert!(!matchit("This is a %coMMand message", "!command", filter));
assert!(matchit("This is a !coMManD message ", "!command", filter));
assert!(!matchit("This is a !coMManD message ", "!command", filter));
assert!(!matchit("command", "!command", filter));
assert!(matchit(" !command", "!command", filter));
assert!(matchit("!command ", "!command", filter));
assert!(matchit("!command ", "!command", filter));
assert!(!matchit("$command", "!command", filter));
assert!(matchit("!command", "!command", filter));
Expand Down

0 comments on commit 2af43cc

Please sign in to comment.