From 08e110c8357d043b0882145d3520f7c0e7c4d5d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Fri, 18 Aug 2023 13:38:05 +0200 Subject: core: use dynamic string in function gui_buffer_set_highlight_words_list --- src/gui/gui-buffer.c | 40 ++++++++++++++-------------------------- 1 file changed, 14 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/gui/gui-buffer.c b/src/gui/gui-buffer.c index df8acfc27..612503603 100644 --- a/src/gui/gui-buffer.c +++ b/src/gui/gui-buffer.c @@ -1630,46 +1630,34 @@ gui_buffer_set_highlight_words_list (struct t_gui_buffer *buffer, struct t_weelist *list) { struct t_weelist_item *ptr_list_item; - int length; const char *ptr_string; - char *words; + char **words; if (!buffer) return; - /* compute length */ - length = 0; - for (ptr_list_item = weelist_get (list, 0); ptr_list_item; - ptr_list_item = weelist_next (ptr_list_item)) - { - ptr_string = weelist_string (ptr_list_item); - if (ptr_string) - length += strlen (ptr_string) + 1; - } - length += 2; /* '\n' + '\0' */ - - /* allocate memory */ - words = malloc (length); + words = string_dyn_alloc (64); if (!words) return; - /* build string */ - words[0] = '\0'; - for (ptr_list_item = weelist_get (list, 0); ptr_list_item; - ptr_list_item = weelist_next (ptr_list_item)) + if (list) { - ptr_string = weelist_string (ptr_list_item); - if (ptr_string) + for (ptr_list_item = weelist_get (list, 0); ptr_list_item; + ptr_list_item = weelist_next (ptr_list_item)) { - strcat (words, ptr_string); - if (weelist_next (ptr_list_item)) - strcat (words, ","); + ptr_string = weelist_string (ptr_list_item); + if (ptr_string) + { + if (*words[0]) + string_dyn_concat (words, ",", -1); + string_dyn_concat (words, ptr_string, -1); + } } } - gui_buffer_set_highlight_words (buffer, words); + gui_buffer_set_highlight_words (buffer, *words); - free (words); + string_dyn_free (words, 1); } /* -- cgit v1.2.3