license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | // Copied from strings/stringpiece.h with modifications |
| 5 | // |
| 6 | // A string-like object that points to a sized piece of memory. |
| 7 | // |
| 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 |
| 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 |
| 12 | // StringPiece as would be appropriate for most other Google classes. |
| 13 | // |
| 14 | // Systematic usage of StringPiece is encouraged as it will reduce unnecessary |
| 15 | // conversions from "const char*" to "string" and back again. |
| 16 | // |
| 17 | |
[email protected] | baa010b | 2008-11-08 23:16:31 | [diff] [blame] | 18 | #ifndef BASE_STRING_PIECE_H_ |
| 19 | #define BASE_STRING_PIECE_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 20 | #pragma once |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 21 | |
| 22 | #include <algorithm> |
| 23 | #include <iosfwd> |
| 24 | #include <string> |
| 25 | |
| 26 | #include "base/basictypes.h" |
| 27 | |
[email protected] | 8a16266e | 2009-09-10 21:08:39 | [diff] [blame] | 28 | namespace base { |
| 29 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 30 | class StringPiece { |
| 31 | public: |
| 32 | typedef size_t size_type; |
| 33 | |
| 34 | private: |
| 35 | const char* ptr_; |
| 36 | size_type length_; |
| 37 | |
| 38 | public: |
| 39 | // We provide non-explicit singleton constructors so users can pass |
| 40 | // in a "const char*" or a "string" wherever a "StringPiece" is |
| 41 | // expected. |
| 42 | StringPiece() : ptr_(NULL), length_(0) { } |
| 43 | StringPiece(const char* str) |
| 44 | : ptr_(str), length_((str == NULL) ? 0 : strlen(str)) { } |
| 45 | StringPiece(const std::string& str) |
| 46 | : ptr_(str.data()), length_(str.size()) { } |
| 47 | StringPiece(const char* offset, size_type len) |
| 48 | : ptr_(offset), length_(len) { } |
| 49 | |
| 50 | // data() may return a pointer to a buffer with embedded NULs, and the |
| 51 | // returned buffer may or may not be null terminated. Therefore it is |
| 52 | // typically a mistake to pass data() to a routine that expects a NUL |
| 53 | // terminated string. |
| 54 | const char* data() const { return ptr_; } |
| 55 | size_type size() const { return length_; } |
| 56 | size_type length() const { return length_; } |
| 57 | bool empty() const { return length_ == 0; } |
| 58 | |
[email protected] | 2fdc86a | 2010-01-26 23:08:02 | [diff] [blame] | 59 | void clear() { |
| 60 | ptr_ = NULL; |
| 61 | length_ = 0; |
| 62 | } |
| 63 | void set(const char* data, size_type len) { |
| 64 | ptr_ = data; |
| 65 | length_ = len; |
| 66 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 67 | void set(const char* str) { |
| 68 | ptr_ = str; |
| 69 | length_ = str ? strlen(str) : 0; |
| 70 | } |
| 71 | void set(const void* data, size_type len) { |
| 72 | ptr_ = reinterpret_cast<const char*>(data); |
| 73 | length_ = len; |
| 74 | } |
| 75 | |
| 76 | char operator[](size_type i) const { return ptr_[i]; } |
| 77 | |
| 78 | void remove_prefix(size_type n) { |
| 79 | ptr_ += n; |
| 80 | length_ -= n; |
| 81 | } |
| 82 | |
| 83 | void remove_suffix(size_type n) { |
| 84 | length_ -= n; |
| 85 | } |
| 86 | |
| 87 | int compare(const StringPiece& x) const { |
| 88 | int r = wordmemcmp(ptr_, x.ptr_, std::min(length_, x.length_)); |
| 89 | if (r == 0) { |
| 90 | if (length_ < x.length_) r = -1; |
| 91 | else if (length_ > x.length_) r = +1; |
| 92 | } |
| 93 | return r; |
| 94 | } |
| 95 | |
| 96 | std::string as_string() const { |
| 97 | // std::string doesn't like to take a NULL pointer even with a 0 size. |
| 98 | return std::string(!empty() ? data() : "", size()); |
| 99 | } |
| 100 | |
| 101 | void CopyToString(std::string* target) const; |
| 102 | void AppendToString(std::string* target) const; |
| 103 | |
| 104 | // Does "this" start with "x" |
| 105 | bool starts_with(const StringPiece& x) const { |
| 106 | return ((length_ >= x.length_) && |
| 107 | (wordmemcmp(ptr_, x.ptr_, x.length_) == 0)); |
| 108 | } |
| 109 | |
| 110 | // Does "this" end with "x" |
| 111 | bool ends_with(const StringPiece& x) const { |
| 112 | return ((length_ >= x.length_) && |
| 113 | (wordmemcmp(ptr_ + (length_-x.length_), x.ptr_, x.length_) == 0)); |
| 114 | } |
| 115 | |
| 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_; } |
| 128 | iterator end() const { return ptr_ + length_; } |
| 129 | const_reverse_iterator rbegin() const { |
| 130 | return const_reverse_iterator(ptr_ + length_); |
| 131 | } |
| 132 | const_reverse_iterator rend() const { |
| 133 | return const_reverse_iterator(ptr_); |
| 134 | } |
| 135 | |
| 136 | size_type max_size() const { return length_; } |
| 137 | size_type capacity() const { return length_; } |
| 138 | |
| 139 | size_type copy(char* buf, size_type n, size_type pos = 0) const; |
| 140 | |
| 141 | size_type find(const StringPiece& s, size_type pos = 0) const; |
| 142 | size_type find(char c, size_type pos = 0) const; |
| 143 | size_type rfind(const StringPiece& s, size_type pos = npos) const; |
| 144 | size_type rfind(char c, size_type pos = npos) const; |
| 145 | |
| 146 | size_type find_first_of(const StringPiece& s, size_type pos = 0) const; |
| 147 | size_type find_first_of(char c, size_type pos = 0) const { |
| 148 | return find(c, pos); |
| 149 | } |
| 150 | size_type find_first_not_of(const StringPiece& s, size_type pos = 0) const; |
| 151 | size_type find_first_not_of(char c, size_type pos = 0) const; |
| 152 | size_type find_last_of(const StringPiece& s, size_type pos = npos) const; |
| 153 | size_type find_last_of(char c, size_type pos = npos) const { |
| 154 | return rfind(c, pos); |
| 155 | } |
| 156 | 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; |
| 158 | |
| 159 | StringPiece substr(size_type pos, size_type n = npos) const; |
| 160 | |
| 161 | static int wordmemcmp(const char* p, const char* p2, size_type N) { |
| 162 | return memcmp(p, p2, N); |
| 163 | } |
| 164 | }; |
| 165 | |
| 166 | bool operator==(const StringPiece& x, const StringPiece& y); |
| 167 | |
| 168 | inline bool operator!=(const StringPiece& x, const StringPiece& y) { |
| 169 | return !(x == y); |
| 170 | } |
| 171 | |
| 172 | inline bool operator<(const StringPiece& x, const StringPiece& y) { |
| 173 | const int r = StringPiece::wordmemcmp(x.data(), y.data(), |
| 174 | std::min(x.size(), y.size())); |
| 175 | return ((r < 0) || ((r == 0) && (x.size() < y.size()))); |
| 176 | } |
| 177 | |
| 178 | inline bool operator>(const StringPiece& x, const StringPiece& y) { |
| 179 | return y < x; |
| 180 | } |
| 181 | |
| 182 | inline bool operator<=(const StringPiece& x, const StringPiece& y) { |
| 183 | return !(x > y); |
| 184 | } |
| 185 | |
| 186 | inline bool operator>=(const StringPiece& x, const StringPiece& y) { |
| 187 | return !(x < y); |
| 188 | } |
| 189 | |
| 190 | // allow StringPiece to be logged (needed for unit testing). |
| 191 | extern std::ostream& operator<<(std::ostream& o, const StringPiece& piece); |
| 192 | |
[email protected] | 8a16266e | 2009-09-10 21:08:39 | [diff] [blame] | 193 | } // namespace base |
| 194 | |
[email protected] | baa010b | 2008-11-08 23:16:31 | [diff] [blame] | 195 | #endif // BASE_STRING_PIECE_H_ |