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

api: add function string_concat (issue #2005)

This commit is contained in:
Sébastien Helleu
2023-09-02 11:53:56 +02:00
parent 9bc9df47d7
commit e34071131e
12 changed files with 334 additions and 2 deletions
+1 -1
View File
@@ -37,7 +37,7 @@ New features::
* core: add options weechat.buffer.* to save buffer properties set by user, add option `setauto` in command `/buffer` (issue #352)
* core: add parameters and key bindings to move to edges of current area with commands `/cursor go` and `/cursor move` (issue #1282)
* core: add variables "_chat_focused_line_bol" and "_chat_focused_line_eol" in focus data (issue #1955)
* api: add function hook_url, add option `url` in command `/debug` (issue #1723)
* api: add function string_concat (issue #2005)
* api: add support of path to variable and hashtable comparison in function hdata_compare (issue #1066)
* api: add infos "nick_color_ignore_case" and "nick_color_name_ignore_case" (issue #194)
* api: add info "buffer" (issue #1962)
+40
View File
@@ -3476,6 +3476,46 @@ weechat_string_dyn_free (string, 1);
[NOTE]
This function is not available in scripting API.
==== string_concat
_WeeChat ≥ 4.2.0._
Concatenate multiple strings using a separator.
Prototype:
[source,c]
----
const char *weechat_string_concat (const char *separator, ...);
----
Arguments:
* _separator_: the separator string which is inserted between concatenated
strings (can be NULL or empty string)
[NOTE]
Last argument *MUST* always be NULL. +
A macro called `WEECHAT_STR_CONCAT` can be used, where the final NULL value is
not needed (usage of this macro is recommended).
Return value:
* concatenated string
C example:
[source,c]
----
const char *result = weechat_string_concat (" / ", "abc", "def", "ghi", NULL); /* result == "abc / def / ghi" */
/* with macro */
const char *result = WEECHAT_STR_CONCAT(" / ", "abc", "def", "ghi"); /* result == "abc / def / ghi" */
----
[NOTE]
This function is not available in scripting API.
[[utf-8]]
=== UTF-8
+40
View File
@@ -3533,6 +3533,46 @@ weechat_string_dyn_free (string, 1);
[NOTE]
Cette fonction n'est pas disponible dans l'API script.
==== string_concat
_WeeChat ≥ 4.2.0._
Concaténer plusieurs chaînes de caractères en utilisant un séparateur.
Prototype :
[source,c]
----
const char *weechat_string_concat (const char *separator, ...);
----
Paramètres :
* _separator_ : la chaîne de séparation qui est insérée entre les chaînes
concaténées (peut être NULL ou une chaîne vide)
[NOTE]
Le dernier paramètre *DOIT* toujours être NULL. +
Une macro nommée `WEECHAT_STR_CONCAT` peut être utilisée, où la valeur finale
NULL n'est pas nécessaire (l'utilisation de cette macro est recommandée).
Valeur de retour :
* chaîne concaténée
Exemple en C :
[source,c]
----
const char *result = weechat_string_concat (" / ", "abc", "def", "ghi", NULL); /* result == "abc / def / ghi" */
/* with macro */
const char *result = WEECHAT_STR_CONCAT(" / ", "abc", "def", "ghi"); /* result == "abc / def / ghi" */
----
[NOTE]
Cette fonction n'est pas disponible dans l'API script.
[[utf-8]]
=== UTF-8
+45
View File
@@ -3641,6 +3641,51 @@ weechat_string_dyn_free (string, 1);
[NOTE]
Questa funzione non è disponibile nelle API per lo scripting.
==== string_concat
_WeeChat ≥ 4.2.0._
// TRANSLATION MISSING
Concatenate multiple strings using a separator.
Prototipo:
[source,c]
----
const char *weechat_string_concat (const char *separator, ...);
----
Argomenti:
// TRANSLATION MISSING
* _separator_: the separator string which is inserted between concatenated
strings (can be NULL or empty string)
// TRANSLATION MISSING
[NOTE]
Last argument *MUST* always be NULL. +
A macro called `WEECHAT_STR_CONCAT` can be used, where the final NULL value is
not needed (usage of this macro is recommended).
Valore restituito:
// TRANSLATION MISSING
* concatenated string
Esempio in C:
// TRANSLATION MISSING
[source,c]
----
const char *result = weechat_string_concat (" / ", "abc", "def", "ghi", NULL); /* result == "abc / def / ghi" */
/* with macro */
const char *result = WEECHAT_STR_CONCAT(" / ", "abc", "def", "ghi"); /* result == "abc / def / ghi" */
----
[NOTE]
Questa funzione non è disponibile nelle API per lo scripting.
[[utf-8]]
=== UTF-8
+45
View File
@@ -3579,6 +3579,51 @@ weechat_string_dyn_free (string, 1);
[NOTE]
スクリプト API ではこの関数を利用できません。
==== string_concat
_WeeChat ≥ 4.2.0._
// TRANSLATION MISSING
Concatenate multiple strings using a separator.
プロトタイプ:
[source,c]
----
const char *weechat_string_concat (const char *separator, ...);
----
引数:
// TRANSLATION MISSING
* _separator_: the separator string which is inserted between concatenated
strings (can be NULL or empty string)
// TRANSLATION MISSING
[NOTE]
Last argument *MUST* always be NULL. +
A macro called `WEECHAT_STR_CONCAT` can be used, where the final NULL value is
not needed (usage of this macro is recommended).
戻り値:
// TRANSLATION MISSING
* concatenated string
C 言語での使用例:
// TRANSLATION MISSING
[source,c]
----
const char *result = weechat_string_concat (" / ", "abc", "def", "ghi", NULL); /* result == "abc / def / ghi" */
/* with macro */
const char *result = WEECHAT_STR_CONCAT(" / ", "abc", "def", "ghi"); /* result == "abc / def / ghi" */
----
[NOTE]
スクリプト API ではこの関数を利用できません。
[[utf-8]]
=== UTF-8
+45
View File
@@ -3357,6 +3357,51 @@ weechat_string_dyn_free (string, 1);
[NOTE]
Ова функција није доступна у API скриптовања.
==== string_concat
_WeeChat ≥ 4.2.0._
// TRANSLATION MISSING
Concatenate multiple strings using a separator.
Прототип:
[source,c]
----
const char *weechat_string_concat (const char *separator, ...);
----
Аргументи:
// TRANSLATION MISSING
* _separator_: the separator string which is inserted between concatenated
strings (can be NULL or empty string)
// TRANSLATION MISSING
[NOTE]
Last argument *MUST* always be NULL. +
A macro called `WEECHAT_STR_CONCAT` can be used, where the final NULL value is
not needed (usage of this macro is recommended).
Повратна вредност:
// TRANSLATION MISSING
* concatenated string
C пример:
// TRANSLATION MISSING
[source,c]
----
const char *result = weechat_string_concat (" / ", "abc", "def", "ghi", NULL); /* result == "abc / def / ghi" */
/* with macro */
const char *result = WEECHAT_STR_CONCAT(" / ", "abc", "def", "ghi"); /* result == "abc / def / ghi" */
----
[NOTE]
Ова функција није доступна у API скриптовања.
[[utf-8]]
=== UTF-8
+74
View File
@@ -66,6 +66,8 @@
#define MIN3(a, b, c) ((a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c)))
struct t_hashtable *string_hashtable_shared = NULL;
int string_concat_index = 0;
char **string_concat_buffer[STRING_NUM_CONCAT_BUFFERS];
/*
@@ -4693,6 +4695,68 @@ string_dyn_free (char **string, int free_string)
return ptr_string;
}
/*
* Concatenates strings, using a separator (which can be NULL or empty string
* to not use any separator).
*
* Last argument must be NULL to terminate the variable list of arguments.
*/
const char *
string_concat (const char *separator, ...)
{
va_list args;
const char *str;
int index;
string_concat_index = (string_concat_index + 1) % 8;
if (string_concat_buffer[string_concat_index])
{
string_dyn_copy (string_concat_buffer[string_concat_index], NULL);
}
else
{
string_concat_buffer[string_concat_index] = string_dyn_alloc (128);
if (!string_concat_buffer[string_concat_index])
return NULL;
}
index = 0;
va_start (args, separator);
while (1)
{
str = va_arg (args, const char *);
if (!str)
break;
if ((index > 0) && separator && separator[0])
{
string_dyn_concat (string_concat_buffer[string_concat_index],
separator, -1);
}
string_dyn_concat (string_concat_buffer[string_concat_index], str, -1);
index++;
}
va_end (args);
return (const char *)(*string_concat_buffer[string_concat_index]);
}
/*
* Initializes string.
*/
void
string_init ()
{
int i;
for (i = 0; i < STRING_NUM_CONCAT_BUFFERS; i++)
{
string_concat_buffer[i] = NULL;
}
}
/*
* Frees all allocated data.
*/
@@ -4700,9 +4764,19 @@ string_dyn_free (char **string, int free_string)
void
string_end ()
{
int i;
if (string_hashtable_shared)
{
hashtable_free (string_hashtable_shared);
string_hashtable_shared = NULL;
}
for (i = 0; i < STRING_NUM_CONCAT_BUFFERS; i++)
{
if (string_concat_buffer[i])
{
string_dyn_free (string_concat_buffer[i], 1);
string_concat_buffer[i] = NULL;
}
}
}
+5
View File
@@ -24,6 +24,9 @@
#include <stdint.h>
#include <regex.h>
#define STRING_NUM_CONCAT_BUFFERS 8
#define STR_CONCAT(separator, argz...) string_concat (separator, ##argz, NULL)
typedef uint32_t string_shared_count_t;
typedef uint32_t string_dyn_size_t;
@@ -157,6 +160,8 @@ extern char **string_dyn_alloc (int size_alloc);
extern int string_dyn_copy (char **string, const char *new_string);
extern int string_dyn_concat (char **string, const char *add, int bytes);
extern char *string_dyn_free (char **string, int free_string);
extern const char *string_concat (const char *separator, ...);
extern void string_init ();
extern void string_end ();
#endif /* WEECHAT_STRING_H */
+1
View File
@@ -642,6 +642,7 @@ weechat_init (int argc, char *argv[], void (*gui_init_cb)())
^ getpid ());
weeurl_init (); /* initialize URL */
string_init (); /* initialize string */
signal_init (); /* initialize signals */
hdata_init (); /* initialize hdata */
hook_init (); /* initialize hooks */
+1
View File
@@ -658,6 +658,7 @@ plugin_load (const char *filename, int init_plugin, int argc, char **argv)
new_plugin->string_dyn_copy = &string_dyn_copy;
new_plugin->string_dyn_concat = &string_dyn_concat;
new_plugin->string_dyn_free = &string_dyn_free;
new_plugin->string_concat = &string_concat;
new_plugin->utf8_has_8bits = &utf8_has_8bits;
new_plugin->utf8_is_valid = &utf8_is_valid;
+8 -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 "20230908-01"
#define WEECHAT_PLUGIN_API_VERSION "20231017-01"
/* macros for defining plugin infos */
#define WEECHAT_PLUGIN_NAME(__name) \
@@ -216,6 +216,10 @@ struct timeval;
} \
}
/* macro to concatenate strings */
#define WEECHAT_STR_CONCAT(separator, argz...) \
weechat_string_concat (separator, ##argz, NULL)
/*
* macro to return error in case of missing arguments in callback of
* hook_command
@@ -367,6 +371,7 @@ struct t_weechat_plugin
int (*string_dyn_copy) (char **string, const char *new_string);
int (*string_dyn_concat) (char **string, const char *add, int bytes);
char *(*string_dyn_free) (char **string, int free_string);
const char *(*string_concat) (const char *separator, ...);
/* UTF-8 strings */
int (*utf8_has_8bits) (const char *string);
@@ -1379,6 +1384,8 @@ extern int weechat_plugin_end (struct t_weechat_plugin *plugin);
(weechat_plugin->string_dyn_concat)(__string, __add, __bytes)
#define weechat_string_dyn_free(__string, __free_string) \
(weechat_plugin->string_dyn_free)(__string, __free_string)
#define weechat_string_concat(__separator, __argz...) \
(weechat_plugin->string_concat)(__separator, ##__argz)
/* UTF-8 strings */
#define weechat_utf8_has_8bits(__string) \
+29
View File
@@ -2969,3 +2969,32 @@ TEST(CoreString, Dyn)
string_dyn_free (NULL, 0);
string_dyn_free (str, 0);
}
/*
* Tests functions:
* string_concat
*/
TEST(CoreString, Concat)
{
STRCMP_EQUAL("", string_concat (NULL, NULL));
STRCMP_EQUAL("", string_concat (NULL, "", NULL));
STRCMP_EQUAL("", string_concat ("", "", NULL));
STRCMP_EQUAL("", string_concat (",", "", NULL));
STRCMP_EQUAL("abc", string_concat (NULL, "abc", NULL));
STRCMP_EQUAL("abcdef", string_concat (NULL, "abc", "def", NULL));
STRCMP_EQUAL("abcdefghi", string_concat (NULL, "abc", "def", "ghi", NULL));
STRCMP_EQUAL("abc", string_concat ("", "abc", NULL));
STRCMP_EQUAL("abcdef", string_concat ("", "abc", "def", NULL));
STRCMP_EQUAL("abcdefghi", string_concat ("", "abc", "def", "ghi", NULL));
STRCMP_EQUAL("abc", string_concat (",", "abc", NULL));
STRCMP_EQUAL("abc,def", string_concat (",", "abc", "def", NULL));
STRCMP_EQUAL("abc,def,ghi", string_concat (",", "abc", "def", "ghi", NULL));
STRCMP_EQUAL("abc", string_concat (" / ", "abc", NULL));
STRCMP_EQUAL("abc / def", string_concat (" / ", "abc", "def", NULL));
STRCMP_EQUAL("abc / def / ghi", string_concat (" / ", "abc", "def", "ghi", NULL));
}