aboutsummaryrefslogtreecommitdiffstats
path: root/searchsummary/src/vespa/juniper/wildcard_match.h
blob: 2fb9dc9c8a438c12cd0d9cfc3e22b2f067c463c8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

namespace fast
{
namespace util
{
template<typename T>
bool wildcard_match(const T* word, const T* pattern, T multiple = '*',
                    T single = '?')
{
    while (*word != 0)
        if (*pattern == 0)
            return false;
        else if (*pattern == multiple)
        {
            // advance past occurrences of multiple
            while (*pattern == multiple)
                ++pattern;

            // if pattern ended with multiple, we're done
            if (*pattern == 0)
                return true;

            while (*word != 0)
            {
                // does this position in the word match
                if (*pattern == single || *pattern == *word)
                {
                    // test the rest of the word
                    if (wildcard_match(word, pattern, multiple, single) == true)
                    {
                        // it matched
                        return true;
                    }
                }

                // try next character
                ++word;
            }
        }
        else if (*pattern != single && *pattern != *word)
            return false;
        else
        {
            ++word;
            ++pattern;
        }

    // should be at end of pattern too if the word matched
    return *pattern == 0;
}
}
}