1#ifndef NOVASVG_SVGPARSERUTILS_H
2#define NOVASVG_SVGPARSERUTILS_H
10constexpr bool IS_NUM(
int cc) {
return cc >=
'0' && cc <=
'9'; }
11constexpr bool IS_ALPHA(
int cc) {
return (cc >=
'a' && cc <=
'z') || (cc >=
'A' && cc <=
'Z'); }
12constexpr bool IS_WS(
int cc) {
return cc ==
' ' || cc ==
'\t' || cc ==
'\n' || cc ==
'\r'; }
16 while(!input.empty() &&
IS_WS(input.front())) {
17 input.remove_prefix(1);
23 while(!input.empty() &&
IS_WS(input.back())) {
24 input.remove_suffix(1);
36 while(!input.empty() &&
IS_WS(input.front()))
37 input.remove_prefix(1);
38 return !input.empty();
43 if(!input.empty() && !
IS_WS(input.front()) && delimiter != input.front())
46 if(delimiter == input.front()) {
47 input.remove_prefix(1);
52 return !input.empty();
62 if(!input.empty() && input.front() == delimiter) {
63 input.remove_prefix(1);
73 if(!input.empty() && input.front() == delimiter) {
74 input.remove_prefix(1);
81constexpr bool skipString(std::string_view& input, std::string_view value)
83 if(input.size() >= value.size() && value == input.substr(0, value.size())) {
84 input.remove_prefix(value.size());
94 return ch -
'0' < base;
96 return (ch >=
'a' && ch <
'a' + base - 10) || (ch >=
'A' && ch <
'A' + base - 10);
105 return ch -
'a' + 10;
106 return ch -
'A' + 10;
110inline bool parseInteger(std::string_view& input, T& integer,
int base = 10)
112 constexpr bool isSigned = std::numeric_limits<T>::is_signed;
113 constexpr T intMax = std::numeric_limits<T>::max();
114 const T maxMultiplier = intMax /
static_cast<T
>(base);
117 bool isNegative =
false;
119 if(!input.empty() && input.front() ==
'+') {
120 input.remove_prefix(1);
121 }
else if(!input.empty() && isSigned && input.front() ==
'-') {
122 input.remove_prefix(1);
130 if(value > maxMultiplier || (value == maxMultiplier &&
static_cast<T
>(digitValue) > (intMax %
static_cast<T
>(base)) + isNegative))
132 value =
static_cast<T
>(base) * value +
static_cast<T
>(digitValue);
133 input.remove_prefix(1);
136 using SignedType =
typename std::make_signed<T>::type;
138 integer = -
static_cast<SignedType
>(value);
147 constexpr T maxValue = std::numeric_limits<T>::max();
154 if(!input.empty() && input.front() ==
'+') {
155 input.remove_prefix(1);
156 }
else if(!input.empty() && input.front() ==
'-') {
157 input.remove_prefix(1);
161 if(input.empty() || (!
IS_NUM(input.front()) && input.front() !=
'.'))
163 if(
IS_NUM(input.front())) {
165 integer =
static_cast<T
>(10) * integer + (input.front() -
'0');
166 input.remove_prefix(1);
167 }
while(!input.empty() &&
IS_NUM(input.front()));
170 if(!input.empty() && input.front() ==
'.') {
171 input.remove_prefix(1);
172 if(input.empty() || !
IS_NUM(input.front()))
174 T divisor =
static_cast<T
>(1);
176 fraction =
static_cast<T
>(10) * fraction + (input.front() -
'0');
177 divisor *=
static_cast<T
>(10);
178 input.remove_prefix(1);
179 }
while(!input.empty() &&
IS_NUM(input.front()));
183 if(input.size() > 1 && (input[0] ==
'e' || input[0] ==
'E')
184 && (input[1] !=
'x' && input[1] !=
'm'))
186 input.remove_prefix(1);
187 if(!input.empty() && input.front() ==
'+')
188 input.remove_prefix(1);
189 else if(!input.empty() && input.front() ==
'-') {
190 input.remove_prefix(1);
194 if(input.empty() || !
IS_NUM(input.front()))
197 exponent = 10 * exponent + (input.front() -
'0');
198 input.remove_prefix(1);
199 }
while(!input.empty() &&
IS_NUM(input.front()));
202 number = sign * (integer + fraction);
204 number *=
static_cast<T
>(std::pow(10.0, expsign * exponent));
205 return number >= -maxValue && number <= maxValue;
Main namespace for the NovaSVG library.
constexpr bool IS_WS(int cc)
constexpr int toIntegralDigit(char ch)
bool parseNumber(std::string_view &input, T &number)
bool parseInteger(std::string_view &input, T &integer, int base=10)
constexpr bool skipDelimiter(std::string_view &input, char delimiter)
constexpr bool skipString(std::string_view &input, std::string_view value)
constexpr bool skipDelimiterAndOptionalSpaces(std::string_view &input, char delimiter)
constexpr bool skipOptionalSpacesOrComma(std::string_view &input)
constexpr bool IS_NUM(int cc)
constexpr void stripLeadingAndTrailingSpaces(std::string_view &input)
constexpr bool IS_ALPHA(int cc)
constexpr bool isIntegralDigit(char ch, int base)
constexpr void stripLeadingSpaces(std::string_view &input)
constexpr bool skipOptionalSpaces(std::string_view &input)
constexpr bool skipOptionalSpacesOrDelimiter(std::string_view &input, char delimiter)
constexpr void stripTrailingSpaces(std::string_view &input)