File: lc_filter.cpp

package info (click to toggle)
leocad 25.09-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,008 kB
  • sloc: cpp: 51,794; xml: 11,265; python: 81; sh: 52; makefile: 16
file content (288 lines) | stat: -rw-r--r-- 7,602 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include "lc_global.h"
#include "lc_filter.h"

lcFilter::lcFilter(const std::string_view FilterString)
{
	size_t TokenStart = std::string_view::npos;
	FilterOp Op = FilterOp::Or;
	bool InQuotes = false;

	auto EndToken=[this, FilterString, &Op, &TokenStart, &InQuotes](size_t Index)
	{
		if (!InQuotes && TokenStart != std::string_view::npos)
		{
			std::string_view Token = FilterString.substr(TokenStart, Index - TokenStart);

			if (Token == "AND")
				Op = FilterOp::And;
			else if (Token == "OR")
				Op = FilterOp::Or;
			else
			{
				mFilterParts.emplace_back(FilterPart{Op, Token.find_first_of("*?") != std::string_view::npos, std::string(Token)});
				Op = FilterOp::And;
			}

			TokenStart = std::string_view::npos;
		}
	};

	for (size_t Index = 0; Index < FilterString.size(); Index++)
	{
		switch (FilterString[Index])
		{
		case ' ':
			EndToken(Index);
			break;

		case '-':
			if (!InQuotes && TokenStart == std::string_view::npos)
			{
				if (Op == FilterOp::And)
					Op = FilterOp::AndNot;
				else if (Op == FilterOp::Or)
					Op = FilterOp::OrNot;
			}
			break;

		case '\"':
			if (InQuotes)
			{
				std::string_view Token = FilterString.substr(TokenStart + 1, Index - TokenStart - 1);

				if (!Token.empty())
					mFilterParts.emplace_back(FilterPart{Op, Token.find_first_of("*?") != std::string_view::npos, std::string(Token)});

				TokenStart = std::string_view::npos;
				Op = FilterOp::And;
			}
			else
			{
				if (TokenStart == std::string_view::npos)
					TokenStart = Index;
			}
			InQuotes = !InQuotes;
			break;

		default:
			if (TokenStart == std::string_view::npos)
				TokenStart = Index;
			break;
		}
	}

	EndToken(FilterString.size());
}

bool lcFilter::Match(const char* String) const
{
	bool CurrentMatch = mFilterParts.empty();

	for (const FilterPart& FilterPart : mFilterParts)
	{
		bool Match = FilterPart.Wildcard ? FastWildCompare(String, FilterPart.String.c_str()) : strcasestr(String, FilterPart.String.c_str()) != nullptr;

		switch (FilterPart.Op)
		{
		case FilterOp::And:
			CurrentMatch &= Match;
			break;

		case FilterOp::AndNot:
			CurrentMatch &= !Match;
			break;

		case FilterOp::Or:
			CurrentMatch |= Match;
			break;

		case FilterOp::OrNot:
			CurrentMatch |= !Match;
			break;
		}
	}

	return CurrentMatch;
}

// Copyright 2018 IBM Corporation
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Compares two text strings.  Accepts '?' as a single-character wildcard.  
// For each '*' wildcard, seeks out a matching sequence of any characters 
// beyond it.  Otherwise compares the strings a character at a time. 
//
bool lcFilter::FastWildCompare(const char* Tame, const char* Wild)
{
    const char* WildSequence;  // Points to prospective wild string match after '*'
    const char* TameSequence;  // Points to prospective tame string match
 
	auto NotEquals=[](char a, char b)
	{
			 // Lowercase the characters to be compared.
                if (a >= 'A' && a <= 'Z')
                    a += ('a' - 'A');
 
                if (b >= 'A' && b <= 'Z')
                    b += ('a' - 'A');

		return a != b;
	};

    // Find a first wildcard, if one exists, and the beginning of any  
    // prospectively matching sequence after it.
    do
    {
        // Check for the end from the start.  Get out fast, if possible.
        if (!*Tame)
        {
            if (*Wild)
            {
                while (*(Wild++) == '*')
                {
                    if (!(*Wild))
                    {
                        return true;   // "ab" matches "ab*".
                    }
                }
 
                return false;          // "abcd" doesn't match "abc".
            }
            else
            {
                return true;           // "abc" matches "abc".
            }
        }
        else if (*Wild == '*')
        {
            // Got wild: set up for the second loop and skip on down there.
            while (*(++Wild) == '*')
            {
                continue;
            }
 
            if (!*Wild)
            {
                return true;           // "abc*" matches "abcd".
            }
 
            // Search for the next prospective match.
            if (*Wild != '?')
            {
                while (NotEquals(*Wild, *Tame))
                {
                    if (!*(++Tame))
                    {
                        return false;  // "a*bc" doesn't match "ab".
                    }
                }
            }
 
            // Keep fallback positions for retry in case of incomplete match.
            WildSequence = Wild;
            TameSequence = Tame;
            break;
        }
        else if (NotEquals(*Wild, *Tame) && *Wild != '?')
        {
            return false;              // "abc" doesn't match "abd".
        }
 
        ++Wild;                       // Everything's a match, so far.
        ++Tame;
    } while (true);
 
    // Find any further wildcards and any further matching sequences.
    do
    {
        if (*Wild == '*')
        {
            // Got wild again.
            while (*(++Wild) == '*')
            {
                continue;
            }
 
            if (!*Wild)
            {
                return true;           // "ab*c*" matches "abcd".
            }
 
            if (!*Tame)
            {
                return false;          // "*bcd*" doesn't match "abc".
            }
 
            // Search for the next prospective match.
            if (*Wild != '?')
            {
                while (NotEquals(*Wild, *Tame))
                {
                    if (!*(++Tame))
                    {
                        return false;  // "a*b*c" doesn't match "ab".
                    }
                }
            }
 
            // Keep the new fallback positions.
            WildSequence = Wild;
            TameSequence = Tame;
        }
        else if (NotEquals(*Wild, *Tame) && *Wild != '?')
        {
            // The equivalent portion of the upper loop is really simple.
            if (!*Tame)
            {
                return false;          // "*bcd" doesn't match "abc".
            }
 
            // A fine time for questions.
            while (*WildSequence == '?')
            {
                ++WildSequence;
                ++TameSequence;
            }
 
            Wild = WildSequence;
 
            // Fall back, but never so far again.
            while (NotEquals(*Wild, *(++TameSequence)))
            {
                if (!*TameSequence)
                {
                    return false;      // "*a*b" doesn't match "ac".
                }
            }
 
            Tame = TameSequence;
        }
 
        // Another check for the end, at the end.
        if (!*Tame)
        {
            if (!*Wild)
            {
                return true;           // "*bc" matches "abc".
            }
            else
            {
                return false;          // "*bc" doesn't match "abcd".
            }
        }
 
        ++Wild;                       // Everything's still a match.
        ++Tame;
    } while (true);
}