From 1c91537c9f9669246713a5be628493ae2fc4899a Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Thu, 3 Mar 2016 17:12:58 -0500 Subject: Combine StrToInt methods. This Cl combines the two StrToInt implementations. In doing so I had to add some more overrides to toDecimalDigit() and add a isDecimalDigit(). BUG=pdfium:423 R=tsepez@chromium.org Review URL: https://codereview.chromium.org/1757283002 . --- core/include/fxcrt/fx_ext.h | 10 ++++- core/src/fpdfapi/fpdf_font/fpdf_font_cid.cpp | 2 +- .../src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp | 15 ++++--- .../src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp | 24 ++++++----- core/src/fxcrt/fx_basic_gcc.cpp | 47 ++++++---------------- core/src/fxcrt/fx_basic_wstring.cpp | 4 +- core/src/fxcrt/fx_extension_unittest.cpp | 10 +++-- core/src/fxcrt/fx_xml_parser.cpp | 5 ++- fpdfsdk/src/javascript/PublicMethods.cpp | 2 +- fpdfsdk/src/javascript/util.cpp | 24 ----------- fpdfsdk/src/javascript/util.h | 2 - 11 files changed, 58 insertions(+), 87 deletions(-) diff --git a/core/include/fxcrt/fx_ext.h b/core/include/fxcrt/fx_ext.h index 3f4f668190..c8afb2b793 100644 --- a/core/include/fxcrt/fx_ext.h +++ b/core/include/fxcrt/fx_ext.h @@ -53,13 +53,21 @@ inline int FXSYS_toHexDigit(const FX_CHAR c) { return upchar > '9' ? upchar - 'A' + 10 : upchar - '0'; } +inline bool FXSYS_isDecimalDigit(const FX_CHAR c) { + return std::isdigit(c); +} + +inline bool FXSYS_isDecimalDigit(const FX_WCHAR c) { + return std::iswdigit(c); +} + inline int FXSYS_toDecimalDigit(const FX_CHAR c) { if (!std::isdigit(c)) return 0; return c - '0'; } -inline int FXSYS_toDecimalDigitWide(const FX_WCHAR c) { +inline int FXSYS_toDecimalDigit(const FX_WCHAR c) { if (!std::iswdigit(c)) return 0; return c - L'0'; diff --git a/core/src/fpdfapi/fpdf_font/fpdf_font_cid.cpp b/core/src/fpdfapi/fpdf_font/fpdf_font_cid.cpp index 3cbd67633f..7c85b8213d 100644 --- a/core/src/fpdfapi/fpdf_font/fpdf_font_cid.cpp +++ b/core/src/fpdfapi/fpdf_font/fpdf_font_cid.cpp @@ -720,7 +720,7 @@ FX_DWORD CPDF_CMapParser::CMap_GetCode(const CFX_ByteStringC& word) { } for (int i = 0; i < word.GetLength() && std::isdigit(word.GetAt(i)); ++i) - num = num * 10 + FXSYS_toDecimalDigit(word.GetAt(i)); + num = num * 10 + FXSYS_toDecimalDigit(static_cast(word.GetAt(i))); return num; } 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 02e3617feb..b49607e743 100644 --- a/core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp +++ b/core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp @@ -533,10 +533,11 @@ CFX_ByteString CPDF_StreamParser::ReadString() { if (!PositionIsInBounds()) return CFX_ByteString(); - int ch = m_pBuf[m_Pos++]; + uint8_t ch = m_pBuf[m_Pos++]; CFX_ByteTextBuf buf; int parlevel = 0; - int status = 0, iEscCode = 0; + int status = 0; + int iEscCode = 0; while (1) { switch (status) { case 0: @@ -560,7 +561,7 @@ CFX_ByteString CPDF_StreamParser::ReadString() { break; case 1: if (ch >= '0' && ch <= '7') { - iEscCode = FXSYS_toDecimalDigit(ch); + iEscCode = FXSYS_toDecimalDigit(static_cast(ch)); status = 2; break; } @@ -585,7 +586,8 @@ CFX_ByteString CPDF_StreamParser::ReadString() { break; case 2: if (ch >= '0' && ch <= '7') { - iEscCode = iEscCode * 8 + FXSYS_toDecimalDigit(ch); + iEscCode = + iEscCode * 8 + FXSYS_toDecimalDigit(static_cast(ch)); status = 3; } else { buf.AppendChar(iEscCode); @@ -595,7 +597,8 @@ CFX_ByteString CPDF_StreamParser::ReadString() { break; case 3: if (ch >= '0' && ch <= '7') { - iEscCode = iEscCode * 8 + FXSYS_toDecimalDigit(ch); + iEscCode = + iEscCode * 8 + FXSYS_toDecimalDigit(static_cast(ch)); buf.AppendChar(iEscCode); status = 0; } else { @@ -617,7 +620,7 @@ CFX_ByteString CPDF_StreamParser::ReadString() { ch = m_pBuf[m_Pos++]; } if (PositionIsInBounds()) - ch = m_pBuf[m_Pos++]; + ++m_Pos; if (buf.GetLength() > MAX_STRING_LENGTH) { return CFX_ByteString(buf.GetBuffer(), MAX_STRING_LENGTH); diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp index 16d1134cb1..737bbe49be 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp @@ -208,12 +208,12 @@ CPDF_Parser::Error CPDF_Parser::StartParse(IFX_FileRead* pFileAccess) { if (!m_Syntax.GetCharAt(5, ch)) return FORMAT_ERROR; if (std::isdigit(ch)) - m_FileVersion = FXSYS_toDecimalDigit(ch) * 10; + m_FileVersion = FXSYS_toDecimalDigit(static_cast(ch)) * 10; if (!m_Syntax.GetCharAt(7, ch)) return FORMAT_ERROR; if (std::isdigit(ch)) - m_FileVersion += FXSYS_toDecimalDigit(ch); + m_FileVersion += FXSYS_toDecimalDigit(static_cast(ch)); if (m_Syntax.m_FileLen < m_Syntax.m_HeaderOffset + 9) return FORMAT_ERROR; @@ -669,7 +669,7 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() { if (std::isdigit(byte)) { start_pos = pos + i; state = ParserState::kObjNum; - objnum = FXSYS_toDecimalDigit(byte); + objnum = FXSYS_toDecimalDigit(static_cast(byte)); } else if (byte == 't') { state = ParserState::kTrailer; inside_index = 1; @@ -684,7 +684,8 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() { case ParserState::kObjNum: if (std::isdigit(byte)) { - objnum = objnum * 10 + FXSYS_toDecimalDigit(byte); + objnum = + objnum * 10 + FXSYS_toDecimalDigit(static_cast(byte)); } else if (PDFCharIsWhitespace(byte)) { state = ParserState::kPostObjNum; } else { @@ -698,7 +699,7 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() { if (std::isdigit(byte)) { start_pos1 = pos + i; state = ParserState::kGenNum; - gennum = FXSYS_toDecimalDigit(byte); + gennum = FXSYS_toDecimalDigit(static_cast(byte)); } else if (byte == 't') { state = ParserState::kTrailer; inside_index = 1; @@ -710,7 +711,8 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() { case ParserState::kGenNum: if (std::isdigit(byte)) { - gennum = gennum * 10 + FXSYS_toDecimalDigit(byte); + gennum = + gennum * 10 + FXSYS_toDecimalDigit(static_cast(byte)); } else if (PDFCharIsWhitespace(byte)) { state = ParserState::kPostGenNum; } else { @@ -725,7 +727,7 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() { inside_index = 1; } else if (std::isdigit(byte)) { objnum = gennum; - gennum = FXSYS_toDecimalDigit(byte); + gennum = FXSYS_toDecimalDigit(static_cast(byte)); start_pos = start_pos1; start_pos1 = pos + i; state = ParserState::kGenNum; @@ -1876,7 +1878,7 @@ CFX_ByteString CPDF_SyntaxParser::ReadString() { break; case 1: if (ch >= '0' && ch <= '7') { - iEscCode = FXSYS_toDecimalDigit(ch); + iEscCode = FXSYS_toDecimalDigit(static_cast(ch)); status = 2; break; } @@ -1901,7 +1903,8 @@ CFX_ByteString CPDF_SyntaxParser::ReadString() { break; case 2: if (ch >= '0' && ch <= '7') { - iEscCode = iEscCode * 8 + FXSYS_toDecimalDigit(ch); + iEscCode = + iEscCode * 8 + FXSYS_toDecimalDigit(static_cast(ch)); status = 3; } else { buf.AppendChar(iEscCode); @@ -1911,7 +1914,8 @@ CFX_ByteString CPDF_SyntaxParser::ReadString() { break; case 3: if (ch >= '0' && ch <= '7') { - iEscCode = iEscCode * 8 + FXSYS_toDecimalDigit(ch); + iEscCode = + iEscCode * 8 + FXSYS_toDecimalDigit(static_cast(ch)); buf.AppendChar(iEscCode); status = 0; } else { diff --git a/core/src/fxcrt/fx_basic_gcc.cpp b/core/src/fxcrt/fx_basic_gcc.cpp index 607f095570..e2ec939e7d 100644 --- a/core/src/fxcrt/fx_basic_gcc.cpp +++ b/core/src/fxcrt/fx_basic_gcc.cpp @@ -11,42 +11,19 @@ #include "core/include/fxcrt/fx_ext.h" #include "core/include/fxcrt/fx_string.h" -template -T FXSYS_StrToInt(const FX_CHAR* str) { - bool neg = false; +template +IntType FXSYS_StrToInt(const CharType* str) { if (!str) return 0; - if (std::numeric_limits::is_signed && *str == '-') { - neg = true; + bool neg = std::numeric_limits::is_signed && *str == '-'; + if (neg) str++; - } - T num = 0; - while (*str && std::isdigit(*str)) { - T val = FXSYS_toDecimalDigit(*str); - if (num > (std::numeric_limits::max() - val) / 10) - break; - num = num * 10 + val; - str++; - } - return neg ? -num : num; -} - -template -T FXSYS_StrToInt(const FX_WCHAR* str) { - bool neg = false; - if (!str) - return 0; - - if (std::numeric_limits::is_signed && *str == '-') { - neg = true; - str++; - } - T num = 0; - while (*str && std::iswdigit(*str)) { - T val = FXSYS_toDecimalDigitWide(*str); - if (num > (std::numeric_limits::max() - val) / 10) + IntType num = 0; + while (*str && FXSYS_isDecimalDigit(*str)) { + IntType val = FXSYS_toDecimalDigit(*str); + if (num > (std::numeric_limits::max() - val) / 10) break; num = num * 10 + val; @@ -93,19 +70,19 @@ STR_T FXSYS_IntToStr(T value, STR_T string, int radix) { extern "C" { #endif int32_t FXSYS_atoi(const FX_CHAR* str) { - return FXSYS_StrToInt(str); + return FXSYS_StrToInt(str); } uint32_t FXSYS_atoui(const FX_CHAR* str) { return FXSYS_StrToInt(str); } int32_t FXSYS_wtoi(const FX_WCHAR* str) { - return FXSYS_StrToInt(str); + return FXSYS_StrToInt(str); } int64_t FXSYS_atoi64(const FX_CHAR* str) { - return FXSYS_StrToInt(str); + return FXSYS_StrToInt(str); } int64_t FXSYS_wtoi64(const FX_WCHAR* str) { - return FXSYS_StrToInt(str); + return FXSYS_StrToInt(str); } const FX_CHAR* FXSYS_i64toa(int64_t value, FX_CHAR* str, int radix) { return FXSYS_IntToStr(value, str, radix); diff --git a/core/src/fxcrt/fx_basic_wstring.cpp b/core/src/fxcrt/fx_basic_wstring.cpp index 396dbc4fff..8df72466e8 100644 --- a/core/src/fxcrt/fx_basic_wstring.cpp +++ b/core/src/fxcrt/fx_basic_wstring.cpp @@ -994,7 +994,7 @@ FX_FLOAT FX_wtof(const FX_WCHAR* str, int len) { if (str[cc] == '.') { break; } - integer = integer * 10 + FXSYS_toDecimalDigitWide(str[cc]); + integer = integer * 10 + FXSYS_toDecimalDigit(str[cc]); cc++; } FX_FLOAT fraction = 0; @@ -1002,7 +1002,7 @@ FX_FLOAT FX_wtof(const FX_WCHAR* str, int len) { cc++; FX_FLOAT scale = 0.1f; while (cc < len) { - fraction += scale * FXSYS_toDecimalDigitWide(str[cc]); + fraction += scale * FXSYS_toDecimalDigit(str[cc]); scale *= 0.1f; cc++; } diff --git a/core/src/fxcrt/fx_extension_unittest.cpp b/core/src/fxcrt/fx_extension_unittest.cpp index b12dcbe07f..e2ae870f93 100644 --- a/core/src/fxcrt/fx_extension_unittest.cpp +++ b/core/src/fxcrt/fx_extension_unittest.cpp @@ -16,9 +16,13 @@ TEST(fxcrt, FXSYS_toHexDigit) { TEST(fxcrt, FXSYS_toDecimalDigit) { EXPECT_EQ(7, FXSYS_toDecimalDigit('7')); EXPECT_EQ(0, FXSYS_toDecimalDigit('a')); + EXPECT_EQ(7, FXSYS_toDecimalDigit(L'7')); + EXPECT_EQ(0, FXSYS_toDecimalDigit(L'a')); } -TEST(fxcrt, FXSYS_toDecimalDigitWide) { - EXPECT_EQ(7, FXSYS_toDecimalDigitWide(L'7')); - EXPECT_EQ(0, FXSYS_toDecimalDigitWide(L'a')); +TEST(fxcrt, FXSYS_isDecimalDigit) { + EXPECT_TRUE(FXSYS_isDecimalDigit('7')); + EXPECT_TRUE(FXSYS_isDecimalDigit(L'7')); + EXPECT_FALSE(FXSYS_isDecimalDigit('a')); + EXPECT_FALSE(FXSYS_isDecimalDigit(L'a')); } diff --git a/core/src/fxcrt/fx_xml_parser.cpp b/core/src/fxcrt/fx_xml_parser.cpp index ab729eebaf..59c3de444e 100644 --- a/core/src/fxcrt/fx_xml_parser.cpp +++ b/core/src/fxcrt/fx_xml_parser.cpp @@ -231,7 +231,7 @@ FX_DWORD CXML_Parser::GetCharRef() { break; } if (g_FXCRT_XML_IsDigital(ch)) - code = code * 10 + FXSYS_toDecimalDigit(ch); + code = code * 10 + FXSYS_toDecimalDigit(static_cast(ch)); break; case 4: m_dwIndex++; @@ -243,7 +243,8 @@ FX_DWORD CXML_Parser::GetCharRef() { g_FXCRT_XML_ByteTypes[ch] & FXCRTM_XML_CHARTYPE_HexChar; if (nHex) { if (nHex == FXCRTM_XML_CHARTYPE_HexDigital) { - code = (code << 4) + FXSYS_toDecimalDigit(ch); + code = + (code << 4) + FXSYS_toDecimalDigit(static_cast(ch)); } else if (nHex == FXCRTM_XML_CHARTYPE_HexLowerLetter) { code = (code << 4) + ch - 87; } else { diff --git a/fpdfsdk/src/javascript/PublicMethods.cpp b/fpdfsdk/src/javascript/PublicMethods.cpp index f510d51d64..f228072b35 100644 --- a/fpdfsdk/src/javascript/PublicMethods.cpp +++ b/fpdfsdk/src/javascript/PublicMethods.cpp @@ -242,7 +242,7 @@ int CJS_PublicMethods::ParseStringInteger(const CFX_WideString& string, if (!FXSYS_iswdigit(c)) break; - nRet = nRet * 10 + FXSYS_toDecimalDigitWide(c); + nRet = nRet * 10 + FXSYS_toDecimalDigit(c); nSkip = i - nStart + 1; if (nSkip >= nMaxStep) break; diff --git a/fpdfsdk/src/javascript/util.cpp b/fpdfsdk/src/javascript/util.cpp index 4bc28cd03a..c9149be570 100644 --- a/fpdfsdk/src/javascript/util.cpp +++ b/fpdfsdk/src/javascript/util.cpp @@ -512,30 +512,6 @@ FX_BOOL util::scand(IJS_Context* cc, return TRUE; } -int64_t FX_atoi64(const char* nptr) { - int c; /* current char */ - int64_t total; /* current total */ - int sign; /* if '-', then negative, otherwise positive */ - - /* skip whitespace */ - while (isspace((int)(unsigned char)*nptr)) - ++nptr; - - c = (int)(unsigned char)*nptr++; - sign = c; /* save sign indication */ - if (c == '-' || c == '+') - c = (int)(unsigned char)*nptr++; /* skip sign */ - - total = 0; - - while (isdigit(c)) { - total = 10 * total + FXSYS_toDecimalDigit(c); /* accumulate digit */ - c = (int)(unsigned char)*nptr++; /* get next char */ - } - - return sign == '-' ? -total : total; -} - FX_BOOL util::byteToChar(IJS_Context* cc, const std::vector& params, CJS_Value& vRet, diff --git a/fpdfsdk/src/javascript/util.h b/fpdfsdk/src/javascript/util.h index 22a8358150..9441b11b13 100644 --- a/fpdfsdk/src/javascript/util.h +++ b/fpdfsdk/src/javascript/util.h @@ -63,6 +63,4 @@ class CJS_Util : public CJS_Object { JS_STATIC_METHOD(byteToChar, util); }; -int64_t FX_atoi64(const char* nptr); - #endif // FPDFSDK_SRC_JAVASCRIPT_UTIL_H_ -- cgit v1.2.3