Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/mimetypes/qmimeglobpattern.cpp |
Switch to Source code | Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
1 | - | |||||||
2 | - | |||||||
3 | - | |||||||
4 | - | |||||||
5 | - | |||||||
6 | void QMimeGlobMatchResult::addMatch(const QString &mimeType, int weight, const QString &pattern) | - | ||||||
7 | { | - | ||||||
8 | - | |||||||
9 | if (weight < m_weight) | - | ||||||
10 | return; | - | ||||||
11 | bool replace = weight > m_weight; | - | ||||||
12 | if (!replace) { | - | ||||||
13 | - | |||||||
14 | if (pattern.length() < m_matchingPatternLength) | - | ||||||
15 | return; | - | ||||||
16 | else if (pattern.length() > m_matchingPatternLength) { | - | ||||||
17 | - | |||||||
18 | replace = true; | - | ||||||
19 | } | - | ||||||
20 | } | - | ||||||
21 | if (replace) { | - | ||||||
22 | m_matchingMimeTypes.clear(); | - | ||||||
23 | - | |||||||
24 | m_matchingPatternLength = pattern.length(); | - | ||||||
25 | m_weight = weight; | - | ||||||
26 | } | - | ||||||
27 | if (!m_matchingMimeTypes.contains(mimeType)) { | - | ||||||
28 | m_matchingMimeTypes.append(mimeType); | - | ||||||
29 | if (pattern.startsWith(QLatin1String("*."))) | - | ||||||
30 | m_foundSuffix = pattern.mid(2); | - | ||||||
31 | } | - | ||||||
32 | } | - | ||||||
33 | bool QMimeGlobPattern::matchFileName(const QString &inputFilename) const | - | ||||||
34 | { | - | ||||||
35 | - | |||||||
36 | - | |||||||
37 | - | |||||||
38 | const QString filename = m_caseSensitivity == Qt::CaseInsensitive ? inputFilename.toLower() : inputFilename; | - | ||||||
39 | - | |||||||
40 | const int pattern_len = m_pattern.length(); | - | ||||||
41 | if (!pattern_len) | - | ||||||
42 | return false; | - | ||||||
43 | const int len = filename.length(); | - | ||||||
44 | - | |||||||
45 | const int starCount = m_pattern.count(QLatin1Char('*')); | - | ||||||
46 | - | |||||||
47 | - | |||||||
48 | if (m_pattern[0] == QLatin1Char('*') && m_pattern.indexOf(QLatin1Char('[')) == -1 && starCount == 1) | - | ||||||
49 | { | - | ||||||
50 | if (len + 1 < pattern_len) return false; | - | ||||||
51 | - | |||||||
52 | const QChar *c1 = m_pattern.unicode() + pattern_len - 1; | - | ||||||
53 | const QChar *c2 = filename.unicode() + len - 1; | - | ||||||
54 | int cnt = 1; | - | ||||||
55 | while (cnt < pattern_len && *c1-- == *c2--) | - | ||||||
56 | ++cnt; | - | ||||||
57 | return cnt == pattern_len; | - | ||||||
58 | } | - | ||||||
59 | - | |||||||
60 | - | |||||||
61 | if (starCount == 1 && m_pattern.at(pattern_len - 1) == QLatin1Char('*')) { | - | ||||||
62 | if (len + 1 < pattern_len) return false; | - | ||||||
63 | if (m_pattern.at(0) == QLatin1Char('*')) | - | ||||||
64 | return filename.indexOf(m_pattern.mid(1, pattern_len - 2)) != -1; | - | ||||||
65 | - | |||||||
66 | const QChar *c1 = m_pattern.unicode(); | - | ||||||
67 | const QChar *c2 = filename.unicode(); | - | ||||||
68 | int cnt = 1; | - | ||||||
69 | while (cnt < pattern_len && *c1++ == *c2++) | - | ||||||
70 | ++cnt; | - | ||||||
71 | return cnt == pattern_len; | - | ||||||
72 | } | - | ||||||
73 | - | |||||||
74 | - | |||||||
75 | if (m_pattern.indexOf(QLatin1Char('[')) == -1 && starCount == 0 && m_pattern.indexOf(QLatin1Char('?'))) | - | ||||||
76 | return (m_pattern == filename); | - | ||||||
77 | - | |||||||
78 | - | |||||||
79 | QRegExp rx(m_pattern, Qt::CaseSensitive, QRegExp::WildcardUnix); | - | ||||||
80 | return rx.exactMatch(filename); | - | ||||||
81 | } | - | ||||||
82 | - | |||||||
83 | static bool isFastPattern(const QString &pattern) | - | ||||||
84 | { | - | ||||||
85 | - | |||||||
86 | return pattern.lastIndexOf(QLatin1Char('*')) == 0 | - | ||||||
87 | && pattern.lastIndexOf(QLatin1Char('.')) == 1 | - | ||||||
88 | - | |||||||
89 | && !pattern.contains(QLatin1Char('?')) | - | ||||||
90 | && !pattern.contains(QLatin1Char('[')) | - | ||||||
91 | ; | - | ||||||
92 | } | - | ||||||
93 | - | |||||||
94 | void QMimeAllGlobPatterns::addGlob(const QMimeGlobPattern &glob) | - | ||||||
95 | { | - | ||||||
96 | const QString &pattern = glob.pattern(); | - | ||||||
97 | ((!(!pattern.isEmpty())) ? qt_assert("!pattern.isEmpty()",__FILE__,154160) : qt_noop()); | - | ||||||
98 | - | |||||||
99 | - | |||||||
100 | - | |||||||
101 | - | |||||||
102 | - | |||||||
103 | if (glob.weight() == 50 && isFastPattern(pattern) && !glob.isCaseSensitive()) { | - | ||||||
104 | - | |||||||
105 | const QString extension = pattern.mid(2).toLower(); | - | ||||||
106 | QStringList &patterns = m_fastPatterns[extension]; | - | ||||||
107 | if (!patterns.contains(glob.mimeType())) | - | ||||||
108 | patterns.append(glob.mimeType()); | - | ||||||
109 | } else { | - | ||||||
110 | if (glob.weight() > 50) { | - | ||||||
111 | if (!m_highWeightGlobs.hasPattern(glob.mimeType(), glob.pattern())) | - | ||||||
112 | m_highWeightGlobs.append(glob); | - | ||||||
113 | } else { | - | ||||||
114 | if (!m_lowWeightGlobs.hasPattern(glob.mimeType(), glob.pattern())) | - | ||||||
115 | m_lowWeightGlobs.append(glob); | - | ||||||
116 | } | - | ||||||
117 | } | - | ||||||
118 | } | - | ||||||
119 | - | |||||||
120 | void QMimeAllGlobPatterns::removeMimeType(const QString &mimeType) | - | ||||||
121 | { | - | ||||||
122 | QMutableHashIterator<QString, QStringList> itfor (auto &x : m_fastPatterns); | - | ||||||
while (it.hasNext()) { | ||||||||
it) | ||||||||
123 | x never executed: x.removeAll(mimeType); never executed: .next().value().removeAll(mimeType);x.removeAll(mimeType); never executed: x.removeAll(mimeType); | 0 | ||||||
124 | }m_highWeightGlobs.removeMimeType(mimeType); | - | ||||||
125 | m_lowWeightGlobs.removeMimeType(mimeType); | - | ||||||
126 | } never executed: end of block | 0 | ||||||
127 | - | |||||||
128 | void QMimeGlobPatternList::match(QMimeGlobMatchResult &result, | - | ||||||
129 | const QString &fileName) const | - | ||||||
130 | { | - | ||||||
131 | - | |||||||
132 | QMimeGlobPatternList::const_iterator it = this->constBegin(); | - | ||||||
133 | const QMimeGlobPatternList::const_iterator endIt = this->constEnd(); | - | ||||||
134 | for (; it != endIt; ++it) { | - | ||||||
135 | const QMimeGlobPattern &glob = *it; | - | ||||||
136 | if (glob.matchFileName(fileName)) | - | ||||||
137 | result.addMatch(glob.mimeType(), glob.weight(), glob.pattern()); | - | ||||||
138 | } | - | ||||||
139 | } | - | ||||||
140 | - | |||||||
141 | QStringList QMimeAllGlobPatterns::matchingGlobs(const QString &fileName, QString *foundSuffix) const | - | ||||||
142 | { | - | ||||||
143 | - | |||||||
144 | QMimeGlobMatchResult result; | - | ||||||
145 | m_highWeightGlobs.match(result, fileName); | - | ||||||
146 | if (result.m_matchingMimeTypes.isEmpty()
| 0-96 | ||||||
147 | - | |||||||
148 | - | |||||||
149 | - | |||||||
150 | const int lastDot = fileName.lastIndexOf(QLatin1Char('.')); | - | ||||||
151 | if (lastDot != -1
| 25-71 | ||||||
152 | const int ext_len = fileName.length() - lastDot - 1; | - | ||||||
153 | const QString simpleExtension = fileName.right(ext_len).toLower(); | - | ||||||
154 | - | |||||||
155 | - | |||||||
156 | const QStringList matchingMimeTypes = m_fastPatterns.value(simpleExtension); | - | ||||||
157 | for (QForeachContainer<typename QtPrivate::remove_reference<decltype(matchingMimeTypes)>::type> _container_((matchingMimeTypes)); _container_.control && _container_.i != _container_.e; ++_container_.i, _container_.control ^const QString simplePattern = 1QLatin1String("*.") + simpleExtension; | - | ||||||
158 | for (const QString &mime = *_container_.i; _container_.control; _container_.control = 0: matchingMimeTypes) | - | ||||||
159 | {result.addMatch(mime, 50, QLatin1String("*.") + simpleExtensionsimplePattern);} executed 57 times by 1 test: result.addMatch(mime, 50, simplePattern); Executed by:
| 57 | ||||||
160 | - | |||||||
161 | - | |||||||
162 | } executed 71 times by 1 test: end of block Executed by:
| 71 | ||||||
163 | - | |||||||
164 | - | |||||||
165 | m_lowWeightGlobs.match(result, fileName); | - | ||||||
166 | } executed 96 times by 1 test: end of block Executed by:
| 96 | ||||||
167 | if (foundSuffix
| 8-88 | ||||||
168 | * executed 8 times by 1 test: foundSuffix = result.m_foundSuffix;*foundSuffix = result.m_foundSuffix; Executed by:
executed 8 times by 1 test: *foundSuffix = result.m_foundSuffix; Executed by:
| 8 | ||||||
169 | return executed 96 times by 1 test: result.m_matchingMimeTypes;return result.m_matchingMimeTypes; Executed by:
executed 96 times by 1 test: return result.m_matchingMimeTypes; Executed by:
| 96 | ||||||
170 | } | - | ||||||
171 | - | |||||||
172 | void QMimeAllGlobPatterns::clear() | - | ||||||
173 | { | - | ||||||
174 | m_fastPatterns.clear(); | - | ||||||
175 | m_highWeightGlobs.clear(); | - | ||||||
176 | m_lowWeightGlobs.clear(); | - | ||||||
177 | } | - | ||||||
178 | - | |||||||
179 | - | |||||||
Switch to Source code | Preprocessed file |