append captured tokens to match_entry
This commit is contained in:
parent
ebe4c008fb
commit
2a0e120a91
4 changed files with 33 additions and 10 deletions
|
@ -16,6 +16,10 @@
|
|||
|
||||
#include "token.h"
|
||||
|
||||
#define node_edge_pattern(node,i) node->edges[i]->pattern
|
||||
#define node_edge_pattern_len(node,i) node->edges[i]->pattern_len
|
||||
|
||||
|
||||
struct _edge;
|
||||
struct _node;
|
||||
typedef struct _edge edge;
|
||||
|
@ -48,9 +52,9 @@ struct _edge {
|
|||
};
|
||||
|
||||
typedef struct {
|
||||
char ** vars;
|
||||
int vars_len;
|
||||
str_array * vars;
|
||||
char * path; // dispatched path
|
||||
int path_len;
|
||||
void * route_ptr; // route ptr
|
||||
} match_entry;
|
||||
|
||||
|
@ -96,5 +100,8 @@ void r3_edge_free(edge * edge);
|
|||
|
||||
|
||||
|
||||
match_entry * match_entry_create(char * path, int path_len);
|
||||
|
||||
void match_entry_free(match_entry * entry);
|
||||
|
||||
#endif /* !NODE_H */
|
||||
|
|
|
@ -18,7 +18,6 @@ typedef struct _str_array {
|
|||
|
||||
str_array * str_array_create(int cap);
|
||||
|
||||
|
||||
bool str_array_is_full(str_array * l);
|
||||
|
||||
bool str_array_resize(str_array *l, int new_cap);
|
||||
|
|
21
src/node.c
21
src/node.c
|
@ -85,7 +85,6 @@ void r3_tree_append_edge(node *n, edge *e) {
|
|||
n->edges[ n->r3_edge_len++ ] = e;
|
||||
}
|
||||
|
||||
|
||||
edge * r3_node_find_edge(node * n, char * pat) {
|
||||
edge * e;
|
||||
for (int i = 0 ; i < n->r3_edge_len ; i++ ) {
|
||||
|
@ -181,6 +180,21 @@ void r3_tree_compile_patterns(node * n) {
|
|||
}
|
||||
|
||||
|
||||
match_entry * match_entry_create(char * path, int path_len) {
|
||||
match_entry * entry = malloc(sizeof(match_entry));
|
||||
if(!entry)
|
||||
return NULL;
|
||||
entry->vars = str_array_create(3);
|
||||
entry->path = path;
|
||||
entry->path_len = path_len;
|
||||
entry->route_ptr = NULL;
|
||||
return entry;
|
||||
}
|
||||
|
||||
void match_entry_free(match_entry * entry) {
|
||||
str_array_free(entry->vars);
|
||||
free(entry);
|
||||
}
|
||||
|
||||
|
||||
node * r3_tree_match(node * n, char * path, int path_len, match_entry * entry) {
|
||||
|
@ -234,7 +248,8 @@ node * r3_tree_match(node * n, char * path, int path_len, match_entry * entry) {
|
|||
e = n->edges[i - 1];
|
||||
|
||||
if (entry && e->has_slug) {
|
||||
// entry->
|
||||
// append captured token to entry
|
||||
str_array_append(entry->vars , strndup(substring_start, substring_length));
|
||||
}
|
||||
if (restlen == 0) {
|
||||
return e->child;
|
||||
|
@ -258,8 +273,6 @@ node * r3_tree_match(node * n, char * path, int path_len, match_entry * entry) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
#define node_edge_pattern(node,i) node->edges[i]->pattern
|
||||
#define node_edge_pattern_len(node,i) node->edges[i]->pattern_len
|
||||
|
||||
inline edge * r3_node_find_edge_str(node * n, char * str, int str_len) {
|
||||
int i = 0;
|
||||
|
|
|
@ -80,7 +80,6 @@ START_TEST (test_compile)
|
|||
n = r3_tree_create(10);
|
||||
|
||||
|
||||
match_entry * entry;
|
||||
node *m;
|
||||
edge *e ;
|
||||
|
||||
|
@ -115,24 +114,29 @@ START_TEST (test_compile)
|
|||
printf( "%s\n", n->combined_pattern );
|
||||
*/
|
||||
|
||||
entry = calloc( sizeof(entry) , 1 );
|
||||
|
||||
match_entry * entry;
|
||||
|
||||
entry = match_entry_create( "foo" , strlen("/foo") );
|
||||
m = r3_tree_match( n , "/foo", strlen("/foo"), entry);
|
||||
fail_if( NULL == m );
|
||||
|
||||
entry = match_entry_create( "/zoo" , strlen("/zoo") );
|
||||
m = r3_tree_match( n , "/zoo", strlen("/zoo"), entry);
|
||||
fail_if( NULL == m );
|
||||
|
||||
entry = match_entry_create( "/bar" , strlen("/bar") );
|
||||
m = r3_tree_match( n , "/bar", strlen("/bar"), entry);
|
||||
fail_if( NULL == m );
|
||||
|
||||
entry = match_entry_create( "/xxx" , strlen("/xxx") );
|
||||
m = r3_tree_match( n , "/xxx", strlen("/xxx"), entry);
|
||||
fail_if( NULL == m );
|
||||
|
||||
entry = match_entry_create( "/foo/xxx" , strlen("/foo/xxx") );
|
||||
m = r3_tree_match( n , "/foo/xxx", strlen("/foo/xxx"), entry);
|
||||
fail_if( NULL == m );
|
||||
|
||||
entry = match_entry_create( "/some_id" , strlen("/some_id") );
|
||||
m = r3_tree_match( n , "/some_id", strlen("/some_id"), entry);
|
||||
fail_if( NULL == m );
|
||||
ck_assert_int_gt( m->endpoint , 0 ); // should not be an endpoint
|
||||
|
|
Loading…
Reference in a new issue