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