Kate
katewildcardmatcher.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2007 Sebastian Pipping <webmaster@hartwork.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License version 2 as published by the Free Software Foundation. 00007 00008 This library is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 Library General Public License for more details. 00012 00013 You should have received a copy of the GNU Library General Public License 00014 along with this library; see the file COPYING.LIB. If not, write to 00015 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00016 Boston, MA 02110-1301, USA. 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 // Eat all we can and go back as far as we have to 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
KDE 4.6 API Reference