3 * Copyright 2015 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include <grpc/support/port_platform.h>
21 #include "src/core/lib/slice/slice_string_helpers.h"
25 #include <grpc/support/log.h>
27 #include "src/core/lib/gpr/string.h"
28 #include "src/core/lib/gprpp/memory.h"
29 #include "src/core/lib/slice/slice_internal.h"
31 char* grpc_dump_slice(const grpc_slice& s, uint32_t flags) {
32 return gpr_dump(reinterpret_cast<const char*> GRPC_SLICE_START_PTR(s),
33 GRPC_SLICE_LENGTH(s), flags);
36 grpc_slice grpc_dump_slice_to_slice(const grpc_slice& s, uint32_t flags) {
38 grpc_core::UniquePtr<char> ptr(
39 gpr_dump_return_len(reinterpret_cast<const char*> GRPC_SLICE_START_PTR(s),
40 GRPC_SLICE_LENGTH(s), flags, &len));
41 return grpc_slice_from_moved_buffer(std::move(ptr), len);
44 /** Finds the initial (\a begin) and final (\a end) offsets of the next
45 * substring from \a str + \a read_offset until the next \a sep or the end of \a
48 * Returns 1 and updates \a begin and \a end. Returns 0 otherwise. */
49 static int slice_find_separator_offset(const grpc_slice str, const char* sep,
50 const size_t read_offset, size_t* begin,
53 const uint8_t* str_ptr = GRPC_SLICE_START_PTR(str) + read_offset;
54 const size_t str_len = GRPC_SLICE_LENGTH(str) - read_offset;
55 const size_t sep_len = strlen(sep);
56 if (str_len < sep_len) {
60 for (i = 0; i <= str_len - sep_len; i++) {
61 if (memcmp(str_ptr + i, sep, sep_len) == 0) {
63 *end = read_offset + i;
70 static void skip_leading_trailing_spaces(const uint8_t* str_buffer,
71 size_t* begin, size_t* end) {
72 while (*begin < *end && str_buffer[*begin] == ' ') {
75 while (*begin < *end && str_buffer[*end - 1] == ' ') {
80 static void grpc_slice_split_inner(grpc_slice str, const char* sep,
81 grpc_slice_buffer* dst, bool no_space) {
82 const size_t sep_len = strlen(sep);
84 const uint8_t* str_buffer = GRPC_SLICE_START_PTR(str);
87 GPR_ASSERT(sep_len > 0);
89 if (slice_find_separator_offset(str, sep, 0, &begin, &end) != 0) {
93 skip_leading_trailing_spaces(str_buffer, &begin, &end);
95 grpc_slice_buffer_add_indexed(dst, grpc_slice_sub(str, begin, end));
96 } while (slice_find_separator_offset(str, sep, sep_pos + sep_len, &begin,
98 begin = sep_pos + sep_len;
99 end = GRPC_SLICE_LENGTH(str);
101 skip_leading_trailing_spaces(str_buffer, &begin, &end);
103 grpc_slice_buffer_add_indexed(dst, grpc_slice_sub(str, begin, end));
104 } else { /* no sep found, add whole input */
106 end = GRPC_SLICE_LENGTH(str);
108 skip_leading_trailing_spaces(str_buffer, &begin, &end);
110 grpc_slice_buffer_add_indexed(dst, grpc_slice_sub(str, begin, end));
114 void grpc_slice_split(grpc_slice str, const char* sep, grpc_slice_buffer* dst) {
115 grpc_slice_split_inner(str, sep, dst, false);
118 void grpc_slice_split_without_space(grpc_slice str, const char* sep,
119 grpc_slice_buffer* dst) {
120 grpc_slice_split_inner(str, sep, dst, true);
123 bool grpc_parse_slice_to_uint32(grpc_slice str, uint32_t* result) {
124 return gpr_parse_bytes_to_uint32(
125 reinterpret_cast<const char*> GRPC_SLICE_START_PTR(str),
126 GRPC_SLICE_LENGTH(str), result) != 0;