Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #include <cctype> | 7 #include <cctype> |
| 8 #include <cwctype> | 8 #include <cwctype> |
| 9 #include <limits> | 9 #include <limits> |
| 10 | 10 |
| 11 #include "core/include/fxcrt/fx_ext.h" | 11 #include "core/include/fxcrt/fx_ext.h" |
| 12 #include "core/include/fxcrt/fx_string.h" | 12 #include "core/include/fxcrt/fx_string.h" |
| 13 | 13 |
| 14 template <class T> | 14 template <class T> |
| 15 T FXSYS_StrToInt(const FX_CHAR* str) { | 15 T FXSYS_StrToInt(const FX_CHAR* str) { |
| 16 FX_BOOL neg = FALSE; | 16 bool neg = false; |
| 17 if (!str) | 17 if (!str) |
| 18 return 0; | 18 return 0; |
| 19 | 19 |
| 20 if (*str == '-') { | 20 if (std::numeric_limits<T>::is_signed && *str == '-') { |
| 21 neg = TRUE; | 21 neg = true; |
| 22 str++; | 22 str++; |
| 23 } | 23 } |
| 24 T num = 0; | 24 T num = 0; |
| 25 while (*str && std::isdigit(*str)) { | 25 while (*str && std::isdigit(*str)) { |
| 26 if (num > (std::numeric_limits<T>::max() - 9) / 10) | 26 T val = FXSYS_toDecimalDigit(*str); |
| 27 if (num > (std::numeric_limits<T>::max() - val) / 10) | |
| 27 break; | 28 break; |
| 28 | 29 |
| 29 num = num * 10 + FXSYS_toDecimalDigit(*str); | 30 num = num * 10 + val; |
| 30 str++; | 31 str++; |
| 31 } | 32 } |
| 32 return neg ? -num : num; | 33 return neg ? -num : num; |
| 33 } | 34 } |
| 34 | 35 |
| 35 template <class T> | 36 template <class T> |
| 36 T FXSYS_StrToInt(const FX_WCHAR* str) { | 37 T FXSYS_StrToInt(const FX_WCHAR* str) { |
| 37 FX_BOOL neg = FALSE; | 38 bool neg = false; |
| 38 if (!str) | 39 if (!str) |
| 39 return 0; | 40 return 0; |
| 40 | 41 |
| 41 if (*str == '-') { | 42 if (std::numeric_limits<T>::is_signed && *str == '-') { |
| 42 neg = TRUE; | 43 neg = true; |
| 43 str++; | 44 str++; |
| 44 } | 45 } |
| 45 T num = 0; | 46 T num = 0; |
| 46 while (*str && std::iswdigit(*str)) { | 47 while (*str && std::iswdigit(*str)) { |
| 47 if (num > (std::numeric_limits<T>::max() - 9) / 10) | 48 T val = FXSYS_toDecimalDigitWide(*str); |
|
Tom Sepez
2016/03/02 21:53:36
If you really want some fun, try overloading FXSYS
dsinclair
2016/03/02 22:09:17
Will do in a separate CL. https://bugs.chromium.or
| |
| 49 if (num > (std::numeric_limits<T>::max() - val) / 10) | |
| 48 break; | 50 break; |
| 49 | 51 |
| 50 num = num * 10 + FXSYS_toDecimalDigitWide(*str); | 52 num = num * 10 + val; |
| 51 str++; | 53 str++; |
| 52 } | 54 } |
| 53 return neg ? -num : num; | 55 return neg ? -num : num; |
| 54 } | 56 } |
| 55 | 57 |
| 56 template <typename T, typename UT, typename STR_T> | 58 template <typename T, typename UT, typename STR_T> |
| 57 STR_T FXSYS_IntToStr(T value, STR_T string, int radix) { | 59 STR_T FXSYS_IntToStr(T value, STR_T string, int radix) { |
| 58 if (radix < 2 || radix > 16) { | 60 if (radix < 2 || radix > 16) { |
| 59 string[0] = 0; | 61 string[0] = 0; |
| 60 return string; | 62 return string; |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 86 string[digits + i] = 0; | 88 string[digits + i] = 0; |
| 87 return string; | 89 return string; |
| 88 } | 90 } |
| 89 | 91 |
| 90 #ifdef __cplusplus | 92 #ifdef __cplusplus |
| 91 extern "C" { | 93 extern "C" { |
| 92 #endif | 94 #endif |
| 93 int32_t FXSYS_atoi(const FX_CHAR* str) { | 95 int32_t FXSYS_atoi(const FX_CHAR* str) { |
| 94 return FXSYS_StrToInt<int32_t>(str); | 96 return FXSYS_StrToInt<int32_t>(str); |
| 95 } | 97 } |
| 98 uint32_t FXSYS_atoui(const FX_CHAR* str) { | |
| 99 return FXSYS_StrToInt<uint32_t>(str); | |
| 100 } | |
| 96 int32_t FXSYS_wtoi(const FX_WCHAR* str) { | 101 int32_t FXSYS_wtoi(const FX_WCHAR* str) { |
| 97 return FXSYS_StrToInt<int32_t>(str); | 102 return FXSYS_StrToInt<int32_t>(str); |
| 98 } | 103 } |
| 99 int64_t FXSYS_atoi64(const FX_CHAR* str) { | 104 int64_t FXSYS_atoi64(const FX_CHAR* str) { |
| 100 return FXSYS_StrToInt<int64_t>(str); | 105 return FXSYS_StrToInt<int64_t>(str); |
| 101 } | 106 } |
| 102 int64_t FXSYS_wtoi64(const FX_WCHAR* str) { | 107 int64_t FXSYS_wtoi64(const FX_WCHAR* str) { |
| 103 return FXSYS_StrToInt<int64_t>(str); | 108 return FXSYS_StrToInt<int64_t>(str); |
| 104 } | 109 } |
| 105 const FX_CHAR* FXSYS_i64toa(int64_t value, FX_CHAR* str, int radix) { | 110 const FX_CHAR* FXSYS_i64toa(int64_t value, FX_CHAR* str, int radix) { |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 251 buf[wlen] = bstr[i]; | 256 buf[wlen] = bstr[i]; |
| 252 } | 257 } |
| 253 wlen++; | 258 wlen++; |
| 254 } | 259 } |
| 255 return wlen; | 260 return wlen; |
| 256 } | 261 } |
| 257 #ifdef __cplusplus | 262 #ifdef __cplusplus |
| 258 } | 263 } |
| 259 #endif | 264 #endif |
| 260 #endif | 265 #endif |
| OLD | NEW |