Debian Codegraph @

src/librssguard/miscellaneous/regexfactory.cpp

raw symbols SCIP
1 // For license of this file, see <project-root-folder>/LICENSE.md.
2
3 /****************************************************************************
4 **
5 ** Copyright (C) 2016 Giuseppe D'Angelo <[email protected]>.
6 ** Copyright (C) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, [email protected], author Giuseppe D'Angelo
7 *<[email protected]>
8 ** Copyright (C) 2016 The Qt Company Ltd.
9 ** Contact: https://www.qt.io/licensing/
10 **
11 ** This file is part of the QtCore module of the Qt Toolkit.
12 **
13 ** $QT_BEGIN_LICENSE:LGPL$
14 ** Commercial License Usage
15 ** Licensees holding valid commercial Qt licenses may use this file in
16 ** accordance with the commercial license agreement provided with the
17 ** Software or, alternatively, in accordance with the terms contained in
18 ** a written agreement between you and The Qt Company. For licensing terms
19 ** and conditions see https://www.qt.io/terms-conditions. For further
20 ** information use the contact form at https://www.qt.io/contact-us.
21 **
22 ** GNU Lesser General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU Lesser
24 ** General Public License version 3 as published by the Free Software
25 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
26 ** packaging of this file. Please review the following information to
27 ** ensure the GNU Lesser General Public License version 3 requirements
28 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 2.0 or (at your option) the GNU General
33 ** Public license version 3 or any later version approved by the KDE Free
34 ** Qt Foundation. The licenses are as published by the Free Software
35 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
36 ** included in the packaging of this file. Please review the following
37 ** information to ensure the GNU General Public License requirements will
38 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
39 ** https://www.gnu.org/licenses/gpl-3.0.html.
40 **
41 ** $QT_END_LICENSE$
42 **
43 ****************************************************************************/
44
45 #include "miscellaneous/regexfactory.h"
46
47 QString RegexFactory::wildcardToRegularExpression(const QString& pattern) {
48 const int wclen = pattern.length();
49 QString rx;
50
51 rx.reserve(wclen + wclen / 16);
52 int i = 0;
53 const QChar* wc = pattern.unicode();
54
55 #if defined(Q_OS_WIN)
56 const QLatin1Char nativePathSeparator('\\');
57 const QLatin1String starEscape("[^/\\\\]*");
58 const QLatin1String questionMarkEscape("[^/\\\\]");
59 #else
60 const QLatin1Char nativePathSeparator('/');
61 const QLatin1String starEscape("[^/]*");
62 const QLatin1String questionMarkEscape("[^/]");
63 #endif
64
65 while (i < wclen) {
66 const QChar c = wc[i++];
67
68 switch (c.unicode()) {
69 case '*':
70 rx += starEscape;
71 break;
72
73 case '?':
74 rx += questionMarkEscape;
75 break;
76
77 case '\\':
78 #if defined(Q_OS_WIN)
79 case '/':
80 rx += QLatin1String("[/\\\\]");
81 break;
82 #endif
83 case '$':
84 case '(':
85 case ')':
86 case '+':
87 case '.':
88 case '^':
89 case '{':
90 case '|':
91 case '}':
92 rx += QLatin1Char('\\');
93 rx += c;
94 break;
95
96 case '[':
97 rx += c;
98
99 // Support for the [!abc] or [!a-c] syntax
100 if (i < wclen) {
101 if (wc[i] == QLatin1Char('!')) {
102 rx += QLatin1Char('^');
103 ++i;
104 }
105
106 if (i < wclen && wc[i] == QLatin1Char(']'))
107 rx += wc[i++];
108
109 while (i < wclen && wc[i] != QLatin1Char(']')) {
110 // The '/' appearing in a character class invalidates the
111 // regular expression parsing. It also concerns '\\' on
112 // Windows OS types.
113 if (wc[i] == QLatin1Char('/') || wc[i] == nativePathSeparator)
114 return rx;
115
116 if (wc[i] == QLatin1Char('\\'))
117 rx += QLatin1Char('\\');
118
119 rx += wc[i++];
120 }
121 }
122
123 break;
124
125 default:
126 rx += c;
127 break;
128 }
129 }
130
131 return anchoredPattern(rx);
132 }