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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/string_util.h ('k') | base/string_util_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/string_util.cc
diff --git a/base/string_util.cc b/base/string_util.cc
index f029e95082a507348ca7b332bf20ab64f894c993..eae60d20f67f2e446a7181068476bc12086559e0 100644
--- a/base/string_util.cc
+++ b/base/string_util.cc
@@ -779,7 +779,8 @@ template<class StringType>
void DoReplaceSubstringsAfterOffset(StringType* str,
typename StringType::size_type start_offset,
const StringType& find_this,
- const StringType& replace_with) {
+ const StringType& replace_with,
+ bool replace_all) {
if ((start_offset == StringType::npos) || (start_offset >= str->length()))
return;
@@ -788,21 +789,42 @@ void DoReplaceSubstringsAfterOffset(StringType* str,
offs != StringType::npos; offs = str->find(find_this, offs)) {
str->replace(offs, find_this.length(), replace_with);
offs += replace_with.length();
+
+ if (!replace_all)
+ break;
}
}
+void ReplaceFirstSubstringAfterOffset(std::wstring* str,
+ std::wstring::size_type start_offset,
+ const std::wstring& find_this,
+ const std::wstring& replace_with) {
+ DoReplaceSubstringsAfterOffset(str, start_offset, find_this, replace_with,
+ false); // replace first instance
+}
+
+void ReplaceFirstSubstringAfterOffset(std::string* str,
+ std::string::size_type start_offset,
+ const std::string& find_this,
+ const std::string& replace_with) {
+ DoReplaceSubstringsAfterOffset(str, start_offset, find_this, replace_with,
+ false); // replace first instance
+}
+
void ReplaceSubstringsAfterOffset(std::wstring* str,
std::wstring::size_type start_offset,
const std::wstring& find_this,
const std::wstring& replace_with) {
- DoReplaceSubstringsAfterOffset(str, start_offset, find_this, replace_with);
+ DoReplaceSubstringsAfterOffset(str, start_offset, find_this, replace_with,
+ true); // replace all instances
}
void ReplaceSubstringsAfterOffset(std::string* str,
std::string::size_type start_offset,
const std::string& find_this,
const std::string& replace_with) {
- DoReplaceSubstringsAfterOffset(str, start_offset, find_this, replace_with);
+ DoReplaceSubstringsAfterOffset(str, start_offset, find_this, replace_with,
+ true); // replace all instances
}
// Overloaded wrappers around vsnprintf and vswprintf. The buf_size parameter
« 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