From 18a783d21cb47bf83918015d85f5493e5712659d Mon Sep 17 00:00:00 2001 From: Karl Fessel Date: Wed, 7 Dec 2022 17:32:01 +0100 Subject: [PATCH] sys/fmt: optimize scn_u32_dec scn_u32_hex --- sys/fmt/fmt.c | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/sys/fmt/fmt.c b/sys/fmt/fmt.c index 11eddf3cfa9c2..78adb8afe6122 100644 --- a/sys/fmt/fmt.c +++ b/sys/fmt/fmt.c @@ -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; }