From 4380f7e63a9982f875faec5355764dd952daacf5 Mon Sep 17 00:00:00 2001 From: c9s Date: Fri, 16 May 2014 14:24:00 +0800 Subject: [PATCH] rnode_match test --- src/node.c | 33 ++++++++++++++++++++++++--------- tests/test_tree.c | 12 ++++++++++++ 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/node.c b/src/node.c index 0eb2787..5e16282 100644 --- a/src/node.c +++ b/src/node.c @@ -161,13 +161,14 @@ void rnode_compile_patterns(rnode * n) { } -rnode * rnode_match(rnode * n, char * path, int path_len) { - int ovector_count = (n->edge_len + 1) * 2; - int ovector[ovector_count]; + +rnode * rnode_match(rnode * n, char * path, int path_len) { if (n->combined_pattern && n->pcre_pattern) { - printf("pcre matching /%s/ on %s\n", n->combined_pattern, path); - // use PCRE for now + // printf("pcre matching /%s/ on %s\n", n->combined_pattern, path); + // int ovector_count = (n->edge_len + 1) * 2; + int ovector_count = 30; + int ovector[ovector_count]; int rc; rc = pcre_exec( n->pcre_pattern, /* the compiled pattern */ @@ -197,15 +198,29 @@ rnode * rnode_match(rnode * n, char * path, int path_len) { for (i = 1; i < rc; i++) { char *substring_start = path + ovector[2*i]; - int substring_length = ovector[2*i+1] - ovector[2*i]; + int substring_length = ovector[2*i+1] - ovector[2*i]; printf("%2d: %.*s\n", i, substring_length, substring_start); if ( substring_length > 0) { - return n->edges[i]->child; + int len = path_len - substring_length; // fully match to the end + // printf("len:%d edges:%d i:%d\n", len, n->edge_len, i); + if (len) { + return rnode_match( n->edges[i - 1]->child, substring_start, len); + } + return n->edges[i - 1]->child; } } + // does not match + return NULL; + } - } else { - + redge *e = rnode_find_edge_str(n, path, path_len); + if (e) { + int len = path_len - e->pattern_len; + if(len == 0) { + return e->child; + } else { + return rnode_match(e->child, path + e->pattern_len, len); + } } return NULL; } diff --git a/tests/test_tree.c b/tests/test_tree.c index 92c737c..57706e3 100644 --- a/tests/test_tree.c +++ b/tests/test_tree.c @@ -71,6 +71,18 @@ START_TEST (test_compile) */ rnode *m = rnode_match( e->child , "foo", strlen("foo") ); fail_if( NULL == m ); + + m = rnode_match( n , "/foo", strlen("/foo") ); + fail_if( NULL == m ); + + m = rnode_match( n , "/zoo", strlen("/zoo") ); + fail_if( NULL == m ); + + m = rnode_match( n , "/bar", strlen("/bar") ); + fail_if( NULL == m ); + + m = rnode_match( n , "/zzz", strlen("/zzz") ); + fail_if( NULL == m ); } END_TEST