api: add "${re:repl_index}" to get the index of replacement in function string_eval_expression

This commit is contained in:
Sébastien Helleu 2021-08-29 10:40:52 +02:00
parent 009a2889e3
commit 2de272ee6c
9 changed files with 88 additions and 33 deletions

View File

@ -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)

View File

@ -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)_ |

View File

@ -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)_ |

View File

@ -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)_ |

View File

@ -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 以上で利用可)_ |

View File

@ -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)_ |

View File

@ -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;

View File

@ -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 */

View File

@ -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);