1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-29 14:26:39 +02:00

api: add function string_translate_chars

This commit is contained in:
Sébastien Helleu
2022-10-03 20:07:08 +02:00
parent 03899e5ea5
commit bc2fb071e2
10 changed files with 299 additions and 1 deletions
+39
View File
@@ -1587,6 +1587,45 @@ if (weechat_string_regcomp (&my_regex, "([0-9]{4})-([0-9]{2})-([0-9]{2})",
[NOTE]
This function is not available in scripting API.
==== string_translate_chars
_WeeChat ≥ 3.8._
Translate chars in a string.
Prototype:
[source,c]
----
char *string_translate_chars (const char *string, const char *chars1, const char *chars2);
----
Arguments:
* _string_: string
* _chars1_: string with chars to translate
* _chars2_: string with replacement chars; it must contain the same number of
UTF-8 chars than _chars1_
Return value:
* string with translated chars, NULL if problem (must be freed by calling "free"
after use)
C examples:
[source,c]
----
/* "test" => "tEst" */
char *str = weechat_string_translate_chars ("test", "abcdef", "ABCDEF");
/* "clean the boat" => "CleAn the BoAt" */
char *str = weechat_string_translate_chars ("clean the boat", "abc", "ABC");
----
[NOTE]
This function is not available in scripting API.
==== string_split
_Updated in 2.5, 2.6._
+39
View File
@@ -1615,6 +1615,45 @@ if (weechat_string_regcomp (&my_regex, "([0-9]{4})-([0-9]{2})-([0-9]{2})",
[NOTE]
Cette fonction n'est pas disponible dans l'API script.
==== string_translate_chars
_WeeChat ≥ 3.8._
Traduire des caractères dans une chaîne.
Prototype :
[source,c]
----
char *string_translate_chars (const char *string, const char *chars1, const char *chars2);
----
Paramètres :
* _string_ : chaîne
* _chars1_ : chaîne avec des caractères à traduire
* _chars2_ : chaîne avec les caractères de remplacement ; elle doit contenir
le même nombre de caractères UTF-8 que _chars1_
Valeur de retour :
* chaîne avec les caractères traduits, NULL en cas de problème (doit être
supprimée par un appel à "free" après utilisation)
Exemples en C :
[source,c]
----
/* "test" => "tEst" */
char *str = weechat_string_translate_chars ("test", "abcdef", "ABCDEF");
/* "clean the boat" => "CleAn the BoAt" */
char *str = weechat_string_translate_chars ("clean the boat", "abc", "ABC");
----
[NOTE]
Cette fonction n'est pas disponible dans l'API script.
==== string_split
_Mis à jour dans la 2.5, 2.6._
+42
View File
@@ -1665,6 +1665,48 @@ if (weechat_string_regcomp (&my_regex, "([0-9]{4})-([0-9]{2})-([0-9]{2})",
[NOTE]
Questa funzione non è disponibile nelle API per lo scripting.
==== string_translate_chars
_WeeChat ≥ 3.8._
// TRANSLATION MISSING
Translate chars in a string.
Prototipo:
[source,c]
----
char *string_translate_chars (const char *string, const char *chars1, const char *chars2);
----
Argomenti:
// TRANSLATION MISSING
* _string_: string
* _chars1_: string with chars to translate
* _chars2_: string with replacement chars; it must contain the same number of
UTF-8 chars than _chars1_
Valore restituito:
// TRANSLATION MISSING
* string with translated chars, NULL if problem (must be freed by calling "free"
after use)
Esempi in C:
[source,c]
----
/* "test" => "tEst" */
char *str = weechat_string_translate_chars ("test", "abcdef", "ABCDEF");
/* "clean the boat" => "CleAn the BoAt" */
char *str = weechat_string_translate_chars ("clean the boat", "abc", "ABC");
----
[NOTE]
Questa funzione non è disponibile nelle API per lo scripting.
==== string_split
// TRANSLATION MISSING
+41
View File
@@ -1605,6 +1605,47 @@ if (weechat_string_regcomp (&my_regex, "([0-9]{4})-([0-9]{2})-([0-9]{2})",
[NOTE]
スクリプト API ではこの関数を利用できません。
==== string_translate_chars
_WeeChat バージョン 3.8 以上で利用可。_
// TRANSLATION MISSING
Translate chars in a string.
プロトタイプ:
[source,c]
----
char *string_translate_chars (const char *string, const char *chars1, const char *chars2);
----
引数:
// TRANSLATION MISSING
* _string_: string
* _chars1_: string with chars to translate
* _chars2_: string with replacement chars; it must contain the same number of
UTF-8 chars than _chars1_
戻り値:
* string with translated chars, NULL if problem (must be freed by calling "free"
after use)
C 言語での使用例:
[source,c]
----
/* "test" => "tEst" */
char *str = weechat_string_translate_chars ("test", "abcdef", "ABCDEF");
/* "clean the boat" => "CleAn the BoAt" */
char *str = weechat_string_translate_chars ("clean the boat", "abc", "ABC");
----
[NOTE]
スクリプト API ではこの関数を利用できません。
==== string_split
_WeeChat バージョン 2.5、2.6 で更新。_
+42
View File
@@ -1520,6 +1520,48 @@ if (weechat_string_regcomp (&my_regex, "([0-9]{4})-([0-9]{2})-([0-9]{2})",
[NOTE]
Ова функција није доступна у API скриптовања.
==== string_translate_chars
_WeeChat ≥ 3.8._
// TRANSLATION MISSING
Translate chars in a string.
Прототип:
[source,c]
----
char *string_translate_chars (const char *string, const char *chars1, const char *chars2);
----
Аргументи:
// TRANSLATION MISSING
* _string_: string
* _chars1_: string with chars to translate
* _chars2_: string with replacement chars; it must contain the same number of
UTF-8 chars than _chars1_
Повратна вредност:
// TRANSLATION MISSING
* string with translated chars, NULL if problem (must be freed by calling "free"
after use)
C примери:
[source,c]
----
/* "test" => "tEst" */
char *str = weechat_string_translate_chars ("test", "abcdef", "ABCDEF");
/* "clean the boat" => "CleAn the BoAt" */
char *str = weechat_string_translate_chars ("clean the boat", "abc", "ABC");
----
[NOTE]
Ова функција није доступна у API скриптовања.
==== string_split
_Ажурирано у верзијама 2.5, 2.6._
+55
View File
@@ -1902,6 +1902,61 @@ string_replace_regex (const char *string, void *regex, const char *replace,
return result;
}
/*
* 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.
*
+2
View File
@@ -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);
+1
View File
@@ -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;
+6 -1
View File
@@ -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, \
+32
View File
@@ -1172,6 +1172,38 @@ TEST(CoreString, ReplaceRegex)
"^(test +)(.*)", "$.%+", '$', NULL);
}
/*
* Tests functions:
* string_translate_chars
*/
TEST(CoreString, TranslateChars)
{
char *str;
POINTERS_EQUAL(NULL, string_translate_chars (NULL, NULL, NULL));
POINTERS_EQUAL(NULL, string_translate_chars (NULL, "abc", NULL));
POINTERS_EQUAL(NULL, string_translate_chars (NULL, "abc", "ABC"));
STRCMP_EQUAL("", string_translate_chars ("", "abc", "ABCDEF"));
STRCMP_EQUAL("test", string_translate_chars ("test", "abc", "ABCDEF"));
WEE_TEST_STR("", string_translate_chars ("", "abc", "ABC"));
WEE_TEST_STR("tEst", string_translate_chars ("test", "abcdef", "ABCDEF"));
WEE_TEST_STR("CleAn the BoAt",
string_translate_chars ("clean the boat", "abc", "ABC"));
WEE_TEST_STR("", string_translate_chars ("", "←↑→↓", "↑→↓←"));
WEE_TEST_STR("", string_translate_chars ("", "←↑→↓", "↑→↓←"));
WEE_TEST_STR("", string_translate_chars ("", "←↑→↓", "↑→↓←"));
WEE_TEST_STR("", string_translate_chars ("", "←↑→↓", "↑→↓←"));
WEE_TEST_STR("uijt jt b uftu",
string_translate_chars ("this is a test",
"abcdefghijklmnopqrstuvwxyz",
"bcdefghijklmnopqrstuvwxyza"));
}
/*
* Tests functions:
* string_replace_with_callback