diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/fxcrt/fx_system.cpp | 6 | ||||
-rw-r--r-- | core/fxcrt/widestring.cpp | 59 | ||||
-rw-r--r-- | core/fxcrt/widestring.h | 3 | ||||
-rw-r--r-- | core/fxcrt/widestring_unittest.cpp | 21 |
4 files changed, 39 insertions, 50 deletions
diff --git a/core/fxcrt/fx_system.cpp b/core/fxcrt/fx_system.cpp index 27cbd65f3c..061ca6d82d 100644 --- a/core/fxcrt/fx_system.cpp +++ b/core/fxcrt/fx_system.cpp @@ -203,6 +203,7 @@ int FXSYS_WideCharToMultiByte(uint32_t codepage, } return len; } + int FXSYS_MultiByteToWideChar(uint32_t codepage, uint32_t dwFlags, const char* bstr, @@ -211,9 +212,8 @@ int FXSYS_MultiByteToWideChar(uint32_t codepage, int buflen) { int wlen = 0; for (int i = 0; i < blen; i++) { - if (buf && wlen < buflen) { - buf[wlen] = bstr[i]; - } + if (buf && wlen < buflen) + buf[wlen] = reinterpret_cast<const uint8_t*>(bstr)[i]; wlen++; } return wlen; diff --git a/core/fxcrt/widestring.cpp b/core/fxcrt/widestring.cpp index 7dd1c30eb0..97073f170d 100644 --- a/core/fxcrt/widestring.cpp +++ b/core/fxcrt/widestring.cpp @@ -277,43 +277,6 @@ Optional<WideString> TryVSWPrintf(size_t size, return {str}; } -#ifndef NDEBUG -bool IsValidWideCodePage(uint16_t codepage) { - switch (codepage) { - case FX_CODEPAGE_DefANSI: - case FX_CODEPAGE_ShiftJIS: - case FX_CODEPAGE_ChineseSimplified: - case FX_CODEPAGE_Hangul: - case FX_CODEPAGE_ChineseTraditional: - return true; - default: - return false; - } -} -#endif - -WideString GetWideString(uint16_t codepage, const ByteStringView& bstr) { -#ifndef NDEBUG - ASSERT(IsValidWideCodePage(codepage)); -#endif - - int src_len = bstr.GetLength(); - int dest_len = FXSYS_MultiByteToWideChar( - codepage, 0, bstr.unterminated_c_str(), src_len, nullptr, 0); - if (!dest_len) - return WideString(); - - WideString wstr; - { - // Span's lifetime must end before ReleaseBuffer() below. - pdfium::span<wchar_t> dest_buf = wstr.GetBuffer(dest_len); - FXSYS_MultiByteToWideChar(codepage, 0, bstr.unterminated_c_str(), src_len, - dest_buf.data(), dest_len); - } - wstr.ReleaseBuffer(dest_len); - return wstr; -} - } // namespace namespace fxcrt { @@ -902,14 +865,22 @@ size_t WideString::Replace(const WideStringView& pOld, } // static -WideString WideString::FromLocal(const ByteStringView& str) { - return FromCodePage(str, 0); -} +WideString WideString::FromLocal(const ByteStringView& bstr) { + int src_len = bstr.GetLength(); + int dest_len = FXSYS_MultiByteToWideChar( + FX_CODEPAGE_DefANSI, 0, bstr.unterminated_c_str(), src_len, nullptr, 0); + if (!dest_len) + return WideString(); -// static -WideString WideString::FromCodePage(const ByteStringView& str, - uint16_t codepage) { - return GetWideString(codepage, str); + WideString wstr; + { + // Span's lifetime must end before ReleaseBuffer() below. + pdfium::span<wchar_t> dest_buf = wstr.GetBuffer(dest_len); + FXSYS_MultiByteToWideChar(FX_CODEPAGE_DefANSI, 0, bstr.unterminated_c_str(), + src_len, dest_buf.data(), dest_len); + } + wstr.ReleaseBuffer(dest_len); + return wstr; } // static diff --git a/core/fxcrt/widestring.h b/core/fxcrt/widestring.h index dc5dd23428..c51c74506b 100644 --- a/core/fxcrt/widestring.h +++ b/core/fxcrt/widestring.h @@ -61,9 +61,6 @@ class WideString { ~WideString(); static WideString FromLocal(const ByteStringView& str) WARN_UNUSED_RESULT; - static WideString FromCodePage(const ByteStringView& str, - uint16_t codepage) WARN_UNUSED_RESULT; - static WideString FromUTF8(const ByteStringView& str) WARN_UNUSED_RESULT; static WideString FromUTF16LE(const unsigned short* str, size_t len) WARN_UNUSED_RESULT; diff --git a/core/fxcrt/widestring_unittest.cpp b/core/fxcrt/widestring_unittest.cpp index 9d38aa45e9..160dc929db 100644 --- a/core/fxcrt/widestring_unittest.cpp +++ b/core/fxcrt/widestring_unittest.cpp @@ -1025,6 +1025,27 @@ TEST(WideString, ToDefANSI) { .ToDefANSI()); } +TEST(WideString, FromLocal) { + EXPECT_EQ(L"", WideString::FromLocal(ByteStringView())); +#if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ + const wchar_t* kResult = + L"x" + L"\u20ac" + L"\u00ff" + L"y"; +#else + const wchar_t* kResult = + L"x" + L"\u0080" + L"\u00ff" + L"y"; +#endif + EXPECT_EQ(kResult, WideString::FromLocal("x" + "\x80" + "\xff" + "y")); +} + TEST(WideStringView, FromVector) { std::vector<WideStringView::UnsignedType> null_vec; WideStringView null_string(null_vec); |