diff options
author | Sébastien Helleu <flashcode@flashtux.org> | 2023-09-02 11:53:56 +0200 |
---|---|---|
committer | Sébastien Helleu <flashcode@flashtux.org> | 2023-10-17 18:14:53 +0200 |
commit | e34071131ec31b53c9dc4491f9eb2aa546a23ac7 (patch) | |
tree | 5d67e684f5d419026eb2c9b9697c1a706f45f893 /src/core | |
parent | 9bc9df47d72af83313483c7324d3dcae9157f939 (diff) | |
download | weechat-e34071131ec31b53c9dc4491f9eb2aa546a23ac7.zip |
api: add function string_concat (issue #2005)
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/wee-string.c | 74 | ||||
-rw-r--r-- | src/core/wee-string.h | 5 | ||||
-rw-r--r-- | src/core/weechat.c | 1 |
3 files changed, 80 insertions, 0 deletions
diff --git a/src/core/wee-string.c b/src/core/wee-string.c index ab0bee82e..7374f9fe9 100644 --- a/src/core/wee-string.c +++ b/src/core/wee-string.c @@ -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]; /* @@ -4694,15 +4696,87 @@ string_dyn_free (char **string, int free_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. */ 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; + } + } } diff --git a/src/core/wee-string.h b/src/core/wee-string.h index 522d3e440..716335e9a 100644 --- a/src/core/wee-string.h +++ b/src/core/wee-string.h @@ -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 */ diff --git a/src/core/weechat.c b/src/core/weechat.c index 9c11dd16b..4eb80c8b2 100644 --- a/src/core/weechat.c +++ b/src/core/weechat.c @@ -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 */ |