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