Add slug_to_pcre pattern function

This commit is contained in:
c9s 2014-05-15 20:38:07 +08:00
parent 4fb498c9b3
commit 89eaad6006
4 changed files with 52 additions and 0 deletions

View file

@ -7,6 +7,8 @@
#ifndef STR_H #ifndef STR_H
#define STR_H #define STR_H
char * slug_to_pcre(char * slug, char sep);
char * ltrim_slash(char* str); char * ltrim_slash(char* str);
char** str_split(char* a_str, const char a_delim); char** str_split(char* a_str, const char a_delim);
@ -15,5 +17,6 @@ void str_repeat(char *s, char *c, int len);
void print_indent(int level); void print_indent(int level);
#endif /* !STR_H */ #endif /* !STR_H */

View file

@ -103,6 +103,13 @@ redge * rnode_find_edge(rnode * n, char * pat) {
return NULL; return NULL;
} }
void rnode_combine_patterns(rnode * n) {
redge *e = NULL;
for ( int i = 0 ; i < n->children_len ; i++ ) {
e = n->children[i];
}
}
rnode * rnode_lookup(rnode * tree, char * path, int path_len) { rnode * rnode_lookup(rnode * tree, char * path, int path_len) {
token_array * tokens = split_route_pattern(path, path_len); token_array * tokens = split_route_pattern(path, path_len);

View file

@ -11,6 +11,30 @@
#include "str.h" #include "str.h"
#include "token.h" #include "token.h"
/**
* @param char * sep separator
*/
char * slug_to_pcre(char * slug, char sep)
{
char * p = NULL;
char * pat = NULL;
char * end = NULL;
if ( NULL != (p = strchr(slug, ':')) ) {
// this slug contains a pattern
end = strchr(p, '}');
// start after ':'
return strndup( (p + 1) , (end - p - 1) );
} else {
if ((pat = malloc(128)) == NULL) {
return (NULL);
}
// should return a '[^/]+' pattern
snprintf(pat, 128, "[^%c]+", sep);
}
return pat;
}
char * ltrim_slash(char* str) char * ltrim_slash(char* str)

View file

@ -40,6 +40,23 @@ START_TEST (test_rnode_find_edge)
END_TEST END_TEST
START_TEST (test_slug_to_pcre)
{
char * pattern;
pattern = slug_to_pcre("{id}", '/');
fail_if( strcmp(pattern, "[^/]+") != 0 , "as [^/]+" );
free(pattern);
pattern = slug_to_pcre("{id:[a-z]+}", '/');
fail_if( strcmp(pattern, "[a-z]+") != 0 , "as [a-z]+" );
free(pattern);
}
END_TEST
START_TEST (test_rnode_insert_tokens) START_TEST (test_rnode_insert_tokens)
{ {
token_array *t; token_array *t;
@ -152,6 +169,7 @@ Suite* r3_suite (void) {
tcase_add_test(tcase, test_rnode_construct_uniq); tcase_add_test(tcase, test_rnode_construct_uniq);
tcase_add_test(tcase, test_rnode_find_edge); tcase_add_test(tcase, test_rnode_find_edge);
tcase_add_test(tcase, test_rnode_insert_tokens); tcase_add_test(tcase, test_rnode_insert_tokens);
tcase_add_test(tcase, test_slug_to_pcre);
suite_add_tcase(suite, tcase); suite_add_tcase(suite, tcase);