From f87c9bb9f70f1a6c8c7081c762c15ba7eaaf971e Mon Sep 17 00:00:00 2001 From: Craig Barnes Date: Sat, 3 Aug 2024 08:12:13 +0100 Subject: xmalloc: remove delim param from xstrjoin() and add separate xstrjoin3() This avoids the need for an unused third argument for most xstrjoin() calls and replaces the cases where it's needed with a more flexible function. Code generation is the same in both cases, when there are 2 string params and a compile-time known delimiter. This commit also converts 4 uses of xasprintf() to use xstrjoin*(). See also: https://godbolt.org/z/xsjrhv9b6 --- xmalloc.h | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'xmalloc.h') diff --git a/xmalloc.h b/xmalloc.h index 76db7e1b..8a2c208f 100644 --- a/xmalloc.h +++ b/xmalloc.h @@ -25,16 +25,25 @@ xmemdup(const void *ptr, size_t size) } static inline char * -xstrjoin(const char *s1, const char *s2, char delim) +xstrjoin(const char *s1, const char *s2) { size_t n1 = strlen(s1); - size_t n2 = delim > 0 ? 1 : 0; - size_t n3 = strlen(s2); + size_t n2 = strlen(s2); + char *joined = xmalloc(n1 + n2 + 1); + memcpy(joined, s1, n1); + memcpy(joined + n1, s2, n2 + 1); + return joined; +} +static inline char * +xstrjoin3(const char *s1, const char *s2, const char *s3) +{ + size_t n1 = strlen(s1); + size_t n2 = strlen(s2); + size_t n3 = strlen(s3); char *joined = xmalloc(n1 + n2 + n3 + 1); memcpy(joined, s1, n1); - if (delim > 0) - joined[n1] = delim; - memcpy(joined + n1 + n2, s2, n3 + 1); + memcpy(joined + n1, s2, n2); + memcpy(joined + n1 + n2, s3, n3 + 1); return joined; } -- cgit v1.3