Skip to content
Snippets Groups Projects
Commit a16cf1ee authored by David Garcia Quintas's avatar David Garcia Quintas
Browse files

PR comments + some more tests.

parent b7541e8e
No related branches found
No related tags found
No related merge requests found
...@@ -173,12 +173,12 @@ char *gpr_strjoin_sep(const char **strs, size_t nstrs, const char *sep, ...@@ -173,12 +173,12 @@ char *gpr_strjoin_sep(const char **strs, size_t nstrs, const char *sep,
out_length = 0; out_length = 0;
for (i = 0; i < nstrs; i++) { for (i = 0; i < nstrs; i++) {
const size_t slen = strlen(strs[i]); const size_t slen = strlen(strs[i]);
memcpy(out + out_length, strs[i], slen); if (i != 0) {
out_length += slen;
if (sep_len > 0 && nstrs > 0 && i < nstrs - 1) {
memcpy(out + out_length, sep, sep_len); memcpy(out + out_length, sep, sep_len);
out_length += sep_len; out_length += sep_len;
} }
memcpy(out + out_length, strs[i], slen);
out_length += slen;
} }
out[out_length] = 0; out[out_length] = 0;
if (final_length != NULL) { if (final_length != NULL) {
......
...@@ -176,10 +176,17 @@ static void test_strjoin_sep(void) { ...@@ -176,10 +176,17 @@ static void test_strjoin_sep(void) {
GPR_ASSERT(0 == strcmp("one, two, three, four", joined)); GPR_ASSERT(0 == strcmp("one, two, three, four", joined));
gpr_free(joined); gpr_free(joined);
/* empty separator */
joined = gpr_strjoin_sep(parts, 4, "", &joined_len);
GPR_ASSERT(0 == strcmp("onetwothreefour", joined));
gpr_free(joined);
/* degenerated case specifying zero input parts */
joined = gpr_strjoin_sep(parts, 0, ", ", &joined_len); joined = gpr_strjoin_sep(parts, 0, ", ", &joined_len);
GPR_ASSERT(0 == strcmp("", joined)); GPR_ASSERT(0 == strcmp("", joined));
gpr_free(joined); gpr_free(joined);
/* single part should have no separator */
joined = gpr_strjoin_sep(parts, 1, ", ", &joined_len); joined = gpr_strjoin_sep(parts, 1, ", ", &joined_len);
GPR_ASSERT(0 == strcmp("one", joined)); GPR_ASSERT(0 == strcmp("one", joined));
gpr_free(joined); gpr_free(joined);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment