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 '{' * provide a quick way to count slugs, simply search for '{'
*/ */
int slug_count(const char * p, int len, char **errstr) { int slug_count(const char * needle, int len, char **errstr) {
int s = 0; int cnt = 0;
int lev = 0; int state = 0;
while( len-- ) { char * p = (char*) needle;
if ( lev == 0 && *p == '{' )
s++; while( (p-needle) < len) {
if ( *p == '{' ) { if (state == 1 && *p == '}') {
lev++; cnt++;
} }
if ( *p == '}' ) { if ( *p == '{' ) {
lev--; state++;
} else if ( *p == '}' ) {
state--;
} }
p++; p++;
} };
/* info("FOUND PATTERN: %s (%d), CHAR: '%s', STATE: %d\n", needle, len, p, state);
XXX: if (state != 0) {
if (lev != 0) {
if (errstr) { 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 0;
} }
*/ return cnt;
return s;
} }
bool contains_slug(const char * str) { bool contains_slug(const char * str) {

View file

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

View file

@ -84,11 +84,15 @@ END_TEST
START_TEST (test_slug_count) START_TEST (test_slug_count)
{ {
int cnt = 0;
char * pattern = "/user/{name:\\s+}/to/{id}"; 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}"; 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 END_TEST
@ -96,7 +100,7 @@ START_TEST (test_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 = slug_find_placeholder("/user/{name:\\s+/to/{id", &slug_len);
ck_assert(! slug); ck_assert(slug == 0);
} }
END_TEST END_TEST