author | Claus Gittinger <cg@exept.de> |
Sat, 02 May 2020 21:40:13 +0200 | |
changeset 5476 | 7355a4b11cb6 |
parent 4272 | ef02a1f2c6fb |
permissions | -rw-r--r-- |
4272 | 1 |
" |
2 |
COPYRIGHT (c) 2011 by eXept Software AG |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
2619 | 12 |
"{ Package: 'stx:libbasic2' }" |
13 |
||
3464
8d9675f053d4
Allow for space in patterns
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3449
diff
changeset
|
14 |
"{ NameSpace: Smalltalk }" |
8d9675f053d4
Allow for space in patterns
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3449
diff
changeset
|
15 |
|
2619 | 16 |
Object subclass:#StringPattern |
17 |
instanceVariableNames:'data' |
|
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Collections-Text-Support' |
|
21 |
! |
|
22 |
||
2672 | 23 |
StringPattern subclass:#Includes |
24 |
instanceVariableNames:'' |
|
25 |
classVariableNames:'' |
|
26 |
poolDictionaries:'' |
|
27 |
privateIn:StringPattern |
|
28 |
! |
|
29 |
||
30 |
StringPattern subclass:#Matches |
|
31 |
instanceVariableNames:'' |
|
32 |
classVariableNames:'' |
|
33 |
poolDictionaries:'' |
|
34 |
privateIn:StringPattern |
|
35 |
! |
|
36 |
||
2619 | 37 |
Object subclass:#Parser |
38 |
instanceVariableNames:'stream errorBlock' |
|
39 |
classVariableNames:'' |
|
40 |
poolDictionaries:'' |
|
41 |
privateIn:StringPattern |
|
42 |
! |
|
43 |
||
44 |
StringPattern subclass:#StartsWith |
|
45 |
instanceVariableNames:'' |
|
46 |
classVariableNames:'' |
|
47 |
poolDictionaries:'' |
|
48 |
privateIn:StringPattern |
|
49 |
! |
|
50 |
||
51 |
!StringPattern class methodsFor:'documentation'! |
|
52 |
||
4272 | 53 |
copyright |
54 |
" |
|
55 |
COPYRIGHT (c) 2011 by eXept Software AG |
|
56 |
All Rights Reserved |
|
57 |
||
58 |
This software is furnished under a license and may be used |
|
59 |
only in accordance with the terms of that license and with the |
|
60 |
inclusion of the above copyright notice. This software may not |
|
61 |
be provided or otherwise made available to, or used by, any |
|
62 |
other person. No title to or ownership of the software is |
|
63 |
hereby transferred. |
|
64 |
||
65 |
" |
|
66 |
! |
|
67 |
||
2619 | 68 |
documentation |
69 |
" |
|
70 |
A StringPattern instances are used to match string. The |
|
71 |
pattern is build from a user-supplied string. It is the |
|
3205 | 72 |
intention to let the user type the search pattern - it can |
73 |
be used for various live searches in lists, dialogs etc. |
|
2619 | 74 |
|
75 |
This is an unfinished class. More detailed description about |
|
76 |
syntax and algorithm will be added once the results will be |
|
3205 | 77 |
considered good and the API become stable. |
78 |
If not, blame it on JV :-) |
|
2619 | 79 |
|
80 |
[author:] |
|
81 |
Jan Vrany <jan.vrany@fit.cvut.cz> |
|
82 |
||
83 |
[instance variables:] |
|
84 |
||
85 |
[class variables:] |
|
86 |
||
87 |
[see also:] |
|
88 |
||
89 |
" |
|
90 |
! ! |
|
91 |
||
92 |
!StringPattern class methodsFor:'instance creation'! |
|
93 |
||
2672 | 94 |
includes: aString |
95 |
||
96 |
^Includes new on: aString |
|
97 |
||
98 |
"Created: / 18-10-2011 / 21:22:59 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
99 |
! |
|
100 |
||
101 |
matches: aString |
|
102 |
||
103 |
^Matches new on: aString |
|
104 |
||
105 |
"Created: / 18-10-2011 / 21:23:11 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
106 |
! |
|
107 |
||
2619 | 108 |
readFrom:aStringOrStream onError:exceptionBlock |
109 |
||
110 |
^Parser parse: aStringOrStream readStream onError: exceptionBlock |
|
111 |
||
2672 | 112 |
" |
113 |
StringPattern readFrom: 'abcd' |
|
114 |
StringPattern readFrom: ' abcd' |
|
115 |
StringPattern readFrom: ' a??d' |
|
116 |
StringPattern readFrom: ' a*d*' |
|
117 |
StringPattern readFrom: 'abcd*' |
|
118 |
" |
|
119 |
||
2619 | 120 |
"Created: / 09-08-2011 / 13:39:02 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
121 |
! |
|
122 |
||
123 |
startsWith: aString |
|
124 |
||
125 |
^StartsWith new on: aString |
|
126 |
||
127 |
"Created: / 09-08-2011 / 13:42:11 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
128 |
! ! |
|
129 |
||
4017 | 130 |
|
3468 | 131 |
!StringPattern methodsFor:'conversion'! |
132 |
||
133 |
asString |
|
134 |
"Returns a string representation of the pattern. |
|
135 |
||
136 |
`StringPattern fromString: pattern asString` should return |
|
137 |
the same pattern (same meaning equal)" |
|
138 |
||
139 |
^ self subclassResponsibility |
|
140 |
||
141 |
"Created: / 23-01-2015 / 17:36:56 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
142 |
! ! |
|
3249 | 143 |
|
2619 | 144 |
!StringPattern methodsFor:'initialization'! |
145 |
||
146 |
on: anObject |
|
147 |
||
148 |
data := anObject. |
|
149 |
||
150 |
"Created: / 09-08-2011 / 13:42:44 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
151 |
! ! |
|
152 |
||
153 |
!StringPattern methodsFor:'matching'! |
|
154 |
||
155 |
match: string |
|
3449
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
156 |
^self match: string from: 1 to: string size |
2619 | 157 |
|
158 |
"Created: / 09-08-2011 / 13:51:18 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
3449
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
159 |
"Modified: / 26-11-2014 / 07:00:30 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
160 |
! |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
161 |
|
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
162 |
match: string case: case |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
163 |
^self match: string case: case relax: 1 |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
164 |
|
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
165 |
"Created: / 26-11-2014 / 07:02:43 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
166 |
! |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
167 |
|
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
168 |
match: string case: case relax: relax |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
169 |
^self match: string from: 1 to: string size case: case relax: relax |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
170 |
|
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
171 |
"Created: / 26-11-2014 / 07:02:59 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
2619 | 172 |
! |
173 |
||
3449
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
174 |
match: string from: from to: to |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
175 |
^self match: string from: 1 to: string size case: true |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
176 |
|
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
177 |
"Created: / 26-11-2014 / 07:01:02 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
178 |
! |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
179 |
|
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
180 |
match: string from: from to: to case: case |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
181 |
^self match: string from: from to: to case: case relax: 1 |
2619 | 182 |
|
3449
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
183 |
"Created: / 26-11-2014 / 07:01:31 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
184 |
"Modified: / 26-11-2014 / 10:00:45 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
185 |
! |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
186 |
|
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
187 |
match: string from: from to: to case: case relax: relax |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
188 |
"Answers true if receiver matches the given `string` |
3524 | 189 |
portion starting at `from`, ending at `to`. |
3449
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
190 |
If `case` is true, then perform natch case sensitive, |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
191 |
otherwise match case insensitive. |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
192 |
|
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
193 |
`relax` argument say how much the matching should |
2619 | 194 |
be relaxed - relax is a number in <1..3>, where |
195 |
1 means no relaxing at all (aka exact match). |
|
196 |
All patterns should support relax == 1. If the relax |
|
197 |
level is not supported by a pattern, false must be |
|
198 |
returned." |
|
199 |
||
200 |
^self subclassResponsibility |
|
201 |
||
3449
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
202 |
"Created: / 26-11-2014 / 06:35:56 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
203 |
! |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
204 |
|
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
205 |
match: string from: from to: to relax: relax |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
206 |
^self match: string from: from to: string size case: true relax: relax |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
207 |
|
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
208 |
"Created: / 26-11-2014 / 07:01:58 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
209 |
! |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
210 |
|
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
211 |
match: string relax: relax |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
212 |
^ self match: string case: true relax: relax |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
213 |
|
2619 | 214 |
"Created: / 09-08-2011 / 13:47:12 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
3449
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
215 |
"Modified: / 26-11-2014 / 07:03:26 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
2619 | 216 |
! ! |
217 |
||
2672 | 218 |
!StringPattern methodsFor:'printing & storing'! |
219 |
||
220 |
printOn:aStream |
|
4017 | 221 |
"append a printed representation of the receiver to the argument, aStream" |
2672 | 222 |
|
223 |
super printOn:aStream. |
|
224 |
aStream nextPut:$(. |
|
225 |
data printOn: aStream. |
|
226 |
aStream nextPut:$). |
|
227 |
||
228 |
"Modified: / 18-10-2011 / 21:28:48 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
229 |
! ! |
|
230 |
||
2687 | 231 |
!StringPattern methodsFor:'utilities'! |
232 |
||
233 |
matchObject: anObject |
|
234 |
"Return true if pattern matches given object with relax level 1. |
|
235 |
||
3524 | 236 |
Some heuristics is performed to obtain the object's textual representation." |
2687 | 237 |
|
238 |
self matchObject: anObject relax: 1 |
|
239 |
||
240 |
"Created: / 29-11-2011 / 15:34:36 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
241 |
! |
|
242 |
||
243 |
matchObject: object relax: relax |
|
3524 | 244 |
"Return true if pattern matches given object. |
2687 | 245 |
|
3524 | 246 |
Some heuristics is performed to obtain the object's textual representation." |
2687 | 247 |
|
248 |
object isString ifTrue:[ |
|
249 |
^self match: object relax: relax. |
|
250 |
]. |
|
251 |
object isHierarchicalItem ifTrue:[ |
|
252 |
^self match: object label relax: relax. |
|
253 |
]. |
|
254 |
||
255 |
^self match: object displayString relax: relax. |
|
256 |
||
257 |
"Created: / 29-11-2011 / 15:37:46 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
258 |
! ! |
|
259 |
||
3468 | 260 |
!StringPattern::Includes methodsFor:'conversion'! |
261 |
||
262 |
asString |
|
263 |
"Returns a string representation of the pattern. |
|
264 |
||
265 |
`StringPattern fromString: pattern asString` should return |
|
266 |
the same pattern (same meaning equal)" |
|
267 |
||
268 |
^ ' ', data |
|
269 |
||
270 |
"Created: / 23-01-2015 / 17:40:07 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
271 |
! ! |
|
272 |
||
2672 | 273 |
!StringPattern::Includes methodsFor:'matching'! |
274 |
||
3449
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
275 |
match:string from: from to: to case: case relax:relax |
2672 | 276 |
|
277 |
relax == 1 ifTrue:[ |
|
3449
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
278 |
| i | |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
279 |
i := string indexOfSubCollection:data startingAt:from ifAbsent:0 caseSensitive:case. |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
280 |
^ i between: from and: to - data size + 1 |
2672 | 281 |
]. |
282 |
^false. |
|
283 |
||
3449
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
284 |
"Created: / 26-11-2014 / 06:40:45 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
2672 | 285 |
! ! |
286 |
||
3468 | 287 |
!StringPattern::Matches methodsFor:'conversion'! |
288 |
||
289 |
asString |
|
290 |
"Returns a string representation of the pattern. |
|
291 |
||
292 |
`StringPattern fromString: pattern asString` should return |
|
293 |
the same pattern (same meaning equal)" |
|
294 |
||
295 |
^ data |
|
296 |
||
297 |
"Created: / 23-01-2015 / 17:41:10 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
298 |
! ! |
|
299 |
||
2672 | 300 |
!StringPattern::Matches methodsFor:'matching'! |
301 |
||
3449
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
302 |
match:string from: from to: to case: case relax:relax |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
303 |
| pattern | |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
304 |
|
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
305 |
relax > 2 ifTrue:[ ^ false ]. |
2672 | 306 |
|
3449
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
307 |
pattern := data. |
2672 | 308 |
relax == 2 ifTrue:[ |
309 |
data first ~~ $* ifTrue:[ |
|
3449
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
310 |
pattern := '*' , pattern. |
2672 | 311 |
]. |
312 |
data last ~~ $* ifTrue:[ |
|
3449
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
313 |
pattern := pattern , '*'. |
2672 | 314 |
]. |
315 |
]. |
|
3449
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
316 |
^ pattern match: string from: from to: to caseSensitive: case. |
2672 | 317 |
|
3449
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
318 |
"Created: / 26-11-2014 / 06:43:44 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
2672 | 319 |
! ! |
320 |
||
2619 | 321 |
!StringPattern::Parser class methodsFor:'parsing'! |
322 |
||
323 |
parse: aStream onError: errorBlock |
|
324 |
||
325 |
^self new parse: aStream onError: errorBlock |
|
326 |
||
327 |
"Created: / 09-08-2011 / 13:39:44 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
328 |
! ! |
|
329 |
||
330 |
!StringPattern::Parser methodsFor:'parsing'! |
|
331 |
||
332 |
parse |
|
333 |
||
2672 | 334 |
"Sorry, no fancy AND/OR patterns now" |
2619 | 335 |
|
2672 | 336 |
^self parsePattern. |
2619 | 337 |
|
338 |
"Created: / 09-08-2011 / 13:41:25 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
339 |
! |
|
340 |
||
341 |
parse: aStream onError: aBlock |
|
342 |
||
343 |
stream := aStream. |
|
344 |
errorBlock := aBlock. |
|
345 |
^self parse. |
|
346 |
||
347 |
"Created: / 09-08-2011 / 13:40:21 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
2672 | 348 |
! |
349 |
||
350 |
parsePattern |
|
351 |
||
352 |
| data | |
|
353 |
||
3079 | 354 |
stream position == 0 ifTrue:[ |
2698
16c85264fef2
Fix (isWhiteSpace -> isSeparator)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2687
diff
changeset
|
355 |
(stream peek notNil and:[stream peek isSeparator]) ifFalse:[ |
3464
8d9675f053d4
Allow for space in patterns
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3449
diff
changeset
|
356 |
data := stream upToEnd."/upToSeparator. |
2672 | 357 |
(data includes: $*) ifTrue:[ |
358 |
^StringPattern matches: data |
|
359 |
]. |
|
360 |
(data includes: $?) ifTrue:[ |
|
361 |
^StringPattern matches: data |
|
362 |
]. |
|
363 |
^StringPattern startsWith: data |
|
364 |
]. |
|
365 |
]. |
|
366 |
||
367 |
stream skipSeparators. |
|
368 |
data := stream upToSeparator. |
|
369 |
(data includes: $*) ifTrue:[ |
|
370 |
^StringPattern matches: data |
|
371 |
]. |
|
372 |
(data includes: $?) ifTrue:[ |
|
373 |
^StringPattern matches: data |
|
374 |
]. |
|
375 |
^StringPattern includes: data |
|
376 |
||
377 |
"Created: / 18-10-2011 / 21:26:20 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
2698
16c85264fef2
Fix (isWhiteSpace -> isSeparator)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2687
diff
changeset
|
378 |
"Modified: / 21-12-2011 / 23:19:06 / jv" |
3464
8d9675f053d4
Allow for space in patterns
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3449
diff
changeset
|
379 |
"Modified (format): / 09-01-2015 / 13:09:23 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
2619 | 380 |
! ! |
381 |
||
3468 | 382 |
!StringPattern::StartsWith methodsFor:'conversion'! |
383 |
||
384 |
asString |
|
385 |
"Returns a string representation of the pattern. |
|
386 |
||
387 |
`StringPattern fromString: pattern asString` should return |
|
388 |
the same pattern (same meaning equal)" |
|
389 |
||
390 |
^ data |
|
391 |
||
392 |
"Created: / 23-01-2015 / 17:41:22 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
393 |
! ! |
|
394 |
||
2619 | 395 |
!StringPattern::StartsWith methodsFor:'matching'! |
396 |
||
3449
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
397 |
match: string from: from to: to case: case relax: relax |
2619 | 398 |
|
399 |
relax == 1 ifTrue:[ |
|
3449
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
400 |
(case and:[from == 1]) ifTrue:[ |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
401 |
^ string startsWith: data. |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
402 |
] ifFalse:[ |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
403 |
| stringSz dataSz | |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
404 |
|
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
405 |
stringSz := string size. |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
406 |
dataSz := data size. |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
407 |
stringSz >= (from + dataSz - 1) ifTrue:[ |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
408 |
| i | |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
409 |
|
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
410 |
i := dataSz. |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
411 |
case ifTrue:[ |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
412 |
[ i > 0 ] whileTrue:[ |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
413 |
(string at: from + i - 1) ~~ (data at: i) ifTrue:[ ^ false ]. |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
414 |
i := i - 1. |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
415 |
]. |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
416 |
] ifFalse:[ |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
417 |
[ i > 0 ] whileTrue:[ |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
418 |
(string at: from + i - 1) asLowercase ~~ (data at: i) asLowercase ifTrue:[ ^ false ]. |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
419 |
i := i - 1. |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
420 |
]. |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
421 |
]. |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
422 |
^ true |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
423 |
]. |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
424 |
^ false |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
425 |
]. |
2619 | 426 |
]. |
427 |
relax == 2 ifTrue:[ |
|
3449
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
428 |
| i | |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
429 |
i := string indexOfSubCollection:data startingAt:from ifAbsent:0 caseSensitive:case. |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
430 |
^ i between: from and: to - data size + 1 |
2619 | 431 |
]. |
432 |
relax == 3 ifTrue:[ |
|
3449
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
433 |
^ ('*', data, '*') match: string from: from to: to caseSensitive: case. |
2619 | 434 |
]. |
435 |
^false. |
|
436 |
||
437 |
" |
|
438 |
(StringPattern startsWith: 'String') match: 'StringPattern' |
|
439 |
(StringPattern startsWith: 'STring') match: 'StringPattern' relax: 2 |
|
3449
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
440 |
(StringPattern startsWith: 'Pattern') match: 'StringPattern' from: 7 to: 13 relax: 1 |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
441 |
(StringPattern startsWith: 'String') match: 'StringPattern' from: 7 to: 13 relax: 1 |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
442 |
(StringPattern startsWith: 'Pattern') match: 'StringPattern' relax: 2 |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
443 |
|
2619 | 444 |
" |
445 |
||
3449
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
446 |
"Created: / 26-11-2014 / 06:45:04 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
21c891242b9d
Added support for matching only a portion of a string and match case sensitive/insensitive.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3249
diff
changeset
|
447 |
"Modified: / 26-11-2014 / 09:58:41 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
2619 | 448 |
! ! |
449 |
||
450 |
!StringPattern class methodsFor:'documentation'! |
|
451 |
||
452 |
version |
|
4017 | 453 |
^ '$Header$' |
2619 | 454 |
! |
455 |
||
456 |
version_CVS |
|
4017 | 457 |
^ '$Header$' |
2619 | 458 |
! ! |
2912
2417be04fe10
Bugfix in StringPattern::Includes>>match:relax:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2698
diff
changeset
|
459 |