diff options
author | Dan Sinclair <dsinclair@chromium.org> | 2015-10-29 14:56:26 -0400 |
---|---|---|
committer | Dan Sinclair <dsinclair@chromium.org> | 2015-10-29 14:56:26 -0400 |
commit | 589f7e0a57675efce9810c15a3e9b7c49bf0bc90 (patch) | |
tree | 6839707c05c00f227744ffc3788665cb1cb5b7bd /core/src/fxcrt | |
parent | 6ed0028f1cf12ee53e9e7f657b7ba186c77a42f3 (diff) | |
download | pdfium-589f7e0a57675efce9810c15a3e9b7c49bf0bc90.tar.xz |
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=thestig@chromium.org
Review URL: https://codereview.chromium.org/1415933005 .
Diffstat (limited to 'core/src/fxcrt')
-rw-r--r-- | core/src/fxcrt/fx_basic_bstring.cpp | 4 | ||||
-rw-r--r-- | core/src/fxcrt/fx_basic_gcc.cpp | 11 | ||||
-rw-r--r-- | core/src/fxcrt/fx_basic_util.cpp | 8 | ||||
-rw-r--r-- | core/src/fxcrt/fx_basic_wstring.cpp | 4 | ||||
-rw-r--r-- | core/src/fxcrt/fx_extension.cpp | 9 | ||||
-rw-r--r-- | core/src/fxcrt/fx_extension_unittest.cpp | 14 |
6 files changed, 36 insertions, 14 deletions
diff --git a/core/src/fxcrt/fx_basic_bstring.cpp b/core/src/fxcrt/fx_basic_bstring.cpp index c706912d9d..d5ffcddb6d 100644 --- a/core/src/fxcrt/fx_basic_bstring.cpp +++ b/core/src/fxcrt/fx_basic_bstring.cpp @@ -493,7 +493,7 @@ void CFX_ByteString::FormatV(const FX_CHAR* lpszFormat, va_list argList) { } if (nWidth == 0) { nWidth = FXSYS_atoi(lpsz); - for (; (*lpsz) >= '0' && (*lpsz) <= '9'; lpsz++) + for (; std::isdigit(*lpsz); lpsz++) ; } if (nWidth < 0 || nWidth > 128 * 1024) { @@ -509,7 +509,7 @@ void CFX_ByteString::FormatV(const FX_CHAR* lpszFormat, va_list argList) { lpsz++; } else { nPrecision = FXSYS_atoi(lpsz); - for (; (*lpsz) >= '0' && (*lpsz) <= '9'; lpsz++) + for (; std::isdigit(*lpsz); lpsz++) ; } } diff --git a/core/src/fxcrt/fx_basic_gcc.cpp b/core/src/fxcrt/fx_basic_gcc.cpp index 6f17482156..71f5fe322f 100644 --- a/core/src/fxcrt/fx_basic_gcc.cpp +++ b/core/src/fxcrt/fx_basic_gcc.cpp @@ -12,21 +12,20 @@ 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') { + if (!std::isdigit(*str)) break; - } - if (num > (std::numeric_limits<T>::max() - 9) / 10) { + if (num > (std::numeric_limits<T>::max() - 9) / 10) break; - } + num = num * 10 + (*str) - '0'; str++; } diff --git a/core/src/fxcrt/fx_basic_util.cpp b/core/src/fxcrt/fx_basic_util.cpp index 46a0dec1e5..71119082a2 100644 --- a/core/src/fxcrt/fx_basic_util.cpp +++ b/core/src/fxcrt/fx_basic_util.cpp @@ -101,13 +101,13 @@ void FX_atonum(const CFX_ByteStringC& strc, FX_BOOL& bInteger, void* pData) { cc++; } while (cc < len) { - if (str[cc] < '0' || str[cc] > '9') { + if (!std::isdigit(str[cc])) break; - } + integer = integer * 10 + str[cc] - '0'; - if (integer < 0) { + if (integer < 0) break; - } + cc++; } if (bNegative) { diff --git a/core/src/fxcrt/fx_basic_wstring.cpp b/core/src/fxcrt/fx_basic_wstring.cpp index c097e1fc09..3310df7062 100644 --- a/core/src/fxcrt/fx_basic_wstring.cpp +++ b/core/src/fxcrt/fx_basic_wstring.cpp @@ -765,7 +765,7 @@ 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++) + for (; *lpsz != 0 && std::isdigit(*lpsz); lpsz++) ; } if (nWidth < 0 || nWidth > 128 * 1024) { @@ -781,7 +781,7 @@ 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++) + for (; *lpsz != 0 && std::isdigit(*lpsz); lpsz++) ; } } diff --git a/core/src/fxcrt/fx_extension.cpp b/core/src/fxcrt/fx_extension.cpp index d64a06d08b..a75cfb48df 100644 --- a/core/src/fxcrt/fx_extension.cpp +++ b/core/src/fxcrt/fx_extension.cpp @@ -13,6 +13,15 @@ #include <ctime> #endif +#include <cctype> + +int HexCharToDigit(char c) { + if (!std::isxdigit(c)) + return 0; + char upchar = std::toupper(c); + return upchar > '9' ? upchar - 'A' + 10 : upchar - '0'; +} + IFX_FileStream* FX_CreateFileStream(const FX_CHAR* filename, FX_DWORD dwModes) { IFXCRT_FileAccess* pFA = FXCRT_FileAccess_Create(); if (!pFA) { diff --git a/core/src/fxcrt/fx_extension_unittest.cpp b/core/src/fxcrt/fx_extension_unittest.cpp new file mode 100644 index 0000000000..e2017fc3ed --- /dev/null +++ b/core/src/fxcrt/fx_extension_unittest.cpp @@ -0,0 +1,14 @@ +// 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, HexCharToDigit) { + EXPECT_EQ(10, HexCharToDigit('a')); + EXPECT_EQ(10, HexCharToDigit('A')); + EXPECT_EQ(7, HexCharToDigit('7')); + EXPECT_EQ(0, HexCharToDigit('i')); +} |