summaryrefslogtreecommitdiffstats
path: root/base/strings
diff options
context:
space:
mode:
authorbrettw <brettw@chromium.org>2015-06-24 13:54:45 -0700
committerCommit bot <commit-bot@chromium.org>2015-06-24 20:55:38 +0000
commite6dae46fc681a8ad62f6937a6ac1cab3d51938a9 (patch)
tree2f75f6b3af2a17c8b1512b6881b00a8c9595ac96 /base/strings
parent2ef675dbcd5943aad03184b67696330cc910568d (diff)
downloadchromium_src-e6dae46fc681a8ad62f6937a6ac1cab3d51938a9.zip
chromium_src-e6dae46fc681a8ad62f6937a6ac1cab3d51938a9.tar.gz
chromium_src-e6dae46fc681a8ad62f6937a6ac1cab3d51938a9.tar.bz2
Add more string_util functions to base namespace.
Moves ReplaceFirstSubstringAfterOffset. It also changes the find/replace arguments to be string pieces (normally these are constants). TBR=gauravsh@chromium.org for chromeos/network Review URL: https://codereview.chromium.org/1200393002 Cr-Commit-Position: refs/heads/master@{#335999}
Diffstat (limited to 'base/strings')
-rw-r--r--base/strings/string_util.cc66
-rw-r--r--base/strings/string_util.h41
2 files changed, 58 insertions, 49 deletions
diff --git a/base/strings/string_util.cc b/base/strings/string_util.cc
index 5839cf2..e3dcd85 100644
--- a/base/strings/string_util.cc
+++ b/base/strings/string_util.cc
@@ -659,19 +659,17 @@ string16 FormatBytesUnlocalized(int64 bytes) {
return ASCIIToUTF16(buf);
}
-} // namespace base
-
// Runs in O(n) time in the length of |str|.
template<class StringType>
void DoReplaceSubstringsAfterOffset(StringType* str,
size_t offset,
- const StringType& find_this,
- const StringType& replace_with,
+ BasicStringPiece<StringType> find_this,
+ BasicStringPiece<StringType> replace_with,
bool replace_all) {
DCHECK(!find_this.empty());
// If the find string doesn't appear, there's nothing to do.
- offset = str->find(find_this, offset);
+ offset = str->find(find_this.data(), offset, find_this.size());
if (offset == StringType::npos)
return;
@@ -679,7 +677,7 @@ void DoReplaceSubstringsAfterOffset(StringType* str,
// complicated.
size_t find_length = find_this.length();
if (!replace_all) {
- str->replace(offset, find_length, replace_with);
+ str->replace(offset, find_length, replace_with.data(), replace_with.size());
return;
}
@@ -688,8 +686,10 @@ void DoReplaceSubstringsAfterOffset(StringType* str,
size_t replace_length = replace_with.length();
if (find_length == replace_length) {
do {
- str->replace(offset, find_length, replace_with);
- offset = str->find(find_this, offset + replace_length);
+ str->replace(offset, find_length,
+ replace_with.data(), replace_with.size());
+ offset = str->find(find_this.data(), offset + replace_length,
+ find_this.size());
} while (offset != StringType::npos);
return;
}
@@ -706,11 +706,14 @@ void DoReplaceSubstringsAfterOffset(StringType* str,
size_t write_offset = offset;
do {
if (replace_length) {
- str->replace(write_offset, replace_length, replace_with);
+ str->replace(write_offset, replace_length,
+ replace_with.data(), replace_with.size());
write_offset += replace_length;
}
size_t read_offset = offset + find_length;
- offset = std::min(str->find(find_this, read_offset), str_length);
+ offset = std::min(
+ str->find(find_this.data(), read_offset, find_this.size()),
+ str_length);
size_t length = offset - read_offset;
if (length) {
memmove(&(*str)[write_offset], &(*str)[read_offset],
@@ -739,13 +742,15 @@ void DoReplaceSubstringsAfterOffset(StringType* str,
// exit from the loop, |current_match| will point at the last instance of
// the find string, and we won't need to find() it again immediately.
current_match = offset;
- offset = str->find(find_this, offset + find_length);
+ offset = str->find(find_this.data(), offset + find_length,
+ find_this.size());
} while (offset != StringType::npos);
str->resize(final_length);
// Now do the replacement loop, working backwards through the string.
for (size_t prev_match = str_length, write_offset = final_length; ;
- current_match = str->rfind(find_this, current_match - 1)) {
+ current_match = str->rfind(find_this.data(), current_match - 1,
+ find_this.size())) {
size_t read_offset = current_match + find_length;
size_t length = prev_match - read_offset;
if (length) {
@@ -754,7 +759,8 @@ void DoReplaceSubstringsAfterOffset(StringType* str,
length * sizeof(typename StringType::value_type));
}
write_offset -= replace_length;
- str->replace(write_offset, replace_length, replace_with);
+ str->replace(write_offset, replace_length,
+ replace_with.data(), replace_with.size());
if (current_match == first_match)
return;
prev_match = current_match;
@@ -763,36 +769,38 @@ void DoReplaceSubstringsAfterOffset(StringType* str,
void ReplaceFirstSubstringAfterOffset(string16* str,
size_t start_offset,
- const string16& find_this,
- const string16& replace_with) {
- DoReplaceSubstringsAfterOffset(str, start_offset, find_this, replace_with,
- false); // replace first instance
+ StringPiece16 find_this,
+ StringPiece16 replace_with) {
+ DoReplaceSubstringsAfterOffset<string16>(
+ str, start_offset, find_this, replace_with, false); // Replace first.
}
void ReplaceFirstSubstringAfterOffset(std::string* str,
size_t start_offset,
- const std::string& find_this,
- const std::string& replace_with) {
- DoReplaceSubstringsAfterOffset(str, start_offset, find_this, replace_with,
- false); // replace first instance
+ StringPiece find_this,
+ StringPiece replace_with) {
+ DoReplaceSubstringsAfterOffset<std::string>(
+ str, start_offset, find_this, replace_with, false); // Replace first.
}
void ReplaceSubstringsAfterOffset(string16* str,
size_t start_offset,
- const string16& find_this,
- const string16& replace_with) {
- DoReplaceSubstringsAfterOffset(str, start_offset, find_this, replace_with,
- true); // replace all instances
+ StringPiece16 find_this,
+ StringPiece16 replace_with) {
+ DoReplaceSubstringsAfterOffset<string16>(
+ str, start_offset, find_this, replace_with, true); // Replace all.
}
void ReplaceSubstringsAfterOffset(std::string* str,
size_t start_offset,
- const std::string& find_this,
- const std::string& replace_with) {
- DoReplaceSubstringsAfterOffset(str, start_offset, find_this, replace_with,
- true); // replace all instances
+ StringPiece find_this,
+ StringPiece replace_with) {
+ DoReplaceSubstringsAfterOffset<std::string>(
+ str, start_offset, find_this, replace_with, true); // Replace all.
}
+} // namespace base
+
size_t Tokenize(const base::string16& str,
const base::string16& delimiters,
std::vector<base::string16>* tokens) {
diff --git a/base/strings/string_util.h b/base/strings/string_util.h
index 8b80868..dc3d4d7 100644
--- a/base/strings/string_util.h
+++ b/base/strings/string_util.h
@@ -417,28 +417,18 @@ inline bool IsUnicodeWhitespace(wchar_t c) {
// FormatBytes instead; remove this.
BASE_EXPORT string16 FormatBytesUnlocalized(int64 bytes);
-} // namespace base
-
-#if defined(OS_WIN)
-#include "base/strings/string_util_win.h"
-#elif defined(OS_POSIX)
-#include "base/strings/string_util_posix.h"
-#else
-#error Define string operations appropriately for your platform
-#endif
-
// Starting at |start_offset| (usually 0), replace the first instance of
// |find_this| with |replace_with|.
BASE_EXPORT void ReplaceFirstSubstringAfterOffset(
base::string16* str,
size_t start_offset,
- const base::string16& find_this,
- const base::string16& replace_with);
+ StringPiece16 find_this,
+ StringPiece16 replace_with);
BASE_EXPORT void ReplaceFirstSubstringAfterOffset(
std::string* str,
size_t start_offset,
- const std::string& find_this,
- const std::string& replace_with);
+ StringPiece find_this,
+ StringPiece replace_with);
// Starting at |start_offset| (usually 0), look through |str| and replace all
// instances of |find_this| with |replace_with|.
@@ -449,12 +439,23 @@ BASE_EXPORT void ReplaceFirstSubstringAfterOffset(
BASE_EXPORT void ReplaceSubstringsAfterOffset(
base::string16* str,
size_t start_offset,
- const base::string16& find_this,
- const base::string16& replace_with);
-BASE_EXPORT void ReplaceSubstringsAfterOffset(std::string* str,
- size_t start_offset,
- const std::string& find_this,
- const std::string& replace_with);
+ StringPiece16 find_this,
+ StringPiece16 replace_with);
+BASE_EXPORT void ReplaceSubstringsAfterOffset(
+ std::string* str,
+ size_t start_offset,
+ StringPiece find_this,
+ StringPiece replace_with);
+
+} // namespace base
+
+#if defined(OS_WIN)
+#include "base/strings/string_util_win.h"
+#elif defined(OS_POSIX)
+#include "base/strings/string_util_posix.h"
+#else
+#error Define string operations appropriately for your platform
+#endif
// Reserves enough memory in |str| to accommodate |length_with_null| characters,
// sets the size of |str| to |length_with_null - 1| characters, and returns a