From 0915087c64d64023f503d466b73835c09d0ed3a3 Mon Sep 17 00:00:00 2001 From: Henrique Nakashima Date: Thu, 8 Jun 2017 10:08:08 -0400 Subject: Replacing CFX_ByteTextBuf with stringstream in cpdf_streamparser.cpp. Loaded PDF Reference 1-7.pdf 10 times to test performance, no apparent changes. All measurements between 62s and 65s in all cases. Bug: pdfium:731 Change-Id: I0a39bae45fc19a6bae0c634c5c8ef1d952ded26a Reviewed-on: https://pdfium-review.googlesource.com/6390 Reviewed-by: Lei Zhang Reviewed-by: dsinclair Commit-Queue: Henrique Nakashima --- core/fpdfapi/page/cpdf_streamparser.cpp | 60 ++++++++++++++++----------------- 1 file changed, 29 insertions(+), 31 deletions(-) (limited to 'core/fpdfapi/page') diff --git a/core/fpdfapi/page/cpdf_streamparser.cpp b/core/fpdfapi/page/cpdf_streamparser.cpp index e8116cf980..c3dc54a734 100644 --- a/core/fpdfapi/page/cpdf_streamparser.cpp +++ b/core/fpdfapi/page/cpdf_streamparser.cpp @@ -8,7 +8,9 @@ #include +#include #include +#include #include #include "core/fpdfapi/cpdf_modulemgr.h" @@ -220,7 +222,7 @@ std::unique_ptr CPDF_StreamParser::ReadInlineStream( memcpy(pData.get(), m_pBuf + m_Pos, dwStreamSize); m_Pos += dwStreamSize; } - pDict->SetNewFor("Length", (int)dwStreamSize); + pDict->SetNewFor("Length", static_cast(dwStreamSize)); return pdfium::MakeUnique(std::move(pData), dwStreamSize, std::move(pDict)); } @@ -473,7 +475,7 @@ CFX_ByteString CPDF_StreamParser::ReadString() { return CFX_ByteString(); uint8_t ch = m_pBuf[m_Pos++]; - CFX_ByteTextBuf buf; + std::ostringstream buf; int parlevel = 0; int status = 0; int iEscCode = 0; @@ -482,20 +484,19 @@ CFX_ByteString CPDF_StreamParser::ReadString() { case 0: if (ch == ')') { if (parlevel == 0) { - if (buf.GetLength() > kMaxStringLength) { - return CFX_ByteString(buf.GetBuffer(), kMaxStringLength); - } - return buf.MakeString(); + return CFX_ByteString( + buf.str().c_str(), + std::min(static_cast(buf.tellp()), kMaxStringLength)); } parlevel--; - buf.AppendChar(')'); + buf << ')'; } else if (ch == '(') { parlevel++; - buf.AppendChar('('); + buf << '('; } else if (ch == '\\') { status = 1; } else { - buf.AppendChar((char)ch); + buf << static_cast(ch); } break; case 1: @@ -505,21 +506,21 @@ CFX_ByteString CPDF_StreamParser::ReadString() { break; } if (ch == 'n') { - buf.AppendChar('\n'); + buf << '\n'; } else if (ch == 'r') { - buf.AppendChar('\r'); + buf << '\r'; } else if (ch == 't') { - buf.AppendChar('\t'); + buf << '\t'; } else if (ch == 'b') { - buf.AppendChar('\b'); + buf << '\b'; } else if (ch == 'f') { - buf.AppendChar('\f'); + buf << '\f'; } else if (ch == '\r') { status = 4; break; } else if (ch == '\n') { } else { - buf.AppendChar(ch); + buf << static_cast(ch); } status = 0; break; @@ -529,7 +530,7 @@ CFX_ByteString CPDF_StreamParser::ReadString() { iEscCode * 8 + FXSYS_DecimalCharToInt(static_cast(ch)); status = 3; } else { - buf.AppendChar(iEscCode); + buf << static_cast(iEscCode); status = 0; continue; } @@ -538,19 +539,18 @@ CFX_ByteString CPDF_StreamParser::ReadString() { if (ch >= '0' && ch <= '7') { iEscCode = iEscCode * 8 + FXSYS_DecimalCharToInt(static_cast(ch)); - buf.AppendChar(iEscCode); + buf << static_cast(iEscCode); status = 0; } else { - buf.AppendChar(iEscCode); + buf << static_cast(iEscCode); status = 0; continue; } break; case 4: status = 0; - if (ch != '\n') { + if (ch != '\n') continue; - } break; } if (!PositionIsInBounds()) @@ -561,17 +561,16 @@ CFX_ByteString CPDF_StreamParser::ReadString() { if (PositionIsInBounds()) ++m_Pos; - if (buf.GetLength() > kMaxStringLength) { - return CFX_ByteString(buf.GetBuffer(), kMaxStringLength); - } - return buf.MakeString(); + return CFX_ByteString( + buf.str().c_str(), + std::min(static_cast(buf.tellp()), kMaxStringLength)); } CFX_ByteString CPDF_StreamParser::ReadHexString() { if (!PositionIsInBounds()) return CFX_ByteString(); - CFX_ByteTextBuf buf; + std::ostringstream buf; bool bFirst = true; int code = 0; while (PositionIsInBounds()) { @@ -588,17 +587,16 @@ CFX_ByteString CPDF_StreamParser::ReadHexString() { code = val * 16; } else { code += val; - buf.AppendByte((uint8_t)code); + buf << static_cast(code); } bFirst = !bFirst; } if (!bFirst) - buf.AppendChar((char)code); - - if (buf.GetLength() > kMaxStringLength) - return CFX_ByteString(buf.GetBuffer(), kMaxStringLength); + buf << static_cast(code); - return buf.MakeString(); + return CFX_ByteString( + buf.str().c_str(), + std::min(static_cast(buf.tellp()), kMaxStringLength)); } bool CPDF_StreamParser::PositionIsInBounds() const { -- cgit v1.2.3