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

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; 16 bool neg = false;
Tom Sepez 2016/03/03 19:26:47 nit: move to line 20. maybe bool neg = std::numeri
dsinclair 2016/03/03 20:06:50 Done.
17 if (!str) 17 if (!str)
18 return 0; 18 return 0;
19 19
20 if (std::numeric_limits<T>::is_signed && *str == '-') { 20 if (std::numeric_limits<IntType>::is_signed && *str == '-') {
21 neg = true; 21 neg = true;
22 str++; 22 str++;
23 } 23 }
24 T num = 0; 24
25 while (*str && std::isdigit(*str)) { 25 IntType num = 0;
26 T val = FXSYS_toDecimalDigit(*str); 26 while (*str && FXSYS_isDecimalDigit(*str)) {
27 if (num > (std::numeric_limits<T>::max() - val) / 10) 27 IntType val = FXSYS_toDecimalDigit(*str);
28 if (num > (std::numeric_limits<IntType>::max() - val) / 10)
28 break; 29 break;
29 30
30 num = num * 10 + val; 31 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++; 32 str++;
54 } 33 }
55 return neg ? -num : num; 34 return neg ? -num : num;
56 } 35 }
57 36
58 template <typename T, typename UT, typename STR_T> 37 template <typename T, typename UT, typename STR_T>
59 STR_T FXSYS_IntToStr(T value, STR_T string, int radix) { 38 STR_T FXSYS_IntToStr(T value, STR_T string, int radix) {
60 if (radix < 2 || radix > 16) { 39 if (radix < 2 || radix > 16) {
61 string[0] = 0; 40 string[0] = 0;
62 return string; 41 return string;
(...skipping 23 matching lines...) Expand all
86 uvalue /= radix; 65 uvalue /= radix;
87 } 66 }
88 string[digits + i] = 0; 67 string[digits + i] = 0;
89 return string; 68 return string;
90 } 69 }
91 70
92 #ifdef __cplusplus 71 #ifdef __cplusplus
93 extern "C" { 72 extern "C" {
94 #endif 73 #endif
95 int32_t FXSYS_atoi(const FX_CHAR* str) { 74 int32_t FXSYS_atoi(const FX_CHAR* str) {
96 return FXSYS_StrToInt<int32_t>(str); 75 return FXSYS_StrToInt<int32_t, FX_CHAR>(str);
97 } 76 }
98 uint32_t FXSYS_atoui(const FX_CHAR* str) { 77 uint32_t FXSYS_atoui(const FX_CHAR* str) {
99 return FXSYS_StrToInt<uint32_t>(str); 78 return FXSYS_StrToInt<uint32_t>(str);
100 } 79 }
101 int32_t FXSYS_wtoi(const FX_WCHAR* str) { 80 int32_t FXSYS_wtoi(const FX_WCHAR* str) {
102 return FXSYS_StrToInt<int32_t>(str); 81 return FXSYS_StrToInt<int32_t, FX_WCHAR>(str);
103 } 82 }
104 int64_t FXSYS_atoi64(const FX_CHAR* str) { 83 int64_t FXSYS_atoi64(const FX_CHAR* str) {
105 return FXSYS_StrToInt<int64_t>(str); 84 return FXSYS_StrToInt<int64_t, FX_CHAR>(str);
106 } 85 }
107 int64_t FXSYS_wtoi64(const FX_WCHAR* str) { 86 int64_t FXSYS_wtoi64(const FX_WCHAR* str) {
108 return FXSYS_StrToInt<int64_t>(str); 87 return FXSYS_StrToInt<int64_t, FX_WCHAR>(str);
109 } 88 }
110 const FX_CHAR* FXSYS_i64toa(int64_t value, FX_CHAR* str, int radix) { 89 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); 90 return FXSYS_IntToStr<int64_t, uint64_t, FX_CHAR*>(value, str, radix);
112 } 91 }
113 #ifdef __cplusplus 92 #ifdef __cplusplus
114 } 93 }
115 #endif 94 #endif
116 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ 95 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
117 #ifdef __cplusplus 96 #ifdef __cplusplus
118 extern "C" { 97 extern "C" {
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 buf[wlen] = bstr[i]; 235 buf[wlen] = bstr[i];
257 } 236 }
258 wlen++; 237 wlen++;
259 } 238 }
260 return wlen; 239 return wlen;
261 } 240 }
262 #ifdef __cplusplus 241 #ifdef __cplusplus
263 } 242 }
264 #endif 243 #endif
265 #endif 244 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698