From 10cfea1fdafc8fcf1edd60bc783e9db9ef6229c0 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Mon, 16 Nov 2015 13:09:00 -0500 Subject: Merge to XFA: Reland "Cleanup some numeric code."" This reverts commit 0569ab0b11b723d9bca4ddd642b0cf8828c4bdd1. This changes the various comparisons of char >= '0' && char <= '9' and char < '0' || char > '9' to use std::isdigit checks. It also cleans up a handful of hex to digit conversions to call one common method. R=tsepez@chromium.org Review URL: https://codereview.chromium.org/1449873003 . (cherry picked from commit 3f148915d12f54a946a0c0bf526162b79c39d650) Review URL: https://codereview.chromium.org/1452673002 . --- .../src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp | 64 ++++++++-------------- 1 file changed, 24 insertions(+), 40 deletions(-) (limited to 'core/src/fpdfapi/fpdf_page') diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp index 8d155e787b..a1aa686248 100644 --- a/core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp +++ b/core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp @@ -11,6 +11,7 @@ #include "core/include/fpdfapi/fpdf_module.h" #include "core/include/fpdfapi/fpdf_page.h" #include "core/include/fxcodec/fx_codec.h" +#include "core/include/fxcrt/fx_ext.h" namespace { @@ -791,7 +792,7 @@ CFX_ByteString CPDF_StreamParser::ReadString() { break; case 1: if (ch >= '0' && ch <= '7') { - iEscCode = ch - '0'; + iEscCode = FXSYS_toDecimalDigit(ch); status = 2; break; } @@ -816,7 +817,7 @@ CFX_ByteString CPDF_StreamParser::ReadString() { break; case 2: if (ch >= '0' && ch <= '7') { - iEscCode = iEscCode * 8 + ch - '0'; + iEscCode = iEscCode * 8 + FXSYS_toDecimalDigit(ch); status = 3; } else { buf.AppendChar(iEscCode); @@ -826,7 +827,7 @@ CFX_ByteString CPDF_StreamParser::ReadString() { break; case 3: if (ch >= '0' && ch <= '7') { - iEscCode = iEscCode * 8 + ch - '0'; + iEscCode = iEscCode * 8 + FXSYS_toDecimalDigit(ch); buf.AppendChar(iEscCode); status = 0; } else { @@ -859,50 +860,33 @@ CFX_ByteString CPDF_StreamParser::ReadHexString() { if (!PositionIsInBounds()) return CFX_ByteString(); - int ch = m_pBuf[m_Pos++]; CFX_ByteTextBuf buf; - FX_BOOL bFirst = TRUE; + bool bFirst = true; int code = 0; - while (1) { - if (ch == '>') { - break; - } - if (ch >= '0' && ch <= '9') { - if (bFirst) { - code = (ch - '0') * 16; - } else { - code += ch - '0'; - buf.AppendChar((char)code); - } - bFirst = !bFirst; - } else if (ch >= 'A' && ch <= 'F') { - if (bFirst) { - code = (ch - 'A' + 10) * 16; - } else { - code += ch - 'A' + 10; - buf.AppendChar((char)code); - } - bFirst = !bFirst; - } else if (ch >= 'a' && ch <= 'f') { - if (bFirst) { - code = (ch - 'a' + 10) * 16; - } else { - code += ch - 'a' + 10; - buf.AppendChar((char)code); - } - bFirst = !bFirst; - } - if (!PositionIsInBounds()) + while (PositionIsInBounds()) { + int ch = m_pBuf[m_Pos++]; + + if (ch == '>') break; - ch = m_pBuf[m_Pos++]; + if (!std::isxdigit(ch)) + continue; + + int val = FXSYS_toHexDigit(ch); + if (bFirst) { + code = val * 16; + } else { + code += val; + buf.AppendByte((uint8_t)code); + } + bFirst = !bFirst; } - if (!bFirst) { + if (!bFirst) buf.AppendChar((char)code); - } - if (buf.GetLength() > MAX_STRING_LENGTH) { + + if (buf.GetLength() > MAX_STRING_LENGTH) return CFX_ByteString(buf.GetBuffer(), MAX_STRING_LENGTH); - } + return buf.GetByteString(); } -- cgit v1.2.3