OLD | NEW |
1 // Copyright 2001-2010 The RE2 Authors. All Rights Reserved. | 1 // Copyright 2001-2010 The RE2 Authors. All Rights Reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // A string-like object that points to a sized piece of memory. | 5 // A string-like object that points to a sized piece of memory. |
6 // | 6 // |
7 // Functions or methods may use const StringPiece& parameters to accept either | 7 // Functions or methods may use const StringPiece& parameters to accept either |
8 // a "const char*" or a "string" value that will be implicitly converted to | 8 // a "const char*" or a "string" value that will be implicitly converted to |
9 // a StringPiece. The implicit conversion means that it is often appropriate | 9 // a StringPiece. The implicit conversion means that it is often appropriate |
10 // to include this .h file in other files rather than forward-declaring | 10 // to include this .h file in other files rather than forward-declaring |
11 // StringPiece as would be appropriate for most other Google classes. | 11 // StringPiece as would be appropriate for most other Google classes. |
12 // | 12 // |
13 // Systematic usage of StringPiece is encouraged as it will reduce unnecessary | 13 // Systematic usage of StringPiece is encouraged as it will reduce unnecessary |
14 // conversions from "const char*" to "string" and back again. | 14 // conversions from "const char*" to "string" and back again. |
15 // | 15 // |
16 // | 16 // |
17 // Arghh! I wish C++ literals were "string". | 17 // Arghh! I wish C++ literals were "string". |
18 | 18 |
19 #ifndef STRINGS_STRINGPIECE_H__ | 19 #ifndef STRINGS_STRINGPIECE_H__ |
20 #define STRINGS_STRINGPIECE_H__ | 20 #define STRINGS_STRINGPIECE_H__ |
21 | 21 |
22 #include <string.h> | 22 #include <string.h> |
| 23 #include <algorithm> |
23 #include <cstddef> | 24 #include <cstddef> |
24 #include <iosfwd> | 25 #include <iosfwd> |
25 #include <string> | 26 #include <string> |
26 #ifdef WIN32 | |
27 #include <algorithm> | |
28 #endif | |
29 | 27 |
30 namespace re2 { | 28 namespace re2 { |
31 | 29 |
32 class StringPiece { | 30 class StringPiece { |
33 private: | 31 private: |
34 const char* ptr_; | 32 const char* ptr_; |
35 int length_; | 33 int length_; |
36 | 34 |
37 public: | 35 public: |
38 // We provide non-explicit singleton constructors so users can pass | 36 // We provide non-explicit singleton constructors so users can pass |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 const_reverse_iterator rbegin() const { | 130 const_reverse_iterator rbegin() const { |
133 return const_reverse_iterator(ptr_ + length_); | 131 return const_reverse_iterator(ptr_ + length_); |
134 } | 132 } |
135 const_reverse_iterator rend() const { | 133 const_reverse_iterator rend() const { |
136 return const_reverse_iterator(ptr_); | 134 return const_reverse_iterator(ptr_); |
137 } | 135 } |
138 // STLS says return size_type, but Google says return int | 136 // STLS says return size_type, but Google says return int |
139 int max_size() const { return length_; } | 137 int max_size() const { return length_; } |
140 int capacity() const { return length_; } | 138 int capacity() const { return length_; } |
141 | 139 |
142 int copy(char* buf, size_type n, size_type pos = 0) const; | 140 size_type copy(char* buf, size_type n, size_type pos = 0) const; |
143 | 141 |
144 int find(const StringPiece& s, size_type pos = 0) const; | 142 bool contains(StringPiece s) const; |
145 int find(char c, size_type pos = 0) const; | 143 |
146 int rfind(const StringPiece& s, size_type pos = npos) const; | 144 size_type find(const StringPiece& s, size_type pos = 0) const; |
147 int rfind(char c, size_type pos = npos) const; | 145 size_type find(char c, size_type pos = 0) const; |
| 146 size_type rfind(const StringPiece& s, size_type pos = npos) const; |
| 147 size_type rfind(char c, size_type pos = npos) const; |
148 | 148 |
149 StringPiece substr(size_type pos, size_type n = npos) const; | 149 StringPiece substr(size_type pos, size_type n = npos) const; |
150 | 150 |
151 static bool _equal(const StringPiece&, const StringPiece&); | 151 static bool _equal(const StringPiece&, const StringPiece&); |
152 }; | 152 }; |
153 | 153 |
154 inline bool operator==(const StringPiece& x, const StringPiece& y) { | 154 inline bool operator==(const StringPiece& x, const StringPiece& y) { |
155 return StringPiece::_equal(x, y); | 155 return StringPiece::_equal(x, y); |
156 } | 156 } |
157 | 157 |
158 inline bool operator!=(const StringPiece& x, const StringPiece& y) { | 158 inline bool operator!=(const StringPiece& x, const StringPiece& y) { |
159 return !(x == y); | 159 return !(x == y); |
160 } | 160 } |
(...skipping 15 matching lines...) Expand all Loading... |
176 inline bool operator>=(const StringPiece& x, const StringPiece& y) { | 176 inline bool operator>=(const StringPiece& x, const StringPiece& y) { |
177 return !(x < y); | 177 return !(x < y); |
178 } | 178 } |
179 | 179 |
180 } // namespace re2 | 180 } // namespace re2 |
181 | 181 |
182 // allow StringPiece to be logged | 182 // allow StringPiece to be logged |
183 extern std::ostream& operator<<(std::ostream& o, const re2::StringPiece& piece); | 183 extern std::ostream& operator<<(std::ostream& o, const re2::StringPiece& piece); |
184 | 184 |
185 #endif // STRINGS_STRINGPIECE_H__ | 185 #endif // STRINGS_STRINGPIECE_H__ |
OLD | NEW |