-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtinymatch.c
More file actions
20 lines (18 loc) · 835 Bytes
/
tinymatch.c
File metadata and controls
20 lines (18 loc) · 835 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// tiny wildcard/pattern matching. Based on anonymous souce code (Rob Pike's ?).
// - rlyeh. public domain | wtrmrkrlyeh
static int match( const char *pattern, const char *str ) {
if( *pattern=='\0' ) return !*str;
if( *pattern=='*' ) return match(pattern+1, str) || (*str && match(pattern, str+1));
if( *pattern=='?' ) return *str && (*str != '.') && match(pattern+1, str+1);
return (*str == *pattern) && match(pattern+1, str+1);
}
/*
#include <stdio.h>
int main() {
printf("%s\n", match("abc", "abc") ? "match!" : "not found" );
printf("%s\n", match("abc*", "abc") ? "match!" : "not found" );
printf("%s\n", match("*bc", "abc") ? "match!" : "not found" );
printf("%s\n", match("*bc*", "abc") ? "match!" : "not found" );
printf("%s\n", match("*b?d*", "abcdef") ? "match!" : "not found" );
}
*/