summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorjcampan@chromium.org <jcampan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-01-14 19:36:32 +0000
committerjcampan@chromium.org <jcampan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-01-14 19:36:32 +0000
commit0bedb8a46053e44f667da0e800568c38e2150e4a (patch)
treecd14724a4078a65970c56e97cbd7c5a904c2f14c /base
parent6fa508a1cef4a920f570174c77eafc5f21d808eb (diff)
downloadchromium_src-0bedb8a46053e44f667da0e800568c38e2150e4a.zip
chromium_src-0bedb8a46053e44f667da0e800568c38e2150e4a.tar.gz
chromium_src-0bedb8a46053e44f667da0e800568c38e2150e4a.tar.bz2
This CL contains the back-end implementation of the translate feature. It adds a Translate method to the renderer.
On invocation this method triggers a traversal of the DOM page to retrieve the text nodes. The text node contents are then sent to the browser for actual translation (at this point, we just up-case the text for testing purpose). The browser sends back the translated text to the renderer that replace the DOM text node values with the translated text. BUG=None TEST=Run the unit-tests. Review URL: http://codereview.chromium.org/547013 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@36258 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/string_util.cc16
-rw-r--r--base/string_util.h12
-rw-r--r--base/string_util_posix.h8
-rw-r--r--base/string_util_unittest.cc18
-rw-r--r--base/string_util_win.h4
5 files changed, 56 insertions, 2 deletions
diff --git a/base/string_util.cc b/base/string_util.cc
index 3a5cef3..bf69b0c 100644
--- a/base/string_util.cc
+++ b/base/string_util.cc
@@ -529,6 +529,22 @@ std::string CollapseWhitespaceASCII(const std::string& text,
return CollapseWhitespaceT(text, trim_sequences_with_line_breaks);
}
+bool ContainsOnlyWhitespaceASCII(const std::string& str) {
+ for (std::string::const_iterator i(str.begin()); i != str.end(); ++i) {
+ if (!IsAsciiWhitespace(*i))
+ return false;
+ }
+ return true;
+}
+
+bool ContainsOnlyWhitespace(const string16& str) {
+ for (string16::const_iterator i(str.begin()); i != str.end(); ++i) {
+ if (!IsWhitespace(*i))
+ return false;
+ }
+ return true;
+}
+
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 dd0f8c1..c895f27 100644
--- a/base/string_util.h
+++ b/base/string_util.h
@@ -30,16 +30,19 @@ namespace base {
// are listed below. These functions are then implemented as inline calls
// to the platform-specific equivalents in the platform-specific headers.
-// Compare the two strings s1 and s2 without regard to case using
+// Compares the two strings s1 and s2 without regard to case using
// the current locale; returns 0 if they are equal, 1 if s1 > s2, and -1 if
// s2 > s1 according to a lexicographic comparison.
int strcasecmp(const char* s1, const char* s2);
-// Compare up to count characters of s1 and s2 without regard to case using
+// Compares up to count characters of s1 and s2 without regard to case using
// the current locale; returns 0 if they are equal, 1 if s1 > s2, and -1 if
// s2 > s1 according to a lexicographic comparison.
int strncasecmp(const char* s1, const char* s2, size_t count);
+// Same as strncmp but for char16 strings.
+int strncmp16(const char16* s1, const char16* s2, size_t count);
+
// Wrapper for vsnprintf that always null-terminates and always returns the
// number of characters that would be in an untruncated formatted
// string, even when truncation occurs.
@@ -196,6 +199,11 @@ string16 CollapseWhitespace(const string16& text,
std::string CollapseWhitespaceASCII(const std::string& text,
bool trim_sequences_with_line_breaks);
+// Returns true if the passed string is empty or contains only white-space
+// characters.
+bool ContainsOnlyWhitespaceASCII(const std::string& str);
+bool ContainsOnlyWhitespace(const string16& str);
+
// 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_posix.h b/base/string_util_posix.h
index b503685a..75cf4b4 100644
--- a/base/string_util_posix.h
+++ b/base/string_util_posix.h
@@ -33,6 +33,14 @@ inline int vsnprintf(char* buffer, size_t size,
return ::vsnprintf(buffer, size, format, arguments);
}
+inline int strncmp16(const char16* s1, const char16* s2, size_t count) {
+#if defined(WCHAR_T_IS_UTF16)
+ return ::wcsncmp(s1, s2, count);
+#elif defined(WCHAR_T_IS_UTF32)
+ return c16memcmp(s1, s2, count);
+#endif
+}
+
inline int vswprintf(wchar_t* buffer, size_t size,
const wchar_t* format, va_list arguments) {
DCHECK(IsWprintfFormatPortable(format));
diff --git a/base/string_util_unittest.cc b/base/string_util_unittest.cc
index 2c1f484..9d848a4 100644
--- a/base/string_util_unittest.cc
+++ b/base/string_util_unittest.cc
@@ -166,6 +166,24 @@ TEST(StringUtilTest, CollapseWhitespaceASCII) {
}
}
+TEST(StringUtilTest, ContainsOnlyWhitespaceASCII) {
+ EXPECT_TRUE(ContainsOnlyWhitespaceASCII(""));
+ EXPECT_TRUE(ContainsOnlyWhitespaceASCII(" "));
+ EXPECT_TRUE(ContainsOnlyWhitespaceASCII("\t"));
+ EXPECT_TRUE(ContainsOnlyWhitespaceASCII("\t \r \n "));
+ EXPECT_FALSE(ContainsOnlyWhitespaceASCII("a"));
+ EXPECT_FALSE(ContainsOnlyWhitespaceASCII("\thello\r \n "));
+}
+
+TEST(StringUtilTest, ContainsOnlyWhitespace) {
+ EXPECT_TRUE(ContainsOnlyWhitespace(ASCIIToUTF16("")));
+ EXPECT_TRUE(ContainsOnlyWhitespace(ASCIIToUTF16(" ")));
+ EXPECT_TRUE(ContainsOnlyWhitespace(ASCIIToUTF16("\t")));
+ EXPECT_TRUE(ContainsOnlyWhitespace(ASCIIToUTF16("\t \r \n ")));
+ EXPECT_FALSE(ContainsOnlyWhitespace(ASCIIToUTF16("a")));
+ EXPECT_FALSE(ContainsOnlyWhitespace(ASCIIToUTF16("\thello\r \n ")));
+}
+
TEST(StringUtilTest, IsStringUTF8) {
EXPECT_TRUE(IsStringUTF8("abc"));
EXPECT_TRUE(IsStringUTF8("\xc2\x81"));
diff --git a/base/string_util_win.h b/base/string_util_win.h
index 0367ec4..124238c 100644
--- a/base/string_util_win.h
+++ b/base/string_util_win.h
@@ -28,6 +28,10 @@ inline int strncasecmp(const char* s1, const char* s2, size_t count) {
return _strnicmp(s1, s2, count);
}
+inline int strncmp16(const char16* s1, const char16* s2, size_t count) {
+ return ::wcsncmp(s1, s2, count);
+}
+
inline int vsnprintf(char* buffer, size_t size,
const char* format, va_list arguments) {
int length = vsnprintf_s(buffer, size, size - 1, format, arguments);