1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

sys/fmt: optimize scn_u32_dec scn_u32_hex

This commit is contained in:
Karl Fessel 2022-12-07 17:32:01 +01:00
parent 2274a56fef
commit 18a783d21c

View File

@ -470,14 +470,13 @@ uint32_t scn_u32_dec(const char *str, size_t n)
uint32_t res = 0;
while (n--) {
char c = *str++;
if (!fmt_is_digit(c)) {
unsigned c = *str++;
unsigned d = c - (unsigned)'0';
if ( !( '0'<= c && c <= '9') ) {
break;
}
else {
res *= 10;
res += (c - '0');
}
res *= 10U;
res += d;
}
return res;
}
@ -487,21 +486,23 @@ uint32_t scn_u32_hex(const char *str, size_t n)
uint32_t res = 0;
while (n--) {
char c = *str++;
if (!fmt_is_digit(c)) {
if (fmt_is_upper(c)) {
c = _to_lower(c);
}
if (c == '\0' || c > 'f') {
break;
}
res <<= 4;
res |= c - 'a' + 0xa;
unsigned c = *str++;
unsigned d;
if (('0'<= c) && (c <= '9')){
d = c - (unsigned)'0';
}
else if (('A' <= c) && (c <= 'F')) {
d = c - (unsigned)'A' + 0xaU;
}
else if (('a' <= c) && (c <= 'f')) {
d = c - (unsigned)'a' + 0xaU;
}
else {
res <<= 4;
res |= c - '0';
break;
}
res <<= 4U;
res |= d;
}
return res;
}