summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimmo Saan <simmo.saan@gmail.com>2017-04-29 17:04:44 +0200
committerSébastien Helleu <flashcode@flashtux.org>2017-04-29 17:04:44 +0200
commit1329dfb57ad6759316dfa366ec38176dfa59a837 (patch)
tree0cf5a041d7994f845336573279a531f8f85755bc
parenta9f6c34faea12d16e7e2710ef106a8f144f0d5f5 (diff)
downloadweechat-1329dfb57ad6759316dfa366ec38176dfa59a837.zip
core: add wildcard matching operators to eval (closes #608)
-rw-r--r--src/core/wee-command.c5
-rw-r--r--src/core/wee-eval.c10
-rw-r--r--src/core/wee-eval.h2
-rw-r--r--tests/unit/core/test-eval.cpp8
4 files changed, 23 insertions, 2 deletions
diff --git a/src/core/wee-command.c b/src/core/wee-command.c
index 6496a0732..fcfcd4726 100644
--- a/src/core/wee-command.c
+++ b/src/core/wee-command.c
@@ -7254,6 +7254,8 @@ command_init ()
" > greater\n"
" =~ is matching POSIX extended regex\n"
" !~ is NOT matching POSIX extended regex\n"
+ " =* is matching wildcard mask\n"
+ " !* is NOT matching wildcard mask\n"
"\n"
"An expression is considered as \"true\" if it is not NULL, not "
"empty, and different from \"0\".\n"
@@ -7326,7 +7328,8 @@ command_init ()
" /eval -n -c abcd =~ ^ABC ==> 1\n"
" /eval -n -c abcd =~ (?-i)^ABC ==> 0\n"
" /eval -n -c abcd =~ (?-i)^abc ==> 1\n"
- " /eval -n -c abcd !~ abc ==> 0"),
+ " /eval -n -c abcd !~ abc ==> 0\n"
+ " /eval -n -c abcd =* a*d ==> 1"),
"-n|-s|-c -n|-s|-c",
&command_eval, NULL, NULL);
hook_command (
diff --git a/src/core/wee-eval.c b/src/core/wee-eval.c
index ebcaec5fe..6864a94d6 100644
--- a/src/core/wee-eval.c
+++ b/src/core/wee-eval.c
@@ -47,7 +47,7 @@ char *logical_ops[EVAL_NUM_LOGICAL_OPS] =
{ "||", "&&" };
char *comparisons[EVAL_NUM_COMPARISONS] =
-{ "=~", "!~", "==", "!=", "<=", "<", ">=", ">" };
+{ "=~", "!~", "=*", "!*", "==", "!=", "<=", "<", ">=", ">" };
char *eval_replace_vars (const char *expr, struct t_hashtable *pointers,
@@ -791,6 +791,14 @@ eval_compare (const char *expr1, int comparison, const char *expr2)
rc ^= 1;
goto end;
}
+ else if ((comparison == EVAL_COMPARE_STRING_MATCHING)
+ || (comparison == EVAL_COMPARE_STRING_NOT_MATCHING))
+ {
+ rc = string_match (expr1, expr2, 0);
+ if (comparison == EVAL_COMPARE_STRING_NOT_MATCHING)
+ rc ^= 1;
+ goto end;
+ }
length1 = strlen (expr1);
length2 = strlen (expr2);
diff --git a/src/core/wee-eval.h b/src/core/wee-eval.h
index 2eee0c0e6..4b4967bc3 100644
--- a/src/core/wee-eval.h
+++ b/src/core/wee-eval.h
@@ -42,6 +42,8 @@ enum t_eval_comparison
{
EVAL_COMPARE_REGEX_MATCHING = 0,
EVAL_COMPARE_REGEX_NOT_MATCHING,
+ EVAL_COMPARE_STRING_MATCHING,
+ EVAL_COMPARE_STRING_NOT_MATCHING,
EVAL_COMPARE_EQUAL,
EVAL_COMPARE_NOT_EQUAL,
EVAL_COMPARE_LESS_EQUAL,
diff --git a/tests/unit/core/test-eval.cpp b/tests/unit/core/test-eval.cpp
index 14f60f80b..f9e8a2fbb 100644
--- a/tests/unit/core/test-eval.cpp
+++ b/tests/unit/core/test-eval.cpp
@@ -121,6 +121,10 @@ TEST(Eval, EvalCondition)
WEE_CHECK_EVAL("0", "abcd =~ (?-i)^ABC");
WEE_CHECK_EVAL("0", "abcd =~ \\(abcd\\)");
WEE_CHECK_EVAL("0", "(abcd) =~ \\(\\(abcd\\)\\)");
+ WEE_CHECK_EVAL("0", "abcd =* abce");
+ WEE_CHECK_EVAL("0", "abcd =* a*e");
+ WEE_CHECK_EVAL("0", "abcd !* *bc*");
+ WEE_CHECK_EVAL("0", "abcd !* *");
WEE_CHECK_EVAL("0", "${test} == test");
WEE_CHECK_EVAL("0", "${test2} == value2");
WEE_CHECK_EVAL("0", "${buffer.number} == 2");
@@ -157,6 +161,10 @@ TEST(Eval, EvalCondition)
WEE_CHECK_EVAL("1", "(abcd) =~ (abcd)");
WEE_CHECK_EVAL("1", "(abcd) =~ \\(abcd\\)");
WEE_CHECK_EVAL("1", "((abcd)) =~ \\(\\(abcd\\)\\)");
+ WEE_CHECK_EVAL("1", "abcd !* abce");
+ WEE_CHECK_EVAL("1", "abcd !* a*e");
+ WEE_CHECK_EVAL("1", "abcd =* *bc*");
+ WEE_CHECK_EVAL("1", "abcd =* *");
WEE_CHECK_EVAL("1", "${test} == value");
WEE_CHECK_EVAL("1", "${test2} ==");
WEE_CHECK_EVAL("1", "${buffer.number} == 1");