Reviewers: haraken, esprehn,
Description:
Add WTFString::toUTF8() method
Follow-up CL to:
[blink-dev] How to call Chromium's methods that take std::strings
Adds a toUTF8() method to WTFString and recursively a toStdString()
method to CString.
This is effectively a backport from
github.com/flutter + tests.
BUG=
Please review this at
https://codereview.chromium.org/1382583002/
Base URL:
https://chromium.googlesource.com/chromium/src.git@master
Affected files (+24, -0 lines):
M third_party/WebKit/Source/wtf/text/CString.h
M third_party/WebKit/Source/wtf/text/CStringTest.cpp
M third_party/WebKit/Source/wtf/text/WTFString.h
M third_party/WebKit/Source/wtf/text/WTFString.cpp
M third_party/WebKit/Source/wtf/text/WTFStringTest.cpp
Index: third_party/WebKit/Source/wtf/text/CString.h
diff --git a/third_party/WebKit/Source/wtf/text/CString.h
b/third_party/WebKit/Source/wtf/text/CString.h
index
08e51027e774748d32f6336d76d19f32af7a41e7..cec3fce820e75905da27600e8abedebf27d0e4b1
100644
--- a/third_party/WebKit/Source/wtf/text/CString.h
+++ b/third_party/WebKit/Source/wtf/text/CString.h
@@ -64,6 +64,11 @@ public:
CString(CStringBuffer* buffer) : m_buffer(buffer) { }
static CString newUninitialized(size_t length, char*& characterBuffer);
+ std::string toStdString() const
+ {
+ return std::string(data(), length());
+ }
+
const char* data() const
{
return m_buffer ? m_buffer->data() : 0;
Index: third_party/WebKit/Source/wtf/text/CStringTest.cpp
diff --git a/third_party/WebKit/Source/wtf/text/CStringTest.cpp
b/third_party/WebKit/Source/wtf/text/CStringTest.cpp
index
087cbb63634c008046a5437ad0d11cae33e1e7a8..a428bdb0707cdb87f60bdc52c5ed963f7e5e4c48
100644
--- a/third_party/WebKit/Source/wtf/text/CStringTest.cpp
+++ b/third_party/WebKit/Source/wtf/text/CStringTest.cpp
@@ -108,7 +108,9 @@ TEST(CStringTest, CopyOnWrite)
string.mutableData()[3] = 'K';
EXPECT_TRUE(string != copy);
EXPECT_STREQ("WebKit", string.data());
+ EXPECT_EQ("WebKit", string.toStdString());
EXPECT_STREQ(initialString, copy.data());
+ EXPECT_EQ(initialString, copy.toStdString());
}
TEST(CStringTest, Comparison)
Index: third_party/WebKit/Source/wtf/text/WTFString.cpp
diff --git a/third_party/WebKit/Source/wtf/text/WTFString.cpp
b/third_party/WebKit/Source/wtf/text/WTFString.cpp
index
6fee9d06de8ce8269a1a0d9ad21a820acf6fc3d9..7433d3f5497624fdd140dc7bc202706c144d7b42
100644
--- a/third_party/WebKit/Source/wtf/text/WTFString.cpp
+++ b/third_party/WebKit/Source/wtf/text/WTFString.cpp
@@ -836,6 +836,11 @@ CString String::utf8(UTF8ConversionMode mode) const
return CString(bufferVector.data(), buffer - bufferVector.data());
}
+std::string String::toUTF8() const
+{
+ return utf8().toStdString();
+}
+
String String::make8BitFrom16BitSource(const UChar* source, size_t length)
{
if (!length)
Index: third_party/WebKit/Source/wtf/text/WTFString.h
diff --git a/third_party/WebKit/Source/wtf/text/WTFString.h
b/third_party/WebKit/Source/wtf/text/WTFString.h
index
256abc6a131a9904461570997cf74da03650d099..b2ee6852357237905b3e7909135cd467b2389b43
100644
--- a/third_party/WebKit/Source/wtf/text/WTFString.h
+++ b/third_party/WebKit/Source/wtf/text/WTFString.h
@@ -179,6 +179,8 @@ public:
CString latin1() const;
CString utf8(UTF8ConversionMode = LenientUTF8Conversion) const;
+ std::string toUTF8() const;
+
UChar operator[](unsigned index) const
{
if (!m_impl || index >= m_impl->length())
Index: third_party/WebKit/Source/wtf/text/WTFStringTest.cpp
diff --git a/third_party/WebKit/Source/wtf/text/WTFStringTest.cpp
b/third_party/WebKit/Source/wtf/text/WTFStringTest.cpp
index
66cf6370575e91a79756585de2b5ad831776f677..0f142fcd6b955f65eb1a0cbeee107dfa457ec02a
100644
--- a/third_party/WebKit/Source/wtf/text/WTFStringTest.cpp
+++ b/third_party/WebKit/Source/wtf/text/WTFStringTest.cpp
@@ -347,4 +347,14 @@ TEST(StringTest, Lower)
EXPECT_STREQ("link",
String::fromUTF8("LIN\xE2\x84\xAA").lower().utf8().data());
}
+TEST(StringTest, ToStdString)
+{
+ char kAsciiChars[] = "1234";
+ EXPECT_EQ(std::string(kAsciiChars), String(kAsciiChars).toUTF8());
+
+ UChar kUTF16Chars[] = {0x20AC, 0xD801, 0};
+ EXPECT_EQ(std::string("\xE2\x82\xAC\xED\xA0\x81"),
String(kUTF16Chars).toUTF8());
+
+}
+
} // namespace WTF