From 401443f7ac0a73a74596394527ee0b4dabd42eff Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 19 May 2014 10:39:03 +0800 Subject: [PATCH] Add r3_ prefix to route struct related functions --- CHANGES.md | 3 +++ README.md | 7 +++++++ include/r3.h | 8 ++++---- src/node.c | 12 ++++++------ tests/bench_str.csv | 1 + tests/check_tree.c | 20 ++++++++++---------- 6 files changed, 31 insertions(+), 20 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 59f0173..3965a49 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -26,3 +26,6 @@ This reduce the interface complexity, e.g., 6. A path that is inserted by `r3_tree_insert_route` can only be matched by `r3_tree_match_route`. +7. Add `r3_` prefix to `route` related methods. + + diff --git a/README.md b/README.md index dca5977..2ccd059 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,9 @@ the catched variables will be pushed into the match entry structure: ```c match_entry * entry = match_entry_create("/foo/bar"); + +// free the match entry +match_entry_free(entry); ``` And you can even specify the request method restriction: @@ -105,6 +108,10 @@ r3_tree_compile(n); route *matched_route = r3_tree_match_route(n, entry); matched_route->data; // get the data from matched route + +// free the objects at the end +r3_route_free(r1); +r3_tree_free(n); ``` diff --git a/include/r3.h b/include/r3.h index 8f7d119..a1b23f7 100644 --- a/include/r3.h +++ b/include/r3.h @@ -155,15 +155,15 @@ match_entry * match_entry_createl(char * path, int path_len); void match_entry_free(match_entry * entry); -route * route_create(char * path); +route * r3_route_create(char * path); -route * route_createl(char * path, int path_len); +route * r3_route_createl(char * path, int path_len); -int route_cmp(route *r1, match_entry *r2); +int r3_route_cmp(route *r1, match_entry *r2); void r3_node_append_route(node * n, route * route); -void route_free(route * route); +void r3_route_free(route * route); route * r3_tree_match_route(node *n, match_entry * entry); diff --git a/src/node.c b/src/node.c index a8aa9ab..d4963f4 100644 --- a/src/node.c +++ b/src/node.c @@ -306,7 +306,7 @@ route * r3_tree_match_route(node *tree, match_entry * entry) { if (n->routes && n->route_len > 0) { int i; for (i = 0; i < n->route_len ; i++ ) { - if ( route_cmp(n->routes[i], entry) == 0 ) { + if ( r3_route_cmp(n->routes[i], entry) == 0 ) { return n->routes[i]; } } @@ -351,15 +351,15 @@ node * r3_node_create() { } -route * route_create(char * path) { - return route_createl(path, strlen(path)); +route * r3_route_create(char * path) { + return r3_route_createl(path, strlen(path)); } -void route_free(route * route) { +void r3_route_free(route * route) { free(route); } -route * route_createl(char * path, int path_len) { +route * r3_route_createl(char * path, int path_len) { route * info = malloc(sizeof(route)); info->path = path; info->path_len = path_len; @@ -527,7 +527,7 @@ void r3_tree_dump(node * n, int level) { * * -1 == different route */ -int route_cmp(route *r1, match_entry *r2) { +int r3_route_cmp(route *r1, match_entry *r2) { if (r1->request_method != 0) { if (0 == (r1->request_method & r2->request_method) ) { return -1; diff --git a/tests/bench_str.csv b/tests/bench_str.csv index 0ed01bc..1d50e1c 100644 --- a/tests/bench_str.csv +++ b/tests/bench_str.csv @@ -327,3 +327,4 @@ 1400466198,13359933.76 1400466833,13166545.46 1400466875,13515485.94 +1400467139,13047096.38 diff --git a/tests/check_tree.c b/tests/check_tree.c index dbf9520..bb69248 100644 --- a/tests/check_tree.c +++ b/tests/check_tree.c @@ -245,24 +245,24 @@ END_TEST START_TEST(test_route_cmp) { - route *r1 = route_create("/blog/post"); + route *r1 = r3_route_create("/blog/post"); match_entry * m = match_entry_create("/blog/post"); - fail_if( route_cmp(r1, m) == -1, "should match"); + fail_if( r3_route_cmp(r1, m) == -1, "should match"); r1->request_method = METHOD_GET; m->request_method = METHOD_GET; - fail_if( route_cmp(r1, m) == -1, "should match"); + fail_if( r3_route_cmp(r1, m) == -1, "should match"); r1->request_method = METHOD_GET; m->request_method = METHOD_POST; - fail_if( route_cmp(r1, m) == 0, "should be different"); + fail_if( r3_route_cmp(r1, m) == 0, "should be different"); r1->request_method = METHOD_GET; m->request_method = METHOD_POST | METHOD_GET; - fail_if( route_cmp(r1, m) == -1, "should match"); + fail_if( r3_route_cmp(r1, m) == -1, "should match"); - route_free(r1); + r3_route_free(r1); match_entry_free(m); } END_TEST @@ -340,8 +340,8 @@ START_TEST(test_insert_route) { int var1 = 22; int var2 = 33; - route *r1 = route_create("/blog/post"); - route *r2 = route_create("/blog/post"); + route *r1 = r3_route_create("/blog/post"); + route *r2 = r3_route_create("/blog/post"); r1->request_method = METHOD_GET; r2->request_method = METHOD_POST; @@ -356,8 +356,8 @@ START_TEST(test_insert_route) fail_if(c == NULL); match_entry_free(entry); - route_free(r1); - route_free(r2); + r3_route_free(r1); + r3_route_free(r2); } END_TEST