Created
June 12, 2026 17:18
-
-
Save uplime/18bf75019aabf3f853a127dae824728e to your computer and use it in GitHub Desktop.
A simple and effective glob star matcher.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| int match(const char *needle, const char *haystack) { | |
| while(*haystack || *needle) { | |
| if(*needle == '*') { | |
| if(*(needle + 1) == '\0') { | |
| return 1; | |
| } else if(*(needle + 1) == '*') { | |
| needle += 1; | |
| } else if(*(needle + 1) == *haystack) { | |
| needle += 1; | |
| } else if(*haystack == '\0') { | |
| needle += 1; | |
| } else { | |
| haystack += 1; | |
| } | |
| } else if(*needle == *haystack) { | |
| needle += 1; | |
| haystack += 1; | |
| } else { | |
| return 0; | |
| } | |
| } | |
| return 1; | |
| } | |
| int main(int argc, char **argv) { | |
| if(argc != 3) { | |
| return 1; | |
| } | |
| return !match(argv[1], argv[2]); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment