patternwildcard.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2020 Giuseppe D'Angelo <dangelog@gmail.com>.
4 ** Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
5 ** Copyright (C) 2016 The Qt Company Ltd.
6 ** Contact: https://www.qt.io/licensing/
7 **
8 ** This file is part of the QtCore module of the Qt Toolkit.
9 **
10 ** $QT_BEGIN_LICENSE:LGPL$
11 ** Commercial License Usage
12 ** Licensees holding valid commercial Qt licenses may use this file in
13 ** accordance with the commercial license agreement provided with the
14 ** Software or, alternatively, in accordance with the terms contained in
15 ** a written agreement between you and The Qt Company. For licensing terms
16 ** and conditions see https://www.qt.io/terms-conditions. For further
17 ** information use the contact form at https://www.qt.io/contact-us.
18 **
19 ** GNU Lesser General Public License Usage
20 ** Alternatively, this file may be used under the terms of the GNU Lesser
21 ** General Public License version 3 as published by the Free Software
22 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
23 ** packaging of this file. Please review the following information to
24 ** ensure the GNU Lesser General Public License version 3 requirements
25 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
26 **
27 ** GNU General Public License Usage
28 ** Alternatively, this file may be used under the terms of the GNU
29 ** General Public License version 2.0 or (at your option) the GNU General
30 ** Public license version 3 or any later version approved by the KDE Free
31 ** Qt Foundation. The licenses are as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
33 ** included in the packaging of this file. Please review the following
34 ** information to ensure the GNU General Public License requirements will
35 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
36 ** https://www.gnu.org/licenses/gpl-3.0.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 // Copy of QRegularExpression::wildcardToRegularExpression from Qt 5.15 for use
43 // with Qt < 5.12
44 
45 #include "pattern.h"
46 
47 #if QT_VERSION < QT_VERSION_CHECK(5, 12, 0)
48 static inline QString anchoredPattern(const QString &expression)
49 {
50  return QLatin1String("\\A(?:")
51  + expression
52  + QLatin1String(")\\z");
53 }
54 
105 QString Pattern::wildcardToRegularExpression(const QString &pattern)
106 {
107  const int wclen = pattern.length();
108  QString rx;
109  rx.reserve(wclen + wclen / 16);
110  int i = 0;
111  const QChar *wc = pattern.data();
112 
113 #ifdef Q_OS_WIN
114  const QLatin1Char nativePathSeparator('\\');
115  const QLatin1String starEscape("[^/\\\\]*");
116  const QLatin1String questionMarkEscape("[^/\\\\]");
117 #else
118  const QLatin1Char nativePathSeparator('/');
119  const QLatin1String starEscape("[^/]*");
120  const QLatin1String questionMarkEscape("[^/]");
121 #endif
122 
123  while (i < wclen) {
124  const QChar c = wc[i++];
125  switch (c.unicode()) {
126  case '*':
127  rx += starEscape;
128  break;
129  case '?':
130  rx += questionMarkEscape;
131  break;
132  case '\\':
133 #ifdef Q_OS_WIN
134  case '/':
135  rx += QLatin1String("[/\\\\]");
136  break;
137 #endif
138  case '$':
139  case '(':
140  case ')':
141  case '+':
142  case '.':
143  case '^':
144  case '{':
145  case '|':
146  case '}':
147  rx += QLatin1Char('\\');
148  rx += c;
149  break;
150  case '[':
151  rx += c;
152  // Support for the [!abc] or [!a-c] syntax
153  if (i < wclen) {
154  if (wc[i] == QLatin1Char('!')) {
155  rx += QLatin1Char('^');
156  ++i;
157  }
158 
159  if (i < wclen && wc[i] == QLatin1Char(']'))
160  rx += wc[i++];
161 
162  while (i < wclen && wc[i] != QLatin1Char(']')) {
163  // The '/' appearing in a character class invalidates the
164  // regular expression parsing. It also concerns '\\' on
165  // Windows OS types.
166  if (wc[i] == QLatin1Char('/') || wc[i] == nativePathSeparator)
167  return rx;
168  if (wc[i] == QLatin1Char('\\'))
169  rx += QLatin1Char('\\');
170  rx += wc[i++];
171  }
172  }
173  break;
174  default:
175  rx += c;
176  break;
177  }
178  }
179 
180  return anchoredPattern(rx);
181 }
182 #endif