summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2022-12-03 16:56:01 +0100
committerSébastien Helleu <flashcode@flashtux.org>2022-12-10 16:05:14 +0100
commit71ae8f1907592a25d8fa6b10e02020f19c16b215 (patch)
tree42d2bccfc3f77ecca605e5441211cb827f69d8da
parente5cbbd781d814e321845598775f594f0f808e18e (diff)
downloadweechat-71ae8f1907592a25d8fa6b10e02020f19c16b215.zip
api: add function utf8_strncpy
-rw-r--r--doc/en/weechat_plugin_api.en.adoc31
-rw-r--r--doc/fr/weechat_plugin_api.fr.adoc32
-rw-r--r--doc/it/weechat_plugin_api.it.adoc34
-rw-r--r--doc/ja/weechat_plugin_api.ja.adoc34
-rw-r--r--doc/sr/weechat_plugin_api.sr.adoc34
-rw-r--r--src/core/wee-utf8.c27
-rw-r--r--src/core/wee-utf8.h1
-rw-r--r--src/plugins/plugin.c1
-rw-r--r--src/plugins/weechat-plugin.h5
-rw-r--r--tests/unit/core/test-core-utf8.cpp42
10 files changed, 238 insertions, 3 deletions
diff --git a/doc/en/weechat_plugin_api.en.adoc b/doc/en/weechat_plugin_api.en.adoc
index ae5fce9f7..3a2c5cba3 100644
--- a/doc/en/weechat_plugin_api.en.adoc
+++ b/doc/en/weechat_plugin_api.en.adoc
@@ -3792,6 +3792,37 @@ free (string);
[NOTE]
This function is not available in scripting API.
+==== utf8_strncpy
+
+_WeeChat ≥ 3.8._
+
+Copy _length_ chars max in another string and add null byte at the end.
+
+Prototype:
+
+[source,c]
+----
+void weechat_utf8_strncpy (char *dest, const char *string, int length);
+----
+
+Arguments:
+
+* _dest_: destination string (must be long enough)
+* _string_: string
+* _length_: max chars to copy
+
+C example:
+
+[source,c]
+----
+char dest[256];
+
+weechat_utf8_strncpy (dest, "chêne", 3); /* copies "chê" to dest */
+----
+
+[NOTE]
+This function is not available in scripting API.
+
[[crypto]]
=== Cryptography
diff --git a/doc/fr/weechat_plugin_api.fr.adoc b/doc/fr/weechat_plugin_api.fr.adoc
index 9025e02eb..898000318 100644
--- a/doc/fr/weechat_plugin_api.fr.adoc
+++ b/doc/fr/weechat_plugin_api.fr.adoc
@@ -3855,6 +3855,38 @@ free (str);
[NOTE]
Cette fonction n'est pas disponible dans l'API script.
+==== utf8_strncpy
+
+_WeeChat ≥ 3.8._
+
+Copier au plus _length_ caractères dans une autre chaîne et ajouter l'octet
+nul à la fin.
+
+Prototype :
+
+[source,c]
+----
+void weechat_utf8_strncpy (char *dest, const char *string, int length);
+----
+
+Paramètres :
+
+* _dest_ : chaîne de destination (doit être suffisamment grande)
+* _string_ : chaîne
+* _length_ : nombre maximum de caractères à copier
+
+Exemple en C :
+
+[source,c]
+----
+char dest[256];
+
+weechat_utf8_strncpy (dest, "chêne", 3); /* copie "chê" dans dest */
+----
+
+[NOTE]
+Cette fonction n'est pas disponible dans l'API script.
+
[[crypto]]
=== Cryptographie
diff --git a/doc/it/weechat_plugin_api.it.adoc b/doc/it/weechat_plugin_api.it.adoc
index 0e850188a..088b728c0 100644
--- a/doc/it/weechat_plugin_api.it.adoc
+++ b/doc/it/weechat_plugin_api.it.adoc
@@ -3938,6 +3938,40 @@ free (string);
[NOTE]
Questa funzione non è disponibile nelle API per lo scripting.
+==== utf8_strncpy
+
+_WeeChat ≥ 3.8._
+
+// TRANSLATION MISSING
+Copy _length_ chars max in another string and add null byte at the end.
+
+Prototipo:
+
+[source,c]
+----
+void weechat_utf8_strncpy (char *dest, const char *string, int length);
+----
+
+Argomenti:
+
+// TRANSLATION MISSING
+* _dest_: destination string (must be long enough)
+* _string_: stringa
+// TRANSLATION MISSING
+* _length_: max chars to copy
+
+Esempio in C:
+
+[source,c]
+----
+char dest[256];
+
+weechat_utf8_strncpy (dest, "chêne", 3); /* copies "chê" to dest */
+----
+
+[NOTE]
+Questa funzione non è disponibile nelle API per lo scripting.
+
// TRANSLATION MISSING
[[crypto]]
=== Cryptography
diff --git a/doc/ja/weechat_plugin_api.ja.adoc b/doc/ja/weechat_plugin_api.ja.adoc
index e23dca910..81ce7668a 100644
--- a/doc/ja/weechat_plugin_api.ja.adoc
+++ b/doc/ja/weechat_plugin_api.ja.adoc
@@ -3858,6 +3858,40 @@ free (string);
[NOTE]
スクリプト API ではこの関数を利用できません。
+==== utf8_strncpy
+
+_WeeChat ≥ 3.8._
+
+// TRANSLATION MISSING
+Copy _length_ chars max in another string and add null byte at the end.
+
+プロトタイプ:
+
+[source,c]
+----
+void weechat_utf8_strncpy (char *dest, const char *string, int length);
+----
+
+引数:
+
+// TRANSLATION MISSING
+* _dest_: destination string (must be long enough)
+* _string_: 文字列
+// TRANSLATION MISSING
+* _length_: max chars to copy
+
+C 言語での使用例:
+
+[source,c]
+----
+char dest[256];
+
+weechat_utf8_strncpy (dest, "chêne", 3); /* copies "chê" to dest */
+----
+
+[NOTE]
+スクリプト API ではこの関数を利用できません。
+
// TRANSLATION MISSING
[[crypto]]
=== Cryptography
diff --git a/doc/sr/weechat_plugin_api.sr.adoc b/doc/sr/weechat_plugin_api.sr.adoc
index 7d1dea04a..3739ddcd3 100644
--- a/doc/sr/weechat_plugin_api.sr.adoc
+++ b/doc/sr/weechat_plugin_api.sr.adoc
@@ -3663,6 +3663,40 @@ free (string);
[NOTE]
Ова функција није доступна у API скриптовања.
+==== utf8_strncpy
+
+_WeeChat ≥ 3.8._
+
+// TRANSLATION MISSING
+Copy _length_ chars max in another string and add null byte at the end.
+
+Прототип:
+
+[source,c]
+----
+void weechat_utf8_strncpy (char *dest, const char *string, int length);
+----
+
+Аргументи:
+
+// TRANSLATION MISSING
+* _dest_: destination string (must be long enough)
+* _string_: стринг
+// TRANSLATION MISSING
+* _length_: max chars to copy
+
+C пример:
+
+[source,c]
+----
+char dest[256];
+
+weechat_utf8_strncpy (dest, "chêne", 3); /* copies "chê" to dest */
+----
+
+[NOTE]
+Ова функција није доступна у API скриптовања.
+
[[crypto]]
=== Криптографија
diff --git a/src/core/wee-utf8.c b/src/core/wee-utf8.c
index 78f717817..6f6526319 100644
--- a/src/core/wee-utf8.c
+++ b/src/core/wee-utf8.c
@@ -738,3 +738,30 @@ utf8_strndup (const char *string, int length)
return string_strndup (string, end - string);
}
+
+/*
+ * Copies max N chars from a string to another and adds null byte at the end.
+ *
+ * Note: the target string "dest" must be long enough.
+ */
+
+void
+utf8_strncpy (char *dest, const char *string, int length)
+{
+ const char *end;
+
+ if (!dest)
+ return;
+
+ dest[0] = '\0';
+
+ if (!string || (length <= 0))
+ return;
+
+ end = utf8_add_offset (string, length);
+ if (!end || (end == string))
+ return;
+
+ memcpy (dest, string, end - string);
+ dest[end - string] = '\0';
+}
diff --git a/src/core/wee-utf8.h b/src/core/wee-utf8.h
index 5a228cc0f..85259ef82 100644
--- a/src/core/wee-utf8.h
+++ b/src/core/wee-utf8.h
@@ -51,5 +51,6 @@ extern const char *utf8_add_offset (const char *string, int offset);
extern int utf8_real_pos (const char *string, int pos);
extern int utf8_pos (const char *string, int real_pos);
extern char *utf8_strndup (const char *string, int length);
+extern void utf8_strncpy (char *dest, const char *string, int length);
#endif /* WEECHAT_UTF8_H */
diff --git a/src/plugins/plugin.c b/src/plugins/plugin.c
index 6f2e7a5cc..b9b7584fb 100644
--- a/src/plugins/plugin.c
+++ b/src/plugins/plugin.c
@@ -665,6 +665,7 @@ plugin_load (const char *filename, int init_plugin, int argc, char **argv)
new_plugin->utf8_real_pos = &utf8_real_pos;
new_plugin->utf8_pos = &utf8_pos;
new_plugin->utf8_strndup = &utf8_strndup;
+ new_plugin->utf8_strncpy = &utf8_strncpy;
new_plugin->crypto_hash = &plugin_api_crypto_hash;
new_plugin->crypto_hash_file = &plugin_api_crypto_hash_file;
diff --git a/src/plugins/weechat-plugin.h b/src/plugins/weechat-plugin.h
index 19c09ba37..551f14008 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 "20221003-01"
+#define WEECHAT_PLUGIN_API_VERSION "20221203-01"
/* macros for defining plugin infos */
#define WEECHAT_PLUGIN_NAME(__name) \
@@ -379,6 +379,7 @@ struct t_weechat_plugin
int (*utf8_real_pos) (const char *string, int pos);
int (*utf8_pos) (const char *string, int real_pos);
char *(*utf8_strndup) (const char *string, int length);
+ void (*utf8_strncpy) (char *dest, const char *string, int length);
/* crypto */
int (*crypto_hash) (const void *data, int data_size,
@@ -1378,6 +1379,8 @@ extern int weechat_plugin_end (struct t_weechat_plugin *plugin);
(weechat_plugin->utf8_pos)(__string, __real_pos)
#define weechat_utf8_strndup(__string, __length) \
(weechat_plugin->utf8_strndup)(__string, __length)
+#define weechat_utf8_strncpy(__dest, __string, __length) \
+ (weechat_plugin->utf8_strncpy)(__dest, __string, __length)
/* crypto */
#define weechat_crypto_hash(__data, __data_size, __hash_algo, \
diff --git a/tests/unit/core/test-core-utf8.cpp b/tests/unit/core/test-core-utf8.cpp
index 0bd8e015b..2bcb6f3f0 100644
--- a/tests/unit/core/test-core-utf8.cpp
+++ b/tests/unit/core/test-core-utf8.cpp
@@ -32,6 +32,18 @@ extern "C"
#include "src/core/wee-config.h"
}
+#define TEST_STRNCPY(__result, __dest, __string, __length) \
+ if (__dest != NULL) \
+ { \
+ dest[0] = '\x01'; \
+ dest[1] = '\0'; \
+ } \
+ utf8_strncpy (__dest, __string, __length); \
+ if (__dest != NULL) \
+ { \
+ STRCMP_EQUAL(__result, __dest); \
+ }
+
/*
* soft hyphen:
* [­]
@@ -303,7 +315,6 @@ TEST(CoreUtf8, Move)
const char *utf8_4bytes_truncated_1 = UTF8_4BYTES_TRUNCATED_1;
const char *utf8_4bytes_truncated_2 = UTF8_4BYTES_TRUNCATED_2;
const char *utf8_4bytes_truncated_3 = UTF8_4BYTES_TRUNCATED_3;
-
const char *han_char = UNICODE_HAN_CHAR;
/* previous/next char */
@@ -385,7 +396,6 @@ TEST(CoreUtf8, Convert)
const char *utf8_4bytes_truncated_1 = UTF8_4BYTES_TRUNCATED_1;
const char *utf8_4bytes_truncated_2 = UTF8_4BYTES_TRUNCATED_2;
const char *utf8_4bytes_truncated_3 = UTF8_4BYTES_TRUNCATED_3;
-
char result[5];
/* get UTF-8 char as integer */
@@ -619,3 +629,31 @@ TEST(CoreUtf8, Duplicate)
WEE_TEST_STR("noël", utf8_strndup (UTF8_NOEL_VALID, 4));
WEE_TEST_STR("noël", utf8_strndup (UTF8_NOEL_VALID, 5));
}
+
+/*
+ * Tests functions:
+ * utf8_strncpy
+ */
+
+TEST(CoreUtf8, Copy)
+{
+ char dest[256];
+
+ /* invalid parameters */
+ TEST_STRNCPY("", NULL, NULL, -1);
+ TEST_STRNCPY("", dest, NULL, -1);
+ TEST_STRNCPY("", dest, "abc", -1);
+
+ TEST_STRNCPY("", dest, "abc", 0);
+ TEST_STRNCPY("a", dest, "abc", 1);
+ TEST_STRNCPY("ab", dest, "abc", 2);
+ TEST_STRNCPY("abc", dest, "abc", 3);
+ TEST_STRNCPY("abc", dest, "abc", 4);
+
+ TEST_STRNCPY("", dest, UTF8_NOEL_VALID, 0);
+ TEST_STRNCPY("n", dest, UTF8_NOEL_VALID, 1);
+ TEST_STRNCPY("no", dest, UTF8_NOEL_VALID, 2);
+ TEST_STRNCPY("noë", dest, UTF8_NOEL_VALID, 3);
+ TEST_STRNCPY("noël", dest, UTF8_NOEL_VALID, 4);
+ TEST_STRNCPY("noël", dest, UTF8_NOEL_VALID, 5);
+}