From 99a3ca17dfa066c570a852c99b1b1cc3dfb3ac32 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Thu, 4 Jul 2024 15:42:17 +0200 Subject: [PATCH] lib/list.c: comma_to_list(): Use strchrcnt() instead of its pattern Signed-off-by: Alejandro Colomar --- lib/list.c | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-) diff --git a/lib/list.c b/lib/list.c index 1132c5c7e..fbc0e14a4 100644 --- a/lib/list.c +++ b/lib/list.c @@ -15,6 +15,7 @@ #include "alloc/x/xmalloc.h" #include "prototypes.h" #include "defines.h" +#include "string/strchr/strchrcnt.h" #include "string/strdup/xstrdup.h" @@ -179,7 +180,8 @@ bool is_on_list (char *const *list, const char *member) * comma_to_list - convert comma-separated list to (char *) array */ -/*@only@*/char **comma_to_list (const char *comma) +/*@only@*/char ** +comma_to_list(const char *comma) { char *members; char **array; @@ -195,30 +197,12 @@ bool is_on_list (char *const *list, const char *member) members = xstrdup (comma); - /* - * Count the number of commas in the list - */ - - for (cp = members, i = 0;; i++) { - cp2 = strchr (cp, ','); - if (NULL != cp2) { - cp = cp2 + 1; - } else { - break; - } - } - - /* - * Add 2 - one for the ending NULL, the other for the last item - */ - - i += 2; - /* * Allocate the array we're going to store the pointers into. + * n: number of delimiters + last element + NULL */ - array = XMALLOC(i, char *); + array = XMALLOC(strchrcnt(members, ',') + 2, char *); /* * Empty list is special - 0 members, not 1 empty member. --marekm -- 2.47.3