• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Abseil 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 //      https://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 "absl/strings/str_split.h"
16 
17 #include <algorithm>
18 #include <cassert>
19 #include <cstdint>
20 #include <cstdlib>
21 #include <cstring>
22 #include <iterator>
23 #include <limits>
24 #include <memory>
25 
26 #include "absl/base/internal/raw_logging.h"
27 #include "absl/strings/ascii.h"
28 
29 namespace absl {
30 ABSL_NAMESPACE_BEGIN
31 
32 namespace {
33 
34 // This GenericFind() template function encapsulates the finding algorithm
35 // shared between the ByString and ByAnyChar delimiters. The FindPolicy
36 // template parameter allows each delimiter to customize the actual find
37 // function to use and the length of the found delimiter. For example, the
38 // Literal delimiter will ultimately use absl::string_view::find(), and the
39 // AnyOf delimiter will use absl::string_view::find_first_of().
40 template <typename FindPolicy>
GenericFind(absl::string_view text,absl::string_view delimiter,size_t pos,FindPolicy find_policy)41 absl::string_view GenericFind(absl::string_view text,
42                               absl::string_view delimiter, size_t pos,
43                               FindPolicy find_policy) {
44   if (delimiter.empty() && text.length() > 0) {
45     // Special case for empty string delimiters: always return a zero-length
46     // absl::string_view referring to the item at position 1 past pos.
47     return absl::string_view(text.data() + pos + 1, 0);
48   }
49   size_t found_pos = absl::string_view::npos;
50   absl::string_view found(text.data() + text.size(),
51                           0);  // By default, not found
52   found_pos = find_policy.Find(text, delimiter, pos);
53   if (found_pos != absl::string_view::npos) {
54     found = absl::string_view(text.data() + found_pos,
55                               find_policy.Length(delimiter));
56   }
57   return found;
58 }
59 
60 // Finds using absl::string_view::find(), therefore the length of the found
61 // delimiter is delimiter.length().
62 struct LiteralPolicy {
Findabsl::__anona26811a60111::LiteralPolicy63   static size_t Find(absl::string_view text, absl::string_view delimiter,
64                      size_t pos) {
65     return text.find(delimiter, pos);
66   }
Lengthabsl::__anona26811a60111::LiteralPolicy67   static size_t Length(absl::string_view delimiter) {
68     return delimiter.length();
69   }
70 };
71 
72 // Finds using absl::string_view::find_first_of(), therefore the length of the
73 // found delimiter is 1.
74 struct AnyOfPolicy {
Findabsl::__anona26811a60111::AnyOfPolicy75   static size_t Find(absl::string_view text, absl::string_view delimiter,
76                      size_t pos) {
77     return text.find_first_of(delimiter, pos);
78   }
Lengthabsl::__anona26811a60111::AnyOfPolicy79   static size_t Length(absl::string_view /* delimiter */) { return 1; }
80 };
81 
82 }  // namespace
83 
84 //
85 // ByString
86 //
87 
ByString(absl::string_view sp)88 ByString::ByString(absl::string_view sp) : delimiter_(sp) {}
89 
Find(absl::string_view text,size_t pos) const90 absl::string_view ByString::Find(absl::string_view text, size_t pos) const {
91   if (delimiter_.length() == 1) {
92     // Much faster to call find on a single character than on an
93     // absl::string_view.
94     size_t found_pos = text.find(delimiter_[0], pos);
95     if (found_pos == absl::string_view::npos)
96       return absl::string_view(text.data() + text.size(), 0);
97     return text.substr(found_pos, 1);
98   }
99   return GenericFind(text, delimiter_, pos, LiteralPolicy());
100 }
101 
102 //
103 // ByChar
104 //
105 
Find(absl::string_view text,size_t pos) const106 absl::string_view ByChar::Find(absl::string_view text, size_t pos) const {
107   size_t found_pos = text.find(c_, pos);
108   if (found_pos == absl::string_view::npos)
109     return absl::string_view(text.data() + text.size(), 0);
110   return text.substr(found_pos, 1);
111 }
112 
113 //
114 // ByAnyChar
115 //
116 
ByAnyChar(absl::string_view sp)117 ByAnyChar::ByAnyChar(absl::string_view sp) : delimiters_(sp) {}
118 
Find(absl::string_view text,size_t pos) const119 absl::string_view ByAnyChar::Find(absl::string_view text, size_t pos) const {
120   return GenericFind(text, delimiters_, pos, AnyOfPolicy());
121 }
122 
123 //
124 // ByLength
125 //
ByLength(ptrdiff_t length)126 ByLength::ByLength(ptrdiff_t length) : length_(length) {
127   ABSL_RAW_CHECK(length > 0, "");
128 }
129 
Find(absl::string_view text,size_t pos) const130 absl::string_view ByLength::Find(absl::string_view text, size_t pos) const {
131   pos = std::min(pos, text.size());  // truncate `pos`
132   absl::string_view substr = text.substr(pos);
133   // If the string is shorter than the chunk size we say we
134   // "can't find the delimiter" so this will be the last chunk.
135   if (substr.length() <= static_cast<size_t>(length_))
136     return absl::string_view(text.data() + text.size(), 0);
137 
138   return absl::string_view(substr.data() + length_, 0);
139 }
140 
141 ABSL_NAMESPACE_END
142 }  // namespace absl
143