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

Side by Side Diff: base/string_util.cc

Issue 3107021: Convert FormatBytes to string16. (Closed) Base URL: http://src.chromium.org/git/chromium.git
Patch Set: Created 10 years, 4 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
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 634 matching lines...) Expand 10 before | Expand all | Expand 10 after
645 if (bytes >= kUnitThresholds[unit_index]) 645 if (bytes >= kUnitThresholds[unit_index])
646 break; 646 break;
647 } 647 }
648 648
649 DCHECK(unit_index >= DATA_UNITS_BYTE && unit_index <= DATA_UNITS_GIBIBYTE); 649 DCHECK(unit_index >= DATA_UNITS_BYTE && unit_index <= DATA_UNITS_GIBIBYTE);
650 return DataUnits(unit_index); 650 return DataUnits(unit_index);
651 } 651 }
652 652
653 // TODO(mpcomplete): deal with locale 653 // TODO(mpcomplete): deal with locale
654 // Byte suffixes. This must match the DataUnits enum. 654 // Byte suffixes. This must match the DataUnits enum.
655 static const wchar_t* const kByteStrings[] = { 655 static const char* const kByteStrings[] = {
656 L"B", 656 "B",
657 L"kB", 657 "kB",
658 L"MB", 658 "MB",
659 L"GB" 659 "GB"
660 }; 660 };
661 661
662 static const wchar_t* const kSpeedStrings[] = { 662 static const char* const kSpeedStrings[] = {
663 L"B/s", 663 "B/s",
664 L"kB/s", 664 "kB/s",
665 L"MB/s", 665 "MB/s",
666 L"GB/s" 666 "GB/s"
667 }; 667 };
668 668
669 std::wstring FormatBytesInternal(int64 bytes, 669 string16 FormatBytesInternal(int64 bytes,
670 DataUnits units, 670 DataUnits units,
671 bool show_units, 671 bool show_units,
672 const wchar_t* const* suffix) { 672 const char* const* suffix) {
673 if (bytes < 0) { 673 if (bytes < 0) {
674 NOTREACHED() << "Negative bytes value"; 674 NOTREACHED() << "Negative bytes value";
675 return std::wstring(); 675 return string16();
676 } 676 }
677 677
678 DCHECK(units >= DATA_UNITS_BYTE && units <= DATA_UNITS_GIBIBYTE); 678 DCHECK(units >= DATA_UNITS_BYTE && units <= DATA_UNITS_GIBIBYTE);
679 679
680 // Put the quantity in the right units. 680 // Put the quantity in the right units.
681 double unit_amount = static_cast<double>(bytes); 681 double unit_amount = static_cast<double>(bytes);
682 for (int i = 0; i < units; ++i) 682 for (int i = 0; i < units; ++i)
683 unit_amount /= 1024.0; 683 unit_amount /= 1024.0;
684 684
685 wchar_t buf[64]; 685 char buf[64];
686 if (bytes != 0 && units != DATA_UNITS_BYTE && unit_amount < 100) 686 if (bytes != 0 && units != DATA_UNITS_BYTE && unit_amount < 100)
687 base::swprintf(buf, arraysize(buf), L"%.1lf", unit_amount); 687 base::snprintf(buf, arraysize(buf), "%.1lf", unit_amount);
688 else 688 else
689 base::swprintf(buf, arraysize(buf), L"%.0lf", unit_amount); 689 base::snprintf(buf, arraysize(buf), "%.0lf", unit_amount);
690 690
691 std::wstring ret(buf); 691 std::string ret(buf);
692 if (show_units) { 692 if (show_units) {
693 ret += L" "; 693 ret += " ";
694 ret += suffix[units]; 694 ret += suffix[units];
695 } 695 }
696 696
697 return ret; 697 return ASCIIToUTF16(ret);
698 } 698 }
699 699
700 std::wstring FormatBytes(int64 bytes, DataUnits units, bool show_units) { 700 string16 FormatBytes(int64 bytes, DataUnits units, bool show_units) {
701 return FormatBytesInternal(bytes, units, show_units, kByteStrings); 701 return FormatBytesInternal(bytes, units, show_units, kByteStrings);
702 } 702 }
703 703
704 std::wstring FormatSpeed(int64 bytes, DataUnits units, bool show_units) { 704 string16 FormatSpeed(int64 bytes, DataUnits units, bool show_units) {
705 return FormatBytesInternal(bytes, units, show_units, kSpeedStrings); 705 return FormatBytesInternal(bytes, units, show_units, kSpeedStrings);
706 } 706 }
707 707
708 template<class StringType> 708 template<class StringType>
709 void DoReplaceSubstringsAfterOffset(StringType* str, 709 void DoReplaceSubstringsAfterOffset(StringType* str,
710 typename StringType::size_type start_offset, 710 typename StringType::size_type start_offset,
711 const StringType& find_this, 711 const StringType& find_this,
712 const StringType& replace_with, 712 const StringType& replace_with,
713 bool replace_all) { 713 bool replace_all) {
714 if ((start_offset == StringType::npos) || (start_offset >= str->length())) 714 if ((start_offset == StringType::npos) || (start_offset >= str->length()))
(...skipping 526 matching lines...) Expand 10 before | Expand all | Expand 10 after
1241 int rstr_len = (max_len - 3) / 2; 1241 int rstr_len = (max_len - 3) / 2;
1242 int lstr_len = rstr_len + ((max_len - 3) % 2); 1242 int lstr_len = rstr_len + ((max_len - 3) % 2);
1243 output->assign(input.substr(0, lstr_len) + L"..." + 1243 output->assign(input.substr(0, lstr_len) + L"..." +
1244 input.substr(input.length() - rstr_len)); 1244 input.substr(input.length() - rstr_len));
1245 break; 1245 break;
1246 } 1246 }
1247 } 1247 }
1248 1248
1249 return true; 1249 return true;
1250 } 1250 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698