summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorjcampan@chromium.org <jcampan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-02-10 18:26:11 +0000
committerjcampan@chromium.org <jcampan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-02-10 18:26:11 +0000
commitef122393f32e86fb5312d373136d99de62274bf8 (patch)
tree645665e7b050dc4810032d7d13ff03a8c45fb53c /base
parenta6c898d9732a133867229811c43e88274898d93f (diff)
downloadchromium_src-ef122393f32e86fb5312d373136d99de62274bf8.zip
chromium_src-ef122393f32e86fb5312d373136d99de62274bf8.tar.gz
chromium_src-ef122393f32e86fb5312d373136d99de62274bf8.tar.bz2
This CL addresses 2 issues:
- we were not computing the max request text size correctly in some cases (by forgetting the size of the &q= string) causing assertions. - the translate server might send inconsistent response in some cases, this CL makes sure we deal with them. A common case is when sending separators only strings, which we are not doing now anymore. Other more complicated case are less clear. BUG=35055 TEST=Visit www.spiegel.de and translate the page. The page should be translated correctly. Review URL: http://codereview.chromium.org/594023 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@38637 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/string_util.cc26
-rw-r--r--base/string_util.h7
-rw-r--r--base/string_util_unittest.cc13
3 files changed, 46 insertions, 0 deletions
diff --git a/base/string_util.cc b/base/string_util.cc
index a66dae6..c9b0aad 100644
--- a/base/string_util.cc
+++ b/base/string_util.cc
@@ -584,6 +584,32 @@ bool ContainsOnlyWhitespace(const string16& str) {
return true;
}
+template<typename STR>
+static bool ContainsOnlyCharsT(const STR& input, const STR& characters) {
+ for (typename STR::const_iterator iter = input.begin();
+ iter != input.end(); ++iter) {
+ if (characters.find(*iter) == STR::npos)
+ return false;
+ }
+ return true;
+}
+
+bool ContainsOnlyChars(const std::wstring& input,
+ const std::wstring& characters) {
+ return ContainsOnlyCharsT(input, characters);
+}
+
+#if !defined(WCHAR_T_IS_UTF16)
+bool ContainsOnlyChars(const string16& input, const string16& characters) {
+ return ContainsOnlyCharsT(input, characters);
+}
+#endif
+
+bool ContainsOnlyChars(const std::string& input,
+ const std::string& characters) {
+ return ContainsOnlyCharsT(input, characters);
+}
+
std::string WideToASCII(const std::wstring& wide) {
DCHECK(IsStringASCII(wide)) << wide;
return std::string(wide.begin(), wide.end());
diff --git a/base/string_util.h b/base/string_util.h
index 60f4f25..9e0da1e 100644
--- a/base/string_util.h
+++ b/base/string_util.h
@@ -217,6 +217,13 @@ std::string CollapseWhitespaceASCII(const std::string& text,
bool ContainsOnlyWhitespaceASCII(const std::string& str);
bool ContainsOnlyWhitespace(const string16& str);
+// Returns true if |input| is empty or contains only characters found in
+// |characters|.
+bool ContainsOnlyChars(const std::wstring& input,
+ const std::wstring& characters);
+bool ContainsOnlyChars(const string16& input, const string16& characters);
+bool ContainsOnlyChars(const std::string& input, const std::string& characters);
+
// These convert between ASCII (7-bit) and Wide/UTF16 strings.
std::string WideToASCII(const std::wstring& wide);
std::wstring ASCIIToWide(const base::StringPiece& ascii);
diff --git a/base/string_util_unittest.cc b/base/string_util_unittest.cc
index 4473cc5..c6961fe 100644
--- a/base/string_util_unittest.cc
+++ b/base/string_util_unittest.cc
@@ -1484,4 +1484,17 @@ TEST(StringUtilTest, RemoveChars) {
EXPECT_EQ(std::string(), input);
}
+TEST(StringUtilTest, ContainsOnlyChars) {
+ // Providing an empty list of characters should return false but for the empty
+ // string.
+ EXPECT_TRUE(ContainsOnlyChars("", ""));
+ EXPECT_FALSE(ContainsOnlyChars("Hello", ""));
+
+ EXPECT_TRUE(ContainsOnlyChars("", "1234"));
+ EXPECT_TRUE(ContainsOnlyChars("1", "1234"));
+ EXPECT_TRUE(ContainsOnlyChars("1", "4321"));
+ EXPECT_TRUE(ContainsOnlyChars("123", "4321"));
+ EXPECT_FALSE(ContainsOnlyChars("123a", "4321"));
+}
+
} // namaspace base