From 479ab5bc58ac006512187ceb839223abdea242fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Thu, 23 Nov 2023 08:20:01 +0100 Subject: [PATCH] core: evaluate expressions even when the suffix is missing (issue #2042, issue #1714) --- ChangeLog.adoc | 1 + src/core/wee-eval.c | 13 +++++++------ src/core/wee-string.c | 23 ++++++++++++----------- src/core/wee-string.h | 5 ++++- tests/unit/core/test-core-eval.cpp | 3 ++- tests/unit/core/test-core-string.cpp | 21 ++++++++++++++------- 6 files changed, 40 insertions(+), 26 deletions(-) diff --git a/ChangeLog.adoc b/ChangeLog.adoc index 4f1ad1ed657..d6ef7077b0b 100644 --- a/ChangeLog.adoc +++ b/ChangeLog.adoc @@ -15,6 +15,7 @@ For a list of important changes that require manual actions, please look at rele New features:: + * core: evaluate expressions even when the suffix is missing ("}" by default) (issue #2042, issue #1714) * core: add syntax highlighting in evaluation of expressions with `raw_hl:string` and `hl:string`, add option weechat.color.eval_syntax_colors (issue #2042) * core: add option `search_history` in command `/input`, add key kbd:[Ctrl+r] to search in commands history, add key context "histsearch" (issue #2040) * core: add option weechat.look.buffer_search_history (issue #2040) diff --git a/src/core/wee-eval.c b/src/core/wee-eval.c index 4d8c0e7da09..e436e26b0d3 100644 --- a/src/core/wee-eval.c +++ b/src/core/wee-eval.c @@ -1472,8 +1472,8 @@ eval_string_hdata (const char *text, struct t_eval_context *eval_context) */ char * -eval_syntax_highlight_add_markers (const char *text, - struct t_eval_context *eval_context) +eval_syntax_highlight_add_markers (const char *prefix, const char *text, + const char *suffix) { char **value; @@ -1482,10 +1482,10 @@ eval_syntax_highlight_add_markers (const char *text, return NULL; string_dyn_concat (value, EVAL_SYNTAX_HL_INC, -1); - string_dyn_concat (value, eval_context->prefix, -1); + string_dyn_concat (value, prefix, -1); if (text) string_dyn_concat (value, text, -1); - string_dyn_concat (value, eval_context->suffix, -1); + string_dyn_concat (value, suffix, -1); string_dyn_concat (value, EVAL_SYNTAX_HL_DEC, -1); return string_dyn_free (value, 0); @@ -1625,7 +1625,8 @@ eval_syntax_highlight (const char *text, struct t_eval_context *eval_context) */ char * -eval_replace_vars_cb (void *data, const char *text) +eval_replace_vars_cb (void *data, + const char *prefix, const char *text, const char *suffix) { struct t_eval_context *eval_context; struct t_config_option *ptr_option; @@ -1641,7 +1642,7 @@ eval_replace_vars_cb (void *data, const char *text) EVAL_DEBUG_MSG(1, "eval_replace_vars_cb(\"%s\")", text); if (eval_context->syntax_highlight) - return eval_syntax_highlight_add_markers (text, eval_context); + return eval_syntax_highlight_add_markers (prefix, text, suffix); /* raw text (no evaluation at all), with syntax highlighting */ if (strncmp (text, "raw_hl:", 7) == 0) diff --git a/src/core/wee-string.c b/src/core/wee-string.c index f1969964872..76567ae5924 100644 --- a/src/core/wee-string.c +++ b/src/core/wee-string.c @@ -4139,7 +4139,10 @@ string_replace_with_callback (const char *string, const char *prefix, const char *suffix, const char **list_prefix_no_replace, - char *(*callback)(void *data, const char *text), + char *(*callback)(void *data, + const char *prefix, + const char *text, + const char *suffix), void *callback_data, int *errors) { @@ -4196,14 +4199,6 @@ string_replace_with_callback (const char *string, } pos_end_name++; } - /* prefix without matching suffix => error! */ - if (!pos_end_name[0]) - { - result[index_result] = '\0'; - if (errors) - (*errors)++; - return result; - } key = string_strndup (string + index_string + length_prefix, pos_end_name - (string + index_string + length_prefix)); if (key) @@ -4241,7 +4236,10 @@ string_replace_with_callback (const char *string, key = key2; } } - value = (*callback) (callback_data, (key) ? key : ""); + value = (*callback) (callback_data, + prefix, + (key) ? key : "", + (pos_end_name[0]) ? suffix : ""); if (value) { length_value = strlen (value); @@ -4262,7 +4260,10 @@ string_replace_with_callback (const char *string, strcpy (result + index_result, value); index_result += length_value; } - index_string = pos_end_name - string + length_suffix; + if (pos_end_name[0]) + index_string = pos_end_name - string + length_suffix; + else + index_string = pos_end_name - string; free (value); } else diff --git a/src/core/wee-string.h b/src/core/wee-string.h index 716335e9a62..fbb63fc567d 100644 --- a/src/core/wee-string.h +++ b/src/core/wee-string.h @@ -147,7 +147,10 @@ extern char *string_replace_with_callback (const char *string, const char *prefix, const char *suffix, const char **list_prefix_no_replace, - char *(*callback)(void *data, const char *text), + char *(*callback)(void *data, + const char *prefix, + const char *text, + const char *suffix), void *callback_data, int *errors); extern void string_get_priority_and_name (const char *string, diff --git a/tests/unit/core/test-core-eval.cpp b/tests/unit/core/test-core-eval.cpp index 4c0163511d0..faae52eff59 100644 --- a/tests/unit/core/test-core-eval.cpp +++ b/tests/unit/core/test-core-eval.cpp @@ -988,7 +988,8 @@ TEST(CoreEval, EvalExpression) hashtable_set (pointers, "my_null_pointer", (const void *)0x0); hashtable_set (pointers, "my_buffer_pointer", gui_buffers); hashtable_set (pointers, "my_other_pointer", (const void *)0x1234abcd); - WEE_CHECK_EVAL("x", "x${buffer.number"); + WEE_CHECK_EVAL("x", "x${buffer.numbe"); + WEE_CHECK_EVAL("x1", "x${buffer.number"); WEE_CHECK_EVAL("x${buffer.number}1", "x\\${buffer.number}${buffer.number}"); WEE_CHECK_EVAL("1", "${buffer.number}"); diff --git a/tests/unit/core/test-core-string.cpp b/tests/unit/core/test-core-string.cpp index a3e22ee2c0d..ea760feb346 100644 --- a/tests/unit/core/test-core-string.cpp +++ b/tests/unit/core/test-core-string.cpp @@ -1308,20 +1308,23 @@ TEST(CoreString, Highlight) /* * Test callback for function string_replace_with_callback. * - * It replaces "abc" by "def", "xxx" by empty string, and for any other value + * It replaces "abc" by "def", "empty" by empty string, and for any other value * it returns NULL (so the value is kept as-is). */ char * -test_replace_cb (void *data, const char *text) +test_replace_cb (void *data, + const char *prefix, const char *text, const char *suffix) { /* make C++ compiler happy */ (void) data; + (void) prefix; + (void) suffix; if (strcmp (text, "abc") == 0) return strdup ("def"); - if (strcmp (text, "xxx") == 0) + if (strcmp (text, "empty") == 0) return strdup (""); if (strncmp (text, "no_replace:", 11) == 0) @@ -1446,13 +1449,17 @@ TEST(CoreString, ReplaceWithCallback) &test_replace_cb, NULL, &errors); WEE_REPLACE_CB("test def", 0, "test ${abc}", "${", "}", NULL, &test_replace_cb, NULL, &errors); - WEE_REPLACE_CB("test ", 0, "test ${xxx}", "${", "}", NULL, + WEE_REPLACE_CB("test ", 0, "test ${empty}", "${", "}", NULL, + &test_replace_cb, NULL, &errors); + WEE_REPLACE_CB("test ${aaa", 1, "test ${aaa", "${", "}", NULL, + &test_replace_cb, NULL, &errors); + WEE_REPLACE_CB("test ", 0, "test ${empty", "${", "}", NULL, &test_replace_cb, NULL, &errors); WEE_REPLACE_CB("test ${aaa}", 1, "test ${aaa}", "${", "}", NULL, &test_replace_cb, NULL, &errors); - WEE_REPLACE_CB("test def ${aaa}", 1, "test ${abc} ${xxx} ${aaa}", + WEE_REPLACE_CB("test def ${aaa}", 1, "test ${abc} ${empty} ${aaa}", "${", "}", NULL, &test_replace_cb, NULL, &errors); - WEE_REPLACE_CB("test ", 1, "test ${abc", "${", "}", NULL, + WEE_REPLACE_CB("test def", 0, "test ${abc", "${", "}", NULL, &test_replace_cb, NULL, &errors); WEE_REPLACE_CB("test abc}", 0, "test abc}", "${", "}", NULL, &test_replace_cb, NULL, &errors); @@ -1462,7 +1469,7 @@ TEST(CoreString, ReplaceWithCallback) &test_replace_cb, NULL, &errors); WEE_REPLACE_CB("def", 0, "${abc}", "${", "}", NULL, &test_replace_cb, NULL, &errors); - WEE_REPLACE_CB("", 0, "${xxx}", "${", "}", NULL, + WEE_REPLACE_CB("", 0, "${empty}", "${", "}", NULL, &test_replace_cb, NULL, &errors); WEE_REPLACE_CB("${aaa}", 1, "${aaa}", "${", "}", NULL, &test_replace_cb, NULL, &errors);