File indexing completed on 2024-05-12 15:50:07
0001 /* 0002 SPDX-FileCopyrightText: 2007 Sebastian Pipping <webmaster@hartwork.org> 0003 0004 SPDX-License-Identifier: MIT 0005 */ 0006 0007 #include "wildcardmatcher.h" 0008 0009 using namespace KSyntaxHighlighting; 0010 0011 #include <QChar> 0012 0013 namespace 0014 { 0015 bool wildcardMatch(QStringView candidate, QStringView wildcard, int candidatePosFromRight, int wildcardPosFromRight) 0016 { 0017 for (; wildcardPosFromRight >= 0; wildcardPosFromRight--) { 0018 const auto ch = wildcard.at(wildcardPosFromRight).unicode(); 0019 switch (ch) { 0020 case L'*': 0021 if (candidatePosFromRight == -1) { 0022 break; 0023 } 0024 0025 if (wildcardPosFromRight == 0) { 0026 return true; 0027 } 0028 0029 // Eat all we can and go back as far as we have to 0030 for (int j = -1; j <= candidatePosFromRight; j++) { 0031 if (wildcardMatch(candidate, wildcard, j, wildcardPosFromRight - 1)) { 0032 return true; 0033 } 0034 } 0035 return false; 0036 0037 case L'?': 0038 if (candidatePosFromRight == -1) { 0039 return false; 0040 } 0041 0042 candidatePosFromRight--; 0043 break; 0044 0045 default: 0046 if (candidatePosFromRight == -1) { 0047 return false; 0048 } 0049 0050 const auto candidateCh = candidate.at(candidatePosFromRight).unicode(); 0051 if (candidateCh == ch) { 0052 candidatePosFromRight--; 0053 } else { 0054 return false; 0055 } 0056 } 0057 } 0058 return candidatePosFromRight == -1; 0059 } 0060 0061 } // unnamed namespace 0062 0063 bool WildcardMatcher::exactMatch(QStringView candidate, QStringView wildcard) 0064 { 0065 return ::wildcardMatch(candidate, wildcard, candidate.length() - 1, wildcard.length() - 1); 0066 }