mirror of
https://github.com/thorvg/thorvg.git
synced 2025-07-24 23:28:57 +00:00
svg_loader util: code refactoring
fix for thorvg coding convention. no logical changes.
This commit is contained in:
parent
e6debdbf15
commit
5a6240bdf3
1 changed files with 76 additions and 117 deletions
|
@ -71,194 +71,153 @@ static uint8_t _base64Value(const char chr)
|
||||||
*/
|
*/
|
||||||
float svgUtilStrtof(const char *nPtr, char **endPtr)
|
float svgUtilStrtof(const char *nPtr, char **endPtr)
|
||||||
{
|
{
|
||||||
const char *iter;
|
if (endPtr) *endPtr = (char*)(nPtr);
|
||||||
const char *a;
|
|
||||||
float val;
|
|
||||||
unsigned long long integerPart;
|
|
||||||
int minus;
|
|
||||||
|
|
||||||
if (endPtr) *endPtr = (char*)nPtr;
|
|
||||||
if (!nPtr) return 0.0f;
|
if (!nPtr) return 0.0f;
|
||||||
|
|
||||||
a = iter = nPtr;
|
auto a = nPtr;
|
||||||
|
auto iter = nPtr;
|
||||||
|
auto val = 0.0f;
|
||||||
|
unsigned long long integerPart = 0;
|
||||||
|
int minus = 1;
|
||||||
|
|
||||||
//ignore leading whitespaces
|
//ignore leading whitespaces
|
||||||
while (isspace(*iter)) iter++;
|
while (isspace(*iter)) iter++;
|
||||||
|
|
||||||
//signed or not
|
//signed or not
|
||||||
minus = 1;
|
if (*iter == '-') {
|
||||||
if (*iter == '-')
|
|
||||||
{
|
|
||||||
minus = -1;
|
minus = -1;
|
||||||
iter++;
|
iter++;
|
||||||
|
} else if (*iter == '+') {
|
||||||
|
iter++;
|
||||||
}
|
}
|
||||||
else if (*iter == '+') iter++;
|
|
||||||
|
|
||||||
if (tolower(*iter) == 'i')
|
if (tolower(*iter) == 'i') {
|
||||||
{
|
if ((tolower(*(iter + 1)) == 'n') && (tolower(*(iter + 2)) == 'f')) iter += 3;
|
||||||
if ((tolower(*(iter + 1)) == 'n') && (tolower(*(iter + 2)) == 'f'))
|
else goto error;
|
||||||
{
|
|
||||||
iter += 3;
|
if (tolower(*(iter + 3)) == 'i') {
|
||||||
|
if ((tolower(*(iter + 4)) == 'n') && (tolower(*(iter + 5)) == 'i') && (tolower(*(iter + 6)) == 't') && (tolower(*(iter + 7)) == 'y')) iter += 5;
|
||||||
|
else goto error;
|
||||||
}
|
}
|
||||||
else goto on_error;
|
if (endPtr) *endPtr = (char *)(iter);
|
||||||
|
return (minus == -1) ? -INFINITY : INFINITY;
|
||||||
if (tolower(*(iter + 3)) == 'i')
|
|
||||||
{
|
|
||||||
if ((tolower(*(iter + 4)) == 'n') &&
|
|
||||||
(tolower(*(iter + 5)) == 'i') &&
|
|
||||||
(tolower(*(iter + 6)) == 't') &&
|
|
||||||
(tolower(*(iter + 7)) == 'y'))
|
|
||||||
{
|
|
||||||
iter += 5;
|
|
||||||
}
|
|
||||||
else goto on_error;
|
|
||||||
}
|
|
||||||
if (endPtr) *endPtr = (char *)iter;
|
|
||||||
return (minus == -1) ? -INFINITY : INFINITY;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tolower(*iter) == 'n')
|
if (tolower(*iter) == 'n') {
|
||||||
{
|
if ((tolower(*(iter + 1)) == 'a') && (tolower(*(iter + 2)) == 'n')) iter += 3;
|
||||||
if ((tolower(*(iter + 1)) == 'a') && (tolower(*(iter + 2)) == 'n')) iter += 3;
|
else goto error;
|
||||||
else goto on_error;
|
|
||||||
|
|
||||||
if (endPtr) *endPtr = (char *)iter;
|
if (endPtr) *endPtr = (char *)(iter);
|
||||||
return (minus == -1) ? -NAN : NAN;
|
return (minus == -1) ? -NAN : NAN;
|
||||||
}
|
}
|
||||||
|
|
||||||
integerPart = 0;
|
//Optional: integer part before dot
|
||||||
|
if (isdigit(*iter)) {
|
||||||
//(optional) integer part before dot
|
for (; isdigit(*iter); iter++) {
|
||||||
if (isdigit(*iter))
|
integerPart = integerPart * 10ULL + (unsigned long long)(*iter - '0');
|
||||||
{
|
}
|
||||||
for (; isdigit(*iter); iter++) integerPart = integerPart * 10ULL + (unsigned long long)(*iter - '0');
|
|
||||||
|
|
||||||
a = iter;
|
a = iter;
|
||||||
}
|
} else if (*iter != '.') {
|
||||||
else if (*iter != '.')
|
goto success;
|
||||||
{
|
|
||||||
val = 0.0;
|
|
||||||
goto on_success;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val = (float)integerPart;
|
val = static_cast<float>(integerPart);
|
||||||
|
|
||||||
//(optional) decimal part after dot
|
//Optional: decimal part after dot
|
||||||
if (*iter == '.')
|
if (*iter == '.') {
|
||||||
{
|
unsigned long long decimalPart = 0;
|
||||||
unsigned long long decimalPart;
|
unsigned long long pow10 = 1;
|
||||||
unsigned long long pow10;
|
int count = 0;
|
||||||
int count;
|
|
||||||
|
|
||||||
iter++;
|
iter++;
|
||||||
|
|
||||||
decimalPart = 0;
|
if (isdigit(*iter)) {
|
||||||
count = 0;
|
for (; isdigit(*iter); iter++, count++) {
|
||||||
pow10 = 1;
|
if (count < 19) {
|
||||||
|
decimalPart = decimalPart * 10ULL + + static_cast<unsigned long long>(*iter - '0');
|
||||||
if (isdigit(*iter))
|
|
||||||
{
|
|
||||||
for (; isdigit(*iter); iter++, count++)
|
|
||||||
{
|
|
||||||
if (count < 19)
|
|
||||||
{
|
|
||||||
decimalPart = decimalPart * 10ULL + + (unsigned long long)(*iter - '0');
|
|
||||||
pow10 *= 10ULL;
|
pow10 *= 10ULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val += (float)decimalPart / (float)pow10;
|
val += static_cast<float>(decimalPart) / static_cast<float>(pow10);
|
||||||
a = iter;
|
a = iter;
|
||||||
}
|
}
|
||||||
|
|
||||||
//(optional) exponent
|
//Optional: exponent
|
||||||
if ((*iter == 'e') || (*iter == 'E'))
|
if (*iter == 'e' || *iter == 'E') {
|
||||||
{
|
|
||||||
float scale = 1.0f;
|
|
||||||
unsigned int expo_part;
|
|
||||||
int minus_e;
|
|
||||||
|
|
||||||
++iter;
|
++iter;
|
||||||
|
|
||||||
//Exception: svg may have 'em' unit for fonts. ex) 5em, 10.5em
|
//Exception: svg may have 'em' unit for fonts. ex) 5em, 10.5em
|
||||||
if ((*iter == 'm') || (*iter == 'M')) {
|
if ((*iter == 'm') || (*iter == 'M')) {
|
||||||
//TODO: We don't support font em unit now, but has to multiply val * font size later...
|
//TODO: We don't support font em unit now, but has to multiply val * font size later...
|
||||||
a = iter + 1;
|
a = iter + 1;
|
||||||
goto on_success;
|
goto success;
|
||||||
}
|
}
|
||||||
|
|
||||||
//signed or not
|
//signed or not
|
||||||
minus_e = 1;
|
int minus_e = 1;
|
||||||
if (*iter == '-')
|
|
||||||
{
|
if (*iter == '-') {
|
||||||
minus_e = -1;
|
minus_e = -1;
|
||||||
++iter;
|
++iter;
|
||||||
|
} else if (*iter == '+') {
|
||||||
|
iter++;
|
||||||
}
|
}
|
||||||
else if (*iter == '+') iter++;
|
|
||||||
|
|
||||||
//exponential part
|
unsigned int exponetPart = 0;
|
||||||
expo_part = 0;
|
|
||||||
if (isdigit(*iter))
|
if (isdigit(*iter)) {
|
||||||
{
|
|
||||||
while (*iter == 0) iter++;
|
while (*iter == 0) iter++;
|
||||||
|
for (; isdigit(*iter); iter++) {
|
||||||
for (; isdigit(*iter); iter++)
|
exponetPart = exponetPart * 10U + static_cast<unsigned int>(*iter - '0');
|
||||||
{
|
|
||||||
expo_part = expo_part * 10U + (unsigned int)(*iter - '0');
|
|
||||||
}
|
}
|
||||||
}
|
} else if (!isdigit(*(a - 1))) {
|
||||||
else if (!isdigit(*(a - 1)))
|
|
||||||
{
|
|
||||||
a = nPtr;
|
a = nPtr;
|
||||||
goto on_success;
|
goto success;
|
||||||
|
} else if (*iter == 0) {
|
||||||
|
goto success;
|
||||||
}
|
}
|
||||||
else if (*iter == 0) goto on_success;
|
|
||||||
|
|
||||||
if ((_floatExact(val, 2.2250738585072011f)) && ((minus_e * (int)expo_part) == -308))
|
if ((_floatExact(val, 2.2250738585072011f)) && ((minus_e * static_cast<int>(exponetPart)) == -308)) {
|
||||||
{
|
|
||||||
val *= 1.0e-308f;
|
val *= 1.0e-308f;
|
||||||
a = iter;
|
a = iter;
|
||||||
errno = ERANGE;
|
errno = ERANGE;
|
||||||
goto on_success;
|
goto success;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((_floatExact(val, 2.2250738585072012f)) && ((minus_e * (int)expo_part) <= -308))
|
if ((_floatExact(val, 2.2250738585072012f)) && ((minus_e * static_cast<int>(exponetPart)) <= -308)) {
|
||||||
{
|
|
||||||
val *= 1.0e-308f;
|
val *= 1.0e-308f;
|
||||||
a = iter;
|
a = iter;
|
||||||
goto on_success;
|
goto success;
|
||||||
}
|
}
|
||||||
|
|
||||||
a = iter;
|
a = iter;
|
||||||
|
auto scale = 1.0f;
|
||||||
|
|
||||||
while (expo_part >= 8U)
|
while (exponetPart >= 8U) {
|
||||||
{
|
|
||||||
scale *= 1E8;
|
scale *= 1E8;
|
||||||
expo_part -= 8U;
|
exponetPart -= 8U;
|
||||||
}
|
}
|
||||||
while (expo_part > 0U)
|
while (exponetPart > 0U) {
|
||||||
{
|
|
||||||
scale *= 10.0f;
|
scale *= 10.0f;
|
||||||
expo_part--;
|
exponetPart--;
|
||||||
}
|
}
|
||||||
|
|
||||||
val = (minus_e == -1) ? (val / scale) : (val * scale);
|
val = (minus_e == -1) ? (val / scale) : (val * scale);
|
||||||
}
|
} else if ((iter > nPtr) && !isdigit(*(iter - 1))) {
|
||||||
else if ((iter > nPtr) && !isdigit(*(iter - 1)))
|
|
||||||
{
|
|
||||||
a = nPtr;
|
a = nPtr;
|
||||||
goto on_success;
|
goto success;
|
||||||
}
|
}
|
||||||
|
|
||||||
on_success:
|
success:
|
||||||
if (endPtr) *endPtr = (char *)a;
|
if (endPtr) *endPtr = (char*)(a);
|
||||||
return minus * val;
|
return minus * val;
|
||||||
|
|
||||||
on_error:
|
error:
|
||||||
if (endPtr) *endPtr = (char *)nPtr;
|
if (endPtr) *endPtr = (char*)(nPtr);
|
||||||
return 0.0f;
|
return 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
string svgUtilURLDecode(const char *src)
|
string svgUtilURLDecode(const char *src)
|
||||||
{
|
{
|
||||||
if (!src) return nullptr;
|
if (!src) return nullptr;
|
||||||
|
@ -286,6 +245,7 @@ string svgUtilURLDecode(const char *src)
|
||||||
return decoded;
|
return decoded;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
string svgUtilBase64Decode(const char *src)
|
string svgUtilBase64Decode(const char *src)
|
||||||
{
|
{
|
||||||
if (!src) return nullptr;
|
if (!src) return nullptr;
|
||||||
|
@ -316,5 +276,4 @@ string svgUtilBase64Decode(const char *src)
|
||||||
src += 4;
|
src += 4;
|
||||||
}
|
}
|
||||||
return decoded;
|
return decoded;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue