Skip to content

Commit

Permalink
sys/fmt: optimize scn_u32_dec scn_u32_hex
Browse files Browse the repository at this point in the history
  • Loading branch information
kfessel committed Dec 8, 2022
1 parent 2274a56 commit 9395feb
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions sys/fmt/fmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -469,15 +469,14 @@ 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)) {
while( n-- ) {
unsigned char c = *str++;
unsigned d = c - '0';
if ( d > 9 ) {
break;
}
else {
res *= 10;
res += (c - '0');
}
res *= 10;
res += d;
}
return res;
}
Expand All @@ -487,21 +486,15 @@ 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;
}
else {
res <<= 4;
res |= c - '0';
}
unsigned char c = *str++;
unsigned d;
if ( c - '0' <= 9 ) d = c - '0';
else if ( c - 'A' <= 'F' - 'A' ) d = c - 'A' + 0xa;
else if ( c - 'a' <= 'f' - 'a' ) d = c - 'a' + 0xa;
else break;
res <<= 4;
res |= d;

}
return res;
}
Expand Down

0 comments on commit 9395feb

Please sign in to comment.