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 <typename IntType, typename CharType> | 14 template <typename IntType, typename CharType> |
15 IntType FXSYS_StrToInt(const CharType* str) { | 15 IntType FXSYS_StrToInt(const CharType* str) { |
16 if (!str) | 16 if (!str) |
17 return 0; | 17 return 0; |
18 | 18 |
19 bool neg = std::numeric_limits<IntType>::is_signed && *str == '-'; | 19 // Process the sign. |
20 if (neg) | 20 bool neg = *str == '-'; |
| 21 if (neg || *str == '+') |
21 str++; | 22 str++; |
22 | 23 |
23 IntType num = 0; | 24 IntType num = 0; |
24 while (*str && FXSYS_isDecimalDigit(*str)) { | 25 while (*str && FXSYS_isDecimalDigit(*str)) { |
25 IntType val = FXSYS_toDecimalDigit(*str); | 26 IntType val = FXSYS_toDecimalDigit(*str); |
26 if (num > (std::numeric_limits<IntType>::max() - val) / 10) | 27 if (num > (std::numeric_limits<IntType>::max() - val) / 10) { |
27 break; | 28 if (neg && std::numeric_limits<IntType>::is_signed) { |
| 29 // Return MIN when the represented number is signed type and is smaller |
| 30 // than the min value. |
| 31 return std::numeric_limits<IntType>::min(); |
| 32 } else { |
| 33 // Return MAX when the represented number is signed type and is larger |
| 34 // than the max value, or the number is unsigned type and out of range. |
| 35 return std::numeric_limits<IntType>::max(); |
| 36 } |
| 37 } |
28 | 38 |
29 num = num * 10 + val; | 39 num = num * 10 + val; |
30 str++; | 40 str++; |
31 } | 41 } |
32 return neg ? -num : num; | 42 return neg ? -num : num; |
33 } | 43 } |
34 | 44 |
35 template <typename T, typename UT, typename STR_T> | 45 template <typename T, typename UT, typename STR_T> |
36 STR_T FXSYS_IntToStr(T value, STR_T str, int radix) { | 46 STR_T FXSYS_IntToStr(T value, STR_T str, int radix) { |
37 if (radix < 2 || radix > 16) { | 47 if (radix < 2 || radix > 16) { |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 buf[wlen] = bstr[i]; | 243 buf[wlen] = bstr[i]; |
234 } | 244 } |
235 wlen++; | 245 wlen++; |
236 } | 246 } |
237 return wlen; | 247 return wlen; |
238 } | 248 } |
239 #ifdef __cplusplus | 249 #ifdef __cplusplus |
240 } | 250 } |
241 #endif | 251 #endif |
242 #endif | 252 #endif |
OLD | NEW |