@@ -93,46 +93,49 @@ constexpr bool isIntegralDigit(char ch, int base)
93
93
if (IS_NUM (ch))
94
94
return ch - ' 0' < base;
95
95
if (IS_ALPHA (ch))
96
- return (ch >= ' a' && ch < ' a' + std::min ( base, 36 ) - 10 ) || (ch >= ' A' && ch < ' A' + std::min ( base, 36 ) - 10 );
96
+ return (ch >= ' a' && ch < ' a' + base - 10 ) || (ch >= ' A' && ch < ' A' + base - 10 );
97
97
return false ;
98
98
}
99
99
100
+ constexpr int toIntegralDigit (char ch)
101
+ {
102
+ if (IS_NUM (ch))
103
+ return ch - ' 0' ;
104
+ if (ch >= ' a' )
105
+ return ch - ' a' + 10 ;
106
+ return ch - ' A' + 10 ;
107
+ }
108
+
100
109
template <typename T>
101
110
inline bool parseInteger (std::string_view& input, T& integer, int base = 10 )
102
111
{
103
- static const T intMax = std::numeric_limits<T>::max () ;
104
- static const bool isSigned = std::numeric_limits<T>::is_signed ;
112
+ constexpr bool isSigned = std::numeric_limits<T>::is_signed ;
113
+ constexpr T intMax = std::numeric_limits<T>::max () ;
105
114
const T maxMultiplier = intMax / static_cast <T>(base);
106
115
116
+ T value = 0 ;
107
117
bool isNegative = false ;
118
+
108
119
if (!input.empty () && input.front () == ' +' ) {
109
120
input.remove_prefix (1 );
110
121
} else if (!input.empty () && isSigned && input.front () == ' -' ) {
111
122
input.remove_prefix (1 );
112
123
isNegative = true ;
113
124
}
114
125
115
- T value = 0 ;
116
126
if (input.empty () || !isIntegralDigit (input.front (), base))
117
127
return false ;
118
128
do {
119
- const char ch = input.front ();
120
- int digitValue;
121
- if (IS_NUM (ch))
122
- digitValue = ch - ' 0' ;
123
- else if (ch >= ' a' )
124
- digitValue = ch - ' a' + 10 ;
125
- else
126
- digitValue = ch - ' A' + 10 ;
129
+ const int digitValue = toIntegralDigit (input.front ());
127
130
if (value > maxMultiplier || (value == maxMultiplier && static_cast <T>(digitValue) > (intMax % static_cast <T>(base)) + isNegative))
128
131
return false ;
129
132
value = static_cast <T>(base) * value + static_cast <T>(digitValue);
130
133
input.remove_prefix (1 );
131
134
} while (!input.empty () && isIntegralDigit (input.front (), base));
132
135
133
- using signed_t = typename std::make_signed<T>::type;
136
+ using SignedType = typename std::make_signed<T>::type;
134
137
if (isNegative)
135
- integer = -static_cast <signed_t >(value);
138
+ integer = -static_cast <SignedType >(value);
136
139
else
137
140
integer = value;
138
141
return true ;
@@ -141,15 +144,12 @@ inline bool parseInteger(std::string_view& input, T& integer, int base = 10)
141
144
template <typename T>
142
145
inline bool parseNumber (std::string_view& input, T& number)
143
146
{
144
- T integer, fraction;
145
- int sign, expsign, exponent;
146
-
147
147
constexpr T maxValue = std::numeric_limits<T>::max ();
148
- fraction = 0 ;
149
- integer = 0 ;
150
- exponent = 0 ;
151
- sign = 1 ;
152
- expsign = 1 ;
148
+ T integer = 0 ;
149
+ T fraction = 0 ;
150
+ int exponent = 0 ;
151
+ int sign = 1 ;
152
+ int expsign = 1 ;
153
153
154
154
if (!input.empty () && input.front () == ' +' ) {
155
155
input.remove_prefix (1 );
@@ -171,7 +171,7 @@ inline bool parseNumber(std::string_view& input, T& number)
171
171
input.remove_prefix (1 );
172
172
if (input.empty () || !IS_NUM (input.front ()))
173
173
return false ;
174
- T divisor = 1 ;
174
+ T divisor = static_cast <T>( 1 ) ;
175
175
do {
176
176
fraction = static_cast <T>(10 ) * fraction + (input.front () - ' 0' );
177
177
divisor *= static_cast <T>(10 );
0 commit comments