1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 *
17 */
18
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/lib/slice/slice_string_helpers.h"
22
23 #include <string.h>
24
25 #include <grpc/support/log.h>
26
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"
30
grpc_dump_slice(const grpc_slice & s,uint32_t flags)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);
34 }
35
grpc_dump_slice_to_slice(const grpc_slice & s,uint32_t flags)36 grpc_slice grpc_dump_slice_to_slice(const grpc_slice& s, uint32_t flags) {
37 size_t len;
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);
42 }
43
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
46 * str.
47 *
48 * Returns 1 and updates \a begin and \a end. Returns 0 otherwise. */
slice_find_separator_offset(const grpc_slice str,const char * sep,const size_t read_offset,size_t * begin,size_t * end)49 static int slice_find_separator_offset(const grpc_slice str, const char* sep,
50 const size_t read_offset, size_t* begin,
51 size_t* end) {
52 size_t i;
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) {
57 return 0;
58 }
59
60 for (i = 0; i <= str_len - sep_len; i++) {
61 if (memcmp(str_ptr + i, sep, sep_len) == 0) {
62 *begin = read_offset;
63 *end = read_offset + i;
64 return 1;
65 }
66 }
67 return 0;
68 }
69
skip_leading_trailing_spaces(const uint8_t * str_buffer,size_t * begin,size_t * end)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] == ' ') {
73 (*begin)++;
74 }
75 while (*begin < *end && str_buffer[*end - 1] == ' ') {
76 (*end)--;
77 }
78 }
79
grpc_slice_split_inner(grpc_slice str,const char * sep,grpc_slice_buffer * dst,bool no_space)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);
83 size_t begin, end;
84 const uint8_t* str_buffer = GRPC_SLICE_START_PTR(str);
85 size_t sep_pos;
86
87 GPR_ASSERT(sep_len > 0);
88
89 if (slice_find_separator_offset(str, sep, 0, &begin, &end) != 0) {
90 do {
91 sep_pos = end;
92 if (no_space) {
93 skip_leading_trailing_spaces(str_buffer, &begin, &end);
94 }
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,
97 &end) != 0);
98 begin = sep_pos + sep_len;
99 end = GRPC_SLICE_LENGTH(str);
100 if (no_space) {
101 skip_leading_trailing_spaces(str_buffer, &begin, &end);
102 }
103 grpc_slice_buffer_add_indexed(dst, grpc_slice_sub(str, begin, end));
104 } else { /* no sep found, add whole input */
105 begin = 0;
106 end = GRPC_SLICE_LENGTH(str);
107 if (no_space) {
108 skip_leading_trailing_spaces(str_buffer, &begin, &end);
109 }
110 grpc_slice_buffer_add_indexed(dst, grpc_slice_sub(str, begin, end));
111 }
112 }
113
grpc_slice_split(grpc_slice str,const char * sep,grpc_slice_buffer * dst)114 void grpc_slice_split(grpc_slice str, const char* sep, grpc_slice_buffer* dst) {
115 grpc_slice_split_inner(str, sep, dst, false);
116 }
117
grpc_slice_split_without_space(grpc_slice str,const char * sep,grpc_slice_buffer * dst)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);
121 }
122
grpc_parse_slice_to_uint32(grpc_slice str,uint32_t * result)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;
127 }
128