From 98d9724d716589bcb2e0982f4d1c08dce58ae13c Mon Sep 17 00:00:00 2001 From: Olivier Matz Date: Fri, 4 Aug 2017 14:23:04 +0200 Subject: [PATCH] max parse len --- lib/ecoli_node_int.c | 7 +++++++ lib/ecoli_node_or.c | 15 +++++++++++++++ lib/ecoli_node_re.c | 7 +++++++ lib/ecoli_node_seq.c | 22 +++++++++++++++++++++- lib/ecoli_node_str.c | 9 ++++++++- 5 files changed, 58 insertions(+), 2 deletions(-) diff --git a/lib/ecoli_node_int.c b/lib/ecoli_node_int.c index 0635a16..a7bd057 100644 --- a/lib/ecoli_node_int.c +++ b/lib/ecoli_node_int.c @@ -97,10 +97,17 @@ static int ec_node_int_parse(const struct ec_node *gen_node, return 1; } +static size_t ec_node_int_get_max_parse_len(const struct ec_node *gen_node) +{ + (void)gen_node; + return 1; +} + static struct ec_node_type ec_node_int_type = { .name = "int", .parse = ec_node_int_parse, .complete = ec_node_default_complete, + .get_max_parse_len = ec_node_int_get_max_parse_len, .size = sizeof(struct ec_node_int), }; diff --git a/lib/ecoli_node_or.c b/lib/ecoli_node_or.c index a8d653c..dcd811b 100644 --- a/lib/ecoli_node_or.c +++ b/lib/ecoli_node_or.c @@ -93,6 +93,20 @@ ec_node_or_complete(const struct ec_node *gen_node, return completed; } +static size_t ec_node_or_get_max_parse_len(const struct ec_node *gen_node) +{ + struct ec_node_or *node = (struct ec_node_or *)gen_node; + size_t i, ret = 0, len; + + for (i = 0; i < node->len; i++) { + len = ec_node_get_max_parse_len(node->table[i]); + if (len > ret) + ret = len; + } + + return ret; +} + static void ec_node_or_free_priv(struct ec_node *gen_node) { struct ec_node_or *node = (struct ec_node_or *)gen_node; @@ -135,6 +149,7 @@ static struct ec_node_type ec_node_or_type = { .name = "or", .parse = ec_node_or_parse, .complete = ec_node_or_complete, + .get_max_parse_len = ec_node_or_get_max_parse_len, .size = sizeof(struct ec_node_or), .free_priv = ec_node_or_free_priv, }; diff --git a/lib/ecoli_node_re.c b/lib/ecoli_node_re.c index 984e686..5bcc407 100644 --- a/lib/ecoli_node_re.c +++ b/lib/ecoli_node_re.c @@ -69,6 +69,12 @@ ec_node_re_parse(const struct ec_node *gen_node, return 1; } +static size_t ec_node_re_get_max_parse_len(const struct ec_node *gen_node) +{ + (void)gen_node; + return 1; +} + static void ec_node_re_free_priv(struct ec_node *gen_node) { struct ec_node_re *node = (struct ec_node_re *)gen_node; @@ -81,6 +87,7 @@ static struct ec_node_type ec_node_re_type = { .name = "re", .parse = ec_node_re_parse, .complete = ec_node_default_complete, + .get_max_parse_len = ec_node_re_get_max_parse_len, .size = sizeof(struct ec_node_re), .free_priv = ec_node_re_free_priv, }; diff --git a/lib/ecoli_node_seq.c b/lib/ecoli_node_seq.c index 2d6363e..9fa2d1a 100644 --- a/lib/ecoli_node_seq.c +++ b/lib/ecoli_node_seq.c @@ -27,6 +27,7 @@ #include #include +#include #include #include #include @@ -41,12 +42,14 @@ #include #include #include +#include +#include #include struct ec_node_seq { struct ec_node gen; struct ec_node **table; - unsigned int len; + size_t len; }; static int @@ -182,6 +185,22 @@ ec_node_seq_complete(const struct ec_node *gen_node, return __ec_node_seq_complete(node->table, node->len, state, strvec); } +static size_t ec_node_seq_get_max_parse_len(const struct ec_node *gen_node) +{ + struct ec_node_seq *node = (struct ec_node_seq *)gen_node; + size_t i, len, ret = 0; + + for (i = 0; i < node->len; i++) { + len = ec_node_get_max_parse_len(node->table[i]); + if (len <= SIZE_MAX - ret) + ret += len; + else + ret = SIZE_MAX; + } + + return ret; +} + static void ec_node_seq_free_priv(struct ec_node *gen_node) { struct ec_node_seq *node = (struct ec_node_seq *)gen_node; @@ -196,6 +215,7 @@ static struct ec_node_type ec_node_seq_type = { .name = "seq", .parse = ec_node_seq_parse, .complete = ec_node_seq_complete, + .get_max_parse_len = ec_node_seq_get_max_parse_len, .size = sizeof(struct ec_node_seq), .free_priv = ec_node_seq_free_priv, }; diff --git a/lib/ecoli_node_str.c b/lib/ecoli_node_str.c index 62ec788..85a4046 100644 --- a/lib/ecoli_node_str.c +++ b/lib/ecoli_node_str.c @@ -55,7 +55,7 @@ ec_node_str_parse(const struct ec_node *gen_node, (void)state; - if (ec_strvec_len(strvec) == 0) + if (ec_strvec_len(strvec) != 1) return EC_PARSED_NOMATCH; str = ec_strvec_val(strvec, 0); @@ -117,10 +117,17 @@ static void ec_node_str_free_priv(struct ec_node *gen_node) ec_free(node->string); } +static size_t ec_node_str_get_max_parse_len(const struct ec_node *gen_node) +{ + (void)gen_node; + return 1; +} + static struct ec_node_type ec_node_str_type = { .name = "str", .parse = ec_node_str_parse, .complete = ec_node_str_complete, + .get_max_parse_len = ec_node_str_get_max_parse_len, .desc = ec_node_str_desc, .size = sizeof(struct ec_node_str), .free_priv = ec_node_str_free_priv, -- 2.39.5