Modified gpr_slice_split to take a char* separator.

pull/2319/head
David Garcia Quintas 10 years ago
parent e35ed0821b
commit ffe3d2b190
  1. 11
      src/core/support/string.c
  2. 2
      src/core/support/string.h
  3. 21
      test/core/support/string_test.c

@ -215,21 +215,20 @@ char *gpr_strjoin_sep(const char **strs, size_t nstrs, const char *sep,
*
* Returns 1 and updates \a begin and \a end. Returns 0 otherwise. */
static int slice_find_separator_offset(const gpr_slice str,
const gpr_slice sep,
const char *sep,
const size_t read_offset,
size_t *begin,
size_t *end) {
size_t i;
const gpr_uint8 *str_ptr = GPR_SLICE_START_PTR(str) + read_offset;
const gpr_uint8 *sep_ptr = GPR_SLICE_START_PTR(sep);
const size_t str_len = GPR_SLICE_LENGTH(str) - read_offset;
const size_t sep_len = GPR_SLICE_LENGTH(sep);
const size_t sep_len = strlen(sep);
if (str_len < sep_len) {
return 0;
}
for (i = 0; i <= str_len - sep_len; i++) {
if (memcmp(str_ptr + i, sep_ptr, sep_len) == 0) {
if (memcmp(str_ptr + i, sep, sep_len) == 0) {
*begin = read_offset;
*end = read_offset + i;
return 1;
@ -238,8 +237,8 @@ static int slice_find_separator_offset(const gpr_slice str,
return 0;
}
void gpr_slice_split(gpr_slice str, gpr_slice sep, gpr_slice_buffer *dst) {
const size_t sep_len = GPR_SLICE_LENGTH(sep);
void gpr_slice_split(gpr_slice str, const char *sep, gpr_slice_buffer *dst) {
const size_t sep_len = strlen(sep);
size_t begin, end;
GPR_ASSERT(sep_len > 0);

@ -86,7 +86,7 @@ char *gpr_strjoin_sep(const char **strs, size_t nstrs, const char *sep,
/** Split \a str by the separator \a sep. Results are stored in \a dst, which
* should be a properly initialized instance. */
void gpr_slice_split(gpr_slice str, gpr_slice sep, gpr_slice_buffer *dst);
void gpr_slice_split(gpr_slice str, const char *sep, gpr_slice_buffer *dst);
/* A vector of strings... for building up a final string one piece at a time */
typedef struct {

@ -223,7 +223,6 @@ static void test_strjoin_sep(void) {
static void test_strsplit(void) {
gpr_slice_buffer* parts;
gpr_slice str;
gpr_slice sep;
LOG_TEST_NAME("test_strsplit");
@ -231,8 +230,7 @@ static void test_strsplit(void) {
gpr_slice_buffer_init(parts);
str = gpr_slice_from_copied_string("one, two, three, four");
sep = gpr_slice_from_copied_string(", ");
gpr_slice_split(str, sep, parts);
gpr_slice_split(str, ", ", parts);
GPR_ASSERT(4 == parts->count);
GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[0], "one"));
GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[1], "two"));
@ -243,15 +241,15 @@ static void test_strsplit(void) {
/* separator not present in string */
str = gpr_slice_from_copied_string("one two three four");
gpr_slice_split(str, sep, parts);
gpr_slice_split(str, ", ", parts);
GPR_ASSERT(1 == parts->count);
GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[0], "one two three four"));
gpr_slice_buffer_reset_and_unref(parts);
gpr_slice_unref(str);
/* separator at the end */
str = gpr_slice_from_copied_string("foo, ");
gpr_slice_split(str, sep, parts);
str = gpr_slice_from_copied_string("foo,");
gpr_slice_split(str, ",", parts);
GPR_ASSERT(2 == parts->count);
GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[0], "foo"));
GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[1], ""));
@ -259,8 +257,8 @@ static void test_strsplit(void) {
gpr_slice_unref(str);
/* separator at the beginning */
str = gpr_slice_from_copied_string(", foo");
gpr_slice_split(str, sep, parts);
str = gpr_slice_from_copied_string(",foo");
gpr_slice_split(str, ",", parts);
GPR_ASSERT(2 == parts->count);
GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[0], ""));
GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[1], "foo"));
@ -268,8 +266,8 @@ static void test_strsplit(void) {
gpr_slice_unref(str);
/* standalone separator */
str = gpr_slice_from_copied_string(", ");
gpr_slice_split(str, sep, parts);
str = gpr_slice_from_copied_string(",");
gpr_slice_split(str, ",", parts);
GPR_ASSERT(2 == parts->count);
GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[0], ""));
GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[1], ""));
@ -278,13 +276,12 @@ static void test_strsplit(void) {
/* empty input */
str = gpr_slice_from_copied_string("");
gpr_slice_split(str, sep, parts);
gpr_slice_split(str, ", ", parts);
GPR_ASSERT(1 == parts->count);
GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[0], ""));
gpr_slice_buffer_reset_and_unref(parts);
gpr_slice_unref(str);
gpr_slice_unref(sep);
gpr_slice_buffer_destroy(parts);
gpr_free(parts);
}

Loading…
Cancel
Save