added a common macro HAWK_XDIGIT_TO_NUM() to replace repeated code

This commit is contained in:
hyung-hwan 2020-05-27 09:15:48 +00:00
parent 8b7b2562df
commit 303118bc5c
4 changed files with 25 additions and 45 deletions

View File

@ -103,13 +103,7 @@ static int str_to_ipv6 (const hawk_ooch_t* src, hawk_oow_t len, struct in6_addr*
ch = *src++; ch = *src++;
if (ch >= '0' && ch <= '9') v1 = HAWK_XDIGIT_TO_NUM(ch);
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;
if (v1 >= 0) if (v1 >= 0)
{ {
val <<= 4; val <<= 4;

View File

@ -84,6 +84,13 @@ typedef enum hawk_ooch_prop_t hawk_ooch_prop_t;
typedef enum hawk_ooch_prop_t hawk_uch_prop_t; typedef enum hawk_ooch_prop_t hawk_uch_prop_t;
typedef enum hawk_ooch_prop_t hawk_bch_prop_t; typedef enum hawk_ooch_prop_t hawk_bch_prop_t;
#define HAWK_DIGIT_TO_NUM(c) (((c) >= '0' && (c) <= '9')? ((c) - '0'): -1)
#define HAWK_XDIGIT_TO_NUM(c) \
(((c) >= '0' && (c) <= '9')? ((c) - '0'): \
((c) >= 'A' && (c) <= 'F')? ((c) - 'A' + 10): \
((c) >= 'a' && (c) <= 'f')? ((c) - 'a' + 10): -1)
#if defined(__cplusplus) #if defined(__cplusplus)
extern "C" { extern "C" {
#endif #endif

View File

@ -5983,12 +5983,12 @@ static int get_number (hawk_t* hawk, hawk_tok_t* tok)
c = hawk->sio.last.c; c = hawk->sio.last.c;
if (c == HAWK_T('0')) if (c == '0')
{ {
ADD_TOKEN_CHAR (hawk, tok, c); ADD_TOKEN_CHAR (hawk, tok, c);
GET_CHAR_TO (hawk, c); GET_CHAR_TO (hawk, c);
if (c == HAWK_T('x') || c == HAWK_T('X')) if (c == 'x' || c == 'X')
{ {
/* hexadecimal number */ /* hexadecimal number */
do do
@ -6000,7 +6000,7 @@ static int get_number (hawk_t* hawk, hawk_tok_t* tok)
return 0; return 0;
} }
else if (c == HAWK_T('b') || c == HAWK_T('B')) else if (c == 'b' || c == 'B')
{ {
/* binary number */ /* binary number */
do do
@ -6008,20 +6008,20 @@ static int get_number (hawk_t* hawk, hawk_tok_t* tok)
ADD_TOKEN_CHAR (hawk, tok, c); ADD_TOKEN_CHAR (hawk, tok, c);
GET_CHAR_TO (hawk, c); GET_CHAR_TO (hawk, c);
} }
while (c == HAWK_T('0') || c == HAWK_T('1')); while (c == '0' || c == '1');
return 0; return 0;
} }
else if (c != '.') else if (c != '.')
{ {
/* octal number */ /* octal number */
while (c >= HAWK_T('0') && c <= HAWK_T('7')) while (c >= '0' && c <= '7')
{ {
ADD_TOKEN_CHAR (hawk, tok, c); ADD_TOKEN_CHAR (hawk, tok, c);
GET_CHAR_TO (hawk, c); GET_CHAR_TO (hawk, c);
} }
if (c == HAWK_T('8') || c == HAWK_T('9')) if (c == '8' || c == '9')
{ {
hawk_seterrfmt (hawk, &hawk->tok.loc, HAWK_ELXCHR, HAWK_T("invalid digit '%jc'"), (hawk_ooch_t)c); hawk_seterrfmt (hawk, &hawk->tok.loc, HAWK_ELXCHR, HAWK_T("invalid digit '%jc'"), (hawk_ooch_t)c);
return -1; return -1;
@ -6037,7 +6037,7 @@ static int get_number (hawk_t* hawk, hawk_tok_t* tok)
GET_CHAR_TO (hawk, c); GET_CHAR_TO (hawk, c);
} }
if (c == HAWK_T('.')) if (c == '.')
{ {
/* floating-point number */ /* floating-point number */
SET_TOKEN_TYPE (hawk, tok, TOK_FLT); SET_TOKEN_TYPE (hawk, tok, TOK_FLT);
@ -6052,14 +6052,14 @@ static int get_number (hawk_t* hawk, hawk_tok_t* tok)
} }
} }
if (c == HAWK_T('E') || c == HAWK_T('e')) if (c == 'E' || c == 'e')
{ {
SET_TOKEN_TYPE (hawk, tok, TOK_FLT); SET_TOKEN_TYPE (hawk, tok, TOK_FLT);
ADD_TOKEN_CHAR (hawk, tok, c); ADD_TOKEN_CHAR (hawk, tok, c);
GET_CHAR_TO (hawk, c); GET_CHAR_TO (hawk, c);
if (c == HAWK_T('+') || c == HAWK_T('-')) if (c == '+' || c == '-')
{ {
ADD_TOKEN_CHAR (hawk, tok, c); ADD_TOKEN_CHAR (hawk, tok, c);
GET_CHAR_TO (hawk, c); GET_CHAR_TO (hawk, c);

View File

@ -2628,17 +2628,9 @@ hawk_int_t hawk_uchars_to_int (const hawk_uch_t* str, hawk_oow_t len, int option
pp = p; pp = p;
while (p < end) while (p < end)
{ {
if (*p >= '0' && *p <= '9') { digit = HAWK_XDIGIT_TO_NUM(*p);
digit = *p - '0'; } if (digit <= - 1 || digit >= base) break;
else if (*p >= 'A' && *p <= 'Z')
digit = *p - 'A' + 10;
else if (*p >= 'a' && *p <= 'z')
digit = *p - 'a' + 10;
else break;
if (digit >= base) break;
n = n * base + digit; n = n * base + digit;
p++; p++;
} }
@ -2722,17 +2714,9 @@ hawk_int_t hawk_bchars_to_int (const hawk_bch_t* str, hawk_oow_t len, int option
pp = p; pp = p;
while (p < end) while (p < end)
{ {
if (*p >= '0' && *p <= '9') digit = HAWK_XDIGIT_TO_NUM(*p);
digit = *p - '0'; if (digit <= - 1 || digit >= base) break;
else if (*p >= 'A' && *p <= 'Z')
digit = *p - 'A' + 10;
else if (*p >= 'a' && *p <= 'z')
digit = *p - 'a' + 10;
else break;
if (digit >= base) break;
n = n * base + digit; n = n * base + digit;
p++; p++;
} }
@ -3314,11 +3298,6 @@ int hawk_bchars_to_num (int option, const hawk_bch_t* ptr, hawk_oow_t len, hawk_
/* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */
#define XDIGIT_TO_NUM(c) \
(((c) >= '0' && (c) <= '9')? ((c) - '0'): \
((c) >= 'A' && (c) <= 'F')? ((c) - 'A' + 10): \
((c) >= 'a' && (c) <= 'f')? ((c) - 'a' + 10): -1)
int hawk_uchars_to_bin (const hawk_uch_t* hex, hawk_oow_t hexlen, hawk_uint8_t* buf, hawk_oow_t buflen) int hawk_uchars_to_bin (const hawk_uch_t* hex, hawk_oow_t hexlen, hawk_uint8_t* buf, hawk_oow_t buflen)
{ {
const hawk_uch_t* end = hex + hexlen; const hawk_uch_t* end = hex + hexlen;
@ -3328,14 +3307,14 @@ int hawk_uchars_to_bin (const hawk_uch_t* hex, hawk_oow_t hexlen, hawk_uint8_t*
{ {
int v; int v;
v = XDIGIT_TO_NUM(*hex); v = HAWK_XDIGIT_TO_NUM(*hex);
if (v <= -1) return -1; if (v <= -1) return -1;
buf[bi] = buf[bi] * 16 + v; buf[bi] = buf[bi] * 16 + v;
hex++; hex++;
if (hex >= end) return -1; if (hex >= end) return -1;
v = XDIGIT_TO_NUM(*hex); v = HAWK_XDIGIT_TO_NUM(*hex);
if (v <= -1) return -1; if (v <= -1) return -1;
buf[bi] = buf[bi] * 16 + v; buf[bi] = buf[bi] * 16 + v;
@ -3355,14 +3334,14 @@ int hawk_bchars_to_bin (const hawk_bch_t* hex, hawk_oow_t hexlen, hawk_uint8_t*
{ {
int v; int v;
v = XDIGIT_TO_NUM(*hex); v = HAWK_XDIGIT_TO_NUM(*hex);
if (v <= -1) return -1; if (v <= -1) return -1;
buf[bi] = buf[bi] * 16 + v; buf[bi] = buf[bi] * 16 + v;
hex++; hex++;
if (hex >= end) return -1; if (hex >= end) return -1;
v = XDIGIT_TO_NUM(*hex); v = HAWK_XDIGIT_TO_NUM(*hex);
if (v <= -1) return -1; if (v <= -1) return -1;
buf[bi] = buf[bi] * 16 + v; buf[bi] = buf[bi] * 16 + v;