1 // Copyright 2021 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <grpc/support/port_platform.h>
16 
17 #include "src/core/lib/slice/slice_split.h"
18 
19 #include <string.h>
20 
21 #include <grpc/support/log.h>
22 
23 /** Finds the initial (\a begin) and final (\a end) offsets of the next
24  * substring from \a str + \a read_offset until the next \a sep or the end of \a
25  * str.
26  *
27  * 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)28 static int slice_find_separator_offset(const grpc_slice str, const char* sep,
29                                        const size_t read_offset, size_t* begin,
30                                        size_t* end) {
31   size_t i;
32   const uint8_t* str_ptr = GRPC_SLICE_START_PTR(str) + read_offset;
33   const size_t str_len = GRPC_SLICE_LENGTH(str) - read_offset;
34   const size_t sep_len = strlen(sep);
35   if (str_len < sep_len) {
36     return 0;
37   }
38 
39   for (i = 0; i <= str_len - sep_len; i++) {
40     if (memcmp(str_ptr + i, sep, sep_len) == 0) {
41       *begin = read_offset;
42       *end = read_offset + i;
43       return 1;
44     }
45   }
46   return 0;
47 }
48 
skip_leading_trailing_spaces(const uint8_t * str_buffer,size_t * begin,size_t * end)49 static void skip_leading_trailing_spaces(const uint8_t* str_buffer,
50                                          size_t* begin, size_t* end) {
51   while (*begin < *end && str_buffer[*begin] == ' ') {
52     (*begin)++;
53   }
54   while (*begin < *end && str_buffer[*end - 1] == ' ') {
55     (*end)--;
56   }
57 }
58 
grpc_slice_split_inner(grpc_slice str,const char * sep,grpc_slice_buffer * dst,bool no_space)59 static void grpc_slice_split_inner(grpc_slice str, const char* sep,
60                                    grpc_slice_buffer* dst, bool no_space) {
61   const size_t sep_len = strlen(sep);
62   size_t begin, end;
63   const uint8_t* str_buffer = GRPC_SLICE_START_PTR(str);
64   size_t sep_pos;
65 
66   GPR_ASSERT(sep_len > 0);
67 
68   if (slice_find_separator_offset(str, sep, 0, &begin, &end) != 0) {
69     do {
70       sep_pos = end;
71       if (no_space) {
72         skip_leading_trailing_spaces(str_buffer, &begin, &end);
73       }
74       grpc_slice_buffer_add_indexed(dst, grpc_slice_sub(str, begin, end));
75     } while (slice_find_separator_offset(str, sep, sep_pos + sep_len, &begin,
76                                          &end) != 0);
77     begin = sep_pos + sep_len;
78     end = GRPC_SLICE_LENGTH(str);
79     if (no_space) {
80       skip_leading_trailing_spaces(str_buffer, &begin, &end);
81     }
82     grpc_slice_buffer_add_indexed(dst, grpc_slice_sub(str, begin, end));
83   } else { /* no sep found, add whole input */
84     begin = 0;
85     end = GRPC_SLICE_LENGTH(str);
86     if (no_space) {
87       skip_leading_trailing_spaces(str_buffer, &begin, &end);
88     }
89     grpc_slice_buffer_add_indexed(dst, grpc_slice_sub(str, begin, end));
90   }
91 }
92 
grpc_slice_split(grpc_slice str,const char * sep,grpc_slice_buffer * dst)93 void grpc_slice_split(grpc_slice str, const char* sep, grpc_slice_buffer* dst) {
94   grpc_slice_split_inner(str, sep, dst, false);
95 }
96 
grpc_slice_split_without_space(grpc_slice str,const char * sep,grpc_slice_buffer * dst)97 void grpc_slice_split_without_space(grpc_slice str, const char* sep,
98                                     grpc_slice_buffer* dst) {
99   grpc_slice_split_inner(str, sep, dst, true);
100 }
101