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