author | Claus Gittinger <cg@exept.de> |
Mon, 16 Dec 2019 11:42:04 +0100 | |
changeset 25127 | 22ff3c08a076 |
parent 25118 | b375fd4078c2 |
permissions | -rw-r--r-- |
25118 | 1 |
"{ Encoding: utf8 }" |
2 |
||
10846 | 3 |
" |
25118 | 4 |
COPYRIGHT (c) 2004 by eXept Software AG |
10846 | 5 |
All Rights Reserved |
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 |
" |
|
8299 | 14 |
"{ Package: 'stx:libbasic' }" |
8298 | 15 |
|
17622 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
8298 | 18 |
WriteStream subclass:#CharacterWriteStream |
19 |
instanceVariableNames:'currentCharacterSize' |
|
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
22 |
category:'Streams' |
|
23 |
! |
|
24 |
||
8321 | 25 |
!CharacterWriteStream class methodsFor:'documentation'! |
26 |
||
10846 | 27 |
copyright |
28 |
" |
|
25118 | 29 |
COPYRIGHT (c) 2004 by eXept Software AG |
10846 | 30 |
All Rights Reserved |
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 |
||
8321 | 41 |
documentation |
42 |
" |
|
43 |
This is a WriteStream, which automagically changes the underlying collection, |
|
18795 | 44 |
if a character does not fit into the current collection |
18796 | 45 |
(i.e. String -> Unicode16String -> Unicode32Sting ) |
8321 | 46 |
|
47 |
[author:] |
|
48 |
Stefan Vogel (stefan@zwerg) |
|
49 |
||
50 |
[instance variables:] |
|
51 |
||
52 |
[class variables:] |
|
53 |
||
54 |
[see also:] |
|
10280 | 55 |
String Unicode16String Unicode32Sting |
8321 | 56 |
" |
57 |
! |
|
58 |
||
59 |
examples |
|
60 |
" |
|
61 |
||
62 |
[exBegin] |
|
63 |
|stream| |
|
64 |
||
65 |
stream := CharacterWriteStream on:(String new:32). |
|
66 |
stream nextPutAll:'abc'. |
|
67 |
stream nextPut:(Character value:16r2c00). |
|
68 |
stream contents inspect |
|
69 |
[exEnd] |
|
70 |
||
71 |
" |
|
72 |
! ! |
|
73 |
||
18265 | 74 |
!CharacterWriteStream class methodsFor:'instance creation'! |
75 |
||
76 |
new |
|
77 |
"I know, that I operate on strings" |
|
78 |
||
18282 | 79 |
^ self on:(String uninitializedNew:10) |
18265 | 80 |
! |
81 |
||
82 |
new:count |
|
83 |
"I know, that I operate on strings" |
|
84 |
||
18282 | 85 |
^ self on:(String uninitializedNew:count) |
18265 | 86 |
! ! |
87 |
||
14607 | 88 |
!CharacterWriteStream methodsFor:'accessing'! |
89 |
||
90 |
reset |
|
19647 | 91 |
"reset the stream, to write again over the existing (or already written) contents. |
14607 | 92 |
See the comment in WriteStream>>contents" |
93 |
||
19886 | 94 |
"start with 8-bit String again" |
14607 | 95 |
collection := String new:collection size. |
96 |
currentCharacterSize := collection bitsPerCharacter. |
|
19886 | 97 |
position := 0. |
14607 | 98 |
! ! |
99 |
||
8321 | 100 |
!CharacterWriteStream methodsFor:'private'! |
101 |
||
18598 | 102 |
characterSizeChangedTo:newCharacterSize size:additionalSize |
103 |
"change aCollection to fit the size of newCharacterSize" |
|
8321 | 104 |
|
18598 | 105 |
|sz newSz| |
8321 | 106 |
|
18598 | 107 |
currentCharacterSize < newCharacterSize ifTrue:[ |
108 |
newSz := sz := collection size. |
|
17622 | 109 |
position + additionalSize >= sz ifTrue:[ |
18598 | 110 |
newSz := newSz + additionalSize + 100. "add some more space, maybe more will be added" |
8321 | 111 |
]. |
18598 | 112 |
collection := ((CharacterArray speciesForCharacterSize:newCharacterSize) new:newSz) |
18613 | 113 |
replaceFrom:1 to:position with:collection startingAt:1. |
18598 | 114 |
currentCharacterSize := collection bitsPerCharacter. |
8321 | 115 |
]. |
116 |
! ! |
|
117 |
||
118 |
!CharacterWriteStream methodsFor:'private-accessing'! |
|
119 |
||
120 |
on:aCollection |
|
19647 | 121 |
"return a stream for writing into aCollection" |
122 |
||
8321 | 123 |
currentCharacterSize := aCollection bitsPerCharacter. |
124 |
^ super on:aCollection. |
|
125 |
! |
|
126 |
||
127 |
on:aCollection from:start to:stop |
|
19647 | 128 |
"return a stream for writing into part of aCollection. |
129 |
This will position the stream to start writing at start-index, |
|
130 |
and setup a writeLimit at stop-index. |
|
131 |
Contents after stop-index will not be overwritten." |
|
132 |
||
8321 | 133 |
currentCharacterSize := aCollection bitsPerCharacter. |
134 |
^ super on:aCollection from:start to:stop. |
|
19647 | 135 |
|
136 |
"notice: only the first 6 characters are overwritten: |
|
137 |
||
138 |
|str s| |
|
139 |
str := 'hello world bla'. |
|
140 |
s := CharacterWriteStream on:str from:6 to:11. |
|
141 |
s nextPutAll:'1234567890'. |
|
142 |
str |
|
143 |
" |
|
8321 | 144 |
! |
145 |
||
146 |
with:aCollection |
|
19647 | 147 |
"return a stream for writing into aCollection. |
148 |
This will position the stream to the end, and append written elements |
|
149 |
after the initial contents. |
|
150 |
I.e. the effect is similar to creating an empty stream first and then write |
|
151 |
aCollection." |
|
152 |
||
8321 | 153 |
currentCharacterSize := aCollection bitsPerCharacter. |
154 |
^ super with:aCollection. |
|
19647 | 155 |
|
156 |
" |
|
157 |
|s| |
|
158 |
s := CharacterWriteStream with:'hello'. |
|
159 |
s nextPutAll:'1234567890'. |
|
160 |
s contents |
|
161 |
" |
|
8321 | 162 |
! ! |
163 |
||
8298 | 164 |
!CharacterWriteStream methodsFor:'writing'! |
165 |
||
8321 | 166 |
next:count put:aCharacter |
167 |
"append anObject count times to the receiver. |
|
168 |
Redefined to avoid count grows of the underlying collection - |
|
169 |
instead a single grow on the final size is performed." |
|
170 |
||
18598 | 171 |
|needCharacterSize| |
172 |
||
173 |
needCharacterSize := aCharacter characterSize. |
|
174 |
needCharacterSize > currentCharacterSize ifTrue:[ |
|
175 |
self characterSizeChangedTo:needCharacterSize size:count. |
|
8321 | 176 |
]. |
12133 | 177 |
super next:count put:aCharacter |
8321 | 178 |
! |
179 |
||
180 |
nextPut:aCharacter |
|
181 |
"append the argument, aCharacter to the stream. |
|
24648 | 182 |
Answer aCharacter. |
18598 | 183 |
Specially tuned for appending to String, Unicode16String and Unicode32String streams." |
8298 | 184 |
|
185 |
%{ /* NOCONTEXT */ |
|
186 |
||
187 |
#ifndef NO_PRIM_STREAM |
|
18598 | 188 |
OBJ coll = __INST(collection); |
189 |
OBJ p = __INST(position); |
|
8298 | 190 |
|
8321 | 191 |
if (__isNonNilObject(coll) && __isSmallInteger(p) && __isCharacter(aCharacter)) { |
18598 | 192 |
REGISTER int pos = __intVal(p) + 1; /* make 1-based */ |
193 |
OBJ wL = __INST(writeLimit); |
|
8298 | 194 |
|
195 |
if ((wL == nil) |
|
196 |
|| (__isSmallInteger(wL) && (pos <= __intVal(wL)))) { |
|
18598 | 197 |
OBJ cls = __qClass(coll); |
198 |
unsigned ch = __intVal(__characterVal(aCharacter)); |
|
199 |
OBJ rL = __INST(readLimit); |
|
200 |
int __readLimit = -1; |
|
8298 | 201 |
|
202 |
if (__isSmallInteger(rL)) { |
|
203 |
__readLimit = __intVal(rL); |
|
204 |
} |
|
205 |
||
206 |
if (cls == @global(String)) { |
|
207 |
if (ch > 0xFF) { |
|
208 |
goto resize; |
|
209 |
} |
|
210 |
if (pos <= __stringSize(coll)) { |
|
211 |
__StringInstPtr(coll)->s_element[pos-1] = ch; |
|
212 |
if ((__readLimit >= 0) && (pos >= __readLimit)) { |
|
8913 | 213 |
__INST(readLimit) = __mkSmallInteger(pos); |
8298 | 214 |
} |
8913 | 215 |
__INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1); |
24648 | 216 |
RETURN ( aCharacter ); |
8298 | 217 |
} |
218 |
} else if (cls == @global(Unicode16String)) { |
|
219 |
if (ch > 0xFFFF) { |
|
220 |
goto resize; |
|
221 |
} |
|
222 |
if (pos <= __unicode16StringSize(coll)) { |
|
223 |
__Unicode16StringInstPtr(coll)->s_element[pos-1] = ch; |
|
224 |
if ((__readLimit >= 0) && (pos >= __readLimit)) { |
|
8913 | 225 |
__INST(readLimit) = __mkSmallInteger(pos); |
8298 | 226 |
} |
8913 | 227 |
__INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1); |
24648 | 228 |
RETURN ( aCharacter ); |
8298 | 229 |
} |
230 |
} else if (cls == @global(Unicode32String)) { |
|
231 |
if ((pos <= __unicode32StringSize(coll))) { |
|
232 |
__Unicode32StringInstPtr(coll)->s_element[pos-1] = ch; |
|
233 |
if ((__readLimit >= 0) && (pos >= __readLimit)) { |
|
8913 | 234 |
__INST(readLimit) = __mkSmallInteger(pos); |
8298 | 235 |
} |
8913 | 236 |
__INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1); |
24648 | 237 |
RETURN ( aCharacter ); |
8298 | 238 |
} |
239 |
} |
|
240 |
} |
|
241 |
} |
|
242 |
resize:; |
|
243 |
#endif |
|
244 |
%}. |
|
245 |
||
246 |
(writeLimit isNil |
|
15349
7c2c460c5feb
class: CharacterWriteStream
Claus Gittinger <cg@exept.de>
parents:
14607
diff
changeset
|
247 |
or:[(position + 1) <= writeLimit]) ifTrue:[ |
18598 | 248 |
|needCharacterSize| |
249 |
||
250 |
needCharacterSize := aCharacter characterSize. |
|
251 |
needCharacterSize > currentCharacterSize ifTrue:[ |
|
252 |
self characterSizeChangedTo:needCharacterSize size:1. |
|
253 |
] ifFalse:[ |
|
254 |
(position >= collection size) ifTrue:[self growCollection]. |
|
8321 | 255 |
]. |
15349
7c2c460c5feb
class: CharacterWriteStream
Claus Gittinger <cg@exept.de>
parents:
14607
diff
changeset
|
256 |
collection at:(position + 1) put:aCharacter. |
15358
6b69ebd9785c
class: CharacterWriteStream
Claus Gittinger <cg@exept.de>
parents:
15349
diff
changeset
|
257 |
(position >= readLimit) ifTrue:[readLimit := (position + 1)]. |
8298 | 258 |
position := position + 1. |
259 |
] ifFalse:[ |
|
8321 | 260 |
WriteError raiseErrorString:'write beyond writeLimit' |
261 |
]. |
|
24648 | 262 |
^ aCharacter |
8321 | 263 |
! |
264 |
||
265 |
nextPutAll:aCollection |
|
11319 | 266 |
"append aCollection to the receiver. |
24642 | 267 |
Answer the receiver. |
22419 | 268 |
Redefined to convert to a string of the needed character size." |
8321 | 269 |
|
18598 | 270 |
|needCharacterSize| |
271 |
||
272 |
needCharacterSize := aCollection characterSize. |
|
273 |
needCharacterSize > currentCharacterSize ifTrue:[ |
|
274 |
self characterSizeChangedTo:needCharacterSize size:aCollection size. |
|
8298 | 275 |
]. |
12133 | 276 |
super nextPutAll:aCollection |
22419 | 277 |
|
278 |
"Modified (comment): / 15-01-2018 / 08:29:39 / mawalch" |
|
8321 | 279 |
! |
280 |
||
281 |
nextPutAll:aCollection startingAt:start to:stop |
|
24029 | 282 |
"append the elements from first index to last index |
283 |
of the argument, aCollection onto the receiver (i.e. both outstreams)" |
|
284 |
||
18598 | 285 |
|needCharacterSize| |
286 |
||
22675 | 287 |
aCollection isCharacters ifFalse:[ |
288 |
aCollection isByteArray ifTrue:[ |
|
23541 | 289 |
super nextPutAll:aCollection startingAt:start to:stop |
22675 | 290 |
] |
291 |
]. |
|
292 |
||
18598 | 293 |
needCharacterSize := aCollection characterSize. |
294 |
needCharacterSize > currentCharacterSize ifTrue:[ |
|
295 |
self characterSizeChangedTo:needCharacterSize size:stop-start+1. |
|
8321 | 296 |
]. |
23541 | 297 |
super nextPutAll:aCollection startingAt:start to:stop |
298 |
||
299 |
"Modified: / 22-11-2018 / 12:53:10 / Stefan Vogel" |
|
24029 | 300 |
"Modified (comment): / 01-04-2019 / 17:13:06 / Claus Gittinger" |
11319 | 301 |
! |
302 |
||
303 |
nextPutAllUnicode:aCollection |
|
24630 | 304 |
self nextPutAll:aCollection |
11319 | 305 |
! |
306 |
||
307 |
nextPutUnicode:aCharacter |
|
24630 | 308 |
self nextPut:aCharacter |
8298 | 309 |
! ! |
310 |
||
8321 | 311 |
!CharacterWriteStream class methodsFor:'documentation'! |
312 |
||
313 |
version |
|
18590 | 314 |
^ '$Header$' |
12133 | 315 |
! |
316 |
||
317 |
version_CVS |
|
18590 | 318 |
^ '$Header$' |
8321 | 319 |
! ! |
14607 | 320 |