author | Claus Gittinger <cg@exept.de> |
Sat, 07 Nov 2009 11:15:14 +0100 | |
changeset 2337 | 755696d72648 |
parent 2303 | 74c77b8cf160 |
child 2364 | 2b7b611e565c |
permissions | -rw-r--r-- |
2337
755696d72648
comment/format in: #asSoundexCode
Claus Gittinger <cg@exept.de>
parents:
2303
diff
changeset
|
1 |
"{ Package: 'stx:libbasic2' }"! |
1668 | 2 |
|
2288 | 3 |
!CharacterArray methodsFor:'matching - phonetic'! |
2199
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
4 |
|
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
5 |
asKoelnerPhoneticCode |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
6 |
"return a koelner phonetic code. |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
7 |
The koelnerPhonetic code is for the german language what the soundex code is for english; |
2303 | 8 |
it returns simular strings for similar sounding words. |
9 |
There are some differences to soundex, though: |
|
10 |
its length is not limited to 4, but depends on the length of the original string; |
|
11 |
it does not start with the first character of the input. |
|
2288 | 12 |
|
13 |
Caveat: this phonetic code is especially suited for german words. |
|
2303 | 14 |
Please have a look at the other phonetic comparison operators found |
15 |
in PhoneticStringUtilities." |
|
2199
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
16 |
|
2303 | 17 |
^ PhoneticStringUtilities koelnerPhoneticCodeOf:self |
2199
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
18 |
|
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
19 |
" |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
20 |
#( |
2303 | 21 |
'Müller' |
22 |
'Miller' |
|
23 |
'Mueller' |
|
24 |
'Mühler' |
|
25 |
'Mühlherr' |
|
26 |
'Mülherr' |
|
27 |
'Myler' |
|
28 |
'Millar' |
|
29 |
'Myller' |
|
30 |
'Müllar' |
|
31 |
'Müler' |
|
32 |
'Muehler' |
|
33 |
'Mülller' |
|
34 |
'Müllerr' |
|
35 |
'Muehlherr' |
|
36 |
'Muellar' |
|
37 |
'Mueler' |
|
38 |
'Mülleer' |
|
39 |
'Mueller' |
|
40 |
'Nüller' |
|
41 |
'Nyller' |
|
42 |
'Niler' |
|
43 |
'Czerny' |
|
44 |
'Tscherny' |
|
45 |
'Czernie' |
|
46 |
'Tschernie' |
|
47 |
'Schernie' |
|
48 |
'Scherny' |
|
49 |
'Scherno' |
|
50 |
'Czerne' |
|
51 |
'Zerny' |
|
52 |
'Tzernie' |
|
53 |
'Breschnew' |
|
2199
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
54 |
) do:[:w | |
2303 | 55 |
Transcript show:w; show:'->'; showCR:(w asKoelnerPhoneticCode) |
2199
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
56 |
]. |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
57 |
" |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
58 |
|
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
59 |
" |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
60 |
'Breschnew' asKoelnerPhoneticCode -> '17863' |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
61 |
'Breschnew' asKoelnerPhoneticCode -> '17863' |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
62 |
'Breschneff' asKoelnerPhoneticCode -> '17863' |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
63 |
'Braeschneff' asKoelnerPhoneticCode -> '17863' |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
64 |
'Braessneff' asKoelnerPhoneticCode -> '17863' |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
65 |
'Pressneff' asKoelnerPhoneticCode -> '17863' |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
66 |
'Presznäph' asKoelnerPhoneticCode -> '17863' |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
67 |
" |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
68 |
! ! |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
69 |
|
2288 | 70 |
!CharacterArray methodsFor:'matching - phonetic'! |
2199
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
71 |
|
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
72 |
asSoundexCode |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
73 |
"return a soundex phonetic code or nil. |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
74 |
Soundex returns similar codes for similar sounding words, making it a useful |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
75 |
tool when searching for words where the correct spelling is unknown. |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
76 |
(read Knuth or search the web if you dont know what a soundex code is). |
2288 | 77 |
|
2337
755696d72648
comment/format in: #asSoundexCode
Claus Gittinger <cg@exept.de>
parents:
2303
diff
changeset
|
78 |
Caveat: 'similar sounding words' means: 'similar sounding in ENGLISH' |
755696d72648
comment/format in: #asSoundexCode
Claus Gittinger <cg@exept.de>
parents:
2303
diff
changeset
|
79 |
Please have a look at the other phonetic comparison operators found |
755696d72648
comment/format in: #asSoundexCode
Claus Gittinger <cg@exept.de>
parents:
2303
diff
changeset
|
80 |
in PhoneticStringUtilities." |
2199
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
81 |
|
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
82 |
^ PhoneticStringUtilities soundexCodeOf:self |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
83 |
|
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
84 |
" |
2303 | 85 |
'claus' asSoundexCode |
2199
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
86 |
'clause' asSoundexCode |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
87 |
'close' asSoundexCode |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
88 |
'smalltalk' asSoundexCode |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
89 |
'smaltalk' asSoundexCode |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
90 |
'smaltak' asSoundexCode |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
91 |
'smaltok' asSoundexCode |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
92 |
'smoltok' asSoundexCode |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
93 |
'aa' asSoundexCode |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
94 |
'by' asSoundexCode |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
95 |
'bab' asSoundexCode |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
96 |
'bob' asSoundexCode |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
97 |
'bop' asSoundexCode |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
98 |
" |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
99 |
! ! |
a9d3b1aa9f03
phonetic stuff extraced into libbasic2
Claus Gittinger <cg@exept.de>
parents:
2150
diff
changeset
|
100 |
|
2241 | 101 |
!CharacterArray methodsFor:'printing & storing'! |
102 |
||
2303 | 103 |
printf:args |
104 |
"Format and print the receiver with <args> formatted in C style, |
|
2295 | 105 |
as specified in the Unix C-language manual page for printf(3). |
106 |
Return the resulting string. |
|
2241 | 107 |
|
108 |
For copyright information, see goodies/String-printf_scanf.chg" |
|
2303 | 109 |
|
2241 | 110 |
|aStream| |
111 |
||
112 |
aStream := WriteStream on:String new. |
|
2260 | 113 |
self printf:args on:aStream. |
2241 | 114 |
^ aStream contents |
115 |
||
116 |
" |
|
2303 | 117 |
Transcript showCR:('%05x %d %f %o' printf:{ 123. 234*5. 1.234. 8r377 } ) |
2256 | 118 |
" |
119 |
||
120 |
" |
|
2241 | 121 |
Transcript showCR: 'Some examples:'!! |
122 |
||
123 |
Transcript show:'''%#x %#X %03o%*.*s'' printf: #(16rABCD 16rEF 5 9 5 ''ghijklmn'') = .'. |
|
2303 | 124 |
Transcript show: ('%#x %#X %03o%*.*s' printf: #(16rABCD 16rEF 5 9 5 'ghijklmn')). |
2241 | 125 |
Transcript showCR: '.' |
126 |
||
127 |
Transcript show: '''%- 10.4s%.2e'' printf: (Array with: ''abcdefghijkl'' with: Float pi) = .'. |
|
2303 | 128 |
Transcript show: ('%- 10.4s%.2e' printf: (Array with: 'abcdefghijkl' with: Float pi)). |
2241 | 129 |
Transcript showCR: '.' |
130 |
||
131 |
Transcript show: '''%8.3f'' printf: (Array with: 200 sqrt negated) = .'. |
|
132 |
Transcript show: ('%8.3f' printf: (Array with: 200 sqrt negated)). |
|
133 |
Transcript showCR: '.' |
|
134 |
||
135 |
Transcript show: '''%c'' printf: #(16r41) = .'. |
|
136 |
Transcript show: ('%c' printf: #(16r41)). |
|
137 |
Transcript showCR: '.' |
|
138 |
||
139 |
Transcript show: '''%f%2s%s%s%s'' sscanf: ''237.0 this is a test'' = '. |
|
140 |
Transcript showCR: ('%f%2s%s%s%s' sscanf: '237.0 this is a test') printString. |
|
141 |
||
142 |
Transcript show: '''%d%f%s'' sscanf: ''25 54.32e-01 monday'' = '. |
|
143 |
Transcript showCR: ('%d%f%s' sscanf: '25 54.32e-01 monday') printString. |
|
144 |
||
145 |
Transcript show: '''%f%*f %8[A-F0-9]%c%d 0x%x%f'' sscanf: ''12.45 1048.73 AE40Z527 0x75BCD15 34'' = '. |
|
146 |
Transcript showCR: ('%f%*f %8[A-F0-9]%c%d 0x%x%f' sscanf: '12.45 1048.73 AE40Z527 0x75BCD15 34') printString. |
|
147 |
" |
|
148 |
! ! |
|
149 |
||
150 |
!CharacterArray methodsFor:'printing & storing'! |
|
151 |
||
2260 | 152 |
printf:args on:outStream |
153 |
"Format and print the receiver on <outStream> with <args> |
|
154 |
formatted in C style, as specified in the Unix C-language manual page for printf(3). |
|
155 |
||
156 |
For copyright information, see goodies/String-printf_scanf.chg" |
|
2303 | 157 |
|
2260 | 158 |
|argStream inStream char| |
159 |
||
160 |
argStream := ReadStream on:args. |
|
161 |
inStream := ReadStream on:self. |
|
162 |
[ inStream atEnd ] whileFalse:[ |
|
2303 | 163 |
(char := inStream next) == $% ifFalse:[ |
164 |
outStream nextPut:char |
|
165 |
] ifTrue:[ |
|
166 |
self |
|
167 |
printf_printArgFrom:inStream |
|
168 |
to:outStream |
|
169 |
withData:argStream |
|
170 |
] |
|
2260 | 171 |
] |
172 |
! ! |
|
173 |
||
174 |
!CharacterArray methodsFor:'printing & storing'! |
|
175 |
||
2303 | 176 |
printfWith:arg1 |
2295 | 177 |
"Format and print the receiver with <arg1> formatted in C style, |
178 |
as specified in the Unix C-language manual page for printf(3). |
|
179 |
Return the resulting string." |
|
2303 | 180 |
|
2256 | 181 |
^ self printf:(Array with:arg1) |
182 |
||
183 |
" |
|
2303 | 184 |
Transcript showCR:('%05x' printfWith:123) |
2256 | 185 |
" |
186 |
! ! |
|
187 |
||
188 |
!CharacterArray methodsFor:'printing & storing'! |
|
189 |
||
2303 | 190 |
printfWith:arg1 with:arg2 |
191 |
"Format and print the receiver with <argI> formatted in C style, |
|
2295 | 192 |
as specified in the Unix C-language manual page for printf(3). |
193 |
Return the resulting string." |
|
2303 | 194 |
|
2256 | 195 |
^ self printf:(Array with:arg1 with:arg2) |
196 |
||
197 |
" |
|
2303 | 198 |
Transcript showCR:('%d %05x' printfWith:123 with:234) |
2256 | 199 |
" |
200 |
! ! |
|
201 |
||
202 |
!CharacterArray methodsFor:'printing & storing'! |
|
203 |
||
204 |
printfWith:arg1 with:arg2 with:arg3 |
|
2303 | 205 |
"Format and print the receiver with <argI> formatted in C style, |
2295 | 206 |
as specified in the Unix C-language manual page for printf(3). |
207 |
Return the resulting string." |
|
2303 | 208 |
|
2256 | 209 |
^ self printf:(Array with:arg1 with:arg2 with:arg3) |
210 |
||
211 |
" |
|
2303 | 212 |
Transcript showCR:('%d %05x %08o' printfWith:123 with:234 with:345) |
2256 | 213 |
" |
214 |
! ! |
|
215 |
||
216 |
!CharacterArray methodsFor:'printing & storing'! |
|
217 |
||
218 |
printfWith:arg1 with:arg2 with:arg3 with:arg4 |
|
2303 | 219 |
"Format and print the receiver with <argI> formatted in C style, |
2295 | 220 |
as specified in the Unix C-language manual page for printf(3). |
221 |
Return the resulting string." |
|
2303 | 222 |
|
2256 | 223 |
^ self printf:(Array with:arg1 with:arg2 with:arg3 with:arg4) |
224 |
||
225 |
" |
|
2303 | 226 |
Transcript showCR:('%d %05x %08o %b' printfWith:123 with:234 with:345 with:123) |
2256 | 227 |
" |
228 |
! ! |
|
229 |
||
230 |
!CharacterArray methodsFor:'printing & storing'! |
|
231 |
||
2241 | 232 |
printf_formatArgCount |
233 |
"Return the number of arguments required/produced if the receiver is interpreted |
|
234 |
as a printf/scanf format control string. |
|
235 |
For copyright information, see goodies/String-printf_scanf.chg" |
|
2303 | 236 |
|
2241 | 237 |
|nonConsecutive count| |
238 |
||
239 |
nonConsecutive := true. |
|
240 |
count := 0. |
|
2303 | 241 |
self do:[:c | |
242 |
c == $% ifTrue:[ |
|
243 |
nonConsecutive ifTrue:[ |
|
244 |
count := count + 1. |
|
245 |
nonConsecutive := false |
|
246 |
] ifFalse:[ |
|
247 |
count := count - 1. |
|
248 |
nonConsecutive := true |
|
249 |
] |
|
250 |
] ifFalse:[ |
|
251 |
nonConsecutive := true |
|
252 |
] |
|
2241 | 253 |
]. |
254 |
^ count |
|
255 |
! ! |
|
256 |
||
257 |
!CharacterArray methodsFor:'private'! |
|
258 |
||
2303 | 259 |
printf_printArgFrom:inStream to:outStream withData:argStream |
2241 | 260 |
"Interpret the required number of arguments from <argStream> |
2303 | 261 |
according to the formatting information in <inStream>. |
262 |
Place the interpretation on <outStream>. |
|
2241 | 263 |
The interpretation is C printf(3) style, as |
264 |
specified in the Unix C-language manual page for printf(3). |
|
265 |
<inStream> is assumed to be positioned just past |
|
266 |
$%, and a complete control string is assumed available. |
|
267 |
||
2303 | 268 |
Return when the conversion control string is consumed. |
2241 | 269 |
Leave <inStream> pointing past the last character in the conversion control string. |
270 |
||
271 |
This code assumes that <inStream> is formatted according to |
|
272 |
specification, and error checking is minimal. Unexpected |
|
273 |
results will be obtained by illegal control strings, or when |
|
274 |
argument types do not match conversion codes, but it probably |
|
275 |
won't dump core, like C does in such cases!! |
|
276 |
||
277 |
For copyright information, see goodies/String-printf_scanf.chg" |
|
2303 | 278 |
|
2256 | 279 |
|nextArg ljust plus pound width precision pad char arg argString| |
280 |
||
2303 | 281 |
nextArg := [ |
282 |
argStream atEnd ifTrue:[ |
|
283 |
self error:'not enough arguments for format string' |
|
284 |
]. |
|
285 |
argStream next |
|
286 |
]. |
|
2241 | 287 |
|
288 |
ljust := plus := pound := false. |
|
289 |
width := 0. |
|
290 |
precision := SmallInteger maxVal. |
|
291 |
pad := $ . |
|
292 |
char := inStream peek. |
|
293 |
char == $% ifTrue:[ |
|
2303 | 294 |
^ outStream nextPut:inStream next |
2241 | 295 |
]. |
296 |
char == $- ifTrue:[ |
|
2303 | 297 |
ljust := true. |
298 |
inStream next. |
|
299 |
char := inStream peek |
|
2241 | 300 |
]. |
301 |
char == $ ifTrue:[ |
|
2303 | 302 |
outStream space. |
303 |
inStream next. |
|
304 |
char := inStream peek |
|
2241 | 305 |
]. |
306 |
char == $+ ifTrue:[ |
|
2303 | 307 |
plus := true. |
308 |
inStream next. |
|
309 |
char := inStream peek |
|
2241 | 310 |
]. |
311 |
char == $# ifTrue:[ |
|
2303 | 312 |
pound := true. |
313 |
inStream next. |
|
314 |
char := inStream peek |
|
2241 | 315 |
]. |
316 |
char == $* ifTrue:[ |
|
2303 | 317 |
width := nextArg value. |
318 |
inStream next. |
|
319 |
char := inStream peek |
|
2241 | 320 |
]. |
321 |
char isDigit ifTrue:[ |
|
2303 | 322 |
char == $0 ifTrue:[ |
323 |
pad := $0 |
|
324 |
]. |
|
325 |
width := Integer readFrom:inStream. |
|
326 |
char := inStream peek |
|
2241 | 327 |
]. |
328 |
char == $. ifTrue:[ |
|
2303 | 329 |
inStream next. |
330 |
char := inStream peek. |
|
331 |
char == $* ifTrue:[ |
|
332 |
precision := nextArg value. |
|
333 |
inStream next. |
|
334 |
char := inStream peek |
|
335 |
] ifFalse:[ |
|
336 |
precision := Integer readFrom:inStream. |
|
337 |
char := inStream peek |
|
338 |
] |
|
2241 | 339 |
]. |
340 |
char == $l "Ignore long specifier." ifTrue:[ |
|
2303 | 341 |
inStream next. |
342 |
char := inStream peek |
|
2241 | 343 |
]. |
344 |
('feg' includes:char) ifTrue:[ |
|
2303 | 345 |
arg := nextArg value asFloat. |
346 |
precision := precision min:6. |
|
347 |
argString := WriteStream on:VariableString "String" new. |
|
348 |
char == $g ifTrue:[ |
|
349 |
arg absPrintOn:argString digits:precision + 1 |
|
350 |
]. |
|
351 |
char == $f ifTrue:[ |
|
352 |
arg absDecimalPrintOn:argString digits:precision + arg abs log + 1 |
|
353 |
]. |
|
354 |
char == $e ifTrue:[ |
|
355 |
arg absScientificPrintOn:argString digits:precision + 1 |
|
356 |
]. |
|
357 |
argString := argString contents. |
|
358 |
arg < 0 ifTrue:[ |
|
359 |
argString := '-' , argString |
|
360 |
] ifFalse:[ |
|
361 |
plus ifTrue:[ |
|
362 |
argString := '+' , argString |
|
363 |
] |
|
364 |
]. |
|
365 |
(precision = 0 and:[ pound not ]) ifTrue:[ |
|
366 |
(argString includes:$e) ifTrue:[ |
|
367 |
"self halt" |
|
368 |
] ifFalse:[ |
|
369 |
argString := arg truncated printString |
|
370 |
] |
|
371 |
]. |
|
372 |
pound ifTrue:[ |
|
373 |
(argString includes:$e) ifTrue:[ |
|
374 |
"self halt" |
|
375 |
] ifFalse:[ |
|
376 |
precision - (argString size - (argString indexOf:$.)) timesRepeat:[ |
|
377 |
argString := argString , '0' |
|
378 |
] |
|
379 |
] |
|
380 |
]. |
|
381 |
ljust ifTrue:[ |
|
382 |
outStream nextPutAll:argString |
|
383 |
]. |
|
384 |
width - argString size timesRepeat:[ |
|
385 |
outStream space |
|
386 |
]. |
|
387 |
ljust ifFalse:[ |
|
388 |
outStream nextPutAll:argString |
|
389 |
]. |
|
390 |
^ inStream next |
|
2241 | 391 |
]. |
392 |
char == $c ifTrue:[ |
|
2303 | 393 |
arg := String with:nextArg value asCharacter |
2241 | 394 |
]. |
395 |
char == $s "Assume the arg is a String or Symbol." ifTrue:[ |
|
2303 | 396 |
arg := nextArg value asString |
2241 | 397 |
]. |
398 |
char == $d ifTrue:[ |
|
2303 | 399 |
arg := nextArg value asInteger printString. |
400 |
plus ifTrue:[ |
|
401 |
arg := '+' , arg |
|
402 |
] |
|
2241 | 403 |
]. |
404 |
char == $u ifTrue:[ |
|
2303 | 405 |
arg := nextArg value asInteger abs printString |
2241 | 406 |
]. |
407 |
char == $o ifTrue:[ |
|
2303 | 408 |
arg := nextArg value asInteger abs printStringRadix:8. |
409 |
pound ifTrue:[ |
|
410 |
arg := '0' , arg |
|
411 |
] |
|
2256 | 412 |
]. |
413 |
char == $b ifTrue:[ |
|
2303 | 414 |
arg := nextArg value asInteger abs printStringRadix:2. |
415 |
pound ifTrue:[ |
|
416 |
arg := '0' , arg |
|
417 |
] |
|
2241 | 418 |
]. |
419 |
('xX' includes:char) ifTrue:[ |
|
2303 | 420 |
arg := nextArg value asInteger abs printStringRadix:16. |
421 |
pound ifTrue:[ |
|
422 |
arg := '0x' , arg |
|
423 |
] |
|
2241 | 424 |
]. |
425 |
char == $x ifTrue:[ |
|
2303 | 426 |
1 to:arg size do:[:i | |
427 |
('ABCDEF' includes:(arg at:i)) ifTrue:[ |
|
428 |
arg at:i put:((arg at:i) asciiValue + 16r20) asCharacter |
|
429 |
] |
|
430 |
] |
|
2241 | 431 |
]. |
432 |
precision := precision min:arg size. |
|
433 |
ljust ifTrue:[ |
|
2303 | 434 |
outStream nextPutAll:(arg copyFrom:1 to:precision) |
2241 | 435 |
]. |
436 |
width - precision timesRepeat:[ |
|
2303 | 437 |
outStream nextPut:pad |
2241 | 438 |
]. |
439 |
ljust ifFalse:[ |
|
2303 | 440 |
outStream nextPutAll:(arg copyFrom:1 to:precision) |
2241 | 441 |
]. |
442 |
^ inStream next |
|
443 |
! ! |
|
444 |
||
445 |
!CharacterArray methodsFor:'printing & storing'! |
|
446 |
||
2260 | 447 |
printf_printOn:outStream withData:args |
448 |
<resource: #obsolete> |
|
2303 | 449 |
|
2241 | 450 |
"Format and print the receiver on <outStream> with <args> |
451 |
formatted in C style, as specified in the Unix C-language manual page for printf(3). |
|
452 |
||
453 |
For copyright information, see goodies/String-printf_scanf.chg" |
|
454 |
||
2260 | 455 |
self obsoleteMethodWarning:'use printf:on:'. |
456 |
self printf:args on:outStream |
|
2241 | 457 |
! ! |
458 |
||
459 |
!CharacterArray methodsFor:'converting'! |
|
460 |
||
2303 | 461 |
scanf:dataStream |
2241 | 462 |
"Return a Collection of objects found in the Character Stream |
2303 | 463 |
<dataStream> as interpreted according to the receiver. |
2241 | 464 |
The receiver is assumed to be a conversion control string as |
465 |
specified in the Unix C-language manual page for scanf(3). |
|
466 |
For copyright information, see goodies/String-printf_scanf.chg" |
|
2303 | 467 |
|
2241 | 468 |
|results format char| |
469 |
||
470 |
results := OrderedCollection new. |
|
471 |
format := ReadStream on:self. |
|
472 |
[ format atEnd ] whileFalse:[ |
|
2303 | 473 |
char := format next. |
474 |
(char == Character space or:[ char == Character tab ]) ifTrue:[ |
|
475 |
dataStream skipSeparators. |
|
476 |
format skipSeparators |
|
477 |
]. |
|
478 |
char == $% ifTrue:[ |
|
479 |
self |
|
480 |
scanf_scanArgFrom:dataStream |
|
481 |
to:results |
|
482 |
format:format |
|
483 |
] ifFalse:[ |
|
484 |
dataStream peekFor:char |
|
485 |
] |
|
2241 | 486 |
]. |
487 |
^ results |
|
488 |
||
489 |
" |
|
2303 | 490 |
'%d %x' scanf:(ReadStream on:'1234 ff00') |
2241 | 491 |
" |
492 |
! ! |
|
493 |
||
494 |
!CharacterArray methodsFor:'private'! |
|
495 |
||
2303 | 496 |
scanf_scanArgFrom:dataStream to:collection format:format |
2241 | 497 |
"Add to <collection> an object who's representation is found |
498 |
in <dataStream> interpreted according to the conversion |
|
2303 | 499 |
control string in the Stream <format>. |
500 |
<format> is assumed to be positioned just past a $%, and a complete control |
|
2241 | 501 |
string is assumed available. |
502 |
||
503 |
Return when the conversion control string is consumed. Leave |
|
504 |
<format> pointing past the last character in the conversion |
|
505 |
control string, leave <dataStream> pointing past any width |
|
506 |
specified in <format>, or at the first character that doesn't |
|
507 |
make sense for the <format>. |
|
508 |
||
509 |
For copyright information, see goodies/String-printf_scanf.chg" |
|
2303 | 510 |
|
2241 | 511 |
|final width char pos data scanset exclusive return last| |
512 |
||
2303 | 513 |
final := [:retval | |
514 |
collection add:retval. |
|
515 |
data == dataStream ifFalse:[ |
|
516 |
dataStream position:dataStream position + data position |
|
517 |
]. |
|
518 |
^ self |
|
519 |
]. |
|
2241 | 520 |
width := 0. |
521 |
char := format peek. |
|
522 |
char == $% ifTrue:[ |
|
2303 | 523 |
^ dataStream peekFor:char |
2241 | 524 |
]. |
525 |
char == $* ifTrue:[ |
|
2303 | 526 |
format next. |
527 |
char := format peek. |
|
528 |
final := [:retval | |
|
529 |
data == dataStream ifFalse:[ |
|
530 |
dataStream position:dataStream position + data position |
|
531 |
]. |
|
532 |
^ self |
|
533 |
] |
|
2241 | 534 |
]. |
535 |
char isDigit ifTrue:[ |
|
2303 | 536 |
width := Integer readFrom:format. |
537 |
char := format peek |
|
2241 | 538 |
]. |
539 |
('slhduoxfeg' includes:char) ifTrue:[ |
|
2303 | 540 |
dataStream skipSeparators |
2241 | 541 |
]. |
542 |
width = 0 ifTrue:[ |
|
2303 | 543 |
data := dataStream |
2241 | 544 |
] ifFalse:[ |
2303 | 545 |
pos := dataStream position. |
546 |
data := ReadStream on:(dataStream next:width). |
|
547 |
dataStream position:pos |
|
2241 | 548 |
]. |
549 |
char == $s ifTrue:[ |
|
2303 | 550 |
final value:(data upToSeparator) |
2241 | 551 |
]. |
552 |
char == $c ifTrue:[ |
|
2303 | 553 |
width = 0 ifTrue:[ |
554 |
final value:(String with:data next) |
|
555 |
] ifFalse:[ |
|
556 |
final value:data contents |
|
557 |
] |
|
2241 | 558 |
]. |
559 |
char == $[ "What a mess!!" ifTrue:[ |
|
2303 | 560 |
return := WriteStream on:(VariableString "String" new:8). |
561 |
scanset := IdentitySet new. |
|
562 |
format next. |
|
563 |
width = 0 ifTrue:[ |
|
564 |
width := SmallInteger maxVal |
|
565 |
]. |
|
566 |
exclusive := format peekFor:$^. |
|
567 |
[ |
|
568 |
last := char. |
|
569 |
char := format next. |
|
570 |
char == $] |
|
571 |
] whileFalse:[ |
|
572 |
char == $- ifFalse:[ |
|
573 |
scanset add:char |
|
574 |
] ifTrue:[ |
|
575 |
(last to:format next) do:[:c | |
|
576 |
scanset add:c |
|
577 |
] |
|
578 |
] |
|
579 |
]. |
|
580 |
[ |
|
581 |
data atEnd not and:[ (scanset includes:data peek) xor:exclusive ] |
|
582 |
] whileTrue:[ return nextPut:data next ]. |
|
583 |
final value:return contents |
|
2241 | 584 |
]. |
585 |
('lh' includes:char) ifTrue:[ |
|
2303 | 586 |
format next. |
587 |
char := format peek |
|
2241 | 588 |
]. |
589 |
('DUdu' includes:char) ifTrue:[ |
|
2303 | 590 |
final value:(Integer readFrom:data) |
2241 | 591 |
]. |
592 |
('FEGfeg' includes:char) ifTrue:[ |
|
2303 | 593 |
final value:(Float readFrom:data) |
2241 | 594 |
]. |
2256 | 595 |
('b' includes:char) ifTrue:[ |
2303 | 596 |
final value:(Integer readFrom:data radix:2) |
2256 | 597 |
]. |
2241 | 598 |
('Oo' includes:char) ifTrue:[ |
2303 | 599 |
final value:(Integer readFrom:data radix:8) |
2241 | 600 |
]. |
601 |
('Xx' includes:char) ifTrue:[ |
|
2303 | 602 |
final value:(Integer readFrom:data radix:16) |
2241 | 603 |
] |
2256 | 604 |
|
605 |
" |
|
2303 | 606 |
'%d %x' sscanf:'1234 ff00' |
607 |
'%d %x %b' sscanf:'1234 ff00 1001' |
|
2256 | 608 |
" |
2241 | 609 |
! ! |
610 |
||
611 |
!CharacterArray methodsFor:'converting'! |
|
612 |
||
2303 | 613 |
sscanf:string |
2241 | 614 |
"Return a Collection of objects found in <string> as |
2303 | 615 |
interpreted according to the receiver. |
2241 | 616 |
The receiver is assumed to be a conversion control string as |
617 |
specified in the Unix C-language manual page for scanf(3). |
|
618 |
For copyright information, see goodies/String-printf_scanf.chg" |
|
2303 | 619 |
|
2241 | 620 |
^ self scanf:(ReadStream on:string) |
621 |
||
622 |
" |
|
2303 | 623 |
'%d %x' sscanf:'1234 ff00' |
624 |
'%d %x %b' sscanf:'1234 ff00 1001' |
|
2241 | 625 |
" |
626 |
! ! |
|
627 |
||
2237 | 628 |
!Float methodsFor:'private'! |
629 |
||
2303 | 630 |
absDecimalPrintOn:aStream digits:digits |
2237 | 631 |
"Place a string representation of the receiver's abs value |
632 |
on <aStream> using <digits> significant digits, using decimal notation. |
|
633 |
This is a helper for printf." |
|
634 |
||
635 |
|exp x fuzz i| |
|
636 |
||
637 |
"x is myself normalized to (1.0, 10.0), exp is my exponent" |
|
2303 | 638 |
exp := self abs < 1.0 |
639 |
ifTrue:[ (10.0 / self abs) log floor negated ] |
|
640 |
ifFalse:[ self abs log floor ]. |
|
2237 | 641 |
x := self abs / (10.0 raisedTo:exp). |
642 |
fuzz := 10.0 raisedTo:1 - digits. |
|
643 |
"round the last digit to be printed" |
|
644 |
x := 0.5 * fuzz + x. |
|
645 |
x >= 10.0 "check if rounding has unnormalized x" ifTrue:[ |
|
2303 | 646 |
x := x / 10.0. |
647 |
exp := exp + 1 |
|
2237 | 648 |
]. |
649 |
exp < 0 ifTrue:[ |
|
2303 | 650 |
1 to:1 - exp do:[:j | |
651 |
aStream nextPut:('0.000000000000' at:j) |
|
652 |
] |
|
2237 | 653 |
]. |
654 |
[ x >= fuzz ] "use fuzz to track significance" whileTrue:[ |
|
2303 | 655 |
i := x truncated. |
656 |
aStream nextPut:(48 + i) asCharacter. |
|
657 |
x := (x - i) * 10.0. |
|
658 |
fuzz := fuzz * 10.0. |
|
659 |
exp := exp - 1. |
|
660 |
exp = -1 ifTrue:[ |
|
661 |
aStream nextPut:$. |
|
662 |
] |
|
2237 | 663 |
]. |
664 |
[ exp >= -1 ] whileTrue:[ |
|
2303 | 665 |
aStream nextPut:$0. |
666 |
exp := exp - 1. |
|
667 |
exp = -1 ifTrue:[ |
|
668 |
aStream nextPut:$. |
|
669 |
] |
|
2237 | 670 |
] |
671 |
! ! |
|
672 |
||
673 |
!Float methodsFor:'private'! |
|
674 |
||
2303 | 675 |
absPrintOn:aStream digits:digits |
676 |
"Place a string representation of the receiver's abs value on <aStream> using |
|
2237 | 677 |
<digits> significant digits. |
678 |
This is a helper for printf." |
|
2303 | 679 |
|
2237 | 680 |
(self < 1.0e6 and:[ self > 1.0e-4 ]) ifTrue:[ |
2303 | 681 |
self absDecimalPrintOn:aStream digits:digits |
2237 | 682 |
] ifFalse:[ |
2303 | 683 |
self absScientificPrintOn:aStream digits:digits |
2237 | 684 |
] |
685 |
! ! |
|
686 |
||
687 |
!Float methodsFor:'private'! |
|
688 |
||
2303 | 689 |
absScientificPrintOn:aStream digits:digits |
2237 | 690 |
"Place a string representation of the receiver's abs value on <aStream> using <digits> significant |
691 |
digits, using scientific notation. |
|
692 |
This is a helper for printf." |
|
2303 | 693 |
|
2237 | 694 |
|exp fuzz x q i| |
695 |
||
696 |
"x is myself normalized to [1.0, 10.0), exp is my exponent" |
|
2303 | 697 |
exp := self abs < 1.0 |
698 |
ifTrue:[ (10.0 / self abs) log floor negated ] |
|
699 |
ifFalse:[ self abs log floor ]. |
|
2237 | 700 |
x := self abs / (10.0 raisedTo:exp). |
701 |
fuzz := 10.0 raisedTo:1 - digits. |
|
702 |
"round the last digit to be printed" |
|
703 |
x := 0.5 * fuzz + x. |
|
704 |
x >= 10.0 "check if rounding has unnormalized x" ifTrue:[ |
|
2303 | 705 |
x := x / 10.0. |
706 |
exp := exp + 1 |
|
2237 | 707 |
]. |
708 |
q := exp. |
|
709 |
exp := 0. |
|
710 |
[ x >= fuzz ] "use fuzz to track significance" whileTrue:[ |
|
2303 | 711 |
i := x truncated. |
712 |
aStream nextPut:(48 + i) asCharacter. |
|
713 |
x := (x - i) * 10.0. |
|
714 |
fuzz := fuzz * 10.0. |
|
715 |
exp := exp - 1. |
|
716 |
exp = -1 ifTrue:[ |
|
717 |
aStream nextPut:$. |
|
718 |
] |
|
2237 | 719 |
]. |
720 |
[ exp >= -1 ] whileTrue:[ |
|
2303 | 721 |
aStream nextPut:$0. |
722 |
exp := exp - 1. |
|
723 |
exp = -1 ifTrue:[ |
|
724 |
aStream nextPut:$. |
|
725 |
] |
|
2237 | 726 |
]. |
727 |
aStream nextPut:$e. |
|
728 |
q printOn:aStream |
|
729 |
! ! |
|
730 |
||
1759 | 731 |
!Object methodsFor:'dependents-interests'! |
732 |
||
733 |
addInterest:anInterest |
|
734 |
"install an interest forwarder. |
|
735 |
Here, we use the nonWeakDependencies." |
|
736 |
||
737 |
self addNonWeakDependent:anInterest |
|
738 |
||
739 |
"Created: 14.10.1996 / 22:27:34 / stefan" |
|
740 |
! ! |
|
741 |
||
742 |
!Object methodsFor:'dependents-interests'! |
|
743 |
||
744 |
expressInterestIn:aspect for:anObject sendBack:aSelector |
|
745 |
"arrange for aSelector to be sent to anObject whenever the receiver |
|
746 |
changes aspect." |
|
747 |
||
748 |
"/ for now, use an interestConverter, which is somewhat less efficient. |
|
749 |
"/ In the future, a more intelligent DependencyCollection class is planned for |
|
750 |
||
2303 | 751 |
self addInterest:(InterestConverter |
752 |
destination:anObject |
|
753 |
selector:aSelector |
|
754 |
aspect:aspect) |
|
1759 | 755 |
|
756 |
" |
|
757 |
|p b| |
|
758 |
||
759 |
b := [Transcript showCR:' -> the point changed']. |
|
760 |
||
761 |
p := Point new. |
|
762 |
Transcript showCR:'interest in #foo:'. |
|
763 |
p expressInterestIn:#foo for:b sendBack:#value. |
|
764 |
p x:1. |
|
765 |
Transcript showCR:'now changing #bar ... (expect no notification)'. |
|
766 |
p changed:#bar. |
|
767 |
Transcript cr. |
|
768 |
||
769 |
Delay waitForSeconds:1. |
|
770 |
Transcript showCR:'now changing #foo ... (expect notification)'. |
|
771 |
p changed:#foo. |
|
772 |
Transcript cr. |
|
773 |
||
774 |
Delay waitForSeconds:1. |
|
775 |
Transcript showCR:'no more interest in #foo:'. |
|
776 |
p retractInterestIn:#foo for:b. |
|
777 |
Transcript showCR:'now changing #foo ... (expect no notification)'. |
|
778 |
p changed:#foo. |
|
779 |
Transcript cr. |
|
780 |
||
781 |
Delay waitForSeconds:1. |
|
782 |
Transcript showCR:'interest in #bar now:'. |
|
783 |
p expressInterestIn:#bar for:b sendBack:#value. |
|
784 |
Transcript showCR:'now changing #foo ... (expect no notification)'. |
|
785 |
p changed:#foo. |
|
786 |
Transcript showCR:'now changing #bar ... (expect notification)'. |
|
787 |
p changed:#bar. |
|
788 |
Transcript cr. |
|
789 |
||
790 |
Delay waitForSeconds:1. |
|
791 |
Transcript showCR:'interest in #foo now:'. |
|
792 |
p expressInterestIn:#foo for:b sendBack:#value. |
|
793 |
Transcript showCR:'now changing #foo ... (expect notification)'. |
|
794 |
p changed:#foo. |
|
795 |
Transcript showCR:'now changing #bar ... (expect notification)'. |
|
796 |
p changed:#bar. |
|
797 |
Transcript cr. |
|
798 |
||
799 |
Delay waitForSeconds:1. |
|
800 |
Transcript showCR:'no more interests:'. |
|
801 |
p retractInterestsFor:b. |
|
802 |
Transcript showCR:'now changing #foo ... (expect no notification)'. |
|
803 |
p changed:#foo. |
|
804 |
Transcript showCR:'now changing #bar... (expect no notification)'. |
|
805 |
p changed:#bar. |
|
806 |
Transcript cr. |
|
807 |
||
808 |
p release. |
|
809 |
" |
|
1668 | 810 |
|
1759 | 811 |
"Created: 19.4.1996 / 10:26:22 / cg" |
812 |
"Modified: 19.4.1996 / 12:34:08 / cg" |
|
813 |
"Modified: 14.10.1996 / 22:28:20 / stefan" |
|
814 |
! ! |
|
815 |
||
816 |
!Object methodsFor:'dependents-interests'! |
|
817 |
||
818 |
interests |
|
819 |
"return a Collection of interests - empty if there is none. |
|
820 |
Here, we use the nonWeakDependents for interests." |
|
821 |
||
822 |
^ self nonWeakDependents |
|
823 |
||
824 |
"Created: / 14.10.1996 / 22:20:51 / stefan" |
|
825 |
"Modified: / 30.1.1998 / 14:07:35 / cg" |
|
826 |
! ! |
|
827 |
||
828 |
!Object methodsFor:'dependents-interests'! |
|
829 |
||
830 |
interestsFor:someOne |
|
831 |
"return a collection of interests of someOne - empty if there is none." |
|
832 |
||
833 |
|coll deps| |
|
834 |
||
835 |
deps := self interests. |
|
836 |
deps size == 0 ifTrue:[^ #()]. |
|
837 |
||
838 |
coll := IdentitySet new. |
|
839 |
||
840 |
deps do:[:dep | |
|
2303 | 841 |
(dep isInterestConverter) ifTrue:[ |
842 |
dep destination == someOne ifTrue:[ |
|
843 |
coll add:dep. |
|
844 |
] |
|
845 |
] |
|
1759 | 846 |
]. |
847 |
^ coll |
|
848 |
||
849 |
"Created: / 30.1.1998 / 14:02:26 / cg" |
|
850 |
"Modified: / 30.1.1998 / 14:08:24 / cg" |
|
851 |
! ! |
|
1668 | 852 |
|
1759 | 853 |
!Object methodsFor:'dependents-interests'! |
854 |
||
855 |
onChangeEvaluate:aBlock |
|
856 |
"arrange for aBlock to be evaluated whenever the receiver changes." |
|
857 |
||
2149
329461c02e84
allow for 0,1,2 arguments (update params) to be passed to interest blocks.
Claus Gittinger <cg@exept.de>
parents:
1759
diff
changeset
|
858 |
|na selector| |
329461c02e84
allow for 0,1,2 arguments (update params) to be passed to interest blocks.
Claus Gittinger <cg@exept.de>
parents:
1759
diff
changeset
|
859 |
|
329461c02e84
allow for 0,1,2 arguments (update params) to be passed to interest blocks.
Claus Gittinger <cg@exept.de>
parents:
1759
diff
changeset
|
860 |
na := aBlock numArgs. |
329461c02e84
allow for 0,1,2 arguments (update params) to be passed to interest blocks.
Claus Gittinger <cg@exept.de>
parents:
1759
diff
changeset
|
861 |
na == 0 ifTrue:[ |
2303 | 862 |
selector := #value |
2149
329461c02e84
allow for 0,1,2 arguments (update params) to be passed to interest blocks.
Claus Gittinger <cg@exept.de>
parents:
1759
diff
changeset
|
863 |
] ifFalse:[ |
2303 | 864 |
selector := #( #'value:' #'value:value:' #'value:value:value:') at:na |
2149
329461c02e84
allow for 0,1,2 arguments (update params) to be passed to interest blocks.
Claus Gittinger <cg@exept.de>
parents:
1759
diff
changeset
|
865 |
]. |
329461c02e84
allow for 0,1,2 arguments (update params) to be passed to interest blocks.
Claus Gittinger <cg@exept.de>
parents:
1759
diff
changeset
|
866 |
^ self onChangeSend:selector to:aBlock |
1759 | 867 |
|
868 |
" |
|
869 |
|p b| |
|
870 |
||
871 |
b := [Transcript showCR:' -> the point changed']. |
|
872 |
||
873 |
p := Point new. |
|
874 |
Transcript showCR:'interest in #foo:'. |
|
875 |
p onChangeEvaluate:b. |
|
876 |
p x:1. |
|
877 |
Transcript showCR:'now changing #bar ... (expect no notification)'. |
|
878 |
p changed:#bar. |
|
879 |
||
880 |
p retractInterests. |
|
881 |
p changed:#bar. |
|
882 |
" |
|
883 |
! ! |
|
884 |
||
885 |
!Object methodsFor:'dependents-interests'! |
|
886 |
||
887 |
onChangeSend:aSelector to:anObject |
|
888 |
"arrange for aSelector to be sent to anObject whenever the receiver |
|
889 |
changes." |
|
890 |
||
891 |
"/ for now, use an interestConverter, which is somewhat less efficient. |
|
892 |
"/ In the future, a more intelligent DependencyCollection class is planned for |
|
893 |
||
894 |
((self interests ? #()) |
|
2303 | 895 |
contains:[:anInterest | |
896 |
(anInterest isInterestConverter) |
|
897 |
and:[ anInterest destination == anObject |
|
898 |
and:[ anInterest selector == aSelector]] |
|
899 |
]) |
|
900 |
ifTrue:[^ self]. |
|
1759 | 901 |
|
2303 | 902 |
self addInterest:(InterestConverter |
903 |
destination:anObject |
|
904 |
selector:aSelector) |
|
1668 | 905 |
|
906 |
" |
|
1759 | 907 |
|p b| |
908 |
||
909 |
b := [Transcript showCR:'the point changed']. |
|
910 |
||
911 |
p := Point new. |
|
912 |
p onChangeSend:#value to:b. |
|
913 |
p x:1. |
|
914 |
Transcript showCR:'now changing'. |
|
915 |
p changed. |
|
916 |
Transcript cr. |
|
917 |
||
918 |
Delay waitForSeconds:1. |
|
919 |
Transcript showCR:'now changing'. |
|
920 |
p changed. |
|
921 |
Transcript cr. |
|
922 |
||
923 |
Delay waitForSeconds:1. |
|
924 |
Transcript showCR:'no more interest'. |
|
925 |
p retractInterestsFor:b. |
|
926 |
Transcript showCR:'now changing again'. |
|
927 |
p changed. |
|
928 |
Transcript cr. |
|
929 |
||
930 |
Delay waitForSeconds:1. |
|
931 |
Transcript showCR:'interest again'. |
|
932 |
p onChangeSend:#value to:b. |
|
933 |
Transcript showCR:'now changing again'. |
|
934 |
p changed. |
|
935 |
Transcript cr. |
|
936 |
" |
|
937 |
||
938 |
"Created: 19.4.1996 / 10:26:38 / cg" |
|
939 |
"Modified: 19.4.1996 / 12:34:26 / cg" |
|
940 |
"Modified: 14.10.1996 / 22:28:27 / stefan" |
|
941 |
! ! |
|
942 |
||
943 |
!Object methodsFor:'dependents-interests'! |
|
944 |
||
945 |
removeInterest:anInterest |
|
946 |
"remove an interest forwarder. |
|
947 |
Here, we use the nonWeakDependencies." |
|
948 |
||
949 |
self removeNonWeakDependent:anInterest |
|
950 |
||
951 |
"Created: 14.10.1996 / 22:21:59 / stefan" |
|
952 |
! ! |
|
953 |
||
954 |
!Object methodsFor:'dependents-interests'! |
|
955 |
||
956 |
retractInterestIn:aspect for:someOne |
|
957 |
"remove the interest of someOne in the receiver changing aspect |
|
958 |
(as installed with #expressInterestIn:for:sendBack:)." |
|
959 |
||
960 |
"/ for now, remove the interestConverter. |
|
961 |
"/ In the future, a more intelligent DependencyCollection class is planned for |
|
962 |
||
963 |
self retractInterestsForWhich:[:i | (i aspect == aspect) and:[i destination == someOne]] |
|
964 |
||
1668 | 965 |
" |
1759 | 966 |
|p b| |
967 |
||
968 |
b := [Transcript showCR:'the point changed']. |
|
969 |
||
970 |
p := Point new. |
|
971 |
Transcript showCR:'interest in #foo'. |
|
972 |
p expressInterestIn:#foo for:b sendBack:#value. |
|
973 |
p x:1. |
|
974 |
Transcript showCR:'now changing #bar'. |
|
975 |
p changed:#bar. |
|
976 |
Transcript cr. |
|
977 |
||
978 |
Delay waitForSeconds:1. |
|
979 |
Transcript showCR:'now changing #foo'. |
|
980 |
p changed:#foo. |
|
981 |
Transcript cr. |
|
982 |
||
983 |
Delay waitForSeconds:1. |
|
984 |
Transcript showCR:'no more interest in #foo'. |
|
985 |
p retractInterestIn:#foo for:b. |
|
986 |
Transcript showCR:'now changing #foo'. |
|
987 |
p changed:#foo. |
|
988 |
Transcript cr. |
|
989 |
||
990 |
Delay waitForSeconds:1. |
|
991 |
Transcript showCR:'interest in #bar now'. |
|
992 |
p expressInterestIn:#bar for:b sendBack:#value. |
|
993 |
Transcript showCR:'now changing #foo'. |
|
994 |
p changed:#foo. |
|
995 |
Transcript showCR:'now changing #bar'. |
|
996 |
p changed:#bar. |
|
997 |
Transcript cr. |
|
998 |
||
999 |
Delay waitForSeconds:1. |
|
1000 |
Transcript showCR:'interest in #foo now'. |
|
1001 |
p expressInterestIn:#foo for:b sendBack:#value. |
|
1002 |
Transcript showCR:'now changing #foo'. |
|
1003 |
p changed:#foo. |
|
1004 |
Transcript showCR:'now changing #bar'. |
|
1005 |
p changed:#bar. |
|
1006 |
Transcript cr. |
|
1007 |
||
1008 |
Delay waitForSeconds:1. |
|
1009 |
Transcript showCR:'no more interests'. |
|
1010 |
p retractInterestsFor:b. |
|
1011 |
Transcript showCR:'now changing #foo'. |
|
1012 |
p changed:#foo. |
|
1013 |
Transcript showCR:'now changing #bar'. |
|
1014 |
p changed:#bar. |
|
1015 |
Transcript cr. |
|
1016 |
" |
|
1017 |
||
1018 |
"Created: / 19.4.1996 / 10:27:11 / cg" |
|
1019 |
"Modified: / 14.10.1996 / 22:21:19 / stefan" |
|
1020 |
"Modified: / 30.1.1998 / 14:05:34 / cg" |
|
1021 |
! ! |
|
1022 |
||
1023 |
!Object methodsFor:'dependents-interests'! |
|
1024 |
||
1025 |
retractInterests |
|
1026 |
"remove all interests in the receiver changing aspect |
|
1027 |
(as installed with #expressInterestIn:for:sendBack:)." |
|
1028 |
||
1029 |
"/ for now, remove the interestConverter. |
|
1030 |
"/ In the future, a more intelligent DependencyCollection class is planned for |
|
1031 |
||
1032 |
self retractInterestsForWhich:[:i | true ] |
|
1033 |
! ! |
|
1034 |
||
1035 |
!Object methodsFor:'dependents-interests'! |
|
1036 |
||
1037 |
retractInterestsFor:someOne |
|
2303 | 1038 |
"remove the interest of someOne in the receiver |
1759 | 1039 |
(as installed with #onChangeSend:to:)." |
1040 |
||
1041 |
"/ for now, remove the interestConverter. |
|
1042 |
"/ In the future, a more intelligent DependencyCollection class is planned for |
|
1043 |
||
1044 |
self retractInterestsForWhich:[:i | i destination == someOne ] |
|
1045 |
||
1046 |
" |
|
1047 |
|p b| |
|
1048 |
||
1049 |
b := [Transcript showCR:'the point changed']. |
|
1050 |
||
1051 |
p := Point new. |
|
1052 |
p onChangeSend:#value to:b. |
|
1053 |
p x:1. |
|
1054 |
Transcript showCR:'now changing'. |
|
1055 |
p changed. |
|
1056 |
Transcript cr. |
|
1057 |
||
1058 |
Delay waitForSeconds:1. |
|
1059 |
Transcript showCR:'now changing'. |
|
1060 |
p changed. |
|
1061 |
Transcript cr. |
|
1062 |
||
1063 |
Delay waitForSeconds:1. |
|
1064 |
Transcript showCR:'no more interest'. |
|
1065 |
p retractInterestsFor:b. |
|
1066 |
Transcript showCR:'now changing again'. |
|
1067 |
p changed. |
|
1068 |
Transcript cr. |
|
1069 |
||
1070 |
Delay waitForSeconds:1. |
|
1071 |
Transcript showCR:'interest again'. |
|
1072 |
p onChangeSend:#value to:b. |
|
1073 |
Transcript showCR:'now changing again'. |
|
1074 |
p changed. |
|
1075 |
Transcript cr. |
|
1076 |
" |
|
1077 |
||
1078 |
"Created: / 19.4.1996 / 10:23:46 / cg" |
|
1079 |
"Modified: / 14.10.1996 / 22:21:25 / stefan" |
|
1080 |
"Modified: / 30.1.1998 / 14:04:52 / cg" |
|
1081 |
! ! |
|
1082 |
||
1083 |
!Object methodsFor:'dependents-interests'! |
|
1084 |
||
1085 |
retractInterestsForWhich:aBlock |
|
1086 |
"remove all interests in the receiver changing aspect |
|
1087 |
(as installed with #expressInterestIn:for:sendBack:)." |
|
1088 |
||
1089 |
"/ for now, remove the interestConverter. |
|
1090 |
"/ In the future, a more intelligent DependencyCollection class is planned for |
|
1091 |
||
1092 |
|deps coll| |
|
1093 |
||
1094 |
deps := self interests. |
|
1095 |
deps size ~~ 0 ifTrue:[ |
|
2303 | 1096 |
"/ cannot removeDependent within the loop - the interests collection rehashes |
1097 |
coll := OrderedCollection new. |
|
1098 |
deps do:[:dep | |
|
1099 |
dep isInterestConverter ifTrue:[ |
|
1100 |
(aBlock value:dep) ifTrue:[coll add:dep]. |
|
1101 |
] |
|
1102 |
]. |
|
1103 |
coll do:[:dep | |
|
1104 |
self removeInterest:dep. |
|
1105 |
]. |
|
1759 | 1106 |
]. |
1107 |
! ! |
|
1108 |
||
1109 |
!Object methodsFor:'dependents-interests'! |
|
1110 |
||
1111 |
retractInterestsIn:aspect |
|
1112 |
"remove all interests in the receiver changing aspect |
|
1113 |
(as installed with #expressInterestIn:for:sendBack:)." |
|
1114 |
||
1115 |
"/ for now, remove the interestConverter. |
|
1116 |
"/ In the future, a more intelligent DependencyCollection class is planned for |
|
1117 |
||
1118 |
self retractInterestsForWhich:[:i | i aspect == aspect ] |
|
1668 | 1119 |
! ! |
1120 |
||
1121 |
!Object methodsFor:'dependents-st/v event simulation'! |
|
1122 |
||
1123 |
when:eventSymbol send:selector to:anObject |
|
1124 |
"install an ST/V-style interest forwarder. |
|
1125 |
Here, we use the nonWeakDependencies." |
|
1126 |
||
1127 |
self addInterest:(InterestConverterWithParameters |
|
2303 | 1128 |
destination:anObject |
1129 |
selector:selector |
|
1130 |
aspect:eventSymbol). |
|
1668 | 1131 |
|
1132 |
" |
|
1133 |
|p b| |
|
1134 |
||
1135 |
b := [Transcript showCR:'the point changed']. |
|
1136 |
||
1137 |
p := Point new. |
|
1138 |
p whem:#foo:bar: send:#value:value: to:[:a :b | Transcript show:'foo: '; show:a; show:' bar: '; showCR:b]. |
|
1139 |
Transcript showCR:'now changing'. |
|
1140 |
p triggerEvent:#foo:bar: withArguments:#('fooArg' 'barArg'). |
|
1141 |
p retracrtInterests. |
|
1142 |
" |
|
1143 |
! ! |
|
1144 |
||
2303 | 1145 |
!stx_libbasic2 class methodsFor:'documentation'! |
1146 |
||
1147 |
extensionsVersion_CVS |
|
2337
755696d72648
comment/format in: #asSoundexCode
Claus Gittinger <cg@exept.de>
parents:
2303
diff
changeset
|
1148 |
^ '$Header: /cvs/stx/stx/libbasic2/extensions.st,v 1.16 2009-11-07 10:15:14 cg Exp $' |
755696d72648
comment/format in: #asSoundexCode
Claus Gittinger <cg@exept.de>
parents:
2303
diff
changeset
|
1149 |
! ! |