grpcpp/impl/codegen/string_ref.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #ifndef GRPCPP_IMPL_CODEGEN_STRING_REF_H
20 #define GRPCPP_IMPL_CODEGEN_STRING_REF_H
21 
22 // IWYU pragma: private, include <grpcpp/support/string_ref.h>
23 
24 #include <string.h>
25 
26 #include <algorithm>
27 #include <iosfwd>
28 #include <iostream>
29 #include <iterator>
30 
32 
33 namespace grpc {
34 
43 class string_ref {
44  public:
46  typedef const char* const_iterator;
47  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
48 
50  const static size_t npos;
51 
53  string_ref() : data_(nullptr), length_(0) {}
54  string_ref(const string_ref& other)
55  : data_(other.data_), length_(other.length_) {}
56  // NOLINTNEXTLINE(bugprone-unhandled-self-assignment)
58  data_ = rhs.data_;
59  length_ = rhs.length_;
60  return *this;
61  }
62 
63  /* NOLINTNEXTLINE(google-explicit-constructor) */
64  string_ref(const char* s) : data_(s), length_(strlen(s)) {}
65  string_ref(const char* s, size_t l) : data_(s), length_(l) {}
66  /* NOLINTNEXTLINE(google-explicit-constructor) */
67  string_ref(const std::string& s) : data_(s.data()), length_(s.length()) {}
68 
70  const_iterator begin() const { return data_; }
71  const_iterator end() const { return data_ + length_; }
72  const_iterator cbegin() const { return data_; }
73  const_iterator cend() const { return data_ + length_; }
75  return const_reverse_iterator(end());
76  }
78  return const_reverse_iterator(begin());
79  }
81  return const_reverse_iterator(end());
82  }
84  return const_reverse_iterator(begin());
85  }
86 
88  size_t size() const { return length_; }
89  size_t length() const { return length_; }
90  size_t max_size() const { return length_; }
91  bool empty() const { return length_ == 0; }
92 
94  const char* data() const { return data_; }
95 
97  int compare(string_ref x) const {
98  size_t min_size = length_ < x.length_ ? length_ : x.length_;
99  int r = memcmp(data_, x.data_, min_size);
100  if (r < 0) return -1;
101  if (r > 0) return 1;
102  if (length_ < x.length_) return -1;
103  if (length_ > x.length_) return 1;
104  return 0;
105  }
106 
107  bool starts_with(string_ref x) const {
108  return length_ >= x.length_ && (memcmp(data_, x.data_, x.length_) == 0);
109  }
110 
111  bool ends_with(string_ref x) const {
112  return length_ >= x.length_ &&
113  (memcmp(data_ + (length_ - x.length_), x.data_, x.length_) == 0);
114  }
115 
116  size_t find(string_ref s) const {
117  auto it = std::search(cbegin(), cend(), s.cbegin(), s.cend());
118  return it == cend() ? npos : std::distance(cbegin(), it);
119  }
120 
121  size_t find(char c) const {
122  auto it = std::find(cbegin(), cend(), c);
123  return it == cend() ? npos : std::distance(cbegin(), it);
124  }
125 
126  string_ref substr(size_t pos, size_t n = npos) const {
127  if (pos > length_) pos = length_;
128  if (n > (length_ - pos)) n = length_ - pos;
129  return string_ref(data_ + pos, n);
130  }
131 
132  private:
133  const char* data_;
134  size_t length_;
135 };
136 
138 inline bool operator==(string_ref x, string_ref y) { return x.compare(y) == 0; }
139 inline bool operator!=(string_ref x, string_ref y) { return x.compare(y) != 0; }
140 inline bool operator<(string_ref x, string_ref y) { return x.compare(y) < 0; }
141 inline bool operator<=(string_ref x, string_ref y) { return x.compare(y) <= 0; }
142 inline bool operator>(string_ref x, string_ref y) { return x.compare(y) > 0; }
143 inline bool operator>=(string_ref x, string_ref y) { return x.compare(y) >= 0; }
144 
145 inline std::ostream& operator<<(std::ostream& out, const string_ref& string) {
146  return out << std::string(string.begin(), string.end());
147 }
148 
149 } // namespace grpc
150 
151 #endif // GRPCPP_IMPL_CODEGEN_STRING_REF_H
grpc::string_ref::ends_with
bool ends_with(string_ref x) const
Definition: grpcpp/impl/codegen/string_ref.h:111
grpc::string_ref
Definition: grpcpp/impl/codegen/string_ref.h:43
grpc::string_ref::string_ref
string_ref(const char *s, size_t l)
Definition: grpcpp/impl/codegen/string_ref.h:65
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
regen-readme.it
it
Definition: regen-readme.py:15
grpc::operator==
bool operator==(string_ref x, string_ref y)
Comparison operators.
Definition: grpcpp/impl/codegen/string_ref.h:138
pos
int pos
Definition: libuv/docs/code/tty-gravity/main.c:11
grpc::string_ref::const_iterator
const typedef char * const_iterator
types
Definition: grpcpp/impl/codegen/string_ref.h:46
find
static void ** find(grpc_chttp2_stream_map *map, uint32_t key)
Definition: stream_map.cc:99
grpc::string_ref::empty
bool empty() const
Definition: grpcpp/impl/codegen/string_ref.h:91
grpc
Definition: grpcpp/alarm.h:33
grpc::string_ref::starts_with
bool starts_with(string_ref x) const
Definition: grpcpp/impl/codegen/string_ref.h:107
begin
char * begin
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1007
y
const double y
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3611
string.h
grpc::string_ref::operator=
string_ref & operator=(const string_ref &rhs)
Definition: grpcpp/impl/codegen/string_ref.h:57
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc::string_ref::compare
int compare(string_ref x) const
string operations
Definition: grpcpp/impl/codegen/string_ref.h:97
grpc::string_ref::find
size_t find(string_ref s) const
Definition: grpcpp/impl/codegen/string_ref.h:116
grpc::string_ref::cend
const_iterator cend() const
Definition: grpcpp/impl/codegen/string_ref.h:73
config.h
grpc::operator>=
bool operator>=(string_ref x, string_ref y)
Definition: grpcpp/impl/codegen/string_ref.h:143
grpc::operator<
bool operator<(string_ref x, string_ref y)
Definition: grpcpp/impl/codegen/string_ref.h:140
grpc::string_ref::npos
const static size_t npos
constants
Definition: grpcpp/impl/codegen/string_ref.h:50
grpc::string_ref::string_ref
string_ref(const string_ref &other)
Definition: grpcpp/impl/codegen/string_ref.h:54
grpc::operator<=
bool operator<=(string_ref x, string_ref y)
Definition: grpcpp/impl/codegen/string_ref.h:141
end
char * end
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1008
grpc::string_ref::string_ref
string_ref(const char *s)
Definition: grpcpp/impl/codegen/string_ref.h:64
grpc::string_ref::length_
size_t length_
Definition: grpcpp/impl/codegen/string_ref.h:134
grpc::string_ref::rbegin
const_reverse_iterator rbegin() const
Definition: grpcpp/impl/codegen/string_ref.h:74
search.search
def search(target, ideal_distance, stop_event, maximum_hashes, interesting_hamming_distance=None)
Definition: search.py:99
grpc::string_ref::substr
string_ref substr(size_t pos, size_t n=npos) const
Definition: grpcpp/impl/codegen/string_ref.h:126
grpc::operator>
bool operator>(string_ref x, string_ref y)
Definition: grpcpp/impl/codegen/string_ref.h:142
grpc::string_ref::end
const_iterator end() const
Definition: grpcpp/impl/codegen/string_ref.h:71
grpc::string_ref::length
size_t length() const
Definition: grpcpp/impl/codegen/string_ref.h:89
grpc::string_ref::begin
const_iterator begin() const
iterators
Definition: grpcpp/impl/codegen/string_ref.h:70
grpc::string_ref::data_
const char * data_
Definition: grpcpp/impl/codegen/string_ref.h:133
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
grpc::string_ref::string_ref
string_ref()
construct/copy.
Definition: grpcpp/impl/codegen/string_ref.h:53
grpc::operator!=
bool operator!=(string_ref x, string_ref y)
Definition: grpcpp/impl/codegen/string_ref.h:139
grpc::string_ref::data
const char * data() const
element access
Definition: grpcpp/impl/codegen/string_ref.h:94
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
grpc::string_ref::const_reverse_iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: grpcpp/impl/codegen/string_ref.h:47
grpc::string_ref::cbegin
const_iterator cbegin() const
Definition: grpcpp/impl/codegen/string_ref.h:72
grpc::string_ref::string_ref
string_ref(const std::string &s)
Definition: grpcpp/impl/codegen/string_ref.h:67
fix_build_deps.r
r
Definition: fix_build_deps.py:491
grpc::string_ref::crbegin
const_reverse_iterator crbegin() const
Definition: grpcpp/impl/codegen/string_ref.h:80
grpc::string_ref::rend
const_reverse_iterator rend() const
Definition: grpcpp/impl/codegen/string_ref.h:77
grpc::string_ref::size
size_t size() const
capacity
Definition: grpcpp/impl/codegen/string_ref.h:88
grpc::string_ref::find
size_t find(char c) const
Definition: grpcpp/impl/codegen/string_ref.h:121
grpc::string_ref::crend
const_reverse_iterator crend() const
Definition: grpcpp/impl/codegen/string_ref.h:83
grpc::operator<<
std::ostream & operator<<(std::ostream &out, const string_ref &string)
Definition: grpcpp/impl/codegen/string_ref.h:145
grpc::string_ref::max_size
size_t max_size() const
Definition: grpcpp/impl/codegen/string_ref.h:90


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