Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
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
00034
00035
00036
00037
00038
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
00045
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);
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
00060
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
00069
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 ) { return 1; }
00075 };
00076
00077 }
00078
00079
00080
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
00088
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
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
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
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());
00128 absl::string_view substr = text.substr(pos);
00129
00130
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 }