| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // Copied from strings/stringpiece.h with modifications | 4 // Copied from strings/stringpiece.h with modifications |
| 5 // | 5 // |
| 6 // A string-like object that points to a sized piece of memory. | 6 // A string-like object that points to a sized piece of memory. |
| 7 // | 7 // |
| 8 // Functions or methods may use const StringPiece& parameters to accept either | 8 // Functions or methods may use const StringPiece& parameters to accept either |
| 9 // a "const char*" or a "string" value that will be implicitly converted to | 9 // a "const char*" or a "string" value that will be implicitly converted to |
| 10 // a StringPiece. The implicit conversion means that it is often appropriate | 10 // a StringPiece. The implicit conversion means that it is often appropriate |
| 11 // to include this .h file in other files rather than forward-declaring | 11 // to include this .h file in other files rather than forward-declaring |
| (...skipping 10 matching lines...) Expand all Loading... |
| 22 #include <algorithm> | 22 #include <algorithm> |
| 23 #include <iosfwd> | 23 #include <iosfwd> |
| 24 #include <string> | 24 #include <string> |
| 25 | 25 |
| 26 #include "base/basictypes.h" | 26 #include "base/basictypes.h" |
| 27 | 27 |
| 28 namespace base { | 28 namespace base { |
| 29 | 29 |
| 30 class StringPiece { | 30 class StringPiece { |
| 31 public: | 31 public: |
| 32 // standard STL container boilerplate |
| 32 typedef size_t size_type; | 33 typedef size_t size_type; |
| 34 typedef char value_type; |
| 35 typedef const char* pointer; |
| 36 typedef const char& reference; |
| 37 typedef const char& const_reference; |
| 38 typedef ptrdiff_t difference_type; |
| 39 typedef const char* const_iterator; |
| 40 typedef const char* iterator; |
| 41 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
| 42 typedef std::reverse_iterator<iterator> reverse_iterator; |
| 33 | 43 |
| 34 private: | 44 static const size_type npos; |
| 35 const char* ptr_; | |
| 36 size_type length_; | |
| 37 | 45 |
| 38 public: | 46 public: |
| 39 // We provide non-explicit singleton constructors so users can pass | 47 // We provide non-explicit singleton constructors so users can pass |
| 40 // in a "const char*" or a "string" wherever a "StringPiece" is | 48 // in a "const char*" or a "string" wherever a "StringPiece" is |
| 41 // expected. | 49 // expected. |
| 42 StringPiece() : ptr_(NULL), length_(0) { } | 50 StringPiece() : ptr_(NULL), length_(0) { } |
| 43 StringPiece(const char* str) | 51 StringPiece(const char* str) |
| 44 : ptr_(str), length_((str == NULL) ? 0 : strlen(str)) { } | 52 : ptr_(str), length_((str == NULL) ? 0 : strlen(str)) { } |
| 45 StringPiece(const std::string& str) | 53 StringPiece(const std::string& str) |
| 46 : ptr_(str.data()), length_(str.size()) { } | 54 : ptr_(str.data()), length_(str.size()) { } |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 return ((length_ >= x.length_) && | 114 return ((length_ >= x.length_) && |
| 107 (wordmemcmp(ptr_, x.ptr_, x.length_) == 0)); | 115 (wordmemcmp(ptr_, x.ptr_, x.length_) == 0)); |
| 108 } | 116 } |
| 109 | 117 |
| 110 // Does "this" end with "x" | 118 // Does "this" end with "x" |
| 111 bool ends_with(const StringPiece& x) const { | 119 bool ends_with(const StringPiece& x) const { |
| 112 return ((length_ >= x.length_) && | 120 return ((length_ >= x.length_) && |
| 113 (wordmemcmp(ptr_ + (length_-x.length_), x.ptr_, x.length_) == 0)); | 121 (wordmemcmp(ptr_ + (length_-x.length_), x.ptr_, x.length_) == 0)); |
| 114 } | 122 } |
| 115 | 123 |
| 116 // standard STL container boilerplate | |
| 117 typedef char value_type; | |
| 118 typedef const char* pointer; | |
| 119 typedef const char& reference; | |
| 120 typedef const char& const_reference; | |
| 121 typedef ptrdiff_t difference_type; | |
| 122 static const size_type npos; | |
| 123 typedef const char* const_iterator; | |
| 124 typedef const char* iterator; | |
| 125 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; | |
| 126 typedef std::reverse_iterator<iterator> reverse_iterator; | |
| 127 iterator begin() const { return ptr_; } | 124 iterator begin() const { return ptr_; } |
| 128 iterator end() const { return ptr_ + length_; } | 125 iterator end() const { return ptr_ + length_; } |
| 129 const_reverse_iterator rbegin() const { | 126 const_reverse_iterator rbegin() const { |
| 130 return const_reverse_iterator(ptr_ + length_); | 127 return const_reverse_iterator(ptr_ + length_); |
| 131 } | 128 } |
| 132 const_reverse_iterator rend() const { | 129 const_reverse_iterator rend() const { |
| 133 return const_reverse_iterator(ptr_); | 130 return const_reverse_iterator(ptr_); |
| 134 } | 131 } |
| 135 | 132 |
| 136 size_type max_size() const { return length_; } | 133 size_type max_size() const { return length_; } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 154 return rfind(c, pos); | 151 return rfind(c, pos); |
| 155 } | 152 } |
| 156 size_type find_last_not_of(const StringPiece& s, size_type pos = npos) const; | 153 size_type find_last_not_of(const StringPiece& s, size_type pos = npos) const; |
| 157 size_type find_last_not_of(char c, size_type pos = npos) const; | 154 size_type find_last_not_of(char c, size_type pos = npos) const; |
| 158 | 155 |
| 159 StringPiece substr(size_type pos, size_type n = npos) const; | 156 StringPiece substr(size_type pos, size_type n = npos) const; |
| 160 | 157 |
| 161 static int wordmemcmp(const char* p, const char* p2, size_type N) { | 158 static int wordmemcmp(const char* p, const char* p2, size_type N) { |
| 162 return memcmp(p, p2, N); | 159 return memcmp(p, p2, N); |
| 163 } | 160 } |
| 161 |
| 162 private: |
| 163 const char* ptr_; |
| 164 size_type length_; |
| 164 }; | 165 }; |
| 165 | 166 |
| 166 bool operator==(const StringPiece& x, const StringPiece& y); | 167 bool operator==(const StringPiece& x, const StringPiece& y); |
| 167 | 168 |
| 168 inline bool operator!=(const StringPiece& x, const StringPiece& y) { | 169 inline bool operator!=(const StringPiece& x, const StringPiece& y) { |
| 169 return !(x == y); | 170 return !(x == y); |
| 170 } | 171 } |
| 171 | 172 |
| 172 inline bool operator<(const StringPiece& x, const StringPiece& y) { | 173 inline bool operator<(const StringPiece& x, const StringPiece& y) { |
| 173 const int r = StringPiece::wordmemcmp(x.data(), y.data(), | 174 const int r = StringPiece::wordmemcmp(x.data(), y.data(), |
| (...skipping 12 matching lines...) Expand all Loading... |
| 186 inline bool operator>=(const StringPiece& x, const StringPiece& y) { | 187 inline bool operator>=(const StringPiece& x, const StringPiece& y) { |
| 187 return !(x < y); | 188 return !(x < y); |
| 188 } | 189 } |
| 189 | 190 |
| 190 // allow StringPiece to be logged (needed for unit testing). | 191 // allow StringPiece to be logged (needed for unit testing). |
| 191 extern std::ostream& operator<<(std::ostream& o, const StringPiece& piece); | 192 extern std::ostream& operator<<(std::ostream& o, const StringPiece& piece); |
| 192 | 193 |
| 193 } // namespace base | 194 } // namespace base |
| 194 | 195 |
| 195 #endif // BASE_STRING_PIECE_H_ | 196 #endif // BASE_STRING_PIECE_H_ |
| OLD | NEW |