GRPC C++  1.0.0
string_ref.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015, Google Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #ifndef GRPCXX_IMPL_CODEGEN_STRING_REF_H
35 #define GRPCXX_IMPL_CODEGEN_STRING_REF_H
36 
37 #include <string.h>
38 
39 #include <algorithm>
40 #include <iosfwd>
41 #include <iostream>
42 #include <iterator>
43 
45 
46 namespace grpc {
47 
56 class string_ref {
57  public:
58  // types
59  typedef const char* const_iterator;
60  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
61 
62  // constants
63  const static size_t npos;
64 
65  // construct/copy.
66  string_ref() : data_(nullptr), length_(0) {}
67  string_ref(const string_ref& other)
68  : data_(other.data_), length_(other.length_) {}
70  data_ = rhs.data_;
71  length_ = rhs.length_;
72  return *this;
73  }
74 
75  string_ref(const char* s) : data_(s), length_(strlen(s)) {}
76  string_ref(const char* s, size_t l) : data_(s), length_(l) {}
77  string_ref(const grpc::string& s) : data_(s.data()), length_(s.length()) {}
78 
79  // iterators
80  const_iterator begin() const { return data_; }
81  const_iterator end() const { return data_ + length_; }
82  const_iterator cbegin() const { return data_; }
83  const_iterator cend() const { return data_ + length_; }
84  const_reverse_iterator rbegin() const {
85  return const_reverse_iterator(end());
86  }
87  const_reverse_iterator rend() const {
88  return const_reverse_iterator(begin());
89  }
90  const_reverse_iterator crbegin() const {
91  return const_reverse_iterator(end());
92  }
93  const_reverse_iterator crend() const {
94  return const_reverse_iterator(begin());
95  }
96 
97  // capacity
98  size_t size() const { return length_; }
99  size_t length() const { return length_; }
100  size_t max_size() const { return length_; }
101  bool empty() const { return length_ == 0; }
102 
103  // element access
104  const char* data() const { return data_; }
105 
106  // string operations
107  int compare(string_ref x) const {
108  size_t min_size = length_ < x.length_ ? length_ : x.length_;
109  int r = memcmp(data_, x.data_, min_size);
110  if (r < 0) return -1;
111  if (r > 0) return 1;
112  if (length_ < x.length_) return -1;
113  if (length_ > x.length_) return 1;
114  return 0;
115  }
116 
117  bool starts_with(string_ref x) const {
118  return length_ >= x.length_ && (memcmp(data_, x.data_, x.length_) == 0);
119  }
120 
121  bool ends_with(string_ref x) const {
122  return length_ >= x.length_ &&
123  (memcmp(data_ + (length_ - x.length_), x.data_, x.length_) == 0);
124  }
125 
126  size_t find(string_ref s) const {
127  auto it = std::search(cbegin(), cend(), s.cbegin(), s.cend());
128  return it == cend() ? npos : std::distance(cbegin(), it);
129  }
130 
131  size_t find(char c) const {
132  auto it = std::find(cbegin(), cend(), c);
133  return it == cend() ? npos : std::distance(cbegin(), it);
134  }
135 
136  string_ref substr(size_t pos, size_t n = npos) const {
137  if (pos > length_) pos = length_;
138  if (n > (length_ - pos)) n = length_ - pos;
139  return string_ref(data_ + pos, n);
140  }
141 
142  private:
143  const char* data_;
144  size_t length_;
145 };
146 
147 // Comparison operators
148 inline bool operator==(string_ref x, string_ref y) { return x.compare(y) == 0; }
149 inline bool operator!=(string_ref x, string_ref y) { return x.compare(y) != 0; }
150 inline bool operator<(string_ref x, string_ref y) { return x.compare(y) < 0; }
151 inline bool operator<=(string_ref x, string_ref y) { return x.compare(y) <= 0; }
152 inline bool operator>(string_ref x, string_ref y) { return x.compare(y) > 0; }
153 inline bool operator>=(string_ref x, string_ref y) { return x.compare(y) >= 0; }
154 
155 inline std::ostream& operator<<(std::ostream& out, const string_ref& string) {
156  return out << grpc::string(string.begin(), string.end());
157 }
158 
159 } // namespace grpc
160 
161 #endif // GRPCXX_IMPL_CODEGEN_STRING_REF_H
size_t max_size() const
Definition: string_ref.h:100
const_iterator cend() const
Definition: string_ref.h:83
const_reverse_iterator crend() const
Definition: string_ref.h:93
const_iterator cbegin() const
Definition: string_ref.h:82
std::string string
Definition: config.h:118
size_t find(char c) const
Definition: string_ref.h:131
size_t size() const
Definition: string_ref.h:98
string_ref(const string_ref &other)
Definition: string_ref.h:67
size_t find(string_ref s) const
Definition: string_ref.h:126
const_reverse_iterator crbegin() const
Definition: string_ref.h:90
const char * const_iterator
Definition: string_ref.h:59
string_ref(const char *s)
Definition: string_ref.h:75
const_reverse_iterator rbegin() const
Definition: string_ref.h:84
string_ref(const char *s, size_t l)
Definition: string_ref.h:76
string_ref(const grpc::string &s)
Definition: string_ref.h:77
const char * data() const
Definition: string_ref.h:104
const_iterator end() const
Definition: string_ref.h:81
std::ostream & operator<<(std::ostream &out, const string_ref &string)
Definition: string_ref.h:155
string_ref()
Definition: string_ref.h:66
Definition: alarm.h:48
bool operator<(string_ref x, string_ref y)
Definition: string_ref.h:150
string_ref & operator=(const string_ref &rhs)
Definition: string_ref.h:69
static const size_t npos
Definition: string_ref.h:63
bool operator<=(string_ref x, string_ref y)
Definition: string_ref.h:151
bool operator>(string_ref x, string_ref y)
Definition: string_ref.h:152
const_reverse_iterator rend() const
Definition: string_ref.h:87
This class is a non owning reference to a string.
Definition: string_ref.h:56
bool starts_with(string_ref x) const
Definition: string_ref.h:117
bool empty() const
Definition: string_ref.h:101
bool operator==(string_ref x, string_ref y)
Definition: string_ref.h:148
bool ends_with(string_ref x) const
Definition: string_ref.h:121
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: string_ref.h:60
const_iterator begin() const
Definition: string_ref.h:80
bool operator!=(string_ref x, string_ref y)
Definition: string_ref.h:149
string_ref substr(size_t pos, size_t n=npos) const
Definition: string_ref.h:136
bool operator>=(string_ref x, string_ref y)
Definition: string_ref.h:153
int compare(string_ref x) const
Definition: string_ref.h:107
size_t length() const
Definition: string_ref.h:99