Last active
February 28, 2022 08:40
-
-
Save orca-zhang/fe6610c3d47299439a0fa118c925e29c to your computer and use it in GitHub Desktop.
case sensitive wildcards match function
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
/* | |
MIT License | |
Copyright (c) 2010-2017 <http://ez8.co> <orca.zhang@yahoo.com> | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
*/ | |
#include <stdio.h> | |
#include <string.h> | |
#include <stack> | |
#define case_sens_eq(x, y, cs) (y == '?' || x == y || (!cs && (unsigned)((x & -33) - 0x41) < 26 && (x ^ y) == 0x20)) | |
bool MatchString(const char* src, const char* matcher, bool case_sens = 1) | |
{ | |
std::stack<std::pair<const char*, const char*> > s; | |
// scan from start | |
while(*src) { | |
// match asterisk | |
if(*matcher == '*') { | |
// skip continuous asterisks | |
while(*matcher == '*' && ++matcher); | |
// forward to first match char of src | |
while(*src && !case_sens_eq(*src, *matcher, case_sens) && ++src); | |
// record current position for back-track | |
s.push(std::make_pair(src + 1, matcher - 1)); | |
} | |
// eat one character | |
else if(case_sens_eq(*src, *matcher, case_sens)) { | |
++src, ++matcher; | |
} | |
// hit mismatch, then back-track | |
else { | |
if(s.empty()) | |
return false; | |
src = s.top().first, matcher = s.top().second; | |
s.pop(); | |
} | |
} | |
// ignore ending asterisks | |
while(*matcher == '*' && ++matcher); | |
return (!*src && !*matcher); | |
} | |
void matcher(const char* src, const char* matcher, bool case_sens = 1) | |
{ | |
printf("[%s] src: %s matcher: %s\n", MatchString(src, matcher, case_sens) ? "true" : "false", src, matcher); | |
} | |
int main() | |
{ | |
matcher("aa", "a*a"); | |
matcher("abcda", "ccc"); | |
matcher("abcda", "a*a"); | |
matcher("dabcsjabcd", "*abc****a?cd**"); | |
matcher("abcaxcd", "*abc*a?cd**"); | |
matcher("dabssjaxcdal", "*abc***a?cd*"); | |
matcher("dabssabcjaxceaxcd9", "*abc*a?cd*"); | |
matcher("A-2-101~122 -7.7_t3.txt", "*.txt"); | |
matcher("ABBCBCCC.TXT", "A*BC??.txt", 0); | |
matcher("AA", "a*a", 0); | |
matcher("Aaaa", "a*a", 0); | |
matcher("ABA", "a?a", 0); | |
matcher("ABAd", "a?a", 0); | |
matcher("xcd", "*?CD*", 0); | |
/* | |
[true] src: aa matcher: a*a | |
[false] src: abcda matcher: ccc | |
[true] src: abcda matcher: a*a | |
[true] src: kjsajkabxasdabcsjabcd matcher: *abc****a?cd** | |
[true] src: abcaxcd matcher: *abc*a?cd** | |
[false] src: kjsajkabxasdabssjaxcdal matcher: *abc***a?cd* | |
[true] src: kjsajkabxasdabssabcjaxceaxcd9 matcher: *abc*a?cd* | |
[true] src: A-2-101~122 -7.7_t3.txt matcher: *.txt | |
[true] src: ABBCBCCC.TXT matcher: A*BC??.txt | |
[true] src: AA matcher: a*a | |
[true] src: Aaaa matcher: a*a | |
[true] src: ABA matcher: a?a | |
[false] src: ABAd matcher: a?a | |
[true] src: xcd matcher: *?CD* | |
*/ | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment