1
0
mirror of https://github.com/weechat/weechat.git synced 2026-07-05 17:23:15 +02:00

api: add callback in function string_replace_regex

This commit is contained in:
Sebastien Helleu
2014-03-13 10:59:58 +01:00
parent c8f574d730
commit cd65198c71
8 changed files with 103 additions and 25 deletions
+37 -6
View File
@@ -1154,7 +1154,9 @@ string_replace (const char *string, const char *search, const char *replace)
char *
string_replace_regex_get_replace (const char *string, regmatch_t *regex_match,
int last_match, const char *replace,
const char reference_char)
const char reference_char,
char *(*callback)(void *data, const char *text),
void *callback_data)
{
int length, length_current, length_add, match;
const char *ptr_replace, *ptr_add;
@@ -1208,8 +1210,26 @@ string_replace_regex_get_replace (const char *string, regmatch_t *regex_match,
}
if (regex_match[match].rm_so >= 0)
{
ptr_add = string + regex_match[match].rm_so;
length_add = regex_match[match].rm_eo - regex_match[match].rm_so;
if (callback)
{
temp = string_strndup (string + regex_match[match].rm_so,
regex_match[match].rm_eo - regex_match[match].rm_so);
if (temp)
{
modified_replace = (*callback) (callback_data, temp);
if (modified_replace)
{
ptr_add = modified_replace;
length_add = strlen (modified_replace);
}
free (temp);
}
}
if (!ptr_add)
{
ptr_add = string + regex_match[match].rm_so;
length_add = regex_match[match].rm_eo - regex_match[match].rm_so;
}
}
}
else if ((ptr_replace[1] == '.')
@@ -1308,6 +1328,11 @@ string_replace_regex_get_replace (const char *string, regmatch_t *regex_match,
* (the char '*' can be replaced by any char between space (32)
* and '~' (126))
*
* If the callback is not NULL, it is called for every reference to a match
* (except for matches replaced by a char).
* If not NULL, the string returned by the callback (which must have been newly
* allocated) is used and freed after use.
*
* Examples:
*
* string | regex | replace | result
@@ -1322,7 +1347,9 @@ string_replace_regex_get_replace (const char *string, regmatch_t *regex_match,
char *
string_replace_regex (const char *string, void *regex, const char *replace,
const char reference_char)
const char reference_char,
char *(*callback)(void *data, const char *text),
void *callback_data)
{
char *result, *result2, *str_replace;
int length, length_replace, start_offset, i, rc, end, last_match;
@@ -1373,9 +1400,13 @@ string_replace_regex (const char *string, void *regex, const char *replace,
/* check if the regex matched the end of string */
end = !result[regex_match[0].rm_eo];
str_replace = string_replace_regex_get_replace (result, regex_match,
str_replace = string_replace_regex_get_replace (result,
regex_match,
last_match,
replace, reference_char);
replace,
reference_char,
callback,
callback_data);
length_replace = (str_replace) ? strlen (str_replace) : 0;
length = regex_match[0].rm_so + length_replace +
+3 -1
View File
@@ -59,7 +59,9 @@ extern int string_has_highlight_regex_compiled (const char *string,
extern int string_has_highlight_regex (const char *string, const char *regex);
extern char *string_replace_regex (const char *string, void *regex,
const char *replace,
const char reference_char);
const char reference_char,
char *(*callback)(void *data, const char *text),
void *callback_data);
extern char **string_split (const char *string, const char *separators,
int keep_eol, int num_items_max, int *num_items);
extern char **string_split_shared (const char *string, const char *separators,
+2 -1
View File
@@ -213,7 +213,8 @@ trigger_callback_replace_regex (struct t_trigger *trigger,
value = weechat_string_replace_regex (ptr_value,
trigger->regex[i].regex,
replace_eval,
'$');
'$',
NULL, NULL);
if (value)
{
/* display debug info on trigger buffer */
+8 -4
View File
@@ -57,7 +57,7 @@ struct timeval;
* please change the date with current one; for a second change at same
* date, increment the 01, otherwise please keep 01.
*/
#define WEECHAT_PLUGIN_API_VERSION "20140311-01"
#define WEECHAT_PLUGIN_API_VERSION "20140313-01"
/* macros for defining plugin infos */
#define WEECHAT_PLUGIN_NAME(__name) \
@@ -250,7 +250,9 @@ struct t_weechat_plugin
int (*string_has_highlight_regex) (const char *string, const char *regex);
char *(*string_replace_regex) (const char *string, void *regex,
const char *replace,
const char reference_char);
const char reference_char,
char *(*callback)(void *data, const char *text),
void *callback_data);
char **(*string_split) (const char *string, const char *separators,
int keep_eol, int num_items_max, int *num_items);
char **(*string_split_shell) (const char *string, int *num_items);
@@ -1017,9 +1019,11 @@ extern int weechat_plugin_end (struct t_weechat_plugin *plugin);
#define weechat_string_has_highlight_regex(__string, __regex) \
weechat_plugin->string_has_highlight_regex(__string, __regex)
#define weechat_string_replace_regex(__string, __regex, __replace, \
__reference_char) \
__reference_char, __callback, \
__callback_data) \
weechat_plugin->string_replace_regex(__string, __regex, __replace, \
__reference_char)
__reference_char, __callback, \
__callback_data)
#define weechat_string_split(__string, __separator, __eol, __max, \
__num_items) \
weechat_plugin->string_split(__string, __separator, __eol, \