diff options
-rw-r--r-- | ChangeLog.adoc | 1 | ||||
-rw-r--r-- | doc/en/weechat_plugin_api.en.adoc | 17 | ||||
-rw-r--r-- | doc/fr/weechat_plugin_api.fr.adoc | 15 | ||||
-rw-r--r-- | doc/it/weechat_plugin_api.it.adoc | 17 | ||||
-rw-r--r-- | doc/ja/weechat_plugin_api.ja.adoc | 20 | ||||
-rw-r--r-- | doc/sr/weechat_plugin_api.sr.adoc | 18 | ||||
-rw-r--r-- | src/core/wee-eval.c | 20 | ||||
-rw-r--r-- | src/core/wee-eval.h | 1 | ||||
-rw-r--r-- | tests/unit/core/test-core-eval.cpp | 12 |
9 files changed, 88 insertions, 33 deletions
diff --git a/ChangeLog.adoc b/ChangeLog.adoc index 4de1342b9..a0eed13ea 100644 --- a/ChangeLog.adoc +++ b/ChangeLog.adoc @@ -21,6 +21,7 @@ https://weechat.org/files/releasenotes/ReleaseNotes-devel.html[release notes] New features:: * core: add option "certs" in command /debug + * api: add `${re:repl_index}` to get the index of replacement in function string_eval_expression * api: add random integer number in evaluation of expressions with "random:min,max" * api: add function string_cut * api: add function file_copy (issue #1667) diff --git a/doc/en/weechat_plugin_api.en.adoc b/doc/en/weechat_plugin_api.en.adoc index f1751f4f7..085e0c845 100644 --- a/doc/en/weechat_plugin_api.en.adoc +++ b/doc/en/weechat_plugin_api.en.adoc @@ -2682,21 +2682,26 @@ expanded to last): `+4+` + `+14+` -| `+${re:N}+` + +| `+${re:xxx}+` + _(WeeChat ≥ 1.1)_ | - Regex captured group: `0` = whole string matching, `1` to `99` = group - captured, `+++` = last group captured, - `#` = index of last group captured _(WeeChat ≥ 1.8)_. | + Regex data: + + `0` = whole string matching, + + `1` to `99` = group captured, + + `+++` = last group captured, + + `#` = index of last group captured _(WeeChat ≥ 1.8)_, + + `repl_index` = index of replacement being done (starts to 1) _(WeeChat ≥ 3.3)_. | `+${re:0}+` + `+${re:1}+` + `+${re:2}+` + `+${re:+}+` + - `+${re:#}+` | + `+${re:#}+` + + `+${re:repl_index}+` | `+test1 test2+` + `+test1+` + `+test2+` + `+test2+` + - `+2+` + `+2+` + + `+1+` | `+${color:name}+` + _(WeeChat ≥ 0.4.2)_ | diff --git a/doc/fr/weechat_plugin_api.fr.adoc b/doc/fr/weechat_plugin_api.fr.adoc index b7b6b777a..1fe87da91 100644 --- a/doc/fr/weechat_plugin_api.fr.adoc +++ b/doc/fr/weechat_plugin_api.fr.adoc @@ -2730,19 +2730,24 @@ première étendue à la dernière) : | `+${re:N}+` + _(WeeChat ≥ 1.1)_ | - Groupe regex capturé : `0` = toute la chaîne correspondante, - `1` à `99` = groupe capturé, `+++` = dernier groupe capturé, - `#` = index du dernier groupe capturé _(WeeChat ≥ 1.8)_. | + Données sur l'expression régulière : + + `0` = toute la chaîne correspondante, + + `1` à `99` = groupe capturé, + + `+++` = dernier groupe capturé, + + `#` = index du dernier groupe capturé _(WeeChat ≥ 1.8)_ + + `repl_index` = index du remplacement en cours (démarre à 1) _(WeeChat ≥ 3.3)_. | `+${re:0}+` + `+${re:1}+` + `+${re:2}+` + `+${re:+}+` + - `+${re:#}+` | + `+${re:#}+` + + `+${re:repl_index}+` | `+test1 test2+` + `+test1+` + `+test2+` + `+test2+` + - `+2+` + `+2+` + + `+1+` | `+${color:nom}+` + _(WeeChat ≥ 0.4.2)_ | diff --git a/doc/it/weechat_plugin_api.it.adoc b/doc/it/weechat_plugin_api.it.adoc index 671eb2fbf..17d4f2336 100644 --- a/doc/it/weechat_plugin_api.it.adoc +++ b/doc/it/weechat_plugin_api.it.adoc @@ -2780,21 +2780,26 @@ expanded to last): `+4+` + `+14+` -| `+${re:N}+` + +| `+${re:xxx}+` + _(WeeChat ≥ 1.1)_ | - Regex captured group: `0` = whole string matching, `1` to `99` = group - captured, `+++` = last group captured, - `#` = index of last group captured _(WeeChat ≥ 1.8)_. | + Regex data: + + `0` = whole string matching, + + `1` to `99` = group captured, + + `+++` = last group captured, + + `#` = index of last group captured _(WeeChat ≥ 1.8)_, + + `repl_index` = index of replacement being done (starts to 1) _(WeeChat ≥ 3.3)_. | `+${re:0}+` + `+${re:1}+` + `+${re:2}+` + `+${re:+}+` + - `+${re:#}+` | + `+${re:#}+` + + `+${re:repl_index}+` | `+test1 test2+` + `+test1+` + `+test2+` + `+test2+` + - `+2+` + `+2+` + + `+1+` | `+${color:name}+` + _(WeeChat ≥ 0.4.2)_ | diff --git a/doc/ja/weechat_plugin_api.ja.adoc b/doc/ja/weechat_plugin_api.ja.adoc index 60b94aa98..ef33a39e8 100644 --- a/doc/ja/weechat_plugin_api.ja.adoc +++ b/doc/ja/weechat_plugin_api.ja.adoc @@ -2717,21 +2717,27 @@ str5 = weechat.string_eval_expression("password=abc password=def", {}, {}, optio `+4+` + `+14+` -| `+${re:N}+` + - _(WeeChat バージョン 1.1 以上で利用可)_ | - 正規表現のキャプチャグループ: `0` = マッチするすべての文字列、`1` から `99` = - キャプチャされたグループ、`+++` = 最後にキャプチャされたグループ、 - `#` = 最後にキャプチャされたグループのインデックス番号 _(WeeChat バージョン 1.8 以上で利用可)_ | +// TRANSLATION MISSING +| `+${re:xxx}+` + + _(WeeChat ≥ 1.1)_ | + Regex data: + + `0` = whole string matching, + + `1` to `99` = group captured, + + `+++` = last group captured, + + `#` = index of last group captured _(WeeChat ≥ 1.8)_, + + `repl_index` = index of replacement being done (starts to 1) _(WeeChat ≥ 3.3)_. | `+${re:0}+` + `+${re:1}+` + `+${re:2}+` + `+${re:+}+` + - `+${re:#}+` | + `+${re:#}+` + + `+${re:repl_index}+` | `+test1 test2+` + `+test1+` + `+test2+` + `+test2+` + - `+2+` + `+2+` + + `+1+` | `+${color:name}+` + _(WeeChat バージョン 0.4.2 以上で利用可)_ | diff --git a/doc/sr/weechat_plugin_api.sr.adoc b/doc/sr/weechat_plugin_api.sr.adoc index 5a6c71624..4eec73132 100644 --- a/doc/sr/weechat_plugin_api.sr.adoc +++ b/doc/sr/weechat_plugin_api.sr.adoc @@ -2566,21 +2566,27 @@ str5 = weechat.string_eval_expression("password=abc password=def", {}, {}, optio `+4+` + `+14+` -| `+${re:N}+` + +// TRANSLATION MISSING +| `+${re:xxx}+` + _(WeeChat ≥ 1.1)_ | - Ухваћена група у регуларном изразу: `0` = комплетан подударени стринг, `1` до `99` = ухваћена - група, `+++` = последња ухваћена група, - `#` = индекс последње ухваћене групе _(WeeChat ≥ 1.8)_. | + Regex data: + + `0` = whole string matching, + + `1` to `99` = group captured, + + `+++` = last group captured, + + `#` = index of last group captured _(WeeChat ≥ 1.8)_, + + `repl_index` = index of replacement being done (starts to 1) _(WeeChat ≥ 3.3)_. | `+${re:0}+` + `+${re:1}+` + `+${re:2}+` + `+${re:+}+` + - `+${re:#}+` | + `+${re:#}+` + + `+${re:repl_index}+` | `+test1 test2+` + `+test1+` + `+test2+` + `+test2+` + - `+2+` + `+2+` + + `+1+` | `+${color:име}+` + _(WeeChat ≥ 0.4.2)_ | diff --git a/src/core/wee-eval.c b/src/core/wee-eval.c index 397b951ce..e60c23928 100644 --- a/src/core/wee-eval.c +++ b/src/core/wee-eval.c @@ -416,14 +416,24 @@ eval_string_regex_group (const char *text, struct t_eval_context *eval_context) if (!eval_context->regex || !eval_context->regex->result) return strdup (""); - if (strcmp (text, "+") == 0) - number = eval_context->regex->last_match; - else if (strcmp (text, "#") == 0) + if (strcmp (text, "#") == 0) { snprintf (str_value, sizeof (str_value), "%d", eval_context->regex->last_match); return strdup (str_value); } + + if (strcmp (text, "repl_index") == 0) + { + snprintf (str_value, sizeof (str_value), + "%d", eval_context->regex_replacement_index); + return strdup (str_value); + } + + if (strcmp (text, "+") == 0) + { + number = eval_context->regex->last_match; + } else { number = strtol (text, &error, 10); @@ -1901,6 +1911,7 @@ eval_replace_regex (const char *string, regex_t *regex, const char *replace, snprintf (result, length, "%s", string); eval_context->regex = &eval_regex; + eval_context->regex_replacement_index = 1; start_offset = 0; @@ -1984,6 +1995,8 @@ eval_replace_regex (const char *string, regex_t *regex, const char *replace, if (!result[start_offset]) break; + + (eval_context->regex_replacement_index)++; } end: @@ -2084,6 +2097,7 @@ eval_expression (const char *expr, struct t_hashtable *pointers, eval_context->prefix = default_prefix; eval_context->suffix = default_suffix; eval_context->regex = NULL; + eval_context->regex_replacement_index = 1; eval_context->recursion_count = 0; eval_context->debug_level = 0; eval_context->debug_depth = 0; diff --git a/src/core/wee-eval.h b/src/core/wee-eval.h index 0fd329717..5c97b7a31 100644 --- a/src/core/wee-eval.h +++ b/src/core/wee-eval.h @@ -77,6 +77,7 @@ struct t_eval_context const char *prefix; /* prefix (default is "${") */ const char *suffix; /* suffix (default is "}") */ struct t_eval_regex *regex; /* in case of replace with regex */ + int regex_replacement_index; /* replacement index (≥ 1) */ int recursion_count; /* to prevent infinite recursion */ int debug_level; /* 0: no debug, 1: debug, 2: extra */ int debug_depth; /* used for debug indentation */ diff --git a/tests/unit/core/test-core-eval.cpp b/tests/unit/core/test-core-eval.cpp index d655dca4b..cbd57e8fd 100644 --- a/tests/unit/core/test-core-eval.cpp +++ b/tests/unit/core/test-core-eval.cpp @@ -989,6 +989,18 @@ TEST(CoreEval, EvalReplaceRegex) hashtable_set (options, "regex_replace", "${re:z}"); WEE_CHECK_EVAL("", "abc"); + /* use replace index: add number before each item */ + hashtable_remove (pointers, "regex"); + hashtable_set (options, "regex", "[^,]+"); + hashtable_set (options, "regex_replace", "${re:repl_index}.${re:0}"); + WEE_CHECK_EVAL("1.item1,2.item2,3.item3", "item1,item2,item3"); + + /* use replace index: replace each letter by its position */ + hashtable_remove (pointers, "regex"); + hashtable_set (options, "regex", "."); + hashtable_set (options, "regex_replace", "${re:repl_index}"); + WEE_CHECK_EVAL("1234", "test"); + hashtable_free (pointers); hashtable_free (extra_vars); hashtable_free (options); |