[ create a new paste ] login | about

Link: http://codepad.org/a21BjBoJ    [ raw code | output | fork ]

malstrom - C++, pasted on Jun 2:
#include <iostream>
#include <cctype>

bool wildcardMatch(const char* p, const char* s) {
    while (*p != '\0') {
        if (*p == '*') {
            if (wildcardMatch(p + 1, s)) {
                return true;
            }
        } else if (*p == '?' || tolower(*p) == tolower(*s)) {
            ++p;
        } else {
            return false;
        }
        ++s;
    }
    return (*s == '\0');
}

void test(const char* x, const char* s, bool expect) {
    bool result = wildcardMatch(x, s);
    std::cout << (result == expect ? "    ok : " : "NOT OK : ")
            << s << " matches " << x << " = " << (result ? "yes" : "no")
            << std::endl;
}

int main(int argc, const char* argv[]) {
    test("", "", true);
    test("", "ab", false);
    test("abc", "ab", false);
    test("abc", "abc", true);
    test("abc", "abcd", false);
    test("abc", "abd", false);
    test("abc*", "abc", true);
    test("abc*", "ab", false);
    test("abc*", "abcd", true);
    test("abc*", "abcde", true);
    test("abc*d*", "abcd", true);
    test("abc*d*", "abcxxdyy", true);
    test("abc?d*", "abcxxdyy", false);
    test("abc?d*", "abcdyy", false);
    test("abc?d*", "abcxdyy", true);
    return 0;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    ok :  matches  = yes
    ok : ab matches  = no
    ok : ab matches abc = no
    ok : abc matches abc = yes
    ok : abcd matches abc = no
    ok : abd matches abc = no
    ok : abc matches abc* = yes
    ok : ab matches abc* = no
    ok : abcd matches abc* = yes
    ok : abcde matches abc* = yes
    ok : abcd matches abc*d* = yes
    ok : abcxxdyy matches abc*d* = yes
    ok : abcxxdyy matches abc?d* = no
    ok : abcdyy matches abc?d* = no
    ok : abcxdyy matches abc?d* = yes


Create a new paste based on this one


Comments: