protobuf/src/google/protobuf/stubs/stringpiece.cc
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <google/protobuf/stubs/stringpiece.h>
31 
32 #include <string.h>
33 #include <algorithm>
34 #include <climits>
35 #include <string>
36 #include <ostream>
37 
38 #include <google/protobuf/stubs/logging.h>
39 
40 namespace google {
41 namespace protobuf {
42 namespace stringpiece_internal {
43 
44 std::ostream& operator<<(std::ostream& o, StringPiece piece) {
45  o.write(piece.data(), piece.size());
46  return o;
47 }
48 
49 void StringPiece::LogFatalSizeTooBig(size_t size, const char* details) {
50  GOOGLE_LOG(FATAL) << "size too big: " << size << " details: " << details;
51 }
52 
54  target->assign(ptr_, length_);
55 }
56 
58  target->append(ptr_, length_);
59 }
60 
62  if (starts_with(x)) {
63  ptr_ += x.length_;
64  length_ -= x.length_;
65  return true;
66  }
67  return false;
68 }
69 
71  if (ends_with(x)) {
72  length_ -= x.length_;
73  return true;
74  }
75  return false;
76 }
77 
79  size_type pos) const {
81  memcpy(buf, ptr_ + pos, ret);
82  return ret;
83 }
84 
86  return find(s, 0) != npos;
87 }
88 
90  if (length_ <= 0 || pos > static_cast<size_type>(length_)) {
91  if (length_ == 0 && pos == 0 && s.length_ == 0) return 0;
92  return npos;
93  }
94  const char *result = std::search(ptr_ + pos, ptr_ + length_,
95  s.ptr_, s.ptr_ + s.length_);
96  return result == ptr_ + length_ ? npos : result - ptr_;
97 }
98 
100  if (length_ <= 0 || pos >= static_cast<size_type>(length_)) {
101  return npos;
102  }
103  const char* result = static_cast<const char*>(
104  memchr(ptr_ + pos, c, length_ - pos));
105  return result != nullptr ? result - ptr_ : npos;
106 }
107 
109  if (length_ < s.length_) return npos;
110  const size_t ulen = length_;
111  if (s.length_ == 0) return std::min(ulen, pos);
112 
113  const char* last = ptr_ + std::min(ulen - s.length_, pos) + s.length_;
114  const char* result = std::find_end(ptr_, last, s.ptr_, s.ptr_ + s.length_);
115  return result != last ? result - ptr_ : npos;
116 }
117 
118 // Search range is [0..pos] inclusive. If pos == npos, search everything.
120  // Note: memrchr() is not available on Windows.
121  if (empty()) return npos;
122  for (size_type i = std::min(pos, length_ - 1);; --i) {
123  if (ptr_[i] == c) {
124  return i;
125  }
126  if (i == 0) break;
127  }
128  return npos;
129 }
130 
131 // For each character in characters_wanted, sets the index corresponding
132 // to the ASCII code of that character to 1 in table. This is used by
133 // the find_.*_of methods below to tell whether or not a character is in
134 // the lookup table in constant time.
135 // The argument `table' must be an array that is large enough to hold all
136 // the possible values of an unsigned char. Thus it should be be declared
137 // as follows:
138 // bool table[UCHAR_MAX + 1]
139 static inline void BuildLookupTable(StringPiece characters_wanted,
140  bool* table) {
141  const StringPiece::size_type length = characters_wanted.length();
142  const char* const data = characters_wanted.data();
143  for (StringPiece::size_type i = 0; i < length; ++i) {
144  table[static_cast<unsigned char>(data[i])] = true;
145  }
146 }
147 
149  size_type pos) const {
150  if (empty() || s.empty()) {
151  return npos;
152  }
153  // Avoid the cost of BuildLookupTable() for a single-character search.
154  if (s.length_ == 1) return find_first_of(s.ptr_[0], pos);
155 
156  bool lookup[UCHAR_MAX + 1] = { false };
158  for (size_type i = pos; i < length_; ++i) {
159  if (lookup[static_cast<unsigned char>(ptr_[i])]) {
160  return i;
161  }
162  }
163  return npos;
164 }
165 
167  size_type pos) const {
168  if (empty()) return npos;
169  if (s.empty()) return 0;
170  // Avoid the cost of BuildLookupTable() for a single-character search.
171  if (s.length_ == 1) return find_first_not_of(s.ptr_[0], pos);
172 
173  bool lookup[UCHAR_MAX + 1] = { false };
175  for (size_type i = pos; i < length_; ++i) {
176  if (!lookup[static_cast<unsigned char>(ptr_[i])]) {
177  return i;
178  }
179  }
180  return npos;
181 }
182 
184  size_type pos) const {
185  if (empty()) return npos;
186 
187  for (; pos < static_cast<size_type>(length_); ++pos) {
188  if (ptr_[pos] != c) {
189  return pos;
190  }
191  }
192  return npos;
193 }
194 
196  size_type pos) const {
197  if (empty() || s.empty()) return npos;
198  // Avoid the cost of BuildLookupTable() for a single-character search.
199  if (s.length_ == 1) return find_last_of(s.ptr_[0], pos);
200 
201  bool lookup[UCHAR_MAX + 1] = { false };
203  for (size_type i = std::min(pos, length_ - 1);; --i) {
204  if (lookup[static_cast<unsigned char>(ptr_[i])]) {
205  return i;
206  }
207  if (i == 0) break;
208  }
209  return npos;
210 }
211 
213  size_type pos) const {
214  if (empty()) return npos;
215 
216  size_type i = std::min(pos, length() - 1);
217  if (s.empty()) return i;
218 
219  // Avoid the cost of BuildLookupTable() for a single-character search.
220  if (s.length_ == 1) return find_last_not_of(s.ptr_[0], pos);
221 
222  bool lookup[UCHAR_MAX + 1] = { false };
224  for (;; --i) {
225  if (!lookup[static_cast<unsigned char>(ptr_[i])]) {
226  return i;
227  }
228  if (i == 0) break;
229  }
230  return npos;
231 }
232 
234  size_type pos) const {
235  if (empty()) return npos;
236  size_type i = std::min(pos, length_ - 1);
237  for (;; --i) {
238  if (ptr_[i] != c) {
239  return i;
240  }
241  if (i == 0) break;
242  }
243  return npos;
244 }
245 
247  if (pos > length()) pos = length();
248  if (n > length_ - pos) n = length() - pos;
249  return StringPiece(ptr_ + pos, n);
250 }
251 
252 const StringPiece::size_type StringPiece::npos = size_type(-1);
253 
254 } // namespace stringpiece_internal
255 } // namespace protobuf
256 } // namespace google
google::protobuf::stringpiece_internal::StringPiece::size
size_type size() const
Definition: protobuf/src/google/protobuf/stubs/stringpiece.h:238
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
google::protobuf::stringpiece_internal::StringPiece::LogFatalSizeTooBig
static void LogFatalSizeTooBig(size_type size, const char *details)
Definition: protobuf/src/google/protobuf/stubs/stringpiece.cc:49
google::protobuf::stringpiece_internal::StringPiece::length_
size_type length_
Definition: protobuf/src/google/protobuf/stubs/stringpiece.h:180
pos
int pos
Definition: libuv/docs/code/tty-gravity/main.c:11
google::protobuf::stringpiece_internal::StringPiece
Definition: protobuf/src/google/protobuf/stubs/stringpiece.h:163
google::protobuf::stringpiece_internal::StringPiece::starts_with
bool starts_with(StringPiece x) const
Definition: protobuf/src/google/protobuf/stubs/stringpiece.h:285
google::protobuf::stringpiece_internal::StringPiece::data
const_pointer data() const
Definition: protobuf/src/google/protobuf/stubs/stringpiece.h:237
google::protobuf::stringpiece_internal::StringPiece::Consume
bool Consume(StringPiece x)
Definition: protobuf/src/google/protobuf/stubs/stringpiece.cc:61
string.h
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
google::protobuf::stringpiece_internal::BuildLookupTable
static void BuildLookupTable(StringPiece characters_wanted, bool *table)
Definition: protobuf/src/google/protobuf/stubs/stringpiece.cc:139
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
google::protobuf::stringpiece_internal::StringPiece::StringPiece
StringPiece()
Definition: protobuf/src/google/protobuf/stubs/stringpiece.h:206
google::protobuf::stringpiece_internal::StringPiece::ptr_
const char * ptr_
Definition: protobuf/src/google/protobuf/stubs/stringpiece.h:179
o
UnboundConversion o
Definition: third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc:97
google::protobuf::stringpiece_internal::StringPiece::rfind
size_type rfind(StringPiece s, size_type pos=npos) const
Definition: protobuf/src/google/protobuf/stubs/stringpiece.cc:108
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
google::protobuf::stringpiece_internal::StringPiece::ends_with
bool ends_with(StringPiece x) const
Definition: protobuf/src/google/protobuf/stubs/stringpiece.h:290
google::protobuf::stringpiece_internal::StringPiece::find_first_not_of
size_type find_first_not_of(StringPiece s, size_type pos=0) const
Definition: protobuf/src/google/protobuf/stubs/stringpiece.cc:166
google::protobuf::stringpiece_internal::StringPiece::substr
StringPiece substr(size_type pos, size_type n=npos) const
Definition: protobuf/src/google/protobuf/stubs/stringpiece.cc:246
google::protobuf::stringpiece_internal::StringPiece::copy
size_type copy(char *buf, size_type n, size_type pos=0) const
Definition: protobuf/src/google/protobuf/stubs/stringpiece.cc:78
google::protobuf::stringpiece_internal::StringPiece::CopyToString
void CopyToString(std::string *target) const
Definition: protobuf/src/google/protobuf/stubs/stringpiece.cc:53
google::protobuf::stringpiece_internal::StringPiece::find
size_type find(StringPiece s, size_type pos=0) const
Definition: protobuf/src/google/protobuf/stubs/stringpiece.cc:89
search.search
def search(target, ideal_distance, stop_event, maximum_hashes, interesting_hamming_distance=None)
Definition: search.py:99
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
min
#define min(a, b)
Definition: qsort.h:83
google::protobuf::stringpiece_internal::StringPiece::empty
bool empty() const
Definition: protobuf/src/google/protobuf/stubs/stringpiece.h:240
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
google::protobuf::stringpiece_internal::StringPiece::length
size_type length() const
Definition: protobuf/src/google/protobuf/stubs/stringpiece.h:239
details
static grpc_slice details
Definition: test/core/fling/client.cc:46
FATAL
#define FATAL(msg)
Definition: task.h:88
google::protobuf::stringpiece_internal::StringPiece::contains
bool contains(StringPiece s) const
Definition: protobuf/src/google/protobuf/stubs/stringpiece.cc:85
google::protobuf::stringpiece_internal::StringPiece::size_type
size_t size_type
Definition: protobuf/src/google/protobuf/stubs/stringpiece.h:175
google::protobuf::stringpiece_internal::operator<<
std::ostream & operator<<(std::ostream &o, StringPiece piece)
Definition: protobuf/src/google/protobuf/stubs/stringpiece.cc:44
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
google::protobuf::stringpiece_internal::StringPiece::find_last_of
size_type find_last_of(StringPiece s, size_type pos=npos) const
Definition: protobuf/src/google/protobuf/stubs/stringpiece.cc:195
google::protobuf::stringpiece_internal::StringPiece::AppendToString
void AppendToString(std::string *target) const
Definition: protobuf/src/google/protobuf/stubs/stringpiece.cc:57
google::protobuf::stringpiece_internal::StringPiece::find_first_of
size_type find_first_of(StringPiece s, size_type pos=0) const
Definition: protobuf/src/google/protobuf/stubs/stringpiece.cc:148
lookup
static bool lookup(const upb_table *t, lookupkey_t key, upb_value *v, uint32_t hash, eqlfunc_t *eql)
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.c:1417
table
uint8_t table[256]
Definition: hpack_parser.cc:456
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
length
std::size_t length
Definition: abseil-cpp/absl/time/internal/test_util.cc:57
GOOGLE_LOG
#define GOOGLE_LOG(LEVEL)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/logging.h:146
google::protobuf::stringpiece_internal::StringPiece::npos
static const size_type npos
Definition: protobuf/src/google/protobuf/stubs/stringpiece.h:304
setup.target
target
Definition: third_party/bloaty/third_party/protobuf/python/setup.py:179
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
google::protobuf::stringpiece_internal::StringPiece::find_last_not_of
size_type find_last_not_of(StringPiece s, size_type pos=npos) const
Definition: protobuf/src/google/protobuf/stubs/stringpiece.cc:212
google::protobuf::stringpiece_internal::StringPiece::ConsumeFromEnd
bool ConsumeFromEnd(StringPiece x)
Definition: protobuf/src/google/protobuf/stubs/stringpiece.cc:70


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