Add namespace for node/edge structs

node => R3Node
edge => R3Edge

Related issue #63
This commit is contained in:
c9s 2015-11-18 12:52:06 +08:00
parent ef55fb9cdb
commit 49ffb454f7
8 changed files with 131 additions and 132 deletions

View file

@ -10,7 +10,7 @@
int main() int main()
{ {
node * n = r3_tree_create(3); R3Node * n = r3_tree_create(3);
r3_tree_insert_path(n, "/foo/bar/baz", NULL); r3_tree_insert_path(n, "/foo/bar/baz", NULL);
r3_tree_insert_path(n, "/foo/bar/qux", NULL); r3_tree_insert_path(n, "/foo/bar/qux", NULL);
@ -44,7 +44,7 @@ int main()
return 1; return 1;
} }
node *m; R3Node *m;
m = r3_tree_match(n , "/qux/bar/corge", NULL); m = r3_tree_match(n , "/qux/bar/corge", NULL);

View file

@ -41,12 +41,12 @@ extern "C" {
struct _edge; struct _edge;
struct _node; struct _node;
struct _route; struct _route;
typedef struct _edge edge; typedef struct _edge R3Edge;
typedef struct _node node; typedef struct _node R3Node;
typedef struct _route route; typedef struct _route route;
struct _node { struct _node {
edge * edges; R3Edge * edges;
char * combined_pattern; char * combined_pattern;
pcre * pcre_pattern; pcre * pcre_pattern;
pcre_extra * pcre_extra; pcre_extra * pcre_extra;
@ -73,7 +73,7 @@ struct _node {
struct _edge { struct _edge {
char * pattern; // 8 bytes char * pattern; // 8 bytes
node * child; // 8 bytes R3Node * child; // 8 bytes
unsigned int pattern_len; // 4byte unsigned int pattern_len; // 4byte
unsigned int opcode; // 4byte unsigned int opcode; // 4byte
unsigned int has_slug; // 4byte unsigned int has_slug; // 4byte
@ -116,32 +116,31 @@ typedef struct {
node * r3_tree_create(int cap); R3Node * r3_tree_create(int cap);
node * r3_node_create(); R3Node * r3_node_create();
void r3_tree_free(node * tree); void r3_tree_free(R3Node * tree);
edge * r3_node_connectl(node * n, const char * pat, int len, int strdup, node *child); R3Edge * r3_node_connectl(R3Node * n, const char * pat, int len, int strdup, R3Node *child);
#define r3_node_connect(n, pat, child) r3_node_connectl(n, pat, strlen(pat), 0, child) #define r3_node_connect(n, pat, child) r3_node_connectl(n, pat, strlen(pat), 0, child)
edge * r3_node_find_edge(const node * n, const char * pat, int pat_len); R3Edge * r3_node_find_edge(const R3Node * n, const char * pat, int pat_len);
edge * r3_node_append_edge(node *n, edge *child); R3Edge * r3_node_append_edge(R3Node *n, R3Edge *child);
R3Edge * r3_node_find_common_prefix(R3Node *n, const char *path, int path_len, int *prefix_len, char **errstr);
edge * r3_node_find_common_prefix(node *n, const char *path, int path_len, int *prefix_len, char **errstr); R3Node * r3_tree_insert_pathl(R3Node *tree, const char *path, int path_len, void * data);
node * r3_tree_insert_pathl(node *tree, const char *path, int path_len, void * data);
#define r3_tree_insert_pathl(tree, path, path_len, data) r3_tree_insert_pathl_ex(tree, path, path_len, NULL , data, NULL) #define r3_tree_insert_pathl(tree, path, path_len, data) r3_tree_insert_pathl_ex(tree, path, path_len, NULL , data, NULL)
route * r3_tree_insert_routel(node *tree, int method, const char *path, int path_len, void *data); route * r3_tree_insert_routel(R3Node * tree, int method, const char *path, int path_len, void *data);
route * r3_tree_insert_routel_ex(node *tree, int method, const char *path, int path_len, void *data, char **errstr); route * r3_tree_insert_routel_ex(R3Node * tree, int method, const char *path, int path_len, void *data, char **errstr);
#define r3_tree_insert_routel(n, method, path, path_len, data) r3_tree_insert_routel_ex(n, method, path, path_len, data, NULL) #define r3_tree_insert_routel(n, method, path, path_len, data) r3_tree_insert_routel_ex(n, method, path, path_len, data, NULL)
@ -153,34 +152,34 @@ route * r3_tree_insert_routel_ex(node *tree, int method, const char *path, int p
/** /**
* The private API to insert a path * The private API to insert a path
*/ */
node * r3_tree_insert_pathl_ex(node *tree, const char *path, int path_len, route * route, void * data, char ** errstr); R3Node * r3_tree_insert_pathl_ex(R3Node *tree, const char *path, int path_len, route * route, void * data, char ** errstr);
void r3_tree_dump(const node * n, int level); void r3_tree_dump(const R3Node * n, int level);
edge * r3_node_find_edge_str(const node * n, const char * str, int str_len); R3Edge * r3_node_find_edge_str(const R3Node * n, const char * str, int str_len);
int r3_tree_compile(node *n, char** errstr); int r3_tree_compile(R3Node *n, char** errstr);
int r3_tree_compile_patterns(node * n, char** errstr); int r3_tree_compile_patterns(R3Node * n, char** errstr);
node * r3_tree_matchl(const node * n, const char * path, int path_len, match_entry * entry); R3Node * r3_tree_matchl(const R3Node * n, const char * path, int path_len, match_entry * entry);
#define r3_tree_match(n,p,e) r3_tree_matchl(n,p, strlen(p), e) #define r3_tree_match(n,p,e) r3_tree_matchl(n,p, strlen(p), e)
// node * r3_tree_match_entry(node * n, match_entry * entry); // R3Node * r3_tree_match_entry(R3Node * n, match_entry * entry);
#define r3_tree_match_entry(n, entry) r3_tree_matchl(n, entry->path, entry->path_len, entry) #define r3_tree_match_entry(n, entry) r3_tree_matchl(n, entry->path, entry->path_len, entry)
bool r3_node_has_slug_edges(const node *n); bool r3_node_has_slug_edges(const R3Node *n);
edge * r3_edge_createl(const char * pattern, int pattern_len, node * child); R3Edge * r3_edge_createl(const char * pattern, int pattern_len, R3Node * child);
void r3_edge_initl(edge *e, const char * pattern, int pattern_len, node * child); void r3_edge_initl(R3Edge *e, const char * pattern, int pattern_len, R3Node * child);
node * r3_edge_branch(edge *e, int dl); R3Node * r3_edge_branch(R3Edge *e, int dl);
void r3_edge_free(edge * edge); void r3_edge_free(R3Edge * edge);
@ -191,13 +190,13 @@ route * r3_route_create(const char * path);
route * r3_route_createl(const char * path, int path_len); route * r3_route_createl(const char * path, int path_len);
void r3_node_append_route(node * n, route * route); void r3_node_append_route(R3Node * n, route * route);
void r3_route_free(route * route); void r3_route_free(route * route);
int r3_route_cmp(const route *r1, const match_entry *r2); int r3_route_cmp(const route *r1, const match_entry *r2);
route * r3_tree_match_route(const node *n, match_entry * entry); route * r3_tree_match_route(const R3Node *n, match_entry * entry);
#define r3_route_create(p) r3_route_createl(p, strlen(p)) #define r3_route_create(p) r3_route_createl(p, strlen(p))

View file

@ -37,7 +37,7 @@ namespace r3 {
private: private:
T* p_; T* p_;
}; };
typedef Base<node> Node; typedef Base<R3Node> Node;
typedef Base<route> Route; typedef Base<route> Route;
class MatchEntry : public Base<match_entry> { class MatchEntry : public Base<match_entry> {
@ -69,7 +69,7 @@ namespace r3 {
MatchEntry& operator =(const MatchEntry&); MatchEntry& operator =(const MatchEntry&);
}; };
class Tree : public Base<node> { class Tree : public Base<R3Node> {
public: public:
explicit Tree(int cap) explicit Tree(int cap)
: Base(r3_tree_create(cap)) { : Base(r3_tree_create(cap)) {

View file

@ -25,7 +25,7 @@
void r3_edge_initl(edge *e, const char * pattern, int pattern_len, node * child) void r3_edge_initl(R3Edge *e, const char * pattern, int pattern_len, R3Node * child)
{ {
e->pattern = (char*) pattern; e->pattern = (char*) pattern;
e->pattern_len = pattern_len; e->pattern_len = pattern_len;
@ -34,9 +34,9 @@ void r3_edge_initl(edge *e, const char * pattern, int pattern_len, node * child)
e->has_slug = r3_path_contains_slug_char(e->pattern); e->has_slug = r3_path_contains_slug_char(e->pattern);
} }
edge * r3_edge_createl(const char * pattern, int pattern_len, node * child) R3Edge * r3_edge_createl(const char * pattern, int pattern_len, R3Node * child)
{ {
edge * e = (edge*) zmalloc( sizeof(edge) ); R3Edge * e = (R3Edge*) zmalloc( sizeof(R3Edge) );
CHECK_PTR(e); CHECK_PTR(e);
e->pattern = (char*) pattern; e->pattern = (char*) pattern;
e->pattern_len = pattern_len; e->pattern_len = pattern_len;
@ -59,9 +59,9 @@ edge * r3_edge_createl(const char * pattern, int pattern_len, node * child)
* A -> [EDGE: abcd] -> B1 -> [efg] -> B2 (new child with copied data from B) * A -> [EDGE: abcd] -> B1 -> [efg] -> B2 (new child with copied data from B)
* *
*/ */
node * r3_edge_branch(edge *e, int dl) { R3Node * r3_edge_branch(R3Edge *e, int dl) {
node * new_child; R3Node * new_child;
edge * new_edge; R3Edge * new_edge;
// the rest string // the rest string
char * s1 = e->pattern + dl; char * s1 = e->pattern + dl;
@ -85,7 +85,7 @@ node * r3_edge_branch(edge *e, int dl) {
return new_child; return new_child;
} }
void r3_edge_free(edge * e) { void r3_edge_free(R3Edge * e) {
zfree(e->pattern); zfree(e->pattern);
if ( e->child ) { if ( e->child ) {
r3_tree_free(e->child); r3_tree_free(e->child);

View file

@ -52,11 +52,11 @@ static int strdiff(char * d1, char * d2) {
/** /**
* Create a node object * Create a node object
*/ */
node * r3_tree_create(int cap) { R3Node * r3_tree_create(int cap) {
node * n = (node*) zmalloc( sizeof(node) ); R3Node * n = (R3Node*) zmalloc( sizeof(R3Node) );
CHECK_PTR(n); CHECK_PTR(n);
n->edges = (edge*) zmalloc(sizeof(edge) * cap); n->edges = (R3Edge*) zmalloc(sizeof(R3Edge) * cap);
CHECK_PTR(n->edges); CHECK_PTR(n->edges);
n->edge_len = 0; n->edge_len = 0;
n->edge_cap = cap; n->edge_cap = cap;
@ -73,7 +73,7 @@ node * r3_tree_create(int cap) {
return n; return n;
} }
void r3_tree_free(node * tree) { void r3_tree_free(R3Node * tree) {
if (tree->edges) { if (tree->edges) {
zfree(tree->edges); zfree(tree->edges);
} }
@ -96,9 +96,9 @@ void r3_tree_free(node * tree) {
/** /**
* Connect two node objects, and create an edge object between them. * Connect two node objects, and create an edge object between them.
*/ */
edge * r3_node_connectl(node * n, const char * pat, int len, int dupl, node *child) { R3Edge * r3_node_connectl(R3Node * n, const char * pat, int len, int dupl, R3Node *child) {
// find the same sub-pattern, if it does not exist, create one // find the same sub-pattern, if it does not exist, create one
edge * e; R3Edge * e;
e = r3_node_find_edge(n, pat, len); e = r3_node_find_edge(n, pat, len);
if (e) { if (e) {
@ -110,20 +110,20 @@ edge * r3_node_connectl(node * n, const char * pat, int len, int dupl, node *chi
} }
e = r3_edge_createl(pat, len, child); e = r3_edge_createl(pat, len, child);
CHECK_PTR(e); CHECK_PTR(e);
edge *e2 = r3_node_append_edge(n, e); R3Edge *e2 = r3_node_append_edge(n, e);
zfree(e); zfree(e);
return e2; return e2;
} }
edge * r3_node_append_edge(node *n, edge *e) R3Edge * r3_node_append_edge(R3Node *n, R3Edge *e)
{ {
if (n->edges == NULL) { if (n->edges == NULL) {
n->edge_cap = 3; n->edge_cap = 3;
n->edges = zmalloc(sizeof(edge) * n->edge_cap); n->edges = zmalloc(sizeof(R3Edge) * n->edge_cap);
} }
if (n->edge_len >= n->edge_cap) { if (n->edge_len >= n->edge_cap) {
n->edge_cap *= 2; n->edge_cap *= 2;
edge * p = zrealloc(n->edges, sizeof(edge) * n->edge_cap); R3Edge * p = zrealloc(n->edges, sizeof(R3Edge) * n->edge_cap);
if(p) { if(p) {
n->edges = p; n->edges = p;
} }
@ -141,8 +141,8 @@ edge * r3_node_append_edge(node *n, edge *e)
* *
* if "pat" is a slug, we should compare with the specified pattern. * if "pat" is a slug, we should compare with the specified pattern.
*/ */
edge * r3_node_find_edge(const node * n, const char * pat, int pat_len) { R3Edge * r3_node_find_edge(const R3Node * n, const char * pat, int pat_len) {
edge * e; R3Edge * e;
int i; int i;
for (i = 0 ; i < n->edge_len ; i++ ) { for (i = 0 ; i < n->edge_len ; i++ ) {
e = &n->edges[i]; e = &n->edges[i];
@ -155,7 +155,7 @@ edge * r3_node_find_edge(const node * n, const char * pat, int pat_len) {
return NULL; return NULL;
} }
int r3_tree_compile(node *n, char **errstr) int r3_tree_compile(R3Node *n, char **errstr)
{ {
int i; int i;
int ret = 0; int ret = 0;
@ -184,8 +184,8 @@ int r3_tree_compile(node *n, char **errstr)
* Return -1 if error occurs * Return -1 if error occurs
* Return 0 if success * Return 0 if success
*/ */
int r3_tree_compile_patterns(node * n, char **errstr) { int r3_tree_compile_patterns(R3Node * n, char **errstr) {
edge *e = NULL; R3Edge *e = NULL;
char * p; char * p;
char * cpat = zcalloc(sizeof(char) * 64 * 3); // XXX char * cpat = zcalloc(sizeof(char) * 64 * 3); // XXX
if (!cpat) { if (!cpat) {
@ -283,10 +283,10 @@ int r3_tree_compile_patterns(node * n, char **errstr) {
* @param int path_len the length of the URL path. * @param int path_len the length of the URL path.
* @param match_entry* entry match_entry is used for saving the captured dynamic strings from pcre result. * @param match_entry* entry match_entry is used for saving the captured dynamic strings from pcre result.
*/ */
node * r3_tree_matchl(const node * n, const char * path, int path_len, match_entry * entry) { R3Node * r3_tree_matchl(const R3Node * n, const char * path, int path_len, match_entry * entry) {
info("try matching: %s\n", path); info("try matching: %s\n", path);
edge *e; R3Edge *e;
unsigned int i; unsigned int i;
unsigned int restlen; unsigned int restlen;
@ -434,8 +434,8 @@ node * r3_tree_matchl(const node * n, const char * path, int path_len, match_ent
route * r3_tree_match_route(const node *tree, match_entry * entry) { route * r3_tree_match_route(const R3Node *tree, match_entry * entry) {
node *n; R3Node *n;
int i; int i;
n = r3_tree_match_entry(tree, entry); n = r3_tree_match_entry(tree, entry);
if (n && n->routes && n->route_len > 0) { if (n && n->routes && n->route_len > 0) {
@ -448,8 +448,8 @@ route * r3_tree_match_route(const node *tree, match_entry * entry) {
return NULL; return NULL;
} }
inline edge * r3_node_find_edge_str(const node * n, const char * str, int str_len) { inline R3Edge * r3_node_find_edge_str(const R3Node * n, const char * str, int str_len) {
edge * e; R3Edge * e;
unsigned int i; unsigned int i;
char firstbyte = *str; char firstbyte = *str;
for (i = n->edge_len; i--; ) { for (i = n->edge_len; i--; ) {
@ -464,8 +464,8 @@ inline edge * r3_node_find_edge_str(const node * n, const char * str, int str_le
return NULL; return NULL;
} }
node * r3_node_create() { R3Node * r3_node_create() {
node * n = (node*) zmalloc( sizeof(node) ); R3Node * n = (R3Node*) zmalloc( sizeof(R3Node) );
CHECK_PTR(n); CHECK_PTR(n);
n->edges = NULL; n->edges = NULL;
n->edge_len = 0; n->edge_len = 0;
@ -510,11 +510,11 @@ route * r3_route_createl(const char * path, int path_len) {
* *
* method (int): METHOD_GET, METHOD_POST, METHOD_PUT, METHOD_DELETE ... * method (int): METHOD_GET, METHOD_POST, METHOD_PUT, METHOD_DELETE ...
*/ */
route * r3_tree_insert_routel_ex(node *tree, int method, const char *path, int path_len, void *data, char **errstr) { route * r3_tree_insert_routel_ex(R3Node *tree, int method, const char *path, int path_len, void *data, char **errstr) {
route *r = r3_route_createl(path, path_len); route *r = r3_route_createl(path, path_len);
CHECK_PTR(r); CHECK_PTR(r);
r->request_method = method; // ALLOW GET OR POST METHOD r->request_method = method; // ALLOW GET OR POST METHOD
node * ret = r3_tree_insert_pathl_ex(tree, path, path_len, r, data, errstr); R3Node * ret = r3_tree_insert_pathl_ex(tree, path, path_len, r, data, errstr);
if (!ret) { if (!ret) {
// failed insert // failed insert
r3_route_free(r); r3_route_free(r);
@ -537,11 +537,11 @@ route * r3_tree_insert_routel_ex(node *tree, int method, const char *path, int p
* 4. "aaa{slug:xxx}/hate" vs "aab{slug:yyy}/bar" => common prefix = "aa" * 4. "aaa{slug:xxx}/hate" vs "aab{slug:yyy}/bar" => common prefix = "aa"
* 5. "/foo/{slug}/hate" vs "/fo{slug}/bar" => common prefix = "/fo" * 5. "/foo/{slug}/hate" vs "/fo{slug}/bar" => common prefix = "/fo"
*/ */
edge * r3_node_find_common_prefix(node *n, const char *path, int path_len, int *prefix_len, char **errstr) { R3Edge * r3_node_find_common_prefix(R3Node *n, const char *path, int path_len, int *prefix_len, char **errstr) {
int i = 0; int i = 0;
int prefix = 0; int prefix = 0;
*prefix_len = 0; *prefix_len = 0;
edge *e = NULL; R3Edge *e = NULL;
for(i = 0 ; i < n->edge_len ; i++ ) { for(i = 0 ; i < n->edge_len ; i++ ) {
// ignore all edges with slug // ignore all edges with slug
prefix = strndiff( (char*) path, n->edges[i].pattern, n->edges[i].pattern_len); prefix = strndiff( (char*) path, n->edges[i].pattern, n->edges[i].pattern_len);
@ -601,12 +601,12 @@ edge * r3_node_find_common_prefix(node *n, const char *path, int path_len, int *
/** /**
* Return the last inserted node. * Return the last inserted node.
*/ */
node * r3_tree_insert_pathl_ex(node *tree, const char *path, int path_len, route * route, void * data, char **errstr) R3Node * r3_tree_insert_pathl_ex(R3Node *tree, const char *path, int path_len, route * route, void * data, char **errstr)
{ {
node * n = tree; R3Node * n = tree;
// common edge // common edge
edge * e = NULL; R3Edge * e = NULL;
// If there is no path to insert at the node, we just increase the mount // If there is no path to insert at the node, we just increase the mount
// point on the node and append the route. // point on the node and append the route.
@ -657,7 +657,7 @@ node * r3_tree_insert_pathl_ex(node *tree, const char *path, int path_len, route
#endif #endif
// insert the first one edge, and break at "p" // insert the first one edge, and break at "p"
node * child = r3_tree_create(3); R3Node * child = r3_tree_create(3);
CHECK_PTR(child); CHECK_PTR(child);
r3_node_connect(n, zstrndup(path, (int)(p - path)), child); r3_node_connect(n, zstrndup(path, (int)(p - path)), child);
@ -685,7 +685,7 @@ node * r3_tree_insert_pathl_ex(node *tree, const char *path, int path_len, route
// if the slug starts after one+ charactor, for example foo{slug} // if the slug starts after one+ charactor, for example foo{slug}
node *c1; R3Node *c1;
if (slug_p > path) { if (slug_p > path) {
c1 = r3_tree_create(3); c1 = r3_tree_create(3);
CHECK_PTR(c1); CHECK_PTR(c1);
@ -694,10 +694,10 @@ node * r3_tree_insert_pathl_ex(node *tree, const char *path, int path_len, route
c1 = n; c1 = n;
} }
node * c2 = r3_tree_create(3); R3Node * c2 = r3_tree_create(3);
CHECK_PTR(c2); CHECK_PTR(c2);
edge * op_edge = r3_node_connectl(c1, slug_p, slug_len , 1, c2); R3Edge * op_edge = r3_node_connectl(c1, slug_p, slug_len , 1, c2);
if(opcode) { if(opcode) {
op_edge->opcode = opcode; op_edge->opcode = opcode;
} }
@ -717,7 +717,7 @@ node * r3_tree_insert_pathl_ex(node *tree, const char *path, int path_len, route
return c2; return c2;
} }
// only one slug // only one slug
node * child = r3_tree_create(3); R3Node * child = r3_tree_create(3);
CHECK_PTR(child); CHECK_PTR(child);
child->endpoint++; child->endpoint++;
if (data) if (data)
@ -771,9 +771,9 @@ node * r3_tree_insert_pathl_ex(node *tree, const char *path, int path_len, route
return n; return n;
} }
bool r3_node_has_slug_edges(const node *n) { bool r3_node_has_slug_edges(const R3Node *n) {
bool found = false; bool found = false;
edge *e; R3Edge *e;
for ( int i = 0 ; i < n->edge_len ; i++ ) { for ( int i = 0 ; i < n->edge_len ; i++ ) {
e = &n->edges[i]; e = &n->edges[i];
e->has_slug = r3_path_contains_slug_char(e->pattern); e->has_slug = r3_path_contains_slug_char(e->pattern);
@ -785,7 +785,7 @@ bool r3_node_has_slug_edges(const node *n) {
void r3_tree_dump(const node * n, int level) { void r3_tree_dump(const R3Node * n, int level) {
print_indent(level); print_indent(level);
printf("(o)"); printf("(o)");
@ -802,7 +802,7 @@ void r3_tree_dump(const node * n, int level) {
printf("\n"); printf("\n");
for ( int i = 0 ; i < n->edge_len ; i++ ) { for ( int i = 0 ; i < n->edge_len ; i++ ) {
edge * e = &n->edges[i]; R3Edge * e = &n->edges[i];
print_indent(level + 1); print_indent(level + 1);
printf("|-\"%s\"", e->pattern); printf("|-\"%s\"", e->pattern);
@ -855,7 +855,7 @@ inline int r3_route_cmp(const route *r1, const match_entry *r2) {
/** /**
* *
*/ */
void r3_node_append_route(node * n, route * r) { void r3_node_append_route(R3Node * n, route * r) {
if (n->routes == NULL) { if (n->routes == NULL) {
n->route_cap = 3; n->route_cap = 3;
n->routes = zmalloc(sizeof(route) * n->route_cap); n->routes = zmalloc(sizeof(route) * n->route_cap);

View file

@ -96,7 +96,7 @@ void bench_append_csv(char *filename, int countOfB, ...) {
int main() int main()
{ {
node * n = r3_tree_create(1); R3Node * n = r3_tree_create(1);
int route_data = 999; int route_data = 999;
@ -441,7 +441,7 @@ r3_tree_insert_path(n, "/garply/grault/corge", NULL);
r3_tree_compile(n, NULL); r3_tree_compile(n, NULL);
END_MEASURE(tree_compile) END_MEASURE(tree_compile)
node *m; R3Node * m;
m = r3_tree_match(n , "/qux/bar/corge", NULL); m = r3_tree_match(n , "/qux/bar/corge", NULL);
assert(m != NULL); assert(m != NULL);
assert( *((int*) m->data) == 999 ); assert( *((int*) m->data) == 999 );
@ -461,7 +461,7 @@ r3_tree_insert_path(n, "/garply/grault/corge", NULL);
BENCHMARK_SUMMARY(str_match_entry); BENCHMARK_SUMMARY(str_match_entry);
node * tree2 = r3_tree_create(1); R3Node * tree2 = r3_tree_create(1);
r3_tree_insert_path(tree2, "/post/{year}/{month}", NULL); r3_tree_insert_path(tree2, "/post/{year}/{month}", NULL);
r3_tree_compile(tree2, NULL); r3_tree_compile(tree2, NULL);

View file

@ -12,8 +12,8 @@
START_TEST (test_routes) START_TEST (test_routes)
{ {
node * n = r3_tree_create(10); R3Node * n = r3_tree_create(10);
node * m = NULL; R3Node * m = NULL;
char *data0 = "/foo/bar/baz"; char *data0 = "/foo/bar/baz";
r3_tree_insert_path(n, "/foo/bar/baz", (void*) data0); r3_tree_insert_path(n, "/foo/bar/baz", (void*) data0);

View file

@ -15,13 +15,13 @@
START_TEST (test_find_common_prefix) START_TEST (test_find_common_prefix)
{ {
node * n = r3_tree_create(10); R3Node * n = r3_tree_create(10);
edge * e = r3_edge_createl(zstrdup("/foo/{slug}"), sizeof("/foo/{slug}")-1, NULL); R3Edge * e = r3_edge_createl(zstrdup("/foo/{slug}"), sizeof("/foo/{slug}")-1, NULL);
r3_node_append_edge(n,e); r3_node_append_edge(n,e);
char *errstr = NULL; char *errstr = NULL;
int prefix_len = 0; int prefix_len = 0;
edge *ret_edge = NULL; R3Edge *ret_edge = NULL;
errstr = NULL; errstr = NULL;
@ -84,12 +84,12 @@ END_TEST
START_TEST (test_find_common_prefix_after) START_TEST (test_find_common_prefix_after)
{ {
node * n = r3_tree_create(10); R3Node * n = r3_tree_create(10);
edge * e = r3_edge_createl(zstrdup("{slug}/foo"), sizeof("{slug}/foo")-1, NULL); R3Edge * e = r3_edge_createl(zstrdup("{slug}/foo"), sizeof("{slug}/foo")-1, NULL);
r3_node_append_edge(n,e); r3_node_append_edge(n,e);
int prefix_len = 0; int prefix_len = 0;
edge *ret_edge = NULL; R3Edge *ret_edge = NULL;
char *errstr = NULL; char *errstr = NULL;
errstr = NULL; errstr = NULL;
@ -121,12 +121,12 @@ END_TEST
START_TEST (test_find_common_prefix_double_middle) START_TEST (test_find_common_prefix_double_middle)
{ {
node * n = r3_tree_create(10); R3Node * n = r3_tree_create(10);
edge * e = r3_edge_createl(zstrdup("{slug}/foo/{name}"), sizeof("{slug}/foo/{name}")-1, NULL); R3Edge * e = r3_edge_createl(zstrdup("{slug}/foo/{name}"), sizeof("{slug}/foo/{name}")-1, NULL);
r3_node_append_edge(n,e); r3_node_append_edge(n,e);
int prefix_len; int prefix_len;
edge *ret_edge = NULL; R3Edge *ret_edge = NULL;
char *errstr; char *errstr;
errstr = NULL; errstr = NULL;
@ -143,12 +143,12 @@ END_TEST
START_TEST (test_find_common_prefix_middle) START_TEST (test_find_common_prefix_middle)
{ {
node * n = r3_tree_create(10); R3Node * n = r3_tree_create(10);
edge * e = r3_edge_createl(zstrdup("/foo/{slug}/hate"), sizeof("/foo/{slug}/hate")-1, NULL); R3Edge * e = r3_edge_createl(zstrdup("/foo/{slug}/hate"), sizeof("/foo/{slug}/hate")-1, NULL);
r3_node_append_edge(n,e); r3_node_append_edge(n,e);
int prefix_len; int prefix_len;
edge *ret_edge = NULL; R3Edge *ret_edge = NULL;
char *errstr = NULL; char *errstr = NULL;
errstr = NULL; errstr = NULL;
@ -169,12 +169,12 @@ END_TEST
START_TEST (test_find_common_prefix_same_pattern) START_TEST (test_find_common_prefix_same_pattern)
{ {
node * n = r3_tree_create(10); R3Node * n = r3_tree_create(10);
edge * e = r3_edge_createl(zstrdup("/foo/{slug:xxx}/hate"), sizeof("/foo/{slug:xxx}/hate")-1, NULL); R3Edge * e = r3_edge_createl(zstrdup("/foo/{slug:xxx}/hate"), sizeof("/foo/{slug:xxx}/hate")-1, NULL);
r3_node_append_edge(n,e); r3_node_append_edge(n,e);
int prefix_len; int prefix_len;
edge *ret_edge = NULL; R3Edge *ret_edge = NULL;
prefix_len = 0; prefix_len = 0;
ret_edge = r3_node_find_common_prefix(n, "/foo/{slug:yyy}/hate", sizeof("/foo/{slug:yyy}/hate")-1, &prefix_len, NULL); ret_edge = r3_node_find_common_prefix(n, "/foo/{slug:yyy}/hate", sizeof("/foo/{slug:yyy}/hate")-1, &prefix_len, NULL);
@ -193,12 +193,12 @@ END_TEST
START_TEST (test_find_common_prefix_same_pattern2) START_TEST (test_find_common_prefix_same_pattern2)
{ {
node * n = r3_tree_create(10); R3Node * n = r3_tree_create(10);
edge * e = r3_edge_createl(zstrdup("{slug:xxx}/hate"), sizeof("{slug:xxx}/hate")-1, NULL); R3Edge * e = r3_edge_createl(zstrdup("{slug:xxx}/hate"), sizeof("{slug:xxx}/hate")-1, NULL);
r3_node_append_edge(n,e); r3_node_append_edge(n,e);
int prefix_len; int prefix_len;
edge *ret_edge = NULL; R3Edge *ret_edge = NULL;
prefix_len = 0; prefix_len = 0;
ret_edge = r3_node_find_common_prefix(n, "{slug:yyy}/hate", sizeof("{slug:yyy}/hate")-1, &prefix_len, NULL); ret_edge = r3_node_find_common_prefix(n, "{slug:yyy}/hate", sizeof("{slug:yyy}/hate")-1, &prefix_len, NULL);
@ -227,15 +227,15 @@ END_TEST
START_TEST (test_node_construct_and_free) START_TEST (test_node_construct_and_free)
{ {
node * n = r3_tree_create(10); R3Node * n = r3_tree_create(10);
node * another_tree = r3_tree_create(3); R3Node * another_tree = r3_tree_create(3);
r3_tree_free(n); r3_tree_free(n);
r3_tree_free(another_tree); r3_tree_free(another_tree);
} }
END_TEST END_TEST
static node * create_simple_str_tree() { static R3Node * create_simple_str_tree() {
node * n; R3Node * n;
n = r3_tree_create(10); n = r3_tree_create(10);
r3_tree_insert_path(n, "/zoo", NULL); r3_tree_insert_path(n, "/zoo", NULL);
r3_tree_insert_path(n, "/foo", NULL); r3_tree_insert_path(n, "/foo", NULL);
@ -248,8 +248,8 @@ static node * create_simple_str_tree() {
START_TEST (test_compile) START_TEST (test_compile)
{ {
node *n; R3Node *n;
node *m; R3Node *m;
n = create_simple_str_tree(); n = create_simple_str_tree();
@ -293,9 +293,9 @@ END_TEST
START_TEST (test_incomplete_slug_path) START_TEST (test_incomplete_slug_path)
{ {
node * n = r3_tree_create(10); R3Node * n = r3_tree_create(10);
node * ret_node; R3Node * ret_node;
// r3_tree_insert_path(n, "/foo-{user}-{id}", NULL, NULL); // r3_tree_insert_path(n, "/foo-{user}-{id}", NULL, NULL);
ret_node = r3_tree_insert_path(n, "/post/{handle", NULL); ret_node = r3_tree_insert_path(n, "/post/{handle", NULL);
@ -322,7 +322,7 @@ START_TEST (test_incomplete_slug_path)
ck_assert(ret_node); ck_assert(ret_node);
// OK to insert, but should return error when compiling patterns // OK to insert, but should return error when compiling patterns
node * ret_node2 = r3_tree_insert_path(n, "/users/{idx:\\d{3}}/{idy:aaa}", NULL); R3Node * ret_node2 = r3_tree_insert_path(n, "/users/{idx:\\d{3}}/{idy:aaa}", NULL);
ck_assert(ret_node2); ck_assert(ret_node2);
ck_assert(ret_node2 != ret_node); // make sure it's another node ck_assert(ret_node2 != ret_node); // make sure it's another node
@ -340,7 +340,7 @@ END_TEST
START_TEST (test_pcre_patterns_insert) START_TEST (test_pcre_patterns_insert)
{ {
node * n = r3_tree_create(10); R3Node * n = r3_tree_create(10);
// r3_tree_insert_path(n, "/foo-{user}-{id}", NULL, NULL); // r3_tree_insert_path(n, "/foo-{user}-{id}", NULL, NULL);
r3_tree_insert_path(n, "/post/{handle:\\d+}-{id:\\d+}", NULL); r3_tree_insert_path(n, "/post/{handle:\\d+}-{id:\\d+}", NULL);
@ -355,7 +355,7 @@ START_TEST (test_pcre_patterns_insert)
// r3_tree_dump(n, 0); // r3_tree_dump(n, 0);
node *matched; R3Node *matched;
matched = r3_tree_match(n, "/post/foo", NULL); matched = r3_tree_match(n, "/post/foo", NULL);
@ -392,7 +392,7 @@ END_TEST
*/ */
START_TEST (test_root_match) START_TEST (test_root_match)
{ {
node * n = r3_tree_create(10); R3Node * n = r3_tree_create(10);
int a = 10; int a = 10;
int b = 20; int b = 20;
@ -405,7 +405,7 @@ START_TEST (test_root_match)
r3_tree_compile(n, &errstr); r3_tree_compile(n, &errstr);
// r3_tree_dump(n, 0); // r3_tree_dump(n, 0);
node *matched; R3Node *matched;
matched = r3_tree_match(n, "/", NULL); matched = r3_tree_match(n, "/", NULL);
ck_assert(matched); ck_assert(matched);
ck_assert(matched->data == &a); ck_assert(matched->data == &a);
@ -431,7 +431,7 @@ END_TEST
*/ */
START_TEST (test_pcre_patterns_insert_2) START_TEST (test_pcre_patterns_insert_2)
{ {
node * n = r3_tree_create(10); R3Node * n = r3_tree_create(10);
r3_tree_insert_path(n, "/post/{idx:\\d{2}}/{idy:\\d{2}}", NULL); r3_tree_insert_path(n, "/post/{idx:\\d{2}}/{idy:\\d{2}}", NULL);
r3_tree_insert_path(n, "/zoo", NULL); r3_tree_insert_path(n, "/zoo", NULL);
r3_tree_insert_path(n, "/foo", NULL); r3_tree_insert_path(n, "/foo", NULL);
@ -441,7 +441,7 @@ START_TEST (test_pcre_patterns_insert_2)
r3_tree_compile(n, &errstr); r3_tree_compile(n, &errstr);
// r3_tree_dump(n, 0); // r3_tree_dump(n, 0);
node *matched; R3Node *matched;
matched = r3_tree_match(n, "/post/11/22", NULL); matched = r3_tree_match(n, "/post/11/22", NULL);
ck_assert((int)matched); ck_assert((int)matched);
ck_assert(matched->endpoint > 0); ck_assert(matched->endpoint > 0);
@ -453,7 +453,7 @@ END_TEST
*/ */
START_TEST (test_pcre_patterns_insert_3) START_TEST (test_pcre_patterns_insert_3)
{ {
node * n = r3_tree_create(10); R3Node * n = r3_tree_create(10);
printf("Inserting /post/{idx:\\d{2}}/{idy}\n"); printf("Inserting /post/{idx:\\d{2}}/{idy}\n");
r3_tree_insert_path(n, "/post/{idx:\\d{2}}/{idy}", NULL); r3_tree_insert_path(n, "/post/{idx:\\d{2}}/{idy}", NULL);
// r3_tree_dump(n, 0); // r3_tree_dump(n, 0);
@ -469,7 +469,7 @@ START_TEST (test_pcre_patterns_insert_3)
r3_tree_compile(n, &errstr); r3_tree_compile(n, &errstr);
// r3_tree_dump(n, 0); // r3_tree_dump(n, 0);
node *matched; R3Node *matched;
matched = r3_tree_match(n, "/post/11/22", NULL); matched = r3_tree_match(n, "/post/11/22", NULL);
@ -492,9 +492,9 @@ END_TEST
START_TEST (test_insert_pathl_fail) START_TEST (test_insert_pathl_fail)
{ {
node * n = r3_tree_create(10); R3Node * n = r3_tree_create(10);
node * ret; R3Node * ret;
char *errstr = NULL; char *errstr = NULL;
ret = r3_tree_insert_pathl_ex(n, "/foo/{name:\\d{5}", strlen("/foo/{name:\\d{5}"), NULL, NULL, &errstr); ret = r3_tree_insert_pathl_ex(n, "/foo/{name:\\d{5}", strlen("/foo/{name:\\d{5}"), NULL, NULL, &errstr);
@ -516,9 +516,9 @@ END_TEST
START_TEST (test_insert_pathl) START_TEST (test_insert_pathl)
{ {
node * n = r3_tree_create(10); R3Node * n = r3_tree_create(10);
node * ret; R3Node * ret;
ret = r3_tree_insert_path(n, "/foo/bar", NULL); ret = r3_tree_insert_path(n, "/foo/bar", NULL);
ck_assert(ret); ck_assert(ret);
@ -563,9 +563,9 @@ END_TEST
START_TEST (test_compile_fail) START_TEST (test_compile_fail)
{ {
node * n = r3_tree_create(10); R3Node * n = r3_tree_create(10);
node * ret; R3Node * ret;
ret = r3_tree_insert_path(n, "/foo/{idx}/{idy:)}", NULL); ret = r3_tree_insert_path(n, "/foo/{idx}/{idy:)}", NULL);
ck_assert(ret); ck_assert(ret);
@ -635,12 +635,12 @@ START_TEST(test_pcre_pattern_simple)
{ {
match_entry * entry; match_entry * entry;
entry = match_entry_createl( "/user/123" , strlen("/user/123") ); entry = match_entry_createl( "/user/123" , strlen("/user/123") );
node * n = r3_tree_create(10); R3Node * n = r3_tree_create(10);
r3_tree_insert_path(n, "/user/{id:\\d+}", NULL); r3_tree_insert_path(n, "/user/{id:\\d+}", NULL);
r3_tree_insert_path(n, "/user", NULL); r3_tree_insert_path(n, "/user", NULL);
r3_tree_compile(n, NULL); r3_tree_compile(n, NULL);
// r3_tree_dump(n, 0); // r3_tree_dump(n, 0);
node *matched; R3Node *matched;
matched = r3_tree_matchl(n, "/user/123", strlen("/user/123"), entry); matched = r3_tree_matchl(n, "/user/123", strlen("/user/123"), entry);
ck_assert(matched); ck_assert(matched);
ck_assert(entry->vars->len > 0); ck_assert(entry->vars->len > 0);
@ -654,7 +654,7 @@ START_TEST(test_pcre_pattern_more)
{ {
match_entry * entry; match_entry * entry;
entry = match_entry_createl( "/user/123" , strlen("/user/123") ); entry = match_entry_createl( "/user/123" , strlen("/user/123") );
node * n = r3_tree_create(10); R3Node * n = r3_tree_create(10);
int var0 = 5; int var0 = 5;
int var1 = 100; int var1 = 100;
@ -672,7 +672,7 @@ START_TEST(test_pcre_pattern_more)
r3_tree_insert_path(n, "/user", &var0); r3_tree_insert_path(n, "/user", &var0);
r3_tree_compile(n, NULL); r3_tree_compile(n, NULL);
// r3_tree_dump(n, 0); // r3_tree_dump(n, 0);
node *matched; R3Node *matched;
matched = r3_tree_matchl(n, "/user/123", strlen("/user/123"), entry); matched = r3_tree_matchl(n, "/user/123", strlen("/user/123"), entry);
ck_assert(matched); ck_assert(matched);
@ -706,7 +706,7 @@ START_TEST(test_insert_pathl_before_root)
int var1 = 22; int var1 = 22;
int var2 = 33; int var2 = 33;
int var3 = 44; int var3 = 44;
node * n = r3_tree_create(3); R3Node * n = r3_tree_create(3);
r3_tree_insert_pathl_ex(n, STR("/blog/post"), NULL, &var1, NULL); r3_tree_insert_pathl_ex(n, STR("/blog/post"), NULL, &var1, NULL);
r3_tree_insert_pathl_ex(n, STR("/blog"), NULL, &var2, NULL); r3_tree_insert_pathl_ex(n, STR("/blog"), NULL, &var2, NULL);
r3_tree_insert_pathl_ex(n, STR("/"), NULL, &var3, NULL); r3_tree_insert_pathl_ex(n, STR("/"), NULL, &var3, NULL);
@ -727,7 +727,7 @@ START_TEST(test_insert_route)
int var2 = 33; int var2 = 33;
node * n = r3_tree_create(2); R3Node * n = r3_tree_create(2);
r3_tree_insert_route(n, METHOD_GET, "/blog/post", &var1); r3_tree_insert_route(n, METHOD_GET, "/blog/post", &var1);
r3_tree_insert_route(n, METHOD_POST, "/blog/post", &var2); r3_tree_insert_route(n, METHOD_POST, "/blog/post", &var2);