diff --git a/src/str.c b/src/str.c index c21cdef..939d067 100644 --- a/src/str.c +++ b/src/str.c @@ -48,6 +48,11 @@ int slug_count(const char * needle, int len, char **errstr) { char * p = (char*) needle; while( (p-needle) < len) { + + if (*p == '\\' ) { + p++; p++; + } + if (state == 1 && *p == '}') { cnt++; } @@ -58,10 +63,10 @@ int slug_count(const char * needle, int len, char **errstr) { } p++; }; - info("FOUND PATTERN: %s (%d), CHAR: '%s', STATE: %d\n", needle, len, p, state); + info("FOUND PATTERN: '%s' (%d), STATE: %d\n", needle, len, state); if (state != 0) { if (errstr) { - asprintf(errstr, "incomplete slug pattern. PATTERN: %s (%d), CHAR: '%s', STATE: %d\n", needle, len, p, state); + asprintf(errstr, "incomplete slug pattern. PATTERN (%d): '%s' (%d), OFFSET: %d, STATE: %d", len, needle, p - needle, state); } return 0; } diff --git a/tests/check_slug.c b/tests/check_slug.c index 7ba7a10..0919487 100644 --- a/tests/check_slug.c +++ b/tests/check_slug.c @@ -82,6 +82,18 @@ START_TEST (test_inside_slug) } END_TEST +START_TEST (test_incomplete_slug) +{ + int cnt = 0; + char * errstr = NULL; + char * pattern = "/user/{name:\\d{3}}/to/{id"; + cnt = slug_count(pattern, strlen(pattern), &errstr); + ck_assert_int_eq(cnt, 0); + ck_assert(errstr); + printf("%s\n",errstr); +} +END_TEST + START_TEST (test_slug_count) { int cnt = 0; @@ -93,6 +105,10 @@ START_TEST (test_slug_count) char * pattern2 = "/user/{name:\\d{3}}/to/{id}"; cnt = slug_count(pattern2, strlen(pattern2), &errstr); ck_assert_int_eq(cnt, 2); + + char * pattern3 = "/user/{name:\\d{3}}/to/{id}"; + cnt = slug_count(pattern3, strlen(pattern3), &errstr); + ck_assert_int_eq(cnt, 2); } END_TEST @@ -117,6 +133,7 @@ Suite* r3_suite (void) { tcase_add_test(tcase, test_slug_count); tcase_add_test(tcase, test_slug_compile); tcase_add_test(tcase, test_pattern_to_opcode); + tcase_add_test(tcase, test_incomplete_slug); suite_add_tcase(suite, tcase); return suite;