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 | 8 |
9 #include "../../include/fxcrt/fx_ext.h" | 9 #include "../../include/fxcrt/fx_ext.h" |
10 #include "../../include/fxcrt/fx_string.h" | 10 #include "../../include/fxcrt/fx_string.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 break; | 25 break; |
26 } | 26 } |
27 if (num > (std::numeric_limits<T>::max() - 9) / 10) { | 27 if (num > (std::numeric_limits<T>::max() - 9) / 10) { |
28 break; | 28 break; |
29 } | 29 } |
30 num = num * 10 + (*str) - '0'; | 30 num = num * 10 + (*str) - '0'; |
31 str++; | 31 str++; |
32 } | 32 } |
33 return neg ? -num : num; | 33 return neg ? -num : num; |
34 } | 34 } |
35 template <typename T, typename STR_T> | 35 |
| 36 template <typename T, typename UT, typename STR_T> |
36 STR_T FXSYS_IntToStr(T value, STR_T string, int radix) { | 37 STR_T FXSYS_IntToStr(T value, STR_T string, int radix) { |
37 int i = 0; | 38 if (radix < 2 || radix > 16) { |
38 if (value < 0) { | 39 string[0] = 0; |
39 string[i++] = '-'; | 40 return string; |
40 value = -value; | 41 } |
41 } else if (value == 0) { | 42 if (value == 0) { |
42 string[0] = '0'; | 43 string[0] = '0'; |
43 string[1] = 0; | 44 string[1] = 0; |
44 return string; | 45 return string; |
45 } | 46 } |
| 47 int i = 0; |
| 48 UT uvalue; |
| 49 if (value < 0) { |
| 50 string[i++] = '-'; |
| 51 // Standard trick to avoid undefined behaviour when negating INT_MIN. |
| 52 uvalue = static_cast<UT>(-(value + 1)) + 1; |
| 53 } else { |
| 54 uvalue = value; |
| 55 } |
46 int digits = 1; | 56 int digits = 1; |
47 T order = value / 10; | 57 T order = uvalue / radix; |
48 while (order > 0) { | 58 while (order > 0) { |
49 digits++; | 59 digits++; |
50 order = order / 10; | 60 order = order / radix; |
51 } | 61 } |
52 for (int d = digits - 1; d > -1; d--) { | 62 for (int d = digits - 1; d > -1; d--) { |
53 string[d + i] = "0123456789abcdef"[value % 10]; | 63 string[d + i] = "0123456789abcdef"[uvalue % radix]; |
54 value /= 10; | 64 uvalue /= radix; |
55 } | 65 } |
56 string[digits + i] = 0; | 66 string[digits + i] = 0; |
57 return string; | 67 return string; |
58 } | 68 } |
| 69 |
59 #ifdef __cplusplus | 70 #ifdef __cplusplus |
60 extern "C" { | 71 extern "C" { |
61 #endif | 72 #endif |
62 int32_t FXSYS_atoi(const FX_CHAR* str) { | 73 int32_t FXSYS_atoi(const FX_CHAR* str) { |
63 return FXSYS_StrToInt<int32_t, const FX_CHAR*>(str); | 74 return FXSYS_StrToInt<int32_t, const FX_CHAR*>(str); |
64 } | 75 } |
65 int32_t FXSYS_wtoi(const FX_WCHAR* str) { | 76 int32_t FXSYS_wtoi(const FX_WCHAR* str) { |
66 return FXSYS_StrToInt<int32_t, const FX_WCHAR*>(str); | 77 return FXSYS_StrToInt<int32_t, const FX_WCHAR*>(str); |
67 } | 78 } |
68 int64_t FXSYS_atoi64(const FX_CHAR* str) { | 79 int64_t FXSYS_atoi64(const FX_CHAR* str) { |
69 return FXSYS_StrToInt<int64_t, const FX_CHAR*>(str); | 80 return FXSYS_StrToInt<int64_t, const FX_CHAR*>(str); |
70 } | 81 } |
71 int64_t FXSYS_wtoi64(const FX_WCHAR* str) { | 82 int64_t FXSYS_wtoi64(const FX_WCHAR* str) { |
72 return FXSYS_StrToInt<int64_t, const FX_WCHAR*>(str); | 83 return FXSYS_StrToInt<int64_t, const FX_WCHAR*>(str); |
73 } | 84 } |
74 const FX_CHAR* FXSYS_i64toa(int64_t value, FX_CHAR* str, int radix) { | 85 const FX_CHAR* FXSYS_i64toa(int64_t value, FX_CHAR* str, int radix) { |
75 return FXSYS_IntToStr<int64_t, FX_CHAR*>(value, str, radix); | 86 return FXSYS_IntToStr<int64_t, uint64_t, FX_CHAR*>(value, str, radix); |
76 } | |
77 const FX_WCHAR* FXSYS_i64tow(int64_t value, FX_WCHAR* str, int radix) { | |
78 return FXSYS_IntToStr<int64_t, FX_WCHAR*>(value, str, radix); | |
79 } | 87 } |
80 #ifdef __cplusplus | 88 #ifdef __cplusplus |
81 } | 89 } |
82 #endif | 90 #endif |
83 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ | 91 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ |
84 #ifdef __cplusplus | 92 #ifdef __cplusplus |
85 extern "C" { | 93 extern "C" { |
86 #endif | 94 #endif |
87 int FXSYS_GetACP() { | 95 int FXSYS_GetACP() { |
88 return 0; | 96 return 0; |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 if (((f = (FX_WCHAR)(*(dst++))) >= 'A') && (f <= 'Z')) { | 183 if (((f = (FX_WCHAR)(*(dst++))) >= 'A') && (f <= 'Z')) { |
176 f -= ('A' - 'a'); | 184 f -= ('A' - 'a'); |
177 } | 185 } |
178 if (((l = (FX_WCHAR)(*(src++))) >= 'A') && (l <= 'Z')) { | 186 if (((l = (FX_WCHAR)(*(src++))) >= 'A') && (l <= 'Z')) { |
179 l -= ('A' - 'a'); | 187 l -= ('A' - 'a'); |
180 } | 188 } |
181 } while (f && (f == l)); | 189 } while (f && (f == l)); |
182 return (f - l); | 190 return (f - l); |
183 } | 191 } |
184 char* FXSYS_itoa(int value, char* string, int radix) { | 192 char* FXSYS_itoa(int value, char* string, int radix) { |
185 return FXSYS_IntToStr<int32_t, FX_CHAR*>(value, string, radix); | 193 return FXSYS_IntToStr<int32_t, uint32_t, FX_CHAR*>(value, string, radix); |
186 } | 194 } |
187 #ifdef __cplusplus | 195 #ifdef __cplusplus |
188 } | 196 } |
189 #endif | 197 #endif |
190 #endif | 198 #endif |
191 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ | 199 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ |
192 #ifdef __cplusplus | 200 #ifdef __cplusplus |
193 extern "C" { | 201 extern "C" { |
194 #endif | 202 #endif |
195 int FXSYS_WideCharToMultiByte(FX_DWORD codepage, | 203 int FXSYS_WideCharToMultiByte(FX_DWORD codepage, |
(...skipping 27 matching lines...) Expand all Loading... |
223 buf[wlen] = bstr[i]; | 231 buf[wlen] = bstr[i]; |
224 } | 232 } |
225 wlen++; | 233 wlen++; |
226 } | 234 } |
227 return wlen; | 235 return wlen; |
228 } | 236 } |
229 #ifdef __cplusplus | 237 #ifdef __cplusplus |
230 } | 238 } |
231 #endif | 239 #endif |
232 #endif | 240 #endif |
OLD | NEW |