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