File: pathmatch.cpp

package info (click to toggle)
cppcheck 2.19.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,688 kB
  • sloc: cpp: 272,455; python: 22,408; ansic: 8,088; sh: 1,059; makefile: 1,041; xml: 987; cs: 291
file content (151 lines) | stat: -rw-r--r-- 4,604 bytes parent folder | download
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Cppcheck - A tool for static C/C++ code analysis
 * Copyright (C) 2007-2025 Cppcheck team.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "pathmatch.h"

#include "path.h"

#include <algorithm>
#include <stack>
#include <string>
#include <utility>
#include <vector>


PathMatch::PathMatch(std::vector<std::string> patterns, std::string basepath, Syntax syntax) :
    mPatterns(std::move(patterns)), mBasepath(std::move(basepath)), mSyntax(syntax)
{}

bool PathMatch::match(const std::string &path, Filemode mode) const
{
    return std::any_of(mPatterns.cbegin(), mPatterns.cend(), [=] (const std::string &pattern) {
        return match(pattern, path, mBasepath, mode, mSyntax);
    });
}

bool PathMatch::match(const std::string &pattern, const std::string &path, const std::string &basepath, Filemode mode, Syntax syntax)
{
    /* Fast paths for common patterns */
    if (pattern.empty())
        return false;

    if (pattern == "*" || pattern == "**")
        return true;

    /* If the pattern ends with a path separator it matches only directories. If the path names a regular file then
     * the last path component can't match. */
    bool dir_mismatch = PathIterator::issep(pattern.back(), syntax) && mode != Filemode::directory;

    if (!dir_mismatch && pattern == path)
        return true;

    /* A "real" pattern is absolute or relative to the base path. A pattern that isn't real can match at any
     * path component boundary. */
    bool real = Path::isAbsolute(pattern) || isRelativePattern(pattern);

    /* Pattern iterator */
    PathIterator s = PathIterator::fromPattern(pattern, basepath, syntax);
    /* Path iterator */
    PathIterator t = PathIterator::fromPath(path, basepath, syntax);

    if (dir_mismatch) {
        /* Final compponent can't match, so skip it. */
        while (*t != '\0' && *t != '/')
            ++t;
    }

    /* Pattern restart position */
    PathIterator p = s;
    /* Path restart position */
    PathIterator q = t;

    /* Backtrack stack */
    std::stack<std::pair<PathIterator::Pos, PathIterator::Pos>> b;

    for (;;) {
        switch (*s) {
        /* Star or star-star, matches any number of characters */
        case '*': {
            bool slash = false;
            ++s;
            if (*s == '*') {
                /* Star-star matches slashes as well */
                slash = true;
                ++s;
            }
            while (*t != '\0' && (slash || *t != '/')) {
                if (*s == *t) {
                    /* Could stop here, but do greedy match and add
                     * backtrack instead */
                    b.emplace(s.getpos(), t.getpos());
                }
                ++t;
            }
            continue;
        }
        /* Single character wildcard */
        case '?': {
            if (*t != '\0' && *t != '/') {
                ++s;
                ++t;
                continue;
            }
            break;
        }
        /* Start of pattern; matches start of path, or a path separator if the
         * pattern is not "real" (an absolute or relative path). */
        case '\0': {
            if (*t == '\0' || (*t == '/' && !real))
                return true;
            break;
        }
        /* Literal character */
        default: {
            if (*s == *t) {
                ++s;
                ++t;
                continue;
            }
            break;
        }
        }

        /* No match, try to backtrack */
        if (!b.empty()) {
            const auto bp = b.top();
            b.pop();
            s.setpos(bp.first);
            t.setpos(bp.second);
            continue;
        }

        /* Couldn't backtrack, try matching from the next path separator */
        while (*q != '\0' && *q != '/')
            ++q;

        if (*q == '/') {
            ++q;
            s = p;
            t = q;
            continue;
        }

        /* No more path seperators to try from */
        return false;
    }
}