From 589f7e0a57675efce9810c15a3e9b7c49bf0bc90 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Thu, 29 Oct 2015 14:56:26 -0400 Subject: 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 . --- core/src/fxcrt/fx_basic_bstring.cpp | 4 ++-- core/src/fxcrt/fx_basic_gcc.cpp | 11 +++++------ core/src/fxcrt/fx_basic_util.cpp | 8 ++++---- core/src/fxcrt/fx_basic_wstring.cpp | 4 ++-- core/src/fxcrt/fx_extension.cpp | 9 +++++++++ core/src/fxcrt/fx_extension_unittest.cpp | 14 ++++++++++++++ 6 files changed, 36 insertions(+), 14 deletions(-) create mode 100644 core/src/fxcrt/fx_extension_unittest.cpp (limited to 'core/src/fxcrt') 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 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::max() - 9) / 10) { + if (num > (std::numeric_limits::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 #endif +#include + +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')); +} -- cgit v1.2.3