KSyntaxHighlighting

wildcardmatcher.cpp
1/*
2 SPDX-FileCopyrightText: 2007 Sebastian Pipping <webmaster@hartwork.org>
3
4 SPDX-License-Identifier: MIT
5*/
6
7#include "wildcardmatcher.h"
8
9using namespace KSyntaxHighlighting;
10
11#include <QChar>
12
13namespace
14{
15bool wildcardMatch(QStringView candidate, QStringView wildcard, int candidatePosFromRight, int wildcardPosFromRight)
16{
17 for (; wildcardPosFromRight >= 0; wildcardPosFromRight--) {
18 const auto ch = wildcard.at(wildcardPosFromRight).unicode();
19 switch (ch) {
20 case L'*':
21 if (candidatePosFromRight == -1) {
22 break;
23 }
24
25 if (wildcardPosFromRight == 0) {
26 return true;
27 }
28
29 // Eat all we can and go back as far as we have to
30 for (int j = -1; j <= candidatePosFromRight; j++) {
31 if (wildcardMatch(candidate, wildcard, j, wildcardPosFromRight - 1)) {
32 return true;
33 }
34 }
35 return false;
36
37 case L'?':
38 if (candidatePosFromRight == -1) {
39 return false;
40 }
41
42 candidatePosFromRight--;
43 break;
44
45 default:
46 if (candidatePosFromRight == -1) {
47 return false;
48 }
49
50 const auto candidateCh = candidate.at(candidatePosFromRight).unicode();
51 if (candidateCh == ch) {
52 candidatePosFromRight--;
53 } else {
54 return false;
55 }
56 }
57 }
58 return candidatePosFromRight == -1;
59}
60
61} // unnamed namespace
62
63bool WildcardMatcher::exactMatch(QStringView candidate, QStringView wildcard)
64{
65 return ::wildcardMatch(candidate, wildcard, candidate.length() - 1, wildcard.length() - 1);
66}
Syntax highlighting engine for Kate syntax definitions.
char16_t & unicode()
QChar at(qsizetype n) const const
qsizetype length() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:03:33 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.