8299
|
1 |
"{ Package: 'stx:libbasic' }"
|
8298
|
2 |
|
|
3 |
WriteStream subclass:#CharacterWriteStream
|
|
4 |
instanceVariableNames:'currentCharacterSize'
|
|
5 |
classVariableNames:''
|
|
6 |
poolDictionaries:''
|
|
7 |
category:'Streams'
|
|
8 |
!
|
|
9 |
|
8321
|
10 |
!CharacterWriteStream class methodsFor:'documentation'!
|
|
11 |
|
|
12 |
documentation
|
|
13 |
"
|
|
14 |
This is a WriteStream, which automagically changes the underlying collection,
|
10280
|
15 |
if a character does fit into the current collection.
|
8321
|
16 |
|
|
17 |
String -> Unicode16String -> Unicode32Sting
|
|
18 |
|
|
19 |
[author:]
|
|
20 |
Stefan Vogel (stefan@zwerg)
|
|
21 |
|
|
22 |
[instance variables:]
|
|
23 |
|
|
24 |
[class variables:]
|
|
25 |
|
|
26 |
[see also:]
|
10280
|
27 |
String Unicode16String Unicode32Sting
|
8321
|
28 |
"
|
|
29 |
!
|
|
30 |
|
|
31 |
examples
|
|
32 |
"
|
|
33 |
|
|
34 |
[exBegin]
|
|
35 |
|stream|
|
|
36 |
|
|
37 |
stream := CharacterWriteStream on:(String new:32).
|
|
38 |
stream nextPutAll:'abc'.
|
|
39 |
stream nextPut:(Character value:16r2c00).
|
|
40 |
stream contents inspect
|
|
41 |
[exEnd]
|
|
42 |
|
|
43 |
"
|
|
44 |
! !
|
|
45 |
|
|
46 |
!CharacterWriteStream methodsFor:'private'!
|
|
47 |
|
|
48 |
characterSizeChanged:aCharacterOrString
|
|
49 |
"change aCollection to fit the size of aCharacter"
|
|
50 |
|
|
51 |
|sz newSz bitsPerCharacter|
|
|
52 |
|
|
53 |
bitsPerCharacter := aCharacterOrString bitsPerCharacter.
|
|
54 |
currentCharacterSize < bitsPerCharacter ifTrue:[
|
|
55 |
sz := collection size.
|
|
56 |
(position + 1 - ZeroPosition) > sz ifTrue:[
|
|
57 |
newSz := sz + 1.
|
|
58 |
] ifFalse:[
|
|
59 |
newSz := sz.
|
|
60 |
].
|
|
61 |
collection := (aCharacterOrString stringSpecies new:newSz)
|
|
62 |
replaceFrom:1 to:sz with:collection startingAt:1.
|
|
63 |
currentCharacterSize := bitsPerCharacter.
|
|
64 |
].
|
|
65 |
! !
|
|
66 |
|
|
67 |
!CharacterWriteStream methodsFor:'private-accessing'!
|
|
68 |
|
|
69 |
on:aCollection
|
|
70 |
|
|
71 |
currentCharacterSize := aCollection bitsPerCharacter.
|
|
72 |
^ super on:aCollection.
|
|
73 |
!
|
|
74 |
|
|
75 |
on:aCollection from:start to:stop
|
|
76 |
|
|
77 |
currentCharacterSize := aCollection bitsPerCharacter.
|
|
78 |
^ super on:aCollection from:start to:stop.
|
|
79 |
!
|
|
80 |
|
|
81 |
with:aCollection
|
|
82 |
|
|
83 |
currentCharacterSize := aCollection bitsPerCharacter.
|
|
84 |
^ super with:aCollection.
|
|
85 |
! !
|
|
86 |
|
8298
|
87 |
!CharacterWriteStream methodsFor:'writing'!
|
|
88 |
|
8321
|
89 |
next:count put:aCharacter
|
|
90 |
"append anObject count times to the receiver.
|
|
91 |
Redefined to avoid count grows of the underlying collection -
|
|
92 |
instead a single grow on the final size is performed."
|
|
93 |
|
|
94 |
|
|
95 |
aCharacter bitsPerCharacter > currentCharacterSize ifTrue:[
|
|
96 |
self characterSizeChanged:aCharacter.
|
|
97 |
].
|
|
98 |
^ super next:count put:aCharacter
|
|
99 |
!
|
|
100 |
|
|
101 |
nextPut:aCharacter
|
|
102 |
"append the argument, aCharacter to the stream.
|
8298
|
103 |
Specially tuned for appending to String, ByteArray and Array streams."
|
|
104 |
|
|
105 |
%{ /* NOCONTEXT */
|
|
106 |
|
|
107 |
#ifndef NO_PRIM_STREAM
|
|
108 |
REGISTER int pos;
|
|
109 |
unsigned ch;
|
|
110 |
OBJ coll;
|
|
111 |
OBJ p, wL, rL;
|
|
112 |
int __readLimit = -1;
|
|
113 |
|
|
114 |
coll = __INST(collection);
|
|
115 |
p = __INST(position);
|
|
116 |
|
8321
|
117 |
if (__isNonNilObject(coll) && __isSmallInteger(p) && __isCharacter(aCharacter)) {
|
8298
|
118 |
pos = __intVal(p);
|
|
119 |
/* make 1-based */
|
|
120 |
pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition));
|
|
121 |
wL = __INST(writeLimit);
|
|
122 |
|
|
123 |
if ((wL == nil)
|
|
124 |
|| (__isSmallInteger(wL) && (pos <= __intVal(wL)))) {
|
|
125 |
OBJ cls;
|
|
126 |
|
|
127 |
cls = __qClass(coll);
|
8321
|
128 |
ch = __intVal(__characterVal(aCharacter));
|
8298
|
129 |
|
|
130 |
rL = __INST(readLimit);
|
|
131 |
if (__isSmallInteger(rL)) {
|
|
132 |
__readLimit = __intVal(rL);
|
|
133 |
}
|
|
134 |
|
|
135 |
if (cls == @global(String)) {
|
|
136 |
if (ch > 0xFF) {
|
|
137 |
goto resize;
|
|
138 |
}
|
|
139 |
if (pos <= __stringSize(coll)) {
|
|
140 |
__StringInstPtr(coll)->s_element[pos-1] = ch;
|
|
141 |
if ((__readLimit >= 0) && (pos >= __readLimit)) {
|
8913
|
142 |
__INST(readLimit) = __mkSmallInteger(pos);
|
8298
|
143 |
}
|
8913
|
144 |
__INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1);
|
8321
|
145 |
RETURN ( aCharacter );
|
8298
|
146 |
}
|
|
147 |
} else if (cls == @global(Unicode16String)) {
|
|
148 |
if (ch > 0xFFFF) {
|
|
149 |
goto resize;
|
|
150 |
}
|
|
151 |
if (pos <= __unicode16StringSize(coll)) {
|
|
152 |
__Unicode16StringInstPtr(coll)->s_element[pos-1] = ch;
|
|
153 |
if ((__readLimit >= 0) && (pos >= __readLimit)) {
|
8913
|
154 |
__INST(readLimit) = __mkSmallInteger(pos);
|
8298
|
155 |
}
|
8913
|
156 |
__INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1);
|
8321
|
157 |
RETURN ( aCharacter );
|
8298
|
158 |
}
|
|
159 |
} else if (cls == @global(Unicode32String)) {
|
|
160 |
if ((pos <= __unicode32StringSize(coll))) {
|
|
161 |
__Unicode32StringInstPtr(coll)->s_element[pos-1] = ch;
|
|
162 |
if ((__readLimit >= 0) && (pos >= __readLimit)) {
|
8913
|
163 |
__INST(readLimit) = __mkSmallInteger(pos);
|
8298
|
164 |
}
|
8913
|
165 |
__INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1);
|
8321
|
166 |
RETURN ( aCharacter );
|
8298
|
167 |
}
|
|
168 |
}
|
|
169 |
}
|
|
170 |
}
|
|
171 |
resize:;
|
|
172 |
#endif
|
|
173 |
%}.
|
|
174 |
|
8321
|
175 |
|
8298
|
176 |
(writeLimit isNil
|
8321
|
177 |
or:[(position + 1 - ZeroPosition) <= writeLimit]) ifTrue:[
|
|
178 |
currentCharacterSize < aCharacter bitsPerCharacter ifTrue:[
|
|
179 |
self characterSizeChanged:aCharacter
|
|
180 |
].
|
8298
|
181 |
((position + 1 - ZeroPosition) > collection size) ifTrue:[self growCollection].
|
8321
|
182 |
collection at:(position + 1 - ZeroPosition) put:aCharacter.
|
8298
|
183 |
((position + 1 - ZeroPosition) > readLimit) ifTrue:[readLimit := (position + 1 - ZeroPosition)].
|
|
184 |
position := position + 1.
|
|
185 |
] ifFalse:[
|
8321
|
186 |
WriteError raiseErrorString:'write beyond writeLimit'
|
|
187 |
].
|
|
188 |
^ aCharacter
|
|
189 |
!
|
|
190 |
|
|
191 |
nextPutAll:aCollection
|
|
192 |
"append anObject count times to the receiver.
|
|
193 |
Redefined to avoid count grows of the underlying collection -
|
|
194 |
instead a single grow on the final size is performed."
|
|
195 |
|
|
196 |
|
|
197 |
aCollection bitsPerCharacter > currentCharacterSize ifTrue:[
|
|
198 |
self characterSizeChanged:aCollection.
|
8298
|
199 |
].
|
8321
|
200 |
^ super nextPutAll:aCollection
|
|
201 |
!
|
|
202 |
|
|
203 |
nextPutAll:aCollection startingAt:start to:stop
|
|
204 |
"append anObject count times to the receiver.
|
|
205 |
Redefined to avoid count grows of the underlying collection -
|
|
206 |
instead a single grow on the final size is performed."
|
|
207 |
|
|
208 |
|
|
209 |
aCollection bitsPerCharacter > currentCharacterSize ifTrue:[
|
|
210 |
self characterSizeChanged:aCollection.
|
|
211 |
].
|
|
212 |
^ super nextPutAll:aCollection startingAt:start to:stop
|
8298
|
213 |
! !
|
|
214 |
|
8321
|
215 |
!CharacterWriteStream class methodsFor:'documentation'!
|
|
216 |
|
|
217 |
version
|
10282
|
218 |
^ '$Header: /cvs/stx/stx/libbasic/CharacterWriteStream.st,v 1.6 2006-12-21 13:18:28 cg Exp $'
|
8321
|
219 |
! !
|