Skip to content

Commit 8b8a1dd

Browse files
committed
Refactor parseInteger and parseNumber
1 parent 23ec1fe commit 8b8a1dd

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

source/svgparserutils.h

+23-23
Original file line numberDiff line numberDiff line change
@@ -93,46 +93,49 @@ constexpr bool isIntegralDigit(char ch, int base)
9393
if(IS_NUM(ch))
9494
return ch - '0' < base;
9595
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);
9797
return false;
9898
}
9999

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+
100109
template<typename T>
101110
inline bool parseInteger(std::string_view& input, T& integer, int base = 10)
102111
{
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();
105114
const T maxMultiplier = intMax / static_cast<T>(base);
106115

116+
T value = 0;
107117
bool isNegative = false;
118+
108119
if(!input.empty() && input.front() == '+') {
109120
input.remove_prefix(1);
110121
} else if(!input.empty() && isSigned && input.front() == '-') {
111122
input.remove_prefix(1);
112123
isNegative = true;
113124
}
114125

115-
T value = 0;
116126
if(input.empty() || !isIntegralDigit(input.front(), base))
117127
return false;
118128
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());
127130
if(value > maxMultiplier || (value == maxMultiplier && static_cast<T>(digitValue) > (intMax % static_cast<T>(base)) + isNegative))
128131
return false;
129132
value = static_cast<T>(base) * value + static_cast<T>(digitValue);
130133
input.remove_prefix(1);
131134
} while(!input.empty() && isIntegralDigit(input.front(), base));
132135

133-
using signed_t = typename std::make_signed<T>::type;
136+
using SignedType = typename std::make_signed<T>::type;
134137
if(isNegative)
135-
integer = -static_cast<signed_t>(value);
138+
integer = -static_cast<SignedType>(value);
136139
else
137140
integer = value;
138141
return true;
@@ -141,15 +144,12 @@ inline bool parseInteger(std::string_view& input, T& integer, int base = 10)
141144
template<typename T>
142145
inline bool parseNumber(std::string_view& input, T& number)
143146
{
144-
T integer, fraction;
145-
int sign, expsign, exponent;
146-
147147
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;
153153

154154
if(!input.empty() && input.front() == '+') {
155155
input.remove_prefix(1);
@@ -171,7 +171,7 @@ inline bool parseNumber(std::string_view& input, T& number)
171171
input.remove_prefix(1);
172172
if(input.empty() || !IS_NUM(input.front()))
173173
return false;
174-
T divisor = 1;
174+
T divisor = static_cast<T>(1);
175175
do {
176176
fraction = static_cast<T>(10) * fraction + (input.front() - '0');
177177
divisor *= static_cast<T>(10);

0 commit comments

Comments
 (0)