GRPC C++  1.70.1
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_SUPPORT_STRING_REF_H
20 #define GRPCPP_SUPPORT_STRING_REF_H
21 
22 #include <grpcpp/support/config.h>
23 #include <string.h>
24 
25 #include <algorithm>
26 #include <iosfwd>
27 #include <iostream>
28 #include <iterator>
29 
30 namespace grpc {
31 
40 class string_ref {
41  public:
43  typedef const char* const_iterator;
44  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
45 
47  const static size_t npos;
48 
50  string_ref() : data_(nullptr), length_(0) {}
51  string_ref(const string_ref& other)
52  : data_(other.data_), length_(other.length_) {}
53  // NOLINTNEXTLINE(bugprone-unhandled-self-assignment)
55  data_ = rhs.data_;
56  length_ = rhs.length_;
57  return *this;
58  }
59 
60  // NOLINTNEXTLINE(google-explicit-constructor)
61  string_ref(const char* s) : data_(s), length_(strlen(s)) {}
62  string_ref(const char* s, size_t l) : data_(s), length_(l) {}
63  // NOLINTNEXTLINE(google-explicit-constructor)
64  string_ref(const std::string& s) : data_(s.data()), length_(s.length()) {}
65 
67  const_iterator begin() const { return data_; }
68  const_iterator end() const { return data_ + length_; }
69  const_iterator cbegin() const { return data_; }
70  const_iterator cend() const { return data_ + length_; }
72  return const_reverse_iterator(end());
73  }
75  return const_reverse_iterator(begin());
76  }
78  return const_reverse_iterator(end());
79  }
81  return const_reverse_iterator(begin());
82  }
83 
85  size_t size() const { return length_; }
86  size_t length() const { return length_; }
87  size_t max_size() const { return length_; }
88  bool empty() const { return length_ == 0; }
89 
91  const char* data() const { return data_; }
92 
94  int compare(string_ref x) const {
95  size_t min_size = length_ < x.length_ ? length_ : x.length_;
96  int r = memcmp(data_, x.data_, min_size);
97  if (r < 0) return -1;
98  if (r > 0) return 1;
99  if (length_ < x.length_) return -1;
100  if (length_ > x.length_) return 1;
101  return 0;
102  }
103 
104  bool starts_with(string_ref x) const {
105  return length_ >= x.length_ && (memcmp(data_, x.data_, x.length_) == 0);
106  }
107 
108  bool ends_with(string_ref x) const {
109  return length_ >= x.length_ &&
110  (memcmp(data_ + (length_ - x.length_), x.data_, x.length_) == 0);
111  }
112 
113  size_t find(string_ref s) const {
114  auto it = std::search(cbegin(), cend(), s.cbegin(), s.cend());
115  return it == cend() ? npos : std::distance(cbegin(), it);
116  }
117 
118  size_t find(char c) const {
119  auto it = std::find(cbegin(), cend(), c);
120  return it == cend() ? npos : std::distance(cbegin(), it);
121  }
122 
123  string_ref substr(size_t pos, size_t n = npos) const {
124  if (pos > length_) pos = length_;
125  if (n > (length_ - pos)) n = length_ - pos;
126  return string_ref(data_ + pos, n);
127  }
128 
129  private:
130  const char* data_;
131  size_t length_;
132 };
133 
135 inline bool operator==(string_ref x, string_ref y) { return x.compare(y) == 0; }
136 inline bool operator!=(string_ref x, string_ref y) { return x.compare(y) != 0; }
137 inline bool operator<(string_ref x, string_ref y) { return x.compare(y) < 0; }
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 
142 inline std::ostream& operator<<(std::ostream& out, const string_ref& string) {
143  return out << std::string(string.begin(), string.end());
144 }
145 
146 } // namespace grpc
147 
148 #endif // GRPCPP_SUPPORT_STRING_REF_H
grpc::string_ref::ends_with
bool ends_with(string_ref x) const
Definition: string_ref.h:108
grpc::string_ref
This class is a non owning reference to a string.
Definition: string_ref.h:40
grpc::string_ref::string_ref
string_ref(const char *s, size_t l)
Definition: string_ref.h:62
grpc::operator==
bool operator==(string_ref x, string_ref y)
Comparison operators.
Definition: string_ref.h:135
grpc::string_ref::const_iterator
const typedef char * const_iterator
types
Definition: string_ref.h:43
grpc::string_ref::empty
bool empty() const
Definition: string_ref.h:88
grpc
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
grpc::string_ref::starts_with
bool starts_with(string_ref x) const
Definition: string_ref.h:104
grpc::string_ref::operator=
string_ref & operator=(const string_ref &rhs)
Definition: string_ref.h:54
grpc::string_ref::compare
int compare(string_ref x) const
string operations
Definition: string_ref.h:94
grpc::string_ref::find
size_t find(string_ref s) const
Definition: string_ref.h:113
grpc::string_ref::cend
const_iterator cend() const
Definition: string_ref.h:70
grpc::operator>=
bool operator>=(string_ref x, string_ref y)
Definition: string_ref.h:140
grpc::operator<
bool operator<(string_ref x, string_ref y)
Definition: string_ref.h:137
grpc::string_ref::string_ref
string_ref(const string_ref &other)
Definition: string_ref.h:51
grpc::operator<=
bool operator<=(string_ref x, string_ref y)
Definition: string_ref.h:138
grpc::string_ref::string_ref
string_ref(const char *s)
Definition: string_ref.h:61
grpc::string_ref::rbegin
const_reverse_iterator rbegin() const
Definition: string_ref.h:71
grpc::string_ref::substr
string_ref substr(size_t pos, size_t n=npos) const
Definition: string_ref.h:123
grpc::operator>
bool operator>(string_ref x, string_ref y)
Definition: string_ref.h:139
grpc::string_ref::end
const_iterator end() const
Definition: string_ref.h:68
grpc::string_ref::length
size_t length() const
Definition: string_ref.h:86
grpc::string_ref::begin
const_iterator begin() const
iterators
Definition: string_ref.h:67
grpc::string_ref::string_ref
string_ref()
construct/copy.
Definition: string_ref.h:50
grpc::operator!=
bool operator!=(string_ref x, string_ref y)
Definition: string_ref.h:136
grpc::string_ref::data
const char * data() const
element access
Definition: string_ref.h:91
grpc::string_ref::const_reverse_iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: string_ref.h:44
grpc::string_ref::cbegin
const_iterator cbegin() const
Definition: string_ref.h:69
config.h
grpc::string_ref::string_ref
string_ref(const std::string &s)
Definition: string_ref.h:64
grpc::string_ref::crbegin
const_reverse_iterator crbegin() const
Definition: string_ref.h:77
grpc::string_ref::rend
const_reverse_iterator rend() const
Definition: string_ref.h:74
grpc::string_ref::size
size_t size() const
capacity
Definition: string_ref.h:85
grpc::string_ref::find
size_t find(char c) const
Definition: string_ref.h:118
grpc::string_ref::crend
const_reverse_iterator crend() const
Definition: string_ref.h:80
grpc::operator<<
std::ostream & operator<<(std::ostream &out, const string_ref &string)
Definition: string_ref.h:142
grpc::string_ref::npos
const static size_t npos
constants
Definition: string_ref.h:47
grpc::string_ref::max_size
size_t max_size() const
Definition: string_ref.h:87