aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/lib/slice/slice_string_helpers.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/lib/slice/slice_string_helpers.cc')
-rw-r--r--src/core/lib/slice/slice_string_helpers.cc45
1 files changed, 40 insertions, 5 deletions
diff --git a/src/core/lib/slice/slice_string_helpers.cc b/src/core/lib/slice/slice_string_helpers.cc
index be0db09252..4441a26d8e 100644
--- a/src/core/lib/slice/slice_string_helpers.cc
+++ b/src/core/lib/slice/slice_string_helpers.cc
@@ -56,24 +56,59 @@ static int slice_find_separator_offset(const grpc_slice str, const char* sep,
return 0;
}
-void grpc_slice_split(grpc_slice str, const char* sep, grpc_slice_buffer* dst) {
+static void skip_leading_trailing_spaces(const uint8_t* str_buffer,
+ size_t* begin, size_t* end) {
+ while (*begin < *end && str_buffer[*begin] == ' ') {
+ (*begin)++;
+ }
+ while (*begin < *end && str_buffer[*end - 1] == ' ') {
+ (*end)--;
+ }
+}
+
+static void grpc_slice_split_inner(grpc_slice str, const char* sep,
+ grpc_slice_buffer* dst, bool no_space) {
const size_t sep_len = strlen(sep);
size_t begin, end;
+ const uint8_t* str_buffer = GRPC_SLICE_START_PTR(str);
+ size_t sep_pos;
GPR_ASSERT(sep_len > 0);
if (slice_find_separator_offset(str, sep, 0, &begin, &end) != 0) {
do {
+ sep_pos = end;
+ if (no_space) {
+ skip_leading_trailing_spaces(str_buffer, &begin, &end);
+ }
grpc_slice_buffer_add_indexed(dst, grpc_slice_sub(str, begin, end));
- } while (slice_find_separator_offset(str, sep, end + sep_len, &begin,
+ } while (slice_find_separator_offset(str, sep, sep_pos + sep_len, &begin,
&end) != 0);
- grpc_slice_buffer_add_indexed(
- dst, grpc_slice_sub(str, end + sep_len, GRPC_SLICE_LENGTH(str)));
+ begin = sep_pos + sep_len;
+ end = GRPC_SLICE_LENGTH(str);
+ if (no_space) {
+ skip_leading_trailing_spaces(str_buffer, &begin, &end);
+ }
+ grpc_slice_buffer_add_indexed(dst, grpc_slice_sub(str, begin, end));
} else { /* no sep found, add whole input */
- grpc_slice_buffer_add_indexed(dst, grpc_slice_ref_internal(str));
+ begin = 0;
+ end = GRPC_SLICE_LENGTH(str);
+ if (no_space) {
+ skip_leading_trailing_spaces(str_buffer, &begin, &end);
+ }
+ grpc_slice_buffer_add_indexed(dst, grpc_slice_sub(str, begin, end));
}
}
+void grpc_slice_split(grpc_slice str, const char* sep, grpc_slice_buffer* dst) {
+ grpc_slice_split_inner(str, sep, dst, false);
+}
+
+void grpc_slice_split_without_space(grpc_slice str, const char* sep,
+ grpc_slice_buffer* dst) {
+ grpc_slice_split_inner(str, sep, dst, true);
+}
+
bool grpc_parse_slice_to_uint32(grpc_slice str, uint32_t* result) {
return gpr_parse_bytes_to_uint32((const char*)GRPC_SLICE_START_PTR(str),
GRPC_SLICE_LENGTH(str), result) != 0;