author | Stefan Vogel <sv@exept.de> |
Thu, 16 Apr 2015 18:28:45 +0200 | |
branch | expecco_2_7_5 |
changeset 18220 | 361e98951d47 |
parent 15358 | 6b69ebd9785c |
child 17622 | 553ee52df434 |
child 18066 | 89d51443ba6f |
permissions | -rw-r--r-- |
10846 | 1 |
" |
2 |
COPYRIGHT (c) 2005 by eXept Software AG |
|
3 |
All Rights Reserved |
|
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 |
" |
|
8299 | 12 |
"{ Package: 'stx:libbasic' }" |
8298 | 13 |
|
14 |
WriteStream subclass:#CharacterWriteStream |
|
15 |
instanceVariableNames:'currentCharacterSize' |
|
16 |
classVariableNames:'' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'Streams' |
|
19 |
! |
|
20 |
||
8321 | 21 |
!CharacterWriteStream class methodsFor:'documentation'! |
22 |
||
10846 | 23 |
copyright |
24 |
" |
|
25 |
COPYRIGHT (c) 2005 by eXept Software AG |
|
26 |
All Rights Reserved |
|
27 |
||
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
36 |
||
8321 | 37 |
documentation |
38 |
" |
|
39 |
This is a WriteStream, which automagically changes the underlying collection, |
|
10280 | 40 |
if a character does fit into the current collection. |
8321 | 41 |
|
42 |
String -> Unicode16String -> Unicode32Sting |
|
43 |
||
44 |
[author:] |
|
45 |
Stefan Vogel (stefan@zwerg) |
|
46 |
||
47 |
[instance variables:] |
|
48 |
||
49 |
[class variables:] |
|
50 |
||
51 |
[see also:] |
|
10280 | 52 |
String Unicode16String Unicode32Sting |
8321 | 53 |
" |
54 |
! |
|
55 |
||
56 |
examples |
|
57 |
" |
|
58 |
||
59 |
[exBegin] |
|
60 |
|stream| |
|
61 |
||
62 |
stream := CharacterWriteStream on:(String new:32). |
|
63 |
stream nextPutAll:'abc'. |
|
64 |
stream nextPut:(Character value:16r2c00). |
|
65 |
stream contents inspect |
|
66 |
[exEnd] |
|
67 |
||
68 |
" |
|
69 |
! ! |
|
70 |
||
14607 | 71 |
!CharacterWriteStream methodsFor:'accessing'! |
72 |
||
73 |
reset |
|
74 |
"reset the stream; write anew. |
|
75 |
See the comment in WriteStream>>contents" |
|
76 |
||
77 |
collection := String new:collection size. |
|
78 |
currentCharacterSize := collection bitsPerCharacter. |
|
79 |
super resetPosition. |
|
80 |
! ! |
|
81 |
||
8321 | 82 |
!CharacterWriteStream methodsFor:'private'! |
83 |
||
84 |
characterSizeChanged:aCharacterOrString |
|
85 |
"change aCollection to fit the size of aCharacter" |
|
86 |
||
87 |
|sz newSz bitsPerCharacter| |
|
88 |
||
89 |
bitsPerCharacter := aCharacterOrString bitsPerCharacter. |
|
90 |
currentCharacterSize < bitsPerCharacter ifTrue:[ |
|
91 |
sz := collection size. |
|
15358
6b69ebd9785c
class: CharacterWriteStream
Claus Gittinger <cg@exept.de>
parents:
15349
diff
changeset
|
92 |
position >= sz ifTrue:[ |
8321 | 93 |
newSz := sz + 1. |
94 |
] ifFalse:[ |
|
95 |
newSz := sz. |
|
96 |
]. |
|
97 |
collection := (aCharacterOrString stringSpecies new:newSz) |
|
98 |
replaceFrom:1 to:sz with:collection startingAt:1. |
|
99 |
currentCharacterSize := bitsPerCharacter. |
|
100 |
]. |
|
101 |
! ! |
|
102 |
||
103 |
!CharacterWriteStream methodsFor:'private-accessing'! |
|
104 |
||
105 |
on:aCollection |
|
106 |
||
107 |
currentCharacterSize := aCollection bitsPerCharacter. |
|
108 |
^ super on:aCollection. |
|
109 |
! |
|
110 |
||
111 |
on:aCollection from:start to:stop |
|
112 |
||
113 |
currentCharacterSize := aCollection bitsPerCharacter. |
|
114 |
^ super on:aCollection from:start to:stop. |
|
115 |
! |
|
116 |
||
117 |
with:aCollection |
|
118 |
||
119 |
currentCharacterSize := aCollection bitsPerCharacter. |
|
120 |
^ super with:aCollection. |
|
121 |
! ! |
|
122 |
||
8298 | 123 |
!CharacterWriteStream methodsFor:'writing'! |
124 |
||
8321 | 125 |
next:count put:aCharacter |
126 |
"append anObject count times to the receiver. |
|
127 |
Redefined to avoid count grows of the underlying collection - |
|
128 |
instead a single grow on the final size is performed." |
|
129 |
||
130 |
aCharacter bitsPerCharacter > currentCharacterSize ifTrue:[ |
|
131 |
self characterSizeChanged:aCharacter. |
|
132 |
]. |
|
12133 | 133 |
super next:count put:aCharacter |
8321 | 134 |
! |
135 |
||
136 |
nextPut:aCharacter |
|
137 |
"append the argument, aCharacter to the stream. |
|
8298 | 138 |
Specially tuned for appending to String, ByteArray and Array streams." |
139 |
||
140 |
%{ /* NOCONTEXT */ |
|
141 |
||
142 |
#ifndef NO_PRIM_STREAM |
|
143 |
REGISTER int pos; |
|
144 |
unsigned ch; |
|
145 |
OBJ coll; |
|
146 |
OBJ p, wL, rL; |
|
147 |
int __readLimit = -1; |
|
148 |
||
149 |
coll = __INST(collection); |
|
150 |
p = __INST(position); |
|
151 |
||
8321 | 152 |
if (__isNonNilObject(coll) && __isSmallInteger(p) && __isCharacter(aCharacter)) { |
8298 | 153 |
pos = __intVal(p); |
154 |
/* make 1-based */ |
|
15349
7c2c460c5feb
class: CharacterWriteStream
Claus Gittinger <cg@exept.de>
parents:
14607
diff
changeset
|
155 |
pos = pos + 1; |
8298 | 156 |
wL = __INST(writeLimit); |
157 |
||
158 |
if ((wL == nil) |
|
159 |
|| (__isSmallInteger(wL) && (pos <= __intVal(wL)))) { |
|
160 |
OBJ cls; |
|
161 |
||
162 |
cls = __qClass(coll); |
|
8321 | 163 |
ch = __intVal(__characterVal(aCharacter)); |
8298 | 164 |
|
165 |
rL = __INST(readLimit); |
|
166 |
if (__isSmallInteger(rL)) { |
|
167 |
__readLimit = __intVal(rL); |
|
168 |
} |
|
169 |
||
170 |
if (cls == @global(String)) { |
|
171 |
if (ch > 0xFF) { |
|
172 |
goto resize; |
|
173 |
} |
|
174 |
if (pos <= __stringSize(coll)) { |
|
175 |
__StringInstPtr(coll)->s_element[pos-1] = ch; |
|
176 |
if ((__readLimit >= 0) && (pos >= __readLimit)) { |
|
8913 | 177 |
__INST(readLimit) = __mkSmallInteger(pos); |
8298 | 178 |
} |
8913 | 179 |
__INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1); |
8321 | 180 |
RETURN ( aCharacter ); |
8298 | 181 |
} |
182 |
} else if (cls == @global(Unicode16String)) { |
|
183 |
if (ch > 0xFFFF) { |
|
184 |
goto resize; |
|
185 |
} |
|
186 |
if (pos <= __unicode16StringSize(coll)) { |
|
187 |
__Unicode16StringInstPtr(coll)->s_element[pos-1] = ch; |
|
188 |
if ((__readLimit >= 0) && (pos >= __readLimit)) { |
|
8913 | 189 |
__INST(readLimit) = __mkSmallInteger(pos); |
8298 | 190 |
} |
8913 | 191 |
__INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1); |
8321 | 192 |
RETURN ( aCharacter ); |
8298 | 193 |
} |
194 |
} else if (cls == @global(Unicode32String)) { |
|
195 |
if ((pos <= __unicode32StringSize(coll))) { |
|
196 |
__Unicode32StringInstPtr(coll)->s_element[pos-1] = ch; |
|
197 |
if ((__readLimit >= 0) && (pos >= __readLimit)) { |
|
8913 | 198 |
__INST(readLimit) = __mkSmallInteger(pos); |
8298 | 199 |
} |
8913 | 200 |
__INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1); |
8321 | 201 |
RETURN ( aCharacter ); |
8298 | 202 |
} |
203 |
} |
|
204 |
} |
|
205 |
} |
|
206 |
resize:; |
|
207 |
#endif |
|
208 |
%}. |
|
209 |
||
8321 | 210 |
|
8298 | 211 |
(writeLimit isNil |
15349
7c2c460c5feb
class: CharacterWriteStream
Claus Gittinger <cg@exept.de>
parents:
14607
diff
changeset
|
212 |
or:[(position + 1) <= writeLimit]) ifTrue:[ |
8321 | 213 |
currentCharacterSize < aCharacter bitsPerCharacter ifTrue:[ |
214 |
self characterSizeChanged:aCharacter |
|
215 |
]. |
|
15358
6b69ebd9785c
class: CharacterWriteStream
Claus Gittinger <cg@exept.de>
parents:
15349
diff
changeset
|
216 |
(position >= collection size) ifTrue:[self growCollection]. |
15349
7c2c460c5feb
class: CharacterWriteStream
Claus Gittinger <cg@exept.de>
parents:
14607
diff
changeset
|
217 |
collection at:(position + 1) put:aCharacter. |
15358
6b69ebd9785c
class: CharacterWriteStream
Claus Gittinger <cg@exept.de>
parents:
15349
diff
changeset
|
218 |
(position >= readLimit) ifTrue:[readLimit := (position + 1)]. |
8298 | 219 |
position := position + 1. |
220 |
] ifFalse:[ |
|
8321 | 221 |
WriteError raiseErrorString:'write beyond writeLimit' |
222 |
]. |
|
223 |
^ aCharacter |
|
224 |
! |
|
225 |
||
226 |
nextPutAll:aCollection |
|
11319 | 227 |
"append aCollection to the receiver. |
228 |
Redefined to convert to a string of the needed charcter size." |
|
8321 | 229 |
|
230 |
aCollection bitsPerCharacter > currentCharacterSize ifTrue:[ |
|
231 |
self characterSizeChanged:aCollection. |
|
8298 | 232 |
]. |
12133 | 233 |
super nextPutAll:aCollection |
8321 | 234 |
! |
235 |
||
236 |
nextPutAll:aCollection startingAt:start to:stop |
|
237 |
aCollection bitsPerCharacter > currentCharacterSize ifTrue:[ |
|
238 |
self characterSizeChanged:aCollection. |
|
239 |
]. |
|
240 |
^ super nextPutAll:aCollection startingAt:start to:stop |
|
11319 | 241 |
! |
242 |
||
243 |
nextPutAllUnicode:aCollection |
|
244 |
^ self nextPutAll:aCollection |
|
245 |
! |
|
246 |
||
247 |
nextPutUnicode:aCharacter |
|
248 |
^ self nextPut:aCharacter |
|
8298 | 249 |
! ! |
250 |
||
8321 | 251 |
!CharacterWriteStream class methodsFor:'documentation'! |
252 |
||
253 |
version |
|
15358
6b69ebd9785c
class: CharacterWriteStream
Claus Gittinger <cg@exept.de>
parents:
15349
diff
changeset
|
254 |
^ '$Header: /cvs/stx/stx/libbasic/CharacterWriteStream.st,v 1.12 2013-06-03 18:40:36 cg Exp $' |
12133 | 255 |
! |
256 |
||
257 |
version_CVS |
|
15358
6b69ebd9785c
class: CharacterWriteStream
Claus Gittinger <cg@exept.de>
parents:
15349
diff
changeset
|
258 |
^ '$Header: /cvs/stx/stx/libbasic/CharacterWriteStream.st,v 1.12 2013-06-03 18:40:36 cg Exp $' |
8321 | 259 |
! ! |
14607 | 260 |