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

api: add function utf8_strncpy

This commit is contained in:
Sébastien Helleu
2022-12-03 16:56:01 +01:00
parent e5cbbd781d
commit 71ae8f1907
10 changed files with 238 additions and 3 deletions
+31
View File
@@ -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
+32
View File
@@ -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
+34
View File
@@ -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
+34
View File
@@ -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
+34
View File
@@ -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]]
=== Криптографија
+27
View File
@@ -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';
}
+1
View File
@@ -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 */
+1
View File
@@ -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;
+4 -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 "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, \
+40 -2
View File
@@ -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);
}