From cf68eda287a5a89e2b48779087f6d8ce99ba05d9 Mon Sep 17 00:00:00 2001 From: Nicolas Hillegeer Date: Sat, 26 Apr 2014 19:19:43 +0200 Subject: [PATCH] garray: reimplement ga_concat_strings with stpcpy Less useless strlen(), stpcpy() is a more natural fit for this task. ga_concat_strings() still has too much strlen() but that would be ugly to remove for a function that's not used very often (just once in the current codebase). --- src/garray.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/garray.c b/src/garray.c index 8ad169b725..b55d5dacce 100644 --- a/src/garray.c +++ b/src/garray.c @@ -104,23 +104,26 @@ void ga_remove_duplicate_strings(garray_T *gap) /// @returns the concatenated strings char_u* ga_concat_strings(garray_T *gap) { + const int nelem = gap->ga_len; + const char **data = gap->ga_data; + size_t len = 0; - - for (int i = 0; i < gap->ga_len; ++i) { - len += strlen(((char **)(gap->ga_data))[i]) + 1; + for (int i = 0; i < nelem; ++i) { + len += strlen(data[i]) + 1; } - char *s = xmallocz(len); + char *const ret = xmallocz(len); + char *s = ret; *s = NUL; - for (int i = 0; i < gap->ga_len; ++i) { - if (*s != NUL) { - strcat(s, ","); + for (int i = 0; i < nelem; ++i) { + s = xstpcpy(s, data[i]); + if (i < nelem - 1) { + s = xstpcpy(s, ","); } - strcat(s, ((char **)(gap->ga_data))[i]); } - return (char_u *)s; + return (char_u *) ret; } /// Concatenate a string to a growarray which contains characters.