Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1336)

Side by Side Diff: core/src/fxcrt/fx_basic_gcc.cpp

Issue 1757283002: Combine StrToInt methods. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <typename IntType, typename CharType>
15 T FXSYS_StrToInt(const FX_CHAR* str) { 15 IntType FXSYS_StrToInt(const CharType* str) {
16 bool neg = false;
17 if (!str) 16 if (!str)
18 return 0; 17 return 0;
19 18
20 if (std::numeric_limits<T>::is_signed && *str == '-') { 19 bool neg = std::numeric_limits<IntType>::is_signed && *str == '-';
21 neg = true; 20 if (neg)
22 str++; 21 str++;
23 } 22
24 T num = 0; 23 IntType num = 0;
25 while (*str && std::isdigit(*str)) { 24 while (*str && FXSYS_isDecimalDigit(*str)) {
26 T val = FXSYS_toDecimalDigit(*str); 25 IntType val = FXSYS_toDecimalDigit(*str);
27 if (num > (std::numeric_limits<T>::max() - val) / 10) 26 if (num > (std::numeric_limits<IntType>::max() - val) / 10)
28 break; 27 break;
29 28
30 num = num * 10 + val; 29 num = num * 10 + val;
31 str++;
32 }
33 return neg ? -num : num;
34 }
35
36 template <class T>
37 T FXSYS_StrToInt(const FX_WCHAR* str) {
38 bool neg = false;
39 if (!str)
40 return 0;
41
42 if (std::numeric_limits<T>::is_signed && *str == '-') {
43 neg = true;
44 str++;
45 }
46 T num = 0;
47 while (*str && std::iswdigit(*str)) {
48 T val = FXSYS_toDecimalDigitWide(*str);
49 if (num > (std::numeric_limits<T>::max() - val) / 10)
50 break;
51
52 num = num * 10 + val;
53 str++; 30 str++;
54 } 31 }
55 return neg ? -num : num; 32 return neg ? -num : num;
56 } 33 }
57 34
58 template <typename T, typename UT, typename STR_T> 35 template <typename T, typename UT, typename STR_T>
59 STR_T FXSYS_IntToStr(T value, STR_T string, int radix) { 36 STR_T FXSYS_IntToStr(T value, STR_T string, int radix) {
60 if (radix < 2 || radix > 16) { 37 if (radix < 2 || radix > 16) {
61 string[0] = 0; 38 string[0] = 0;
62 return string; 39 return string;
(...skipping 23 matching lines...) Expand all
86 uvalue /= radix; 63 uvalue /= radix;
87 } 64 }
88 string[digits + i] = 0; 65 string[digits + i] = 0;
89 return string; 66 return string;
90 } 67 }
91 68
92 #ifdef __cplusplus 69 #ifdef __cplusplus
93 extern "C" { 70 extern "C" {
94 #endif 71 #endif
95 int32_t FXSYS_atoi(const FX_CHAR* str) { 72 int32_t FXSYS_atoi(const FX_CHAR* str) {
96 return FXSYS_StrToInt<int32_t>(str); 73 return FXSYS_StrToInt<int32_t, FX_CHAR>(str);
97 } 74 }
98 uint32_t FXSYS_atoui(const FX_CHAR* str) { 75 uint32_t FXSYS_atoui(const FX_CHAR* str) {
99 return FXSYS_StrToInt<uint32_t>(str); 76 return FXSYS_StrToInt<uint32_t>(str);
100 } 77 }
101 int32_t FXSYS_wtoi(const FX_WCHAR* str) { 78 int32_t FXSYS_wtoi(const FX_WCHAR* str) {
102 return FXSYS_StrToInt<int32_t>(str); 79 return FXSYS_StrToInt<int32_t, FX_WCHAR>(str);
103 } 80 }
104 int64_t FXSYS_atoi64(const FX_CHAR* str) { 81 int64_t FXSYS_atoi64(const FX_CHAR* str) {
105 return FXSYS_StrToInt<int64_t>(str); 82 return FXSYS_StrToInt<int64_t, FX_CHAR>(str);
106 } 83 }
107 int64_t FXSYS_wtoi64(const FX_WCHAR* str) { 84 int64_t FXSYS_wtoi64(const FX_WCHAR* str) {
108 return FXSYS_StrToInt<int64_t>(str); 85 return FXSYS_StrToInt<int64_t, FX_WCHAR>(str);
109 } 86 }
110 const FX_CHAR* FXSYS_i64toa(int64_t value, FX_CHAR* str, int radix) { 87 const FX_CHAR* FXSYS_i64toa(int64_t value, FX_CHAR* str, int radix) {
111 return FXSYS_IntToStr<int64_t, uint64_t, FX_CHAR*>(value, str, radix); 88 return FXSYS_IntToStr<int64_t, uint64_t, FX_CHAR*>(value, str, radix);
112 } 89 }
113 #ifdef __cplusplus 90 #ifdef __cplusplus
114 } 91 }
115 #endif 92 #endif
116 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ 93 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
117 #ifdef __cplusplus 94 #ifdef __cplusplus
118 extern "C" { 95 extern "C" {
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 buf[wlen] = bstr[i]; 233 buf[wlen] = bstr[i];
257 } 234 }
258 wlen++; 235 wlen++;
259 } 236 }
260 return wlen; 237 return wlen;
261 } 238 }
262 #ifdef __cplusplus 239 #ifdef __cplusplus
263 } 240 }
264 #endif 241 #endif
265 #endif 242 #endif
OLDNEW
« no previous file with comments | « core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp ('k') | core/src/fxcrt/fx_basic_wstring.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698