1+ script "CoreStringFilter"
2+ /*
3+ Copyright (C) 2015 LiveCode Ltd.
4+
5+ This file is part of LiveCode.
6+
7+ LiveCode is free software; you can redistribute it and/or modify it under
8+ the terms of the GNU General Public License v3 as published by the Free
9+ Software Foundation.
10+
11+ LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
12+ WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+ for more details.
15+
16+ You should have received a copy of the GNU General Public License
17+ along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
18+
19+ /*
20+ Filter wildcard special characters:
21+ * - matches any number of occurrences of any character
22+ ? - matches a single occurrence of any character
23+ [abc] - matches any of a, b or c
24+ [a-m] - matches any lowercase character in the first half of the alphabet
25+ [!abc] - matches any character which is not a, b or c
26+
27+ The last three of these can be combined to create more complex character classes, for
28+ example [!abcA-C] - matches any character which is not a, b or c (upper or lower case)
29+ */
30+ on __testFilterRangeAndOptions pIsNative, pLower, pMid, pUpper
31+ local tTestType
32+ if pIsNative then
33+ put "native" into tTestType
34+ else
35+ put "non-native" into tTestType
36+ end if
37+
38+ local tSource , tPattern
39+
40+ # Option tests
41+ put pLower into tSource
42+ put "[" & pLower & pMid & "]" into tPattern
43+
44+ filter tSource with tPattern
45+ TestAssert "filter with matching option pattern" && tTestType , tSource is pLower
46+
47+ put pUpper into tSource
48+ filter tSource with tPattern
49+ TestAssert "filter with non-matching option pattern" && tTestType , tSource is empty
50+
51+ put "[!" & pLower & pMid & "]" into tPattern
52+ put pUpper into tSource
53+ filter tSource with tPattern
54+ TestAssert "filter with matching negated option pattern" && tTestType , tSource is pUpper
55+
56+ # Range tests
57+ put pMid into tSource
58+ put "[" & pLower & "-" & pUpper & "]" into tPattern
59+ filter tSource with tPattern
60+ TestAssert "filter with match within range pattern" && tTestType , tSource is pMid
61+
62+ put pLower into tSource
63+ put "[" & pLower & "-" & pMid & "]" into tPattern
64+ filter tSource with tPattern
65+ TestAssert "filter with match on range pattern lower bound" && tTestType , tSource is pLower
66+
67+ put pMid into tSource
68+ filter tSource with tPattern
69+ TestAssert "filter with match on range pattern upper bound" && tTestType , tSource is pMid
70+
71+ put pUpper into tSource
72+ filter tSource with tPattern
73+ TestDiagnostic tSource
74+ TestAssert "filter with non-match outside range pattern" && tTestType , tSource is empty
75+
76+ put pUpper into tSource
77+ put "[!" & pLower & "-" & pMid & "]" into tPattern
78+ filter tSource with tPattern
79+ TestAssert "filter with match outside negated range pattern" && tTestType , tSource is pUpper
80+
81+ # Range options
82+ put pUpper into tSource
83+ put "[" & pLower & "-" & pMid & pLower & "-" & pUpper & "]" into tPattern
84+ filter tSource with tPattern
85+ TestAssert "filter with match within option range pattern" && tTestType , tSource is pUpper
86+
87+ # Range + options
88+ put pUpper into tSource
89+ put "[" & pLower & "-" & pMid & pLower & pMid & pUpper & "]" into tPattern
90+ filter tSource with tPattern
91+ TestAssert "filter with matching option after range pattern" && tTestType , tSource is pUpper
92+ end __testFilterRangeAndOptions
93+
94+ on __testFilterWildcard pIsNative, pString
95+ if pString is empty then
96+ TestFail "filter wildcard string not valid"
97+ exit __testFilterWildcard
98+ end if
99+
100+ # Single character wildcard
101+ local tSource , tPattern
102+
103+ put "?" into tPattern
104+
105+ if pIsNative then
106+ put return into tSource
107+ filter tSource with tPattern
108+ TestAssert "filter single char wildcard doesn't match empty" , tSource is empty
109+ end if
110+
111+ local tTestType
112+ if pIsNative then
113+ put "native" into tTestType
114+ else
115+ put "non-native" into tTestType
116+ end if
117+
118+ local tChar
119+ put any char of pString into tChar
120+
121+ put tChar into tSource
122+ TestAssert "filter single char wildcard matches single char" && tTestType , tSource is tChar
123+
124+ put tChar & tChar into tSource
125+ filter tSource with tPattern
126+ TestAssert "filter single char wildcard doesn't match multiple chars" && tTestType , tSource is empty
127+
128+ put pString into tPattern
129+ put "?" into any char of tPattern
130+ put pString into tSource
131+ TestAssert "filter single char wildcard matches in string" && tTestType , tSource is pString
132+
133+ # Asterisk
134+ put "*" into tPattern
135+
136+ if pIsNative then
137+ put return into tSource
138+ filter tSource with tPattern
139+ TestAssert "filter asterisk matches empty" , tSource is return
140+ end if
141+
142+ put pString into tSource
143+ filter tSource with tPattern
144+ TestAssert "filter asterisk matches mulitple chars" && tTestType , tSource is pString
145+
146+ put pString & "**" into tPattern
147+ put pString into tSource
148+ filter tSource with tPattern
149+ TestAssert "filter asterisks at end don't affect match" && tTestType , tSource is pString
150+
151+ put "*" & pString into tPattern
152+ put pString into tSource
153+ filter tSource with tPattern
154+ TestAssert "filter simple asterisk recursion" && tTestType , tSource is pString
155+ end __testFilterWildcard
156+
157+ on TestFilterWildcardNative
158+ __testFilterRangeAndOptions true , "a" , "b" , "c"
159+ __testFilterWildcard true , "abc"
160+ end TestFilterWildcardNative
161+
162+ on TestFilterWildcardNonNative
163+ local tGClef , tFClef , tCClef
164+ put numToCodepoint (0x1d11e ) into tGClef
165+ put numToCodepoint (0x1d121 ) into tCClef
166+ put numToCodepoint (0x1d122 ) into tFClef
167+
168+ __testFilterRangeAndOptions false , tGClef , tCClef , tFClef
169+ __testFilterWildcard false , tGClef & tCClef & tFClef
170+ end TestFilterWildcardNonNative
171+
172+ on __testBug12644 pIsNative, pString
173+ local tTestType
174+ if pIsNative then
175+ put "native" into tTestType
176+ else
177+ put "non-native" into tTestType
178+ end if
179+
180+ local tSource , tPattern
181+
182+ put "*-U-*" into tPattern
183+ put "01-A-001" & pString into tSource
184+
185+ filter tSource with tPattern
186+ TestAssert "filter false positive bug 12644" , tSource is empty
187+ end __testBug12644
188+
189+ on TestBug12644
190+ __testBug12644 true , "word"
191+ __testBug12644 false , "뤻뤼뤾"
192+ end TestBug12644
193+
194+ on __testBug16584 pIsNative, pString
195+ local tTestType
196+ if pIsNative then
197+ put "native" into tTestType
198+ else
199+ put "non-native" into tTestType
200+ end if
201+
202+ local tSource , tPattern
203+
204+ put pString into tPattern
205+ put "*" into char 1 of tPattern
206+ put "*" into char 3 to 5 of tPattern
207+ put pString into tSource
208+
209+ filter tSource with tPattern
210+ TestAssert "filter asterisk false negative bug 16584" , tSource is pString
211+ end __testBug16584
212+
213+ on TestBug16584
214+ __testBug16584 true , "apple"
215+ __testBug16584 false , "뤻뤼뤼뤾룜"
216+ end TestBug16584
0 commit comments