api: add "${re:repl_index}" to get the index of replacement in function string_eval_expression
This commit is contained in:
parent
009a2889e3
commit
2de272ee6c
@ -21,6 +21,7 @@ https://weechat.org/files/releasenotes/ReleaseNotes-devel.html[release notes]
|
|||||||
New features::
|
New features::
|
||||||
|
|
||||||
* core: add option "certs" in command /debug
|
* 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 random integer number in evaluation of expressions with "random:min,max"
|
||||||
* api: add function string_cut
|
* api: add function string_cut
|
||||||
* api: add function file_copy (issue #1667)
|
* api: add function file_copy (issue #1667)
|
||||||
|
@ -2682,21 +2682,26 @@ expanded to last):
|
|||||||
`+4+` +
|
`+4+` +
|
||||||
`+14+`
|
`+14+`
|
||||||
|
|
||||||
| `+${re:N}+` +
|
| `+${re:xxx}+` +
|
||||||
_(WeeChat ≥ 1.1)_ |
|
_(WeeChat ≥ 1.1)_ |
|
||||||
Regex captured group: `0` = whole string matching, `1` to `99` = group
|
Regex data: +
|
||||||
captured, `+++` = last group captured,
|
`0` = whole string matching, +
|
||||||
`#` = index of last group captured _(WeeChat ≥ 1.8)_. |
|
`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:0}+` +
|
||||||
`+${re:1}+` +
|
`+${re:1}+` +
|
||||||
`+${re:2}+` +
|
`+${re:2}+` +
|
||||||
`+${re:+}+` +
|
`+${re:+}+` +
|
||||||
`+${re:#}+` |
|
`+${re:#}+` +
|
||||||
|
`+${re:repl_index}+` |
|
||||||
`+test1 test2+` +
|
`+test1 test2+` +
|
||||||
`+test1+` +
|
`+test1+` +
|
||||||
`+test2+` +
|
`+test2+` +
|
||||||
`+test2+` +
|
`+test2+` +
|
||||||
`+2+`
|
`+2+` +
|
||||||
|
`+1+`
|
||||||
|
|
||||||
| `+${color:name}+` +
|
| `+${color:name}+` +
|
||||||
_(WeeChat ≥ 0.4.2)_ |
|
_(WeeChat ≥ 0.4.2)_ |
|
||||||
|
@ -2730,19 +2730,24 @@ première étendue à la dernière) :
|
|||||||
|
|
||||||
| `+${re:N}+` +
|
| `+${re:N}+` +
|
||||||
_(WeeChat ≥ 1.1)_ |
|
_(WeeChat ≥ 1.1)_ |
|
||||||
Groupe regex capturé : `0` = toute la chaîne correspondante,
|
Données sur l'expression régulière : +
|
||||||
`1` à `99` = groupe capturé, `+++` = dernier groupe capturé,
|
`0` = toute la chaîne correspondante, +
|
||||||
`#` = index du dernier groupe capturé _(WeeChat ≥ 1.8)_. |
|
`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:0}+` +
|
||||||
`+${re:1}+` +
|
`+${re:1}+` +
|
||||||
`+${re:2}+` +
|
`+${re:2}+` +
|
||||||
`+${re:+}+` +
|
`+${re:+}+` +
|
||||||
`+${re:#}+` |
|
`+${re:#}+` +
|
||||||
|
`+${re:repl_index}+` |
|
||||||
`+test1 test2+` +
|
`+test1 test2+` +
|
||||||
`+test1+` +
|
`+test1+` +
|
||||||
`+test2+` +
|
`+test2+` +
|
||||||
`+test2+` +
|
`+test2+` +
|
||||||
`+2+`
|
`+2+` +
|
||||||
|
`+1+`
|
||||||
|
|
||||||
| `+${color:nom}+` +
|
| `+${color:nom}+` +
|
||||||
_(WeeChat ≥ 0.4.2)_ |
|
_(WeeChat ≥ 0.4.2)_ |
|
||||||
|
@ -2780,21 +2780,26 @@ expanded to last):
|
|||||||
`+4+` +
|
`+4+` +
|
||||||
`+14+`
|
`+14+`
|
||||||
|
|
||||||
| `+${re:N}+` +
|
| `+${re:xxx}+` +
|
||||||
_(WeeChat ≥ 1.1)_ |
|
_(WeeChat ≥ 1.1)_ |
|
||||||
Regex captured group: `0` = whole string matching, `1` to `99` = group
|
Regex data: +
|
||||||
captured, `+++` = last group captured,
|
`0` = whole string matching, +
|
||||||
`#` = index of last group captured _(WeeChat ≥ 1.8)_. |
|
`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:0}+` +
|
||||||
`+${re:1}+` +
|
`+${re:1}+` +
|
||||||
`+${re:2}+` +
|
`+${re:2}+` +
|
||||||
`+${re:+}+` +
|
`+${re:+}+` +
|
||||||
`+${re:#}+` |
|
`+${re:#}+` +
|
||||||
|
`+${re:repl_index}+` |
|
||||||
`+test1 test2+` +
|
`+test1 test2+` +
|
||||||
`+test1+` +
|
`+test1+` +
|
||||||
`+test2+` +
|
`+test2+` +
|
||||||
`+test2+` +
|
`+test2+` +
|
||||||
`+2+`
|
`+2+` +
|
||||||
|
`+1+`
|
||||||
|
|
||||||
| `+${color:name}+` +
|
| `+${color:name}+` +
|
||||||
_(WeeChat ≥ 0.4.2)_ |
|
_(WeeChat ≥ 0.4.2)_ |
|
||||||
|
@ -2717,21 +2717,27 @@ str5 = weechat.string_eval_expression("password=abc password=def", {}, {}, optio
|
|||||||
`+4+` +
|
`+4+` +
|
||||||
`+14+`
|
`+14+`
|
||||||
|
|
||||||
| `+${re:N}+` +
|
// TRANSLATION MISSING
|
||||||
_(WeeChat バージョン 1.1 以上で利用可)_ |
|
| `+${re:xxx}+` +
|
||||||
正規表現のキャプチャグループ: `0` = マッチするすべての文字列、`1` から `99` =
|
_(WeeChat ≥ 1.1)_ |
|
||||||
キャプチャされたグループ、`+++` = 最後にキャプチャされたグループ、
|
Regex data: +
|
||||||
`#` = 最後にキャプチャされたグループのインデックス番号 _(WeeChat バージョン 1.8 以上で利用可)_ |
|
`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:0}+` +
|
||||||
`+${re:1}+` +
|
`+${re:1}+` +
|
||||||
`+${re:2}+` +
|
`+${re:2}+` +
|
||||||
`+${re:+}+` +
|
`+${re:+}+` +
|
||||||
`+${re:#}+` |
|
`+${re:#}+` +
|
||||||
|
`+${re:repl_index}+` |
|
||||||
`+test1 test2+` +
|
`+test1 test2+` +
|
||||||
`+test1+` +
|
`+test1+` +
|
||||||
`+test2+` +
|
`+test2+` +
|
||||||
`+test2+` +
|
`+test2+` +
|
||||||
`+2+`
|
`+2+` +
|
||||||
|
`+1+`
|
||||||
|
|
||||||
| `+${color:name}+` +
|
| `+${color:name}+` +
|
||||||
_(WeeChat バージョン 0.4.2 以上で利用可)_ |
|
_(WeeChat バージョン 0.4.2 以上で利用可)_ |
|
||||||
|
@ -2566,21 +2566,27 @@ str5 = weechat.string_eval_expression("password=abc password=def", {}, {}, optio
|
|||||||
`+4+` +
|
`+4+` +
|
||||||
`+14+`
|
`+14+`
|
||||||
|
|
||||||
| `+${re:N}+` +
|
// TRANSLATION MISSING
|
||||||
|
| `+${re:xxx}+` +
|
||||||
_(WeeChat ≥ 1.1)_ |
|
_(WeeChat ≥ 1.1)_ |
|
||||||
Ухваћена група у регуларном изразу: `0` = комплетан подударени стринг, `1` до `99` = ухваћена
|
Regex data: +
|
||||||
група, `+++` = последња ухваћена група,
|
`0` = whole string matching, +
|
||||||
`#` = индекс последње ухваћене групе _(WeeChat ≥ 1.8)_. |
|
`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:0}+` +
|
||||||
`+${re:1}+` +
|
`+${re:1}+` +
|
||||||
`+${re:2}+` +
|
`+${re:2}+` +
|
||||||
`+${re:+}+` +
|
`+${re:+}+` +
|
||||||
`+${re:#}+` |
|
`+${re:#}+` +
|
||||||
|
`+${re:repl_index}+` |
|
||||||
`+test1 test2+` +
|
`+test1 test2+` +
|
||||||
`+test1+` +
|
`+test1+` +
|
||||||
`+test2+` +
|
`+test2+` +
|
||||||
`+test2+` +
|
`+test2+` +
|
||||||
`+2+`
|
`+2+` +
|
||||||
|
`+1+`
|
||||||
|
|
||||||
| `+${color:име}+` +
|
| `+${color:име}+` +
|
||||||
_(WeeChat ≥ 0.4.2)_ |
|
_(WeeChat ≥ 0.4.2)_ |
|
||||||
|
@ -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)
|
if (!eval_context->regex || !eval_context->regex->result)
|
||||||
return strdup ("");
|
return strdup ("");
|
||||||
|
|
||||||
if (strcmp (text, "+") == 0)
|
if (strcmp (text, "#") == 0)
|
||||||
number = eval_context->regex->last_match;
|
|
||||||
else if (strcmp (text, "#") == 0)
|
|
||||||
{
|
{
|
||||||
snprintf (str_value, sizeof (str_value),
|
snprintf (str_value, sizeof (str_value),
|
||||||
"%d", eval_context->regex->last_match);
|
"%d", eval_context->regex->last_match);
|
||||||
return strdup (str_value);
|
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
|
else
|
||||||
{
|
{
|
||||||
number = strtol (text, &error, 10);
|
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);
|
snprintf (result, length, "%s", string);
|
||||||
|
|
||||||
eval_context->regex = &eval_regex;
|
eval_context->regex = &eval_regex;
|
||||||
|
eval_context->regex_replacement_index = 1;
|
||||||
|
|
||||||
start_offset = 0;
|
start_offset = 0;
|
||||||
|
|
||||||
@ -1984,6 +1995,8 @@ eval_replace_regex (const char *string, regex_t *regex, const char *replace,
|
|||||||
|
|
||||||
if (!result[start_offset])
|
if (!result[start_offset])
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
(eval_context->regex_replacement_index)++;
|
||||||
}
|
}
|
||||||
|
|
||||||
end:
|
end:
|
||||||
@ -2084,6 +2097,7 @@ eval_expression (const char *expr, struct t_hashtable *pointers,
|
|||||||
eval_context->prefix = default_prefix;
|
eval_context->prefix = default_prefix;
|
||||||
eval_context->suffix = default_suffix;
|
eval_context->suffix = default_suffix;
|
||||||
eval_context->regex = NULL;
|
eval_context->regex = NULL;
|
||||||
|
eval_context->regex_replacement_index = 1;
|
||||||
eval_context->recursion_count = 0;
|
eval_context->recursion_count = 0;
|
||||||
eval_context->debug_level = 0;
|
eval_context->debug_level = 0;
|
||||||
eval_context->debug_depth = 0;
|
eval_context->debug_depth = 0;
|
||||||
|
@ -77,6 +77,7 @@ struct t_eval_context
|
|||||||
const char *prefix; /* prefix (default is "${") */
|
const char *prefix; /* prefix (default is "${") */
|
||||||
const char *suffix; /* suffix (default is "}") */
|
const char *suffix; /* suffix (default is "}") */
|
||||||
struct t_eval_regex *regex; /* in case of replace with regex */
|
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 recursion_count; /* to prevent infinite recursion */
|
||||||
int debug_level; /* 0: no debug, 1: debug, 2: extra */
|
int debug_level; /* 0: no debug, 1: debug, 2: extra */
|
||||||
int debug_depth; /* used for debug indentation */
|
int debug_depth; /* used for debug indentation */
|
||||||
|
@ -989,6 +989,18 @@ TEST(CoreEval, EvalReplaceRegex)
|
|||||||
hashtable_set (options, "regex_replace", "${re:z}");
|
hashtable_set (options, "regex_replace", "${re:z}");
|
||||||
WEE_CHECK_EVAL("", "abc");
|
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 (pointers);
|
||||||
hashtable_free (extra_vars);
|
hashtable_free (extra_vars);
|
||||||
hashtable_free (options);
|
hashtable_free (options);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user