aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Hillegeer <nicolas@hillegeer.com>2014-04-26 19:19:43 +0200
committerThiago de Arruda <tpadilha84@gmail.com>2014-04-29 09:29:10 -0300
commitcf68eda287a5a89e2b48779087f6d8ce99ba05d9 (patch)
tree5debb6e557d406d7fb98b2c088a8520c4d90bb87
parent6b486f41d1b113bdb4018f5611105b83c7cd5455 (diff)
downloadrneovim-cf68eda287a5a89e2b48779087f6d8ce99ba05d9.tar.gz
rneovim-cf68eda287a5a89e2b48779087f6d8ce99ba05d9.tar.bz2
rneovim-cf68eda287a5a89e2b48779087f6d8ce99ba05d9.zip
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).
-rw-r--r--src/garray.c21
1 files 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)
{
- size_t len = 0;
+ const int nelem = gap->ga_len;
+ const char **data = gap->ga_data;
- for (int i = 0; i < gap->ga_len; ++i) {
- len += strlen(((char **)(gap->ga_data))[i]) + 1;
+ size_t len = 0;
+ 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.