Rename dl => offset for readiblity

This commit is contained in:
c9s 2014-05-18 09:05:55 +08:00
parent 097a180f8c
commit 02197595c9
3 changed files with 21 additions and 23 deletions

View file

@ -1,5 +1,5 @@
lib_LTLIBRARIES=libr3.la lib_LTLIBRARIES=libr3.la
libr3_la_SOURCES=node.c edge.c list.c str.c token.c gvc.c libr3_la_SOURCES=node.c edge.c list.c str.c token.c gvc.c
libr3_la_LIBADD=$(DEPS_LIBS) libr3_la_LIBADD=$(DEPS_LIBS)
AM_CFLAGS=$(DEPS_CFLAGS) -I$(top_builddir) -I$(top_builddir)/include AM_CFLAGS=$(DEPS_CFLAGS) -I$(top_builddir) -I$(top_builddir)/include -Wall
# AM_CFLAGS=$(DEPS_CFLAGS) # AM_CFLAGS=$(DEPS_CFLAGS)

View file

@ -314,7 +314,7 @@ top_srcdir = @top_srcdir@
lib_LTLIBRARIES = libr3.la lib_LTLIBRARIES = libr3.la
libr3_la_SOURCES = node.c edge.c list.c str.c token.c gvc.c libr3_la_SOURCES = node.c edge.c list.c str.c token.c gvc.c
libr3_la_LIBADD = $(DEPS_LIBS) libr3_la_LIBADD = $(DEPS_LIBS)
AM_CFLAGS = $(DEPS_CFLAGS) -I$(top_builddir) -I$(top_builddir)/include AM_CFLAGS = $(DEPS_CFLAGS) -I$(top_builddir) -I$(top_builddir)/include -Wall
all: all-am all: all-am
.SUFFIXES: .SUFFIXES:

View file

@ -169,7 +169,7 @@ void r3_tree_compile_patterns(node * n) {
const char *error; const char *error;
int erroffset; int erroffset;
unsigned int option_bits; unsigned int option_bits = 0;
if (n->pcre_pattern) if (n->pcre_pattern)
free(n->pcre_pattern); free(n->pcre_pattern);
@ -179,7 +179,7 @@ void r3_tree_compile_patterns(node * n) {
// n->pcre_pattern; // n->pcre_pattern;
n->pcre_pattern = pcre_compile( n->pcre_pattern = pcre_compile(
n->combined_pattern, /* the pattern */ n->combined_pattern, /* the pattern */
0, /* default options */ option_bits, /* default options */
&error, /* for error message */ &error, /* for error message */
&erroffset, /* for error offset */ &erroffset, /* for error offset */
NULL); /* use default character tables */ NULL); /* use default character tables */
@ -357,17 +357,15 @@ node * r3_tree_insert_pathn(node *tree, char *route, int route_len, void * route
node * n = tree; node * n = tree;
edge * e = NULL; edge * e = NULL;
char * p = route;
/* length of common prefix */ /* length of common prefix */
int dl = 0; int offset = 0;
for( int i = 0 ; i < n->edge_len ; i++ ) { for( int i = 0 ; i < n->edge_len ; i++ ) {
dl = strndiff(route, n->edges[i]->pattern, n->edges[i]->pattern_len); offset = strndiff(route, n->edges[i]->pattern, n->edges[i]->pattern_len);
// printf("dl: %d %s vs %s\n", dl, route, n->edges[i]->pattern ); // printf("offset: %d %s vs %s\n", offset, route, n->edges[i]->pattern );
// no common, consider insert a new edge // no common, consider insert a new edge
if ( dl > 0 ) { if ( offset > 0 ) {
e = n->edges[i]; e = n->edges[i];
break; break;
} }
@ -377,13 +375,13 @@ node * r3_tree_insert_pathn(node *tree, char *route, int route_len, void * route
char *slug_s = strchr(route, '{'); char *slug_s = strchr(route, '{');
char *slug_e = strchr(route, '}'); char *slug_e = strchr(route, '}');
if ( slug_s && slug_e ) { if ( slug_s && slug_e ) {
if ( dl > (slug_s - route) && dl < (slug_e - route) ) { if ( offset > (slug_s - route) && offset < (slug_e - route) ) {
// break before '{' // break before '{'
dl = slug_s - route; offset = slug_s - route;
} }
} }
if ( dl == 0 ) { if ( offset == 0 ) {
// not found, we should just insert a whole new edge // not found, we should just insert a whole new edge
node * child = r3_tree_create(3); node * child = r3_tree_create(3);
r3_tree_add_child(n, strndup(route, route_len) , child); r3_tree_add_child(n, strndup(route, route_len) , child);
@ -391,10 +389,10 @@ node * r3_tree_insert_pathn(node *tree, char *route, int route_len, void * route
child->route_ptr = route_ptr; child->route_ptr = route_ptr;
child->endpoint++; child->endpoint++;
return child; return child;
} else if ( dl == e->pattern_len ) { // fully-equal to the pattern of the edge } else if ( offset == e->pattern_len ) { // fully-equal to the pattern of the edge
char * subroute = route + dl; char * subroute = route + offset;
int subroute_len = route_len - dl; int subroute_len = route_len - offset;
// there are something more we can insert // there are something more we can insert
if ( subroute_len > 0 ) { if ( subroute_len > 0 ) {
@ -406,8 +404,8 @@ node * r3_tree_insert_pathn(node *tree, char *route, int route_len, void * route
return e->child; return e->child;
} }
} else if ( dl < e->pattern_len ) { } else if ( offset < e->pattern_len ) {
// printf("branch the edge dl: %d\n", dl); // printf("branch the edge offset: %d\n", offset);
/* it's partially matched with the pattern, /* it's partially matched with the pattern,
@ -415,14 +413,14 @@ node * r3_tree_insert_pathn(node *tree, char *route, int route_len, void * route
*/ */
node *c2; // child 1, child 2 node *c2; // child 1, child 2
edge *e2; // edge 1, edge 2 edge *e2; // edge 1, edge 2
char * s2 = route + dl; char * s2 = route + offset;
int s2_len = 0; int s2_len = 0;
r3_edge_branch(e, dl); r3_edge_branch(e, offset);
// here is the new edge from. // here is the new edge from.
c2 = r3_tree_create(3); c2 = r3_tree_create(3);
s2_len = route_len - dl; s2_len = route_len - offset;
e2 = r3_edge_create(strndup(s2, s2_len), s2_len, c2); e2 = r3_edge_create(strndup(s2, s2_len), s2_len, c2);
// printf("edge right: %s\n", e2->pattern); // printf("edge right: %s\n", e2->pattern);
r3_tree_append_edge(e->child, e2); r3_tree_append_edge(e->child, e2);
@ -430,8 +428,8 @@ node * r3_tree_insert_pathn(node *tree, char *route, int route_len, void * route
char *op = e->pattern; char *op = e->pattern;
// truncate the original edge pattern // truncate the original edge pattern
e->pattern = strndup(e->pattern, dl); e->pattern = strndup(e->pattern, offset);
e->pattern_len = dl; e->pattern_len = offset;
free(op); free(op);
// move n->edges to c1 // move n->edges to c1