Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(330)

Side by Side Diff: base/string_util.cc

Issue 7189076: Localize strings, speeds. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: copyright dates Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/string_util.h" 5 #include "base/string_util.h"
6 6
7 #include "build/build_config.h" 7 #include "build/build_config.h"
8 8
9 #include <ctype.h> 9 #include <ctype.h>
10 #include <errno.h> 10 #include <errno.h>
(...skipping 586 matching lines...) Expand 10 before | Expand all | Expand 10 after
597 return EndsWithT(str, search, case_sensitive); 597 return EndsWithT(str, search, case_sensitive);
598 } 598 }
599 599
600 #if !defined(WCHAR_T_IS_UTF16) 600 #if !defined(WCHAR_T_IS_UTF16)
601 bool EndsWith(const string16& str, const string16& search, 601 bool EndsWith(const string16& str, const string16& search,
602 bool case_sensitive) { 602 bool case_sensitive) {
603 return EndsWithT(str, search, case_sensitive); 603 return EndsWithT(str, search, case_sensitive);
604 } 604 }
605 #endif 605 #endif
606 606
607 DataUnits GetByteDisplayUnits(int64 bytes) { 607 static const char* const kByteStringsUnlocalized[] = {
608 // The byte thresholds at which we display amounts. A byte count is displayed 608 " B",
609 // in unit U when kUnitThresholds[U] <= bytes < kUnitThresholds[U+1]. 609 " kB",
610 // This must match the DataUnits enum. 610 " MB",
611 static const int64 kUnitThresholds[] = { 611 " GB",
612 0, // DATA_UNITS_BYTE, 612 " TB",
613 3*1024, // DATA_UNITS_KIBIBYTE, 613 " PB"
614 2*1024*1024, // DATA_UNITS_MEBIBYTE, 614 };
615 1024*1024*1024 // DATA_UNITS_GIBIBYTE,
616 };
617 615
618 if (bytes < 0) { 616 string16 FormatBytesUnlocalized(int64 bytes) {
619 NOTREACHED() << "Negative bytes value"; 617 double unit_amount = static_cast<double>(bytes);
620 return DATA_UNITS_BYTE; 618 size_t dimension = 0;
619 const int kKilo = 1024;
620 while (unit_amount >= kKilo &&
621 dimension < arraysize(kByteStringsUnlocalized) - 1) {
622 unit_amount /= kKilo;
623 dimension++;
621 } 624 }
622 625
623 int unit_index = arraysize(kUnitThresholds); 626 char buf[64];
624 while (--unit_index > 0) { 627 if (bytes != 0 && dimension > 0 && unit_amount < 100) {
625 if (bytes >= kUnitThresholds[unit_index]) 628 base::snprintf(buf, arraysize(buf), "%.1lf%s", unit_amount,
626 break; 629 kByteStringsUnlocalized[dimension]);
630 } else {
631 base::snprintf(buf, arraysize(buf), "%.0lf%s", unit_amount,
632 kByteStringsUnlocalized[dimension]);
627 } 633 }
628 634
629 DCHECK(unit_index >= DATA_UNITS_BYTE && unit_index <= DATA_UNITS_GIBIBYTE); 635 return ASCIIToUTF16(buf);
630 return DataUnits(unit_index);
631 }
632
633 // TODO(mpcomplete): deal with locale
634 // Byte suffixes. This must match the DataUnits enum.
635 static const char* const kByteStrings[] = {
636 "B",
637 "kB",
638 "MB",
639 "GB"
640 };
641
642 static const char* const kSpeedStrings[] = {
643 "B/s",
644 "kB/s",
645 "MB/s",
646 "GB/s"
647 };
648
649 string16 FormatBytesInternal(int64 bytes,
650 DataUnits units,
651 bool show_units,
652 const char* const* suffix) {
653 if (bytes < 0) {
654 NOTREACHED() << "Negative bytes value";
655 return string16();
656 }
657
658 DCHECK(units >= DATA_UNITS_BYTE && units <= DATA_UNITS_GIBIBYTE);
659
660 // Put the quantity in the right units.
661 double unit_amount = static_cast<double>(bytes);
662 for (int i = 0; i < units; ++i)
663 unit_amount /= 1024.0;
664
665 char buf[64];
666 if (bytes != 0 && units != DATA_UNITS_BYTE && unit_amount < 100)
667 base::snprintf(buf, arraysize(buf), "%.1lf", unit_amount);
668 else
669 base::snprintf(buf, arraysize(buf), "%.0lf", unit_amount);
670
671 std::string ret(buf);
672 if (show_units) {
673 ret += " ";
674 ret += suffix[units];
675 }
676
677 return ASCIIToUTF16(ret);
678 }
679
680 string16 FormatBytes(int64 bytes, DataUnits units, bool show_units) {
681 return FormatBytesInternal(bytes, units, show_units, kByteStrings);
682 }
683
684 string16 FormatSpeed(int64 bytes, DataUnits units, bool show_units) {
685 return FormatBytesInternal(bytes, units, show_units, kSpeedStrings);
686 } 636 }
687 637
688 template<class StringType> 638 template<class StringType>
689 void DoReplaceSubstringsAfterOffset(StringType* str, 639 void DoReplaceSubstringsAfterOffset(StringType* str,
690 typename StringType::size_type start_offset, 640 typename StringType::size_type start_offset,
691 const StringType& find_this, 641 const StringType& find_this,
692 const StringType& replace_with, 642 const StringType& replace_with,
693 bool replace_all) { 643 bool replace_all) {
694 if ((start_offset == StringType::npos) || (start_offset >= str->length())) 644 if ((start_offset == StringType::npos) || (start_offset >= str->length()))
695 return; 645 return;
(...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after
1078 } 1028 }
1079 1029
1080 } // namespace 1030 } // namespace
1081 1031
1082 size_t base::strlcpy(char* dst, const char* src, size_t dst_size) { 1032 size_t base::strlcpy(char* dst, const char* src, size_t dst_size) {
1083 return lcpyT<char>(dst, src, dst_size); 1033 return lcpyT<char>(dst, src, dst_size);
1084 } 1034 }
1085 size_t base::wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) { 1035 size_t base::wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) {
1086 return lcpyT<wchar_t>(dst, src, dst_size); 1036 return lcpyT<wchar_t>(dst, src, dst_size);
1087 } 1037 }
OLDNEW
« base/string_util.h ('K') | « base/string_util.h ('k') | base/string_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698