| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/strings/string_util.h" | 5 #include "base/strings/string_util.h" |
| 6 | 6 |
| 7 #include <ctype.h> | 7 #include <ctype.h> |
| 8 #include <errno.h> | 8 #include <errno.h> |
| 9 #include <math.h> | 9 #include <math.h> |
| 10 #include <stdarg.h> | 10 #include <stdarg.h> |
| 11 #include <stdint.h> |
| 11 #include <stdio.h> | 12 #include <stdio.h> |
| 12 #include <stdlib.h> | 13 #include <stdlib.h> |
| 13 #include <string.h> | 14 #include <string.h> |
| 14 #include <time.h> | 15 #include <time.h> |
| 15 #include <wchar.h> | 16 #include <wchar.h> |
| 16 #include <wctype.h> | 17 #include <wctype.h> |
| 17 | 18 |
| 18 #include <algorithm> | 19 #include <algorithm> |
| 20 #include <limits> |
| 19 #include <vector> | 21 #include <vector> |
| 20 | 22 |
| 21 #include "base/basictypes.h" | |
| 22 #include "base/logging.h" | 23 #include "base/logging.h" |
| 23 #include "base/memory/singleton.h" | 24 #include "base/memory/singleton.h" |
| 24 #include "base/strings/string_split.h" | 25 #include "base/strings/string_split.h" |
| 25 #include "base/strings/utf_string_conversion_utils.h" | 26 #include "base/strings/utf_string_conversion_utils.h" |
| 26 #include "base/strings/utf_string_conversions.h" | 27 #include "base/strings/utf_string_conversions.h" |
| 27 #include "base/third_party/icu/icu_utf.h" | 28 #include "base/third_party/icu/icu_utf.h" |
| 28 #include "build/build_config.h" | 29 #include "build/build_config.h" |
| 29 | 30 |
| 30 namespace base { | 31 namespace base { |
| 31 | 32 |
| (...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 347 } | 348 } |
| 348 | 349 |
| 349 void TruncateUTF8ToByteSize(const std::string& input, | 350 void TruncateUTF8ToByteSize(const std::string& input, |
| 350 const size_t byte_size, | 351 const size_t byte_size, |
| 351 std::string* output) { | 352 std::string* output) { |
| 352 DCHECK(output); | 353 DCHECK(output); |
| 353 if (byte_size > input.length()) { | 354 if (byte_size > input.length()) { |
| 354 *output = input; | 355 *output = input; |
| 355 return; | 356 return; |
| 356 } | 357 } |
| 357 DCHECK_LE(byte_size, static_cast<uint32>(kint32max)); | 358 DCHECK_LE(byte_size, |
| 358 // Note: This cast is necessary because CBU8_NEXT uses int32s. | 359 static_cast<uint32_t>(std::numeric_limits<int32_t>::max())); |
| 359 int32 truncation_length = static_cast<int32>(byte_size); | 360 // Note: This cast is necessary because CBU8_NEXT uses int32_ts. |
| 360 int32 char_index = truncation_length - 1; | 361 int32_t truncation_length = static_cast<int32_t>(byte_size); |
| 362 int32_t char_index = truncation_length - 1; |
| 361 const char* data = input.data(); | 363 const char* data = input.data(); |
| 362 | 364 |
| 363 // Using CBU8, we will move backwards from the truncation point | 365 // Using CBU8, we will move backwards from the truncation point |
| 364 // to the beginning of the string looking for a valid UTF8 | 366 // to the beginning of the string looking for a valid UTF8 |
| 365 // character. Once a full UTF8 character is found, we will | 367 // character. Once a full UTF8 character is found, we will |
| 366 // truncate the string to the end of that character. | 368 // truncate the string to the end of that character. |
| 367 while (char_index >= 0) { | 369 while (char_index >= 0) { |
| 368 int32 prev = char_index; | 370 int32_t prev = char_index; |
| 369 base_icu::UChar32 code_point = 0; | 371 base_icu::UChar32 code_point = 0; |
| 370 CBU8_NEXT(data, char_index, truncation_length, code_point); | 372 CBU8_NEXT(data, char_index, truncation_length, code_point); |
| 371 if (!IsValidCharacter(code_point) || | 373 if (!IsValidCharacter(code_point) || |
| 372 !IsValidCodepoint(code_point)) { | 374 !IsValidCodepoint(code_point)) { |
| 373 char_index = prev - 1; | 375 char_index = prev - 1; |
| 374 } else { | 376 } else { |
| 375 break; | 377 break; |
| 376 } | 378 } |
| 377 } | 379 } |
| 378 | 380 |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 516 } | 518 } |
| 517 | 519 |
| 518 #if defined(WCHAR_T_IS_UTF32) | 520 #if defined(WCHAR_T_IS_UTF32) |
| 519 bool IsStringASCII(const std::wstring& str) { | 521 bool IsStringASCII(const std::wstring& str) { |
| 520 return DoIsStringASCII(str.data(), str.length()); | 522 return DoIsStringASCII(str.data(), str.length()); |
| 521 } | 523 } |
| 522 #endif | 524 #endif |
| 523 | 525 |
| 524 bool IsStringUTF8(const StringPiece& str) { | 526 bool IsStringUTF8(const StringPiece& str) { |
| 525 const char *src = str.data(); | 527 const char *src = str.data(); |
| 526 int32 src_len = static_cast<int32>(str.length()); | 528 int32_t src_len = static_cast<int32_t>(str.length()); |
| 527 int32 char_index = 0; | 529 int32_t char_index = 0; |
| 528 | 530 |
| 529 while (char_index < src_len) { | 531 while (char_index < src_len) { |
| 530 int32 code_point; | 532 int32_t code_point; |
| 531 CBU8_NEXT(src, char_index, src_len, code_point); | 533 CBU8_NEXT(src, char_index, src_len, code_point); |
| 532 if (!IsValidCharacter(code_point)) | 534 if (!IsValidCharacter(code_point)) |
| 533 return false; | 535 return false; |
| 534 } | 536 } |
| 535 return true; | 537 return true; |
| 536 } | 538 } |
| 537 | 539 |
| 538 // Implementation note: Normally this function will be called with a hardcoded | 540 // Implementation note: Normally this function will be called with a hardcoded |
| 539 // constant for the lowercase_ascii parameter. Constructing a StringPiece from | 541 // constant for the lowercase_ascii parameter. Constructing a StringPiece from |
| 540 // a C constant requires running strlen, so the result will be two passes | 542 // a C constant requires running strlen, so the result will be two passes |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 673 | 675 |
| 674 static const char* const kByteStringsUnlocalized[] = { | 676 static const char* const kByteStringsUnlocalized[] = { |
| 675 " B", | 677 " B", |
| 676 " kB", | 678 " kB", |
| 677 " MB", | 679 " MB", |
| 678 " GB", | 680 " GB", |
| 679 " TB", | 681 " TB", |
| 680 " PB" | 682 " PB" |
| 681 }; | 683 }; |
| 682 | 684 |
| 683 string16 FormatBytesUnlocalized(int64 bytes) { | 685 string16 FormatBytesUnlocalized(int64_t bytes) { |
| 684 double unit_amount = static_cast<double>(bytes); | 686 double unit_amount = static_cast<double>(bytes); |
| 685 size_t dimension = 0; | 687 size_t dimension = 0; |
| 686 const int kKilo = 1024; | 688 const int kKilo = 1024; |
| 687 while (unit_amount >= kKilo && | 689 while (unit_amount >= kKilo && |
| 688 dimension < arraysize(kByteStringsUnlocalized) - 1) { | 690 dimension < arraysize(kByteStringsUnlocalized) - 1) { |
| 689 unit_amount /= kKilo; | 691 unit_amount /= kKilo; |
| 690 dimension++; | 692 dimension++; |
| 691 } | 693 } |
| 692 | 694 |
| 693 char buf[64]; | 695 char buf[64]; |
| (...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 997 } // namespace | 999 } // namespace |
| 998 | 1000 |
| 999 size_t strlcpy(char* dst, const char* src, size_t dst_size) { | 1001 size_t strlcpy(char* dst, const char* src, size_t dst_size) { |
| 1000 return lcpyT<char>(dst, src, dst_size); | 1002 return lcpyT<char>(dst, src, dst_size); |
| 1001 } | 1003 } |
| 1002 size_t wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) { | 1004 size_t wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) { |
| 1003 return lcpyT<wchar_t>(dst, src, dst_size); | 1005 return lcpyT<wchar_t>(dst, src, dst_size); |
| 1004 } | 1006 } |
| 1005 | 1007 |
| 1006 } // namespace base | 1008 } // namespace base |
| OLD | NEW |