KSyntaxHighlighting

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

KDE's Doxygen guidelines are available online.