slug_count return errstr

This commit is contained in:
c9s 2014-06-02 00:22:12 +08:00
parent da2f2311c9
commit 258128f30d
3 changed files with 27 additions and 22 deletions

View file

@ -42,30 +42,30 @@ int r3_pattern_to_opcode(const char * pattern, int len) {
/**
* provide a quick way to count slugs, simply search for '{'
*/
int slug_count(const char * p, int len, char **errstr) {
int s = 0;
int lev = 0;
while( len-- ) {
if ( lev == 0 && *p == '{' )
s++;
if ( *p == '{' ) {
lev++;
int slug_count(const char * needle, int len, char **errstr) {
int cnt = 0;
int state = 0;
char * p = (char*) needle;
while( (p-needle) < len) {
if (state == 1 && *p == '}') {
cnt++;
}
if ( *p == '}' ) {
lev--;
if ( *p == '{' ) {
state++;
} else if ( *p == '}' ) {
state--;
}
p++;
}
/*
XXX:
if (lev != 0) {
};
info("FOUND PATTERN: %s (%d), CHAR: '%s', STATE: %d\n", needle, len, p, state);
if (state != 0) {
if (errstr) {
asprintf(errstr, "incomplete slug pattern: %s", p);
asprintf(errstr, "incomplete slug pattern. PATTERN: %s (%d), CHAR: '%s', STATE: %d\n", needle, len, p, state);
}
return 0;
}
*/
return s;
return cnt;
}
bool contains_slug(const char * str) {

View file

@ -41,8 +41,9 @@ TESTS += check_gvc
check_gvc_SOURCES = check_gvc.c
endif
TESTS += benchmark
benchmark_SOURCES = bench.c
# TESTS += benchmark
# benchmark_SOURCES = bench.c
check_PROGRAMS = $(TESTS)

View file

@ -84,11 +84,15 @@ END_TEST
START_TEST (test_slug_count)
{
int cnt = 0;
char * pattern = "/user/{name:\\s+}/to/{id}";
ck_assert_int_eq( slug_count(pattern, strlen(pattern), NULL), 2 );
char * errstr = NULL;
cnt = slug_count(pattern, strlen(pattern), &errstr);
ck_assert_int_eq(cnt, 2);
char * pattern2 = "/user/{name:\\d{3}}/to/{id}";
ck_assert_int_eq( slug_count(pattern2, strlen(pattern), NULL), 2 );
cnt = slug_count(pattern2, strlen(pattern2), &errstr);
ck_assert_int_eq(cnt, 2);
}
END_TEST
@ -96,7 +100,7 @@ START_TEST (test_slug_find_placeholder_with_broken_slug)
{
int slug_len = 0;
char * slug = slug_find_placeholder("/user/{name:\\s+/to/{id", &slug_len);
ck_assert(! slug);
ck_assert(slug == 0);
}
END_TEST