From 360f900d921bae23228b8124477e4d24dc95e190 Mon Sep 17 00:00:00 2001 From: Sebastien Helleu Date: Sat, 15 Feb 2014 12:59:05 +0100 Subject: [PATCH] core: move function string_replace before string_replace_regex --- doc/en/weechat_plugin_api.en.txt | 70 +++++++++---------- doc/fr/weechat_plugin_api.fr.txt | 70 +++++++++---------- doc/it/weechat_plugin_api.it.txt | 70 +++++++++---------- doc/ja/weechat_plugin_api.ja.txt | 70 +++++++++---------- src/core/wee-string.c | 116 +++++++++++++++---------------- 5 files changed, 198 insertions(+), 198 deletions(-) diff --git a/doc/en/weechat_plugin_api.en.txt b/doc/en/weechat_plugin_api.en.txt index 6db28bd8c..add27a47d 100644 --- a/doc/en/weechat_plugin_api.en.txt +++ b/doc/en/weechat_plugin_api.en.txt @@ -854,41 +854,6 @@ match3 = weechat.string_match("abcdef", "*def", 0) # 1 match4 = weechat.string_match("abcdef", "*de*", 0) # 1 ---- -==== weechat_string_replace - -Replace all occurrences of a string by another string. - -Prototype: - -[source,C] ----- -char *weechat_string_replace (const char *string, const char *search, - const char *replace); ----- - -Arguments: - -* 'string': string -* 'search': string to replace -* 'replace': replacement for string 'search' - -Return value: - -* string with 'search' replaced by 'replace' (must be freed by calling "free" - after use) - -C example: - -[source,C] ----- -char *str = weechat_string_replace ("test", "s", "x"); /* result: "text" */ -/* ... */ -free (str); ----- - -[NOTE] -This function is not available in scripting API. - ==== weechat_string_expand_home _WeeChat ≥ 0.3.3._ @@ -1264,6 +1229,41 @@ highlight = weechat.string_has_highlight_regex(string, regex) highlight = weechat.string_has_highlight_regex("my test string", "test|word2") # 1 ---- +==== weechat_string_replace + +Replace all occurrences of a string by another string. + +Prototype: + +[source,C] +---- +char *weechat_string_replace (const char *string, const char *search, + const char *replace); +---- + +Arguments: + +* 'string': string +* 'search': string to replace +* 'replace': replacement for string 'search' + +Return value: + +* string with 'search' replaced by 'replace' (must be freed by calling "free" + after use) + +C example: + +[source,C] +---- +char *str = weechat_string_replace ("test", "s", "x"); /* result: "text" */ +/* ... */ +free (str); +---- + +[NOTE] +This function is not available in scripting API. + ==== weechat_string_replace_regex _WeeChat ≥ 0.4.4._ diff --git a/doc/fr/weechat_plugin_api.fr.txt b/doc/fr/weechat_plugin_api.fr.txt index 117d25031..1bc4360ba 100644 --- a/doc/fr/weechat_plugin_api.fr.txt +++ b/doc/fr/weechat_plugin_api.fr.txt @@ -863,41 +863,6 @@ match3 = weechat.string_match("abcdef", "*def", 0) # 1 match4 = weechat.string_match("abcdef", "*de*", 0) # 1 ---- -==== weechat_string_replace - -Remplacer toutes les occurrences d'une chaîne par une autre chaîne. - -Prototype : - -[source,C] ----- -char *weechat_string_replace (const char *string, const char *search, - const char *replace); ----- - -Paramètres : - -* 'string' : chaîne -* 'search' : chaîne à remplacer -* 'replace' : remplacement pour la chaîne 'search' - -Valeur de retour : - -* chaîne avec 'search' remplacée par 'replace' (doit être supprimée par un appel - à "free" après utilisation) - -Exemple en C : - -[source,C] ----- -char *str = weechat_string_replace ("test, test", "s", "x"); /* résultat : "text" */ -/* ... */ -free (str); ----- - -[NOTE] -Cette fonction n'est pas disponible dans l'API script. - ==== weechat_string_expand_home _WeeChat ≥ 0.3.3._ @@ -1282,6 +1247,41 @@ highlight = weechat.string_has_highlight_regex(string, regex) highlight = weechat.string_has_highlight_regex("my test string", "test|word2") # 1 ---- +==== weechat_string_replace + +Remplacer toutes les occurrences d'une chaîne par une autre chaîne. + +Prototype : + +[source,C] +---- +char *weechat_string_replace (const char *string, const char *search, + const char *replace); +---- + +Paramètres : + +* 'string' : chaîne +* 'search' : chaîne à remplacer +* 'replace' : remplacement pour la chaîne 'search' + +Valeur de retour : + +* chaîne avec 'search' remplacée par 'replace' (doit être supprimée par un appel + à "free" après utilisation) + +Exemple en C : + +[source,C] +---- +char *str = weechat_string_replace ("test, test", "s", "x"); /* résultat : "text" */ +/* ... */ +free (str); +---- + +[NOTE] +Cette fonction n'est pas disponible dans l'API script. + ==== weechat_string_replace_regex _WeeChat ≥ 0.4.4._ diff --git a/doc/it/weechat_plugin_api.it.txt b/doc/it/weechat_plugin_api.it.txt index 3bde8d1d2..1edb181f5 100644 --- a/doc/it/weechat_plugin_api.it.txt +++ b/doc/it/weechat_plugin_api.it.txt @@ -871,41 +871,6 @@ match3 = weechat.string_match("abcdef", "*def", 0) # 1 match4 = weechat.string_match("abcdef", "*de*", 0) # 1 ---- -==== weechat_string_replace - -Sostituisce tutte le ricorrenze di una stringa con un'altra. - -Prototipo: - -[source,C] ----- -char *weechat_string_replace (const char *string, const char *search, - const char *replace); ----- - -Argomenti: - -* 'string': stringa -* 'search': stringa da sostituire -* 'replace': sostituzione per la stringa 'search' - -Valore restituito: - -* la stringa dopo 'search' sostituita da 'replace' (deve essere liberata - chiamando "free" dopo l'uso) - -Esempio in C: - -[source,C] ----- -char *str = weechat_string_replace ("test", "s", "x"); /* result: "text" */ -/* ... */ -free (str); ----- - -[NOTE] -Questa funzione non è disponibile nelle API per lo scripting. - ==== weechat_string_expand_home _WeeChat ≥ 0.3.3._ @@ -1297,6 +1262,41 @@ highlight = weechat.string_has_highlight_regex(string, regex) highlight = weechat.string_has_highlight_regex("my test string", "test|word2") # 1 ---- +==== weechat_string_replace + +Sostituisce tutte le ricorrenze di una stringa con un'altra. + +Prototipo: + +[source,C] +---- +char *weechat_string_replace (const char *string, const char *search, + const char *replace); +---- + +Argomenti: + +* 'string': stringa +* 'search': stringa da sostituire +* 'replace': sostituzione per la stringa 'search' + +Valore restituito: + +* la stringa dopo 'search' sostituita da 'replace' (deve essere liberata + chiamando "free" dopo l'uso) + +Esempio in C: + +[source,C] +---- +char *str = weechat_string_replace ("test", "s", "x"); /* result: "text" */ +/* ... */ +free (str); +---- + +[NOTE] +Questa funzione non è disponibile nelle API per lo scripting. + ==== weechat_string_replace_regex _WeeChat ≥ 0.4.4._ diff --git a/doc/ja/weechat_plugin_api.ja.txt b/doc/ja/weechat_plugin_api.ja.txt index 060bdccb3..5af1d4919 100644 --- a/doc/ja/weechat_plugin_api.ja.txt +++ b/doc/ja/weechat_plugin_api.ja.txt @@ -853,41 +853,6 @@ match3 = weechat.string_match("abcdef", "*def", 0) # 1 match4 = weechat.string_match("abcdef", "*de*", 0) # 1 ---- -==== weechat_string_replace - -マッチした全ての文字列を別の文字列で置換。 - -プロトタイプ: - -[source,C] ----- -char *weechat_string_replace (const char *string, const char *search, - const char *replace); ----- - -引数: - -* 'string': 文字列 -* 'search': マッチさせる文字列 -* 'replace': 'search' を置き換える文字列 - -戻り値: - -* 'search' を 'replace' で置き換えた文字列 - (使用後には必ず "free" を呼び出して領域を開放してください) - -C 言語での使用例: - -[source,C] ----- -char *str = weechat_string_replace ("test", "s", "x"); /* result: "text" */ -/* ... */ -free (str); ----- - -[NOTE] -スクリプト API ではこの関数を利用できません。 - ==== weechat_string_expand_home _WeeChat バージョン 0.3.3 以上で利用可。_ @@ -1264,6 +1229,41 @@ highlight = weechat.string_has_highlight_regex(string, regex) highlight = weechat.string_has_highlight_regex("my test string", "test|word2") # 1 ---- +==== weechat_string_replace + +マッチした全ての文字列を別の文字列で置換。 + +プロトタイプ: + +[source,C] +---- +char *weechat_string_replace (const char *string, const char *search, + const char *replace); +---- + +引数: + +* 'string': 文字列 +* 'search': マッチさせる文字列 +* 'replace': 'search' を置き換える文字列 + +戻り値: + +* 'search' を 'replace' で置き換えた文字列 + (使用後には必ず "free" を呼び出して領域を開放してください) + +C 言語での使用例: + +[source,C] +---- +char *str = weechat_string_replace ("test", "s", "x"); /* result: "text" */ +/* ... */ +free (str); +---- + +[NOTE] +スクリプト API ではこの関数を利用できません。 + ==== weechat_string_replace_regex _WeeChat バージョン 0.4.4 以上で利用可。_ diff --git a/src/core/wee-string.c b/src/core/wee-string.c index c6b5161c1..08fb4c7e1 100644 --- a/src/core/wee-string.c +++ b/src/core/wee-string.c @@ -448,64 +448,6 @@ string_match (const char *string, const char *mask, int case_sensitive) return 0; } -/* - * Replaces a string by new one in a string. - * - * Note: result must be freed after use. - */ - -char * -string_replace (const char *string, const char *search, const char *replace) -{ - const char *pos; - char *new_string; - int length1, length2, length_new, count; - - if (!string || !search || !replace) - return NULL; - - length1 = strlen (search); - length2 = strlen (replace); - - /* count number of strings to replace */ - count = 0; - pos = string; - while (pos && pos[0] && (pos = strstr (pos, search))) - { - count++; - pos += length1; - } - - /* easy: no string to replace! */ - if (count == 0) - return strdup (string); - - /* compute needed memory for new string */ - length_new = strlen (string) - (count * length1) + (count * length2) + 1; - - /* allocate new string */ - new_string = malloc (length_new); - if (!new_string) - return strdup (string); - - /* replace all occurrences */ - new_string[0] = '\0'; - while (string && string[0]) - { - pos = strstr (string, search); - if (pos) - { - strncat (new_string, string, pos - string); - strcat (new_string, replace); - pos += length1; - } - else - strcat (new_string, string); - string = pos; - } - return new_string; -} - /* * Expands home in a path. * @@ -1144,6 +1086,64 @@ string_has_highlight_regex (const char *string, const char *regex) return rc; } +/* + * Replaces a string by new one in a string. + * + * Note: result must be freed after use. + */ + +char * +string_replace (const char *string, const char *search, const char *replace) +{ + const char *pos; + char *new_string; + int length1, length2, length_new, count; + + if (!string || !search || !replace) + return NULL; + + length1 = strlen (search); + length2 = strlen (replace); + + /* count number of strings to replace */ + count = 0; + pos = string; + while (pos && pos[0] && (pos = strstr (pos, search))) + { + count++; + pos += length1; + } + + /* easy: no string to replace! */ + if (count == 0) + return strdup (string); + + /* compute needed memory for new string */ + length_new = strlen (string) - (count * length1) + (count * length2) + 1; + + /* allocate new string */ + new_string = malloc (length_new); + if (!new_string) + return strdup (string); + + /* replace all occurrences */ + new_string[0] = '\0'; + while (string && string[0]) + { + pos = strstr (string, search); + if (pos) + { + strncat (new_string, string, pos - string); + strcat (new_string, replace); + pos += length1; + } + else + strcat (new_string, string); + string = pos; + } + return new_string; +} + /* * Get replacement string for a regex, using array of "match" * (for more info, see function "string_replace_regex").