str_split.cc
Go to the documentation of this file.
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 
27 #include "absl/strings/ascii.h"
28 
29 namespace absl {
30 
31 namespace {
32 
33 // This GenericFind() template function encapsulates the finding algorithm
34 // shared between the ByString and ByAnyChar delimiters. The FindPolicy
35 // template parameter allows each delimiter to customize the actual find
36 // function to use and the length of the found delimiter. For example, the
37 // Literal delimiter will ultimately use absl::string_view::find(), and the
38 // AnyOf delimiter will use absl::string_view::find_first_of().
39 template <typename FindPolicy>
40 absl::string_view GenericFind(absl::string_view text,
41  absl::string_view delimiter, size_t pos,
42  FindPolicy find_policy) {
43  if (delimiter.empty() && text.length() > 0) {
44  // Special case for empty std::string delimiters: always return a zero-length
45  // absl::string_view referring to the item at position 1 past pos.
46  return absl::string_view(text.data() + pos + 1, 0);
47  }
48  size_t found_pos = absl::string_view::npos;
49  absl::string_view found(text.data() + text.size(),
50  0); // By default, not found
51  found_pos = find_policy.Find(text, delimiter, pos);
52  if (found_pos != absl::string_view::npos) {
53  found = absl::string_view(text.data() + found_pos,
54  find_policy.Length(delimiter));
55  }
56  return found;
57 }
58 
59 // Finds using absl::string_view::find(), therefore the length of the found
60 // delimiter is delimiter.length().
61 struct LiteralPolicy {
62  size_t Find(absl::string_view text, absl::string_view delimiter, size_t pos) {
63  return text.find(delimiter, pos);
64  }
65  size_t Length(absl::string_view delimiter) { return delimiter.length(); }
66 };
67 
68 // Finds using absl::string_view::find_first_of(), therefore the length of the
69 // found delimiter is 1.
70 struct AnyOfPolicy {
71  size_t Find(absl::string_view text, absl::string_view delimiter, size_t pos) {
72  return text.find_first_of(delimiter, pos);
73  }
74  size_t Length(absl::string_view /* delimiter */) { return 1; }
75 };
76 
77 } // namespace
78 
79 //
80 // ByString
81 //
82 
83 ByString::ByString(absl::string_view sp) : delimiter_(sp) {}
84 
86  if (delimiter_.length() == 1) {
87  // Much faster to call find on a single character than on an
88  // absl::string_view.
89  size_t found_pos = text.find(delimiter_[0], pos);
90  if (found_pos == absl::string_view::npos)
91  return absl::string_view(text.data() + text.size(), 0);
92  return text.substr(found_pos, 1);
93  }
94  return GenericFind(text, delimiter_, pos, LiteralPolicy());
95 }
96 
97 //
98 // ByChar
99 //
100 
102  size_t found_pos = text.find(c_, pos);
103  if (found_pos == absl::string_view::npos)
104  return absl::string_view(text.data() + text.size(), 0);
105  return text.substr(found_pos, 1);
106 }
107 
108 //
109 // ByAnyChar
110 //
111 
112 ByAnyChar::ByAnyChar(absl::string_view sp) : delimiters_(sp) {}
113 
115  return GenericFind(text, delimiters_, pos, AnyOfPolicy());
116 }
117 
118 //
119 // ByLength
120 //
121 ByLength::ByLength(ptrdiff_t length) : length_(length) {
122  ABSL_RAW_CHECK(length > 0, "");
123 }
124 
126  size_t pos) const {
127  pos = std::min(pos, text.size()); // truncate `pos`
128  absl::string_view substr = text.substr(pos);
129  // If the std::string is shorter than the chunk size we say we
130  // "can't find the delimiter" so this will be the last chunk.
131  if (substr.length() <= static_cast<size_t>(length_))
132  return absl::string_view(text.data() + text.size(), 0);
133 
134  return absl::string_view(substr.data() + length_, 0);
135 }
136 
137 } // namespace absl
const ptrdiff_t length_
Definition: str_split.h:217
string_view substr(size_type pos, size_type n=npos) const
Definition: string_view.h:354
constexpr size_t Find(Needle, Needle, Ts...)
Definition: layout.h:266
ByAnyChar(absl::string_view sp)
Definition: str_split.cc:112
size_type find(string_view s, size_type pos=0) const noexcept
Definition: string_view.cc:92
const std::string delimiter_
Definition: str_split.h:128
static constexpr size_type npos
Definition: string_view.h:159
absl::string_view Find(absl::string_view text, size_t pos) const
Definition: str_split.cc:125
absl::string_view Find(absl::string_view text, size_t pos) const
Definition: str_split.cc:101
absl::string_view Find(absl::string_view text, size_t pos) const
Definition: str_split.cc:85
Definition: algorithm.h:29
ByLength(ptrdiff_t length)
Definition: str_split.cc:121
constexpr size_type size() const noexcept
Definition: string_view.h:260
#define ABSL_RAW_CHECK(condition, message)
Definition: raw_logging.h:57
absl::string_view Find(absl::string_view text, size_t pos) const
Definition: str_split.cc:114
constexpr bool empty() const noexcept
Definition: string_view.h:277
constexpr size_type length() const noexcept
Definition: string_view.h:267
const std::string delimiters_
Definition: str_split.h:186
constexpr const_pointer data() const noexcept
Definition: string_view.h:302
size_type find_first_of(string_view s, size_type pos=0) const noexcept
Definition: string_view.cc:135
std::size_t length
Definition: test_util.cc:52
ByString(absl::string_view sp)
Definition: str_split.cc:83


abseil_cpp
Author(s):
autogenerated on Tue Jun 18 2019 19:44:37