Line data Source code
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 :
44 : /// This class is a non owning reference to a string.
45 : ///
46 : /// It should be a strict subset of the upcoming std::string_ref.
47 : ///
48 : /// \see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3442.html
49 : ///
50 : /// The constexpr is dropped or replaced with const for legacy compiler
51 : /// compatibility.
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 3 : string_ref() : data_(nullptr), length_(0) {}
63 3894926 : string_ref(const string_ref& other)
64 3894926 : : data_(other.data_), length_(other.length_) {}
65 : string_ref& operator=(const string_ref& rhs);
66 : string_ref(const char* s);
67 1299160 : string_ref(const char* s, size_t l) : data_(s), length_(l) {}
68 29 : string_ref(const grpc::string& s) : data_(s.data()), length_(s.length()) {}
69 :
70 : // iterators
71 5 : const_iterator begin() const { return data_; }
72 1 : const_iterator end() const { return data_ + length_; }
73 25 : const_iterator cbegin() const { return data_; }
74 33 : const_iterator cend() const { return data_ + length_; }
75 : const_reverse_iterator rbegin() const {
76 : return const_reverse_iterator(end());
77 : }
78 : const_reverse_iterator rend() const {
79 : return const_reverse_iterator(begin());
80 : }
81 1 : const_reverse_iterator crbegin() const {
82 1 : return const_reverse_iterator(end());
83 : }
84 5 : const_reverse_iterator crend() const {
85 5 : return const_reverse_iterator(begin());
86 : }
87 :
88 : // capacity
89 108 : size_t size() const { return length_; }
90 32 : size_t length() const { return length_; }
91 2 : size_t max_size() const { return length_; }
92 2 : bool empty() const { return length_ == 0; }
93 :
94 : // element access
95 128 : 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
|