From 646f28ba57a773477be31d332fa1e772f5d89492 Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Mon, 3 Jun 2019 07:57:18 +0200 Subject: [PATCH] forgotten files --- src/miscellaneous/regexfactory.cpp | 131 +++++++++++++++++++++++++++++ src/miscellaneous/regexfactory.h | 66 +++++++++++++++ 2 files changed, 197 insertions(+) create mode 100644 src/miscellaneous/regexfactory.cpp create mode 100644 src/miscellaneous/regexfactory.h diff --git a/src/miscellaneous/regexfactory.cpp b/src/miscellaneous/regexfactory.cpp new file mode 100644 index 000000000..8920b2d7a --- /dev/null +++ b/src/miscellaneous/regexfactory.cpp @@ -0,0 +1,131 @@ +// For license of this file, see /LICENSE.md. + +/**************************************************************************** +** +** Copyright (C) 2016 Giuseppe D'Angelo . +** Copyright (C) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtCore module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "miscellaneous/regexfactory.h" + +QString RegexFactory::wildcardToRegularExpression(const QString& pattern) { + const int wclen = pattern.length(); + QString rx; + + rx.reserve(wclen + wclen / 16); + int i = 0; + const QChar* wc = pattern.unicode(); + +#ifdef Q_OS_WIN + const QLatin1Char nativePathSeparator('\\'); + const QLatin1String starEscape("[^/\\\\]*"); + const QLatin1String questionMarkEscape("[^/\\\\]"); +#else + const QLatin1Char nativePathSeparator('/'); + const QLatin1String starEscape("[^/]*"); + const QLatin1String questionMarkEscape("[^/]"); +#endif + + while (i < wclen) { + const QChar c = wc[i++]; + + switch (c.unicode()) { + case '*': + rx += starEscape; + break; + + case '?': + rx += questionMarkEscape; + break; + + case '\\': +#ifdef Q_OS_WIN + case '/': + rx += QLatin1String("[/\\\\]"); + break; +#endif + case '$': + case '(': + case ')': + case '+': + case '.': + case '^': + case '{': + case '|': + case '}': + rx += QLatin1Char('\\'); + rx += c; + break; + + case '[': + rx += c; + + // Support for the [!abc] or [!a-c] syntax + if (i < wclen) { + if (wc[i] == QLatin1Char('!')) { + rx += QLatin1Char('^'); + ++i; + } + + if (i < wclen && wc[i] == QLatin1Char(']')) + rx += wc[i++]; + + while (i < wclen && wc[i] != QLatin1Char(']')) { + // The '/' appearing in a character class invalidates the + // regular expression parsing. It also concerns '\\' on + // Windows OS types. + if (wc[i] == QLatin1Char('/') || wc[i] == nativePathSeparator) + return rx; + + if (wc[i] == QLatin1Char('\\')) + rx += QLatin1Char('\\'); + + rx += wc[i++]; + } + } + + break; + + default: + rx += c; + break; + } + } + + return anchoredPattern(rx); +} diff --git a/src/miscellaneous/regexfactory.h b/src/miscellaneous/regexfactory.h new file mode 100644 index 000000000..469a80625 --- /dev/null +++ b/src/miscellaneous/regexfactory.h @@ -0,0 +1,66 @@ +// For license of this file, see /LICENSE.md. + +/**************************************************************************** +** +** Copyright (C) 2016 Giuseppe D'Angelo . +** Copyright (C) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtCore module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef REGEXFACTORY_H +#define REGEXFACTORY_H + +#include "definitions/definitions.h" + +#include +#include + +class RegexFactory { + public: + + static QString anchoredPattern(const QString& expression); + static QString wildcardToRegularExpression(const QString& pattern); + + private: + explicit RegexFactory() = delete; +}; + +inline QString RegexFactory::anchoredPattern(const QString& expression) { + return QL1S("\\A(?:") + expression + QL1S(")\\z"); +} + +#endif // REGEXFACTORY_H