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