bloaty/third_party/abseil-cpp/absl/strings/string_view.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/string_view.h"
16 
17 #ifndef ABSL_USES_STD_STRING_VIEW
18 
19 #include <algorithm>
20 #include <climits>
21 #include <cstring>
22 #include <ostream>
23 
24 #include "absl/strings/internal/memutil.h"
25 
26 namespace absl {
28 
29 namespace {
30 void WritePadding(std::ostream& o, size_t pad) {
31  char fill_buf[32];
32  memset(fill_buf, o.fill(), sizeof(fill_buf));
33  while (pad) {
34  size_t n = std::min(pad, sizeof(fill_buf));
35  o.write(fill_buf, n);
36  pad -= n;
37  }
38 }
39 
40 class LookupTable {
41  public:
42  // For each character in wanted, sets the index corresponding
43  // to the ASCII code of that character. This is used by
44  // the find_.*_of methods below to tell whether or not a character is in
45  // the lookup table in constant time.
46  explicit LookupTable(string_view wanted) {
47  for (char c : wanted) {
48  table_[Index(c)] = true;
49  }
50  }
51  bool operator[](char c) const { return table_[Index(c)]; }
52 
53  private:
54  static unsigned char Index(char c) { return static_cast<unsigned char>(c); }
55  bool table_[UCHAR_MAX + 1] = {};
56 };
57 
58 } // namespace
59 
60 std::ostream& operator<<(std::ostream& o, string_view piece) {
61  std::ostream::sentry sentry(o);
62  if (sentry) {
63  size_t lpad = 0;
64  size_t rpad = 0;
65  if (static_cast<size_t>(o.width()) > piece.size()) {
66  size_t pad = o.width() - piece.size();
67  if ((o.flags() & o.adjustfield) == o.left) {
68  rpad = pad;
69  } else {
70  lpad = pad;
71  }
72  }
73  if (lpad) WritePadding(o, lpad);
74  o.write(piece.data(), piece.size());
75  if (rpad) WritePadding(o, rpad);
76  o.width(0);
77  }
78  return o;
79 }
80 
82  noexcept {
83  if (empty() || pos > length_) {
84  if (empty() && pos == 0 && s.empty()) return 0;
85  return npos;
86  }
87  const char* result =
88  strings_internal::memmatch(ptr_ + pos, length_ - pos, s.ptr_, s.length_);
89  return result ? result - ptr_ : npos;
90 }
91 
92 string_view::size_type string_view::find(char c, size_type pos) const noexcept {
93  if (empty() || pos >= length_) {
94  return npos;
95  }
96  const char* result =
97  static_cast<const char*>(memchr(ptr_ + pos, c, length_ - pos));
98  return result != nullptr ? result - ptr_ : npos;
99 }
100 
102  noexcept {
103  if (length_ < s.length_) return npos;
104  if (s.empty()) return std::min(length_, pos);
105  const char* last = ptr_ + std::min(length_ - s.length_, pos) + s.length_;
106  const char* result = std::find_end(ptr_, last, s.ptr_, s.ptr_ + s.length_);
107  return result != last ? result - ptr_ : npos;
108 }
109 
110 // Search range is [0..pos] inclusive. If pos == npos, search everything.
111 string_view::size_type string_view::rfind(char c, size_type pos) const
112  noexcept {
113  // Note: memrchr() is not available on Windows.
114  if (empty()) return npos;
115  for (size_type i = std::min(pos, length_ - 1);; --i) {
116  if (ptr_[i] == c) {
117  return i;
118  }
119  if (i == 0) break;
120  }
121  return npos;
122 }
123 
125  size_type pos) const
126  noexcept {
127  if (empty() || s.empty()) {
128  return npos;
129  }
130  // Avoid the cost of LookupTable() for a single-character search.
131  if (s.length_ == 1) return find_first_of(s.ptr_[0], pos);
132  LookupTable tbl(s);
133  for (size_type i = pos; i < length_; ++i) {
134  if (tbl[ptr_[i]]) {
135  return i;
136  }
137  }
138  return npos;
139 }
140 
142  size_type pos) const
143  noexcept {
144  if (empty()) return npos;
145  // Avoid the cost of LookupTable() for a single-character search.
146  if (s.length_ == 1) return find_first_not_of(s.ptr_[0], pos);
147  LookupTable tbl(s);
148  for (size_type i = pos; i < length_; ++i) {
149  if (!tbl[ptr_[i]]) {
150  return i;
151  }
152  }
153  return npos;
154 }
155 
157  size_type pos) const
158  noexcept {
159  if (empty()) return npos;
160  for (; pos < length_; ++pos) {
161  if (ptr_[pos] != c) {
162  return pos;
163  }
164  }
165  return npos;
166 }
167 
169  size_type pos) const noexcept {
170  if (empty() || s.empty()) return npos;
171  // Avoid the cost of LookupTable() for a single-character search.
172  if (s.length_ == 1) return find_last_of(s.ptr_[0], pos);
173  LookupTable tbl(s);
174  for (size_type i = std::min(pos, length_ - 1);; --i) {
175  if (tbl[ptr_[i]]) {
176  return i;
177  }
178  if (i == 0) break;
179  }
180  return npos;
181 }
182 
184  size_type pos) const
185  noexcept {
186  if (empty()) return npos;
187  size_type i = std::min(pos, length_ - 1);
188  if (s.empty()) return i;
189  // Avoid the cost of LookupTable() for a single-character search.
190  if (s.length_ == 1) return find_last_not_of(s.ptr_[0], pos);
191  LookupTable tbl(s);
192  for (;; --i) {
193  if (!tbl[ptr_[i]]) {
194  return i;
195  }
196  if (i == 0) break;
197  }
198  return npos;
199 }
200 
202  size_type pos) const
203  noexcept {
204  if (empty()) return npos;
205  size_type i = std::min(pos, length_ - 1);
206  for (;; --i) {
207  if (ptr_[i] != c) {
208  return i;
209  }
210  if (i == 0) break;
211  }
212  return npos;
213 }
214 
215 // MSVC has non-standard behavior that implicitly creates definitions for static
216 // const members. These implicit definitions conflict with explicit out-of-class
217 // member definitions that are required by the C++ standard, resulting in
218 // LNK1169 "multiply defined" errors at link time. __declspec(selectany) asks
219 // MSVC to choose only one definition for the symbol it decorates. See details
220 // at https://msdn.microsoft.com/en-us/library/34h23df8(v=vs.100).aspx
221 #ifdef _MSC_VER
222 #define ABSL_STRING_VIEW_SELECTANY __declspec(selectany)
223 #else
224 #define ABSL_STRING_VIEW_SELECTANY
225 #endif
226 
231 
233 } // namespace absl
234 
235 #endif // ABSL_USES_STD_STRING_VIEW
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
pos
int pos
Definition: libuv/docs/code/tty-gravity/main.c:11
memset
return memset(p, 0, total)
absl::operator<<
ABSL_NAMESPACE_BEGIN std::ostream & operator<<(std::ostream &os, absl::LogSeverity s)
Definition: abseil-cpp/absl/base/log_severity.cc:24
absl::string_view::find
size_type find(string_view s, size_type pos=0) const noexcept
Definition: abseil-cpp/absl/strings/string_view.cc:81
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
ABSL_STRING_VIEW_SELECTANY
#define ABSL_STRING_VIEW_SELECTANY
Definition: bloaty/third_party/abseil-cpp/absl/strings/string_view.cc:224
absl::FormatConversionChar::s
@ s
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
ptr_
std::string * ptr_
Definition: abseil-cpp/absl/container/internal/container_memory_test.cc:103
absl::string_view::find_first_of
size_type find_first_of(string_view s, size_type pos=0) const noexcept
Definition: abseil-cpp/absl/strings/string_view.cc:124
absl::string_view::kMaxSize
static constexpr size_type kMaxSize
Definition: abseil-cpp/absl/strings/string_view.h:606
o
UnboundConversion o
Definition: third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc:97
absl::ABSL_NAMESPACE_BEGIN::LookupTable::LookupTable
LookupTable(string_view wanted)
Definition: bloaty/third_party/abseil-cpp/absl/strings/string_view.cc:46
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
absl::string_view::size
constexpr size_type size() const noexcept
Definition: abseil-cpp/absl/strings/string_view.h:277
absl::ABSL_NAMESPACE_BEGIN::WritePadding
void WritePadding(std::ostream &o, size_t pad)
Definition: abseil-cpp/absl/strings/string_view.cc:30
pad
int pad
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/statusor_test.cc:47
absl::ABSL_NAMESPACE_BEGIN::LookupTable::operator[]
bool operator[](char c) const
Definition: bloaty/third_party/abseil-cpp/absl/strings/string_view.cc:51
min
#define min(a, b)
Definition: qsort.h:83
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
google_benchmark.example.empty
def empty(state)
Definition: example.py:31
absl::string_view::size_type
size_t size_type
Definition: abseil-cpp/absl/strings/string_view.h:179
absl::string_view::find_first_not_of
size_type find_first_not_of(string_view s, size_type pos=0) const noexcept
Definition: abseil-cpp/absl/strings/string_view.cc:140
table_
Vec< int32_t > table_
Definition: abseil-cpp/absl/synchronization/internal/graphcycles.cc:210
absl::string_view::rfind
size_type rfind(string_view s, size_type pos=npos) const noexcept
Definition: abseil-cpp/absl/strings/string_view.cc:101
absl::string_view::find_last_of
size_type find_last_of(string_view s, size_type pos=npos) const noexcept
Definition: abseil-cpp/absl/strings/string_view.cc:165
absl::strings_internal::memmatch
const char * memmatch(const char *phaystack, size_t haylen, const char *pneedle, size_t neelen)
Definition: abseil-cpp/absl/strings/internal/memutil.cc:89
absl::ABSL_NAMESPACE_BEGIN::LookupTable::Index
static unsigned char Index(char c)
Definition: bloaty/third_party/abseil-cpp/absl/strings/string_view.cc:54
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
google::protobuf::python::descriptor::Index
static PyObject * Index(PyContainer *self, PyObject *item)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/descriptor_containers.cc:672
absl::string_view::npos
static constexpr size_type npos
Definition: abseil-cpp/absl/strings/string_view.h:182
absl::string_view::data
constexpr const_pointer data() const noexcept
Definition: abseil-cpp/absl/strings/string_view.h:336
absl::string_view::find_last_not_of
size_type find_last_not_of(string_view s, size_type pos=npos) const noexcept
Definition: abseil-cpp/absl/strings/string_view.cc:180
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:21