aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Hillegeer <nicolas@hillegeer.com>2014-04-26 19:49:02 +0200
committerThiago de Arruda <tpadilha84@gmail.com>2014-04-29 09:29:10 -0300
commit104000eff649886777563f36f03d260f5e8baa5c (patch)
treeb2b960771352d95b45b8ddaac2ccf4c84a6f9013
parentcf68eda287a5a89e2b48779087f6d8ce99ba05d9 (diff)
downloadrneovim-104000eff649886777563f36f03d260f5e8baa5c.tar.gz
rneovim-104000eff649886777563f36f03d260f5e8baa5c.tar.bz2
rneovim-104000eff649886777563f36f03d260f5e8baa5c.zip
garray: implement ga_concat_strings_sep
A generalized version of ga_concat_strings that can handle any separator. Reimplement ga_concat_strings on top of it.
-rw-r--r--src/garray.c39
-rw-r--r--src/garray.h2
2 files changed, 29 insertions, 12 deletions
diff --git a/src/garray.c b/src/garray.c
index b55d5dacce..ea9d554e45 100644
--- a/src/garray.c
+++ b/src/garray.c
@@ -97,35 +97,50 @@ void ga_remove_duplicate_strings(garray_T *gap)
}
/// For a growing array that contains a list of strings: concatenate all the
-/// strings with a separating comma.
+/// strings with sep as separator.
///
/// @param gap
///
/// @returns the concatenated strings
-char_u* ga_concat_strings(garray_T *gap)
+char_u *ga_concat_strings_sep(const garray_T *gap, const char *sep)
{
- const int nelem = gap->ga_len;
- const char **data = gap->ga_data;
+ const size_t nelem = (size_t) gap->ga_len;
+ const char **strings = gap->ga_data;
+
+ if (nelem == 0) {
+ return (char_u *) xstrdup("");
+ }
size_t len = 0;
- for (int i = 0; i < nelem; ++i) {
- len += strlen(data[i]) + 1;
+ for (size_t i = 0; i < nelem; i++) {
+ len += strlen(strings[i]);
}
+ // add some space for the (num - 1) separators
+ len += (nelem - 1) * strlen(sep);
char *const ret = xmallocz(len);
char *s = ret;
- *s = NUL;
- for (int i = 0; i < nelem; ++i) {
- s = xstpcpy(s, data[i]);
- if (i < nelem - 1) {
- s = xstpcpy(s, ",");
- }
+ for (size_t i = 0; i < nelem - 1; i++) {
+ s = xstpcpy(s, strings[i]);
+ s = xstpcpy(s, sep);
}
+ s = xstpcpy(s, strings[nelem - 1]);
return (char_u *) ret;
}
+/// For a growing array that contains a list of strings: concatenate all the
+/// strings with a separating comma.
+///
+/// @param gap
+///
+/// @returns the concatenated strings
+char_u* ga_concat_strings(garray_T *gap)
+{
+ return ga_concat_strings_sep(gap, ",");
+}
+
/// Concatenate a string to a growarray which contains characters.
/// Note: Does NOT copy the NUL at the end!
///
diff --git a/src/garray.h b/src/garray.h
index eccade2ebb..4f07d5523b 100644
--- a/src/garray.h
+++ b/src/garray.h
@@ -20,6 +20,8 @@ void ga_clear(garray_T *gap);
void ga_clear_strings(garray_T *gap);
void ga_init(garray_T *gap, int itemsize, int growsize);
void ga_grow(garray_T *gap, int n);
+char_u *ga_concat_strings_sep(const garray_T *gap, const char *sep)
+ FUNC_ATTR_NONNULL_RET;
char_u *ga_concat_strings(garray_T *gap) FUNC_ATTR_NONNULL_RET;
void ga_remove_duplicate_strings(garray_T *gap);
void ga_concat(garray_T *gap, char_u *s);