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

Side by Side Diff: base/string_util.cc

Issue 18603: Add a replace_all param to ReplaceSubstringsAfterOffset and update call sites. (Closed)
Patch Set: Add ReplaceFirstSubstringAfterPos() Created 11 years, 11 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
« no previous file with comments | « base/string_util.h ('k') | base/string_util_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 761 matching lines...) Expand 10 before | Expand all | Expand 10 after
772 } 772 }
773 773
774 std::wstring FormatSpeed(int64 bytes, DataUnits units, bool show_units) { 774 std::wstring FormatSpeed(int64 bytes, DataUnits units, bool show_units) {
775 return FormatBytesInternal(bytes, units, show_units, kSpeedStrings); 775 return FormatBytesInternal(bytes, units, show_units, kSpeedStrings);
776 } 776 }
777 777
778 template<class StringType> 778 template<class StringType>
779 void DoReplaceSubstringsAfterOffset(StringType* str, 779 void DoReplaceSubstringsAfterOffset(StringType* str,
780 typename StringType::size_type start_offset, 780 typename StringType::size_type start_offset,
781 const StringType& find_this, 781 const StringType& find_this,
782 const StringType& replace_with) { 782 const StringType& replace_with,
783 bool replace_all) {
783 if ((start_offset == StringType::npos) || (start_offset >= str->length())) 784 if ((start_offset == StringType::npos) || (start_offset >= str->length()))
784 return; 785 return;
785 786
786 DCHECK(!find_this.empty()); 787 DCHECK(!find_this.empty());
787 for (typename StringType::size_type offs(str->find(find_this, start_offset)); 788 for (typename StringType::size_type offs(str->find(find_this, start_offset));
788 offs != StringType::npos; offs = str->find(find_this, offs)) { 789 offs != StringType::npos; offs = str->find(find_this, offs)) {
789 str->replace(offs, find_this.length(), replace_with); 790 str->replace(offs, find_this.length(), replace_with);
790 offs += replace_with.length(); 791 offs += replace_with.length();
792
793 if (!replace_all)
794 break;
791 } 795 }
792 } 796 }
793 797
798 void ReplaceFirstSubstringAfterOffset(std::wstring* str,
799 std::wstring::size_type start_offset,
800 const std::wstring& find_this,
801 const std::wstring& replace_with) {
802 DoReplaceSubstringsAfterOffset(str, start_offset, find_this, replace_with,
803 false); // replace first instance
804 }
805
806 void ReplaceFirstSubstringAfterOffset(std::string* str,
807 std::string::size_type start_offset,
808 const std::string& find_this,
809 const std::string& replace_with) {
810 DoReplaceSubstringsAfterOffset(str, start_offset, find_this, replace_with,
811 false); // replace first instance
812 }
813
794 void ReplaceSubstringsAfterOffset(std::wstring* str, 814 void ReplaceSubstringsAfterOffset(std::wstring* str,
795 std::wstring::size_type start_offset, 815 std::wstring::size_type start_offset,
796 const std::wstring& find_this, 816 const std::wstring& find_this,
797 const std::wstring& replace_with) { 817 const std::wstring& replace_with) {
798 DoReplaceSubstringsAfterOffset(str, start_offset, find_this, replace_with); 818 DoReplaceSubstringsAfterOffset(str, start_offset, find_this, replace_with,
819 true); // replace all instances
799 } 820 }
800 821
801 void ReplaceSubstringsAfterOffset(std::string* str, 822 void ReplaceSubstringsAfterOffset(std::string* str,
802 std::string::size_type start_offset, 823 std::string::size_type start_offset,
803 const std::string& find_this, 824 const std::string& find_this,
804 const std::string& replace_with) { 825 const std::string& replace_with) {
805 DoReplaceSubstringsAfterOffset(str, start_offset, find_this, replace_with); 826 DoReplaceSubstringsAfterOffset(str, start_offset, find_this, replace_with,
827 true); // replace all instances
806 } 828 }
807 829
808 // Overloaded wrappers around vsnprintf and vswprintf. The buf_size parameter 830 // Overloaded wrappers around vsnprintf and vswprintf. The buf_size parameter
809 // is the size of the buffer. These return the number of characters in the 831 // is the size of the buffer. These return the number of characters in the
810 // formatted string excluding the NUL terminator. If the buffer is not 832 // formatted string excluding the NUL terminator. If the buffer is not
811 // large enough to accommodate the formatted string without truncation, they 833 // large enough to accommodate the formatted string without truncation, they
812 // return the number of characters that would be in the fully-formatted string 834 // return the number of characters that would be in the fully-formatted string
813 // (vsnprintf, and vswprintf on Windows), or -1 (vswprintf on POSIX platforms). 835 // (vsnprintf, and vswprintf on Windows), or -1 (vswprintf on POSIX platforms).
814 inline int vsnprintfT(char* buffer, 836 inline int vsnprintfT(char* buffer,
815 size_t buf_size, 837 size_t buf_size,
(...skipping 687 matching lines...) Expand 10 before | Expand all | Expand 10 after
1503 int rstr_len = (max_len - 3) / 2; 1525 int rstr_len = (max_len - 3) / 2;
1504 int lstr_len = rstr_len + ((max_len - 3) % 2); 1526 int lstr_len = rstr_len + ((max_len - 3) % 2);
1505 output->assign(input.substr(0, lstr_len) + L"..." + 1527 output->assign(input.substr(0, lstr_len) + L"..." +
1506 input.substr(input.length() - rstr_len)); 1528 input.substr(input.length() - rstr_len));
1507 break; 1529 break;
1508 } 1530 }
1509 } 1531 }
1510 1532
1511 return true; 1533 return true;
1512 } 1534 }
OLDNEW
« no previous file with comments | « 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