Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | // Copied from strings/stringpiece.h with modifications |
michael@0 | 5 | // |
michael@0 | 6 | // A string-like object that points to a sized piece of memory. |
michael@0 | 7 | // |
michael@0 | 8 | // Functions or methods may use const StringPiece& parameters to accept either |
michael@0 | 9 | // a "const char*" or a "string" value that will be implicitly converted to |
michael@0 | 10 | // a StringPiece. The implicit conversion means that it is often appropriate |
michael@0 | 11 | // to include this .h file in other files rather than forward-declaring |
michael@0 | 12 | // StringPiece as would be appropriate for most other Google classes. |
michael@0 | 13 | // |
michael@0 | 14 | // Systematic usage of StringPiece is encouraged as it will reduce unnecessary |
michael@0 | 15 | // conversions from "const char*" to "string" and back again. |
michael@0 | 16 | // |
michael@0 | 17 | |
michael@0 | 18 | #ifndef BASE_STRING_PIECE_H_ |
michael@0 | 19 | #define BASE_STRING_PIECE_H_ |
michael@0 | 20 | |
michael@0 | 21 | #include <algorithm> |
michael@0 | 22 | #include <iosfwd> |
michael@0 | 23 | #include <string> |
michael@0 | 24 | |
michael@0 | 25 | #include "base/basictypes.h" |
michael@0 | 26 | |
michael@0 | 27 | class StringPiece { |
michael@0 | 28 | public: |
michael@0 | 29 | typedef size_t size_type; |
michael@0 | 30 | |
michael@0 | 31 | private: |
michael@0 | 32 | const char* ptr_; |
michael@0 | 33 | size_type length_; |
michael@0 | 34 | |
michael@0 | 35 | public: |
michael@0 | 36 | // We provide non-explicit singleton constructors so users can pass |
michael@0 | 37 | // in a "const char*" or a "string" wherever a "StringPiece" is |
michael@0 | 38 | // expected. |
michael@0 | 39 | StringPiece() : ptr_(NULL), length_(0) { } |
michael@0 | 40 | StringPiece(const char* str) |
michael@0 | 41 | : ptr_(str), length_((str == NULL) ? 0 : strlen(str)) { } |
michael@0 | 42 | StringPiece(const std::string& str) |
michael@0 | 43 | : ptr_(str.data()), length_(str.size()) { } |
michael@0 | 44 | StringPiece(const char* offset, size_type len) |
michael@0 | 45 | : ptr_(offset), length_(len) { } |
michael@0 | 46 | |
michael@0 | 47 | // data() may return a pointer to a buffer with embedded NULs, and the |
michael@0 | 48 | // returned buffer may or may not be null terminated. Therefore it is |
michael@0 | 49 | // typically a mistake to pass data() to a routine that expects a NUL |
michael@0 | 50 | // terminated string. |
michael@0 | 51 | const char* data() const { return ptr_; } |
michael@0 | 52 | size_type size() const { return length_; } |
michael@0 | 53 | size_type length() const { return length_; } |
michael@0 | 54 | bool empty() const { return length_ == 0; } |
michael@0 | 55 | |
michael@0 | 56 | void clear() { ptr_ = NULL; length_ = 0; } |
michael@0 | 57 | void set(const char* data, size_type len) { ptr_ = data; length_ = len; } |
michael@0 | 58 | void set(const char* str) { |
michael@0 | 59 | ptr_ = str; |
michael@0 | 60 | length_ = str ? strlen(str) : 0; |
michael@0 | 61 | } |
michael@0 | 62 | void set(const void* data, size_type len) { |
michael@0 | 63 | ptr_ = reinterpret_cast<const char*>(data); |
michael@0 | 64 | length_ = len; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | char operator[](size_type i) const { return ptr_[i]; } |
michael@0 | 68 | |
michael@0 | 69 | void remove_prefix(size_type n) { |
michael@0 | 70 | ptr_ += n; |
michael@0 | 71 | length_ -= n; |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | void remove_suffix(size_type n) { |
michael@0 | 75 | length_ -= n; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | int compare(const StringPiece& x) const { |
michael@0 | 79 | int r = wordmemcmp(ptr_, x.ptr_, std::min(length_, x.length_)); |
michael@0 | 80 | if (r == 0) { |
michael@0 | 81 | if (length_ < x.length_) r = -1; |
michael@0 | 82 | else if (length_ > x.length_) r = +1; |
michael@0 | 83 | } |
michael@0 | 84 | return r; |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | std::string as_string() const { |
michael@0 | 88 | // std::string doesn't like to take a NULL pointer even with a 0 size. |
michael@0 | 89 | return std::string(!empty() ? data() : "", size()); |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | void CopyToString(std::string* target) const; |
michael@0 | 93 | void AppendToString(std::string* target) const; |
michael@0 | 94 | |
michael@0 | 95 | // Does "this" start with "x" |
michael@0 | 96 | bool starts_with(const StringPiece& x) const { |
michael@0 | 97 | return ((length_ >= x.length_) && |
michael@0 | 98 | (wordmemcmp(ptr_, x.ptr_, x.length_) == 0)); |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | // Does "this" end with "x" |
michael@0 | 102 | bool ends_with(const StringPiece& x) const { |
michael@0 | 103 | return ((length_ >= x.length_) && |
michael@0 | 104 | (wordmemcmp(ptr_ + (length_-x.length_), x.ptr_, x.length_) == 0)); |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | // standard STL container boilerplate |
michael@0 | 108 | typedef char value_type; |
michael@0 | 109 | typedef const char* pointer; |
michael@0 | 110 | typedef const char& reference; |
michael@0 | 111 | typedef const char& const_reference; |
michael@0 | 112 | typedef ptrdiff_t difference_type; |
michael@0 | 113 | static const size_type npos; |
michael@0 | 114 | typedef const char* const_iterator; |
michael@0 | 115 | typedef const char* iterator; |
michael@0 | 116 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
michael@0 | 117 | typedef std::reverse_iterator<iterator> reverse_iterator; |
michael@0 | 118 | iterator begin() const { return ptr_; } |
michael@0 | 119 | iterator end() const { return ptr_ + length_; } |
michael@0 | 120 | const_reverse_iterator rbegin() const { |
michael@0 | 121 | return const_reverse_iterator(ptr_ + length_); |
michael@0 | 122 | } |
michael@0 | 123 | const_reverse_iterator rend() const { |
michael@0 | 124 | return const_reverse_iterator(ptr_); |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | size_type max_size() const { return length_; } |
michael@0 | 128 | size_type capacity() const { return length_; } |
michael@0 | 129 | |
michael@0 | 130 | size_type copy(char* buf, size_type n, size_type pos = 0) const; |
michael@0 | 131 | |
michael@0 | 132 | size_type find(const StringPiece& s, size_type pos = 0) const; |
michael@0 | 133 | size_type find(char c, size_type pos = 0) const; |
michael@0 | 134 | size_type rfind(const StringPiece& s, size_type pos = npos) const; |
michael@0 | 135 | size_type rfind(char c, size_type pos = npos) const; |
michael@0 | 136 | |
michael@0 | 137 | size_type find_first_of(const StringPiece& s, size_type pos = 0) const; |
michael@0 | 138 | size_type find_first_of(char c, size_type pos = 0) const { |
michael@0 | 139 | return find(c, pos); |
michael@0 | 140 | } |
michael@0 | 141 | size_type find_first_not_of(const StringPiece& s, size_type pos = 0) const; |
michael@0 | 142 | size_type find_first_not_of(char c, size_type pos = 0) const; |
michael@0 | 143 | size_type find_last_of(const StringPiece& s, size_type pos = npos) const; |
michael@0 | 144 | size_type find_last_of(char c, size_type pos = npos) const { |
michael@0 | 145 | return rfind(c, pos); |
michael@0 | 146 | } |
michael@0 | 147 | size_type find_last_not_of(const StringPiece& s, size_type pos = npos) const; |
michael@0 | 148 | size_type find_last_not_of(char c, size_type pos = npos) const; |
michael@0 | 149 | |
michael@0 | 150 | StringPiece substr(size_type pos, size_type n = npos) const; |
michael@0 | 151 | |
michael@0 | 152 | static int wordmemcmp(const char* p, const char* p2, size_type N) { |
michael@0 | 153 | return memcmp(p, p2, N); |
michael@0 | 154 | } |
michael@0 | 155 | }; |
michael@0 | 156 | |
michael@0 | 157 | bool operator==(const ::StringPiece& x, const ::StringPiece& y); |
michael@0 | 158 | |
michael@0 | 159 | inline bool operator!=(const ::StringPiece& x, const ::StringPiece& y) { |
michael@0 | 160 | return !(x == y); |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | inline bool operator<(const ::StringPiece& x, const ::StringPiece& y) { |
michael@0 | 164 | const int r = ::StringPiece::wordmemcmp(x.data(), y.data(), |
michael@0 | 165 | std::min(x.size(), y.size())); |
michael@0 | 166 | return ((r < 0) || ((r == 0) && (x.size() < y.size()))); |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | inline bool operator>(const ::StringPiece& x, const ::StringPiece& y) { |
michael@0 | 170 | return y < x; |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | inline bool operator<=(const ::StringPiece& x, const ::StringPiece& y) { |
michael@0 | 174 | return !(x > y); |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | inline bool operator>=(const ::StringPiece& x, const ::StringPiece& y) { |
michael@0 | 178 | return !(x < y); |
michael@0 | 179 | } |
michael@0 | 180 | |
michael@0 | 181 | // allow StringPiece to be logged (needed for unit testing). |
michael@0 | 182 | extern std::ostream& operator<<(std::ostream& o, const ::StringPiece& piece); |
michael@0 | 183 | |
michael@0 | 184 | #endif // BASE_STRING_PIECE_H_ |