summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2022-10-03 20:07:08 +0200
committerSébastien Helleu <flashcode@flashtux.org>2022-11-05 22:34:38 +0100
commitbc2fb071e22589aa219ce551b0112bacbd3cca8f (patch)
tree46885363dbf95737baf92331f6187ecefce55d4a
parent03899e5ea5ed408d334c203ff535d02d1a8b5b4a (diff)
downloadweechat-bc2fb071e22589aa219ce551b0112bacbd3cca8f.zip
api: add function string_translate_chars
-rw-r--r--doc/en/weechat_plugin_api.en.adoc39
-rw-r--r--doc/fr/weechat_plugin_api.fr.adoc39
-rw-r--r--doc/it/weechat_plugin_api.it.adoc42
-rw-r--r--doc/ja/weechat_plugin_api.ja.adoc41
-rw-r--r--doc/sr/weechat_plugin_api.sr.adoc42
-rw-r--r--src/core/wee-string.c55
-rw-r--r--src/core/wee-string.h2
-rw-r--r--src/plugins/plugin.c1
-rw-r--r--src/plugins/weechat-plugin.h7
-rw-r--r--tests/unit/core/test-core-string.cpp32
10 files changed, 299 insertions, 1 deletions
diff --git a/doc/en/weechat_plugin_api.en.adoc b/doc/en/weechat_plugin_api.en.adoc
index c72bebe2f..317dbfaf1 100644
--- a/doc/en/weechat_plugin_api.en.adoc
+++ b/doc/en/weechat_plugin_api.en.adoc
@@ -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._
diff --git a/doc/fr/weechat_plugin_api.fr.adoc b/doc/fr/weechat_plugin_api.fr.adoc
index 7f738eca8..fcb24bb6d 100644
--- a/doc/fr/weechat_plugin_api.fr.adoc
+++ b/doc/fr/weechat_plugin_api.fr.adoc
@@ -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._
diff --git a/doc/it/weechat_plugin_api.it.adoc b/doc/it/weechat_plugin_api.it.adoc
index 78cf72caa..14c509a51 100644
--- a/doc/it/weechat_plugin_api.it.adoc
+++ b/doc/it/weechat_plugin_api.it.adoc
@@ -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
diff --git a/doc/ja/weechat_plugin_api.ja.adoc b/doc/ja/weechat_plugin_api.ja.adoc
index 2eec03bc0..67ac48c06 100644
--- a/doc/ja/weechat_plugin_api.ja.adoc
+++ b/doc/ja/weechat_plugin_api.ja.adoc
@@ -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 で更新。_
diff --git a/doc/sr/weechat_plugin_api.sr.adoc b/doc/sr/weechat_plugin_api.sr.adoc
index b46cc19db..183877476 100644
--- a/doc/sr/weechat_plugin_api.sr.adoc
+++ b/doc/sr/weechat_plugin_api.sr.adoc
@@ -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._
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, \
diff --git a/tests/unit/core/test-core-string.cpp b/tests/unit/core/test-core-string.cpp
index 16ccf36a1..44bdd3ce2 100644
--- a/tests/unit/core/test-core-string.cpp
+++ b/tests/unit/core/test-core-string.cpp
@@ -1174,6 +1174,38 @@ TEST(CoreString, ReplaceRegex)
/*
* 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
*/