summaryrefslogtreecommitdiff
path: root/core/src/fxcrt
diff options
context:
space:
mode:
authorDan Sinclair <dsinclair@chromium.org>2015-11-03 14:54:35 -0500
committerDan Sinclair <dsinclair@chromium.org>2015-11-03 14:54:35 -0500
commite0e922db5fb77df9a5a9cc802096f484ed21da1c (patch)
treed6b3da42c1a28ec150c5cb9fe6b00d8f6af099d5 /core/src/fxcrt
parentc9e76c09b9e7901823ac52a3705da235bd2abe24 (diff)
downloadpdfium-e0e922db5fb77df9a5a9cc802096f484ed21da1c.tar.xz
Revert "Revert "Cleanup some numeric code.""
This reverts commit 23d576f0b498bd4f37ef2175916223a2e5ea0324. Cleanup some numeric code. 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/1405253007 .
Diffstat (limited to 'core/src/fxcrt')
-rw-r--r--core/src/fxcrt/fx_basic_bstring.cpp9
-rw-r--r--core/src/fxcrt/fx_basic_gcc.cpp16
-rw-r--r--core/src/fxcrt/fx_basic_util.cpp20
-rw-r--r--core/src/fxcrt/fx_basic_wstring.cpp14
-rw-r--r--core/src/fxcrt/fx_extension.cpp16
-rw-r--r--core/src/fxcrt/fx_extension_unittest.cpp19
-rw-r--r--core/src/fxcrt/fx_xml_parser.cpp9
7 files changed, 59 insertions, 44 deletions
diff --git a/core/src/fxcrt/fx_basic_bstring.cpp b/core/src/fxcrt/fx_basic_bstring.cpp
index c706912d9d..1b9d7a0344 100644
--- a/core/src/fxcrt/fx_basic_bstring.cpp
+++ b/core/src/fxcrt/fx_basic_bstring.cpp
@@ -5,6 +5,7 @@
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
#include <stddef.h> // For offsetof().
+#include <cctype>
#include "../../include/fxcrt/fx_basic.h"
#include "../../../third_party/base/numerics/safe_math.h"
@@ -493,8 +494,8 @@ void CFX_ByteString::FormatV(const FX_CHAR* lpszFormat, va_list argList) {
}
if (nWidth == 0) {
nWidth = FXSYS_atoi(lpsz);
- for (; (*lpsz) >= '0' && (*lpsz) <= '9'; lpsz++)
- ;
+ while (std::isdigit(*lpsz))
+ lpsz++;
}
if (nWidth < 0 || nWidth > 128 * 1024) {
lpszFormat = "Bad width";
@@ -509,8 +510,8 @@ void CFX_ByteString::FormatV(const FX_CHAR* lpszFormat, va_list argList) {
lpsz++;
} else {
nPrecision = FXSYS_atoi(lpsz);
- for (; (*lpsz) >= '0' && (*lpsz) <= '9'; lpsz++)
- ;
+ while (std::isdigit(*lpsz))
+ lpsz++;
}
}
if (nPrecision < 0 || nPrecision > 128 * 1024) {
diff --git a/core/src/fxcrt/fx_basic_gcc.cpp b/core/src/fxcrt/fx_basic_gcc.cpp
index 6f17482156..250079913c 100644
--- a/core/src/fxcrt/fx_basic_gcc.cpp
+++ b/core/src/fxcrt/fx_basic_gcc.cpp
@@ -5,6 +5,7 @@
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
#include <limits>
+#include <cctype>
#include "../../include/fxcrt/fx_ext.h"
#include "../../include/fxcrt/fx_string.h"
@@ -12,22 +13,19 @@
template <class T, class STR_T>
T FXSYS_StrToInt(STR_T str) {
FX_BOOL neg = FALSE;
- if (str == NULL) {
+ if (!str)
return 0;
- }
+
if (*str == '-') {
neg = TRUE;
str++;
}
T num = 0;
- while (*str) {
- if ((*str) < '0' || (*str) > '9') {
+ while (*str && std::isdigit(*str)) {
+ if (num > (std::numeric_limits<T>::max() - 9) / 10)
break;
- }
- if (num > (std::numeric_limits<T>::max() - 9) / 10) {
- break;
- }
- num = num * 10 + (*str) - '0';
+
+ num = num * 10 + FXSYS_toDecimalDigit(*str);
str++;
}
return neg ? -num : num;
diff --git a/core/src/fxcrt/fx_basic_util.cpp b/core/src/fxcrt/fx_basic_util.cpp
index 46a0dec1e5..78bdeee693 100644
--- a/core/src/fxcrt/fx_basic_util.cpp
+++ b/core/src/fxcrt/fx_basic_util.cpp
@@ -5,12 +5,17 @@
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
#include "../../include/fxcrt/fx_basic.h"
+#include "../../include/fxcrt/fx_ext.h"
+
#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
#include <sys/types.h>
#include <dirent.h>
#else
#include <direct.h>
#endif
+
+#include <cctype>
+
CFX_PrivateData::~CFX_PrivateData() {
ClearAll();
}
@@ -100,14 +105,11 @@ void FX_atonum(const CFX_ByteStringC& strc, FX_BOOL& bInteger, void* pData) {
bNegative = TRUE;
cc++;
}
- while (cc < len) {
- if (str[cc] < '0' || str[cc] > '9') {
+ while (cc < len && std::isdigit(str[cc])) {
+ integer = integer * 10 + FXSYS_toDecimalDigit(str[cc]);
+ if (integer < 0)
break;
- }
- integer = integer * 10 + str[cc] - '0';
- if (integer < 0) {
- break;
- }
+
cc++;
}
if (bNegative) {
@@ -144,7 +146,7 @@ FX_FLOAT FX_atof(const CFX_ByteStringC& strc) {
if (str[cc] == '.') {
break;
}
- value = value * 10 + str[cc] - '0';
+ value = value * 10 + FXSYS_toDecimalDigit(str[cc]);
cc++;
}
static const FX_FLOAT fraction_scales[] = {
@@ -155,7 +157,7 @@ FX_FLOAT FX_atof(const CFX_ByteStringC& strc) {
if (cc < len && str[cc] == '.') {
cc++;
while (cc < len) {
- value += fraction_scales[scale] * (str[cc] - '0');
+ value += fraction_scales[scale] * FXSYS_toDecimalDigit(str[cc]);
scale++;
if (scale == sizeof fraction_scales / sizeof(FX_FLOAT)) {
break;
diff --git a/core/src/fxcrt/fx_basic_wstring.cpp b/core/src/fxcrt/fx_basic_wstring.cpp
index c097e1fc09..cb94d81196 100644
--- a/core/src/fxcrt/fx_basic_wstring.cpp
+++ b/core/src/fxcrt/fx_basic_wstring.cpp
@@ -5,8 +5,10 @@
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
#include <stddef.h> // For offsetof().
+#include <cctype>
#include "../../include/fxcrt/fx_basic.h"
+#include "../../include/fxcrt/fx_ext.h"
#include "../../../third_party/base/numerics/safe_math.h"
// static
@@ -765,8 +767,8 @@ void CFX_WideString::FormatV(const FX_WCHAR* lpszFormat, va_list argList) {
}
if (nWidth == 0) {
nWidth = FXSYS_wtoi(lpsz);
- for (; *lpsz != 0 && (*lpsz) <= '9' && (*lpsz) >= '0'; lpsz++)
- ;
+ while (std::isdigit(*lpsz))
+ ++lpsz;
}
if (nWidth < 0 || nWidth > 128 * 1024) {
lpszFormat = L"Bad width";
@@ -781,8 +783,8 @@ void CFX_WideString::FormatV(const FX_WCHAR* lpszFormat, va_list argList) {
lpsz++;
} else {
nPrecision = FXSYS_wtoi(lpsz);
- for (; *lpsz != 0 && (*lpsz) >= '0' && (*lpsz) <= '9'; lpsz++)
- ;
+ while (std::isdigit(*lpsz))
+ ++lpsz;
}
}
if (nPrecision < 0 || nPrecision > 128 * 1024) {
@@ -968,7 +970,7 @@ FX_FLOAT FX_wtof(const FX_WCHAR* str, int len) {
if (str[cc] == '.') {
break;
}
- integer = integer * 10 + str[cc] - '0';
+ integer = integer * 10 + FXSYS_toDecimalDigit(str[cc]);
cc++;
}
FX_FLOAT fraction = 0;
@@ -976,7 +978,7 @@ FX_FLOAT FX_wtof(const FX_WCHAR* str, int len) {
cc++;
FX_FLOAT scale = 0.1f;
while (cc < len) {
- fraction += scale * (str[cc] - '0');
+ fraction += scale * FXSYS_toDecimalDigit(str[cc]);
scale *= 0.1f;
cc++;
}
diff --git a/core/src/fxcrt/fx_extension.cpp b/core/src/fxcrt/fx_extension.cpp
index d64a06d08b..4951fbde9c 100644
--- a/core/src/fxcrt/fx_extension.cpp
+++ b/core/src/fxcrt/fx_extension.cpp
@@ -7,6 +7,7 @@
#include "../../include/fxcrt/fx_basic.h"
#include "../../include/fxcrt/fx_ext.h"
#include "extension.h"
+
#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
#include <wincrypt.h>
#else
@@ -50,9 +51,7 @@ IFX_MemoryStream* FX_CreateMemoryStream(uint8_t* pBuffer,
IFX_MemoryStream* FX_CreateMemoryStream(FX_BOOL bConsecutive) {
return new CFX_MemoryStream(bConsecutive);
}
-#ifdef __cplusplus
-extern "C" {
-#endif
+
FX_FLOAT FXSYS_tan(FX_FLOAT a) {
return (FX_FLOAT)tan(a);
}
@@ -189,12 +188,7 @@ FX_DWORD FX_HashCode_String_GetW(const FX_WCHAR* pStr,
}
return dwHashCode;
}
-#ifdef __cplusplus
-}
-#endif
-#ifdef __cplusplus
-extern "C" {
-#endif
+
void* FX_Random_MT_Start(FX_DWORD dwSeed) {
FX_LPMTRANDOMCONTEXT pContext = FX_Alloc(FX_MTRANDOMCONTEXT, 1);
pContext->mt[0] = dwSeed;
@@ -297,6 +291,4 @@ void FX_Random_GenerateCrypto(FX_DWORD* pBuffer, int32_t iCount) {
FX_Random_GenerateBase(pBuffer, iCount);
#endif
}
-#ifdef __cplusplus
-}
-#endif
+
diff --git a/core/src/fxcrt/fx_extension_unittest.cpp b/core/src/fxcrt/fx_extension_unittest.cpp
new file mode 100644
index 0000000000..2cadbbfede
--- /dev/null
+++ b/core/src/fxcrt/fx_extension_unittest.cpp
@@ -0,0 +1,19 @@
+// Copyright 2015 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "testing/gtest/include/gtest/gtest.h"
+
+#include "../../include/fxcrt/fx_ext.h"
+
+TEST(fxcrt, FXSYS_toHexDigit) {
+ EXPECT_EQ(10, FXSYS_toHexDigit('a'));
+ EXPECT_EQ(10, FXSYS_toHexDigit('A'));
+ EXPECT_EQ(7, FXSYS_toHexDigit('7'));
+ EXPECT_EQ(0, FXSYS_toHexDigit('i'));
+}
+
+TEST(fxcrt, FXSYS_toDecimalDigit) {
+ EXPECT_EQ(7, FXSYS_toDecimalDigit('7'));
+ EXPECT_EQ(0, FXSYS_toDecimalDigit('a'));
+}
diff --git a/core/src/fxcrt/fx_xml_parser.cpp b/core/src/fxcrt/fx_xml_parser.cpp
index dc59ded2b7..845fd84cfd 100644
--- a/core/src/fxcrt/fx_xml_parser.cpp
+++ b/core/src/fxcrt/fx_xml_parser.cpp
@@ -5,7 +5,9 @@
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
#include "../../include/fxcrt/fx_xml.h"
+#include "../../include/fxcrt/fx_ext.h"
#include "xml_int.h"
+
CXML_Parser::~CXML_Parser() {
if (m_bOwnedStream) {
m_pDataAcc->Release();
@@ -226,9 +228,8 @@ FX_DWORD CXML_Parser::GetCharRef() {
iState = 10;
break;
}
- if (g_FXCRT_XML_IsDigital(ch)) {
- code = code * 10 + ch - '0';
- }
+ if (g_FXCRT_XML_IsDigital(ch))
+ code = code * 10 + FXSYS_toDecimalDigit(ch);
break;
case 4:
m_dwIndex++;
@@ -240,7 +241,7 @@ 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) + ch - '0';
+ code = (code << 4) + FXSYS_toDecimalDigit(ch);
} else if (nHex == FXCRTM_XML_CHARTYPE_HexLowerLetter) {
code = (code << 4) + ch - 87;
} else {