bloaty/third_party/re2/re2/stringpiece.h
Go to the documentation of this file.
1 // Copyright 2001-2010 The RE2 Authors. All Rights Reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4 
5 #ifndef RE2_STRINGPIECE_H_
6 #define RE2_STRINGPIECE_H_
7 
8 // A string-like object that points to a sized piece of memory.
9 //
10 // Functions or methods may use const StringPiece& parameters to accept either
11 // a "const char*" or a "string" value that will be implicitly converted to
12 // a StringPiece. The implicit conversion means that it is often appropriate
13 // to include this .h file in other files rather than forward-declaring
14 // StringPiece as would be appropriate for most other Google classes.
15 //
16 // Systematic usage of StringPiece is encouraged as it will reduce unnecessary
17 // conversions from "const char*" to "string" and back again.
18 //
19 //
20 // Arghh! I wish C++ literals were "string".
21 
22 // Doing this simplifies the logic below.
23 #ifndef __has_include
24 #define __has_include(x) 0
25 #endif
26 
27 #include <stddef.h>
28 #include <string.h>
29 #include <algorithm>
30 #include <iosfwd>
31 #include <iterator>
32 #include <string>
33 #if __has_include(<string_view>) && __cplusplus >= 201703L
34 #include <string_view>
35 #endif
36 
37 namespace re2 {
38 
39 class StringPiece {
40  public:
41  typedef std::char_traits<char> traits_type;
42  typedef char value_type;
43  typedef char* pointer;
44  typedef const char* const_pointer;
45  typedef char& reference;
46  typedef const char& const_reference;
47  typedef const char* const_iterator;
49  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
51  typedef size_t size_type;
52  typedef ptrdiff_t difference_type;
53  static const size_type npos = static_cast<size_type>(-1);
54 
55  // We provide non-explicit singleton constructors so users can pass
56  // in a "const char*" or a "string" wherever a "StringPiece" is
57  // expected.
59  : data_(NULL), size_(0) {}
60 #if __has_include(<string_view>) && __cplusplus >= 201703L
62  : data_(str.data()), size_(str.size()) {}
63 #endif
65  : data_(str.data()), size_(str.size()) {}
66  StringPiece(const char* str)
67  : data_(str), size_(str == NULL ? 0 : strlen(str)) {}
68  StringPiece(const char* str, size_type len)
69  : data_(str), size_(len) {}
70 
71  const_iterator begin() const { return data_; }
72  const_iterator end() const { return data_ + size_; }
75  }
78  }
79 
80  size_type size() const { return size_; }
81  size_type length() const { return size_; }
82  bool empty() const { return size_ == 0; }
83 
84  const_reference operator[](size_type i) const { return data_[i]; }
85  const_pointer data() const { return data_; }
86 
88  data_ += n;
89  size_ -= n;
90  }
91 
93  size_ -= n;
94  }
95 
96  void set(const char* str) {
97  data_ = str;
98  size_ = str == NULL ? 0 : strlen(str);
99  }
100 
101  void set(const char* str, size_type len) {
102  data_ = str;
103  size_ = len;
104  }
105 
106  // Converts to `std::basic_string`.
107  template <typename A>
108  explicit operator std::basic_string<char, traits_type, A>() const {
109  if (!data_) return {};
110  return std::basic_string<char, traits_type, A>(data_, size_);
111  }
112 
114  return std::string(data_, size_);
115  }
116 
117  // We also define ToString() here, since many other string-like
118  // interfaces name the routine that converts to a C++ string
119  // "ToString", and it's confusing to have the method that does that
120  // for a StringPiece be called "as_string()". We also leave the
121  // "as_string()" method defined here for existing code.
123  return std::string(data_, size_);
124  }
125 
127  target->assign(data_, size_);
128  }
129 
131  target->append(data_, size_);
132  }
133 
134  size_type copy(char* buf, size_type n, size_type pos = 0) const;
135  StringPiece substr(size_type pos = 0, size_type n = npos) const;
136 
137  int compare(const StringPiece& x) const {
138  size_type min_size = std::min(size(), x.size());
139  if (min_size > 0) {
140  int r = memcmp(data(), x.data(), min_size);
141  if (r < 0) return -1;
142  if (r > 0) return 1;
143  }
144  if (size() < x.size()) return -1;
145  if (size() > x.size()) return 1;
146  return 0;
147  }
148 
149  // Does "this" start with "x"?
150  bool starts_with(const StringPiece& x) const {
151  return x.empty() ||
152  (size() >= x.size() && memcmp(data(), x.data(), x.size()) == 0);
153  }
154 
155  // Does "this" end with "x"?
156  bool ends_with(const StringPiece& x) const {
157  return x.empty() ||
158  (size() >= x.size() &&
159  memcmp(data() + (size() - x.size()), x.data(), x.size()) == 0);
160  }
161 
162  bool contains(const StringPiece& s) const {
163  return find(s) != npos;
164  }
165 
166  size_type find(const StringPiece& s, size_type pos = 0) const;
167  size_type find(char c, size_type pos = 0) const;
168  size_type rfind(const StringPiece& s, size_type pos = npos) const;
169  size_type rfind(char c, size_type pos = npos) const;
170 
171  private:
174 };
175 
176 inline bool operator==(const StringPiece& x, const StringPiece& y) {
177  StringPiece::size_type len = x.size();
178  if (len != y.size()) return false;
179  return x.data() == y.data() || len == 0 ||
180  memcmp(x.data(), y.data(), len) == 0;
181 }
182 
183 inline bool operator!=(const StringPiece& x, const StringPiece& y) {
184  return !(x == y);
185 }
186 
187 inline bool operator<(const StringPiece& x, const StringPiece& y) {
188  StringPiece::size_type min_size = std::min(x.size(), y.size());
189  int r = min_size == 0 ? 0 : memcmp(x.data(), y.data(), min_size);
190  return (r < 0) || (r == 0 && x.size() < y.size());
191 }
192 
193 inline bool operator>(const StringPiece& x, const StringPiece& y) {
194  return y < x;
195 }
196 
197 inline bool operator<=(const StringPiece& x, const StringPiece& y) {
198  return !(x > y);
199 }
200 
201 inline bool operator>=(const StringPiece& x, const StringPiece& y) {
202  return !(x < y);
203 }
204 
205 // Allow StringPiece to be logged.
206 std::ostream& operator<<(std::ostream& o, const StringPiece& p);
207 
208 } // namespace re2
209 
210 #endif // RE2_STRINGPIECE_H_
re2::StringPiece::size_type
size_t size_type
Definition: bloaty/third_party/re2/re2/stringpiece.h:51
xds_interop_client.str
str
Definition: xds_interop_client.py:487
re2::StringPiece::starts_with
bool starts_with(const StringPiece &x) const
Definition: bloaty/third_party/re2/re2/stringpiece.h:150
re2::StringPiece::size_
size_type size_
Definition: bloaty/third_party/re2/re2/stringpiece.h:173
pos
int pos
Definition: libuv/docs/code/tty-gravity/main.c:11
re2::StringPiece::iterator
const_iterator iterator
Definition: bloaty/third_party/re2/re2/stringpiece.h:48
re2::StringPiece::remove_prefix
void remove_prefix(size_type n)
Definition: bloaty/third_party/re2/re2/stringpiece.h:87
re2::operator<<
std::ostream & operator<<(std::ostream &o, const StringPiece &p)
Definition: bloaty/third_party/re2/re2/stringpiece.cc:60
re2::StringPiece::traits_type
std::char_traits< char > traits_type
Definition: bloaty/third_party/re2/re2/stringpiece.h:41
re2::StringPiece::remove_suffix
void remove_suffix(size_type n)
Definition: bloaty/third_party/re2/re2/stringpiece.h:92
re2::StringPiece::end
const_iterator end() const
Definition: bloaty/third_party/re2/re2/stringpiece.h:72
y
const double y
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3611
string.h
re2::StringPiece::size
size_type size() const
Definition: bloaty/third_party/re2/re2/stringpiece.h:80
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
re2::StringPiece::npos
static const size_type npos
Definition: bloaty/third_party/re2/re2/stringpiece.h:53
re2::StringPiece::data_
const_pointer data_
Definition: bloaty/third_party/re2/re2/stringpiece.h:172
re2::StringPiece::pointer
char * pointer
Definition: bloaty/third_party/re2/re2/stringpiece.h:43
re2
Definition: bloaty/third_party/re2/re2/bitmap256.h:17
re2::StringPiece::length
size_type length() const
Definition: bloaty/third_party/re2/re2/stringpiece.h:81
re2::operator>=
bool operator>=(const StringPiece &x, const StringPiece &y)
Definition: bloaty/third_party/re2/re2/stringpiece.h:201
re2::StringPiece::empty
bool empty() const
Definition: bloaty/third_party/re2/re2/stringpiece.h:82
re2::StringPiece::const_reverse_iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: bloaty/third_party/re2/re2/stringpiece.h:49
re2::StringPiece::StringPiece
StringPiece(const std::string &str)
Definition: bloaty/third_party/re2/re2/stringpiece.h:64
re2::operator!=
bool operator!=(const StringPiece &x, const StringPiece &y)
Definition: bloaty/third_party/re2/re2/stringpiece.h:183
re2::StringPiece::StringPiece
StringPiece(const char *str)
Definition: bloaty/third_party/re2/re2/stringpiece.h:66
re2::StringPiece::ends_with
bool ends_with(const StringPiece &x) const
Definition: bloaty/third_party/re2/re2/stringpiece.h:156
re2::StringPiece::compare
int compare(const StringPiece &x) const
Definition: bloaty/third_party/re2/re2/stringpiece.h:137
re2::StringPiece::StringPiece
StringPiece()
Definition: bloaty/third_party/re2/re2/stringpiece.h:58
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
re2::StringPiece::find
size_type find(const StringPiece &s, size_type pos=0) const
Definition: bloaty/third_party/re2/re2/stringpiece.cc:28
min
#define min(a, b)
Definition: qsort.h:83
re2::StringPiece::ToString
std::string ToString() const
Definition: bloaty/third_party/re2/re2/stringpiece.h:122
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
re2::StringPiece::const_iterator
const typedef char * const_iterator
Definition: bloaty/third_party/re2/re2/stringpiece.h:47
re2::StringPiece::difference_type
ptrdiff_t difference_type
Definition: bloaty/third_party/re2/re2/stringpiece.h:52
re2::StringPiece::data
const_pointer data() const
Definition: bloaty/third_party/re2/re2/stringpiece.h:85
re2::StringPiece::contains
bool contains(const StringPiece &s) const
Definition: bloaty/third_party/re2/re2/stringpiece.h:162
re2::operator==
bool operator==(const StringPiece &x, const StringPiece &y)
Definition: bloaty/third_party/re2/re2/stringpiece.h:176
re2::operator<=
bool operator<=(const StringPiece &x, const StringPiece &y)
Definition: bloaty/third_party/re2/re2/stringpiece.h:197
re2::StringPiece::set
void set(const char *str)
Definition: bloaty/third_party/re2/re2/stringpiece.h:96
re2::StringPiece::reference
char & reference
Definition: bloaty/third_party/re2/re2/stringpiece.h:45
re2::operator>
bool operator>(const StringPiece &x, const StringPiece &y)
Definition: bloaty/third_party/re2/re2/stringpiece.h:193
re2::StringPiece::set
void set(const char *str, size_type len)
Definition: bloaty/third_party/re2/re2/stringpiece.h:101
fix_build_deps.r
r
Definition: fix_build_deps.py:491
string_view
absl::string_view string_view
Definition: attr.cc:22
re2::StringPiece::begin
const_iterator begin() const
Definition: bloaty/third_party/re2/re2/stringpiece.h:71
re2::StringPiece::operator[]
const_reference operator[](size_type i) const
Definition: bloaty/third_party/re2/re2/stringpiece.h:84
re2::StringPiece::rbegin
const_reverse_iterator rbegin() const
Definition: bloaty/third_party/re2/re2/stringpiece.h:73
re2::StringPiece::as_string
std::string as_string() const
Definition: bloaty/third_party/re2/re2/stringpiece.h:113
re2::StringPiece::AppendToString
void AppendToString(std::string *target) const
Definition: bloaty/third_party/re2/re2/stringpiece.h:130
re2::StringPiece::substr
StringPiece substr(size_type pos=0, size_type n=npos) const
Definition: bloaty/third_party/re2/re2/stringpiece.cc:22
re2::StringPiece::rfind
size_type rfind(const StringPiece &s, size_type pos=npos) const
Definition: bloaty/third_party/re2/re2/stringpiece.cc:43
re2::StringPiece::StringPiece
StringPiece(const char *str, size_type len)
Definition: bloaty/third_party/re2/re2/stringpiece.h:68
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
re2::StringPiece::CopyToString
void CopyToString(std::string *target) const
Definition: bloaty/third_party/re2/re2/stringpiece.h:126
re2::StringPiece::reverse_iterator
const_reverse_iterator reverse_iterator
Definition: bloaty/third_party/re2/re2/stringpiece.h:50
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
re2::StringPiece::copy
size_type copy(char *buf, size_type n, size_type pos=0) const
Definition: bloaty/third_party/re2/re2/stringpiece.cc:15
re2::StringPiece
Definition: bloaty/third_party/re2/re2/stringpiece.h:39
re2::StringPiece::value_type
char value_type
Definition: bloaty/third_party/re2/re2/stringpiece.h:42
re2::operator<
bool operator<(const StringPiece &x, const StringPiece &y)
Definition: bloaty/third_party/re2/re2/stringpiece.h:187
setup.target
target
Definition: third_party/bloaty/third_party/protobuf/python/setup.py:179
re2::StringPiece::const_pointer
const typedef char * const_pointer
Definition: bloaty/third_party/re2/re2/stringpiece.h:44
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
re2::StringPiece::const_reference
const typedef char & const_reference
Definition: bloaty/third_party/re2/re2/stringpiece.h:46
re2::StringPiece::rend
const_reverse_iterator rend() const
Definition: bloaty/third_party/re2/re2/stringpiece.h:76


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