author | Claus Gittinger <cg@exept.de> |
Wed, 05 Mar 2014 16:13:46 +0100 | |
changeset 3205 | 154e2d2fcb71 |
parent 3079 | a419a95f3e3f |
child 3249 | 16699e7a3c08 |
permissions | -rw-r--r-- |
2619 | 1 |
"{ Package: 'stx:libbasic2' }" |
2 |
||
3 |
Object subclass:#StringPattern |
|
4 |
instanceVariableNames:'data' |
|
5 |
classVariableNames:'' |
|
6 |
poolDictionaries:'' |
|
7 |
category:'Collections-Text-Support' |
|
8 |
! |
|
9 |
||
2672 | 10 |
StringPattern subclass:#Includes |
11 |
instanceVariableNames:'' |
|
12 |
classVariableNames:'' |
|
13 |
poolDictionaries:'' |
|
14 |
privateIn:StringPattern |
|
15 |
! |
|
16 |
||
17 |
StringPattern subclass:#Matches |
|
18 |
instanceVariableNames:'' |
|
19 |
classVariableNames:'' |
|
20 |
poolDictionaries:'' |
|
21 |
privateIn:StringPattern |
|
22 |
! |
|
23 |
||
2619 | 24 |
Object subclass:#Parser |
25 |
instanceVariableNames:'stream errorBlock' |
|
26 |
classVariableNames:'' |
|
27 |
poolDictionaries:'' |
|
28 |
privateIn:StringPattern |
|
29 |
! |
|
30 |
||
31 |
StringPattern subclass:#StartsWith |
|
32 |
instanceVariableNames:'' |
|
33 |
classVariableNames:'' |
|
34 |
poolDictionaries:'' |
|
35 |
privateIn:StringPattern |
|
36 |
! |
|
37 |
||
38 |
!StringPattern class methodsFor:'documentation'! |
|
39 |
||
40 |
documentation |
|
41 |
" |
|
42 |
A StringPattern instances are used to match string. The |
|
43 |
pattern is build from a user-supplied string. It is the |
|
3205 | 44 |
intention to let the user type the search pattern - it can |
45 |
be used for various live searches in lists, dialogs etc. |
|
2619 | 46 |
|
47 |
This is an unfinished class. More detailed description about |
|
48 |
syntax and algorithm will be added once the results will be |
|
3205 | 49 |
considered good and the API become stable. |
50 |
If not, blame it on JV :-) |
|
2619 | 51 |
|
52 |
[author:] |
|
53 |
Jan Vrany <jan.vrany@fit.cvut.cz> |
|
54 |
||
55 |
[instance variables:] |
|
56 |
||
57 |
[class variables:] |
|
58 |
||
59 |
[see also:] |
|
60 |
||
61 |
" |
|
62 |
! ! |
|
63 |
||
64 |
!StringPattern class methodsFor:'instance creation'! |
|
65 |
||
2672 | 66 |
includes: aString |
67 |
||
68 |
^Includes new on: aString |
|
69 |
||
70 |
"Created: / 18-10-2011 / 21:22:59 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
71 |
! |
|
72 |
||
73 |
matches: aString |
|
74 |
||
75 |
^Matches new on: aString |
|
76 |
||
77 |
"Created: / 18-10-2011 / 21:23:11 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
78 |
! |
|
79 |
||
2619 | 80 |
readFrom:aStringOrStream onError:exceptionBlock |
81 |
||
82 |
^Parser parse: aStringOrStream readStream onError: exceptionBlock |
|
83 |
||
2672 | 84 |
" |
85 |
StringPattern readFrom: 'abcd' |
|
86 |
StringPattern readFrom: ' abcd' |
|
87 |
StringPattern readFrom: ' a??d' |
|
88 |
StringPattern readFrom: ' a*d*' |
|
89 |
StringPattern readFrom: 'abcd*' |
|
90 |
" |
|
91 |
||
2619 | 92 |
"Created: / 09-08-2011 / 13:39:02 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
93 |
! |
|
94 |
||
95 |
startsWith: aString |
|
96 |
||
97 |
^StartsWith new on: aString |
|
98 |
||
99 |
"Created: / 09-08-2011 / 13:42:11 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
100 |
! ! |
|
101 |
||
102 |
!StringPattern methodsFor:'initialization'! |
|
103 |
||
104 |
on: anObject |
|
105 |
||
106 |
data := anObject. |
|
107 |
||
108 |
"Created: / 09-08-2011 / 13:42:44 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
109 |
! ! |
|
110 |
||
111 |
!StringPattern methodsFor:'matching'! |
|
112 |
||
113 |
match: string |
|
114 |
||
115 |
"Answers true if myself match the given string. |
|
116 |
No relaxing done" |
|
117 |
||
118 |
^self match: string relax: 1. |
|
119 |
||
120 |
"Created: / 09-08-2011 / 13:51:18 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
121 |
! |
|
122 |
||
123 |
match: string relax: relax |
|
124 |
||
125 |
"Answers true if myself match the given string. |
|
126 |
relax argument say how much the matching should |
|
127 |
be relaxed - relax is a number in <1..3>, where |
|
128 |
1 means no relaxing at all (aka exact match). |
|
129 |
All patterns should support relax == 1. If the relax |
|
130 |
level is not supported by a pattern, false must be |
|
131 |
returned." |
|
132 |
||
133 |
^self subclassResponsibility |
|
134 |
||
135 |
"Created: / 09-08-2011 / 13:47:12 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
136 |
! ! |
|
137 |
||
2672 | 138 |
!StringPattern methodsFor:'printing & storing'! |
139 |
||
140 |
printOn:aStream |
|
141 |
"append a printed representation if the receiver to the argument, aStream" |
|
142 |
||
143 |
super printOn:aStream. |
|
144 |
aStream nextPut:$(. |
|
145 |
data printOn: aStream. |
|
146 |
aStream nextPut:$). |
|
147 |
||
148 |
"Modified: / 18-10-2011 / 21:28:48 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
149 |
! ! |
|
150 |
||
2687 | 151 |
!StringPattern methodsFor:'utilities'! |
152 |
||
153 |
matchObject: anObject |
|
154 |
"Return true if pattern matches given object with relax level 1. |
|
155 |
||
156 |
Some heuristics is performed to obtain objects textual |
|
157 |
representation." |
|
158 |
||
159 |
self matchObject: anObject relax: 1 |
|
160 |
||
161 |
"Created: / 29-11-2011 / 15:34:36 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
162 |
! |
|
163 |
||
164 |
matchObject: object relax: relax |
|
165 |
"Return true if pattern matches given object.. |
|
166 |
||
167 |
Some heuristics is performed to obtain objects textual |
|
168 |
representation." |
|
169 |
||
170 |
object isString ifTrue:[ |
|
171 |
^self match: object relax: relax. |
|
172 |
]. |
|
173 |
object isHierarchicalItem ifTrue:[ |
|
174 |
^self match: object label relax: relax. |
|
175 |
]. |
|
176 |
||
177 |
^self match: object displayString relax: relax. |
|
178 |
||
179 |
"Created: / 29-11-2011 / 15:37:46 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
180 |
! ! |
|
181 |
||
2672 | 182 |
!StringPattern::Includes methodsFor:'matching'! |
183 |
||
184 |
match:string relax:relax |
|
185 |
||
186 |
relax == 1 ifTrue:[ |
|
2912
2417be04fe10
Bugfix in StringPattern::Includes>>match:relax:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2698
diff
changeset
|
187 |
^string includesString: data |
2672 | 188 |
]. |
189 |
relax == 2 ifTrue:[ |
|
190 |
^string matches: ('*', data, '*') |
|
191 |
]. |
|
192 |
^false. |
|
193 |
||
2912
2417be04fe10
Bugfix in StringPattern::Includes>>match:relax:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2698
diff
changeset
|
194 |
"Modified: / 11-03-2013 / 14:11:20 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
2672 | 195 |
! ! |
196 |
||
197 |
!StringPattern::Matches methodsFor:'matching'! |
|
198 |
||
199 |
match:string relax:relax |
|
200 |
||
201 |
relax == 1 ifTrue:[ |
|
202 |
^string matches: data |
|
203 |
]. |
|
204 |
relax == 2 ifTrue:[ |
|
205 |
| relaxed | |
|
206 |
relaxed := data. |
|
207 |
data first ~~ $* ifTrue:[ |
|
208 |
relaxed := '*' , relaxed. |
|
209 |
]. |
|
210 |
data last ~~ $* ifTrue:[ |
|
211 |
relaxed := relaxed , '*'. |
|
212 |
]. |
|
213 |
^string matches: relaxed |
|
214 |
]. |
|
215 |
^false. |
|
216 |
||
217 |
"Modified: / 18-10-2011 / 21:35:24 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
218 |
! ! |
|
219 |
||
2619 | 220 |
!StringPattern::Parser class methodsFor:'parsing'! |
221 |
||
222 |
parse: aStream onError: errorBlock |
|
223 |
||
224 |
^self new parse: aStream onError: errorBlock |
|
225 |
||
226 |
"Created: / 09-08-2011 / 13:39:44 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
227 |
! ! |
|
228 |
||
229 |
!StringPattern::Parser methodsFor:'parsing'! |
|
230 |
||
231 |
parse |
|
232 |
||
2672 | 233 |
"Sorry, no fancy AND/OR patterns now" |
2619 | 234 |
|
2672 | 235 |
^self parsePattern. |
2619 | 236 |
|
237 |
"Created: / 09-08-2011 / 13:41:25 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
238 |
! |
|
239 |
||
240 |
parse: aStream onError: aBlock |
|
241 |
||
242 |
stream := aStream. |
|
243 |
errorBlock := aBlock. |
|
244 |
^self parse. |
|
245 |
||
246 |
"Created: / 09-08-2011 / 13:40:21 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
2672 | 247 |
! |
248 |
||
249 |
parsePattern |
|
250 |
||
251 |
| data | |
|
252 |
||
3079 | 253 |
stream position == 0 ifTrue:[ |
2698
16c85264fef2
Fix (isWhiteSpace -> isSeparator)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2687
diff
changeset
|
254 |
(stream peek notNil and:[stream peek isSeparator]) ifFalse:[ |
2672 | 255 |
data := stream upToSeparator. |
256 |
(data includes: $*) ifTrue:[ |
|
257 |
^StringPattern matches: data |
|
258 |
]. |
|
259 |
(data includes: $?) ifTrue:[ |
|
260 |
^StringPattern matches: data |
|
261 |
]. |
|
262 |
^StringPattern startsWith: data |
|
263 |
]. |
|
264 |
]. |
|
265 |
||
266 |
stream skipSeparators. |
|
267 |
data := stream upToSeparator. |
|
268 |
(data includes: $*) ifTrue:[ |
|
269 |
^StringPattern matches: data |
|
270 |
]. |
|
271 |
(data includes: $?) ifTrue:[ |
|
272 |
^StringPattern matches: data |
|
273 |
]. |
|
274 |
^StringPattern includes: data |
|
275 |
||
276 |
"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
|
277 |
"Modified: / 21-12-2011 / 23:19:06 / jv" |
2619 | 278 |
! ! |
279 |
||
280 |
!StringPattern::StartsWith methodsFor:'matching'! |
|
281 |
||
282 |
match: string relax: relax |
|
283 |
||
284 |
relax == 1 ifTrue:[ |
|
285 |
^string startsWith: data. |
|
286 |
]. |
|
287 |
relax == 2 ifTrue:[ |
|
2672 | 288 |
^string includes: data |
2619 | 289 |
]. |
290 |
relax == 3 ifTrue:[ |
|
2672 | 291 |
^string matches: ('*', data, '*') |
2619 | 292 |
]. |
293 |
^false. |
|
294 |
||
295 |
" |
|
296 |
(StringPattern startsWith: 'String') match: 'StringPattern' |
|
297 |
(StringPattern startsWith: 'STring') match: 'StringPattern' relax: 2 |
|
298 |
" |
|
299 |
||
300 |
"Created: / 09-08-2011 / 13:50:48 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
301 |
! ! |
|
302 |
||
303 |
!StringPattern class methodsFor:'documentation'! |
|
304 |
||
305 |
version |
|
3205 | 306 |
^ '$Header: /cvs/stx/stx/libbasic2/StringPattern.st,v 1.7 2014-03-05 15:13:46 cg Exp $' |
2619 | 307 |
! |
308 |
||
309 |
version_CVS |
|
3205 | 310 |
^ '$Header: /cvs/stx/stx/libbasic2/StringPattern.st,v 1.7 2014-03-05 15:13:46 cg Exp $' |
2619 | 311 |
! ! |
2912
2417be04fe10
Bugfix in StringPattern::Includes>>match:relax:
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
2698
diff
changeset
|
312 |