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