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 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
508 } | 510 } |
509 | 511 |
510 #if defined(WCHAR_T_IS_UTF32) | 512 #if defined(WCHAR_T_IS_UTF32) |
511 bool IsStringASCII(const std::wstring& str) { | 513 bool IsStringASCII(const std::wstring& str) { |
512 return DoIsStringASCII(str.data(), str.length()); | 514 return DoIsStringASCII(str.data(), str.length()); |
513 } | 515 } |
514 #endif | 516 #endif |
515 | 517 |
516 bool IsStringUTF8(const StringPiece& str) { | 518 bool IsStringUTF8(const StringPiece& str) { |
517 const char *src = str.data(); | 519 const char *src = str.data(); |
518 int32 src_len = static_cast<int32>(str.length()); | 520 int32_t src_len = static_cast<int32_t>(str.length()); |
519 int32 char_index = 0; | 521 int32_t char_index = 0; |
520 | 522 |
521 while (char_index < src_len) { | 523 while (char_index < src_len) { |
522 int32 code_point; | 524 int32_t code_point; |
523 CBU8_NEXT(src, char_index, src_len, code_point); | 525 CBU8_NEXT(src, char_index, src_len, code_point); |
524 if (!IsValidCharacter(code_point)) | 526 if (!IsValidCharacter(code_point)) |
525 return false; | 527 return false; |
526 } | 528 } |
527 return true; | 529 return true; |
528 } | 530 } |
529 | 531 |
530 // Implementation note: Normally this function will be called with a hardcoded | 532 // Implementation note: Normally this function will be called with a hardcoded |
531 // constant for the lowercase_ascii parameter. Constructing a StringPiece from | 533 // constant for the lowercase_ascii parameter. Constructing a StringPiece from |
532 // a C constant requires running strlen, so the result will be two passes | 534 // 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... |
665 | 667 |
666 static const char* const kByteStringsUnlocalized[] = { | 668 static const char* const kByteStringsUnlocalized[] = { |
667 " B", | 669 " B", |
668 " kB", | 670 " kB", |
669 " MB", | 671 " MB", |
670 " GB", | 672 " GB", |
671 " TB", | 673 " TB", |
672 " PB" | 674 " PB" |
673 }; | 675 }; |
674 | 676 |
675 string16 FormatBytesUnlocalized(int64 bytes) { | 677 string16 FormatBytesUnlocalized(int64_t bytes) { |
676 double unit_amount = static_cast<double>(bytes); | 678 double unit_amount = static_cast<double>(bytes); |
677 size_t dimension = 0; | 679 size_t dimension = 0; |
678 const int kKilo = 1024; | 680 const int kKilo = 1024; |
679 while (unit_amount >= kKilo && | 681 while (unit_amount >= kKilo && |
680 dimension < arraysize(kByteStringsUnlocalized) - 1) { | 682 dimension < arraysize(kByteStringsUnlocalized) - 1) { |
681 unit_amount /= kKilo; | 683 unit_amount /= kKilo; |
682 dimension++; | 684 dimension++; |
683 } | 685 } |
684 | 686 |
685 char buf[64]; | 687 char buf[64]; |
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
989 } // namespace | 991 } // namespace |
990 | 992 |
991 size_t strlcpy(char* dst, const char* src, size_t dst_size) { | 993 size_t strlcpy(char* dst, const char* src, size_t dst_size) { |
992 return lcpyT<char>(dst, src, dst_size); | 994 return lcpyT<char>(dst, src, dst_size); |
993 } | 995 } |
994 size_t wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) { | 996 size_t wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) { |
995 return lcpyT<wchar_t>(dst, src, dst_size); | 997 return lcpyT<wchar_t>(dst, src, dst_size); |
996 } | 998 } |
997 | 999 |
998 } // namespace base | 1000 } // namespace base |
OLD | NEW |