Skip to content

Commit

Permalink
refactor(utils): replace ctype functions to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Water-Melon committed Feb 8, 2024
1 parent f13a0f1 commit 54514c3
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 45 deletions.
26 changes: 13 additions & 13 deletions include/mln_lex.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "mln_stack.h"
#include "mln_string.h"
#include "mln_alloc.h"
#include "mln_rbtree.h"
#include "mln_func.h"
#include "mln_utils.h"
#include <stdlib.h>
#include <errno.h>

Expand Down Expand Up @@ -295,7 +295,7 @@ MLN_FUNC(static inline, char, mln_lex_getchar, (mln_lex_t *lex), (lex), {
})

MLN_FUNC(static inline, int, mln_lex_is_letter, (char c), (c), {
if (c == '_' || isalpha(c))
if (c == '_' || mln_isalpha(c))
return 1;
return 0;
})
Expand All @@ -307,7 +307,7 @@ MLN_FUNC(static inline, int, mln_lex_is_oct, (char c), (c), {
})

MLN_FUNC(static inline, int, mln_lex_is_hex, (char c), (c), {
if (isdigit(c) || \
if (mln_isdigit(c) || \
(c >= 'a' && c <= 'f') || \
(c >= 'A' && c <= 'F'))
return 1;
Expand Down Expand Up @@ -691,7 +691,7 @@ lp:\
default:\
{\
if (mln_lex_is_letter(c)) {\
while (mln_lex_is_letter(c) || isdigit(c)) {\
while (mln_lex_is_letter(c) || mln_isdigit(c)) {\
if (mln_lex_putchar(lex, c) == MLN_ERR) return NULL;\
c = mln_lex_getchar(lex);\
if (c == MLN_ERR) return NULL;\
Expand All @@ -705,12 +705,12 @@ lp:\
sret = PREFIX_NAME##_process_keywords(lex);\
if (sret == NULL || !lex->ignore) return sret;\
goto beg;\
} else if (isdigit(c)) {\
} else if (mln_isdigit(c)) {\
while ( 1 ) {\
if (mln_lex_putchar(lex, c) == MLN_ERR) return NULL;\
c = mln_lex_getchar(lex);\
if (c == MLN_ERR) return NULL;\
if (!isdigit(c) && !isalpha(c) && c != '.') {\
if (!mln_isdigit(c) && !mln_isalpha(c) && c != '.') {\
mln_lex_stepback(lex, c);\
break;\
}\
Expand Down Expand Up @@ -738,7 +738,7 @@ lp:\
mln_s32_t dot_cnt = 0;\
for (; chk < lex->result_pos; ++chk) {\
if (*chk == (mln_u8_t)'.') ++dot_cnt;\
if (!isdigit((char)(*chk)) && *chk != (mln_u8_t)'.') {\
if (!mln_isdigit((char)(*chk)) && *chk != (mln_u8_t)'.') {\
mln_lex_error_set(lex, MLN_LEX_EINVOCT);\
return NULL;\
}\
Expand All @@ -761,10 +761,10 @@ lp:\
}\
} else {\
for (; chk < lex->result_pos; ++chk) {\
if (isdigit((char)(*chk))) continue;\
if (mln_isdigit((char)(*chk))) continue;\
if (*chk == (mln_u8_t)'.') {\
for (++chk; chk < lex->result_pos; ++chk) {\
if (!isdigit((char)(*chk))) {\
if (!mln_isdigit((char)(*chk))) {\
mln_lex_error_set(lex, MLN_LEX_EINVREAL);\
return NULL;\
}\
Expand Down Expand Up @@ -878,7 +878,7 @@ lp:\
mln_lex_error_set(lex, MLN_LEX_EINVEOL);\
return NULL;\
}\
if (!mln_lex_is_letter(c) && !isdigit(c)) {\
if (!mln_lex_is_letter(c) && !mln_isdigit(c)) {\
mln_lex_error_set(lex, MLN_LEX_EINVCHAR);\
return NULL;\
}\
Expand Down Expand Up @@ -1017,7 +1017,7 @@ goon:\
mln_lex_error_set(lex, MLN_LEX_EINVEOL);\
return NULL;\
}\
if (!mln_lex_is_letter(c) && !isdigit(c)) {\
if (!mln_lex_is_letter(c) && !mln_isdigit(c)) {\
mln_lex_error_set(lex, MLN_LEX_EINVCHAR);\
return NULL;\
}\
Expand Down Expand Up @@ -1058,12 +1058,12 @@ goon:\
mln_lex_macro_t lm;\
char c = mln_lex_getchar(lex);\
if (c == MLN_ERR) return NULL;\
if (!mln_lex_is_letter(c) && !isdigit(c)) {\
if (!mln_lex_is_letter(c) && !mln_isdigit(c)) {\
mln_lex_stepback(lex, c);\
return PREFIX_NAME##_new(lex, TK_PREFIX##_TK_AT);\
}\
mln_lex_result_clean(lex);\
while (mln_lex_is_letter(c) || isdigit(c)) {\
while (mln_lex_is_letter(c) || mln_isdigit(c)) {\
if (mln_lex_putchar(lex, c) == MLN_ERR) return NULL;\
if ((c = mln_lex_getchar(lex)) == MLN_ERR) return NULL;\
}\
Expand Down
5 changes: 5 additions & 0 deletions include/mln_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ extern int socketpair(int domain, int type, int protocol, int sv[2]);
#define mln_socket_close close
#endif

#define mln_isalpha(x) (((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z'))
#define mln_isdigit(x) ((x) >= '0' && (x) <= '9')
#define mln_isascii(x) ((x) >= 0 && (x) <= 127)
#define mln_isprint(x) ((x) >= 32 && (x) <= 126)

#define MLN_AUTHOR "Niklaus F.Schen"

#endif
Expand Down
3 changes: 1 addition & 2 deletions src/mln_asn1.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "mln_asn1.h"
#include "mln_tools.h"
#include "mln_func.h"
Expand Down Expand Up @@ -696,7 +695,7 @@ MLN_FUNC(, int, mln_asn1_encode_utf8string, (mln_asn1_enresult_t *res, mln_u8ptr
MLN_FUNC(, int, mln_asn1_encode_printablestring, (mln_asn1_enresult_t *res, mln_s8ptr_t s, mln_u64_t slen), (res, s, slen), {
mln_s8ptr_t scan, end;
for (scan = s, end = s+slen; scan < end; ++scan) {
if (!isprint(*scan)) return M_ASN1_RET_ERROR;
if (!mln_isprint(*scan)) return M_ASN1_RET_ERROR;
}

mln_u64_t len = 0;
Expand Down
7 changes: 3 additions & 4 deletions src/mln_bignum.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include <sys/time.h>
#include <math.h>
Expand Down Expand Up @@ -130,7 +129,7 @@ MLN_FUNC(static inline, int, mln_bignum_assign_hex, \

for (i = 0; p >= sval; --p, ++i) {
if (i % 2) {
if (isdigit(*p)) {
if (mln_isdigit(*p)) {
b |= (((*p - '0') << 4) & 0xf0);
} else if (*p >= 'a' && *p <= 'f') {
b |= (((*p - 'a' + 10) << 4) & 0xf0);
Expand All @@ -141,7 +140,7 @@ MLN_FUNC(static inline, int, mln_bignum_assign_hex, \
}
} else {
b = 0;
if (isdigit(*p)) {
if (mln_isdigit(*p)) {
b |= ((*p - '0') & 0xf);
} else if (*p >= 'a' && *p <= 'f') {
b |= ((*p - 'a' + 10) & 0xf);
Expand Down Expand Up @@ -227,7 +226,7 @@ MLN_FUNC(static inline, int, mln_bignum_assign_dec, \
mln_bignum_positive(bn);

for (cnt = 0; p >= sval; --p, ++cnt) {
if (!isdigit(*p)) return -1;
if (!mln_isdigit(*p)) return -1;
memset(&tmp, 0, sizeof(tmp));
mln_bignum_positive(&tmp);
mln_bignum_dec_recursive(cnt, *p-'0', &tmp);
Expand Down
2 changes: 1 addition & 1 deletion src/mln_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ MLN_FUNC(static, mln_conf_lex_struct_t *, mln_conf_lex_sglq_handler, (mln_lex_t
mln_lex_result_clean(lex);
char c = mln_lex_getchar(lex);
if (c == MLN_ERR) return NULL;
if (!isascii(c) || c == '\'') {
if (!mln_isascii(c) || c == '\'') {
mln_lex_error_set(lex, MLN_LEX_EINVCHAR);
return NULL;
}
Expand Down
11 changes: 5 additions & 6 deletions src/mln_cron.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* Copyright (C) Niklaus F.Schen.
*/

#include <ctype.h>
#include "mln_cron.h"
#include "mln_tools.h"
#include "mln_func.h"
Expand Down Expand Up @@ -104,7 +103,7 @@ MLN_FUNC(static, long, mln_cron_parse_minute, (mln_string_t *smin, long min), (s
else head = p;
break;
default:
if (!isdigit(*p++)) return -1;
if (!mln_isdigit(*p++)) return -1;
break;
}
}
Expand Down Expand Up @@ -172,7 +171,7 @@ MLN_FUNC(static, long, mln_cron_parse_hour, \
else head = p;
break;
default:
if (!isdigit(*p++)) return -1;
if (!mln_isdigit(*p++)) return -1;
break;
}
}
Expand Down Expand Up @@ -244,7 +243,7 @@ MLN_FUNC(static, long, mln_cron_parse_day, \
else head = p;
break;
default:
if (!isdigit(*p++)) return -1;
if (!mln_isdigit(*p++)) return -1;
break;
}
}
Expand Down Expand Up @@ -316,7 +315,7 @@ MLN_FUNC(static, long, mln_cron_parse_month, \
else head = p;
break;
default:
if (!isdigit(*p++)) return -1;
if (!mln_isdigit(*p++)) return -1;
break;
}
}
Expand Down Expand Up @@ -388,7 +387,7 @@ MLN_FUNC(static, long, mln_cron_parse_week, \
else head = p;
break;
default:
if (!isdigit(*p++)) return -1;
if (!mln_isdigit(*p++)) return -1;
break;
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/mln_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include <stdio.h>
#include <unistd.h>
#include <ctype.h>
#include "mln_types.h"
#include "mln_http.h"
#include "mln_func.h"
Expand Down Expand Up @@ -864,7 +863,7 @@ MLN_FUNC(static inline, int, mln_http_atou, (mln_string_t *s, mln_u32_t *status)
mln_u8ptr_t p, end = s->data + s->len;

for (p = s->data; p < end; ++p) {
if (!isdigit(*p)) return M_HTTP_RET_ERROR;
if (!mln_isdigit(*p)) return M_HTTP_RET_ERROR;
st *= 10;
st += (*p - '0');
}
Expand Down
13 changes: 6 additions & 7 deletions src/mln_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* Copyright (C) Niklaus F.Schen.
*/

#include <ctype.h>
#include <stdio.h>
#include <stdarg.h>
#include "mln_json.h"
Expand Down Expand Up @@ -341,7 +340,7 @@ MLN_FUNC(static inline, int, mln_json_parse_json, \
case '\"':
return mln_json_parse_string(j, jstr, len, index);
default:
if (isdigit(jstr[0]) || jstr[0] == '-') {
if (mln_isdigit(jstr[0]) || jstr[0] == '-') {
return mln_json_parse_digit(j, jstr, len, index);
}
left = mln_json_parse_true(j, jstr, len, index);
Expand Down Expand Up @@ -710,15 +709,15 @@ MLN_FUNC(static inline, int, mln_json_digit_process, (double *val, char *s, int
double f = 0;
int i, j, dir = 1;

if (!isdigit(*s)) return -1;
if (!mln_isdigit(*s)) return -1;

if (s[0] == '0') {
++s;
if (--len <= 0) return 0;
if (isdigit(*s)) return -1;
if (mln_isdigit(*s)) return -1;
} else {
for (; len > 0; --len, ++s) {
if (!isdigit(*s)) break;
if (!mln_isdigit(*s)) break;
(*val) *= 10;
(*val) += (*s - '0');
}
Expand All @@ -729,7 +728,7 @@ MLN_FUNC(static inline, int, mln_json_digit_process, (double *val, char *s, int
++s;
if (--len <= 0) return -1;
for (i = 1; len > 0; --len, ++s, ++i) {
if (!isdigit(*s)) break;
if (!mln_isdigit(*s)) break;
f = *s - '0';
for (j = 0; j < i; ++j) {
f /= 10;
Expand All @@ -753,7 +752,7 @@ MLN_FUNC(static inline, int, mln_json_digit_process, (double *val, char *s, int
}

for (i = 0; len > 0; --len, ++s) {
if (!isdigit(*s)) break;
if (!mln_isdigit(*s)) break;
i *= 10;
i += (*s - '0');
}
Expand Down
3 changes: 1 addition & 2 deletions src/mln_lex.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include <dirent.h>
#include <errno.h>
#include <unistd.h>
#include <ctype.h>
#include "mln_string.h"
#include "mln_lex.h"
#include "mln_path.h"
Expand Down Expand Up @@ -697,7 +696,7 @@ MLN_FUNC(, int, mln_lex_condition_test, (mln_lex_t *lex), (lex), {
if (c == ' ' || c == '\t' || c == '\n' || c == MLN_EOF) {
break;
}
if (!mln_lex_is_letter(c) && !isdigit(c)) {
if (!mln_lex_is_letter(c) && !mln_isdigit(c)) {
mln_lex_error_set(lex, MLN_LEX_EINVCHAR);
return -1;
}
Expand Down
9 changes: 4 additions & 5 deletions src/mln_regexp.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include "mln_regexp.h"
#include "mln_func.h"

Expand Down Expand Up @@ -157,7 +156,7 @@ MLN_FUNC(static, int, mln_match_here, \
len -= mln_get_length(regexp+steplen+(reglen-len), reglen-steplen-(reglen-len));
continue;
}
if (!isdigit(c)) {
if (!mln_isdigit(c)) {
return -1;
}
existent = 1;
Expand Down Expand Up @@ -200,7 +199,7 @@ MLN_FUNC(static, int, mln_match_here, \
}
}
if (c_0 == M_REGEXP_NUM && textlen > 0) {
if (!isdigit(*text)) {
if (!mln_isdigit(*text)) {
return -1;
}
++text;
Expand All @@ -210,7 +209,7 @@ MLN_FUNC(static, int, mln_match_here, \
goto again;
}
if (c_0 == M_REGEXP_NOT_NUM && textlen > 0) {
if (isdigit(*text)) {
if (mln_isdigit(*text)) {
return -1;
}
++text;
Expand All @@ -220,7 +219,7 @@ MLN_FUNC(static, int, mln_match_here, \
goto again;
}
if (c_0 == M_REGEXP_ALPHA && textlen > 0) {
if (!isalpha(*text)) {
if (!mln_isalpha(*text)) {
return -1;
}
++text;
Expand Down
1 change: 0 additions & 1 deletion src/mln_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include "mln_string.h"
#include "mln_func.h"
Expand Down
4 changes: 2 additions & 2 deletions src/mln_tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ MLN_FUNC(, int, mln_s2time, (time_t *tm, mln_string_t *s, int type), (tm, s, typ
p = s->data;
end = s->data + s->len - 1;
if (s->len != 13 || (*end != 'Z' && *end != 'z')) return -1;
for (; p < end; ++p) if (!isdigit(*p)) return -1;
for (; p < end; ++p) if (!mln_isdigit(*p)) return -1;
p = s->data;
year = ((*p++) - '0') * 10;
year += ((*p++) - '0');
Expand All @@ -475,7 +475,7 @@ MLN_FUNC(, int, mln_s2time, (time_t *tm, mln_string_t *s, int type), (tm, s, typ
p = s->data;
end = s->data + s->len - 1;
if (s->len != 15 || (*end != 'Z' && *end != 'z')) return -1;
for (; p < end; ++p) if (!isdigit(*p)) return -1;
for (; p < end; ++p) if (!mln_isdigit(*p)) return -1;
p = s->data;
year = ((*p++) - '0') * 1000;
year += (((*p++) - '0') * 100);
Expand Down

0 comments on commit 54514c3

Please sign in to comment.