author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Mon, 12 Jul 2010 17:20:36 +0100 | |
branch | jv |
changeset 17780 | b6e42c92eba0 |
parent 17772 | 6e0b3c06c364 |
child 17807 | 06cc6c49e291 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
252 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
5556 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
1 | 14 |
PositionableStream subclass:#WriteStream |
1295 | 15 |
instanceVariableNames:'' |
16 |
classVariableNames:'' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'Streams' |
|
1 | 19 |
! |
20 |
||
1965 | 21 |
!WriteStream class methodsFor:'documentation'! |
1 | 22 |
|
88 | 23 |
copyright |
24 |
" |
|
25 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
252 | 26 |
All Rights Reserved |
88 | 27 |
|
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
36 |
||
68 | 37 |
documentation |
38 |
" |
|
2883 | 39 |
Streams for writing into. |
40 |
WriteStreams are especially useful, if big strings are to be constructed |
|
41 |
from pieces - create a writeStream, add the pieces (with #nextPut or |
|
42 |
#nextPutAll) and finally fetch the concatenated string via #contents. |
|
43 |
This is much better than constructing the big string by concatenating via |
|
44 |
the comma (,) operator, since less intermediate garbage objects are created. |
|
45 |
||
46 |
This implementation currently DOES change the |
|
261 | 47 |
identity if the streamed-upon collection IF it cannot grow easily. |
48 |
Collections which cannot grow easily are for example: Array, ByteArray and String. |
|
88 | 49 |
Thus it is slightly incompatible to ST-80 since 'aStream contents' does |
50 |
not always return the original collection. This may change. |
|
1295 | 51 |
|
52 |
[author:] |
|
10281 | 53 |
Claus Gittinger |
54 |
||
55 |
[see also:] |
|
56 |
CharacterWriteStream (if streaming for a unicode string) |
|
68 | 57 |
" |
2883 | 58 |
! |
59 |
||
60 |
examples |
|
61 |
" |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
62 |
[exBegin] |
2883 | 63 |
|s| |
64 |
||
65 |
s := WriteStream on:''. |
|
66 |
s nextPutAll:'hello'; |
|
67 |
space; |
|
68 |
nextPutAll:'world'. |
|
69 |
||
70 |
s contents inspect |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
71 |
[exEnd] |
2883 | 72 |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
73 |
[exBegin] |
2883 | 74 |
|s| |
75 |
||
76 |
s := WriteStream on:''. |
|
77 |
s nextPutAll:'hello'; |
|
78 |
space; |
|
79 |
nextPutAll:'world'. |
|
80 |
||
81 |
Transcript nextPutLine:(s contents) |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
82 |
[exEnd] |
2883 | 83 |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
84 |
[exBegin] |
2883 | 85 |
|s| |
86 |
||
87 |
s := '' writeStream. |
|
88 |
s nextPutAll:'hello'; |
|
89 |
space; |
|
90 |
nextPutAll:'world'. |
|
91 |
||
92 |
Transcript nextPutLine:(s contents) |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
93 |
[exEnd] |
2883 | 94 |
" |
95 |
||
345 | 96 |
! ! |
97 |
||
7261 | 98 |
!WriteStream methodsFor:'Compatibility-Dolphin'! |
6325 | 99 |
|
100 |
display:someObject |
|
6871
1c4a799a048c
display: use printOn instead of printString.
Claus Gittinger <cg@exept.de>
parents:
6326
diff
changeset
|
101 |
someObject printOn:self. |
6325 | 102 |
! ! |
103 |
||
1 | 104 |
!WriteStream methodsFor:'accessing'! |
105 |
||
106 |
contents |
|
345 | 107 |
"return the current contents (a collection) of the stream. |
108 |
Currently, this returns the actual collection if possible |
|
109 |
(and reset is implemented to create a new one) in contrast to |
|
110 |
ST80, where contents returns a copy and reset only sets the writePointer. |
|
111 |
The ST/X behavior creates less temporary garbage in the normal case |
|
2410 | 112 |
(where things are written for the contents only) but may be incompatible |
345 | 113 |
with some applications. Time will show, if this is to be changed." |
1 | 114 |
|
260 | 115 |
|lastIndex| |
116 |
||
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
117 |
lastIndex := position - ZeroPosition. |
260 | 118 |
collection size == lastIndex ifFalse:[ |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
119 |
collection isFixedSize ifTrue:[ |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
120 |
" |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
121 |
grow is expensive - return a copy. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
122 |
(is this what users of writeStream expect ?) |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
123 |
" |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
124 |
collection := collection copyFrom:1 to:lastIndex |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
125 |
] ifFalse:[ |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
126 |
collection grow:lastIndex |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
127 |
] |
1 | 128 |
]. |
129 |
^ collection |
|
2410 | 130 |
|
3087 | 131 |
"Modified: / 19.2.1997 / 08:57:28 / stefan" |
132 |
"Modified: / 30.10.1997 / 16:21:23 / cg" |
|
1 | 133 |
! |
134 |
||
5888 | 135 |
last |
136 |
"return the last element - report an error if the stream is empty" |
|
137 |
||
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
138 |
|position1Based| |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
139 |
|
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
140 |
position1Based := position - ZeroPosition + 1. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
141 |
^ collection at:(position1Based - 1). |
5888 | 142 |
|
143 |
" |
|
144 |
|s| |
|
145 |
||
146 |
s := '' writeStream. |
|
147 |
s nextPut:$a. |
|
148 |
s last. |
|
149 |
s nextPut:$b. |
|
150 |
s last. |
|
151 |
s nextPut:$c. |
|
152 |
s last. |
|
153 |
" |
|
154 |
! |
|
155 |
||
156 |
last:n |
|
157 |
"return the last n elements as species of the underlying collection; |
|
158 |
Report an error if the stream is empty" |
|
159 |
||
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
160 |
|position1Based| |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
161 |
|
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
162 |
position1Based := position - ZeroPosition + 1. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
163 |
^ collection copyFrom:(position1Based - n) to:(position1Based - 1). |
5888 | 164 |
|
165 |
" |
|
166 |
|s| |
|
167 |
||
168 |
s := '' writeStream. |
|
169 |
s nextPut:$a. |
|
170 |
s last:1. |
|
171 |
s nextPut:$b. |
|
172 |
s last:1. |
|
173 |
s last:2. |
|
174 |
s nextPut:$c. |
|
175 |
s last:1. |
|
176 |
s last:2. |
|
177 |
s last:3. |
|
178 |
" |
|
179 |
! |
|
180 |
||
329 | 181 |
reset |
345 | 182 |
"reset the stream; write anew. |
2410 | 183 |
See the comment in WriteStream>>contents" |
329 | 184 |
|
185 |
collection := collection species new:(collection size). |
|
186 |
super reset |
|
2410 | 187 |
|
188 |
"Modified: 19.2.1997 / 08:57:00 / stefan" |
|
1 | 189 |
! ! |
190 |
||
611 | 191 |
!WriteStream methodsFor:'positioning'! |
192 |
||
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
193 |
position0Based:index0Based |
611 | 194 |
"redefined to allow positioning past the readLimit" |
195 |
||
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
196 |
((index0Based > collection size) or:[index0Based < 0]) ifTrue: [^ self positionError]. |
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
197 |
position := index0Based + ZeroPosition |
611 | 198 |
! ! |
199 |
||
200 |
!WriteStream methodsFor:'private'! |
|
201 |
||
202 |
growCollection |
|
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
203 |
"grow the streamed collection to at least 10 elements" |
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
204 |
|
611 | 205 |
self growCollection:10 |
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
206 |
|
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
207 |
"Modified: 19.8.1997 / 17:53:28 / cg" |
611 | 208 |
! |
209 |
||
210 |
growCollection:minNewSize |
|
211 |
"grow the streamed collection to at least minNewSize" |
|
212 |
||
213 |
|oldSize newSize newColl| |
|
214 |
||
215 |
oldSize := collection size. |
|
216 |
(oldSize == 0) ifTrue:[ |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
217 |
newSize := minNewSize |
611 | 218 |
] ifFalse:[ |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
219 |
newSize := oldSize * 2. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
220 |
(newSize < minNewSize) ifTrue:[newSize := minNewSize]. |
611 | 221 |
]. |
222 |
collection isFixedSize ifTrue:[ |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
223 |
newColl := collection species new:newSize. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
224 |
newColl replaceFrom:1 to:oldSize with:collection startingAt:1. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
225 |
collection := newColl |
611 | 226 |
] ifFalse:[ |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
227 |
collection grow:newSize |
611 | 228 |
]. |
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
229 |
|
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
230 |
"Modified: 19.8.1997 / 17:53:11 / cg" |
8868 | 231 |
! |
232 |
||
233 |
setCollection:newCollection |
|
234 |
collection := newCollection |
|
611 | 235 |
! ! |
236 |
||
7260 | 237 |
!WriteStream methodsFor:'private-accessing'! |
611 | 238 |
|
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
239 |
on:aCollection |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
240 |
super on:aCollection. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
241 |
readLimit := 0. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
242 |
! |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
243 |
|
611 | 244 |
on:aCollection from:start to:last |
245 |
"create and return a new stream for writing onto aCollection, where |
|
246 |
writing is limited to the elements in the range start to last." |
|
247 |
||
248 |
super on:aCollection from:start to:last. |
|
249 |
writeLimit := last. |
|
250 |
! ! |
|
251 |
||
252 |
!WriteStream methodsFor:'queries'! |
|
253 |
||
3098
4203b0d13fc8
added #isReadable (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3087
diff
changeset
|
254 |
isReadable |
4203b0d13fc8
added #isReadable (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3087
diff
changeset
|
255 |
"return true if the receiver supports reading - thats not true" |
4203b0d13fc8
added #isReadable (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3087
diff
changeset
|
256 |
|
4203b0d13fc8
added #isReadable (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3087
diff
changeset
|
257 |
^ false |
4203b0d13fc8
added #isReadable (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3087
diff
changeset
|
258 |
|
4203b0d13fc8
added #isReadable (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3087
diff
changeset
|
259 |
"Created: / 8.11.1997 / 14:06:07 / cg" |
4203b0d13fc8
added #isReadable (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3087
diff
changeset
|
260 |
! |
4203b0d13fc8
added #isReadable (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3087
diff
changeset
|
261 |
|
611 | 262 |
isWritable |
1405 | 263 |
"return true, if writing is supported by the recevier. |
264 |
Always return true here" |
|
265 |
||
611 | 266 |
^ true |
1405 | 267 |
|
268 |
"Modified: 16.5.1996 / 14:44:49 / cg" |
|
3031 | 269 |
! |
270 |
||
271 |
size |
|
272 |
"return the current size" |
|
273 |
||
9311 | 274 |
^ position - ZeroPosition |
611 | 275 |
! ! |
276 |
||
63 | 277 |
!WriteStream methodsFor:'reading'! |
1 | 278 |
|
279 |
next |
|
10 | 280 |
"catch read access to write stream - report an error" |
1 | 281 |
|
10 | 282 |
self shouldNotImplement |
1 | 283 |
! |
284 |
||
285 |
peek |
|
10 | 286 |
"catch read access to write stream - report an error" |
1 | 287 |
|
10 | 288 |
self shouldNotImplement |
63 | 289 |
! ! |
290 |
||
5556 | 291 |
!WriteStream methodsFor:'testing'! |
292 |
||
293 |
isEmpty |
|
294 |
"return true, if the contents of the stream is empty" |
|
295 |
||
7620
cf2c3d9d6d9c
Fix #isEmpty for external streams.
Stefan Vogel <sv@exept.de>
parents:
7261
diff
changeset
|
296 |
^ self position0Based == ZeroPosition |
5556 | 297 |
|
298 |
"Created: 14.10.1997 / 20:44:37 / cg" |
|
299 |
! ! |
|
300 |
||
611 | 301 |
!WriteStream methodsFor:'writing'! |
369 | 302 |
|
611 | 303 |
next:count put:anObject |
304 |
"append anObject count times to the receiver. |
|
305 |
Redefined to avoid count grows of the underlying collection - |
|
306 |
instead a single grow on the final size is performed." |
|
307 |
||
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
308 |
|final position1Based| |
611 | 309 |
|
310 |
(collection isNil or:[writeLimit notNil]) ifTrue:[ |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
311 |
super next:count put:anObject. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
312 |
^ self. |
611 | 313 |
]. |
369 | 314 |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
315 |
position1Based := position - ZeroPosition + 1. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
316 |
final := position1Based + count - 1. |
611 | 317 |
(final > collection size) ifTrue:[ |
7113 | 318 |
self growCollection:final |
611 | 319 |
]. |
369 | 320 |
|
7113 | 321 |
position1Based to:final do:[:index | |
322 |
collection at:index put:anObject. |
|
611 | 323 |
]. |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
324 |
position1Based := position1Based + count. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
325 |
(position1Based > readLimit) ifTrue:[readLimit := position1Based - 1]. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
326 |
position := position1Based - 1 + ZeroPosition. |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
327 |
"/ ^ anObject -- return self |
611 | 328 |
! |
1 | 329 |
|
5198
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
330 |
next:n putAll:aCollection startingAt:pos1 |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
331 |
"append some elements of the argument, aCollection to the stream." |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
332 |
|
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
333 |
^ self nextPutAll:aCollection startingAt:pos1 to:pos1+n-1 |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
334 |
|
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
335 |
" |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
336 |
|s| |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
337 |
|
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
338 |
s := '' writeStream. |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
339 |
s nextPutAll:'hello '. |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
340 |
s next:5 putAll:'1234world012345' startingAt:5. |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
341 |
s contents |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
342 |
" |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
343 |
|
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
344 |
"Modified: 12.7.1996 / 10:31:36 / cg" |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
345 |
! |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
346 |
|
1 | 347 |
nextPut:anObject |
1584 | 348 |
"append the argument, anObject to the stream. |
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
349 |
Specially tuned for appending to String, ByteArray and Array streams." |
1 | 350 |
|
63 | 351 |
%{ /* NOCONTEXT */ |
352 |
||
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
353 |
#ifndef NO_PRIM_STREAM |
63 | 354 |
REGISTER int pos; |
355 |
unsigned ch; |
|
356 |
OBJ coll; |
|
2883 | 357 |
OBJ p, wL, rL; |
1584 | 358 |
int __readLimit = -1; |
63 | 359 |
|
1136 | 360 |
coll = __INST(collection); |
361 |
p = __INST(position); |
|
63 | 362 |
|
293 | 363 |
if (__isNonNilObject(coll) && __isSmallInteger(p)) { |
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
364 |
pos = __intVal(p); |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
365 |
/* make 1-based */ |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
366 |
pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition)); |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
367 |
wL = __INST(writeLimit); |
63 | 368 |
|
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
369 |
if ((wL == nil) |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
370 |
|| (__isSmallInteger(wL) && (pos <= __intVal(wL)))) { |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
371 |
OBJ cls; |
252 | 372 |
|
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
373 |
cls = __qClass(coll); |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
374 |
|
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
375 |
rL = __INST(readLimit); |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
376 |
if (__isSmallInteger(rL)) { |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
377 |
__readLimit = __intVal(rL); |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
378 |
} |
293 | 379 |
|
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
380 |
if (cls == @global(String)) { |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
381 |
if (__isCharacter(anObject) |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
382 |
&& ((ch = __intVal(__characterVal(anObject))) <= 255) /* ch is unsigned */ |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
383 |
&& (pos <= __stringSize(coll))) { |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
384 |
__StringInstPtr(coll)->s_element[pos-1] = ch; |
8615 | 385 |
advancePositionAndReturn: ; |
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
386 |
if ((__readLimit >= 0) && (pos >= __readLimit)) { |
8913 | 387 |
__INST(readLimit) = __mkSmallInteger(pos); |
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
388 |
} |
8913 | 389 |
__INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1); |
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
390 |
RETURN ( anObject ); |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
391 |
} |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
392 |
} else if (cls == @global(ByteArray)) { |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
393 |
if (__isSmallInteger(anObject) |
8296 | 394 |
&& ((ch = __intVal(anObject)) <= 0xFF) /* ch is unsigned */ |
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
395 |
&& (pos <= __byteArraySize(coll))) { |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
396 |
__ByteArrayInstPtr(coll)->ba_element[pos-1] = ch; |
8615 | 397 |
goto advancePositionAndReturn; |
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
398 |
} |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
399 |
} else if (cls == @global(Array)) { |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
400 |
if (pos <= __arraySize(coll)) { |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
401 |
__ArrayInstPtr(coll)->a_element[pos-1] = anObject; |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
402 |
__STORE(coll, anObject); |
8615 | 403 |
goto advancePositionAndReturn; |
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
404 |
} |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
405 |
} else if (cls == @global(Unicode16String)) { |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
406 |
if (__isCharacter(anObject) |
8296 | 407 |
&& ((ch = __intVal(__characterVal(anObject))) <= 0xFFFF) /* ch is unsigned */ |
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
408 |
&& (pos <= __unicode16StringSize(coll))) { |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
409 |
__Unicode16StringInstPtr(coll)->s_element[pos-1] = ch; |
8615 | 410 |
goto advancePositionAndReturn; |
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
411 |
} |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
412 |
} else if (cls == @global(Unicode32String)) { |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
413 |
if (__isCharacter(anObject) |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
414 |
&& (pos <= __unicode32StringSize(coll))) { |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
415 |
__Unicode32StringInstPtr(coll)->s_element[pos-1] = __intVal(__characterVal(anObject)); |
8615 | 416 |
goto advancePositionAndReturn; |
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
417 |
} |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
418 |
} |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
419 |
} |
63 | 420 |
} |
2894 | 421 |
#endif |
260 | 422 |
%}. |
293 | 423 |
(writeLimit isNil |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
424 |
or:[(position + 1 - ZeroPosition) <= writeLimit]) ifTrue:[ |
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
425 |
((position + 1 - ZeroPosition) > collection size) ifTrue:[self growCollection]. |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
426 |
collection at:(position + 1 - ZeroPosition) put:anObject. |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
427 |
((position + 1 - ZeroPosition) > readLimit) ifTrue:[readLimit := (position + 1 - ZeroPosition)]. |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
428 |
position := position + 1. |
838 | 429 |
] ifFalse:[ |
8320
229adaeaf183
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
8296
diff
changeset
|
430 |
WriteError raiseErrorString:'write beyond writeLimit' |
293 | 431 |
]. |
1 | 432 |
^anObject |
433 |
! |
|
434 |
||
435 |
nextPutAll:aCollection |
|
63 | 436 |
"append all elements of the argument, aCollection to the stream. |
437 |
Redefined to avoid count grows of the underlying collection - |
|
438 |
instead a single grow on the final size is performed." |
|
1 | 439 |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
440 |
|nMore final position1Based| |
1 | 441 |
|
77 | 442 |
collection isNil ifTrue:[ |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
443 |
super nextPutAll:aCollection. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
444 |
^ self |
77 | 445 |
]. |
446 |
||
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
447 |
position1Based := position - ZeroPosition + 1. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
448 |
|
1 | 449 |
nMore := aCollection size. |
9499 | 450 |
nMore == 0 ifTrue:[ |
451 |
"/ for the programmer.. |
|
452 |
aCollection isCollection ifFalse:[ |
|
10070 | 453 |
self error:'invalid argument (not a collection)' mayProceed:true |
9499 | 454 |
]. |
455 |
]. |
|
456 |
||
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
457 |
final := position1Based + nMore - 1. |
293 | 458 |
(writeLimit notNil |
459 |
and:[final > writeLimit]) ifTrue:[ |
|
7686
290cda6075ac
String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents:
7620
diff
changeset
|
460 |
final := writeLimit. |
290cda6075ac
String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents:
7620
diff
changeset
|
461 |
nMore := final - position1Based + 1 |
293 | 462 |
]. |
1 | 463 |
(final > collection size) ifTrue:[ |
7686
290cda6075ac
String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents:
7620
diff
changeset
|
464 |
self growCollection:final |
1 | 465 |
]. |
7686
290cda6075ac
String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents:
7620
diff
changeset
|
466 |
collection |
290cda6075ac
String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents:
7620
diff
changeset
|
467 |
replaceFrom:position1Based |
290cda6075ac
String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents:
7620
diff
changeset
|
468 |
to:final |
290cda6075ac
String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents:
7620
diff
changeset
|
469 |
with:aCollection |
290cda6075ac
String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents:
7620
diff
changeset
|
470 |
startingAt:1. |
1 | 471 |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
472 |
position1Based := position1Based + nMore. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
473 |
(position1Based > readLimit) ifTrue:[readLimit := position1Based - 1]. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
474 |
position := position1Based - 1 + ZeroPosition. |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
475 |
"/ ^ aCollection -- self |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
476 |
! |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
477 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
478 |
nextPutAll:aCollection startingAt:pos1 to:pos2 |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
479 |
"append some elements of the argument, aCollection to the stream. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
480 |
Redefined to avoid count grows of the underlying collection - |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
481 |
instead a single grow on the final size is performed." |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
482 |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
483 |
|nMore final position1Based| |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
484 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
485 |
collection isNil ifTrue:[ |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
486 |
^ super nextPutAll:aCollection startingAt:pos1 to:pos2 |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
487 |
]. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
488 |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
489 |
position1Based := position - ZeroPosition + 1. |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
490 |
nMore := pos2 - pos1 + 1. |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
491 |
final := position1Based + nMore - 1. |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
492 |
(writeLimit notNil |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
493 |
and:[final > writeLimit]) ifTrue:[ |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
494 |
final := writeLimit. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
495 |
nMore := final - position1Based + 1 |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
496 |
]. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
497 |
(final > collection size) ifTrue:[ |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
498 |
self growCollection:final |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
499 |
]. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
500 |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
501 |
collection replaceFrom:position1Based |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
502 |
to:final |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
503 |
with:aCollection |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
504 |
startingAt:pos1. |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
505 |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
506 |
position1Based := position1Based + nMore. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
507 |
(position1Based > readLimit) ifTrue:[readLimit := position1Based - 1]. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
508 |
position := position1Based - 1 + ZeroPosition. |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
509 |
"/ ^ aCollection -- return self |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
510 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
511 |
" |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
512 |
|s| |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
513 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
514 |
s := '' writeStream. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
515 |
s nextPutAll:'hello '. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
516 |
s nextPutAll:'1234world012345' startingAt:5 to:9. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
517 |
s contents |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
518 |
" |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
519 |
|
1541 | 520 |
"Modified: 12.7.1996 / 10:31:36 / cg" |
8361 | 521 |
! |
522 |
||
11324 | 523 |
nextPutAllUnicode:aString |
524 |
"normal streams can not handle multi-byte characters, so convert them to utf8" |
|
525 |
||
526 |
(collection isString and:[collection bitsPerCharacter == 8]) ifTrue:[ |
|
527 |
aString do:[:eachCharacter| |
|
528 |
self nextPutUtf8:eachCharacter. |
|
529 |
]. |
|
530 |
] ifFalse:[ |
|
531 |
self nextPutAll:aString |
|
532 |
]. |
|
533 |
! |
|
534 |
||
8615 | 535 |
nextPutByte:anObject |
536 |
"append the argument, anObject to the stream. |
|
537 |
Specially tuned for appending to String and ByteArray streams." |
|
538 |
||
539 |
%{ /* NOCONTEXT */ |
|
540 |
||
541 |
#ifndef NO_PRIM_STREAM |
|
542 |
REGISTER int pos; |
|
543 |
OBJ coll; |
|
544 |
OBJ p, wL, rL; |
|
545 |
int __readLimit = -1; |
|
546 |
||
547 |
coll = __INST(collection); |
|
548 |
p = __INST(position); |
|
549 |
||
550 |
if (__isNonNilObject(coll) && __isSmallInteger(p) && __isSmallInteger(anObject)) { |
|
551 |
unsigned ch; |
|
552 |
||
553 |
ch = __intVal(anObject); |
|
554 |
||
555 |
pos = __intVal(p); |
|
556 |
/* make 1-based */ |
|
557 |
pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition)); |
|
558 |
wL = __INST(writeLimit); |
|
559 |
||
560 |
if ((wL == nil) |
|
561 |
|| (__isSmallInteger(wL) && (pos <= __intVal(wL)))) { |
|
562 |
OBJ cls; |
|
563 |
||
564 |
cls = __qClass(coll); |
|
565 |
||
566 |
rL = __INST(readLimit); |
|
567 |
if (__isSmallInteger(rL)) { |
|
568 |
__readLimit = __intVal(rL); |
|
569 |
} |
|
570 |
||
571 |
if (cls == @global(String)) { |
|
572 |
if ((pos <= __stringSize(coll)) |
|
573 |
&& (ch <= 0xFF)) { /* ch is unsigned */ |
|
574 |
__StringInstPtr(coll)->s_element[pos-1] = ch; |
|
575 |
advancePositionAndReturn: ; |
|
576 |
if ((__readLimit >= 0) && (pos >= __readLimit)) { |
|
8913 | 577 |
__INST(readLimit) = __mkSmallInteger(pos); |
8615 | 578 |
} |
8913 | 579 |
__INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1); |
8615 | 580 |
RETURN ( anObject ); |
581 |
} |
|
582 |
} else if (cls == @global(ByteArray)) { |
|
583 |
if ((pos <= __byteArraySize(coll)) |
|
584 |
&& (ch <= 0xFF)) { /* ch is unsigned */ |
|
585 |
__ByteArrayInstPtr(coll)->ba_element[pos-1] = ch; |
|
586 |
goto advancePositionAndReturn; |
|
587 |
} |
|
588 |
} |
|
589 |
} |
|
590 |
} |
|
591 |
#endif |
|
592 |
%}. |
|
593 |
^ super nextPutByte:anObject |
|
594 |
! |
|
595 |
||
8361 | 596 |
nextPutBytes:count from:anObject startingAt:start |
597 |
"write count bytes from an object starting at index start. |
|
598 |
Return the number of bytes written. |
|
599 |
The object must have non-pointer indexed instvars |
|
600 |
(i.e. be a ByteArray, String, Float- or DoubleArray). |
|
601 |
Use with care - non object oriented i/o. |
|
602 |
This is provided for compatibility with externalStream; |
|
603 |
to support binary storage" |
|
604 |
||
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
605 |
anObject isByteCollection ifTrue:[ |
8361 | 606 |
self nextPutAll:anObject startingAt:start to:(start + count - 1). |
607 |
^ count. |
|
608 |
]. |
|
609 |
^ super nextPutBytes:count from:anObject startingAt:start |
|
11324 | 610 |
! |
611 |
||
612 |
nextPutUnicode:aCharacter |
|
613 |
"normal streams can not handle multi-byte characters, so convert them to utf8" |
|
614 |
||
615 |
(collection isString and:[collection bitsPerCharacter == 8]) ifTrue:[ |
|
616 |
self nextPutUtf8:aCharacter. |
|
617 |
] ifFalse:[ |
|
618 |
self nextPut:aCharacter. |
|
619 |
]. |
|
1 | 620 |
! ! |
621 |
||
1965 | 622 |
!WriteStream class methodsFor:'documentation'! |
701 | 623 |
|
624 |
version |
|
17780 | 625 |
^ '$Id: WriteStream.st 10544 2010-07-12 16:20:36Z vranyj1 $' |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
626 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
627 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
628 |
version_CVS |
17780 | 629 |
^ 'Header: /cvs/stx/stx/libbasic/WriteStream.st,v 1.71 2009/10/05 09:19:55 cg Exp ' |
17761
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17735
diff
changeset
|
630 |
! |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
631 |
|
17761
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17735
diff
changeset
|
632 |
version_SVN |
17780 | 633 |
^ '$Id: WriteStream.st 10544 2010-07-12 16:20:36Z vranyj1 $' |
17761
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17735
diff
changeset
|
634 |
! ! |
17767 | 635 |
|
17772
6e0b3c06c364
Method reverted to r10532
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17767
diff
changeset
|
636 |
|
17780 | 637 |