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.cc with modifications |
michael@0 | 5 | |
michael@0 | 6 | #include <algorithm> |
michael@0 | 7 | #include <ostream> |
michael@0 | 8 | |
michael@0 | 9 | #include "base/string_piece.h" |
michael@0 | 10 | |
michael@0 | 11 | typedef StringPiece::size_type size_type; |
michael@0 | 12 | |
michael@0 | 13 | std::ostream& operator<<(std::ostream& o, const StringPiece& piece) { |
michael@0 | 14 | o.write(piece.data(), static_cast<std::streamsize>(piece.size())); |
michael@0 | 15 | return o; |
michael@0 | 16 | } |
michael@0 | 17 | |
michael@0 | 18 | bool operator==(const StringPiece& x, const StringPiece& y) { |
michael@0 | 19 | if (x.size() != y.size()) |
michael@0 | 20 | return false; |
michael@0 | 21 | |
michael@0 | 22 | return StringPiece::wordmemcmp(x.data(), y.data(), x.size()) == 0; |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | void StringPiece::CopyToString(std::string* target) const { |
michael@0 | 26 | target->assign(!empty() ? data() : "", size()); |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | void StringPiece::AppendToString(std::string* target) const { |
michael@0 | 30 | if (!empty()) |
michael@0 | 31 | target->append(data(), size()); |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | size_type StringPiece::copy(char* buf, size_type n, size_type pos) const { |
michael@0 | 35 | size_type ret = std::min(length_ - pos, n); |
michael@0 | 36 | memcpy(buf, ptr_ + pos, ret); |
michael@0 | 37 | return ret; |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | size_type StringPiece::find(const StringPiece& s, size_type pos) const { |
michael@0 | 41 | if (pos > length_) |
michael@0 | 42 | return npos; |
michael@0 | 43 | |
michael@0 | 44 | const char* result = std::search(ptr_ + pos, ptr_ + length_, |
michael@0 | 45 | s.ptr_, s.ptr_ + s.length_); |
michael@0 | 46 | const size_type xpos = result - ptr_; |
michael@0 | 47 | return xpos + s.length_ <= length_ ? xpos : npos; |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | size_type StringPiece::find(char c, size_type pos) const { |
michael@0 | 51 | if (pos >= length_) |
michael@0 | 52 | return npos; |
michael@0 | 53 | |
michael@0 | 54 | const char* result = std::find(ptr_ + pos, ptr_ + length_, c); |
michael@0 | 55 | return result != ptr_ + length_ ? result - ptr_ : npos; |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | size_type StringPiece::rfind(const StringPiece& s, size_type pos) const { |
michael@0 | 59 | if (length_ < s.length_) |
michael@0 | 60 | return npos; |
michael@0 | 61 | |
michael@0 | 62 | if (s.empty()) |
michael@0 | 63 | return std::min(length_, pos); |
michael@0 | 64 | |
michael@0 | 65 | const char* last = ptr_ + std::min(length_ - s.length_, pos) + s.length_; |
michael@0 | 66 | const char* result = std::find_end(ptr_, last, s.ptr_, s.ptr_ + s.length_); |
michael@0 | 67 | return result != last ? result - ptr_ : npos; |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | size_type StringPiece::rfind(char c, size_type pos) const { |
michael@0 | 71 | if (length_ == 0) |
michael@0 | 72 | return npos; |
michael@0 | 73 | |
michael@0 | 74 | for (size_type i = std::min(pos, length_ - 1); ; --i) { |
michael@0 | 75 | if (ptr_[i] == c) |
michael@0 | 76 | return i; |
michael@0 | 77 | if (i == 0) |
michael@0 | 78 | break; |
michael@0 | 79 | } |
michael@0 | 80 | return npos; |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | // For each character in characters_wanted, sets the index corresponding |
michael@0 | 84 | // to the ASCII code of that character to 1 in table. This is used by |
michael@0 | 85 | // the find_.*_of methods below to tell whether or not a character is in |
michael@0 | 86 | // the lookup table in constant time. |
michael@0 | 87 | // The argument `table' must be an array that is large enough to hold all |
michael@0 | 88 | // the possible values of an unsigned char. Thus it should be be declared |
michael@0 | 89 | // as follows: |
michael@0 | 90 | // bool table[UCHAR_MAX + 1] |
michael@0 | 91 | static inline void BuildLookupTable(const StringPiece& characters_wanted, |
michael@0 | 92 | bool* table) { |
michael@0 | 93 | const size_type length = characters_wanted.length(); |
michael@0 | 94 | const char* const data = characters_wanted.data(); |
michael@0 | 95 | for (size_type i = 0; i < length; ++i) { |
michael@0 | 96 | table[static_cast<unsigned char>(data[i])] = true; |
michael@0 | 97 | } |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | size_type StringPiece::find_first_of(const StringPiece& s, |
michael@0 | 101 | size_type pos) const { |
michael@0 | 102 | if (length_ == 0 || s.length_ == 0) |
michael@0 | 103 | return npos; |
michael@0 | 104 | |
michael@0 | 105 | // Avoid the cost of BuildLookupTable() for a single-character search. |
michael@0 | 106 | if (s.length_ == 1) |
michael@0 | 107 | return find_first_of(s.ptr_[0], pos); |
michael@0 | 108 | |
michael@0 | 109 | bool lookup[UCHAR_MAX + 1] = { false }; |
michael@0 | 110 | BuildLookupTable(s, lookup); |
michael@0 | 111 | for (size_type i = pos; i < length_; ++i) { |
michael@0 | 112 | if (lookup[static_cast<unsigned char>(ptr_[i])]) { |
michael@0 | 113 | return i; |
michael@0 | 114 | } |
michael@0 | 115 | } |
michael@0 | 116 | return npos; |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | size_type StringPiece::find_first_not_of(const StringPiece& s, |
michael@0 | 120 | size_type pos) const { |
michael@0 | 121 | if (length_ == 0) |
michael@0 | 122 | return npos; |
michael@0 | 123 | |
michael@0 | 124 | if (s.length_ == 0) |
michael@0 | 125 | return 0; |
michael@0 | 126 | |
michael@0 | 127 | // Avoid the cost of BuildLookupTable() for a single-character search. |
michael@0 | 128 | if (s.length_ == 1) |
michael@0 | 129 | return find_first_not_of(s.ptr_[0], pos); |
michael@0 | 130 | |
michael@0 | 131 | bool lookup[UCHAR_MAX + 1] = { false }; |
michael@0 | 132 | BuildLookupTable(s, lookup); |
michael@0 | 133 | for (size_type i = pos; i < length_; ++i) { |
michael@0 | 134 | if (!lookup[static_cast<unsigned char>(ptr_[i])]) { |
michael@0 | 135 | return i; |
michael@0 | 136 | } |
michael@0 | 137 | } |
michael@0 | 138 | return npos; |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | size_type StringPiece::find_first_not_of(char c, size_type pos) const { |
michael@0 | 142 | if (length_ == 0) |
michael@0 | 143 | return npos; |
michael@0 | 144 | |
michael@0 | 145 | for (; pos < length_; ++pos) { |
michael@0 | 146 | if (ptr_[pos] != c) { |
michael@0 | 147 | return pos; |
michael@0 | 148 | } |
michael@0 | 149 | } |
michael@0 | 150 | return npos; |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | size_type StringPiece::find_last_of(const StringPiece& s, size_type pos) const { |
michael@0 | 154 | if (length_ == 0 || s.length_ == 0) |
michael@0 | 155 | return npos; |
michael@0 | 156 | |
michael@0 | 157 | // Avoid the cost of BuildLookupTable() for a single-character search. |
michael@0 | 158 | if (s.length_ == 1) |
michael@0 | 159 | return find_last_of(s.ptr_[0], pos); |
michael@0 | 160 | |
michael@0 | 161 | bool lookup[UCHAR_MAX + 1] = { false }; |
michael@0 | 162 | BuildLookupTable(s, lookup); |
michael@0 | 163 | for (size_type i = std::min(pos, length_ - 1); ; --i) { |
michael@0 | 164 | if (lookup[static_cast<unsigned char>(ptr_[i])]) |
michael@0 | 165 | return i; |
michael@0 | 166 | if (i == 0) |
michael@0 | 167 | break; |
michael@0 | 168 | } |
michael@0 | 169 | return npos; |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | size_type StringPiece::find_last_not_of(const StringPiece& s, |
michael@0 | 173 | size_type pos) const { |
michael@0 | 174 | if (length_ == 0) |
michael@0 | 175 | return npos; |
michael@0 | 176 | |
michael@0 | 177 | size_type i = std::min(pos, length_ - 1); |
michael@0 | 178 | if (s.length_ == 0) |
michael@0 | 179 | return i; |
michael@0 | 180 | |
michael@0 | 181 | // Avoid the cost of BuildLookupTable() for a single-character search. |
michael@0 | 182 | if (s.length_ == 1) |
michael@0 | 183 | return find_last_not_of(s.ptr_[0], pos); |
michael@0 | 184 | |
michael@0 | 185 | bool lookup[UCHAR_MAX + 1] = { false }; |
michael@0 | 186 | BuildLookupTable(s, lookup); |
michael@0 | 187 | for (; ; --i) { |
michael@0 | 188 | if (!lookup[static_cast<unsigned char>(ptr_[i])]) |
michael@0 | 189 | return i; |
michael@0 | 190 | if (i == 0) |
michael@0 | 191 | break; |
michael@0 | 192 | } |
michael@0 | 193 | return npos; |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | size_type StringPiece::find_last_not_of(char c, size_type pos) const { |
michael@0 | 197 | if (length_ == 0) |
michael@0 | 198 | return npos; |
michael@0 | 199 | |
michael@0 | 200 | for (size_type i = std::min(pos, length_ - 1); ; --i) { |
michael@0 | 201 | if (ptr_[i] != c) |
michael@0 | 202 | return i; |
michael@0 | 203 | if (i == 0) |
michael@0 | 204 | break; |
michael@0 | 205 | } |
michael@0 | 206 | return npos; |
michael@0 | 207 | } |
michael@0 | 208 | |
michael@0 | 209 | StringPiece StringPiece::substr(size_type pos, size_type n) const { |
michael@0 | 210 | if (pos > length_) pos = length_; |
michael@0 | 211 | if (n > length_ - pos) n = length_ - pos; |
michael@0 | 212 | return StringPiece(ptr_ + pos, n); |
michael@0 | 213 | } |
michael@0 | 214 | |
michael@0 | 215 | const StringPiece::size_type StringPiece::npos = size_type(-1); |