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 <limits> | 7 #include <limits> |
| 8 #include <cctype> |
| 9 #include <cwctype> |
8 | 10 |
9 #include "core/include/fxcrt/fx_ext.h" | 11 #include "core/include/fxcrt/fx_ext.h" |
10 #include "core/include/fxcrt/fx_string.h" | 12 #include "core/include/fxcrt/fx_string.h" |
11 | 13 |
12 template <class T, class STR_T> | 14 template <class T> |
13 T FXSYS_StrToInt(STR_T str) { | 15 T FXSYS_StrToInt(const FX_CHAR* str) { |
14 FX_BOOL neg = FALSE; | 16 FX_BOOL neg = FALSE; |
15 if (str == NULL) { | 17 if (!str) |
16 return 0; | 18 return 0; |
17 } | 19 |
18 if (*str == '-') { | 20 if (*str == '-') { |
19 neg = TRUE; | 21 neg = TRUE; |
20 str++; | 22 str++; |
21 } | 23 } |
22 T num = 0; | 24 T num = 0; |
23 while (*str) { | 25 while (*str && std::isdigit(*str)) { |
24 if ((*str) < '0' || (*str) > '9') { | 26 if (num > (std::numeric_limits<T>::max() - 9) / 10) |
25 break; | 27 break; |
26 } | 28 |
27 if (num > (std::numeric_limits<T>::max() - 9) / 10) { | 29 num = num * 10 + FXSYS_toDecimalDigit(*str); |
28 break; | |
29 } | |
30 num = num * 10 + (*str) - '0'; | |
31 str++; | 30 str++; |
32 } | 31 } |
33 return neg ? -num : num; | 32 return neg ? -num : num; |
| 33 } |
| 34 |
| 35 template <class T> |
| 36 T FXSYS_StrToInt(const FX_WCHAR* str) { |
| 37 FX_BOOL neg = FALSE; |
| 38 if (!str) |
| 39 return 0; |
| 40 |
| 41 if (*str == '-') { |
| 42 neg = TRUE; |
| 43 str++; |
| 44 } |
| 45 T num = 0; |
| 46 while (*str && std::iswdigit(*str)) { |
| 47 if (num > (std::numeric_limits<T>::max() - 9) / 10) |
| 48 break; |
| 49 |
| 50 num = num * 10 + FXSYS_toDecimalDigitWide(*str); |
| 51 str++; |
| 52 } |
| 53 return neg ? -num : num; |
34 } | 54 } |
35 | 55 |
36 template <typename T, typename UT, typename STR_T> | 56 template <typename T, typename UT, typename STR_T> |
37 STR_T FXSYS_IntToStr(T value, STR_T string, int radix) { | 57 STR_T FXSYS_IntToStr(T value, STR_T string, int radix) { |
38 if (radix < 2 || radix > 16) { | 58 if (radix < 2 || radix > 16) { |
39 string[0] = 0; | 59 string[0] = 0; |
40 return string; | 60 return string; |
41 } | 61 } |
42 if (value == 0) { | 62 if (value == 0) { |
43 string[0] = '0'; | 63 string[0] = '0'; |
(...skipping 20 matching lines...) Expand all Loading... |
64 uvalue /= radix; | 84 uvalue /= radix; |
65 } | 85 } |
66 string[digits + i] = 0; | 86 string[digits + i] = 0; |
67 return string; | 87 return string; |
68 } | 88 } |
69 | 89 |
70 #ifdef __cplusplus | 90 #ifdef __cplusplus |
71 extern "C" { | 91 extern "C" { |
72 #endif | 92 #endif |
73 int32_t FXSYS_atoi(const FX_CHAR* str) { | 93 int32_t FXSYS_atoi(const FX_CHAR* str) { |
74 return FXSYS_StrToInt<int32_t, const FX_CHAR*>(str); | 94 return FXSYS_StrToInt<int32_t>(str); |
75 } | 95 } |
76 int32_t FXSYS_wtoi(const FX_WCHAR* str) { | 96 int32_t FXSYS_wtoi(const FX_WCHAR* str) { |
77 return FXSYS_StrToInt<int32_t, const FX_WCHAR*>(str); | 97 return FXSYS_StrToInt<int32_t>(str); |
78 } | 98 } |
79 int64_t FXSYS_atoi64(const FX_CHAR* str) { | 99 int64_t FXSYS_atoi64(const FX_CHAR* str) { |
80 return FXSYS_StrToInt<int64_t, const FX_CHAR*>(str); | 100 return FXSYS_StrToInt<int64_t>(str); |
81 } | 101 } |
82 int64_t FXSYS_wtoi64(const FX_WCHAR* str) { | 102 int64_t FXSYS_wtoi64(const FX_WCHAR* str) { |
83 return FXSYS_StrToInt<int64_t, const FX_WCHAR*>(str); | 103 return FXSYS_StrToInt<int64_t>(str); |
84 } | 104 } |
85 const FX_CHAR* FXSYS_i64toa(int64_t value, FX_CHAR* str, int radix) { | 105 const FX_CHAR* FXSYS_i64toa(int64_t value, FX_CHAR* str, int radix) { |
86 return FXSYS_IntToStr<int64_t, uint64_t, FX_CHAR*>(value, str, radix); | 106 return FXSYS_IntToStr<int64_t, uint64_t, FX_CHAR*>(value, str, radix); |
87 } | 107 } |
88 #ifdef __cplusplus | 108 #ifdef __cplusplus |
89 } | 109 } |
90 #endif | 110 #endif |
91 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ | 111 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ |
92 #ifdef __cplusplus | 112 #ifdef __cplusplus |
93 extern "C" { | 113 extern "C" { |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 buf[wlen] = bstr[i]; | 251 buf[wlen] = bstr[i]; |
232 } | 252 } |
233 wlen++; | 253 wlen++; |
234 } | 254 } |
235 return wlen; | 255 return wlen; |
236 } | 256 } |
237 #ifdef __cplusplus | 257 #ifdef __cplusplus |
238 } | 258 } |
239 #endif | 259 #endif |
240 #endif | 260 #endif |
OLD | NEW |