Add node type for comparison

This commit is contained in:
c9s 2014-05-23 12:08:06 +08:00
parent 403b1d9ba2
commit fe70b55253
5 changed files with 35 additions and 18 deletions

View file

@ -459,3 +459,9 @@
1400816594,9389101.94,1989498.64,34663.67 1400816594,9389101.94,1989498.64,34663.67
1400816645,9201251.49,2105517.01,43240.25 1400816645,9201251.49,2105517.01,43240.25
1400816815,9228390.29,2097606.06,23301.69 1400816815,9228390.29,2097606.06,23301.69
1400817268,9242018.45,2109706.16,38479.85
1400817280,9320308.27,1891100.64,47662.55
1400817392,6448229.88,1994875.02,34663.67
1400817671,8654311.52,2094510.75,47662.55
1400817706,9362050.27,1981074.38,62601.55
1400818067,9247601.69,1944615.34,35848.75

Can't render this file because it has a wrong number of fields in line 447.

View file

@ -40,9 +40,8 @@ struct _node {
/** compile-time variables here.... **/ /** compile-time variables here.... **/
/* the combined regexp pattern string from pattern_tokens */ /* the combined regexp pattern string from pattern_tokens */
int compare_type;
char * combined_pattern; char * combined_pattern;
int ov_cnt;
int * ov;
pcre * pcre_pattern; pcre * pcre_pattern;
pcre_extra * pcre_extra; pcre_extra * pcre_extra;
@ -174,7 +173,11 @@ route * r3_tree_match_route(const node *n, match_entry * entry);
#define METHOD_HEAD 2<<5 #define METHOD_HEAD 2<<5
#define METHOD_OPTIONS 2<<6 #define METHOD_OPTIONS 2<<6
int r3_pattern_to_opcode(char * pattern, int pattern_len);
int r3_pattern_to_opcode(char * pattern);
enum { NODE_COMPARE_STR, NODE_COMPARE_PCRE, NODE_COMPARE_OPCODE };
enum { OP_EXPECT_DIGITS = 1, OP_EXPECT_WORDS, OP_EXPECT_NOSLASH, OP_EXPECT_NODASH }; enum { OP_EXPECT_DIGITS = 1, OP_EXPECT_WORDS, OP_EXPECT_NOSLASH, OP_EXPECT_NODASH };

View file

@ -58,8 +58,6 @@ node * r3_tree_create(int cap) {
n->combined_pattern = NULL; n->combined_pattern = NULL;
n->pcre_pattern = NULL; n->pcre_pattern = NULL;
n->pcre_extra = NULL; n->pcre_extra = NULL;
n->ov_cnt = 0;
n->ov = NULL;
return n; return n;
} }
@ -81,7 +79,6 @@ void r3_tree_free(node * tree) {
} }
#endif #endif
zfree(tree->combined_pattern); zfree(tree->combined_pattern);
zfree(tree->ov);
zfree(tree); zfree(tree);
tree = NULL; tree = NULL;
} }
@ -168,10 +165,19 @@ void r3_tree_compile_patterns(node * n) {
p++; p++;
edge *e = NULL; edge *e = NULL;
int opcode_cnt = 0;
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];
if ( e->has_slug ) { if ( e->has_slug ) {
// compile "foo/{slug}" to "foo/[^/]+"
char * slug_pat = slug_compile(e->pattern, e->pattern_len); char * slug_pat = slug_compile(e->pattern, e->pattern_len);
// if found available opcode
e->opcode = r3_pattern_to_opcode(slug_pat);
if (e->opcode) {
opcode_cnt++;
}
strcat(p, slug_pat); strcat(p, slug_pat);
} else { } else {
strncat(p++,"(", 1); strncat(p++,"(", 1);
@ -189,9 +195,11 @@ void r3_tree_compile_patterns(node * n) {
info("pattern: %s\n",cpat); info("pattern: %s\n",cpat);
n->ov_cnt = (1 + n->edge_len) * 3; // if all edges use opcode, we should skip the combined_pattern.
n->ov = (int*) zcalloc(sizeof(int) * n->ov_cnt); if ( opcode_cnt == n->edge_len ) {
zfree(cpat);
return;
}
n->combined_pattern = cpat; n->combined_pattern = cpat;

View file

@ -13,17 +13,17 @@
#include "str_array.h" #include "str_array.h"
#include "zmalloc.h" #include "zmalloc.h"
int r3_pattern_to_opcode(char * pattern, int pattern_len) { int r3_pattern_to_opcode(char * pattern) {
if ( strncmp(pattern, "\\w+", pattern_len) == 0 ) { if ( strcmp(pattern, "\\w+") == 0 ) {
return OP_EXPECT_WORDS; return OP_EXPECT_WORDS;
} }
if ( strncmp(pattern, "\\d+", pattern_len) == 0 ) { if ( strcmp(pattern, "\\d+") == 0 ) {
return OP_EXPECT_DIGITS; return OP_EXPECT_DIGITS;
} }
if ( strncmp(pattern, "[^/]+", pattern_len) == 0 ) { if ( strcmp(pattern, "[^/]+") == 0 ) {
return OP_EXPECT_NOSLASH; return OP_EXPECT_NOSLASH;
} }
if ( strncmp(pattern, "[^-]+", pattern_len) == 0 ) { if ( strcmp(pattern, "[^-]+") == 0 ) {
return OP_EXPECT_NODASH; return OP_EXPECT_NODASH;
} }
return 0; return 0;

View file

@ -15,10 +15,10 @@
START_TEST (test_pattern_to_opcode) START_TEST (test_pattern_to_opcode)
{ {
ck_assert( r3_pattern_to_opcode("\\w+", sizeof("\\w+") - 1) == OP_EXPECT_WORDS ); ck_assert( r3_pattern_to_opcode("\\w+") == OP_EXPECT_WORDS );
ck_assert( r3_pattern_to_opcode("\\d+", sizeof("\\d+") - 1) == OP_EXPECT_DIGITS ); ck_assert( r3_pattern_to_opcode("\\d+") == OP_EXPECT_DIGITS );
ck_assert( r3_pattern_to_opcode("[^/]+", sizeof("[^/]+") - 1) == OP_EXPECT_NOSLASH ); ck_assert( r3_pattern_to_opcode("[^/]+") == OP_EXPECT_NOSLASH );
ck_assert( r3_pattern_to_opcode("[^-]+", sizeof("[^-]+") - 1) == OP_EXPECT_NODASH ); ck_assert( r3_pattern_to_opcode("[^-]+") == OP_EXPECT_NODASH );
} }
END_TEST END_TEST