diff options
author | Dan Sinclair <dsinclair@chromium.org> | 2017-05-04 10:17:51 -0400 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2017-05-04 17:56:25 +0000 |
commit | c0aefd45c89a2980de8965f12bc80db408dfa78c (patch) | |
tree | 0dd4d480453dc7ae743b3b26c3fb06e412a69da2 /core/fxcrt | |
parent | b4a75830cc03dc6c44c4e0f3b5e31d05a0d8596a (diff) | |
download | pdfium-c0aefd45c89a2980de8965f12bc80db408dfa78c.tar.xz |
Cleanup CFX_CharMap
This CL removes the CFX_CharMap class and moves the two static methods
into the files in which they're used.
Change-Id: I8ff7cbfd7f1ef3970e39c77ffa3439099f7fec02
Reviewed-on: https://pdfium-review.googlesource.com/4873
Reviewed-by: dsinclair <dsinclair@chromium.org>
Reviewed-by: Nicolás Peña <npm@chromium.org>
Commit-Queue: dsinclair <dsinclair@chromium.org>
Diffstat (limited to 'core/fxcrt')
-rw-r--r-- | core/fxcrt/cfx_bytestring.cpp | 36 | ||||
-rw-r--r-- | core/fxcrt/cfx_widestring.cpp | 86 | ||||
-rw-r--r-- | core/fxcrt/fx_basic.h | 23 |
3 files changed, 71 insertions, 74 deletions
diff --git a/core/fxcrt/cfx_bytestring.cpp b/core/fxcrt/cfx_bytestring.cpp index 5c534507c6..f0dc370cc9 100644 --- a/core/fxcrt/cfx_bytestring.cpp +++ b/core/fxcrt/cfx_bytestring.cpp @@ -12,7 +12,7 @@ #include <cctype> #include "core/fxcrt/cfx_string_pool_template.h" -#include "core/fxcrt/fx_basic.h" +#include "core/fxcrt/fx_codepage.h" #include "core/fxcrt/fx_extension.h" #include "core/fxcrt/fx_safe_types.h" #include "third_party/base/numerics/safe_math.h" @@ -82,6 +82,38 @@ const char* FX_strstr(const char* haystack, return nullptr; } +#ifndef NDEBUG +bool IsValidCodePage(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 + +CFX_ByteString GetByteString(uint16_t codepage, const CFX_WideStringC& wstr) { + ASSERT(IsValidCodePage(codepage)); + + int src_len = wstr.GetLength(); + int dest_len = FXSYS_WideCharToMultiByte(codepage, 0, wstr.c_str(), src_len, + nullptr, 0, nullptr, nullptr); + if (!dest_len) + return CFX_ByteString(); + + CFX_ByteString bstr; + char* dest_buf = bstr.GetBuffer(dest_len); + FXSYS_WideCharToMultiByte(codepage, 0, wstr.c_str(), src_len, dest_buf, + dest_len, nullptr, nullptr); + bstr.ReleaseBuffer(dest_len); + return bstr; +} + } // namespace static_assert(sizeof(CFX_ByteString) <= sizeof(char*), @@ -685,7 +717,7 @@ CFX_ByteString CFX_ByteString::FromUnicode(const wchar_t* str, FX_STRSIZE len) { // static CFX_ByteString CFX_ByteString::FromUnicode(const CFX_WideString& str) { - return CFX_CharMap::GetByteString(0, str.AsStringC()); + return GetByteString(0, str.AsStringC()); } int CFX_ByteString::Compare(const CFX_ByteStringC& str) const { diff --git a/core/fxcrt/cfx_widestring.cpp b/core/fxcrt/cfx_widestring.cpp index 8cd4de6edd..08f41681a4 100644 --- a/core/fxcrt/cfx_widestring.cpp +++ b/core/fxcrt/cfx_widestring.cpp @@ -13,7 +13,7 @@ #include <cwctype> #include "core/fxcrt/cfx_string_pool_template.h" -#include "core/fxcrt/fx_basic.h" +#include "core/fxcrt/fx_codepage.h" #include "core/fxcrt/fx_extension.h" #include "core/fxcrt/fx_safe_types.h" #include "third_party/base/numerics/safe_math.h" @@ -30,22 +30,6 @@ template struct std::hash<CFX_WideString>; namespace { -#ifndef NDEBUG -bool IsValidCodePage(uint16_t codepage) { - switch (codepage) { - case 0: - case 932: - case 936: - case 949: - case 950: - return true; - - default: - return false; - } -} -#endif - const wchar_t* FX_wcsstr(const wchar_t* haystack, int haystack_len, const wchar_t* needle, @@ -260,6 +244,38 @@ FX_STRSIZE GuessSizeForVSWPrintf(const wchar_t* pFormat, va_list argList) { return nMaxLen; } +#ifndef NDEBUG +bool IsValidCodePage(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 + +CFX_WideString GetWideString(uint16_t codepage, const CFX_ByteStringC& bstr) { + ASSERT(IsValidCodePage(codepage)); + + int src_len = bstr.GetLength(); + int dest_len = + FXSYS_MultiByteToWideChar(codepage, 0, bstr.c_str(), src_len, nullptr, 0); + if (!dest_len) + return CFX_WideString(); + + CFX_WideString wstr; + wchar_t* dest_buf = wstr.GetBuffer(dest_len); + FXSYS_MultiByteToWideChar(codepage, 0, bstr.c_str(), src_len, dest_buf, + dest_len); + wstr.ReleaseBuffer(dest_len); + return wstr; +} + } // namespace static_assert(sizeof(CFX_WideString) <= sizeof(wchar_t*), @@ -849,7 +865,7 @@ CFX_WideString CFX_WideString::FromLocal(const CFX_ByteStringC& str) { // static CFX_WideString CFX_WideString::FromCodePage(const CFX_ByteStringC& str, uint16_t codepage) { - return CFX_CharMap::GetWideString(codepage, str); + return GetWideString(codepage, str); } // static @@ -1033,37 +1049,3 @@ int CFX_WideString::GetInteger() const { float CFX_WideString::GetFloat() const { return m_pData ? FX_wtof(m_pData->m_String, m_pData->m_nDataLength) : 0.0f; } - -// static -CFX_ByteString CFX_CharMap::GetByteString(uint16_t codepage, - const CFX_WideStringC& wstr) { - ASSERT(IsValidCodePage(codepage)); - int src_len = wstr.GetLength(); - int dest_len = FXSYS_WideCharToMultiByte(codepage, 0, wstr.c_str(), src_len, - nullptr, 0, nullptr, nullptr); - CFX_ByteString bstr; - if (dest_len) { - char* dest_buf = bstr.GetBuffer(dest_len); - FXSYS_WideCharToMultiByte(codepage, 0, wstr.c_str(), src_len, dest_buf, - dest_len, nullptr, nullptr); - bstr.ReleaseBuffer(dest_len); - } - return bstr; -} - -// static -CFX_WideString CFX_CharMap::GetWideString(uint16_t codepage, - const CFX_ByteStringC& bstr) { - ASSERT(IsValidCodePage(codepage)); - int src_len = bstr.GetLength(); - int dest_len = - FXSYS_MultiByteToWideChar(codepage, 0, bstr.c_str(), src_len, nullptr, 0); - CFX_WideString wstr; - if (dest_len) { - wchar_t* dest_buf = wstr.GetBuffer(dest_len); - FXSYS_MultiByteToWideChar(codepage, 0, bstr.c_str(), src_len, dest_buf, - dest_len); - wstr.ReleaseBuffer(dest_len); - } - return wstr; -} diff --git a/core/fxcrt/fx_basic.h b/core/fxcrt/fx_basic.h index ed2f579579..8669c66f5e 100644 --- a/core/fxcrt/fx_basic.h +++ b/core/fxcrt/fx_basic.h @@ -131,36 +131,19 @@ class CFX_FileBufferArchive { CFX_RetainPtr<IFX_WriteStream> m_pFile; }; -class CFX_CharMap { - public: - static CFX_ByteString GetByteString(uint16_t codepage, - const CFX_WideStringC& wstr); - - static CFX_WideString GetWideString(uint16_t codepage, - const CFX_ByteStringC& bstr); - - CFX_CharMap() = delete; -}; - class CFX_UTF8Decoder { public: CFX_UTF8Decoder() { m_PendingBytes = 0; } void Clear(); - void Input(uint8_t byte); - void AppendChar(uint32_t ch); - void ClearStatus() { m_PendingBytes = 0; } - CFX_WideStringC GetResult() const { return m_Buffer.AsStringC(); } - protected: + private: int m_PendingBytes; - uint32_t m_PendingChar; - CFX_WideTextBuf m_Buffer; }; @@ -172,7 +155,7 @@ class CFX_UTF8Encoder { void AppendStr(const CFX_ByteStringC& str) { m_Buffer << str; } CFX_ByteStringC GetResult() const { return m_Buffer.AsStringC(); } - protected: + private: CFX_ByteTextBuf m_Buffer; }; @@ -207,7 +190,7 @@ class CFX_BitStream { return m_BitSize >= m_BitPos ? m_BitSize - m_BitPos : 0; } - protected: + private: uint32_t m_BitPos; uint32_t m_BitSize; const uint8_t* m_pData; |