diff --git a/include/r3.h b/include/r3.h index e6ba33a..2a75460 100644 --- a/include/r3.h +++ b/include/r3.h @@ -174,6 +174,8 @@ route * r3_tree_match_route(const node *n, match_entry * entry); #define METHOD_HEAD 2<<5 #define METHOD_OPTIONS 2<<6 -enum { OP_EXPECT_DIGITS, OP_EXPECT_WORDS, OP_EXPECT_NOSLASH }; +int r3_pattern_to_opcode(char * pattern, int pattern_len); + +enum { OP_EXPECT_DIGITS = 1, OP_EXPECT_WORDS, OP_EXPECT_NOSLASH }; #endif /* !R3_NODE_H */ diff --git a/src/str.c b/src/str.c index a02da74..1d63873 100644 --- a/src/str.c +++ b/src/str.c @@ -13,6 +13,20 @@ #include "str_array.h" #include "zmalloc.h" +int r3_pattern_to_opcode(char * pattern, int pattern_len) { + if ( strncmp(pattern, "\\w+", pattern_len) == 0 ) { + return OP_EXPECT_WORDS; + } + if ( strncmp(pattern, "\\d+", pattern_len) == 0 ) { + return OP_EXPECT_DIGITS; + } + if ( strncmp(pattern, "[^/]+", pattern_len) == 0 ) { + return OP_EXPECT_NOSLASH; + } + return 0; +} + + /** * provide a quick way to count slugs, simply search for '{' diff --git a/tests/check_slug.c b/tests/check_slug.c index 1453fd2..6acb7fa 100644 --- a/tests/check_slug.c +++ b/tests/check_slug.c @@ -13,6 +13,14 @@ #include "str_array.h" #include "zmalloc.h" +START_TEST (test_pattern_to_opcode) +{ + ck_assert( r3_pattern_to_opcode("\\w+", sizeof("\\w+") - 1) == OP_EXPECT_WORDS ); + ck_assert( r3_pattern_to_opcode("\\d+", sizeof("\\d+") - 1) == OP_EXPECT_DIGITS ); + ck_assert( r3_pattern_to_opcode("[^/]+", sizeof("[^/]+") - 1) == OP_EXPECT_NOSLASH ); +} +END_TEST + START_TEST (test_slug_compile) { char * path = "/user/{id}"; @@ -106,6 +114,7 @@ Suite* r3_suite (void) { tcase_add_test(tcase, test_find_slug_placeholder_with_broken_slug); tcase_add_test(tcase, test_slug_count); tcase_add_test(tcase, test_slug_compile); + tcase_add_test(tcase, test_pattern_to_opcode); suite_add_tcase(suite, tcase); return suite;