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