Add WideString::{From,To}Latin1().
Also fix a typo in WideString::FromASCII()'s unit test.
Change-Id: I0cbed4e490347242a4750bd1b35c94a49dae8f85
Reviewed-on: https://pdfium-review.googlesource.com/c/48211
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxcrt/widestring.cpp b/core/fxcrt/widestring.cpp
index 038b302..76d596c 100644
--- a/core/fxcrt/widestring.cpp
+++ b/core/fxcrt/widestring.cpp
@@ -644,6 +644,14 @@
return result;
}
+ByteString WideString::ToLatin1() const {
+ ByteString result;
+ result.Reserve(GetLength());
+ for (wchar_t wc : *this)
+ result.InsertAtBack(static_cast<char>(wc & 0xff));
+ return result;
+}
+
ByteString WideString::ToDefANSI() const {
int src_len = GetLength();
int dest_len = FXSYS_WideCharToMultiByte(
@@ -879,6 +887,15 @@
}
// static
+WideString WideString::FromLatin1(ByteStringView bstr) {
+ WideString result;
+ result.Reserve(bstr.GetLength());
+ for (char c : bstr)
+ result.InsertAtBack(static_cast<wchar_t>(c & 0xff));
+ return result;
+}
+
+// static
WideString WideString::FromDefANSI(ByteStringView bstr) {
int src_len = bstr.GetLength();
int dest_len = FXSYS_MultiByteToWideChar(
diff --git a/core/fxcrt/widestring.h b/core/fxcrt/widestring.h
index 2dd4d8a..73c275b 100644
--- a/core/fxcrt/widestring.h
+++ b/core/fxcrt/widestring.h
@@ -60,6 +60,7 @@
~WideString();
static WideString FromASCII(ByteStringView str) WARN_UNUSED_RESULT;
+ static WideString FromLatin1(ByteStringView str) WARN_UNUSED_RESULT;
static WideString FromDefANSI(ByteStringView str) WARN_UNUSED_RESULT;
static WideString FromUTF8(ByteStringView str) WARN_UNUSED_RESULT;
static WideString FromUTF16LE(const unsigned short* str,
@@ -200,6 +201,7 @@
}
ByteString ToASCII() const;
+ ByteString ToLatin1() const;
ByteString ToDefANSI() const;
ByteString ToUTF8() const;
diff --git a/core/fxcrt/widestring_unittest.cpp b/core/fxcrt/widestring_unittest.cpp
index 09b9645..08edf48 100644
--- a/core/fxcrt/widestring_unittest.cpp
+++ b/core/fxcrt/widestring_unittest.cpp
@@ -1062,6 +1062,23 @@
.ToASCII());
}
+TEST(WideString, ToLatin1) {
+ const char* kResult =
+ "x"
+ "\x82"
+ "\xff"
+ "\x22"
+ "\x8c"
+ "y";
+ EXPECT_EQ(kResult, WideString(L"x"
+ L"\u0082"
+ L"\u00ff"
+ L"\u0122"
+ L"\u208c"
+ L"y")
+ .ToLatin1());
+}
+
TEST(WideString, ToDefANSI) {
EXPECT_EQ("", WideString().ToDefANSI());
#if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
@@ -1089,7 +1106,7 @@
}
TEST(WideString, FromASCII) {
- EXPECT_EQ(L"", WideString::FromDefANSI(ByteStringView()));
+ EXPECT_EQ(L"", WideString::FromASCII(ByteStringView()));
const wchar_t* kResult =
L"x"
L"\u0002"
@@ -1101,6 +1118,19 @@
"y"));
}
+TEST(WideString, FromLatin1) {
+ EXPECT_EQ(L"", WideString::FromLatin1(ByteStringView()));
+ const wchar_t* kResult =
+ L"x"
+ L"\u0082"
+ L"\u00ff"
+ L"y";
+ EXPECT_EQ(kResult, WideString::FromLatin1("x"
+ "\x82"
+ "\xff"
+ "y"));
+}
+
TEST(WideString, FromDefANSI) {
EXPECT_EQ(L"", WideString::FromDefANSI(ByteStringView()));
#if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_