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).
This commit is contained in:
Nicolas Hillegeer 2014-04-26 19:19:43 +02:00 committed by Thiago de Arruda
parent 6b486f41d1
commit cf68eda287

View File

@ -104,23 +104,26 @@ void ga_remove_duplicate_strings(garray_T *gap)
/// @returns the concatenated strings /// @returns the concatenated strings
char_u* ga_concat_strings(garray_T *gap) char_u* ga_concat_strings(garray_T *gap)
{ {
const int nelem = gap->ga_len;
const char **data = gap->ga_data;
size_t len = 0; size_t len = 0;
for (int i = 0; i < nelem; ++i) {
for (int i = 0; i < gap->ga_len; ++i) { len += strlen(data[i]) + 1;
len += strlen(((char **)(gap->ga_data))[i]) + 1;
} }
char *s = xmallocz(len); char *const ret = xmallocz(len);
char *s = ret;
*s = NUL; *s = NUL;
for (int i = 0; i < gap->ga_len; ++i) { for (int i = 0; i < nelem; ++i) {
if (*s != NUL) { s = xstpcpy(s, data[i]);
strcat(s, ","); 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. /// Concatenate a string to a growarray which contains characters.