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