summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorasvitkine@chromium.org <asvitkine@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-11-10 15:08:41 +0000
committerasvitkine@chromium.org <asvitkine@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-11-10 15:08:41 +0000
commit531e034c4256147c7edd37f5404eaaeb8fe3b0cd (patch)
tree71ea8a6f2b5d71bc00a104b176a2917a8335fe13 /base
parent23efbcbf7bc712ea22ad731aaa852c1a4763a0ea (diff)
downloadchromium_src-531e034c4256147c7edd37f5404eaaeb8fe3b0cd.zip
chromium_src-531e034c4256147c7edd37f5404eaaeb8fe3b0cd.tar.gz
chromium_src-531e034c4256147c7edd37f5404eaaeb8fe3b0cd.tar.bz2
Fix AutocompleteMatch DCHECK() triggered by |RenderViewContextMenu::AppendSearchProvider()|.
Makes |AppendSearchProvider()| sanitize the text by replacing unwanted whitespace characters with spaces. Do this instead of calling |AutocompleteMatch::SanitizeString()|, since we want to preserve the whitespace between words separated by newlines here, rather than appending such words together. Added |ReplaceChars()| to base/string_util to support replacing any occurence of the given characters with a replacement string. BUG=103338 TEST=Right click on multiline text in a text area in a Debug build. A DCHECK() shouldn't trigger. Review URL: http://codereview.chromium.org/8502027 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@109430 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/string_util.cc31
-rw-r--r--base/string_util.h24
-rw-r--r--base/string_util_unittest.cc33
3 files changed, 75 insertions, 13 deletions
diff --git a/base/string_util.cc b/base/string_util.cc
index 360a25eb8..22e94aa 100644
--- a/base/string_util.cc
+++ b/base/string_util.cc
@@ -165,34 +165,49 @@ const char kWhitespaceASCII[] = {
const char kUtf8ByteOrderMark[] = "\xEF\xBB\xBF";
template<typename STR>
-bool RemoveCharsT(const STR& input,
- const typename STR::value_type remove_chars[],
- STR* output) {
+bool ReplaceCharsT(const STR& input,
+ const typename STR::value_type replace_chars[],
+ const STR& replace_with,
+ STR* output) {
bool removed = false;
size_t found;
*output = input;
- found = output->find_first_of(remove_chars);
+ found = output->find_first_of(replace_chars);
while (found != STR::npos) {
removed = true;
- output->replace(found, 1, STR());
- found = output->find_first_of(remove_chars, found);
+ output->replace(found, 1, replace_with);
+ found = output->find_first_of(replace_chars, found);
}
return removed;
}
+bool ReplaceChars(const string16& input,
+ const char16 replace_chars[],
+ const string16& replace_with,
+ string16* output) {
+ return ReplaceCharsT(input, replace_chars, replace_with, output);
+}
+
+bool ReplaceChars(const std::string& input,
+ const char replace_chars[],
+ const std::string& replace_with,
+ std::string* output) {
+ return ReplaceCharsT(input, replace_chars, replace_with, output);
+}
+
bool RemoveChars(const string16& input,
const char16 remove_chars[],
string16* output) {
- return RemoveCharsT(input, remove_chars, output);
+ return ReplaceChars(input, remove_chars, string16(), output);
}
bool RemoveChars(const std::string& input,
const char remove_chars[],
std::string* output) {
- return RemoveCharsT(input, remove_chars, output);
+ return ReplaceChars(input, remove_chars, std::string(), output);
}
template<typename STR>
diff --git a/base/string_util.h b/base/string_util.h
index 740124f..c359e73 100644
--- a/base/string_util.h
+++ b/base/string_util.h
@@ -173,9 +173,9 @@ BASE_EXPORT extern const char kWhitespaceASCII[];
BASE_EXPORT extern const char kUtf8ByteOrderMark[];
-// Removes characters in |remove_chars| from anywhere in input. Returns true if
-// any characters were removed. |remove_chars| must be null-terminated.
-// NOTE: Safe to use the same variable for both input and output.
+// Removes characters in |remove_chars| from anywhere in |input|. Returns true
+// if any characters were removed. |remove_chars| must be null-terminated.
+// NOTE: Safe to use the same variable for both |input| and |output|.
BASE_EXPORT bool RemoveChars(const string16& input,
const char16 remove_chars[],
string16* output);
@@ -183,9 +183,23 @@ BASE_EXPORT bool RemoveChars(const std::string& input,
const char remove_chars[],
std::string* output);
-// Removes characters in |trim_chars| from the beginning and end of input.
+// Replaces characters in |replace_chars| from anywhere in |input| with
+// |replace_with|. Each character in |replace_chars| will be replaced with
+// the |replace_with| string. Returns true if any characters were replaced.
+// |replace_chars| must be null-terminated.
+// NOTE: Safe to use the same variable for both |input| and |output|.
+BASE_EXPORT bool ReplaceChars(const string16& input,
+ const char16 replace_chars[],
+ const string16& replace_with,
+ string16* output);
+BASE_EXPORT bool ReplaceChars(const std::string& input,
+ const char replace_chars[],
+ const std::string& replace_with,
+ std::string* output);
+
+// Removes characters in |trim_chars| from the beginning and end of |input|.
// |trim_chars| must be null-terminated.
-// NOTE: Safe to use the same variable for both input and output.
+// NOTE: Safe to use the same variable for both |input| and |output|.
BASE_EXPORT bool TrimString(const std::wstring& input,
const wchar_t trim_chars[],
std::wstring* output);
diff --git a/base/string_util_unittest.cc b/base/string_util_unittest.cc
index f354a99..ecdb84f 100644
--- a/base/string_util_unittest.cc
+++ b/base/string_util_unittest.cc
@@ -1066,6 +1066,39 @@ TEST(StringUtilTest, RemoveChars) {
EXPECT_EQ(std::string(), input);
}
+TEST(StringUtilTest, ReplaceChars) {
+ struct TestData {
+ const char* input;
+ const char* replace_chars;
+ const char* replace_with;
+ const char* output;
+ bool result;
+ } cases[] = {
+ { "", "", "", "", false },
+ { "test", "", "", "test", false },
+ { "test", "", "!", "test", false },
+ { "test", "z", "!", "test", false },
+ { "test", "e", "!", "t!st", true },
+ { "test", "e", "!?", "t!?st", true },
+ { "test", "ez", "!", "t!st", true },
+ { "test", "zed", "!?", "t!?st", true },
+ { "test", "t", "!?", "!?es!?", true },
+ { "test", "et", "!>", "!>!>s!>", true },
+ { "test", "zest", "!", "!!!!", true },
+ { "test", "szt", "!", "!e!!", true },
+ };
+
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
+ std::string output;
+ bool result = ReplaceChars(cases[i].input,
+ cases[i].replace_chars,
+ cases[i].replace_with,
+ &output);
+ EXPECT_EQ(cases[i].result, result);
+ EXPECT_EQ(cases[i].output, output);
+ }
+}
+
TEST(StringUtilTest, ContainsOnlyChars) {
// Providing an empty list of characters should return false but for the empty
// string.