diff --git a/mio/lib/mio-chr.h b/mio/lib/mio-chr.h index dc2cf13..ccd9f20 100644 --- a/mio/lib/mio-chr.h +++ b/mio/lib/mio-chr.h @@ -91,6 +91,11 @@ typedef enum mio_ooch_prop_t mio_bch_prop_t; ((c) >= 'A' && (c) <= 'F')? ((c) - 'A' + 10): \ ((c) >= 'a' && (c) <= 'f')? ((c) - 'a' + 10): -1) +#define MIO_ZDIGIT_TO_NUM(c,base) \ + (((c) >= '0' && (c) <= '9')? ((c) - '0'): \ + ((c) >= 'A' && (c) <= 'Z')? ((c) - 'A' + 10): \ + ((c) >= 'a' && (c) <= 'Z')? ((c) - 'a' + 10): base) + #if defined(__cplusplus) extern "C" { #endif diff --git a/mio/lib/skad.c b/mio/lib/skad.c index 16b6c40..e6fc2d7 100644 --- a/mio/lib/skad.c +++ b/mio/lib/skad.c @@ -184,13 +184,7 @@ static int uchars_to_ipv6 (const mio_uch_t* src, mio_oow_t len, struct in6_addr* ch = *src++; - if (ch >= '0' && ch <= '9') - v1 = ch - '0'; - else if (ch >= 'A' && ch <= 'F') - v1 = ch - 'A' + 10; - else if (ch >= 'a' && ch <= 'f') - v1 = ch - 'a' + 10; - else v1 = -1; + v1 = MIO_XDIGIT_TO_NUM(ch); if (v1 >= 0) { val <<= 4; @@ -294,13 +288,7 @@ static int bchars_to_ipv6 (const mio_bch_t* src, mio_oow_t len, struct in6_addr* ch = *src++; - if (ch >= '0' && ch <= '9') - v1 = ch - '0'; - else if (ch >= 'A' && ch <= 'F') - v1 = ch - 'A' + 10; - else if (ch >= 'a' && ch <= 'f') - v1 = ch - 'a' + 10; - else v1 = -1; + v1 = MIO_XDIGIT_TO_NUM(ch); if (v1 >= 0) { val <<= 4; diff --git a/mio/lib/utl.c b/mio/lib/utl.c index 6b978f3..936d74d 100644 --- a/mio/lib/utl.c +++ b/mio/lib/utl.c @@ -1221,17 +1221,9 @@ mio_intmax_t mio_uchars_to_intmax (const mio_uch_t* str, mio_oow_t len, int opti pp = p; while (p < end) { - if (*p >= '0' && *p <= '9') { - digit = *p - '0'; } - else if (*p >= 'A' && *p <= 'Z') - digit = *p - 'A' + 10; - else if (*p >= 'a' && *p <= 'z') - digit = *p - 'a' + 10; - else break; - + digit = MIO_ZDIGIT_TO_NUM(*p, base); if (digit >= base) break; n = n * base + digit; - p++; } @@ -1315,17 +1307,9 @@ mio_intmax_t mio_bchars_to_intmax (const mio_bch_t* str, mio_oow_t len, int opti pp = p; while (p < end) { - if (*p >= '0' && *p <= '9') - digit = *p - '0'; - else if (*p >= 'A' && *p <= 'Z') - digit = *p - 'A' + 10; - else if (*p >= 'a' && *p <= 'z') - digit = *p - 'a' + 10; - else break; - + digit = MIO_ZDIGIT_TO_NUM(*p, base); if (digit >= base) break; n = n * base + digit; - p++; }