From 01a0b0da011e7e5414d6552611212f7c4c20546c Mon Sep 17 00:00:00 2001 From: c9s Date: Thu, 15 May 2014 21:17:30 +0800 Subject: [PATCH] combine_pattern function --- include/node.h | 2 ++ src/node.c | 37 ++++++++++++++++++++++++++++++++++--- tests/test_tree.c | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 3 deletions(-) diff --git a/include/node.h b/include/node.h index 73a374a..4986ad7 100644 --- a/include/node.h +++ b/include/node.h @@ -43,6 +43,8 @@ rnode * rnode_insert_routel(rnode *tree, char *route, int route_len); void rnode_dump(rnode * n, int level); +void rnode_combine_patterns(rnode * n); + rnode * rnode_lookup(rnode * tree, char * path, int path_len); redge * redge_create(char * pattern, int pattern_len, rnode * child); diff --git a/src/node.c b/src/node.c index 73b8212..1d8e11a 100644 --- a/src/node.c +++ b/src/node.c @@ -22,14 +22,16 @@ struct _rnode { int children_cap; /* the combined regexp pattern string from pattern_tokens */ - char* combined_pattern; + char * combined_pattern; + int combined_pattern_len; - int endpoint; + int endpoint; }; struct _redge { char * pattern; int pattern_len; + bool is_slug; rnode * child; }; @@ -41,12 +43,13 @@ struct _redge { * Create a rnode object */ rnode * rnode_create(int cap) { - rnode * n = (rnode*) calloc( sizeof(rnode) , 1); + rnode * n = (rnode*) malloc( sizeof(rnode) ); n->children = (redge**) malloc( sizeof(redge*) * 10 ); n->children_len = 0; n->children_cap = 10; n->endpoint = 0; + n->combined_pattern = NULL; // n->edge_patterns = token_array_create(10); return n; } @@ -103,11 +106,38 @@ redge * rnode_find_edge(rnode * n, char * pat) { return NULL; } + + +/** + * This function combines ['/foo', '/bar', '/{slug}'] into (/foo)|(/bar)|/([^/]+)} + * + */ void rnode_combine_patterns(rnode * n) { + char * cpat; + char * p; + + cpat = malloc(128); + if (cpat==NULL) + return; + + p = cpat; + redge *e = NULL; for ( int i = 0 ; i < n->children_len ; i++ ) { e = n->children[i]; + strncat(p++,"(", 1); + strncat(p, e->pattern, e->pattern_len); + + p += e->pattern_len; + + strncat(p++,")", 1); + + if ( i + 1 < n->children_len ) { + strncat(p++,"|",1); + } } + n->combined_pattern = cpat; + n->combined_pattern_len = p - cpat; } @@ -179,6 +209,7 @@ redge * redge_create(char * pattern, int pattern_len, rnode * child) { edge->pattern = pattern; edge->pattern_len = pattern_len; edge->child = child; + edge->is_slug = 0; return edge; } diff --git a/tests/test_tree.c b/tests/test_tree.c index fa7a421..ad4bd70 100644 --- a/tests/test_tree.c +++ b/tests/test_tree.c @@ -40,6 +40,28 @@ START_TEST (test_rnode_find_edge) END_TEST +START_TEST (test_combine_patterns) +{ + token_array *t; + rnode * n; + n = rnode_create(10); + + + t = split_route_pattern("/foo", strlen("/foo") ); + fail_if( rnode_insert_tokens(n , t) == NULL ); + + t = split_route_pattern("/bar", strlen("/bar") ); + fail_if( rnode_insert_tokens(n , t) == NULL ); + + t = split_route_pattern("/zoo", strlen("/zoo") ); + fail_if( rnode_insert_tokens(n , t) == NULL ); + + rnode_combine_patterns(n); + + // printf("%s\n", n->combined_pattern); +} +END_TEST + START_TEST (test_slug_to_pcre) { char * pattern; @@ -53,6 +75,17 @@ START_TEST (test_slug_to_pcre) + + /* + char * p = malloc(sizeof(char) * 10); + strncat(p, "foo", 3); + free(p); + */ + + + + + } END_TEST @@ -170,6 +203,7 @@ Suite* r3_suite (void) { tcase_add_test(tcase, test_rnode_find_edge); tcase_add_test(tcase, test_rnode_insert_tokens); tcase_add_test(tcase, test_slug_to_pcre); + tcase_add_test(tcase, test_combine_patterns); suite_add_tcase(suite, tcase);