Kate
katewildcardmatcher.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "katewildcardmatcher.h"
00020 #include <QString>
00021 #include <QChar>
00022
00023
00024
00025 namespace KateWildcardMatcher {
00026
00027
00028
00029 bool exactMatch(const QString & candidate, const QString & wildcard, int candidatePosFromRight,
00030 int wildcardPosFromRight, bool caseSensitive = true) {
00031 for (; wildcardPosFromRight >= 0; wildcardPosFromRight--) {
00032 const ushort ch = wildcard[wildcardPosFromRight].unicode();
00033 switch (ch) {
00034 case L'*':
00035 if (candidatePosFromRight == -1) {
00036 break;
00037 }
00038
00039 if (wildcardPosFromRight == 0) {
00040 return true;
00041 }
00042
00043
00044 for (int j = -1; j <= candidatePosFromRight; j++) {
00045 if (exactMatch(candidate, wildcard, j, wildcardPosFromRight - 1)) {
00046 return true;
00047 }
00048 }
00049 return false;
00050
00051 case L'?':
00052 if (candidatePosFromRight == -1) {
00053 return false;
00054 }
00055
00056 candidatePosFromRight--;
00057 break;
00058
00059 default:
00060 if (candidatePosFromRight == -1) {
00061 return false;
00062 }
00063
00064 const ushort candidateCh = candidate[candidatePosFromRight].unicode();
00065 const bool match = caseSensitive
00066 ? (candidateCh == ch)
00067 : (QChar::toLower(candidateCh) == QChar::toLower(ch));
00068 if (match) {
00069 candidatePosFromRight--;
00070 } else {
00071 return false;
00072 }
00073 }
00074 }
00075 return true;
00076 }
00077
00078
00079
00080 bool exactMatch(const QString & candidate, const QString & wildcard,
00081 bool caseSensitive) {
00082 return exactMatch(candidate, wildcard, candidate.length() - 1,
00083 wildcard.length() - 1, caseSensitive);
00084 }
00085
00086
00087
00088 }
00089