Added convenience method gpr_strjoin_sep

pull/2319/head
David Garcia Quintas 10 years ago
parent 772187cdf0
commit b7541e8eea
  1. 15
      src/core/support/string.c
  2. 6
      src/core/support/string.h
  3. 42
      test/core/support/string_test.c

@ -153,6 +153,12 @@ int gpr_ltoa(long value, char *string) {
}
char *gpr_strjoin(const char **strs, size_t nstrs, size_t *final_length) {
return gpr_strjoin_sep(strs, nstrs, "", final_length);
}
char *gpr_strjoin_sep(const char **strs, size_t nstrs, const char *sep,
size_t *final_length) {
const size_t sep_len = strlen(sep);
size_t out_length = 0;
size_t i;
char *out;
@ -160,12 +166,19 @@ char *gpr_strjoin(const char **strs, size_t nstrs, size_t *final_length) {
out_length += strlen(strs[i]);
}
out_length += 1; /* null terminator */
if (nstrs > 0) {
out_length += sep_len * (nstrs - 1); /* separators */
}
out = gpr_malloc(out_length);
out_length = 0;
for (i = 0; i < nstrs; i++) {
size_t slen = strlen(strs[i]);
const size_t slen = strlen(strs[i]);
memcpy(out + out_length, strs[i], slen);
out_length += slen;
if (sep_len > 0 && nstrs > 0 && i < nstrs - 1) {
memcpy(out + out_length, sep, sep_len);
out_length += sep_len;
}
}
out[out_length] = 0;
if (final_length != NULL) {

@ -72,6 +72,12 @@ void gpr_reverse_bytes(char *str, int len);
if it is non-null. */
char *gpr_strjoin(const char **strs, size_t nstrs, size_t *total_length);
/* Join a set of strings using a separator, returning the resulting string.
Total combined length (excluding null terminator) is returned in total_length
if it is non-null. */
char *gpr_strjoin_sep(const char **strs, size_t nstrs, const char *sep,
size_t *total_length);
/* A vector of strings... for building up a final string one piece at a time */
typedef struct {
char **strs;

@ -145,11 +145,53 @@ static void test_asprintf(void) {
}
}
static void test_strjoin(void) {
const char *parts[4] = {"one", "two", "three", "four"};
size_t joined_len;
char *joined;
LOG_TEST_NAME("test_strjoin");
joined = gpr_strjoin(parts, 4, &joined_len);
GPR_ASSERT(0 == strcmp("onetwothreefour", joined));
gpr_free(joined);
joined = gpr_strjoin(parts, 0, &joined_len);
GPR_ASSERT(0 == strcmp("", joined));
gpr_free(joined);
joined = gpr_strjoin(parts, 1, &joined_len);
GPR_ASSERT(0 == strcmp("one", joined));
gpr_free(joined);
}
static void test_strjoin_sep(void) {
const char *parts[4] = {"one", "two", "three", "four"};
size_t joined_len;
char *joined;
LOG_TEST_NAME("test_strjoin_sep");
joined = gpr_strjoin_sep(parts, 4, ", ", &joined_len);
GPR_ASSERT(0 == strcmp("one, two, three, four", joined));
gpr_free(joined);
joined = gpr_strjoin_sep(parts, 0, ", ", &joined_len);
GPR_ASSERT(0 == strcmp("", joined));
gpr_free(joined);
joined = gpr_strjoin_sep(parts, 1, ", ", &joined_len);
GPR_ASSERT(0 == strcmp("one", joined));
gpr_free(joined);
}
int main(int argc, char **argv) {
grpc_test_init(argc, argv);
test_strdup();
test_hexdump();
test_parse_uint32();
test_asprintf();
test_strjoin();
test_strjoin_sep();
return 0;
}

Loading…
Cancel
Save