| 6 |
| 7 | #include <ctype.h> |
| 8 | #include <errno.h> |
| 9 | #include <math.h> |
| 10 | #include <stdarg.h> |
| 11 | #include <stdint.h> |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | #include <time.h> |
| 16 | #include <wchar.h> |
| 17 | #include <wctype.h> |
| 18 |
| 19 | #include <algorithm> |
| 20 | #include <limits> |
| 21 | #include <vector> |
| 22 |
| 23 | #include "base/logging.h" |
| 24 | #include "base/no_destructor.h" |
| 25 | #include "base/stl_util.h" |
| 26 | #include "base/strings/utf_string_conversion_utils.h" |
| 27 | #include "base/strings/utf_string_conversions.h" |
| 28 | #include "base/third_party/icu/icu_utf.h" |
| 29 | #include "build/build_config.h" |
| 30 |
| 31 | namespace base { |
| 32 |
| 33 | namespace { |
| 34 |
| 35 | // Used by ReplaceStringPlaceholders to track the position in the string of |
| 36 | // replaced parameters. |
| 37 | struct ReplacementOffset { |
| 38 | ReplacementOffset(uintptr_t parameter, size_t offset) |
| 39 | : parameter(parameter), |
| 40 | offset(offset) {} |
| 41 |
| 42 | // Index of the parameter. |
| 43 | uintptr_t parameter; |
| 44 |
| 45 | // Starting position in the string. |
| 46 | size_t offset; |
| 47 | }; |
| 48 |
| 49 | static bool CompareParameter(const ReplacementOffset& elem1, |
| 50 | const ReplacementOffset& elem2) { |
| 51 | return elem1.parameter < elem2.parameter; |
| 52 | } |
| 53 |
| 54 | // Assuming that a pointer is the size of a "machine word", then |
| 55 | // uintptr_t is an integer type that is also a machine word. |
| 56 | using MachineWord = uintptr_t; |
| 57 |
| 58 | inline bool IsMachineWordAligned(const void* pointer) { |
| 59 | return !(reinterpret_cast<MachineWord>(pointer) & (sizeof(MachineWord) - 1)); |
| 60 | } |
| 61 |
| 62 | template <typename CharacterType> |
| 63 | struct NonASCIIMask; |
| 64 | template <> |
| 65 | struct NonASCIIMask<char> { |
| 66 | static constexpr MachineWord value() { |
| 67 | return static_cast<MachineWord>(0x8080808080808080ULL); |
| 68 | } |
| 69 | }; |
| 70 | template <> |
| 71 | struct NonASCIIMask<char16> { |
| 72 | static constexpr MachineWord value() { |
| 73 | return static_cast<MachineWord>(0xFF80FF80FF80FF80ULL); |
| 74 | } |
| 75 | }; |
| 76 | #if defined(WCHAR_T_IS_UTF32) |
| 77 | template <> |
| 78 | struct NonASCIIMask<wchar_t> { |
| 79 | static constexpr MachineWord value() { |
| 80 | return static_cast<MachineWord>(0xFFFFFF80FFFFFF80ULL); |
| 81 | } |
| 82 | }; |
| 83 | #endif // WCHAR_T_IS_UTF32 |
| 84 |
| 85 | } // namespace |
| 86 |
| 87 | bool IsWprintfFormatPortable(const wchar_t* format) { |
| 88 | for (const wchar_t* position = format; *position != '\0'; ++position) { |
| 89 | if (*position == '%') { |
| 90 | bool in_specification = true; |
| 91 | bool modifier_l = false; |
| 92 | while (in_specification) { |
| 93 | // Eat up characters until reaching a known specifier. |
| 94 | if (*++position == '\0') { |
| 95 | // The format string ended in the middle of a specification. Call |
| 96 | // it portable because no unportable specifications were found. The |
| 97 | // string is equally broken on all platforms. |
| 98 | return true; |
| 99 | } |
| 100 |
| 101 | if (*position == 'l') { |
| 102 | // 'l' is the only thing that can save the 's' and 'c' specifiers. |
| 103 | modifier_l = true; |
| 104 | } else if (((*position == 's' || *position == 'c') && !modifier_l) || |
| 105 | *position == 'S' || *position == 'C' || *position == 'F' || |
| 106 | *position == 'D' || *position == 'O' || *position == 'U') { |
| 107 | // Not portable. |
| 108 | return false; |
| 109 | } |
| 110 |
| 111 | if (wcschr(L"diouxXeEfgGaAcspn%", *position)) { |
| 112 | // Portable, keep scanning the rest of the format string. |
| 113 | in_specification = false; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 |
| 119 | return true; |
| 120 | } |
| 121 |
| 122 | namespace { |
| 123 |
| 124 | template<typename StringType> |
| 125 | StringType ToLowerASCIIImpl(BasicStringPiece<StringType> str) { |
| 126 | StringType ret; |
| 127 | ret.reserve(str.size()); |
| 128 | for (size_t i = 0; i < str.size(); i++) |
| 129 | ret.push_back(ToLowerASCII(str[i])); |
| 130 | return ret; |
| 131 | } |
| 132 |
| 133 | template<typename StringType> |
| 134 | StringType ToUpperASCIIImpl(BasicStringPiece<StringType> str) { |
| 135 | StringType ret; |
| 136 | ret.reserve(str.size()); |
| 137 | for (size_t i = 0; i < str.size(); i++) |
| 138 | ret.push_back(ToUpperASCII(str[i])); |
| 139 | return ret; |
| 140 | } |
| 141 |
| 142 | } // namespace |
| 143 |
| 144 | std::string ToLowerASCII(StringPiece str) { |
| 145 | return ToLowerASCIIImpl<std::string>(str); |
| 146 | } |
| 147 |
| 148 | string16 ToLowerASCII(StringPiece16 str) { |
| 149 | return ToLowerASCIIImpl<string16>(str); |
| 150 | } |
| 151 |
| 152 | std::string ToUpperASCII(StringPiece str) { |
| 153 | return ToUpperASCIIImpl<std::string>(str); |
| 154 | } |
| 155 |
| 156 | string16 ToUpperASCII(StringPiece16 str) { |
| 157 | return ToUpperASCIIImpl<string16>(str); |
| 158 | } |
| 159 |
| 160 | template<class StringType> |
| 161 | int CompareCaseInsensitiveASCIIT(BasicStringPiece<StringType> a, |
| 162 | BasicStringPiece<StringType> b) { |
| 163 | // Find the first characters that aren't equal and compare them. If the end |
| 164 | // of one of the strings is found before a nonequal character, the lengths |
| 165 | // of the strings are compared. |
| 166 | size_t i = 0; |
| 167 | while (i < a.length() && i < b.length()) { |
| 168 | typename StringType::value_type lower_a = ToLowerASCII(a[i]); |
| 169 | typename StringType::value_type lower_b = ToLowerASCII(b[i]); |
| 170 | if (lower_a < lower_b) |
| 171 | return -1; |
| 172 | if (lower_a > lower_b) |
| 173 | return 1; |
| 174 | i++; |
| 175 | } |
| 176 |
| 177 | // End of one string hit before finding a different character. Expect the |
| 178 | // common case to be "strings equal" at this point so check that first. |
| 179 | if (a.length() == b.length()) |
| 180 | return 0; |
| 181 |
| 182 | if (a.length() < b.length()) |
| 183 | return -1; |
| 184 | return 1; |
| 185 | } |
| 186 |
| 187 | int CompareCaseInsensitiveASCII(StringPiece a, StringPiece b) { |
| 188 | return CompareCaseInsensitiveASCIIT<std::string>(a, b); |
| 189 | } |
| 190 |
| 191 | int CompareCaseInsensitiveASCII(StringPiece16 a, StringPiece16 b) { |
| 192 | return CompareCaseInsensitiveASCIIT<string16>(a, b); |
| 193 | } |
| 194 |
| 195 | bool EqualsCaseInsensitiveASCII(StringPiece a, StringPiece b) { |
| 196 | if (a.length() != b.length()) |
| 197 | return false; |
| 198 | return CompareCaseInsensitiveASCIIT<std::string>(a, b) == 0; |
| 199 | } |
| 200 |
| 201 | bool EqualsCaseInsensitiveASCII(StringPiece16 a, StringPiece16 b) { |
| 202 | if (a.length() != b.length()) |
| 203 | return false; |
| 204 | return CompareCaseInsensitiveASCIIT<string16>(a, b) == 0; |
| 205 | } |
| 206 |
| 207 | const std::string& EmptyString() { |
| 208 | static const base::NoDestructor<std::string> s; |
| 209 | return *s; |
| 210 | } |
| 211 |
| 212 | const string16& EmptyString16() { |
| 213 | static const base::NoDestructor<string16> s16; |
| 214 | return *s16; |
| 215 | } |
| 216 |
| 217 | template <class StringType> |
| 218 | bool ReplaceCharsT(const StringType& input, |
| 219 | BasicStringPiece<StringType> find_any_of_these, |
| 220 | BasicStringPiece<StringType> replace_with, |
| 221 | StringType* output); |
| 222 |
| 223 | bool ReplaceChars(const string16& input, |
| 224 | StringPiece16 replace_chars, |
| 225 | StringPiece16 replace_with, |
| 226 | string16* output) { |
| 227 | return ReplaceCharsT(input, replace_chars, replace_with, output); |
| 228 | } |
| 229 |
| 230 | bool ReplaceChars(const std::string& input, |
| 231 | StringPiece replace_chars, |
| 232 | StringPiece replace_with, |
| 233 | std::string* output) { |
| 234 | return ReplaceCharsT(input, replace_chars, replace_with, output); |
| 235 | } |
| 236 |
| 237 | bool RemoveChars(const string16& input, |
| 238 | StringPiece16 remove_chars, |
| 239 | string16* output) { |
| 240 | return ReplaceCharsT(input, remove_chars, StringPiece16(), output); |
| 241 | } |
| 242 |
| 243 | bool RemoveChars(const std::string& input, |
| 244 | StringPiece remove_chars, |
| 245 | std::string* output) { |
| 246 | return ReplaceCharsT(input, remove_chars, StringPiece(), output); |
| 247 | } |
| 248 |
| 249 | template <typename Str> |
| 250 | TrimPositions TrimStringT(BasicStringPiece<Str> input, |
| 251 | BasicStringPiece<Str> trim_chars, |
| 252 | TrimPositions positions, |
| 253 | Str* output) { |
| 254 | // Find the edges of leading/trailing whitespace as desired. Need to use |
| 255 | // a StringPiece version of input to be able to call find* on it with the |
| 256 | // StringPiece version of trim_chars (normally the trim_chars will be a |
| 257 | // constant so avoid making a copy). |
| 258 | const size_t last_char = input.length() - 1; |
| 259 | const size_t first_good_char = |
| 260 | (positions & TRIM_LEADING) ? input.find_first_not_of(trim_chars) : 0; |
| 261 | const size_t last_good_char = (positions & TRIM_TRAILING) |
| 262 | ? input.find_last_not_of(trim_chars) |
| 263 | : last_char; |
| 264 |
| 265 | // When the string was all trimmed, report that we stripped off characters |
| 266 | // from whichever position the caller was interested in. For empty input, we |
| 267 | // stripped no characters, but we still need to clear |output|. |
| 268 | if (input.empty() || first_good_char == Str::npos || |
| 269 | last_good_char == Str::npos) { |
| 270 | bool input_was_empty = input.empty(); // in case output == &input |
| 271 | output->clear(); |
| 272 | return input_was_empty ? TRIM_NONE : positions; |
| 273 | } |
| 274 |
| 275 | // Trim. |
| 276 | output->assign(input.data() + first_good_char, |
| 277 | last_good_char - first_good_char + 1); |
| 278 |
| 279 | // Return where we trimmed from. |
| 280 | return static_cast<TrimPositions>( |
| 281 | (first_good_char == 0 ? TRIM_NONE : TRIM_LEADING) | |
| 282 | (last_good_char == last_char ? TRIM_NONE : TRIM_TRAILING)); |
| 283 | } |
| 284 |
| 285 | bool TrimString(StringPiece16 input, |
| 286 | StringPiece16 trim_chars, |
| 287 | string16* output) { |
| 288 | return TrimStringT(input, trim_chars, TRIM_ALL, output) != TRIM_NONE; |
| 289 | } |
| 290 |
| 291 | bool TrimString(StringPiece input, |
| 292 | StringPiece trim_chars, |
| 293 | std::string* output) { |
| 294 | return TrimStringT(input, trim_chars, TRIM_ALL, output) != TRIM_NONE; |
| 295 | } |
| 296 |
| 297 | template<typename Str> |
| 298 | BasicStringPiece<Str> TrimStringPieceT(BasicStringPiece<Str> input, |
| 299 | BasicStringPiece<Str> trim_chars, |
| 300 | TrimPositions positions) { |
| 301 | size_t begin = (positions & TRIM_LEADING) ? |
| 302 | input.find_first_not_of(trim_chars) : 0; |
| 303 | size_t end = (positions & TRIM_TRAILING) ? |
| 304 | input.find_last_not_of(trim_chars) + 1 : input.size(); |
| 305 | return input.substr(begin, end - begin); |
| 306 | } |
| 307 |
| 308 | StringPiece16 TrimString(StringPiece16 input, |
| 309 | StringPiece16 trim_chars, |
| 310 | TrimPositions positions) { |
| 311 | return TrimStringPieceT(input, trim_chars, positions); |
| 312 | } |
| 313 |
| 314 | StringPiece TrimString(StringPiece input, |
| 315 | StringPiece trim_chars, |
| 316 | TrimPositions positions) { |
| 317 | return TrimStringPieceT(input, trim_chars, positions); |
| 318 | } |
| 319 |
| 320 | void TruncateUTF8ToByteSize(const std::string& input, |
| 321 | const size_t byte_size, |
| 322 | std::string* output) { |
| 323 | DCHECK(output); |
| 324 | if (byte_size > input.length()) { |
| 325 | *output = input; |
| 326 | return; |
| 327 | } |
| 328 | DCHECK_LE(byte_size, |
| 329 | static_cast<uint32_t>(std::numeric_limits<int32_t>::max())); |
| 330 | // Note: This cast is necessary because CBU8_NEXT uses int32_ts. |
| 331 | int32_t truncation_length = static_cast<int32_t>(byte_size); |
| 332 | int32_t char_index = truncation_length - 1; |
| 333 | const char* data = input.data(); |
| 334 |
| 335 | // Using CBU8, we will move backwards from the truncation point |
| 336 | // to the beginning of the string looking for a valid UTF8 |
| 337 | // character. Once a full UTF8 character is found, we will |
| 338 | // truncate the string to the end of that character. |
| 339 | while (char_index >= 0) { |
| 340 | int32_t prev = char_index; |
| 341 | base_icu::UChar32 code_point = 0; |
| 342 | CBU8_NEXT(data, char_index, truncation_length, code_point); |
| 343 | if (!IsValidCharacter(code_point) || |
| 344 | !IsValidCodepoint(code_point)) { |
| 345 | char_index = prev - 1; |
| 346 | } else { |
| 347 | break; |
| 348 | } |
| 349 | } |
| 350 |
| 351 | if (char_index >= 0 ) |
| 352 | *output = input.substr(0, char_index); |
| 353 | else |
| 354 | output->clear(); |
| 355 | } |
| 356 |
| 357 | TrimPositions TrimWhitespace(StringPiece16 input, |
| 358 | TrimPositions positions, |
| 359 | string16* output) { |
| 360 | return TrimStringT(input, StringPiece16(kWhitespaceUTF16), positions, output); |
| 361 | } |
| 362 |
| 363 | StringPiece16 TrimWhitespace(StringPiece16 input, |
| 364 | TrimPositions positions) { |
| 365 | return TrimStringPieceT(input, StringPiece16(kWhitespaceUTF16), positions); |
| 366 | } |
| 367 |
| 368 | TrimPositions TrimWhitespaceASCII(StringPiece input, |
| 369 | TrimPositions positions, |
| 370 | std::string* output) { |
| 371 | return TrimStringT(input, StringPiece(kWhitespaceASCII), positions, output); |
| 372 | } |
| 373 |
| 374 | StringPiece TrimWhitespaceASCII(StringPiece input, TrimPositions positions) { |
| 375 | return TrimStringPieceT(input, StringPiece(kWhitespaceASCII), positions); |
| 376 | } |
| 377 |
| 378 | template<typename STR> |
| 379 | STR CollapseWhitespaceT(const STR& text, |
| 380 | bool trim_sequences_with_line_breaks) { |
| 381 | STR result; |
| 382 | result.resize(text.size()); |
| 383 |
| 384 | // Set flags to pretend we're already in a trimmed whitespace sequence, so we |
| 385 | // will trim any leading whitespace. |
| 386 | bool in_whitespace = true; |
| 387 | bool already_trimmed = true; |
| 388 |
| 389 | int chars_written = 0; |
| 390 | for (typename STR::const_iterator i(text.begin()); i != text.end(); ++i) { |
| 391 | if (IsUnicodeWhitespace(*i)) { |
| 392 | if (!in_whitespace) { |
| 393 | // Reduce all whitespace sequences to a single space. |
| 394 | in_whitespace = true; |
| 395 | result[chars_written++] = L' '; |
| 396 | } |
| 397 | if (trim_sequences_with_line_breaks && !already_trimmed && |
| 398 | ((*i == '\n') || (*i == '\r'))) { |
| 399 | // Whitespace sequences containing CR or LF are eliminated entirely. |
| 400 | already_trimmed = true; |
| 401 | --chars_written; |
| 402 | } |
| 403 | } else { |
| 404 | // Non-whitespace chracters are copied straight across. |
| 405 | in_whitespace = false; |
| 406 | already_trimmed = false; |
| 407 | result[chars_written++] = *i; |
| 408 | } |
| 409 | } |
| 410 |
| 411 | if (in_whitespace && !already_trimmed) { |
| 412 | // Any trailing whitespace is eliminated. |
| 413 | --chars_written; |
| 414 | } |
| 415 |
| 416 | result.resize(chars_written); |
| 417 | return result; |
| 418 | } |
| 419 |
| 420 | string16 CollapseWhitespace(const string16& text, |
| 421 | bool trim_sequences_with_line_breaks) { |
| 422 | return CollapseWhitespaceT(text, trim_sequences_with_line_breaks); |
| 423 | } |
| 424 |
| 425 | std::string CollapseWhitespaceASCII(const std::string& text, |
| 426 | bool trim_sequences_with_line_breaks) { |
| 427 | return CollapseWhitespaceT(text, trim_sequences_with_line_breaks); |
| 428 | } |
| 429 |
| 430 | bool ContainsOnlyChars(StringPiece input, StringPiece characters) { |
| 431 | return input.find_first_not_of(characters) == StringPiece::npos; |
| 432 | } |
| 433 |
| 434 | bool ContainsOnlyChars(StringPiece16 input, StringPiece16 characters) { |
| 435 | return input.find_first_not_of(characters) == StringPiece16::npos; |
| 436 | } |
| 437 |
| 438 | template <class Char> |
| 439 | inline bool DoIsStringASCII(const Char* characters, size_t length) { |
| 440 | if (!length) |
| 441 | return true; |
| 442 | constexpr MachineWord non_ascii_bit_mask = NonASCIIMask<Char>::value(); |
| 443 | MachineWord all_char_bits = 0; |
| 444 | const Char* end = characters + length; |
| 445 |
| 446 | // Prologue: align the input. |
| 447 | while (!IsMachineWordAligned(characters) && characters < end) |
| 448 | all_char_bits |= *characters++; |
| 449 | if (all_char_bits & non_ascii_bit_mask) |
| 450 | return false; |
| 451 |
| 452 | // Compare the values of CPU word size. |
| 453 | constexpr size_t chars_per_word = sizeof(MachineWord) / sizeof(Char); |
| 454 | constexpr int batch_count = 16; |
| 455 | while (characters <= end - batch_count * chars_per_word) { |
| 456 | all_char_bits = 0; |
| 457 | for (int i = 0; i < batch_count; ++i) { |
| 458 | all_char_bits |= *(reinterpret_cast<const MachineWord*>(characters)); |
| 459 | characters += chars_per_word; |
| 460 | } |
| 461 | if (all_char_bits & non_ascii_bit_mask) |
| 462 | return false; |
| 463 | } |
| 464 |
| 465 | // Process the remaining words. |
| 466 | all_char_bits = 0; |
| 467 | while (characters <= end - chars_per_word) { |
| 468 | all_char_bits |= *(reinterpret_cast<const MachineWord*>(characters)); |
| 469 | characters += chars_per_word; |
| 470 | } |
| 471 |
| 472 | // Process the remaining bytes. |
| 473 | while (characters < end) |
| 474 | all_char_bits |= *characters++; |
| 475 |
| 476 | return !(all_char_bits & non_ascii_bit_mask); |
| 477 | } |
| 478 |
| 479 | bool IsStringASCII(StringPiece str) { |
| 480 | return DoIsStringASCII(str.data(), str.length()); |
| 481 | } |
| 482 |
| 483 | bool IsStringASCII(StringPiece16 str) { |
| 484 | return DoIsStringASCII(str.data(), str.length()); |
| 485 | } |
| 486 |
| 487 | #if defined(WCHAR_T_IS_UTF32) |
| 488 | bool IsStringASCII(WStringPiece str) { |
| 489 | return DoIsStringASCII(str.data(), str.length()); |
| 490 | } |
| 491 | #endif |
| 492 |
| 493 | template <bool (*Validator)(uint32_t)> |
| 494 | inline static bool DoIsStringUTF8(StringPiece str) { |
| 495 | const char* src = str.data(); |
| 496 | int32_t src_len = static_cast<int32_t>(str.length()); |
| 497 | int32_t char_index = 0; |
| 498 |
| 499 | while (char_index < src_len) { |
| 500 | int32_t code_point; |
| 501 | CBU8_NEXT(src, char_index, src_len, code_point); |
| 502 | if (!Validator(code_point)) |
| 503 | return false; |
| 504 | } |
| 505 | return true; |
| 506 | } |
| 507 |
| 508 | bool IsStringUTF8(StringPiece str) { |
| 509 | return DoIsStringUTF8<IsValidCharacter>(str); |
| 510 | } |
| 511 |
| 512 | bool IsStringUTF8AllowingNoncharacters(StringPiece str) { |
| 513 | return DoIsStringUTF8<IsValidCodepoint>(str); |
| 514 | } |
| 515 |
| 516 | // Implementation note: Normally this function will be called with a hardcoded |
| 517 | // constant for the lowercase_ascii parameter. Constructing a StringPiece from |
| 518 | // a C constant requires running strlen, so the result will be two passes |
| 519 | // through the buffers, one to file the length of lowercase_ascii, and one to |
| 520 | // compare each letter. |
| 521 | // |
| 522 | // This function could have taken a const char* to avoid this and only do one |
| 523 | // pass through the string. But the strlen is faster than the case-insensitive |
| 524 | // compares and lets us early-exit in the case that the strings are different |
| 525 | // lengths (will often be the case for non-matches). So whether one approach or |
| 526 | // the other will be faster depends on the case. |
| 527 | // |
| 528 | // The hardcoded strings are typically very short so it doesn't matter, and the |
| 529 | // string piece gives additional flexibility for the caller (doesn't have to be |
| 530 | // null terminated) so we choose the StringPiece route. |
| 531 | template<typename Str> |
| 532 | static inline bool DoLowerCaseEqualsASCII(BasicStringPiece<Str> str, |
| 533 | StringPiece lowercase_ascii) { |
| 534 | if (str.size() != lowercase_ascii.size()) |
| 535 | return false; |
| 536 | for (size_t i = 0; i < str.size(); i++) { |
| 537 | if (ToLowerASCII(str[i]) != lowercase_ascii[i]) |
| 538 | return false; |
| 539 | } |
| 540 | return true; |
| 541 | } |
| 542 |
| 543 | bool LowerCaseEqualsASCII(StringPiece str, StringPiece lowercase_ascii) { |
| 544 | return DoLowerCaseEqualsASCII<std::string>(str, lowercase_ascii); |
| 545 | } |
| 546 |
| 547 | bool LowerCaseEqualsASCII(StringPiece16 str, StringPiece lowercase_ascii) { |
| 548 | return DoLowerCaseEqualsASCII<string16>(str, lowercase_ascii); |
| 549 | } |
| 550 |
| 551 | bool EqualsASCII(StringPiece16 str, StringPiece ascii) { |
| 552 | if (str.length() != ascii.length()) |
| 553 | return false; |
| 554 | return std::equal(ascii.begin(), ascii.end(), str.begin()); |
| 555 | } |
| 556 |
| 557 | template<typename Str> |
| 558 | bool StartsWithT(BasicStringPiece<Str> str, |
| 559 | BasicStringPiece<Str> search_for, |
| 560 | CompareCase case_sensitivity) { |
| 561 | if (search_for.size() > str.size()) |
| 562 | return false; |
| 563 |
| 564 | BasicStringPiece<Str> source = str.substr(0, search_for.size()); |
| 565 |
| 566 | switch (case_sensitivity) { |
| 567 | case CompareCase::SENSITIVE: |
| 568 | return source == search_for; |
| 569 |
| 570 | case CompareCase::INSENSITIVE_ASCII: |
| 571 | return std::equal( |
| 572 | search_for.begin(), search_for.end(), |
| 573 | source.begin(), |
| 574 | CaseInsensitiveCompareASCII<typename Str::value_type>()); |
| 575 |
| 576 | default: |
| 577 | NOTREACHED(); |
| 578 | return false; |
| 579 | } |
| 580 | } |
| 581 |
| 582 | bool StartsWith(StringPiece str, |
| 583 | StringPiece search_for, |
| 584 | CompareCase case_sensitivity) { |
| 585 | return StartsWithT<std::string>(str, search_for, case_sensitivity); |
| 586 | } |
| 587 |
| 588 | bool StartsWith(StringPiece16 str, |
| 589 | StringPiece16 search_for, |
| 590 | CompareCase case_sensitivity) { |
| 591 | return StartsWithT<string16>(str, search_for, case_sensitivity); |
| 592 | } |
| 593 |
| 594 | template <typename Str> |
| 595 | bool EndsWithT(BasicStringPiece<Str> str, |
| 596 | BasicStringPiece<Str> search_for, |
| 597 | CompareCase case_sensitivity) { |
| 598 | if (search_for.size() > str.size()) |
| 599 | return false; |
| 600 |
| 601 | BasicStringPiece<Str> source = str.substr(str.size() - search_for.size(), |
| 602 | search_for.size()); |
| 603 |
| 604 | switch (case_sensitivity) { |
| 605 | case CompareCase::SENSITIVE: |
| 606 | return source == search_for; |
| 607 |
| 608 | case CompareCase::INSENSITIVE_ASCII: |
| 609 | return std::equal( |
| 610 | source.begin(), source.end(), |
| 611 | search_for.begin(), |
| 612 | CaseInsensitiveCompareASCII<typename Str::value_type>()); |
| 613 |
| 614 | default: |
| 615 | NOTREACHED(); |
| 616 | return false; |
| 617 | } |
| 618 | } |
| 619 |
| 620 | bool EndsWith(StringPiece str, |
| 621 | StringPiece search_for, |
| 622 | CompareCase case_sensitivity) { |
| 623 | return EndsWithT<std::string>(str, search_for, case_sensitivity); |
| 624 | } |
| 625 |
| 626 | bool EndsWith(StringPiece16 str, |
| 627 | StringPiece16 search_for, |
| 628 | CompareCase case_sensitivity) { |
| 629 | return EndsWithT<string16>(str, search_for, case_sensitivity); |
| 630 | } |
| 631 |
| 632 | char HexDigitToInt(wchar_t c) { |
| 633 | DCHECK(IsHexDigit(c)); |
| 634 | if (c >= '0' && c <= '9') |
| 635 | return static_cast<char>(c - '0'); |
| 636 | if (c >= 'A' && c <= 'F') |
| 637 | return static_cast<char>(c - 'A' + 10); |
| 638 | if (c >= 'a' && c <= 'f') |
| 639 | return static_cast<char>(c - 'a' + 10); |
| 640 | return 0; |
| 641 | } |
| 642 |
| 643 | bool IsUnicodeWhitespace(wchar_t c) { |
| 644 | // kWhitespaceWide is a NULL-terminated string |
| 645 | for (const wchar_t* cur = kWhitespaceWide; *cur; ++cur) { |
| 646 | if (*cur == c) |
| 647 | return true; |
| 648 | } |
| 649 | return false; |
| 650 | } |
| 651 |
| 652 | static const char* const kByteStringsUnlocalized[] = { |
| 653 | " B", |
| 654 | " kB", |
| 655 | " MB", |
| 656 | " GB", |
| 657 | " TB", |
| 658 | " PB" |
| 659 | }; |
| 660 |
| 661 | string16 FormatBytesUnlocalized(int64_t bytes) { |
| 662 | double unit_amount = static_cast<double>(bytes); |
| 663 | size_t dimension = 0; |
| 664 | const int kKilo = 1024; |
| 665 | while (unit_amount >= kKilo && |
| 666 | dimension < base::size(kByteStringsUnlocalized) - 1) { |
| 667 | unit_amount /= kKilo; |
| 668 | dimension++; |
| 669 | } |
| 670 |
| 671 | char buf[64]; |
| 672 | if (bytes != 0 && dimension > 0 && unit_amount < 100) { |
| 673 | base::snprintf(buf, base::size(buf), "%.1lf%s", unit_amount, |
| 674 | kByteStringsUnlocalized[dimension]); |
| 675 | } else { |
| 676 | base::snprintf(buf, base::size(buf), "%.0lf%s", unit_amount, |
| 677 | kByteStringsUnlocalized[dimension]); |
| 678 | } |
| 679 |
| 680 | return ASCIIToUTF16(buf); |
| 681 | } |
| 682 |
| 683 | // A Matcher for DoReplaceMatchesAfterOffset() that matches substrings. |
| 684 | template <class StringType> |
| 685 | struct SubstringMatcher { |
| 686 | BasicStringPiece<StringType> find_this; |
| 687 |
| 688 | size_t Find(const StringType& input, size_t pos) { |
| 689 | return input.find(find_this.data(), pos, find_this.length()); |
| 690 | } |
| 691 | size_t MatchSize() { return find_this.length(); } |
| 692 | }; |
| 693 |
| 694 | // A Matcher for DoReplaceMatchesAfterOffset() that matches single characters. |
| 695 | template <class StringType> |
| 696 | struct CharacterMatcher { |
| 697 | BasicStringPiece<StringType> find_any_of_these; |
| 698 |
| 699 | size_t Find(const StringType& input, size_t pos) { |
| 700 | return input.find_first_of(find_any_of_these.data(), pos, |
| 701 | find_any_of_these.length()); |
| 702 | } |
| 703 | constexpr size_t MatchSize() { return 1; } |
| 704 | }; |
| 705 |
| 706 | enum class ReplaceType { REPLACE_ALL, REPLACE_FIRST }; |
| 707 |
| 708 | // Runs in O(n) time in the length of |str|, and transforms the string without |
| 709 | // reallocating when possible. Returns |true| if any matches were found. |
| 710 | // |
| 711 | // This is parameterized on a |Matcher| traits type, so that it can be the |
| 712 | // implementation for both ReplaceChars() and ReplaceSubstringsAfterOffset(). |
| 713 | template <class StringType, class Matcher> |
| 714 | bool DoReplaceMatchesAfterOffset(StringType* str, |
| 715 | size_t initial_offset, |
| 716 | Matcher matcher, |
| 717 | BasicStringPiece<StringType> replace_with, |
| 718 | ReplaceType replace_type) { |
| 719 | using CharTraits = typename StringType::traits_type; |
| 720 |
| 721 | const size_t find_length = matcher.MatchSize(); |
| 722 | if (!find_length) |
| 723 | return false; |
| 724 |
| 725 | // If the find string doesn't appear, there's nothing to do. |
| 726 | size_t first_match = matcher.Find(*str, initial_offset); |
| 727 | if (first_match == StringType::npos) |
| 728 | return false; |
| 729 |
| 730 | // If we're only replacing one instance, there's no need to do anything |
| 731 | // complicated. |
| 732 | const size_t replace_length = replace_with.length(); |
| 733 | if (replace_type == ReplaceType::REPLACE_FIRST) { |
| 734 | str->replace(first_match, find_length, replace_with.data(), replace_length); |
| 735 | return true; |
| 736 | } |
| 737 |
| 738 | // If the find and replace strings are the same length, we can simply use |
| 739 | // replace() on each instance, and finish the entire operation in O(n) time. |
| 740 | if (find_length == replace_length) { |
| 741 | auto* buffer = &((*str)[0]); |
| 742 | for (size_t offset = first_match; offset != StringType::npos; |
| 743 | offset = matcher.Find(*str, offset + replace_length)) { |
| 744 | CharTraits::copy(buffer + offset, replace_with.data(), replace_length); |
| 745 | } |
| 746 | return true; |
| 747 | } |
| 748 |
| 749 | // Since the find and replace strings aren't the same length, a loop like the |
| 750 | // one above would be O(n^2) in the worst case, as replace() will shift the |
| 751 | // entire remaining string each time. We need to be more clever to keep things |
| 752 | // O(n). |
| 753 | // |
| 754 | // When the string is being shortened, it's possible to just shift the matches |
| 755 | // down in one pass while finding, and truncate the length at the end of the |
| 756 | // search. |
| 757 | // |
| 758 | // If the string is being lengthened, more work is required. The strategy used |
| 759 | // here is to make two find() passes through the string. The first pass counts |
| 760 | // the number of matches to determine the new size. The second pass will |
| 761 | // either construct the new string into a new buffer (if the existing buffer |
| 762 | // lacked capacity), or else -- if there is room -- create a region of scratch |
| 763 | // space after |first_match| by shifting the tail of the string to a higher |
| 764 | // index, and doing in-place moves from the tail to lower indices thereafter. |
| 765 | size_t str_length = str->length(); |
| 766 | size_t expansion = 0; |
| 767 | if (replace_length > find_length) { |
| 768 | // This operation lengthens the string; determine the new length by counting |
| 769 | // matches. |
| 770 | const size_t expansion_per_match = (replace_length - find_length); |
| 771 | size_t num_matches = 0; |
| 772 | for (size_t match = first_match; match != StringType::npos; |
| 773 | match = matcher.Find(*str, match + find_length)) { |
| 774 | expansion += expansion_per_match; |
| 775 | ++num_matches; |
| 776 | } |
| 777 | const size_t final_length = str_length + expansion; |
| 778 |
| 779 | if (str->capacity() < final_length) { |
| 780 | // If we'd have to allocate a new buffer to grow the string, build the |
| 781 | // result directly into the new allocation via append(). |
| 782 | StringType src(str->get_allocator()); |
| 783 | str->swap(src); |
| 784 | str->reserve(final_length); |
| 785 |
| 786 | size_t pos = 0; |
| 787 | for (size_t match = first_match;; match = matcher.Find(src, pos)) { |
| 788 | str->append(src, pos, match - pos); |
| 789 | str->append(replace_with.data(), replace_length); |
| 790 | pos = match + find_length; |
| 791 |
| 792 | // A mid-loop test/break enables skipping the final Find() call; the |
| 793 | // number of matches is known, so don't search past the last one. |
| 794 | if (!--num_matches) |
| 795 | break; |
| 796 | } |
| 797 |
| 798 | // Handle substring after the final match. |
| 799 | str->append(src, pos, str_length - pos); |
| 800 | return true; |
| 801 | } |
| 802 |
| 803 | // Prepare for the copy/move loop below -- expand the string to its final |
| 804 | // size by shifting the data after the first match to the end of the resized |
| 805 | // string. |
| 806 | size_t shift_src = first_match + find_length; |
| 807 | size_t shift_dst = shift_src + expansion; |
| 808 |
| 809 | // Big |expansion| factors (relative to |str_length|) require padding up to |
| 810 | // |shift_dst|. |
| 811 | if (shift_dst > str_length) |
| 812 | str->resize(shift_dst); |
| 813 |
| 814 | str->replace(shift_dst, str_length - shift_src, *str, shift_src, |
| 815 | str_length - shift_src); |
| 816 | str_length = final_length; |
| 817 | } |
| 818 |
| 819 | // We can alternate replacement and move operations. This won't overwrite the |
| 820 | // unsearched region of the string so long as |write_offset| <= |read_offset|; |
| 821 | // that condition is always satisfied because: |
| 822 | // |
| 823 | // (a) If the string is being shortened, |expansion| is zero and |
| 824 | // |write_offset| grows slower than |read_offset|. |
| 825 | // |
| 826 | // (b) If the string is being lengthened, |write_offset| grows faster than |
| 827 | // |read_offset|, but |expansion| is big enough so that |write_offset| |
| 828 | // will only catch up to |read_offset| at the point of the last match. |
| 829 | auto* buffer = &((*str)[0]); |
| 830 | size_t write_offset = first_match; |
| 831 | size_t read_offset = first_match + expansion; |
| 832 | do { |
| 833 | if (replace_length) { |
| 834 | CharTraits::copy(buffer + write_offset, replace_with.data(), |
| 835 | replace_length); |
| 836 | write_offset += replace_length; |
| 837 | } |
| 838 | read_offset += find_length; |
| 839 |
| 840 | // min() clamps StringType::npos (the largest unsigned value) to str_length. |
| 841 | size_t match = std::min(matcher.Find(*str, read_offset), str_length); |
| 842 |
| 843 | size_t length = match - read_offset; |
| 844 | if (length) { |
| 845 | CharTraits::move(buffer + write_offset, buffer + read_offset, length); |
| 846 | write_offset += length; |
| 847 | read_offset += length; |
| 848 | } |
| 849 | } while (read_offset < str_length); |
| 850 |
| 851 | // If we're shortening the string, truncate it now. |
| 852 | str->resize(write_offset); |
| 853 | return true; |
| 854 | } |
| 855 |
| 856 | template <class StringType> |
| 857 | bool ReplaceCharsT(const StringType& input, |
| 858 | BasicStringPiece<StringType> find_any_of_these, |
| 859 | BasicStringPiece<StringType> replace_with, |
| 860 | StringType* output) { |
| 861 | // Commonly, this is called with output and input being the same string; in |
| 862 | // that case, this assignment is inexpensive. |
| 863 | *output = input; |
| 864 |
| 865 | return DoReplaceMatchesAfterOffset( |
| 866 | output, 0, CharacterMatcher<StringType>{find_any_of_these}, replace_with, |
| 867 | ReplaceType::REPLACE_ALL); |
| 868 | } |
| 869 |
| 870 | void ReplaceFirstSubstringAfterOffset(string16* str, |
| 871 | size_t start_offset, |
| 872 | StringPiece16 find_this, |
| 873 | StringPiece16 replace_with) { |
| 874 | DoReplaceMatchesAfterOffset(str, start_offset, |
| 875 | SubstringMatcher<string16>{find_this}, |
| 876 | replace_with, ReplaceType::REPLACE_FIRST); |
| 877 | } |
| 878 |
| 879 | void ReplaceFirstSubstringAfterOffset(std::string* str, |
| 880 | size_t start_offset, |
| 881 | StringPiece find_this, |
| 882 | StringPiece replace_with) { |
| 883 | DoReplaceMatchesAfterOffset(str, start_offset, |
| 884 | SubstringMatcher<std::string>{find_this}, |
| 885 | replace_with, ReplaceType::REPLACE_FIRST); |
| 886 | } |
| 887 |
| 888 | void ReplaceSubstringsAfterOffset(string16* str, |
| 889 | size_t start_offset, |
| 890 | StringPiece16 find_this, |
| 891 | StringPiece16 replace_with) { |
| 892 | DoReplaceMatchesAfterOffset(str, start_offset, |
| 893 | SubstringMatcher<string16>{find_this}, |
| 894 | replace_with, ReplaceType::REPLACE_ALL); |
| 895 | } |
| 896 |
| 897 | void ReplaceSubstringsAfterOffset(std::string* str, |
| 898 | size_t start_offset, |
| 899 | StringPiece find_this, |
| 900 | StringPiece replace_with) { |
| 901 | DoReplaceMatchesAfterOffset(str, start_offset, |
| 902 | SubstringMatcher<std::string>{find_this}, |
| 903 | replace_with, ReplaceType::REPLACE_ALL); |
| 904 | } |
| 905 |
| 906 | template <class string_type> |
| 907 | inline typename string_type::value_type* WriteIntoT(string_type* str, |
| 908 | size_t length_with_null) { |
| 909 | DCHECK_GE(length_with_null, 1u); |
| 910 | str->reserve(length_with_null); |
| 911 | str->resize(length_with_null - 1); |
| 912 | return &((*str)[0]); |
| 913 | } |
| 914 |
| 915 | char* WriteInto(std::string* str, size_t length_with_null) { |
| 916 | return WriteIntoT(str, length_with_null); |
| 917 | } |
| 918 |
| 919 | char16* WriteInto(string16* str, size_t length_with_null) { |
| 920 | return WriteIntoT(str, length_with_null); |
| 921 | } |
| 922 |
| 923 | // Generic version for all JoinString overloads. |list_type| must be a sequence |
| 924 | // (std::vector or std::initializer_list) of strings/StringPieces (std::string, |
| 925 | // string16, StringPiece or StringPiece16). |string_type| is either std::string |
| 926 | // or string16. |
| 927 | template <typename list_type, typename string_type> |
| 928 | static string_type JoinStringT(const list_type& parts, |
| 929 | BasicStringPiece<string_type> sep) { |
| 930 | if (base::empty(parts)) |
| 931 | return string_type(); |
| 932 |
| 933 | // Pre-allocate the eventual size of the string. Start with the size of all of |
| 934 | // the separators (note that this *assumes* parts.size() > 0). |
| 935 | size_t total_size = (parts.size() - 1) * sep.size(); |
| 936 | for (const auto& part : parts) |
| 937 | total_size += part.size(); |
| 938 | string_type result; |
| 939 | result.reserve(total_size); |
| 940 |
| 941 | auto iter = parts.begin(); |
| 942 | DCHECK(iter != parts.end()); |
| 943 | result.append(iter->data(), iter->size()); |
| 944 | ++iter; |
| 945 |
| 946 | for (; iter != parts.end(); ++iter) { |
| 947 | result.append(sep.data(), sep.size()); |
| 948 | result.append(iter->data(), iter->size()); |
| 949 | } |
| 950 |
| 951 | // Sanity-check that we pre-allocated correctly. |
| 952 | DCHECK_EQ(total_size, result.size()); |
| 953 |
| 954 | return result; |
| 955 | } |
| 956 |
| 957 | std::string JoinString(const std::vector<std::string>& parts, |
| 958 | StringPiece separator) { |
| 959 | return JoinStringT(parts, separator); |
| 960 | } |
| 961 |
| 962 | string16 JoinString(const std::vector<string16>& parts, |
| 963 | StringPiece16 separator) { |
| 964 | return JoinStringT(parts, separator); |
| 965 | } |
| 966 |
| 967 | std::string JoinString(const std::vector<StringPiece>& parts, |
| 968 | StringPiece separator) { |
| 969 | return JoinStringT(parts, separator); |
| 970 | } |
| 971 |
| 972 | string16 JoinString(const std::vector<StringPiece16>& parts, |
| 973 | StringPiece16 separator) { |
| 974 | return JoinStringT(parts, separator); |
| 975 | } |
| 976 |
| 977 | std::string JoinString(std::initializer_list<StringPiece> parts, |
| 978 | StringPiece separator) { |
| 979 | return JoinStringT(parts, separator); |
| 980 | } |
| 981 |
| 982 | string16 JoinString(std::initializer_list<StringPiece16> parts, |
| 983 | StringPiece16 separator) { |
| 984 | return JoinStringT(parts, separator); |
| 985 | } |
| 986 |
| 987 | template<class FormatStringType, class OutStringType> |
| 988 | OutStringType DoReplaceStringPlaceholders( |
| 989 | const FormatStringType& format_string, |
| 990 | const std::vector<OutStringType>& subst, |
| 991 | std::vector<size_t>* offsets) { |
| 992 | size_t substitutions = subst.size(); |
| 993 | DCHECK_LT(substitutions, 10U); |
| 994 |
| 995 | size_t sub_length = 0; |
| 996 | for (const auto& cur : subst) |
| 997 | sub_length += cur.length(); |
| 998 |
| 999 | OutStringType formatted; |
| 1000 | formatted.reserve(format_string.length() + sub_length); |
| 1001 |
| 1002 | std::vector<ReplacementOffset> r_offsets; |
| 1003 | for (auto i = format_string.begin(); i != format_string.end(); ++i) { |
| 1004 | if ('$' == *i) { |
| 1005 | if (i + 1 != format_string.end()) { |
| 1006 | ++i; |
| 1007 | if ('$' == *i) { |
| 1008 | while (i != format_string.end() && '$' == *i) { |
| 1009 | formatted.push_back('$'); |
| 1010 | ++i; |
| 1011 | } |
| 1012 | --i; |
| 1013 | } else { |
| 1014 | if (*i < '1' || *i > '9') { |
| 1015 | DLOG(ERROR) << "Invalid placeholder: $" << *i; |
| 1016 | continue; |
| 1017 | } |
| 1018 | uintptr_t index = *i - '1'; |
| 1019 | if (offsets) { |
| 1020 | ReplacementOffset r_offset(index, |
| 1021 | static_cast<int>(formatted.size())); |
| 1022 | r_offsets.insert( |
| 1023 | std::upper_bound(r_offsets.begin(), r_offsets.end(), r_offset, |
| 1024 | &CompareParameter), |
| 1025 | r_offset); |
| 1026 | } |
| 1027 | if (index < substitutions) |
| 1028 | formatted.append(subst.at(index)); |
| 1029 | } |
| 1030 | } |
| 1031 | } else { |
| 1032 | formatted.push_back(*i); |
| 1033 | } |
| 1034 | } |
| 1035 | if (offsets) { |
| 1036 | for (const auto& cur : r_offsets) |
| 1037 | offsets->push_back(cur.offset); |
| 1038 | } |
| 1039 | return formatted; |
| 1040 | } |
| 1041 |
| 1042 | string16 ReplaceStringPlaceholders(const string16& format_string, |
| 1043 | const std::vector<string16>& subst, |
| 1044 | std::vector<size_t>* offsets) { |
| 1045 | return DoReplaceStringPlaceholders(format_string, subst, offsets); |
| 1046 | } |
| 1047 |
| 1048 | std::string ReplaceStringPlaceholders(StringPiece format_string, |
| 1049 | const std::vector<std::string>& subst, |
| 1050 | std::vector<size_t>* offsets) { |
| 1051 | return DoReplaceStringPlaceholders(format_string, subst, offsets); |
| 1052 | } |
| 1053 |
| 1054 | string16 ReplaceStringPlaceholders(const string16& format_string, |
| 1055 | const string16& a, |
| 1056 | size_t* offset) { |
| 1057 | std::vector<size_t> offsets; |
| 1058 | std::vector<string16> subst; |
| 1059 | subst.push_back(a); |
| 1060 | string16 result = ReplaceStringPlaceholders(format_string, subst, &offsets); |
| 1061 |
| 1062 | DCHECK_EQ(1U, offsets.size()); |
| 1063 | if (offset) |
| 1064 | *offset = offsets[0]; |
| 1065 | return result; |
| 1066 | } |
| 1067 |
| 1068 | #if defined(OS_WIN) && defined(BASE_STRING16_IS_STD_U16STRING) |
| 1069 |
| 1070 | TrimPositions TrimWhitespace(WStringPiece input, |
| 1071 | TrimPositions positions, |
| 1072 | std::wstring* output) { |
| 1073 | return TrimStringT(input, WStringPiece(kWhitespaceWide), positions, output); |
| 1074 | } |
| 1075 |
| 1076 | WStringPiece TrimWhitespace(WStringPiece input, TrimPositions positions) { |
| 1077 | return TrimStringPieceT(input, WStringPiece(kWhitespaceWide), positions); |
| 1078 | } |
| 1079 |
| 1080 | bool TrimString(WStringPiece input, |
| 1081 | WStringPiece trim_chars, |
| 1082 | std::wstring* output) { |
| 1083 | return TrimStringT(input, trim_chars, TRIM_ALL, output) != TRIM_NONE; |
| 1084 | } |
| 1085 |
| 1086 | WStringPiece TrimString(WStringPiece input, |
| 1087 | WStringPiece trim_chars, |
| 1088 | TrimPositions positions) { |
| 1089 | return TrimStringPieceT(input, trim_chars, positions); |
| 1090 | } |
| 1091 |
| 1092 | wchar_t* WriteInto(std::wstring* str, size_t length_with_null) { |
| 1093 | return WriteIntoT(str, length_with_null); |
| 1094 | } |
| 1095 |
| 1096 | #endif |
| 1097 |
| 1098 | // The following code is compatible with the OpenBSD lcpy interface. See: |
| 1099 | // http://www.gratisoft.us/todd/papers/strlcpy.html |
| 1100 | // ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/{wcs,str}lcpy.c |
| 1101 |
| 1102 | namespace { |
| 1103 |
| 1104 | template <typename CHAR> |
| 1105 | size_t lcpyT(CHAR* dst, const CHAR* src, size_t dst_size) { |
| 1106 | for (size_t i = 0; i < dst_size; ++i) { |
| 1107 | if ((dst[i] = src[i]) == 0) // We hit and copied the terminating NULL. |
| 1108 | return i; |
| 1109 | } |
| 1110 |
| 1111 | // We were left off at dst_size. We over copied 1 byte. Null terminate. |
| 1112 | if (dst_size != 0) |
| 1113 | dst[dst_size - 1] = 0; |
| 1114 |
| 1115 | // Count the rest of the |src|, and return it's length in characters. |
| 1116 | while (src[dst_size]) ++dst_size; |
| 1117 | return dst_size; |
| 1118 | } |
| 1119 |
| 1120 | } // namespace |
| 1121 |
| 1122 | size_t strlcpy(char* dst, const char* src, size_t dst_size) { |
| 1123 | return lcpyT<char>(dst, src, dst_size); |
| 1124 | } |
| 1125 | size_t wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) { |
| 1126 | return lcpyT<wchar_t>(dst, src, dst_size); |
| 1127 | } |
| 1128 |
| 1129 | } // namespace base |
| 1130 |