author | Stefan Vogel <sv@exept.de> |
Tue, 28 Apr 2020 16:22:26 +0200 | |
changeset 25375 | b784fc06a5eb |
parent 25267 | 260aad034ec4 |
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 |
||
22569 | 105 |
!WriteStream methodsFor:'Compatibility-Squeak'! |
106 |
||
107 |
resetContents |
|
108 |
"Squeak compatibility" |
|
109 |
||
110 |
self reset |
|
111 |
! ! |
|
112 |
||
1 | 113 |
!WriteStream methodsFor:'accessing'! |
114 |
||
15179 | 115 |
clear |
116 |
"for compatibility with Transcript" |
|
117 |
||
118 |
self reset |
|
119 |
! |
|
120 |
||
1 | 121 |
contents |
345 | 122 |
"return the current contents (a collection) of the stream. |
123 |
Currently, this returns the actual collection if possible |
|
124 |
(and reset is implemented to create a new one) in contrast to |
|
125 |
ST80, where contents returns a copy and reset only sets the writePointer. |
|
126 |
The ST/X behavior creates less temporary garbage in the normal case |
|
2410 | 127 |
(where things are written for the contents only) but may be incompatible |
345 | 128 |
with some applications. Time will show, if this is to be changed." |
1 | 129 |
|
260 | 130 |
|lastIndex| |
131 |
||
15347 | 132 |
lastIndex := position. |
22645 | 133 |
collection size ~~ lastIndex ifTrue:[ |
134 |
collection isFixedSize ifTrue:[ |
|
135 |
" |
|
136 |
grow is expensive - return a copy. |
|
137 |
(is this what users of writeStream expect ?) |
|
138 |
" |
|
139 |
collection := collection copyFrom:1 to:lastIndex |
|
140 |
] ifFalse:[ |
|
141 |
collection grow:lastIndex |
|
142 |
] |
|
1 | 143 |
]. |
144 |
^ collection |
|
2410 | 145 |
|
22645 | 146 |
"Modified: / 30-10-1997 / 16:21:23 / cg" |
147 |
"Modified: / 20-03-2018 / 16:34:29 / stefan" |
|
1 | 148 |
! |
149 |
||
5888 | 150 |
last |
151 |
"return the last element - report an error if the stream is empty" |
|
152 |
||
16201 | 153 |
^ collection at:position. |
5888 | 154 |
|
155 |
" |
|
156 |
|s| |
|
157 |
||
158 |
s := '' writeStream. |
|
159 |
s nextPut:$a. |
|
18294 | 160 |
s last. |
5888 | 161 |
s nextPut:$b. |
18294 | 162 |
s last. |
5888 | 163 |
s nextPut:$c. |
18294 | 164 |
s last. |
5888 | 165 |
" |
166 |
! |
|
167 |
||
168 |
last:n |
|
169 |
"return the last n elements as species of the underlying collection; |
|
170 |
Report an error if the stream is empty" |
|
171 |
||
17181 | 172 |
^ collection copyFrom:(position - n + 1) to:position. |
5888 | 173 |
|
174 |
" |
|
175 |
|s| |
|
176 |
||
177 |
s := '' writeStream. |
|
178 |
s nextPut:$a. |
|
18294 | 179 |
s last:1. |
5888 | 180 |
s nextPut:$b. |
18294 | 181 |
s last:1. |
182 |
s last:2. |
|
5888 | 183 |
s nextPut:$c. |
18294 | 184 |
s last:1. |
185 |
s last:2. |
|
186 |
s last:3. |
|
5888 | 187 |
" |
188 |
! |
|
189 |
||
22553 | 190 |
readStream |
191 |
"return a readstream on the current contents; |
|
192 |
that is: if more is written to the stream in the meantime, |
|
193 |
the readStream will NOT show it. |
|
194 |
To avoid copying the contents, a readStream with a readLimit |
|
195 |
is created. In the hope, that noone repositions and rewrites the |
|
196 |
underlying writeStream. |
|
197 |
If that leads to problems, you should use contents readStream." |
|
198 |
||
199 |
^ ReadStream on:collection from:1 to:self position. |
|
200 |
||
201 |
" |
|
202 |
|w r| |
|
203 |
||
204 |
w := WriteStream on:(String new). |
|
205 |
w nextPutAll:'12345'. |
|
206 |
r := w readStream. |
|
207 |
w nextPutAll:'abc'. |
|
208 |
w nextPutAll:(r upToEnd). |
|
209 |
w nextPutAll:'def'. |
|
210 |
w contents. |
|
211 |
" |
|
212 |
! |
|
213 |
||
329 | 214 |
reset |
345 | 215 |
"reset the stream; write anew. |
2410 | 216 |
See the comment in WriteStream>>contents" |
329 | 217 |
|
218 |
collection := collection species new:(collection size). |
|
19887 | 219 |
position := 0. |
2410 | 220 |
|
221 |
"Modified: 19.2.1997 / 08:57:00 / stefan" |
|
22150 | 222 |
! ! |
223 |
||
224 |
!WriteStream methodsFor:'positioning'! |
|
225 |
||
226 |
position:index0Based |
|
227 |
"redefined to allow positioning past the readLimit" |
|
228 |
||
229 |
((index0Based > collection size) or:[index0Based < 0]) ifTrue: [^ self positionError]. |
|
230 |
position := index0Based |
|
231 |
! |
|
232 |
||
233 |
skip:count |
|
234 |
"redefined to allow positioning back over already written characters" |
|
235 |
||
236 |
self position:(self position + count). |
|
22149 | 237 |
! |
238 |
||
239 |
truncateTo:givenMax |
|
240 |
"if the streamed contents is larger than maxSize, |
|
241 |
reset the write position back to this size. |
|
242 |
Otherwise do nothing." |
|
243 |
||
244 |
self size > givenMax ifTrue:[ |
|
245 |
self position:givenMax. |
|
246 |
]. |
|
247 |
||
248 |
" |
|
249 |
|s| |
|
250 |
||
251 |
s := '' writeStream. |
|
252 |
s nextPutAll:'123456'. |
|
253 |
s truncateTo:4. |
|
254 |
s nextPutAll:'abc'. |
|
255 |
s contents |
|
256 |
" |
|
257 |
||
258 |
" |
|
259 |
|s| |
|
260 |
||
261 |
s := '' writeStream. |
|
262 |
s nextPutAll:'1234'. |
|
263 |
s truncateTo:4. |
|
264 |
s nextPutAll:'abc'. |
|
265 |
s contents |
|
266 |
" |
|
267 |
||
268 |
" |
|
269 |
|s| |
|
270 |
||
271 |
s := '' writeStream. |
|
272 |
s nextPutAll:'123'. |
|
273 |
s truncateTo:4. |
|
274 |
s nextPutAll:'abc'. |
|
275 |
s contents |
|
276 |
" |
|
277 |
||
22152 | 278 |
" |
279 |
|s| |
|
280 |
||
281 |
s := 'testFile' asFilename writeStream. |
|
282 |
s nextPutAll:'123456'. |
|
283 |
s truncateTo:4. |
|
284 |
s nextPutAll:'abc'. |
|
285 |
s close. |
|
286 |
'testFile' asFilename contents |
|
287 |
" |
|
288 |
||
22149 | 289 |
"Created: / 02-08-2017 / 09:24:58 / cg" |
1 | 290 |
! ! |
291 |
||
611 | 292 |
!WriteStream methodsFor:'private'! |
293 |
||
294 |
growCollection |
|
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
295 |
"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
|
296 |
|
611 | 297 |
self growCollection:10 |
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
298 |
|
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
299 |
"Modified: 19.8.1997 / 17:53:28 / cg" |
611 | 300 |
! |
301 |
||
302 |
growCollection:minNewSize |
|
303 |
"grow the streamed collection to at least minNewSize" |
|
304 |
||
305 |
|oldSize newSize newColl| |
|
306 |
||
307 |
oldSize := collection size. |
|
308 |
(oldSize == 0) ifTrue:[ |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
309 |
newSize := minNewSize |
611 | 310 |
] ifFalse:[ |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
311 |
newSize := oldSize * 2. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
312 |
(newSize < minNewSize) ifTrue:[newSize := minNewSize]. |
611 | 313 |
]. |
314 |
collection isFixedSize ifTrue:[ |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
315 |
newColl := collection species new:newSize. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
316 |
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
|
317 |
collection := newColl |
611 | 318 |
] ifFalse:[ |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
319 |
collection grow:newSize |
611 | 320 |
]. |
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
321 |
|
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
322 |
"Modified: 19.8.1997 / 17:53:11 / cg" |
611 | 323 |
! ! |
324 |
||
7260 | 325 |
!WriteStream methodsFor:'private-accessing'! |
611 | 326 |
|
11794
ad5fcc151a09
#on: - set readLimit to 0, so that setToEnd positions to end of written
Stefan Vogel <sv@exept.de>
parents:
11324
diff
changeset
|
327 |
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
|
328 |
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
|
329 |
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
|
330 |
! |
ad5fcc151a09
#on: - set readLimit to 0, so that setToEnd positions to end of written
Stefan Vogel <sv@exept.de>
parents:
11324
diff
changeset
|
331 |
|
611 | 332 |
on:aCollection from:start to:last |
333 |
"create and return a new stream for writing onto aCollection, where |
|
334 |
writing is limited to the elements in the range start to last." |
|
335 |
||
336 |
super on:aCollection from:start to:last. |
|
337 |
writeLimit := last. |
|
338 |
! ! |
|
339 |
||
340 |
!WriteStream methodsFor:'queries'! |
|
341 |
||
25267 | 342 |
collectionSize |
343 |
^ position |
|
344 |
||
345 |
"Created: / 18-02-2020 / 23:28:01 / Stefan Vogel" |
|
346 |
! |
|
347 |
||
21132 | 348 |
endsWith:aCollection |
349 |
"Answer true, if the contents of the stream ends with aCollection. |
|
350 |
Speedup for upToAll:, throughAll etc." |
|
351 |
||
352 |
|sz start "{ Class: SmallInteger }"| |
|
353 |
||
354 |
collection isNil ifTrue:[ |
|
355 |
"only supported for internal streams" |
|
356 |
self shouldNotImplement. |
|
357 |
]. |
|
358 |
||
359 |
sz := aCollection size. |
|
360 |
start := position + 1 - sz. |
|
361 |
^ start > 0 and:[(collection copyFrom:position+1-sz to:position) endsWith:aCollection]. |
|
362 |
||
363 |
" |
|
364 |
(WriteStream with:'abcdef') endsWith:'def' |
|
365 |
(WriteStream with:'def') endsWith:'def' |
|
366 |
(WriteStream with:'abc') endsWith:'def' |
|
367 |
(WriteStream with:'ef') endsWith:'def' |
|
368 |
false " |
|
369 |
! |
|
370 |
||
3031 | 371 |
size |
372 |
"return the current size" |
|
373 |
||
15347 | 374 |
^ position |
611 | 375 |
! ! |
376 |
||
63 | 377 |
!WriteStream methodsFor:'reading'! |
1 | 378 |
|
379 |
next |
|
10 | 380 |
"catch read access to write stream - report an error" |
1 | 381 |
|
10 | 382 |
self shouldNotImplement |
1 | 383 |
! |
384 |
||
385 |
peek |
|
10 | 386 |
"catch read access to write stream - report an error" |
1 | 387 |
|
10 | 388 |
self shouldNotImplement |
63 | 389 |
! ! |
390 |
||
5556 | 391 |
!WriteStream methodsFor:'testing'! |
392 |
||
393 |
isEmpty |
|
394 |
"return true, if the contents of the stream is empty" |
|
395 |
||
15617 | 396 |
^ self position == 0 |
5556 | 397 |
|
398 |
"Created: 14.10.1997 / 20:44:37 / cg" |
|
16201 | 399 |
! |
400 |
||
18294 | 401 |
isReadable |
20432 | 402 |
"return true if the receiver supports reading - that's not true" |
16201 | 403 |
|
404 |
^ false |
|
405 |
||
406 |
"Created: / 8.11.1997 / 14:06:07 / cg" |
|
407 |
! |
|
408 |
||
409 |
isWritable |
|
19437 | 410 |
"return true, if writing is supported by the receiver. |
16201 | 411 |
Always return true here" |
412 |
||
413 |
^ true |
|
414 |
||
415 |
"Modified: 16.5.1996 / 14:44:49 / cg" |
|
5556 | 416 |
! ! |
417 |
||
611 | 418 |
!WriteStream methodsFor:'writing'! |
369 | 419 |
|
611 | 420 |
next:count put:anObject |
421 |
"append anObject count times to the receiver. |
|
422 |
Redefined to avoid count grows of the underlying collection - |
|
423 |
instead a single grow on the final size is performed." |
|
424 |
||
17181 | 425 |
|final| |
611 | 426 |
|
17062 | 427 |
count == 0 ifTrue:[^ self]. |
428 |
||
611 | 429 |
(collection isNil or:[writeLimit notNil]) ifTrue:[ |
19643 | 430 |
super next:count put:anObject. |
431 |
^ self. |
|
611 | 432 |
]. |
369 | 433 |
|
17181 | 434 |
final := position + count. |
611 | 435 |
(final > collection size) ifTrue:[ |
19643 | 436 |
self growCollection:final |
611 | 437 |
]. |
369 | 438 |
|
17181 | 439 |
collection from:position + 1 to:final put:anObject. |
440 |
position := position + count. |
|
19643 | 441 |
(position > readLimit) ifTrue:[readLimit := position]. |
24629 | 442 |
"/ ^ anObject -- no; return self |
16793 | 443 |
|
444 |
" |
|
445 |
'' writeStream next:10 put:$* |
|
446 |
" |
|
611 | 447 |
! |
1 | 448 |
|
449 |
nextPut:anObject |
|
1584 | 450 |
"append the argument, anObject to the stream. |
24657 | 451 |
Answer the argument. |
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
452 |
Specially tuned for appending to String, ByteArray and Array streams." |
1 | 453 |
|
63 | 454 |
%{ /* NOCONTEXT */ |
455 |
||
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
456 |
#ifndef NO_PRIM_STREAM |
63 | 457 |
REGISTER int pos; |
458 |
unsigned ch; |
|
459 |
OBJ coll; |
|
2883 | 460 |
OBJ p, wL, rL; |
1584 | 461 |
int __readLimit = -1; |
63 | 462 |
|
1136 | 463 |
coll = __INST(collection); |
464 |
p = __INST(position); |
|
63 | 465 |
|
293 | 466 |
if (__isNonNilObject(coll) && __isSmallInteger(p)) { |
19643 | 467 |
pos = __intVal(p); |
468 |
/* make 1-based */ |
|
469 |
pos = pos + 1; |
|
470 |
wL = __INST(writeLimit); |
|
63 | 471 |
|
19643 | 472 |
if ((wL == nil) |
473 |
|| (__isSmallInteger(wL) && (pos <= __intVal(wL)))) { |
|
474 |
OBJ cls; |
|
252 | 475 |
|
19643 | 476 |
cls = __qClass(coll); |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
477 |
|
19643 | 478 |
rL = __INST(readLimit); |
479 |
if (__isSmallInteger(rL)) { |
|
480 |
__readLimit = __intVal(rL); |
|
481 |
} |
|
293 | 482 |
|
19643 | 483 |
if (cls == @global(String)) { |
24900 | 484 |
if (pos <= __stringSize(coll) |
485 |
&& ((__isCharacter(anObject) && (ch =__intVal(__characterVal(anObject)), 1)) |
|
486 |
|| (__isSmallInteger(anObject) && (ch =__intVal(anObject)), 1)) |
|
487 |
&& ch <= 0xFF |
|
488 |
) { |
|
19643 | 489 |
__StringInstPtr(coll)->s_element[pos-1] = ch; |
18294 | 490 |
advancePositionAndReturn: ; |
19643 | 491 |
if ((__readLimit >= 0) && (pos > __readLimit)) { |
492 |
__INST(readLimit) = __mkSmallInteger(pos); |
|
493 |
} |
|
494 |
__INST(position) = __mkSmallInteger(pos); |
|
24647 | 495 |
RETURN ( anObject ); |
19643 | 496 |
} |
497 |
} else if (cls == @global(ByteArray)) { |
|
498 |
if (__isSmallInteger(anObject) |
|
499 |
&& ((ch = __intVal(anObject)) <= 0xFF) /* ch is unsigned */ |
|
500 |
&& (pos <= __byteArraySize(coll))) { |
|
501 |
__ByteArrayInstPtr(coll)->ba_element[pos-1] = ch; |
|
502 |
goto advancePositionAndReturn; |
|
503 |
} |
|
504 |
} else if (cls == @global(Array)) { |
|
505 |
if (pos <= __arraySize(coll)) { |
|
506 |
__ArrayInstPtr(coll)->a_element[pos-1] = anObject; |
|
507 |
__STORE(coll, anObject); |
|
508 |
goto advancePositionAndReturn; |
|
509 |
} |
|
510 |
} else if (cls == @global(Unicode16String)) { |
|
511 |
if (__isCharacter(anObject) |
|
512 |
&& ((ch = __intVal(__characterVal(anObject))) <= 0xFFFF) /* ch is unsigned */ |
|
513 |
&& (pos <= __unicode16StringSize(coll))) { |
|
514 |
__Unicode16StringInstPtr(coll)->s_element[pos-1] = ch; |
|
515 |
goto advancePositionAndReturn; |
|
516 |
} |
|
517 |
} else if (cls == @global(Unicode32String)) { |
|
518 |
if (__isCharacter(anObject) |
|
519 |
&& (pos <= __unicode32StringSize(coll))) { |
|
520 |
__Unicode32StringInstPtr(coll)->s_element[pos-1] = __intVal(__characterVal(anObject)); |
|
521 |
goto advancePositionAndReturn; |
|
522 |
} |
|
523 |
} |
|
524 |
} |
|
63 | 525 |
} |
2894 | 526 |
#endif |
260 | 527 |
%}. |
24032 | 528 |
(writeLimit isNil or:[(position + 1) <= writeLimit]) ifTrue:[ |
19643 | 529 |
(position >= collection size) ifTrue:[self growCollection]. |
24900 | 530 |
(anObject isInteger and:[collection class == String]) ifTrue:[ |
531 |
"for compatibility with #nextPutAll:, which allows to write a ByteArray, |
|
532 |
allow Bytes too" |
|
533 |
collection byteAt:(position + 1) put:anObject. |
|
534 |
] ifFalse:[ |
|
535 |
collection at:(position + 1) put:anObject. |
|
536 |
]. |
|
19643 | 537 |
position := position + 1. |
538 |
(position > readLimit) ifTrue:[readLimit := position]. |
|
838 | 539 |
] ifFalse:[ |
19643 | 540 |
WriteError raiseErrorString:'write beyond writeLimit' |
293 | 541 |
]. |
24900 | 542 |
^ anObject |
543 |
||
544 |
" |
|
545 |
(WriteStream on:'') nextPut:30; nextPut:$a; nextPut:20; nextPut:$Z; contents |
|
546 |
" |
|
547 |
||
548 |
"Modified (format): / 15-11-2019 / 17:51:52 / Stefan Vogel" |
|
1 | 549 |
! |
550 |
||
551 |
nextPutAll:aCollection |
|
63 | 552 |
"append all elements of the argument, aCollection to the stream. |
24635 | 553 |
Answer the receiver. |
63 | 554 |
Redefined to avoid count grows of the underlying collection - |
555 |
instead a single grow on the final size is performed." |
|
1 | 556 |
|
17181 | 557 |
|nMore "{ Class: SmallInteger }" |
558 |
final "{ Class: SmallInteger }" | |
|
1 | 559 |
|
18294 | 560 |
(collection notNil and:[aCollection isSequenceable]) ifFalse:[ |
19643 | 561 |
"/ fallback |
562 |
super nextPutAll:aCollection. |
|
563 |
^ self. |
|
13628 | 564 |
]. |
77 | 565 |
|
1 | 566 |
nMore := aCollection size. |
9499 | 567 |
nMore == 0 ifTrue:[ |
19643 | 568 |
"/ for the programmer.. |
569 |
aCollection isCollection ifFalse:[ |
|
23011 | 570 |
self proceedableError:'invalid argument (not a collection)'. |
19643 | 571 |
]. |
9499 | 572 |
]. |
573 |
||
17181 | 574 |
final := position + nMore. |
24032 | 575 |
(writeLimit notNil and:[final > writeLimit]) ifTrue:[ |
576 |
"/ cg: the code below silently truncated the written string, |
|
577 |
"/ which is incorrect, as it should report a writeError!! |
|
578 |
^ super nextPutAll:aCollection. |
|
579 |
"/ final := writeLimit. |
|
580 |
"/ nMore := final - position |
|
293 | 581 |
]. |
1 | 582 |
(final > collection size) ifTrue:[ |
19643 | 583 |
self growCollection:final |
1 | 584 |
]. |
24032 | 585 |
collection replaceFrom:(position + 1) to:final with:aCollection startingAt:1. |
1 | 586 |
|
17181 | 587 |
position := position + nMore. |
19643 | 588 |
(position > readLimit) ifTrue:[readLimit := position]. |
12130 | 589 |
"/ ^ aCollection -- self |
13628 | 590 |
|
591 |
"Modified: / 04-09-2011 / 20:03:32 / cg" |
|
24032 | 592 |
"Modified: / 01-04-2019 / 17:05:36 / Claus Gittinger" |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
593 |
! |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
594 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
595 |
nextPutAll:aCollection startingAt:pos1 to:pos2 |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
596 |
"append some elements of the argument, aCollection to the stream. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
597 |
Redefined to avoid count grows of the underlying collection - |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
598 |
instead a single grow on the final size is performed." |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
599 |
|
17181 | 600 |
|nMore final| |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
601 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
602 |
collection isNil ifTrue:[ |
24634 | 603 |
"/ will raise an error |
604 |
^ super nextPutAll:aCollection startingAt:pos1 to:pos2. |
|
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
605 |
]. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
606 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
607 |
nMore := pos2 - pos1 + 1. |
17181 | 608 |
final := position + nMore. |
24032 | 609 |
(writeLimit notNil and:[final > writeLimit]) ifTrue:[ |
610 |
"/ cg: the code below silently truncated the written string, |
|
611 |
"/ which is incorrect, as it should report a writeError!! |
|
612 |
^ super nextPutAll:aCollection startingAt:pos1 to:pos2. |
|
613 |
"/ final := writeLimit. |
|
614 |
"/ nMore := final - position |
|
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
615 |
]. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
616 |
(final > collection size) ifTrue:[ |
19643 | 617 |
self growCollection:final |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
618 |
]. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
619 |
|
24032 | 620 |
collection replaceFrom:(position + 1) to:final with:aCollection startingAt:pos1. |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
621 |
|
17181 | 622 |
position := position + nMore. |
19643 | 623 |
(position > readLimit) ifTrue:[readLimit := position]. |
12131 | 624 |
"/ ^ aCollection -- return self |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
625 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
626 |
" |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
627 |
|s| |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
628 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
629 |
s := '' writeStream. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
630 |
s nextPutAll:'hello '. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
631 |
s nextPutAll:'1234world012345' startingAt:5 to:9. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
632 |
s contents |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
633 |
" |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
634 |
|
24032 | 635 |
"Modified: / 12-07-1996 / 10:31:36 / cg" |
636 |
"Modified: / 01-04-2019 / 17:05:52 / Claus Gittinger" |
|
8361 | 637 |
! |
638 |
||
11324 | 639 |
nextPutAllUnicode:aString |
640 |
"normal streams can not handle multi-byte characters, so convert them to utf8" |
|
641 |
||
15653 | 642 |
"this code is not perfect if you use both #nextPutAll: and #nextPutAllUnicode: |
18294 | 643 |
with the same stream, since 8-bit characters (with the highest bits set) |
15653 | 644 |
are not stored as UTF, so we get some inconsistent string" |
645 |
||
646 |
collection isString ifTrue:[ |
|
18294 | 647 |
collection bitsPerCharacter == 16 ifTrue:[ |
648 |
self nextPutAllUtf16:aString. |
|
649 |
] ifFalse:[ |
|
650 |
self nextPutAllUtf8:aString. |
|
651 |
]. |
|
11324 | 652 |
] ifFalse:[ |
18294 | 653 |
self nextPutAll:aString |
11324 | 654 |
]. |
13738
e11fccebb0c3
changed: #nextPutAllUnicode:
Claus Gittinger <cg@exept.de>
parents:
13628
diff
changeset
|
655 |
|
e11fccebb0c3
changed: #nextPutAllUnicode:
Claus Gittinger <cg@exept.de>
parents:
13628
diff
changeset
|
656 |
"Modified: / 28-09-2011 / 16:15:52 / cg" |
11324 | 657 |
! |
658 |
||
8615 | 659 |
nextPutByte:anObject |
660 |
"append the argument, anObject to the stream. |
|
661 |
Specially tuned for appending to String and ByteArray streams." |
|
662 |
||
663 |
%{ /* NOCONTEXT */ |
|
664 |
||
665 |
#ifndef NO_PRIM_STREAM |
|
19828 | 666 |
OBJ coll = __INST(collection); |
667 |
OBJ p = __INST(position); |
|
8615 | 668 |
|
669 |
if (__isNonNilObject(coll) && __isSmallInteger(p) && __isSmallInteger(anObject)) { |
|
19828 | 670 |
OBJ wL = __INST(writeLimit); |
671 |
INT pos = __intVal(p) + 1; /* make 1-based and usable for update below */ |
|
672 |
unsigned int ch = __intVal(anObject); |
|
8615 | 673 |
|
24032 | 674 |
if ((ch <= 0xFF) /* ch is unsigned */ |
675 |
&& ((wL == nil) || (__isSmallInteger(wL) && (pos <= __intVal(wL))))) { |
|
19828 | 676 |
OBJ cls = __qClass(coll); |
677 |
OBJ rL = __INST(readLimit); |
|
678 |
INT __readLimit = -1; |
|
8615 | 679 |
|
19643 | 680 |
if (__isSmallInteger(rL)) { |
681 |
__readLimit = __intVal(rL); |
|
682 |
} |
|
8615 | 683 |
|
19643 | 684 |
if (cls == @global(String)) { |
19828 | 685 |
if (pos <= __stringSize(coll)) { |
19643 | 686 |
__StringInstPtr(coll)->s_element[pos-1] = ch; |
18294 | 687 |
advancePositionAndReturn: ; |
19643 | 688 |
if ((__readLimit >= 0) && (pos > __readLimit)) { |
689 |
__INST(readLimit) = __mkSmallInteger(pos); |
|
690 |
} |
|
691 |
__INST(position) = __mkSmallInteger(pos); |
|
24632 | 692 |
RETURN ( self ); |
19643 | 693 |
} |
694 |
} else if (cls == @global(ByteArray)) { |
|
19828 | 695 |
if (pos <= __byteArraySize(coll)) { |
19643 | 696 |
__ByteArrayInstPtr(coll)->ba_element[pos-1] = ch; |
697 |
goto advancePositionAndReturn; |
|
698 |
} |
|
699 |
} |
|
700 |
} |
|
8615 | 701 |
} |
702 |
#endif |
|
703 |
%}. |
|
19828 | 704 |
((writeLimit isNil or:[(position + 1) <= writeLimit]) |
705 |
and:[position >= collection size]) ifTrue:[ |
|
706 |
self growCollection. |
|
24632 | 707 |
self nextPutByte:anObject. "try again" |
19828 | 708 |
] ifFalse:[ |
24632 | 709 |
super nextPutByte:anObject |
19828 | 710 |
]. |
8615 | 711 |
! |
712 |
||
8361 | 713 |
nextPutBytes:count from:anObject startingAt:start |
714 |
"write count bytes from an object starting at index start. |
|
715 |
Return the number of bytes written. |
|
18294 | 716 |
The object must have non-pointer indexed instvars |
717 |
(i.e. be a ByteArray, String, Float- or DoubleArray). |
|
8361 | 718 |
Use with care - non object oriented i/o. |
719 |
This is provided for compatibility with externalStream; |
|
720 |
to support binary storage" |
|
721 |
||
11794
ad5fcc151a09
#on: - set readLimit to 0, so that setToEnd positions to end of written
Stefan Vogel <sv@exept.de>
parents:
11324
diff
changeset
|
722 |
anObject isByteCollection ifTrue:[ |
18294 | 723 |
self nextPutAll:anObject startingAt:start to:(start + count - 1). |
724 |
^ count. |
|
8361 | 725 |
]. |
726 |
^ super nextPutBytes:count from:anObject startingAt:start |
|
11324 | 727 |
! |
728 |
||
729 |
nextPutUnicode:aCharacter |
|
730 |
"normal streams can not handle multi-byte characters, so convert them to utf8" |
|
731 |
||
15653 | 732 |
"this code is not perfect if you use both #nextPut: and #nextPutUnicode: |
18294 | 733 |
with the same stream, since 8-bit characters (with the highest bits set) |
15653 | 734 |
are not stored as UTF, so we get some inconsistent string" |
735 |
||
736 |
collection isString ifTrue:[ |
|
18294 | 737 |
collection bitsPerCharacter == 16 ifTrue:[ |
738 |
self nextPutUtf16:aCharacter. |
|
739 |
] ifFalse:[ |
|
740 |
self nextPutUtf8:aCharacter. |
|
741 |
]. |
|
11324 | 742 |
] ifFalse:[ |
18294 | 743 |
self nextPut:aCharacter. |
11324 | 744 |
]. |
1 | 745 |
! ! |
746 |
||
1965 | 747 |
!WriteStream class methodsFor:'documentation'! |
701 | 748 |
|
749 |
version |
|
19437 | 750 |
^ '$Header$' |
12130 | 751 |
! |
752 |
||
753 |
version_CVS |
|
19437 | 754 |
^ '$Header$' |
701 | 755 |
! ! |
19437 | 756 |