| 6 |
| #include "base/strings/string_piece.h" |
| 7 |
| 8 | #include <limits.h> |
| 9 |
| 10 | #include <algorithm> |
| 11 | #include <ostream> |
| 12 |
| 13 | #include "base/logging.h" |
| 14 | #include "base/strings/utf_string_conversions.h" |
| 15 |
| 16 | namespace base { |
| 17 | namespace { |
| 18 |
| 19 | // For each character in characters_wanted, sets the index corresponding |
| 20 | // to the ASCII code of that character to 1 in table. This is used by |
| 21 | // the find_.*_of methods below to tell whether or not a character is in |
| 22 | // the lookup table in constant time. |
| 23 | // The argument `table' must be an array that is large enough to hold all |
| 24 | // the possible values of an unsigned char. Thus it should be be declared |
| 25 | // as follows: |
| 26 | // bool table[UCHAR_MAX + 1] |
| 27 | inline void BuildLookupTable(const StringPiece& characters_wanted, |
| 28 | bool* table) { |
| 29 | const size_t length = characters_wanted.length(); |
| 30 | const char* const data = characters_wanted.data(); |
| 31 | for (size_t i = 0; i < length; ++i) { |
| 32 | table[static_cast<unsigned char>(data[i])] = true; |
| 33 | } |
| 34 | } |
| 35 |
| 36 | } // namespace |
| 37 |
| 38 | // MSVC doesn't like complex extern templates and DLLs. |
| 39 | #if !defined(COMPILER_MSVC) |
| 40 | template class BasicStringPiece<std::string>; |
| 41 | template class BasicStringPiece<string16>; |
| 42 | #endif |
| 43 |
| 44 | std::ostream& operator<<(std::ostream& o, const StringPiece& piece) { |
| 45 | o.write(piece.data(), static_cast<std::streamsize>(piece.size())); |
| 46 | return o; |
| 47 | } |
| 48 |
| 49 | std::ostream& operator<<(std::ostream& o, const StringPiece16& piece) { |
| 50 | return o << UTF16ToUTF8(piece); |
| 51 | } |
| 52 |
| 53 | namespace internal { |
| 54 |
| 55 | template<typename STR> |
| 56 | size_t copyT(const BasicStringPiece<STR>& self, |
| 57 | typename STR::value_type* buf, |
| 58 | size_t n, |
| 59 | size_t pos) { |
| 60 | size_t ret = std::min(self.size() - pos, n); |
| 61 | memcpy(buf, self.data() + pos, ret * sizeof(typename STR::value_type)); |
| 62 | return ret; |
| 63 | } |
| 64 |
| 65 | size_t copy(const StringPiece& self, char* buf, size_t n, size_t pos) { |
| 66 | return copyT(self, buf, n, pos); |
| 67 | } |
| 68 |
| 69 | size_t copy(const StringPiece16& self, char16* buf, size_t n, size_t pos) { |
| 70 | return copyT(self, buf, n, pos); |
| 71 | } |
| 72 |
| 73 | template<typename STR> |
| 74 | size_t findT(const BasicStringPiece<STR>& self, |
| 75 | const BasicStringPiece<STR>& s, |
| 76 | size_t pos) { |
| 77 | if (pos > self.size()) |
| 78 | return BasicStringPiece<STR>::npos; |
| 79 |
| 80 | typename BasicStringPiece<STR>::const_iterator result = |
| 81 | std::search(self.begin() + pos, self.end(), s.begin(), s.end()); |
| 82 | const size_t xpos = |
| 83 | static_cast<size_t>(result - self.begin()); |
| 84 | return xpos + s.size() <= self.size() ? xpos : BasicStringPiece<STR>::npos; |
| 85 | } |
| 86 |
| 87 | size_t find(const StringPiece& self, const StringPiece& s, size_t pos) { |
| 88 | return findT(self, s, pos); |
| 89 | } |
| 90 |
| 91 | size_t find(const StringPiece16& self, const StringPiece16& s, size_t pos) { |
| 92 | return findT(self, s, pos); |
| 93 | } |
| 94 |
| 95 | template<typename STR> |
| 96 | size_t findT(const BasicStringPiece<STR>& self, |
| 97 | typename STR::value_type c, |
| 98 | size_t pos) { |
| 99 | if (pos >= self.size()) |
| 100 | return BasicStringPiece<STR>::npos; |
| 101 |
| 102 | typename BasicStringPiece<STR>::const_iterator result = |
| 103 | std::find(self.begin() + pos, self.end(), c); |
| 104 | return result != self.end() ? |
| 105 | static_cast<size_t>(result - self.begin()) : BasicStringPiece<STR>::npos; |
| 106 | } |
| 107 |
| 108 | size_t find(const StringPiece& self, char c, size_t pos) { |
| 109 | return findT(self, c, pos); |
| 110 | } |
| 111 |
| 112 | size_t find(const StringPiece16& self, char16 c, size_t pos) { |
| 113 | return findT(self, c, pos); |
| 114 | } |
| 115 |
| 116 | template<typename STR> |
| 117 | size_t rfindT(const BasicStringPiece<STR>& self, |
| 118 | const BasicStringPiece<STR>& s, |
| 119 | size_t pos) { |
| 120 | if (self.size() < s.size()) |
| 121 | return BasicStringPiece<STR>::npos; |
| 122 |
| 123 | if (s.empty()) |
| 124 | return std::min(self.size(), pos); |
| 125 |
| 126 | typename BasicStringPiece<STR>::const_iterator last = |
| 127 | self.begin() + std::min(self.size() - s.size(), pos) + s.size(); |
| 128 | typename BasicStringPiece<STR>::const_iterator result = |
| 129 | std::find_end(self.begin(), last, s.begin(), s.end()); |
| 130 | return result != last ? |
| 131 | static_cast<size_t>(result - self.begin()) : BasicStringPiece<STR>::npos; |
| 132 | } |
| 133 |
| 134 | size_t rfind(const StringPiece& self, const StringPiece& s, size_t pos) { |
| 135 | return rfindT(self, s, pos); |
| 136 | } |
| 137 |
| 138 | size_t rfind(const StringPiece16& self, const StringPiece16& s, size_t pos) { |
| 139 | return rfindT(self, s, pos); |
| 140 | } |
| 141 |
| 142 | template<typename STR> |
| 143 | size_t rfindT(const BasicStringPiece<STR>& self, |
| 144 | typename STR::value_type c, |
| 145 | size_t pos) { |
| 146 | if (self.size() == 0) |
| 147 | return BasicStringPiece<STR>::npos; |
| 148 |
| 149 | for (size_t i = std::min(pos, self.size() - 1); ; |
| 150 | --i) { |
| 151 | if (self.data()[i] == c) |
| 152 | return i; |
| 153 | if (i == 0) |
| 154 | break; |
| 155 | } |
| 156 | return BasicStringPiece<STR>::npos; |
| 157 | } |
| 158 |
| 159 | size_t rfind(const StringPiece& self, char c, size_t pos) { |
| 160 | return rfindT(self, c, pos); |
| 161 | } |
| 162 |
| 163 | size_t rfind(const StringPiece16& self, char16 c, size_t pos) { |
| 164 | return rfindT(self, c, pos); |
| 165 | } |
| 166 |
| 167 | // 8-bit version using lookup table. |
| 168 | size_t find_first_of(const StringPiece& self, |
| 169 | const StringPiece& s, |
| 170 | size_t pos) { |
| 171 | if (self.size() == 0 || s.size() == 0) |
| 172 | return StringPiece::npos; |
| 173 |
| 174 | // Avoid the cost of BuildLookupTable() for a single-character search. |
| 175 | if (s.size() == 1) |
| 176 | return find(self, s.data()[0], pos); |
| 177 |
| 178 | bool lookup[UCHAR_MAX + 1] = { false }; |
| 179 | BuildLookupTable(s, lookup); |
| 180 | for (size_t i = pos; i < self.size(); ++i) { |
| 181 | if (lookup[static_cast<unsigned char>(self.data()[i])]) { |
| 182 | return i; |
| 183 | } |
| 184 | } |
| 185 | return StringPiece::npos; |
| 186 | } |
| 187 |
| 188 | // 16-bit brute force version. |
| 189 | size_t find_first_of(const StringPiece16& self, |
| 190 | const StringPiece16& s, |
| 191 | size_t pos) { |
| 192 | // Use the faster std::find() if searching for a single character. |
| 193 | StringPiece16::const_iterator found = |
| 194 | s.size() == 1 ? std::find(self.begin() + pos, self.end(), s[0]) |
| 195 | : std::find_first_of(self.begin() + pos, self.end(), |
| 196 | s.begin(), s.end()); |
| 197 | if (found == self.end()) |
| 198 | return StringPiece16::npos; |
| 199 | return found - self.begin(); |
| 200 | } |
| 201 |
| 202 | // 8-bit version using lookup table. |
| 203 | size_t find_first_not_of(const StringPiece& self, |
| 204 | const StringPiece& s, |
| 205 | size_t pos) { |
| 206 | if (self.size() == 0) |
| 207 | return StringPiece::npos; |
| 208 |
| 209 | if (s.size() == 0) |
| 210 | return 0; |
| 211 |
| 212 | // Avoid the cost of BuildLookupTable() for a single-character search. |
| 213 | if (s.size() == 1) |
| 214 | return find_first_not_of(self, s.data()[0], pos); |
| 215 |
| 216 | bool lookup[UCHAR_MAX + 1] = { false }; |
| 217 | BuildLookupTable(s, lookup); |
| 218 | for (size_t i = pos; i < self.size(); ++i) { |
| 219 | if (!lookup[static_cast<unsigned char>(self.data()[i])]) { |
| 220 | return i; |
| 221 | } |
| 222 | } |
| 223 | return StringPiece::npos; |
| 224 | } |
| 225 |
| 226 | // 16-bit brute-force version. |
| 227 | BASE_EXPORT size_t find_first_not_of(const StringPiece16& self, |
| 228 | const StringPiece16& s, |
| 229 | size_t pos) { |
| 230 | if (self.size() == 0) |
| 231 | return StringPiece16::npos; |
| 232 |
| 233 | for (size_t self_i = pos; self_i < self.size(); ++self_i) { |
| 234 | bool found = false; |
| 235 | for (auto c : s) { |
| 236 | if (self[self_i] == c) { |
| 237 | found = true; |
| 238 | break; |
| 239 | } |
| 240 | } |
| 241 | if (!found) |
| 242 | return self_i; |
| 243 | } |
| 244 | return StringPiece16::npos; |
| 245 | } |
| 246 |
| 247 | template<typename STR> |
| 248 | size_t find_first_not_ofT(const BasicStringPiece<STR>& self, |
| 249 | typename STR::value_type c, |
| 250 | size_t pos) { |
| 251 | if (self.size() == 0) |
| 252 | return BasicStringPiece<STR>::npos; |
| 253 |
| 254 | for (; pos < self.size(); ++pos) { |
| 255 | if (self.data()[pos] != c) { |
| 256 | return pos; |
| 257 | } |
| 258 | } |
| 259 | return BasicStringPiece<STR>::npos; |
| 260 | } |
| 261 |
| 262 | size_t find_first_not_of(const StringPiece& self, |
| 263 | char c, |
| 264 | size_t pos) { |
| 265 | return find_first_not_ofT(self, c, pos); |
| 266 | } |
| 267 |
| 268 | size_t find_first_not_of(const StringPiece16& self, |
| 269 | char16 c, |
| 270 | size_t pos) { |
| 271 | return find_first_not_ofT(self, c, pos); |
| 272 | } |
| 273 |
| 274 | // 8-bit version using lookup table. |
| 275 | size_t find_last_of(const StringPiece& self, const StringPiece& s, size_t pos) { |
| 276 | if (self.size() == 0 || s.size() == 0) |
| 277 | return StringPiece::npos; |
| 278 |
| 279 | // Avoid the cost of BuildLookupTable() for a single-character search. |
| 280 | if (s.size() == 1) |
| 281 | return rfind(self, s.data()[0], pos); |
| 282 |
| 283 | bool lookup[UCHAR_MAX + 1] = { false }; |
| 284 | BuildLookupTable(s, lookup); |
| 285 | for (size_t i = std::min(pos, self.size() - 1); ; --i) { |
| 286 | if (lookup[static_cast<unsigned char>(self.data()[i])]) |
| 287 | return i; |
| 288 | if (i == 0) |
| 289 | break; |
| 290 | } |
| 291 | return StringPiece::npos; |
| 292 | } |
| 293 |
| 294 | // 16-bit brute-force version. |
| 295 | size_t find_last_of(const StringPiece16& self, |
| 296 | const StringPiece16& s, |
| 297 | size_t pos) { |
| 298 | if (self.size() == 0) |
| 299 | return StringPiece16::npos; |
| 300 |
| 301 | for (size_t self_i = std::min(pos, self.size() - 1); ; |
| 302 | --self_i) { |
| 303 | for (auto c : s) { |
| 304 | if (self.data()[self_i] == c) |
| 305 | return self_i; |
| 306 | } |
| 307 | if (self_i == 0) |
| 308 | break; |
| 309 | } |
| 310 | return StringPiece16::npos; |
| 311 | } |
| 312 |
| 313 | // 8-bit version using lookup table. |
| 314 | size_t find_last_not_of(const StringPiece& self, |
| 315 | const StringPiece& s, |
| 316 | size_t pos) { |
| 317 | if (self.size() == 0) |
| 318 | return StringPiece::npos; |
| 319 |
| 320 | size_t i = std::min(pos, self.size() - 1); |
| 321 | if (s.size() == 0) |
| 322 | return i; |
| 323 |
| 324 | // Avoid the cost of BuildLookupTable() for a single-character search. |
| 325 | if (s.size() == 1) |
| 326 | return find_last_not_of(self, s.data()[0], pos); |
| 327 |
| 328 | bool lookup[UCHAR_MAX + 1] = { false }; |
| 329 | BuildLookupTable(s, lookup); |
| 330 | for (; ; --i) { |
| 331 | if (!lookup[static_cast<unsigned char>(self.data()[i])]) |
| 332 | return i; |
| 333 | if (i == 0) |
| 334 | break; |
| 335 | } |
| 336 | return StringPiece::npos; |
| 337 | } |
| 338 |
| 339 | // 16-bit brute-force version. |
| 340 | size_t find_last_not_of(const StringPiece16& self, |
| 341 | const StringPiece16& s, |
| 342 | size_t pos) { |
| 343 | if (self.size() == 0) |
| 344 | return StringPiece::npos; |
| 345 |
| 346 | for (size_t self_i = std::min(pos, self.size() - 1); ; --self_i) { |
| 347 | bool found = false; |
| 348 | for (auto c : s) { |
| 349 | if (self.data()[self_i] == c) { |
| 350 | found = true; |
| 351 | break; |
| 352 | } |
| 353 | } |
| 354 | if (!found) |
| 355 | return self_i; |
| 356 | if (self_i == 0) |
| 357 | break; |
| 358 | } |
| 359 | return StringPiece16::npos; |
| 360 | } |
| 361 |
| 362 | template<typename STR> |
| 363 | size_t find_last_not_ofT(const BasicStringPiece<STR>& self, |
| 364 | typename STR::value_type c, |
| 365 | size_t pos) { |
| 366 | if (self.size() == 0) |
| 367 | return BasicStringPiece<STR>::npos; |
| 368 |
| 369 | for (size_t i = std::min(pos, self.size() - 1); ; --i) { |
| 370 | if (self.data()[i] != c) |
| 371 | return i; |
| 372 | if (i == 0) |
| 373 | break; |
| 374 | } |
| 375 | return BasicStringPiece<STR>::npos; |
| 376 | } |
| 377 |
| 378 | size_t find_last_not_of(const StringPiece& self, |
| 379 | char c, |
| 380 | size_t pos) { |
| 381 | return find_last_not_ofT(self, c, pos); |
| 382 | } |
| 383 |
| 384 | size_t find_last_not_of(const StringPiece16& self, |
| 385 | char16 c, |
| 386 | size_t pos) { |
| 387 | return find_last_not_ofT(self, c, pos); |
| 388 | } |
| 389 |
| 390 | template<typename STR> |
| 391 | BasicStringPiece<STR> substrT(const BasicStringPiece<STR>& self, |
| 392 | size_t pos, |
| 393 | size_t n) { |
| 394 | if (pos > self.size()) pos = self.size(); |
| 395 | if (n > self.size() - pos) n = self.size() - pos; |
| 396 | return BasicStringPiece<STR>(self.data() + pos, n); |
| 397 | } |
| 398 |
| 399 | StringPiece substr(const StringPiece& self, |
| 400 | size_t pos, |
| 401 | size_t n) { |
| 402 | return substrT(self, pos, n); |
| 403 | } |
| 404 |
| 405 | StringPiece16 substr(const StringPiece16& self, |
| 406 | size_t pos, |
| 407 | size_t n) { |
| 408 | return substrT(self, pos, n); |
| 409 | } |
| 410 |
| 411 | } // namespace internal |
| 412 | } // namespace base |
| 413 |