From cfd221014c1e5337ec14503430c98ca620024f81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Sun, 23 Aug 2020 23:27:57 +0200 Subject: api: add argument "bytes" in function string_dyn_concat --- src/core/wee-eval.c | 4 ++-- src/core/wee-string.c | 14 +++++++++++--- src/core/wee-string.h | 2 +- 3 files changed, 14 insertions(+), 6 deletions(-) (limited to 'src/core') diff --git a/src/core/wee-eval.c b/src/core/wee-eval.c index 4d64c641f..c36192f09 100644 --- a/src/core/wee-eval.c +++ b/src/core/wee-eval.c @@ -80,8 +80,8 @@ eval_debug_message (struct t_eval_context *eval_context, char *message, ...) return; if (*(eval_context->debug)[0]) - string_dyn_concat (eval_context->debug, "\n"); - string_dyn_concat (eval_context->debug, vbuffer); + string_dyn_concat (eval_context->debug, "\n", -1); + string_dyn_concat (eval_context->debug, vbuffer, -1); free (vbuffer); } diff --git a/src/core/wee-string.c b/src/core/wee-string.c index 4c41239c3..9dbc2ce50 100644 --- a/src/core/wee-string.c +++ b/src/core/wee-string.c @@ -3978,6 +3978,10 @@ string_dyn_copy (char **string, const char *new_string) /* * Concatenates a string to a dynamic string and adjusts its size accordingly. * + * The parameter "bytes" is the max number of bytes to concatenate + * (a terminating null byte '\0' is automatically added); value -1 means + * automatic: whole string "add" is concatenated. + * * The string pointer (*string) is updated with the new allocated string * if the string had to be extended, or the same pointer if there was enough * size to concatenate the new string. @@ -3988,7 +3992,7 @@ string_dyn_copy (char **string, const char *new_string) */ int -string_dyn_concat (char **string, const char *add) +string_dyn_concat (char **string, const char *add, int bytes) { struct t_string_dyn *ptr_string_dyn; char *string_realloc; @@ -3997,12 +4001,15 @@ string_dyn_concat (char **string, const char *add) if (!string || !*string) return 0; - if (!add || !add[0]) + if (!add || !add[0] || (bytes == 0)) return 1; ptr_string_dyn = (struct t_string_dyn *)string; length_add = strlen (add); + if ((bytes >= 0) && (bytes < (int)length_add)) + length_add = bytes; + new_size = ptr_string_dyn->size + length_add; if (new_size > ptr_string_dyn->size_alloc) @@ -4027,8 +4034,9 @@ string_dyn_concat (char **string, const char *add) /* concatenate "add" after "string" */ memmove (ptr_string_dyn->string + ptr_string_dyn->size - 1, add, - length_add + 1); + length_add); ptr_string_dyn->size = new_size; + ptr_string_dyn->string[new_size - 1] = '\0'; return 1; } diff --git a/src/core/wee-string.h b/src/core/wee-string.h index a34dbd041..c54513535 100644 --- a/src/core/wee-string.h +++ b/src/core/wee-string.h @@ -134,7 +134,7 @@ extern const char *string_shared_get (const char *string); extern void string_shared_free (const char *string); 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); +extern int string_dyn_concat (char **string, const char *add, int bytes); extern char *string_dyn_free (char **string, int free_string); extern void string_end (); -- cgit v1.2.3