GRPC C++  0.11.0.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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_SUPPORT_STRING_REF_H
35 #define GRPCXX_SUPPORT_STRING_REF_H
36 
37 #include <iterator>
38 #include <iosfwd>
39 
40 #include <grpc++/support/config.h>
41 
42 namespace grpc {
43 
52 class string_ref {
53  public:
54  // types
55  typedef const char* const_iterator;
56  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
57 
58  // constants
59  const static size_t npos = size_t(-1);
60 
61  // construct/copy.
62  string_ref() : data_(nullptr), length_(0) {}
63  string_ref(const string_ref& other)
64  : data_(other.data_), length_(other.length_) {}
65  string_ref& operator=(const string_ref& rhs);
66  string_ref(const char* s);
67  string_ref(const char* s, size_t l) : data_(s), length_(l) {}
68  string_ref(const grpc::string& s) : data_(s.data()), length_(s.length()) {}
69 
70  // iterators
71  const_iterator begin() const { return data_; }
72  const_iterator end() const { return data_ + length_; }
73  const_iterator cbegin() const { return data_; }
74  const_iterator cend() const { return data_ + length_; }
76  return const_reverse_iterator(end());
77  }
79  return const_reverse_iterator(begin());
80  }
82  return const_reverse_iterator(end());
83  }
85  return const_reverse_iterator(begin());
86  }
87 
88  // capacity
89  size_t size() const { return length_; }
90  size_t length() const { return length_; }
91  size_t max_size() const { return length_; }
92  bool empty() const { return length_ == 0; }
93 
94  // element access
95  const char* data() const { return data_; }
96 
97  // string operations
98  int compare(string_ref x) const;
99  bool starts_with(string_ref x) const;
100  bool ends_with(string_ref x) const;
101  size_t find(string_ref s) const;
102  size_t find(char c) const;
103 
104  string_ref substr(size_t pos, size_t n = npos) const;
105 
106  private:
107  const char* data_;
108  size_t length_;
109 };
110 
111 // Comparison operators
112 bool operator==(string_ref x, string_ref y);
113 bool operator!=(string_ref x, string_ref y);
114 bool operator<(string_ref x, string_ref y);
115 bool operator>(string_ref x, string_ref y);
116 bool operator<=(string_ref x, string_ref y);
117 bool operator>=(string_ref x, string_ref y);
118 
119 std::ostream& operator<<(std::ostream& stream, const string_ref& string);
120 
121 } // namespace grpc
122 
123 #endif // GRPCXX_SUPPORT_STRING_REF_H
size_t max_size() const
Definition: string_ref.h:91
const_iterator cend() const
Definition: string_ref.h:74
const_reverse_iterator crend() const
Definition: string_ref.h:84
const_iterator cbegin() const
Definition: string_ref.h:73
std::string string
Definition: config.h:112
size_t size() const
Definition: string_ref.h:89
std::ostream & operator<<(std::ostream &stream, const string_ref &string)
string_ref(const string_ref &other)
Definition: string_ref.h:63
size_t find(string_ref s) const
const_reverse_iterator crbegin() const
Definition: string_ref.h:81
const char * const_iterator
Definition: string_ref.h:55
const_reverse_iterator rbegin() const
Definition: string_ref.h:75
string_ref(const char *s, size_t l)
Definition: string_ref.h:67
string_ref(const grpc::string &s)
Definition: string_ref.h:68
const char * data() const
Definition: string_ref.h:95
const_iterator end() const
Definition: string_ref.h:72
string_ref()
Definition: string_ref.h:62
bool operator<(string_ref x, string_ref y)
string_ref & operator=(const string_ref &rhs)
static const size_t npos
Definition: string_ref.h:59
bool operator<=(string_ref x, string_ref y)
bool operator>(string_ref x, string_ref y)
const_reverse_iterator rend() const
Definition: string_ref.h:78
This class is a non owning reference to a string.
Definition: string_ref.h:52
bool starts_with(string_ref x) const
bool empty() const
Definition: string_ref.h:92
bool operator==(string_ref x, string_ref y)
bool ends_with(string_ref x) const
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: string_ref.h:56
const_iterator begin() const
Definition: string_ref.h:71
bool operator!=(string_ref x, string_ref y)
string_ref substr(size_t pos, size_t n=npos) const
bool operator>=(string_ref x, string_ref y)
int compare(string_ref x) const
size_t length() const
Definition: string_ref.h:90