File indexing completed on 2024-05-05 04:38:47

0001 /*
0002     SPDX-FileCopyrightText: 2020 Igor Kushnir <igorkuo@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KDEVPLATFORM_WILDCARDHELPERS_H
0008 #define KDEVPLATFORM_WILDCARDHELPERS_H
0009 
0010 #include <QRegExp>
0011 #include <QString>
0012 #include <QStringList>
0013 
0014 #include <algorithm>
0015 
0016 namespace WildcardHelpers {
0017 /**
0018  * @note Unlike the QDir::match() overload with the same signature, this
0019  * function treats its first argument as a single pattern, not a filter with
0020  * multiple patterns separated by spaces or semicolons.
0021  */
0022 inline bool matchSinglePattern(const QString& pattern, const QString& fileName)
0023 {
0024     QRegExp rx(pattern, Qt::CaseInsensitive, QRegExp::Wildcard);
0025     return rx.exactMatch(fileName);
0026 }
0027 
0028 /**
0029  *  @brief Works exactly as the QDir::match() overload with the same signature
0030  *  before Qt Base commit a2c85bffbeaa027e98fb6c23b2d7919adc8d28b7.
0031  *
0032  *  @note In this commit QDir::match() migrated to QRegularExpression and changed
0033  *  behavior as described in QTBUG-73797. Unlike QRegExp, QRegularExpression
0034  *  wildcard support follows closely the definition of wildcard for glob patterns.
0035  *  Most notably '*' in a wildcard no longer matches '/' (and '\\' on Windows).
0036  */
0037 inline bool match(const QStringList& filters, const QString& fileName)
0038 {
0039     return std::any_of(filters.cbegin(), filters.cend(), [&fileName](const QString& pattern) {
0040         return matchSinglePattern(pattern, fileName);
0041     });
0042 }
0043 }
0044 
0045 #endif // KDEVPLATFORM_WILDCARDHELPERS_H