From 11d33361e95d5ea61ee43e4261fa8f53aa372731 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Fri, 19 Feb 2016 14:26:46 -0800 Subject: Get rid of CFX_CharMap instantiations. The only thing left are a couple of static methods. R=tsepez@chromium.org Review URL: https://codereview.chromium.org/1711273002 . --- core/include/fpdfapi/fpdf_objects.h | 13 +-- core/include/fpdfapi/fpdf_parser.h | 20 +--- core/include/fxcrt/fx_basic.h | 16 +-- core/include/fxcrt/fx_string.h | 1 - .../src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp | 54 +++++----- .../fpdfapi/fpdf_parser/fpdf_parser_objects.cpp | 18 ++-- core/src/fxcrt/fx_basic_bstring.cpp | 3 +- core/src/fxcrt/fx_basic_wstring.cpp | 118 +++++++++------------ 8 files changed, 104 insertions(+), 139 deletions(-) (limited to 'core') diff --git a/core/include/fpdfapi/fpdf_objects.h b/core/include/fpdfapi/fpdf_objects.h index ddc16d95b0..129eebfcfc 100644 --- a/core/include/fpdfapi/fpdf_objects.h +++ b/core/include/fpdfapi/fpdf_objects.h @@ -58,9 +58,7 @@ class CPDF_Object { virtual CFX_ByteString GetString() const { return CFX_ByteString(); } virtual CFX_ByteStringC GetConstString() const { return CFX_ByteStringC(); } - virtual CFX_WideString GetUnicodeText(CFX_CharMap* pCharMap = nullptr) const { - return CFX_WideString(); - } + virtual CFX_WideString GetUnicodeText() const { return CFX_WideString(); } virtual FX_FLOAT GetNumber() const { return 0; } virtual int GetInteger() const { return 0; } virtual CPDF_Dictionary* GetDict() const { return nullptr; } @@ -207,7 +205,7 @@ class CPDF_String : public CPDF_Object { CFX_ByteStringC GetConstString() const override { return CFX_ByteStringC(m_String); } - CFX_WideString GetUnicodeText(CFX_CharMap* pCharMap = nullptr) const override; + CFX_WideString GetUnicodeText() const override; void SetString(const CFX_ByteString& str) override { m_String = str; } bool IsString() const override { return true; } CPDF_String* AsString() override { return this; } @@ -245,7 +243,7 @@ class CPDF_Name : public CPDF_Object { CFX_ByteStringC GetConstString() const override { return CFX_ByteStringC(m_Name); } - CFX_WideString GetUnicodeText(CFX_CharMap* pCharMap = nullptr) const override; + CFX_WideString GetUnicodeText() const override; void SetString(const CFX_ByteString& str) override { m_Name = str; } bool IsName() const override { return true; } CPDF_Name* AsName() override { return this; } @@ -355,8 +353,7 @@ class CPDF_Dictionary : public CPDF_Object { const CFX_ByteStringC& default_str) const; CFX_ByteStringC GetConstStringBy(const CFX_ByteStringC& key, const CFX_ByteStringC& default_str) const; - CFX_WideString GetUnicodeTextBy(const CFX_ByteStringC& key, - CFX_CharMap* pCharMap = NULL) const; + CFX_WideString GetUnicodeTextBy(const CFX_ByteStringC& key) const; int GetIntegerBy(const CFX_ByteStringC& key) const; int GetIntegerBy(const CFX_ByteStringC& key, int default_int) const; FX_BOOL GetBooleanBy(const CFX_ByteStringC& key, @@ -428,7 +425,7 @@ class CPDF_Stream : public CPDF_Object { Type GetType() const override { return STREAM; } CPDF_Object* Clone(FX_BOOL bDirect = FALSE) const override; CPDF_Dictionary* GetDict() const override { return m_pDict; } - CFX_WideString GetUnicodeText(CFX_CharMap* pCharMap = nullptr) const override; + CFX_WideString GetUnicodeText() const override; bool IsStream() const override { return true; } CPDF_Stream* AsStream() override { return this; } const CPDF_Stream* AsStream() const override { return this; } diff --git a/core/include/fpdfapi/fpdf_parser.h b/core/include/fpdfapi/fpdf_parser.h index f57ee5593c..9fa05c75ed 100644 --- a/core/include/fpdfapi/fpdf_parser.h +++ b/core/include/fpdfapi/fpdf_parser.h @@ -697,21 +697,11 @@ CFX_ByteString PDF_NameDecode(const CFX_ByteString& orig); CFX_ByteString PDF_NameEncode(const CFX_ByteString& orig); CFX_ByteString PDF_EncodeString(const CFX_ByteString& src, FX_BOOL bHex = FALSE); -CFX_WideString PDF_DecodeText(const uint8_t* pData, - FX_DWORD size, - CFX_CharMap* pCharMap = NULL); -inline CFX_WideString PDF_DecodeText(const CFX_ByteString& bstr, - CFX_CharMap* pCharMap = NULL) { - return PDF_DecodeText((const uint8_t*)bstr.c_str(), bstr.GetLength(), - pCharMap); -} -CFX_ByteString PDF_EncodeText(const FX_WCHAR* pString, - int len = -1, - CFX_CharMap* pCharMap = NULL); -inline CFX_ByteString PDF_EncodeText(const CFX_WideString& str, - CFX_CharMap* pCharMap = NULL) { - return PDF_EncodeText(str.c_str(), str.GetLength(), pCharMap); -} +CFX_WideString PDF_DecodeText(const uint8_t* pData, FX_DWORD size); +CFX_WideString PDF_DecodeText(const CFX_ByteString& bstr); +CFX_ByteString PDF_EncodeText(const FX_WCHAR* pString, int len = -1); +CFX_ByteString PDF_EncodeText(const CFX_WideString& str); + FX_FLOAT PDF_ClipFloat(FX_FLOAT f); class CFDF_Document : public CPDF_IndirectObjectHolder { public: diff --git a/core/include/fxcrt/fx_basic.h b/core/include/fxcrt/fx_basic.h index 4402856996..3e30b1d1a1 100644 --- a/core/include/fxcrt/fx_basic.h +++ b/core/include/fxcrt/fx_basic.h @@ -221,17 +221,17 @@ class CFX_FileBufferArchive : public IFX_BufferArchive { FX_BOOL m_bTakeover; }; -struct CFX_CharMap { - static CFX_CharMap* GetDefaultMapper(int32_t codepage = 0); - - CFX_WideString (*m_GetWideString)(CFX_CharMap* pMap, - const CFX_ByteString& bstr); +class CFX_CharMap { + public: + static CFX_ByteString GetByteString(FX_WORD codepage, + const CFX_WideString& wstr); - CFX_ByteString (*m_GetByteString)(CFX_CharMap* pMap, - const CFX_WideString& wstr); + static CFX_WideString GetWideString(FX_WORD codepage, + const CFX_ByteString& bstr); - int32_t (*m_GetCodePage)(); + CFX_CharMap() = delete; }; + class CFX_UTF8Decoder { public: CFX_UTF8Decoder() { m_PendingBytes = 0; } diff --git a/core/include/fxcrt/fx_string.h b/core/include/fxcrt/fx_string.h index c625bfa925..ca29e1321a 100644 --- a/core/include/fxcrt/fx_string.h +++ b/core/include/fxcrt/fx_string.h @@ -16,7 +16,6 @@ class CFX_BinaryBuf; class CFX_ByteString; class CFX_WideString; -struct CFX_CharMap; // An immutable string with caller-provided storage which must outlive the // string itself. diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp index 7c1c6b1f53..0b7a841cd4 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp @@ -426,13 +426,11 @@ FX_BOOL PDF_DataDecode(const uint8_t* src_buf, return TRUE; } -CFX_WideString PDF_DecodeText(const uint8_t* src_data, - FX_DWORD src_len, - CFX_CharMap* pCharMap) { +CFX_WideString PDF_DecodeText(const uint8_t* src_data, FX_DWORD src_len) { CFX_WideString result; if (src_len >= 2 && ((src_data[0] == 0xfe && src_data[1] == 0xff) || (src_data[0] == 0xff && src_data[1] == 0xfe))) { - FX_BOOL bBE = src_data[0] == 0xfe; + bool bBE = src_data[0] == 0xfe; FX_DWORD max_chars = (src_len - 2) / 2; if (!max_chars) { return result; @@ -461,44 +459,40 @@ CFX_WideString PDF_DecodeText(const uint8_t* src_data, } } result.ReleaseBuffer(dest_pos); - } else if (!pCharMap) { + } else { FX_WCHAR* dest_buf = result.GetBuffer(src_len); - for (FX_DWORD i = 0; i < src_len; i++) { + for (FX_DWORD i = 0; i < src_len; i++) dest_buf[i] = PDFDocEncoding[src_data[i]]; - } result.ReleaseBuffer(src_len); - } else { - return (*pCharMap->m_GetWideString)( - pCharMap, CFX_ByteString((const FX_CHAR*)src_data, src_len)); } return result; } -CFX_ByteString PDF_EncodeText(const FX_WCHAR* pString, - int len, - CFX_CharMap* pCharMap) { +CFX_WideString PDF_DecodeText(const CFX_ByteString& bstr) { + return PDF_DecodeText((const uint8_t*)bstr.c_str(), bstr.GetLength()); +} + +CFX_ByteString PDF_EncodeText(const FX_WCHAR* pString, int len) { if (len == -1) { len = FXSYS_wcslen(pString); } CFX_ByteString result; - if (!pCharMap) { - FX_CHAR* dest_buf1 = result.GetBuffer(len); - int i; - for (i = 0; i < len; i++) { - int code; - for (code = 0; code < 256; code++) - if (PDFDocEncoding[code] == pString[i]) { - break; - } - if (code == 256) { + FX_CHAR* dest_buf1 = result.GetBuffer(len); + int i; + for (i = 0; i < len; i++) { + int code; + for (code = 0; code < 256; code++) + if (PDFDocEncoding[code] == pString[i]) { break; } - dest_buf1[i] = code; - } - result.ReleaseBuffer(i); - if (i == len) { - return result; + if (code == 256) { + break; } + dest_buf1[i] = code; + } + result.ReleaseBuffer(i); + if (i == len) { + return result; } if (len > INT_MAX / 2 - 1) { @@ -520,6 +514,10 @@ CFX_ByteString PDF_EncodeText(const FX_WCHAR* pString, return result; } +CFX_ByteString PDF_EncodeText(const CFX_WideString& str) { + return PDF_EncodeText(str.c_str(), str.GetLength()); +} + CFX_ByteString PDF_EncodeString(const CFX_ByteString& src, FX_BOOL bHex) { CFX_ByteTextBuf result; int srclen = src.GetLength(); diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp index 5f8b075296..c3b0ebde63 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp @@ -36,12 +36,12 @@ CPDF_String::CPDF_String(const CFX_WideString& str) : m_bHex(FALSE) { m_String = PDF_EncodeText(str); } -CFX_WideString CPDF_String::GetUnicodeText(CFX_CharMap* pCharMap) const { - return PDF_DecodeText(m_String, pCharMap); +CFX_WideString CPDF_String::GetUnicodeText() const { + return PDF_DecodeText(m_String); } -CFX_WideString CPDF_Name::GetUnicodeText(CFX_CharMap* pCharMap) const { - return PDF_DecodeText(m_Name, pCharMap); +CFX_WideString CPDF_Name::GetUnicodeText() const { + return PDF_DecodeText(m_Name); } CPDF_Array::~CPDF_Array() { @@ -256,12 +256,12 @@ CFX_ByteStringC CPDF_Dictionary::GetConstStringBy( return CFX_ByteStringC(); } -CFX_WideString CPDF_Dictionary::GetUnicodeTextBy(const CFX_ByteStringC& key, - CFX_CharMap* pCharMap) const { +CFX_WideString CPDF_Dictionary::GetUnicodeTextBy( + const CFX_ByteStringC& key) const { CPDF_Object* p = GetElement(key); if (CPDF_Reference* pRef = ToReference(p)) p = pRef->GetDirect(); - return p ? p->GetUnicodeText(pCharMap) : CFX_WideString(); + return p ? p->GetUnicodeText() : CFX_WideString(); } CFX_ByteString CPDF_Dictionary::GetStringBy(const CFX_ByteStringC& key, @@ -562,10 +562,10 @@ void CPDF_Stream::InitStreamFromFile(IFX_FileRead* pFile, } } -CFX_WideString CPDF_Stream::GetUnicodeText(CFX_CharMap* pCharMap) const { +CFX_WideString CPDF_Stream::GetUnicodeText() const { CPDF_StreamAcc stream; stream.LoadAllData(this, FALSE); - return PDF_DecodeText(stream.GetData(), stream.GetSize(), pCharMap); + return PDF_DecodeText(stream.GetData(), stream.GetSize()); } CPDF_StreamAcc::CPDF_StreamAcc() { diff --git a/core/src/fxcrt/fx_basic_bstring.cpp b/core/src/fxcrt/fx_basic_bstring.cpp index 85e5041095..af06f0074b 100644 --- a/core/src/fxcrt/fx_basic_bstring.cpp +++ b/core/src/fxcrt/fx_basic_bstring.cpp @@ -920,8 +920,7 @@ CFX_ByteString CFX_ByteString::FromUnicode(const FX_WCHAR* str, // static CFX_ByteString CFX_ByteString::FromUnicode(const CFX_WideString& str) { - CFX_CharMap* pCharMap = CFX_CharMap::GetDefaultMapper(); - return (*pCharMap->m_GetByteString)(pCharMap, str); + return CFX_CharMap::GetByteString(0, str); } int CFX_ByteString::Compare(const CFX_ByteStringC& str) const { diff --git a/core/src/fxcrt/fx_basic_wstring.cpp b/core/src/fxcrt/fx_basic_wstring.cpp index d83aa85213..8415e998a0 100644 --- a/core/src/fxcrt/fx_basic_wstring.cpp +++ b/core/src/fxcrt/fx_basic_wstring.cpp @@ -13,6 +13,24 @@ #include "core/include/fxcrt/fx_ext.h" #include "third_party/base/numerics/safe_math.h" +namespace { + +bool IsValidCodePage(FX_WORD codepage) { + switch (codepage) { + case 0: + case 932: + case 936: + case 949: + case 950: + return true; + + default: + return false; + } +} + +} // namespace + // static CFX_WideString::StringData* CFX_WideString::StringData::Create(int nLen) { // TODO(palmer): |nLen| should really be declared as |size_t|, or @@ -349,8 +367,7 @@ CFX_WideString CFX_WideString::FromLocal(const CFX_ByteString& str) { // static CFX_WideString CFX_WideString::FromCodePage(const CFX_ByteString& str, FX_WORD codepage) { - CFX_CharMap* pCharMap = CFX_CharMap::GetDefaultMapper(codepage); - return pCharMap->m_GetWideString(pCharMap, str); + return CFX_CharMap::GetWideString(codepage, str); } // static @@ -997,71 +1014,36 @@ int CFX_WideString::GetInteger() const { FX_FLOAT CFX_WideString::GetFloat() const { return m_pData ? FX_wtof(m_pData->m_String, m_pData->m_nDataLength) : 0.0f; } -static CFX_ByteString _DefMap_GetByteString(CFX_CharMap* pCharMap, - const CFX_WideString& widestr) { - int src_len = widestr.GetLength(); - int codepage = pCharMap->m_GetCodePage ? pCharMap->m_GetCodePage() : 0; - int dest_len = FXSYS_WideCharToMultiByte(codepage, 0, widestr.c_str(), - src_len, NULL, 0, NULL, NULL); - if (dest_len == 0) { - return CFX_ByteString(); - } - CFX_ByteString bytestr; - FX_CHAR* dest_buf = bytestr.GetBuffer(dest_len); - FXSYS_WideCharToMultiByte(codepage, 0, widestr.c_str(), src_len, dest_buf, - dest_len, NULL, NULL); - bytestr.ReleaseBuffer(dest_len); - return bytestr; -} -static CFX_WideString _DefMap_GetWideString(CFX_CharMap* pCharMap, - const CFX_ByteString& bytestr) { - int src_len = bytestr.GetLength(); - int codepage = pCharMap->m_GetCodePage ? pCharMap->m_GetCodePage() : 0; + +// static +CFX_ByteString CFX_CharMap::GetByteString(FX_WORD codepage, + const CFX_WideString& wstr) { + FXSYS_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) { + FX_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(FX_WORD codepage, + const CFX_ByteString& bstr) { + FXSYS_assert(IsValidCodePage(codepage)); + int src_len = bstr.GetLength(); int dest_len = - FXSYS_MultiByteToWideChar(codepage, 0, bytestr, src_len, NULL, 0); - if (dest_len == 0) { - return CFX_WideString(); - } - CFX_WideString widestr; - FX_WCHAR* dest_buf = widestr.GetBuffer(dest_len); - FXSYS_MultiByteToWideChar(codepage, 0, bytestr, src_len, dest_buf, dest_len); - widestr.ReleaseBuffer(dest_len); - return widestr; -} -static int _DefMap_GetGBKCodePage() { - return 936; -} -static int _DefMap_GetUHCCodePage() { - return 949; -} -static int _DefMap_GetJISCodePage() { - return 932; -} -static int _DefMap_GetBig5CodePage() { - return 950; -} -static const CFX_CharMap g_DefaultMapper = {&_DefMap_GetWideString, - &_DefMap_GetByteString, NULL}; -static const CFX_CharMap g_DefaultGBKMapper = { - &_DefMap_GetWideString, &_DefMap_GetByteString, &_DefMap_GetGBKCodePage}; -static const CFX_CharMap g_DefaultJISMapper = { - &_DefMap_GetWideString, &_DefMap_GetByteString, &_DefMap_GetJISCodePage}; -static const CFX_CharMap g_DefaultUHCMapper = { - &_DefMap_GetWideString, &_DefMap_GetByteString, &_DefMap_GetUHCCodePage}; -static const CFX_CharMap g_DefaultBig5Mapper = { - &_DefMap_GetWideString, &_DefMap_GetByteString, &_DefMap_GetBig5CodePage}; -CFX_CharMap* CFX_CharMap::GetDefaultMapper(int32_t codepage) { - switch (codepage) { - case 0: - return (CFX_CharMap*)&g_DefaultMapper; - case 932: - return (CFX_CharMap*)&g_DefaultJISMapper; - case 936: - return (CFX_CharMap*)&g_DefaultGBKMapper; - case 949: - return (CFX_CharMap*)&g_DefaultUHCMapper; - case 950: - return (CFX_CharMap*)&g_DefaultBig5Mapper; - } - return NULL; + FXSYS_MultiByteToWideChar(codepage, 0, bstr, src_len, nullptr, 0); + CFX_WideString wstr; + if (dest_len) { + FX_WCHAR* dest_buf = wstr.GetBuffer(dest_len); + FXSYS_MultiByteToWideChar(codepage, 0, bstr, src_len, dest_buf, dest_len); + wstr.ReleaseBuffer(dest_len); + } + return wstr; } -- cgit v1.2.3