From 821e59c5d2034066843dd6040a06fe637a6369df Mon Sep 17 00:00:00 2001 From: Henrique Nakashima Date: Wed, 9 Aug 2017 17:02:51 -0400 Subject: Remove CFX_ByteTextBuf from CFX_UTF8Encoder Also making CFX_ByteString(ostringstream) const. Bug: pdfium:731 Change-Id: Iaee610248a5634e8731eaf05944c32d52c4e8015 Reviewed-on: https://pdfium-review.googlesource.com/10151 Commit-Queue: Henrique Nakashima Reviewed-by: Tom Sepez --- core/fxcrt/fx_basic.h | 13 +++++++++---- core/fxcrt/fx_basic_utf.cpp | 38 +++++++++++++++++++++++++++----------- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/core/fxcrt/fx_basic.h b/core/fxcrt/fx_basic.h index e856a3e0ba..c6ffc0d535 100644 --- a/core/fxcrt/fx_basic.h +++ b/core/fxcrt/fx_basic.h @@ -9,6 +9,7 @@ #include #include +#include #include "core/fxcrt/cfx_retain_ptr.h" #include "core/fxcrt/fx_memory.h" @@ -128,14 +129,18 @@ class CFX_UTF8Decoder { class CFX_UTF8Encoder { public: - CFX_UTF8Encoder() {} + CFX_UTF8Encoder(); + ~CFX_UTF8Encoder(); void Input(wchar_t unicode); - void AppendStr(const CFX_ByteStringC& str) { m_Buffer << str; } - CFX_ByteStringC GetResult() const { return m_Buffer.AsStringC(); } + void AppendStr(const CFX_ByteStringC& str); + + // The data returned by GetResult() is invalidated when this is modified by + // appending any data. + CFX_ByteStringC GetResult() const; private: - CFX_ByteTextBuf m_Buffer; + std::vector m_Buffer; }; template diff --git a/core/fxcrt/fx_basic_utf.cpp b/core/fxcrt/fx_basic_utf.cpp index 4dbfa37284..fcfd5934e4 100644 --- a/core/fxcrt/fx_basic_utf.cpp +++ b/core/fxcrt/fx_basic_utf.cpp @@ -10,9 +10,11 @@ void CFX_UTF8Decoder::Clear() { m_Buffer.Clear(); m_PendingBytes = 0; } + void CFX_UTF8Decoder::AppendChar(uint32_t ch) { m_Buffer.AppendChar((wchar_t)ch); } + void CFX_UTF8Decoder::Input(uint8_t byte) { if (byte < 0x80) { m_PendingBytes = 0; @@ -43,37 +45,51 @@ void CFX_UTF8Decoder::Input(uint8_t byte) { m_PendingChar = (byte & 0x01) << 30; } } -void CFX_UTF8Encoder::Input(wchar_t unicode) { - if ((uint32_t)unicode < 0x80) { - m_Buffer.AppendChar(unicode); + +CFX_UTF8Encoder::CFX_UTF8Encoder() {} + +CFX_UTF8Encoder::~CFX_UTF8Encoder() {} + +void CFX_UTF8Encoder::Input(wchar_t unicodeAsWchar) { + uint32_t unicode = static_cast(unicodeAsWchar); + if (unicode < 0x80) { + m_Buffer.push_back(unicode); } else { - if ((uint32_t)unicode >= 0x80000000) { + if (unicode >= 0x80000000) { return; } int nbytes = 0; - if ((uint32_t)unicode < 0x800) { + if (unicode < 0x800) { nbytes = 2; - } else if ((uint32_t)unicode < 0x10000) { + } else if (unicode < 0x10000) { nbytes = 3; - } else if ((uint32_t)unicode < 0x200000) { + } else if (unicode < 0x200000) { nbytes = 4; - } else if ((uint32_t)unicode < 0x4000000) { + } else if (unicode < 0x4000000) { nbytes = 5; } else { nbytes = 6; } static uint8_t prefix[] = {0xc0, 0xe0, 0xf0, 0xf8, 0xfc}; int order = 1 << ((nbytes - 1) * 6); - int code = unicode; - m_Buffer.AppendChar(prefix[nbytes - 2] | (code / order)); + int code = unicodeAsWchar; + m_Buffer.push_back(prefix[nbytes - 2] | (code / order)); for (int i = 0; i < nbytes - 1; i++) { code = code % order; order >>= 6; - m_Buffer.AppendChar(0x80 | (code / order)); + m_Buffer.push_back(0x80 | (code / order)); } } } +void CFX_UTF8Encoder::AppendStr(const CFX_ByteStringC& str) { + m_Buffer.insert(m_Buffer.end(), str.begin(), str.end()); +} + +CFX_ByteStringC CFX_UTF8Encoder::GetResult() const { + return CFX_ByteStringC(m_Buffer.data(), m_Buffer.size()); +} + CFX_ByteString FX_UTF8Encode(const CFX_WideStringC& wsStr) { FX_STRSIZE len = wsStr.GetLength(); const wchar_t* pStr = wsStr.unterminated_c_str(); -- cgit v1.2.3