author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Sat, 24 Oct 2009 16:48:19 +0100 | |
branch | jv |
changeset 17732 | a1892eeca6c0 |
parent 17711 | 39faaaf888b4 |
child 17734 | 406b1590afe8 |
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 |
||
71 |
!CharacterWriteStream methodsFor:'private'! |
|
72 |
||
73 |
characterSizeChanged:aCharacterOrString |
|
74 |
"change aCollection to fit the size of aCharacter" |
|
75 |
||
76 |
|sz newSz bitsPerCharacter| |
|
77 |
||
78 |
bitsPerCharacter := aCharacterOrString bitsPerCharacter. |
|
79 |
currentCharacterSize < bitsPerCharacter ifTrue:[ |
|
80 |
sz := collection size. |
|
81 |
(position + 1 - ZeroPosition) > sz ifTrue:[ |
|
82 |
newSz := sz + 1. |
|
83 |
] ifFalse:[ |
|
84 |
newSz := sz. |
|
85 |
]. |
|
86 |
collection := (aCharacterOrString stringSpecies new:newSz) |
|
87 |
replaceFrom:1 to:sz with:collection startingAt:1. |
|
88 |
currentCharacterSize := bitsPerCharacter. |
|
89 |
]. |
|
90 |
! ! |
|
91 |
||
92 |
!CharacterWriteStream methodsFor:'private-accessing'! |
|
93 |
||
94 |
on:aCollection |
|
95 |
||
96 |
currentCharacterSize := aCollection bitsPerCharacter. |
|
97 |
^ super on:aCollection. |
|
98 |
! |
|
99 |
||
100 |
on:aCollection from:start to:stop |
|
101 |
||
102 |
currentCharacterSize := aCollection bitsPerCharacter. |
|
103 |
^ super on:aCollection from:start to:stop. |
|
104 |
! |
|
105 |
||
106 |
with:aCollection |
|
107 |
||
108 |
currentCharacterSize := aCollection bitsPerCharacter. |
|
109 |
^ super with:aCollection. |
|
110 |
! ! |
|
111 |
||
8298 | 112 |
!CharacterWriteStream methodsFor:'writing'! |
113 |
||
8321 | 114 |
next:count put:aCharacter |
115 |
"append anObject count times to the receiver. |
|
116 |
Redefined to avoid count grows of the underlying collection - |
|
117 |
instead a single grow on the final size is performed." |
|
118 |
||
119 |
aCharacter bitsPerCharacter > currentCharacterSize ifTrue:[ |
|
120 |
self characterSizeChanged:aCharacter. |
|
121 |
]. |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
122 |
super next:count put:aCharacter |
8321 | 123 |
! |
124 |
||
125 |
nextPut:aCharacter |
|
126 |
"append the argument, aCharacter to the stream. |
|
8298 | 127 |
Specially tuned for appending to String, ByteArray and Array streams." |
128 |
||
129 |
%{ /* NOCONTEXT */ |
|
130 |
||
131 |
#ifndef NO_PRIM_STREAM |
|
132 |
REGISTER int pos; |
|
133 |
unsigned ch; |
|
134 |
OBJ coll; |
|
135 |
OBJ p, wL, rL; |
|
136 |
int __readLimit = -1; |
|
137 |
||
138 |
coll = __INST(collection); |
|
139 |
p = __INST(position); |
|
140 |
||
8321 | 141 |
if (__isNonNilObject(coll) && __isSmallInteger(p) && __isCharacter(aCharacter)) { |
8298 | 142 |
pos = __intVal(p); |
143 |
/* make 1-based */ |
|
144 |
pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition)); |
|
145 |
wL = __INST(writeLimit); |
|
146 |
||
147 |
if ((wL == nil) |
|
148 |
|| (__isSmallInteger(wL) && (pos <= __intVal(wL)))) { |
|
149 |
OBJ cls; |
|
150 |
||
151 |
cls = __qClass(coll); |
|
8321 | 152 |
ch = __intVal(__characterVal(aCharacter)); |
8298 | 153 |
|
154 |
rL = __INST(readLimit); |
|
155 |
if (__isSmallInteger(rL)) { |
|
156 |
__readLimit = __intVal(rL); |
|
157 |
} |
|
158 |
||
159 |
if (cls == @global(String)) { |
|
160 |
if (ch > 0xFF) { |
|
161 |
goto resize; |
|
162 |
} |
|
163 |
if (pos <= __stringSize(coll)) { |
|
164 |
__StringInstPtr(coll)->s_element[pos-1] = ch; |
|
165 |
if ((__readLimit >= 0) && (pos >= __readLimit)) { |
|
8913 | 166 |
__INST(readLimit) = __mkSmallInteger(pos); |
8298 | 167 |
} |
8913 | 168 |
__INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1); |
8321 | 169 |
RETURN ( aCharacter ); |
8298 | 170 |
} |
171 |
} else if (cls == @global(Unicode16String)) { |
|
172 |
if (ch > 0xFFFF) { |
|
173 |
goto resize; |
|
174 |
} |
|
175 |
if (pos <= __unicode16StringSize(coll)) { |
|
176 |
__Unicode16StringInstPtr(coll)->s_element[pos-1] = ch; |
|
177 |
if ((__readLimit >= 0) && (pos >= __readLimit)) { |
|
8913 | 178 |
__INST(readLimit) = __mkSmallInteger(pos); |
8298 | 179 |
} |
8913 | 180 |
__INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1); |
8321 | 181 |
RETURN ( aCharacter ); |
8298 | 182 |
} |
183 |
} else if (cls == @global(Unicode32String)) { |
|
184 |
if ((pos <= __unicode32StringSize(coll))) { |
|
185 |
__Unicode32StringInstPtr(coll)->s_element[pos-1] = ch; |
|
186 |
if ((__readLimit >= 0) && (pos >= __readLimit)) { |
|
8913 | 187 |
__INST(readLimit) = __mkSmallInteger(pos); |
8298 | 188 |
} |
8913 | 189 |
__INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1); |
8321 | 190 |
RETURN ( aCharacter ); |
8298 | 191 |
} |
192 |
} |
|
193 |
} |
|
194 |
} |
|
195 |
resize:; |
|
196 |
#endif |
|
197 |
%}. |
|
198 |
||
8321 | 199 |
|
8298 | 200 |
(writeLimit isNil |
8321 | 201 |
or:[(position + 1 - ZeroPosition) <= writeLimit]) ifTrue:[ |
202 |
currentCharacterSize < aCharacter bitsPerCharacter ifTrue:[ |
|
203 |
self characterSizeChanged:aCharacter |
|
204 |
]. |
|
8298 | 205 |
((position + 1 - ZeroPosition) > collection size) ifTrue:[self growCollection]. |
8321 | 206 |
collection at:(position + 1 - ZeroPosition) put:aCharacter. |
8298 | 207 |
((position + 1 - ZeroPosition) > readLimit) ifTrue:[readLimit := (position + 1 - ZeroPosition)]. |
208 |
position := position + 1. |
|
209 |
] ifFalse:[ |
|
8321 | 210 |
WriteError raiseErrorString:'write beyond writeLimit' |
211 |
]. |
|
212 |
^ aCharacter |
|
213 |
! |
|
214 |
||
215 |
nextPutAll:aCollection |
|
11319 | 216 |
"append aCollection to the receiver. |
217 |
Redefined to convert to a string of the needed charcter size." |
|
8321 | 218 |
|
219 |
aCollection bitsPerCharacter > currentCharacterSize ifTrue:[ |
|
220 |
self characterSizeChanged:aCollection. |
|
8298 | 221 |
]. |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
222 |
super nextPutAll:aCollection |
8321 | 223 |
! |
224 |
||
225 |
nextPutAll:aCollection startingAt:start to:stop |
|
226 |
aCollection bitsPerCharacter > currentCharacterSize ifTrue:[ |
|
227 |
self characterSizeChanged:aCollection. |
|
228 |
]. |
|
229 |
^ super nextPutAll:aCollection startingAt:start to:stop |
|
11319 | 230 |
! |
231 |
||
232 |
nextPutAllUnicode:aCollection |
|
233 |
^ self nextPutAll:aCollection |
|
234 |
! |
|
235 |
||
236 |
nextPutUnicode:aCharacter |
|
237 |
^ self nextPut:aCharacter |
|
8298 | 238 |
! ! |
239 |
||
8321 | 240 |
!CharacterWriteStream class methodsFor:'documentation'! |
241 |
||
242 |
version |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
243 |
^ '$Id: CharacterWriteStream.st 10473 2009-10-24 15:48:19Z vranyj1 $' |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
244 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
245 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
246 |
version_CVS |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
247 |
^ '§Header: /cvs/stx/stx/libbasic/CharacterWriteStream.st,v 1.9 2009/10/05 09:20:25 cg Exp §' |
8321 | 248 |
! ! |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
249 |