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