author | Stefan Vogel <sv@exept.de> |
Fri, 13 Mar 2015 12:27:34 +0100 | |
changeset 17617 | 3f5422b2b175 |
parent 17614 | 21f3862fe413 |
child 17688 | 74182903ca82 |
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 |
||
48 |
This implementation currently DOES change the |
|
261 | 49 |
identity if the streamed-upon collection IF it cannot grow easily. |
50 |
Collections which cannot grow easily are for example: Array, ByteArray and String. |
|
88 | 51 |
Thus it is slightly incompatible to ST-80 since 'aStream contents' does |
52 |
not always return the original collection. This may change. |
|
1295 | 53 |
|
17557 | 54 |
[caveat:] |
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) |
|
58 |
||
1295 | 59 |
[author:] |
10281 | 60 |
Claus Gittinger |
61 |
||
62 |
[see also:] |
|
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:[ |
15347 | 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. |
|
152 |
s last. |
|
153 |
s nextPut:$b. |
|
154 |
s last. |
|
155 |
s nextPut:$c. |
|
156 |
s last. |
|
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. |
|
171 |
s last:1. |
|
172 |
s nextPut:$b. |
|
173 |
s last:1. |
|
174 |
s last:2. |
|
175 |
s nextPut:$c. |
|
176 |
s last:1. |
|
177 |
s last:2. |
|
178 |
s last:3. |
|
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). |
|
187 |
super reset |
|
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 |
611 | 199 |
! ! |
200 |
||
201 |
!WriteStream methodsFor:'private'! |
|
202 |
||
203 |
growCollection |
|
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
204 |
"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
|
205 |
|
611 | 206 |
self growCollection:10 |
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
207 |
|
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
208 |
"Modified: 19.8.1997 / 17:53:28 / cg" |
611 | 209 |
! |
210 |
||
211 |
growCollection:minNewSize |
|
212 |
"grow the streamed collection to at least minNewSize" |
|
213 |
||
214 |
|oldSize newSize newColl| |
|
215 |
||
216 |
oldSize := collection size. |
|
217 |
(oldSize == 0) ifTrue:[ |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
218 |
newSize := minNewSize |
611 | 219 |
] ifFalse:[ |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
220 |
newSize := oldSize * 2. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
221 |
(newSize < minNewSize) ifTrue:[newSize := minNewSize]. |
611 | 222 |
]. |
223 |
collection isFixedSize ifTrue:[ |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
224 |
newColl := collection species new:newSize. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
225 |
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
|
226 |
collection := newColl |
611 | 227 |
] ifFalse:[ |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
228 |
collection grow:newSize |
611 | 229 |
]. |
2863
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
230 |
|
aec08a0c2c2e
no need for unsigned compare against 0
Claus Gittinger <cg@exept.de>
parents:
2410
diff
changeset
|
231 |
"Modified: 19.8.1997 / 17:53:11 / cg" |
611 | 232 |
! ! |
233 |
||
7260 | 234 |
!WriteStream methodsFor:'private-accessing'! |
611 | 235 |
|
11794
ad5fcc151a09
#on: - set readLimit to 0, so that setToEnd positions to end of written
Stefan Vogel <sv@exept.de>
parents:
11324
diff
changeset
|
236 |
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
|
237 |
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
|
238 |
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
|
239 |
! |
ad5fcc151a09
#on: - set readLimit to 0, so that setToEnd positions to end of written
Stefan Vogel <sv@exept.de>
parents:
11324
diff
changeset
|
240 |
|
611 | 241 |
on:aCollection from:start to:last |
242 |
"create and return a new stream for writing onto aCollection, where |
|
243 |
writing is limited to the elements in the range start to last." |
|
244 |
||
245 |
super on:aCollection from:start to:last. |
|
246 |
writeLimit := last. |
|
247 |
! ! |
|
248 |
||
249 |
!WriteStream methodsFor:'queries'! |
|
250 |
||
3031 | 251 |
size |
252 |
"return the current size" |
|
253 |
||
15347 | 254 |
^ position |
611 | 255 |
! ! |
256 |
||
63 | 257 |
!WriteStream methodsFor:'reading'! |
1 | 258 |
|
259 |
next |
|
10 | 260 |
"catch read access to write stream - report an error" |
1 | 261 |
|
10 | 262 |
self shouldNotImplement |
1 | 263 |
! |
264 |
||
265 |
peek |
|
10 | 266 |
"catch read access to write stream - report an error" |
1 | 267 |
|
10 | 268 |
self shouldNotImplement |
63 | 269 |
! ! |
270 |
||
5556 | 271 |
!WriteStream methodsFor:'testing'! |
272 |
||
273 |
isEmpty |
|
274 |
"return true, if the contents of the stream is empty" |
|
275 |
||
15617 | 276 |
^ self position == 0 |
5556 | 277 |
|
278 |
"Created: 14.10.1997 / 20:44:37 / cg" |
|
16201 | 279 |
! |
280 |
||
281 |
isReadable |
|
282 |
"return true if the receiver supports reading - thats not true" |
|
283 |
||
284 |
^ false |
|
285 |
||
286 |
"Created: / 8.11.1997 / 14:06:07 / cg" |
|
287 |
! |
|
288 |
||
289 |
isWritable |
|
290 |
"return true, if writing is supported by the recevier. |
|
291 |
Always return true here" |
|
292 |
||
293 |
^ true |
|
294 |
||
295 |
"Modified: 16.5.1996 / 14:44:49 / cg" |
|
5556 | 296 |
! ! |
297 |
||
611 | 298 |
!WriteStream methodsFor:'writing'! |
369 | 299 |
|
611 | 300 |
next:count put:anObject |
301 |
"append anObject count times to the receiver. |
|
302 |
Redefined to avoid count grows of the underlying collection - |
|
303 |
instead a single grow on the final size is performed." |
|
304 |
||
17181 | 305 |
|final| |
611 | 306 |
|
17062 | 307 |
count == 0 ifTrue:[^ self]. |
308 |
||
611 | 309 |
(collection isNil or:[writeLimit notNil]) ifTrue:[ |
12131 | 310 |
super next:count put:anObject. |
311 |
^ self. |
|
611 | 312 |
]. |
369 | 313 |
|
17181 | 314 |
final := position + count. |
611 | 315 |
(final > collection size) ifTrue:[ |
7113 | 316 |
self growCollection:final |
611 | 317 |
]. |
369 | 318 |
|
17181 | 319 |
collection from:position + 1 to:final put:anObject. |
320 |
position := position + count. |
|
321 |
(position >= readLimit) ifTrue:[readLimit := position]. |
|
12131 | 322 |
"/ ^ anObject -- return self |
16793 | 323 |
|
324 |
" |
|
325 |
'' writeStream next:10 put:$* |
|
326 |
" |
|
611 | 327 |
! |
1 | 328 |
|
5198
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
329 |
next:n putAll:aCollection startingAt:pos1 |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
330 |
"append some elements of the argument, aCollection to the stream." |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
331 |
|
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
332 |
^ self nextPutAll:aCollection startingAt:pos1 to:pos1+n-1 |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
333 |
|
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
334 |
" |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
335 |
|s| |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
336 |
|
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
337 |
s := '' writeStream. |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
338 |
s nextPutAll:'hello '. |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
339 |
s next:5 putAll:'1234world012345' startingAt:5. |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
340 |
s contents |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
341 |
" |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
342 |
|
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
343 |
"Modified: 12.7.1996 / 10:31:36 / cg" |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
344 |
! |
ca2c2f7ec69c
added #next:putAll:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4406
diff
changeset
|
345 |
|
1 | 346 |
nextPut:anObject |
1584 | 347 |
"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
|
348 |
Specially tuned for appending to String, ByteArray and Array streams." |
1 | 349 |
|
63 | 350 |
%{ /* NOCONTEXT */ |
351 |
||
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
352 |
#ifndef NO_PRIM_STREAM |
63 | 353 |
REGISTER int pos; |
354 |
unsigned ch; |
|
355 |
OBJ coll; |
|
2883 | 356 |
OBJ p, wL, rL; |
1584 | 357 |
int __readLimit = -1; |
63 | 358 |
|
1136 | 359 |
coll = __INST(collection); |
360 |
p = __INST(position); |
|
63 | 361 |
|
293 | 362 |
if (__isNonNilObject(coll) && __isSmallInteger(p)) { |
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
363 |
pos = __intVal(p); |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
364 |
/* make 1-based */ |
15347 | 365 |
pos = pos + 1; |
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
366 |
wL = __INST(writeLimit); |
63 | 367 |
|
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
368 |
if ((wL == nil) |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
369 |
|| (__isSmallInteger(wL) && (pos <= __intVal(wL)))) { |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
370 |
OBJ cls; |
252 | 371 |
|
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
372 |
cls = __qClass(coll); |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6871
diff
changeset
|
373 |
|
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
374 |
rL = __INST(readLimit); |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
375 |
if (__isSmallInteger(rL)) { |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
376 |
__readLimit = __intVal(rL); |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
377 |
} |
293 | 378 |
|
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
379 |
if (cls == @global(String)) { |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
380 |
if (__isCharacter(anObject) |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
381 |
&& ((ch = __intVal(__characterVal(anObject))) <= 255) /* ch is unsigned */ |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
382 |
&& (pos <= __stringSize(coll))) { |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
383 |
__StringInstPtr(coll)->s_element[pos-1] = ch; |
8615 | 384 |
advancePositionAndReturn: ; |
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
385 |
if ((__readLimit >= 0) && (pos >= __readLimit)) { |
8913 | 386 |
__INST(readLimit) = __mkSmallInteger(pos); |
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
387 |
} |
8913 | 388 |
__INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1); |
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
389 |
RETURN ( anObject ); |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
390 |
} |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
391 |
} else if (cls == @global(ByteArray)) { |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
392 |
if (__isSmallInteger(anObject) |
8296 | 393 |
&& ((ch = __intVal(anObject)) <= 0xFF) /* ch is unsigned */ |
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
394 |
&& (pos <= __byteArraySize(coll))) { |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
395 |
__ByteArrayInstPtr(coll)->ba_element[pos-1] = ch; |
8615 | 396 |
goto advancePositionAndReturn; |
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
397 |
} |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
398 |
} else if (cls == @global(Array)) { |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
399 |
if (pos <= __arraySize(coll)) { |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
400 |
__ArrayInstPtr(coll)->a_element[pos-1] = anObject; |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
401 |
__STORE(coll, anObject); |
8615 | 402 |
goto advancePositionAndReturn; |
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
403 |
} |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
404 |
} else if (cls == @global(Unicode16String)) { |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
405 |
if (__isCharacter(anObject) |
8296 | 406 |
&& ((ch = __intVal(__characterVal(anObject))) <= 0xFFFF) /* ch is unsigned */ |
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
407 |
&& (pos <= __unicode16StringSize(coll))) { |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
408 |
__Unicode16StringInstPtr(coll)->s_element[pos-1] = ch; |
8615 | 409 |
goto advancePositionAndReturn; |
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
410 |
} |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
411 |
} else if (cls == @global(Unicode32String)) { |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
412 |
if (__isCharacter(anObject) |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
413 |
&& (pos <= __unicode32StringSize(coll))) { |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
414 |
__Unicode32StringInstPtr(coll)->s_element[pos-1] = __intVal(__characterVal(anObject)); |
8615 | 415 |
goto advancePositionAndReturn; |
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
416 |
} |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
417 |
} |
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
418 |
} |
63 | 419 |
} |
2894 | 420 |
#endif |
260 | 421 |
%}. |
293 | 422 |
(writeLimit isNil |
15347 | 423 |
or:[(position + 1) <= writeLimit]) ifTrue:[ |
15354 | 424 |
(position >= collection size) ifTrue:[self growCollection]. |
15347 | 425 |
collection at:(position + 1) put:anObject. |
15354 | 426 |
(position >= readLimit) ifTrue:[readLimit := (position + 1)]. |
8293
dcffeb2c11e0
Optimize #nextPut: on UnicodeStrings
Stefan Vogel <sv@exept.de>
parents:
7686
diff
changeset
|
427 |
position := position + 1. |
838 | 428 |
] ifFalse:[ |
8320
229adaeaf183
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
8296
diff
changeset
|
429 |
WriteError raiseErrorString:'write beyond writeLimit' |
293 | 430 |
]. |
1 | 431 |
^anObject |
432 |
! |
|
433 |
||
434 |
nextPutAll:aCollection |
|
63 | 435 |
"append all elements of the argument, aCollection to the stream. |
436 |
Redefined to avoid count grows of the underlying collection - |
|
437 |
instead a single grow on the final size is performed." |
|
1 | 438 |
|
17181 | 439 |
|nMore "{ Class: SmallInteger }" |
440 |
final "{ Class: SmallInteger }" | |
|
1 | 441 |
|
17305 | 442 |
(collection isNil or:[aCollection isSequenceable not]) ifTrue:[ |
17208 | 443 |
"/ fallback |
13628 | 444 |
super nextPutAll:aCollection. |
445 |
^ self. |
|
446 |
]. |
|
77 | 447 |
|
1 | 448 |
nMore := aCollection size. |
9499 | 449 |
nMore == 0 ifTrue:[ |
450 |
"/ for the programmer.. |
|
451 |
aCollection isCollection ifFalse:[ |
|
10070 | 452 |
self error:'invalid argument (not a collection)' mayProceed:true |
9499 | 453 |
]. |
454 |
]. |
|
455 |
||
17181 | 456 |
final := position + nMore. |
293 | 457 |
(writeLimit notNil |
458 |
and:[final > writeLimit]) ifTrue:[ |
|
7686
290cda6075ac
String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents:
7620
diff
changeset
|
459 |
final := writeLimit. |
17181 | 460 |
nMore := final - position |
293 | 461 |
]. |
1 | 462 |
(final > collection size) ifTrue:[ |
7686
290cda6075ac
String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents:
7620
diff
changeset
|
463 |
self growCollection:final |
1 | 464 |
]. |
7686
290cda6075ac
String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents:
7620
diff
changeset
|
465 |
collection |
17181 | 466 |
replaceFrom:(position + 1) |
7686
290cda6075ac
String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents:
7620
diff
changeset
|
467 |
to:final |
290cda6075ac
String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents:
7620
diff
changeset
|
468 |
with:aCollection |
290cda6075ac
String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents:
7620
diff
changeset
|
469 |
startingAt:1. |
1 | 470 |
|
17181 | 471 |
(position >= readLimit) ifTrue:[readLimit := position]. |
472 |
position := position + nMore. |
|
12130 | 473 |
"/ ^ aCollection -- self |
13628 | 474 |
|
475 |
"Modified: / 04-09-2011 / 20:03:32 / cg" |
|
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
476 |
! |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
477 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
478 |
nextPutAll:aCollection startingAt:pos1 to:pos2 |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
479 |
"append some elements of the argument, aCollection to the stream. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
480 |
Redefined to avoid count grows of the underlying collection - |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
481 |
instead a single grow on the final size is performed." |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
482 |
|
17181 | 483 |
|nMore final| |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
484 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
485 |
collection isNil ifTrue:[ |
12131 | 486 |
^ super nextPutAll:aCollection startingAt:pos1 to:pos2 |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
487 |
]. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
488 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
489 |
nMore := pos2 - pos1 + 1. |
17181 | 490 |
final := position + nMore. |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
491 |
(writeLimit notNil |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
492 |
and:[final > writeLimit]) ifTrue:[ |
12131 | 493 |
final := writeLimit. |
17181 | 494 |
nMore := final - position |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
495 |
]. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
496 |
(final > collection size) ifTrue:[ |
12131 | 497 |
self growCollection:final |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
498 |
]. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
499 |
|
17181 | 500 |
collection replaceFrom:position + 1 |
12131 | 501 |
to:final |
502 |
with:aCollection |
|
503 |
startingAt:pos1. |
|
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
504 |
|
17181 | 505 |
position := position + nMore. |
506 |
(position >= readLimit) ifTrue:[readLimit := position]. |
|
12131 | 507 |
"/ ^ aCollection -- return self |
1539
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
508 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
509 |
" |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
510 |
|s| |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
511 |
|
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
512 |
s := '' writeStream. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
513 |
s nextPutAll:'hello '. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
514 |
s nextPutAll:'1234world012345' startingAt:5 to:9. |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
515 |
s contents |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
516 |
" |
b1ac096a81e3
added #nextPutAll:startingAt:to:
Claus Gittinger <cg@exept.de>
parents:
1405
diff
changeset
|
517 |
|
1541 | 518 |
"Modified: 12.7.1996 / 10:31:36 / cg" |
8361 | 519 |
! |
520 |
||
11324 | 521 |
nextPutAllUnicode:aString |
522 |
"normal streams can not handle multi-byte characters, so convert them to utf8" |
|
523 |
||
15653 | 524 |
"this code is not perfect if you use both #nextPutAll: and #nextPutAllUnicode: |
525 |
with the same stream, since 8-bit characters (with the highest bits set) |
|
526 |
are not stored as UTF, so we get some inconsistent string" |
|
527 |
||
528 |
collection isString ifTrue:[ |
|
529 |
collection bitsPerCharacter == 16 ifTrue:[ |
|
530 |
self nextPutAllUtf16:aString. |
|
531 |
] ifFalse:[ |
|
532 |
self nextPutAllUtf8:aString. |
|
11324 | 533 |
]. |
534 |
] ifFalse:[ |
|
535 |
self nextPutAll:aString |
|
536 |
]. |
|
13738
e11fccebb0c3
changed: #nextPutAllUnicode:
Claus Gittinger <cg@exept.de>
parents:
13628
diff
changeset
|
537 |
|
e11fccebb0c3
changed: #nextPutAllUnicode:
Claus Gittinger <cg@exept.de>
parents:
13628
diff
changeset
|
538 |
"Modified: / 28-09-2011 / 16:15:52 / cg" |
11324 | 539 |
! |
540 |
||
8615 | 541 |
nextPutByte:anObject |
542 |
"append the argument, anObject to the stream. |
|
543 |
Specially tuned for appending to String and ByteArray streams." |
|
544 |
||
545 |
%{ /* NOCONTEXT */ |
|
546 |
||
547 |
#ifndef NO_PRIM_STREAM |
|
548 |
REGISTER int pos; |
|
549 |
OBJ coll; |
|
550 |
OBJ p, wL, rL; |
|
551 |
int __readLimit = -1; |
|
552 |
||
553 |
coll = __INST(collection); |
|
554 |
p = __INST(position); |
|
555 |
||
556 |
if (__isNonNilObject(coll) && __isSmallInteger(p) && __isSmallInteger(anObject)) { |
|
557 |
unsigned ch; |
|
558 |
||
559 |
ch = __intVal(anObject); |
|
560 |
||
561 |
pos = __intVal(p); |
|
562 |
/* make 1-based */ |
|
563 |
pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition)); |
|
564 |
wL = __INST(writeLimit); |
|
565 |
||
566 |
if ((wL == nil) |
|
567 |
|| (__isSmallInteger(wL) && (pos <= __intVal(wL)))) { |
|
568 |
OBJ cls; |
|
569 |
||
570 |
cls = __qClass(coll); |
|
571 |
||
572 |
rL = __INST(readLimit); |
|
573 |
if (__isSmallInteger(rL)) { |
|
574 |
__readLimit = __intVal(rL); |
|
575 |
} |
|
576 |
||
577 |
if (cls == @global(String)) { |
|
578 |
if ((pos <= __stringSize(coll)) |
|
579 |
&& (ch <= 0xFF)) { /* ch is unsigned */ |
|
580 |
__StringInstPtr(coll)->s_element[pos-1] = ch; |
|
581 |
advancePositionAndReturn: ; |
|
582 |
if ((__readLimit >= 0) && (pos >= __readLimit)) { |
|
8913 | 583 |
__INST(readLimit) = __mkSmallInteger(pos); |
8615 | 584 |
} |
8913 | 585 |
__INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1); |
8615 | 586 |
RETURN ( anObject ); |
587 |
} |
|
588 |
} else if (cls == @global(ByteArray)) { |
|
589 |
if ((pos <= __byteArraySize(coll)) |
|
590 |
&& (ch <= 0xFF)) { /* ch is unsigned */ |
|
591 |
__ByteArrayInstPtr(coll)->ba_element[pos-1] = ch; |
|
592 |
goto advancePositionAndReturn; |
|
593 |
} |
|
594 |
} |
|
595 |
} |
|
596 |
} |
|
597 |
#endif |
|
598 |
%}. |
|
599 |
^ super nextPutByte:anObject |
|
600 |
! |
|
601 |
||
8361 | 602 |
nextPutBytes:count from:anObject startingAt:start |
603 |
"write count bytes from an object starting at index start. |
|
604 |
Return the number of bytes written. |
|
605 |
The object must have non-pointer indexed instvars |
|
606 |
(i.e. be a ByteArray, String, Float- or DoubleArray). |
|
607 |
Use with care - non object oriented i/o. |
|
608 |
This is provided for compatibility with externalStream; |
|
609 |
to support binary storage" |
|
610 |
||
11794
ad5fcc151a09
#on: - set readLimit to 0, so that setToEnd positions to end of written
Stefan Vogel <sv@exept.de>
parents:
11324
diff
changeset
|
611 |
anObject isByteCollection ifTrue:[ |
8361 | 612 |
self nextPutAll:anObject startingAt:start to:(start + count - 1). |
613 |
^ count. |
|
614 |
]. |
|
615 |
^ super nextPutBytes:count from:anObject startingAt:start |
|
11324 | 616 |
! |
617 |
||
618 |
nextPutUnicode:aCharacter |
|
619 |
"normal streams can not handle multi-byte characters, so convert them to utf8" |
|
620 |
||
15653 | 621 |
"this code is not perfect if you use both #nextPut: and #nextPutUnicode: |
622 |
with the same stream, since 8-bit characters (with the highest bits set) |
|
623 |
are not stored as UTF, so we get some inconsistent string" |
|
624 |
||
625 |
collection isString ifTrue:[ |
|
626 |
collection bitsPerCharacter == 16 ifTrue:[ |
|
627 |
self nextPutUtf16:aCharacter. |
|
628 |
] ifFalse:[ |
|
629 |
self nextPutUtf8:aCharacter. |
|
630 |
]. |
|
11324 | 631 |
] ifFalse:[ |
632 |
self nextPut:aCharacter. |
|
633 |
]. |
|
1 | 634 |
! ! |
635 |
||
1965 | 636 |
!WriteStream class methodsFor:'documentation'! |
701 | 637 |
|
638 |
version |
|
17617 | 639 |
^ '$Header: /cvs/stx/stx/libbasic/WriteStream.st,v 1.94 2015-03-13 11:27:34 stefan Exp $' |
12130 | 640 |
! |
641 |
||
642 |
version_CVS |
|
17617 | 643 |
^ '$Header: /cvs/stx/stx/libbasic/WriteStream.st,v 1.94 2015-03-13 11:27:34 stefan Exp $' |
701 | 644 |
! ! |
14935 | 645 |