diff options
Diffstat (limited to 'src/core/lib/support')
-rw-r--r-- | src/core/lib/support/string.c | 29 | ||||
-rw-r--r-- | src/core/lib/support/string.h | 3 |
2 files changed, 31 insertions, 1 deletions
diff --git a/src/core/lib/support/string.c b/src/core/lib/support/string.c index f263f82baf..d20b86f7cf 100644 --- a/src/core/lib/support/string.c +++ b/src/core/lib/support/string.c @@ -276,10 +276,37 @@ int gpr_stricmp(const char *a, const char *b) { return ca - cb; } +static void add_string_to_split(const char *beg, const char *end, char ***strs, + size_t *nstrs, size_t *capstrs) { + char *out = gpr_malloc((size_t)(end - beg) + 1); + memcpy(out, beg, (size_t)(end - beg)); + out[end - beg] = 0; + if (*nstrs == *capstrs) { + *capstrs = GPR_MAX(8, 2 * *capstrs); + *strs = gpr_realloc(*strs, sizeof(*strs) * *capstrs); + } + (*strs)[*nstrs] = out; + ++*nstrs; +} + +void gpr_string_split(const char *input, const char *sep, char ***strs, + size_t *nstrs) { + char *next; + *strs = NULL; + *nstrs = 0; + size_t capstrs = 0; + while ((next = strstr(input, sep))) { + add_string_to_split(input, next, strs, nstrs, &capstrs); + input = next + strlen(sep); + } + add_string_to_split(input, input + strlen(input), strs, nstrs, &capstrs); +} + void *gpr_memrchr(const void *s, int c, size_t n) { if (s == NULL) return NULL; char *b = (char *)s; - for (size_t i = 0; i < n; i++) { + size_t i; + for (i = 0; i < n; i++) { if (b[n - i - 1] == c) { return &b[n - i - 1]; } diff --git a/src/core/lib/support/string.h b/src/core/lib/support/string.h index 6d1f7cc632..c7fb0dbd14 100644 --- a/src/core/lib/support/string.h +++ b/src/core/lib/support/string.h @@ -98,6 +98,9 @@ char *gpr_strjoin(const char **strs, size_t nstrs, size_t *total_length); char *gpr_strjoin_sep(const char **strs, size_t nstrs, const char *sep, size_t *total_length); +void gpr_string_split(const char *input, const char *sep, char ***strs, + size_t *nstrs); + /* A vector of strings... for building up a final string one piece at a time */ typedef struct { char **strs; |