Fix bugs in matcher. Improve code.

This commit is contained in:
CismonX 2020-12-17 15:53:20 +08:00
parent 7bee35ba94
commit adeaf195b6
Signed by: cismonx
GPG Key ID: 3094873E29A482FB
2 changed files with 38 additions and 37 deletions

View File

@ -63,26 +63,26 @@
# define CTLSEQS_TRIE_INIT_SIZE 16 # define CTLSEQS_TRIE_INIT_SIZE 16
#endif // !CTLSEQS_TRIE_INIT_SIZE #endif // !CTLSEQS_TRIE_INIT_SIZE
#define CTLSEQS_VALUE_STR(stop_cond) \ #define CTLSEQS_VALUE_STR(stop_cond) \
for (cnt = 0; ; ++cnt) { \ for (cnt = 0; ; ++cnt) { \
num = seq_val[cnt]; \ num = seq_val[cnt]; \
if (stop_cond) { \ if (stop_cond) { \
break; \ break; \
} \ } \
} \ } \
buf[0].num = cnt; \ buf_val[0].num = cnt; \
buf[1].str = seq_val; \ buf_val[1].str = seq_val; \
*seq = seq_val + cnt; \ *seq = seq_val + cnt; \
*buf_offset += 2; \ *buf += 2; \
return true return true
#define CTLSEQS_VALUE_NUM(base) \ #define CTLSEQS_VALUE_NUM(base) \
errno = 0; \ errno = 0; \
num = strtoul(seq_val, seq, base); \ num = strtoul(seq_val, seq, base); \
if (errno != 0 || seq_val == *seq) { \ if (errno || seq_val == *seq) { \
return false; \ return false; \
} \ } \
buf[0].num = num; \ buf_val[0].num = num; \
*buf_offset += 1; \ ++*buf; \
return true return true
enum ctlseqs_placeholder { enum ctlseqs_placeholder {
@ -120,9 +120,9 @@ struct ctlseqs_trie_node {
struct ctlseqs_match_ctx { struct ctlseqs_match_ctx {
ssize_t value; ssize_t value;
size_t result_idx;
struct ctlseqs_trie_node const *node; struct ctlseqs_trie_node const *node;
char *rbuf; char *rbuf;
union ctlseqs_value *result;
}; };
struct ctlseqs_matcher { struct ctlseqs_matcher {
@ -233,29 +233,29 @@ ctlseqs_state_transition(enum ctlseqs_state state, char ch)
} }
CTLSEQS_HOT static bool CTLSEQS_HOT static bool
ctlseqs_fetch(char **seq, int type, union ctlseqs_value *buf, size_t *buf_offset) ctlseqs_fetch(char **seq, int type, union ctlseqs_value **buf)
{ {
unsigned long cnt, num; unsigned long cnt, num;
char *seq_val = *seq; char *seq_val = *seq;
buf += *buf_offset; union ctlseqs_value *buf_val = *buf;
switch (type) { switch (type) {
case ctlseqs_ph_num: case ctlseqs_ph_num:
CTLSEQS_VALUE_NUM(10); CTLSEQS_VALUE_NUM(10);
case ctlseqs_ph_nums: case ctlseqs_ph_nums:
for (cnt = 1; ; ++cnt) { for (cnt = 1; ; ++cnt) {
errno = 0;
num = strtoul(seq_val, seq, 10); num = strtoul(seq_val, seq, 10);
if (errno != 0 || seq_val == *seq) { if (errno || seq_val == *seq) {
return false; return false;
} }
buf[cnt].num = num; buf_val[cnt].num = num;
if (seq_val[0] == ';') { if ((*seq)[0] != ';') {
++seq_val;
} else {
break; break;
} }
seq_val = *seq + 1;
} }
buf[0].num = cnt; buf_val[0].num = cnt;
*buf_offset += 1; *buf += cnt + 1;
return true; return true;
case ctlseqs_ph_str: case ctlseqs_ph_str:
CTLSEQS_VALUE_STR(num < ' ' || num > '~'); CTLSEQS_VALUE_STR(num < ' ' || num > '~');
@ -280,8 +280,9 @@ ctlseqs_match_pattern(struct ctlseqs_reader *reader, struct ctlseqs_matcher cons
{ {
struct ctlseqs_trie_node const *old_node, empty_node = { 0 }; struct ctlseqs_trie_node const *old_node, empty_node = { 0 };
struct ctlseqs_match_ctx match_ctx = { struct ctlseqs_match_ctx match_ctx = {
.node = matcher == NULL ? &empty_node : &matcher->root, .node = matcher == NULL ? &empty_node : &matcher->root,
.rbuf = reader->rbuf + reader->buf_start + 1, .rbuf = reader->rbuf + reader->buf_start + 1,
.result = reader->buffer,
}; };
ssize_t match_stack_top = -1; ssize_t match_stack_top = -1;
while (true) { while (true) {
@ -298,12 +299,12 @@ ctlseqs_match_pattern(struct ctlseqs_reader *reader, struct ctlseqs_matcher cons
match_ctx.node = match_ctx.node->children[-match_ctx.value]; match_ctx.node = match_ctx.node->children[-match_ctx.value];
struct ctlseqs_trie_node *next_node = match_ctx.node->next; struct ctlseqs_trie_node *next_node = match_ctx.node->next;
matcher->match_stack[++match_stack_top] = (struct ctlseqs_match_ctx) { matcher->match_stack[++match_stack_top] = (struct ctlseqs_match_ctx) {
.value = next_node == NULL ? 0 : -next_node->placeholder, .value = next_node == NULL ? 0 : -next_node->placeholder,
.node = old_node, .node = old_node,
.rbuf = match_ctx.rbuf, .rbuf = match_ctx.rbuf,
.result_idx = match_ctx.result_idx, .result = match_ctx.result,
}; };
if (!ctlseqs_fetch(&match_ctx.rbuf, -match_ctx.value, reader->buffer, &match_ctx.result_idx)) { if (!ctlseqs_fetch(&match_ctx.rbuf, -match_ctx.value, &match_ctx.result)) {
break; break;
} }
} else { } else {

View File

@ -287,7 +287,7 @@
// XTGETTCAP response message // XTGETTCAP response message
#define CTLSEQS_RESP_XTGETTCAP(n, s) CTLSEQS_DCS n "+r" s CTLSEQS_ST #define CTLSEQS_RESP_XTGETTCAP(n, s) CTLSEQS_DCS n "+r" s CTLSEQS_ST
// Primary DA response message // Primary DA response message
#define CTLSEQS_RESP_PRIMARY_DA(ns) CTLSEQS_CSI ns "c" #define CTLSEQS_RESP_PRIMARY_DA(ns) CTLSEQS_CSI "?" ns "c"
// Secondary DA response message // Secondary DA response message
#define CTLSEQS_RESP_SECONDARY_DA(n1, n2, n3) CTLSEQS_CSI ">" n1 ";" n2 ";" n3 "c" #define CTLSEQS_RESP_SECONDARY_DA(n1, n2, n3) CTLSEQS_CSI ">" n1 ";" n2 ";" n3 "c"
// DECLRP response message // DECLRP response message