diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/core/wee-string.c | 55 | ||||
-rw-r--r-- | src/core/wee-string.h | 2 | ||||
-rw-r--r-- | src/plugins/plugin.c | 1 | ||||
-rw-r--r-- | src/plugins/weechat-plugin.h | 7 |
4 files changed, 64 insertions, 1 deletions
diff --git a/src/core/wee-string.c b/src/core/wee-string.c index cecc02f2a..1fcb18716 100644 --- a/src/core/wee-string.c +++ b/src/core/wee-string.c @@ -1903,6 +1903,61 @@ string_replace_regex (const char *string, void *regex, const char *replace, } /* + * Translates chars by other ones in a string. + * + * Note: result must be freed after use. + */ + +char * +string_translate_chars (const char *string, + const char *chars1, const char *chars2) +{ + int length, length2, translated; + const char *ptr_string, *ptr_chars1, *ptr_chars2; + char **result, *ptr_result; + + if (!string) + return NULL; + + length = (chars1) ? utf8_strlen (chars1) : 0; + length2 = (chars2) ? utf8_strlen (chars2) : 0; + + if (!chars1 || !chars2 || (length != length2)) + return strdup (string); + + result = string_dyn_alloc (strlen (string) + 1); + if (!result) + return strdup (string); + + ptr_string = string; + while (ptr_string && ptr_string[0]) + { + translated = 0; + ptr_chars1 = chars1; + ptr_chars2 = chars2; + while (ptr_chars1 && ptr_chars1[0] && ptr_chars2 && ptr_chars2[0]) + { + if (utf8_charcmp (ptr_chars1, ptr_string) == 0) + { + string_dyn_concat (result, ptr_chars2, utf8_char_size (ptr_chars2)); + translated = 1; + break; + } + ptr_chars1 = utf8_next_char (ptr_chars1); + ptr_chars2 = utf8_next_char (ptr_chars2); + } + if (!translated) + string_dyn_concat (result, ptr_string, utf8_char_size (ptr_string)); + ptr_string = utf8_next_char (ptr_string); + } + + ptr_result = *result; + string_dyn_free (result, 0); + + return ptr_result; +} + +/* * Splits a string according to separators. * * This function must not be called directly (call string_split or diff --git a/src/core/wee-string.h b/src/core/wee-string.h index f1e9109c0..62e04f76d 100644 --- a/src/core/wee-string.h +++ b/src/core/wee-string.h @@ -88,6 +88,8 @@ extern char *string_replace_regex (const char *string, void *regex, const char reference_char, char *(*callback)(void *data, const char *text), void *callback_data); +extern char *string_translate_chars (const char *string, + const char *chars1, const char *chars2); extern char **string_split (const char *string, const char *separators, const char *strip_items, int flags, int num_items_max, int *num_items); diff --git a/src/plugins/plugin.c b/src/plugins/plugin.c index 741876d12..6f2e7a5cc 100644 --- a/src/plugins/plugin.c +++ b/src/plugins/plugin.c @@ -626,6 +626,7 @@ plugin_load (const char *filename, int init_plugin, int argc, char **argv) new_plugin->string_has_highlight = &string_has_highlight; new_plugin->string_has_highlight_regex = &string_has_highlight_regex; new_plugin->string_replace_regex = &string_replace_regex; + new_plugin->string_translate_chars = &string_translate_chars; new_plugin->string_split = &string_split; new_plugin->string_split_shell = &string_split_shell; new_plugin->string_free_split = &string_free_split; diff --git a/src/plugins/weechat-plugin.h b/src/plugins/weechat-plugin.h index a0f6184ad..19c09ba37 100644 --- a/src/plugins/weechat-plugin.h +++ b/src/plugins/weechat-plugin.h @@ -68,7 +68,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 "20220926-01" +#define WEECHAT_PLUGIN_API_VERSION "20221003-01" /* macros for defining plugin infos */ #define WEECHAT_PLUGIN_NAME(__name) \ @@ -327,6 +327,8 @@ struct t_weechat_plugin char *(*callback)(void *data, const char *text), void *callback_data); + char *(*string_translate_chars) (const char *string, const char *chars1, + const char *chars2); char **(*string_split) (const char *string, const char *separators, const char *strip_items, int flags, int num_items_max, int *num_items); @@ -1282,6 +1284,9 @@ extern int weechat_plugin_end (struct t_weechat_plugin *plugin); __reference_char, \ __callback, \ __callback_data) +#define weechat_string_translate_chars(__string, __chars1, __chars2) \ + (weechat_plugin->string_translate_chars)(__string, __chars1, \ + __chars2); #define weechat_string_split(__string, __separators, __strip_items, \ __flags, __max, __num_items) \ (weechat_plugin->string_split)(__string, __separators, \ |