str_split.cc
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include "absl/strings/str_split.h"
00016 
00017 #include <algorithm>
00018 #include <cassert>
00019 #include <cstdint>
00020 #include <cstdlib>
00021 #include <cstring>
00022 #include <iterator>
00023 #include <limits>
00024 #include <memory>
00025 
00026 #include "absl/base/internal/raw_logging.h"
00027 #include "absl/strings/ascii.h"
00028 
00029 namespace absl {
00030 
00031 namespace {
00032 
00033 // This GenericFind() template function encapsulates the finding algorithm
00034 // shared between the ByString and ByAnyChar delimiters. The FindPolicy
00035 // template parameter allows each delimiter to customize the actual find
00036 // function to use and the length of the found delimiter. For example, the
00037 // Literal delimiter will ultimately use absl::string_view::find(), and the
00038 // AnyOf delimiter will use absl::string_view::find_first_of().
00039 template <typename FindPolicy>
00040 absl::string_view GenericFind(absl::string_view text,
00041                               absl::string_view delimiter, size_t pos,
00042                               FindPolicy find_policy) {
00043   if (delimiter.empty() && text.length() > 0) {
00044     // Special case for empty std::string delimiters: always return a zero-length
00045     // absl::string_view referring to the item at position 1 past pos.
00046     return absl::string_view(text.data() + pos + 1, 0);
00047   }
00048   size_t found_pos = absl::string_view::npos;
00049   absl::string_view found(text.data() + text.size(),
00050                           0);  // By default, not found
00051   found_pos = find_policy.Find(text, delimiter, pos);
00052   if (found_pos != absl::string_view::npos) {
00053     found = absl::string_view(text.data() + found_pos,
00054                               find_policy.Length(delimiter));
00055   }
00056   return found;
00057 }
00058 
00059 // Finds using absl::string_view::find(), therefore the length of the found
00060 // delimiter is delimiter.length().
00061 struct LiteralPolicy {
00062   size_t Find(absl::string_view text, absl::string_view delimiter, size_t pos) {
00063     return text.find(delimiter, pos);
00064   }
00065   size_t Length(absl::string_view delimiter) { return delimiter.length(); }
00066 };
00067 
00068 // Finds using absl::string_view::find_first_of(), therefore the length of the
00069 // found delimiter is 1.
00070 struct AnyOfPolicy {
00071   size_t Find(absl::string_view text, absl::string_view delimiter, size_t pos) {
00072     return text.find_first_of(delimiter, pos);
00073   }
00074   size_t Length(absl::string_view /* delimiter */) { return 1; }
00075 };
00076 
00077 }  // namespace
00078 
00079 //
00080 // ByString
00081 //
00082 
00083 ByString::ByString(absl::string_view sp) : delimiter_(sp) {}
00084 
00085 absl::string_view ByString::Find(absl::string_view text, size_t pos) const {
00086   if (delimiter_.length() == 1) {
00087     // Much faster to call find on a single character than on an
00088     // absl::string_view.
00089     size_t found_pos = text.find(delimiter_[0], pos);
00090     if (found_pos == absl::string_view::npos)
00091       return absl::string_view(text.data() + text.size(), 0);
00092     return text.substr(found_pos, 1);
00093   }
00094   return GenericFind(text, delimiter_, pos, LiteralPolicy());
00095 }
00096 
00097 //
00098 // ByChar
00099 //
00100 
00101 absl::string_view ByChar::Find(absl::string_view text, size_t pos) const {
00102   size_t found_pos = text.find(c_, pos);
00103   if (found_pos == absl::string_view::npos)
00104     return absl::string_view(text.data() + text.size(), 0);
00105   return text.substr(found_pos, 1);
00106 }
00107 
00108 //
00109 // ByAnyChar
00110 //
00111 
00112 ByAnyChar::ByAnyChar(absl::string_view sp) : delimiters_(sp) {}
00113 
00114 absl::string_view ByAnyChar::Find(absl::string_view text, size_t pos) const {
00115   return GenericFind(text, delimiters_, pos, AnyOfPolicy());
00116 }
00117 
00118 //
00119 // ByLength
00120 //
00121 ByLength::ByLength(ptrdiff_t length) : length_(length) {
00122   ABSL_RAW_CHECK(length > 0, "");
00123 }
00124 
00125 absl::string_view ByLength::Find(absl::string_view text,
00126                                       size_t pos) const {
00127   pos = std::min(pos, text.size());  // truncate `pos`
00128   absl::string_view substr = text.substr(pos);
00129   // If the std::string is shorter than the chunk size we say we
00130   // "can't find the delimiter" so this will be the last chunk.
00131   if (substr.length() <= static_cast<size_t>(length_))
00132     return absl::string_view(text.data() + text.size(), 0);
00133 
00134   return absl::string_view(substr.data() + length_, 0);
00135 }
00136 
00137 }  // namespace absl


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:15