when child is not endpoint, return NULL

This commit is contained in:
c9s 2014-05-21 00:37:27 +08:00
parent 90890b09e5
commit cae305b92d
4 changed files with 27 additions and 45 deletions

View file

@ -14,36 +14,10 @@
#include <assert.h>
#include <pcre.h>
#ifndef bool
typedef unsigned char bool;
#endif
#ifndef FALSE
# define FALSE 0
#endif
#ifndef TRUE
# define TRUE 1
#endif
// #define DEBUG 1
#ifdef DEBUG
#define info(fmt, ...) \
do { fprintf(stderr, fmt, __VA_ARGS__); } while (0)
#define debug(fmt, ...) \
do { fprintf(stderr, "%s:%d:%s(): " fmt, __FILE__, \
__LINE__, __func__, __VA_ARGS__); } while (0)
#else
#define info(...);
#define debug(...);
#endif
#include "r3_define.h"
#include "str_array.h"
#define node_edge_pattern(node,i) node->edges[i]->pattern
#define node_edge_pattern_len(node,i) node->edges[i]->pattern_len

View file

@ -307,8 +307,8 @@ node * r3_tree_matchl(node * n, char * path, int path_len, match_entry * entry)
// append captured token to entry
str_array_append(entry->vars , strndup(substring_start, substring_length));
}
if (restlen == 0) {
return e->child;
if (restlen == 0 ) {
return e->child && e->child->endpoint ? e->child : NULL;
}
// get the length of orginal string: $0
return r3_tree_matchl( e->child, path + (n->ov[1] - n->ov[0]), restlen, entry);
@ -320,10 +320,10 @@ node * r3_tree_matchl(node * n, char * path, int path_len, match_entry * entry)
if ( (e = r3_node_find_edge_str(n, path, path_len)) != NULL ) {
int restlen = path_len - e->pattern_len;
if(restlen > 0) {
return r3_tree_matchl(e->child, path + e->pattern_len, restlen, entry);
if (restlen == 0) {
return e->child && e->child->endpoint ? e->child : NULL;
}
return e->child;
return r3_tree_matchl(e->child, path + e->pattern_len, restlen, entry);
}
return NULL;
}

View file

@ -394,3 +394,6 @@
1400600677,13384789.38
1400600754,13649118.34
1400601224,13742996.84
1400603778,13736212.66
1400603800,13715365.00
1400603827,12742770.05

1 1400242718 5649455.80
394 1400600677 13384789.38
395 1400600754 13649118.34
396 1400601224 13742996.84
397 1400603778 13736212.66
398 1400603800 13715365.00
399 1400603827 12742770.05

View file

@ -114,8 +114,7 @@ START_TEST (test_pcre_patterns_insert)
// incomplete string shouldn't match
matched = r3_tree_matchl(n, "/post/111-", strlen("/post/111-"), NULL);
ck_assert(matched);
ck_assert_int_eq(matched->endpoint, 0);
ck_assert(! matched);
r3_tree_free(n);
}
@ -128,27 +127,33 @@ END_TEST
START_TEST (test_pcre_patterns_insert_2)
{
node * n = r3_tree_create(10);
// r3_tree_insert_path(n, "/foo-{user}-{id}", NULL, NULL);
// r3_tree_dump(n, 0);
r3_tree_insert_path(n, "/post/{idx:\\d{2}}/{idy:\\d{2}}", NULL);
r3_tree_compile(n);
r3_tree_dump(n, 0);
node *matched;
matched = r3_tree_match(n, "/post/11/22", NULL);
ck_assert(matched);
ck_assert(matched->endpoint > 0);
/*
matched = r3_tree_match(n, "/post/11", NULL);
ck_assert(matched);
ck_assert_int_eq(matched->endpoint, 0);
r3_tree_free(n);
*/
}
END_TEST
/**
* Test for \d{3}-\d{4}
*/
START_TEST (test_pcre_patterns_insert_3)
{
node * n = r3_tree_create(10);
r3_tree_insert_path(n, "/post/{idx:\\d{2}}/{idy:\\d{2}}", NULL);
r3_tree_compile(n);
r3_tree_dump(n, 0);
node *matched;
matched = r3_tree_match(n, "/post/11/22", NULL);
ck_assert(matched);
ck_assert(matched->endpoint > 0);
}
END_TEST