/src/syntax-highlighting/src/lib/wildcardmatcher.cpp
Line | Count | Source |
1 | | /* |
2 | | SPDX-FileCopyrightText: 2007 Sebastian Pipping <webmaster@hartwork.org> |
3 | | |
4 | | SPDX-License-Identifier: MIT |
5 | | */ |
6 | | |
7 | | #include "wildcardmatcher.h" |
8 | | |
9 | | using namespace KSyntaxHighlighting; |
10 | | |
11 | | #include <QChar> |
12 | | |
13 | | namespace |
14 | | { |
15 | | bool wildcardMatch(QStringView candidate, QStringView wildcard, int candidatePosFromRight, int wildcardPosFromRight) |
16 | 38.5M | { |
17 | 39.5M | for (; wildcardPosFromRight >= 0; wildcardPosFromRight--) { |
18 | 39.5M | const auto ch = wildcard.at(wildcardPosFromRight).unicode(); |
19 | 39.5M | switch (ch) { |
20 | 761k | case L'*': |
21 | 761k | if (candidatePosFromRight == -1) { |
22 | 0 | break; |
23 | 0 | } |
24 | | |
25 | 761k | if (wildcardPosFromRight == 0) { |
26 | 4 | return true; |
27 | 4 | } |
28 | | |
29 | | // Eat all we can and go back as far as we have to |
30 | 11.9M | for (int j = -1; j <= candidatePosFromRight; j++) { |
31 | 11.1M | if (wildcardMatch(candidate, wildcard, j, wildcardPosFromRight - 1)) { |
32 | 4 | return true; |
33 | 4 | } |
34 | 11.1M | } |
35 | 761k | return false; |
36 | | |
37 | 0 | case L'?': |
38 | 0 | if (candidatePosFromRight == -1) { |
39 | 0 | return false; |
40 | 0 | } |
41 | | |
42 | 0 | candidatePosFromRight--; |
43 | 0 | break; |
44 | | |
45 | 38.7M | default: |
46 | 38.7M | if (candidatePosFromRight == -1) { |
47 | 761k | return false; |
48 | 761k | } |
49 | | |
50 | 38.0M | const auto candidateCh = candidate.at(candidatePosFromRight).unicode(); |
51 | 38.0M | if (candidateCh == ch) { |
52 | 1.04M | candidatePosFromRight--; |
53 | 36.9M | } else { |
54 | 36.9M | return false; |
55 | 36.9M | } |
56 | 39.5M | } |
57 | 39.5M | } |
58 | 0 | return candidatePosFromRight == -1; |
59 | 38.5M | } |
60 | | |
61 | | } // unnamed namespace |
62 | | |
63 | | bool WildcardMatcher::exactMatch(QStringView candidate, QStringView wildcard) |
64 | 27.3M | { |
65 | 27.3M | return ::wildcardMatch(candidate, wildcard, candidate.length() - 1, wildcard.length() - 1); |
66 | 27.3M | } |