Chromium Code Reviews| Index: core/src/fxcrt/fx_basic_gcc.cpp |
| diff --git a/core/src/fxcrt/fx_basic_gcc.cpp b/core/src/fxcrt/fx_basic_gcc.cpp |
| index 6f17482156be7436f0e53e55243b2e1a63548202..220fa4d45928c9ada8d7e3cc056f76e2e42350b7 100644 |
| --- a/core/src/fxcrt/fx_basic_gcc.cpp |
| +++ b/core/src/fxcrt/fx_basic_gcc.cpp |
| @@ -5,29 +5,49 @@ |
| // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| #include <limits> |
| +#include <cctype> |
| +#include <cwctype> |
| #include "../../include/fxcrt/fx_ext.h" |
| #include "../../include/fxcrt/fx_string.h" |
| -template <class T, class STR_T> |
| -T FXSYS_StrToInt(STR_T str) { |
| +template <class T> |
| +T FXSYS_StrToInt(const FX_CHAR* 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) { |
| + |
| + num = num * 10 + FXSYS_toDecimalDigit(*str); |
| + str++; |
| + } |
| + return neg ? -num : num; |
| +} |
| + |
| +template <class T> |
|
Tom Sepez
2015/11/04 18:42:45
If you wanted to get really sneaky, FXSYS_toDecima
dsinclair
2015/11/04 20:16:24
Ack. I'd prefer to stick with dumb, as I'll rememb
|
| +T FXSYS_StrToInt(const FX_WCHAR* str) { |
| + FX_BOOL neg = FALSE; |
| + if (!str) |
| + return 0; |
| + |
| + if (*str == '-') { |
| + neg = TRUE; |
| + str++; |
| + } |
| + T num = 0; |
| + while (*str && std::iswdigit(*str)) { |
| + if (num > (std::numeric_limits<T>::max() - 9) / 10) |
| break; |
| - } |
| - num = num * 10 + (*str) - '0'; |
| + |
| + num = num * 10 + FXSYS_toDecimalDigitWide(*str); |
| str++; |
| } |
| return neg ? -num : num; |
| @@ -71,16 +91,16 @@ 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<int32_t, const FX_CHAR*>(str); |
| + return FXSYS_StrToInt<int32_t>(str); |
| } |
| int32_t FXSYS_wtoi(const FX_WCHAR* str) { |
| - return FXSYS_StrToInt<int32_t, const FX_WCHAR*>(str); |
| + return FXSYS_StrToInt<int32_t>(str); |
| } |
| int64_t FXSYS_atoi64(const FX_CHAR* str) { |
| - return FXSYS_StrToInt<int64_t, const FX_CHAR*>(str); |
| + return FXSYS_StrToInt<int64_t>(str); |
| } |
| int64_t FXSYS_wtoi64(const FX_WCHAR* str) { |
| - return FXSYS_StrToInt<int64_t, const FX_WCHAR*>(str); |
| + return FXSYS_StrToInt<int64_t>(str); |
| } |
| const FX_CHAR* FXSYS_i64toa(int64_t value, FX_CHAR* str, int radix) { |
| return FXSYS_IntToStr<int64_t, uint64_t, FX_CHAR*>(value, str, radix); |