r3 function prefix

- Remove ltrim_slash.
- Add "r3_" prefix to r3_str.c functions.
This commit is contained in:
c9s 2014-06-03 20:50:19 +08:00
parent 053f9202f6
commit 5609fd5f9e
5 changed files with 37 additions and 46 deletions

View file

@ -10,15 +10,13 @@
#include "r3.h" #include "r3.h"
#include "config.h" #include "config.h"
char * slug_compile(const char * str, int len); char * r3_slug_compile(const char * str, int len);
char * slug_find_pattern(const char *s1, int *len); char * r3_slug_find_pattern(const char *s1, int *len);
char * slug_find_placeholder(const char *s1, int *len); char * r3_slug_find_placeholder(const char *s1, int *len);
char * inside_slug(const char * needle, int needle_len, char *offset, char **errstr); char * r3_inside_slug(const char * needle, int needle_len, char *offset, char **errstr);
char * ltrim_slash(char* str);
void str_repeat(char *s, const char *c, int len); void str_repeat(char *s, const char *c, int len);

View file

@ -191,7 +191,7 @@ int r3_tree_compile_patterns(node * n, char **errstr) {
if ( e->has_slug ) { if ( e->has_slug ) {
// compile "foo/{slug}" to "foo/[^/]+" // compile "foo/{slug}" to "foo/[^/]+"
char * slug_pat = slug_compile(e->pattern, e->pattern_len); char * slug_pat = r3_slug_compile(e->pattern, e->pattern_len);
strcat(p, slug_pat); strcat(p, slug_pat);
} else { } else {
strncat(p,"^(", 2); strncat(p,"^(", 2);
@ -590,7 +590,7 @@ node * r3_tree_insert_pathl_ex(node *tree, const char *path, int path_len, route
if ( slug_cnt > 1 ) { if ( slug_cnt > 1 ) {
int slug_len; int slug_len;
char *p = slug_find_placeholder(path, &slug_len); char *p = r3_slug_find_placeholder(path, &slug_len);
#ifdef DEBUG #ifdef DEBUG
assert(p); assert(p);
@ -598,7 +598,7 @@ node * r3_tree_insert_pathl_ex(node *tree, const char *path, int path_len, route
// find the next one '{', then break there // find the next one '{', then break there
if(p) { if(p) {
p = slug_find_placeholder(p + slug_len + 1, NULL); p = r3_slug_find_placeholder(p + slug_len + 1, NULL);
} }
#ifdef DEBUG #ifdef DEBUG
assert(p); assert(p);
@ -617,14 +617,14 @@ node * r3_tree_insert_pathl_ex(node *tree, const char *path, int path_len, route
if (slug_cnt == 1) { if (slug_cnt == 1) {
// there is one slug, let's see if it's optimiz-able by opcode // there is one slug, let's see if it's optimiz-able by opcode
int slug_len = 0; int slug_len = 0;
char *slug_p = slug_find_placeholder(path, &slug_len); char *slug_p = r3_slug_find_placeholder(path, &slug_len);
int slug_pattern_len = 0; int slug_pattern_len = 0;
char *slug_pattern = slug_find_pattern(slug_p, &slug_pattern_len); char *slug_pattern = r3_slug_find_pattern(slug_p, &slug_pattern_len);
int opcode = 0; int opcode = 0;
// if there is a pattern defined. // if there is a pattern defined.
if (slug_pattern_len) { if (slug_pattern_len) {
char *cpattern = slug_compile(slug_pattern, slug_pattern_len); char *cpattern = r3_slug_compile(slug_pattern, slug_pattern_len);
opcode = r3_pattern_to_opcode(cpattern, strlen(cpattern)); opcode = r3_pattern_to_opcode(cpattern, strlen(cpattern));
zfree(cpattern); zfree(cpattern);
} else { } else {

View file

@ -42,7 +42,7 @@ int r3_pattern_to_opcode(const char * pattern, int len) {
char * inside_slug(const char * needle, int needle_len, char *offset, char **errstr) { char * r3_inside_slug(const char * needle, int needle_len, char *offset, char **errstr) {
char * s1 = offset; char * s1 = offset;
char * s2 = offset; char * s2 = offset;
@ -78,7 +78,7 @@ char * inside_slug(const char * needle, int needle_len, char *offset, char **err
return NULL; return NULL;
} }
char * slug_find_placeholder(const char *s1, int *len) { char * r3_slug_find_placeholder(const char *s1, int *len) {
char *c; char *c;
char *s2; char *s2;
int cnt = 0; int cnt = 0;
@ -110,7 +110,7 @@ char * slug_find_placeholder(const char *s1, int *len) {
/** /**
* given a slug string, duplicate the pattern string of the slug * given a slug string, duplicate the pattern string of the slug
*/ */
char * slug_find_pattern(const char *s1, int *len) { char * r3_slug_find_pattern(const char *s1, int *len) {
char *c; char *c;
char *s2; char *s2;
int cnt = 1; int cnt = 1;
@ -139,7 +139,7 @@ char * slug_find_pattern(const char *s1, int *len) {
/** /**
* @param char * sep separator * @param char * sep separator
*/ */
char * slug_compile(const char * str, int len) char * r3_slug_compile(const char * str, int len)
{ {
char *s1 = NULL, *o = NULL; char *s1 = NULL, *o = NULL;
char *pat = NULL; char *pat = NULL;
@ -148,7 +148,7 @@ char * slug_compile(const char * str, int len)
// append prefix // append prefix
int s1_len; int s1_len;
s1 = slug_find_placeholder(str, &s1_len); s1 = r3_slug_find_placeholder(str, &s1_len);
if ( s1 == NULL ) { if ( s1 == NULL ) {
return zstrdup(str); return zstrdup(str);
@ -168,7 +168,7 @@ char * slug_compile(const char * str, int len)
int pat_len; int pat_len;
pat = slug_find_pattern(s1, &pat_len); pat = r3_slug_find_pattern(s1, &pat_len);
if (pat) { if (pat) {
*o = '('; *o = '(';

View file

@ -22,23 +22,23 @@ START_TEST (test_pattern_to_opcode)
} }
END_TEST END_TEST
START_TEST (test_slug_compile) START_TEST (test_r3_slug_compile)
{ {
char * path = "/user/{id}"; char * path = "/user/{id}";
char * c = NULL; char * c = NULL;
ck_assert_str_eq( c = slug_compile(path, strlen(path) ) , "^/user/([^/]+)" ); ck_assert_str_eq( c = r3_slug_compile(path, strlen(path) ) , "^/user/([^/]+)" );
zfree(c); zfree(c);
char * path2 = "/what/{id}-foo"; char * path2 = "/what/{id}-foo";
ck_assert_str_eq( c = slug_compile(path2, strlen(path2) ) , "^/what/([^/]+)-foo" ); ck_assert_str_eq( c = r3_slug_compile(path2, strlen(path2) ) , "^/what/([^/]+)-foo" );
zfree(c); zfree(c);
char * path3 = "-{id}"; char * path3 = "-{id}";
ck_assert_str_eq( c = slug_compile(path3, strlen(path3)), "^-([^/]+)" ); ck_assert_str_eq( c = r3_slug_compile(path3, strlen(path3)), "^-([^/]+)" );
zfree(c); zfree(c);
char * path4 = "-{idx:\\d{3}}"; char * path4 = "-{idx:\\d{3}}";
ck_assert_str_eq( c = slug_compile(path4, strlen(path4)), "^-(\\d{3})" ); ck_assert_str_eq( c = r3_slug_compile(path4, strlen(path4)), "^-(\\d{3})" );
zfree(c); zfree(c);
} }
END_TEST END_TEST
@ -49,36 +49,36 @@ START_TEST (test_contains_slug)
} }
END_TEST END_TEST
START_TEST (test_slug_find_pattern) START_TEST (test_r3_slug_find_pattern)
{ {
int len; int len;
char * namerex = slug_find_pattern("{name:\\s+}", &len); char * namerex = r3_slug_find_pattern("{name:\\s+}", &len);
ck_assert( strncmp(namerex, "\\s+", len) == 0 ); ck_assert( strncmp(namerex, "\\s+", len) == 0 );
} }
END_TEST END_TEST
START_TEST (test_slug_find_placeholder) START_TEST (test_r3_slug_find_placeholder)
{ {
int slug_len = 0; int slug_len = 0;
char * slug; char * slug;
slug = slug_find_placeholder("/user/{name:\\s+}/to/{id}", &slug_len); slug = r3_slug_find_placeholder("/user/{name:\\s+}/to/{id}", &slug_len);
ck_assert( strncmp(slug, "{name:\\s+}", slug_len) == 0 ); ck_assert( strncmp(slug, "{name:\\s+}", slug_len) == 0 );
slug = slug_find_placeholder("/user/{idx:\\d{3}}/to/{idy:\\d{3}}", &slug_len); slug = r3_slug_find_placeholder("/user/{idx:\\d{3}}/to/{idy:\\d{3}}", &slug_len);
ck_assert( slug_len == strlen("{idx:\\d{3}}") ); ck_assert( slug_len == strlen("{idx:\\d{3}}") );
ck_assert( strncmp(slug, "{idx:\\d{3}}", slug_len) == 0 ); ck_assert( strncmp(slug, "{idx:\\d{3}}", slug_len) == 0 );
} }
END_TEST END_TEST
START_TEST (test_inside_slug) START_TEST (test_r3_inside_slug)
{ {
char * pattern = "/user/{name:\\s+}/to/{id}"; char * pattern = "/user/{name:\\s+}/to/{id}";
char * offset = strchr(pattern, '{') + 2; char * offset = strchr(pattern, '{') + 2;
ck_assert( (int)inside_slug(pattern, strlen(pattern), offset, NULL) ); ck_assert( (int)r3_inside_slug(pattern, strlen(pattern), offset, NULL) );
ck_assert( *(inside_slug(pattern, strlen(pattern), offset, NULL)) == '{' ); ck_assert( *(r3_inside_slug(pattern, strlen(pattern), offset, NULL)) == '{' );
ck_assert( ! inside_slug(pattern, strlen(pattern), pattern, NULL) ); ck_assert( ! r3_inside_slug(pattern, strlen(pattern), pattern, NULL) );
} }
END_TEST END_TEST
@ -159,10 +159,10 @@ START_TEST (test_slug_count)
} }
END_TEST END_TEST
START_TEST (test_slug_find_placeholder_with_broken_slug) START_TEST (test_r3_slug_find_placeholder_with_broken_slug)
{ {
int slug_len = 0; int slug_len = 0;
char * slug = slug_find_placeholder("/user/{name:\\s+/to/{id", &slug_len); char * slug = r3_slug_find_placeholder("/user/{name:\\s+/to/{id", &slug_len);
ck_assert(slug == 0); ck_assert(slug == 0);
} }
END_TEST END_TEST
@ -173,12 +173,12 @@ Suite* r3_suite (void) {
TCase *tcase = tcase_create("test_slug"); TCase *tcase = tcase_create("test_slug");
tcase_set_timeout(tcase, 30); tcase_set_timeout(tcase, 30);
tcase_add_test(tcase, test_contains_slug); tcase_add_test(tcase, test_contains_slug);
tcase_add_test(tcase, test_inside_slug); tcase_add_test(tcase, test_r3_inside_slug);
tcase_add_test(tcase, test_slug_find_pattern); tcase_add_test(tcase, test_r3_slug_find_pattern);
tcase_add_test(tcase, test_slug_find_placeholder); tcase_add_test(tcase, test_r3_slug_find_placeholder);
tcase_add_test(tcase, test_slug_find_placeholder_with_broken_slug); tcase_add_test(tcase, test_r3_slug_find_placeholder_with_broken_slug);
tcase_add_test(tcase, test_slug_count); tcase_add_test(tcase, test_slug_count);
tcase_add_test(tcase, test_slug_compile); tcase_add_test(tcase, test_r3_slug_compile);
tcase_add_test(tcase, test_pattern_to_opcode); tcase_add_test(tcase, test_pattern_to_opcode);
tcase_add_test(tcase, test_incomplete_slug); tcase_add_test(tcase, test_incomplete_slug);
// tcase_add_test(tcase, test_slug_parse_with_pattern); // tcase_add_test(tcase, test_slug_parse_with_pattern);

View file

@ -221,12 +221,6 @@ END_TEST
START_TEST (test_ltrim_slash)
{
fail_if( strcmp( ltrim_slash("/blog") , "blog" ) != 0 );
fail_if( strcmp( ltrim_slash("blog") , "blog" ) != 0 );
}
END_TEST
START_TEST (test_node_construct_and_free) START_TEST (test_node_construct_and_free)
{ {
@ -702,7 +696,6 @@ Suite* r3_suite (void) {
tcase_add_test(tcase, test_insert_pathl); tcase_add_test(tcase, test_insert_pathl);
tcase_add_test(tcase, test_insert_pathl_fail); tcase_add_test(tcase, test_insert_pathl_fail);
tcase_add_test(tcase, test_node_construct_and_free); tcase_add_test(tcase, test_node_construct_and_free);
tcase_add_test(tcase, test_ltrim_slash);
tcase_add_test(tcase, test_compile); tcase_add_test(tcase, test_compile);
tcase_add_test(tcase, test_compile_fail); tcase_add_test(tcase, test_compile_fail);
tcase_add_test(tcase, test_route_cmp); tcase_add_test(tcase, test_route_cmp);