author | Claus Gittinger <cg@exept.de> |
Wed, 21 Jan 1998 17:09:08 +0100 | |
changeset 3208 | 2d71538b9fd5 |
parent 3205 | 86ad9ad5e29c |
child 3210 | 6c4099f5c318 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
155 | 3 |
All Rights Reserved |
1 | 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 |
" |
|
12 |
||
3208
2d71538b9fd5
now subclass of UIBytes - which contains common protocol
Claus Gittinger <cg@exept.de>
parents:
3205
diff
changeset
|
13 |
UninterpretedBytes variableByteSubclass:#ByteArray |
1002
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
14 |
instanceVariableNames:'' |
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
15 |
classVariableNames:'' |
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
16 |
poolDictionaries:'' |
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
17 |
category:'Collections-Arrayed' |
1 | 18 |
! |
19 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
20 |
!ByteArray class methodsFor:'documentation'! |
88 | 21 |
|
563 | 22 |
copyright |
23 |
" |
|
24 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
25 |
All Rights Reserved |
|
26 |
||
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
88 | 36 |
documentation |
37 |
" |
|
38 |
ByteArrays store integers in the range 0..255. |
|
129 | 39 |
In contrast to normal arrays (which store pointers to their elements), |
362 | 40 |
byteArrays store the values in a dense & compact way. ByteArrays can |
129 | 41 |
be used to hold the data for bitmaps, images and other bulk data. |
42 |
ByteArrays are also used to store the bytecode-instructions of an |
|
43 |
interpreted method and are used as superclass for Strings. |
|
44 |
||
45 |
ByteArrays can be used as literals i.e. you can enter ByteArray-constants |
|
46 |
as #[ element1 element2 .... elementN] and also use byteArray constants |
|
47 |
as elements in a constant array. |
|
48 |
As in: #( #[1 1 1] #[2 2 2] #[3 3 3]) |
|
49 |
||
50 |
If you have to communicate structure-data (in the C-sense) with external |
|
51 |
programs/data-bases, see a companion class (Structure) in the goodies directory. |
|
52 |
It allows the definition of subclasses of ByteArray, which transparently fetch |
|
53 |
and store C-structure fields. |
|
1266 | 54 |
|
2145 | 55 |
[warning:] |
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
56 |
read the warning about 'growing fixed size collection' |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
57 |
in ArrayedCollection's documentation |
2145 | 58 |
|
1290 | 59 |
[author:] |
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
60 |
Claus Gittinger |
1290 | 61 |
|
1266 | 62 |
[See also:] |
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
63 |
Array CharacterArray String |
88 | 64 |
" |
2 | 65 |
! ! |
66 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
67 |
!ByteArray class methodsFor:'instance creation'! |
1 | 68 |
|
563 | 69 |
fromPackedString:aString |
70 |
"ST-80 compatibility: decode a byteArray from a packed string in which |
|
71 |
6bits are encoded per character. The argument, aString must be a multiple |
|
72 |
of 4 in size (since 24 is the lcm of 6 and 8). This is somewhat like |
|
73 |
the radix-encoding used in good old PDP11 times ;-) |
|
74 |
ST-80 uses this encoding for Images ... |
|
75 |
PS: It took a while to figure that one out ... I dont like it ;-)" |
|
76 |
||
77 |
|index "{ Class: SmallInteger }" |
|
78 |
dstIndex "{ Class: SmallInteger }" |
|
79 |
stop "{ Class: SmallInteger }" |
|
2447 | 80 |
sixBits "{ Class: SmallInteger }" |
81 |
n "{ Class: SmallInteger }" |
|
563 | 82 |
sz "{ Class: SmallInteger }" |
83 |
lastCharacter bytes| |
|
84 |
||
85 |
sz := aString size. |
|
86 |
sz == 0 ifTrue:[^ self new]. |
|
87 |
stop := sz // 4 * 3. |
|
3135 | 88 |
"the size modulo 3 is encoded in the last character, if its in the |
563 | 89 |
range 97 .. otherwise, its exact." |
90 |
||
91 |
lastCharacter := aString last. |
|
92 |
lastCharacter asciiValue > 96 ifTrue:[ |
|
3135 | 93 |
stop := stop - 3 + lastCharacter asciiValue - 96 |
563 | 94 |
]. |
95 |
bytes := self new:stop. |
|
2447 | 96 |
|
563 | 97 |
index := 1. dstIndex := 1. |
98 |
[dstIndex <= stop] whileTrue:[ |
|
3135 | 99 |
"take 4 characters ..." |
100 |
sixBits := (aString at:index) asciiValue. |
|
101 |
sixBits := sixBits bitAnd:16r3F. |
|
102 |
n := sixBits. |
|
2447 | 103 |
|
3135 | 104 |
sixBits := (aString at:index+1) asciiValue. |
105 |
sixBits := sixBits bitAnd:16r3F. |
|
106 |
n := (n bitShift:6) + sixBits. |
|
2447 | 107 |
|
3135 | 108 |
sixBits := (aString at:index+2) asciiValue. |
109 |
sixBits := sixBits bitAnd:16r3F. |
|
110 |
n := (n bitShift:6) + sixBits. |
|
2447 | 111 |
|
3135 | 112 |
sixBits := (aString at:index+3) asciiValue. |
113 |
sixBits := sixBits bitAnd:16r3F. |
|
114 |
n := (n bitShift:6) + sixBits. |
|
2447 | 115 |
|
3135 | 116 |
index := index + 4. |
2447 | 117 |
|
3135 | 118 |
"/ now have 24 bits in n |
2447 | 119 |
|
3135 | 120 |
bytes at:dstIndex put:(n bitShift:-16). |
2447 | 121 |
|
3135 | 122 |
dstIndex < stop ifTrue:[ |
123 |
bytes at:dstIndex+1 put:((n bitShift:-8) bitAnd:16rFF). |
|
124 |
dstIndex+2 <= stop ifTrue:[ |
|
125 |
bytes at:dstIndex+2 put:(n bitAnd:16rFF). |
|
126 |
] |
|
127 |
]. |
|
128 |
dstIndex := dstIndex + 3. |
|
563 | 129 |
]. |
130 |
^ bytes |
|
131 |
||
132 |
" |
|
133 |
ByteArray fromPackedString:(#[1 1 1 1] asPackedString) |
|
134 |
ByteArray fromPackedString:(#[1 1 1 1 1] asPackedString) |
|
135 |
ByteArray fromPackedString:(#[1 1 1 1 1 1] asPackedString) |
|
136 |
ByteArray fromPackedString:(#[1 1 1 1 1 1 1] asPackedString) |
|
137 |
ByteArray fromPackedString:(#[1 1 1 1 1 1 1 1] asPackedString) |
|
138 |
||
139 |
" |
|
2447 | 140 |
|
3135 | 141 |
"Modified: / 6.3.1997 / 15:28:52 / cg" |
142 |
"Modified: / 18.12.1997 / 17:17:11 / stefan" |
|
563 | 143 |
! |
144 |
||
1 | 145 |
uninitializedNew:anInteger |
146 |
"return a new instance of the receiver with uninitialized |
|
147 |
(i.e. undefined) contents. The indexed elements have any random |
|
75 | 148 |
value. However, any named instance variables are still nilled. |
149 |
For use, when contents will be set anyway shortly after - this |
|
150 |
is a bit faster than the regular basicNew:, which clears the bytes. |
|
151 |
Of course, it only makes a difference for very big ByteArrays, such |
|
152 |
as used for images/bitmaps. |
|
153 |
||
154 |
Notice: if you want to port code using uninitializedNew: to another |
|
155 |
smalltalk, you have to add an 'uninitializedNew: -> basicNew:'-calling |
|
156 |
method to the ByteArray class of the other smalltalk." |
|
1 | 157 |
|
158 |
%{ /* NOCONTEXT */ |
|
159 |
OBJ newobj; |
|
160 |
INT instsize, nInstVars, nindexedinstvars; |
|
161 |
REGISTER OBJ *op; |
|
162 |
||
249 | 163 |
if (__isSmallInteger(anInteger)) { |
1133 | 164 |
nindexedinstvars = __intVal(anInteger); |
155 | 165 |
if (nindexedinstvars >= 0) { |
249 | 166 |
if (self == ByteArray) { |
167 |
/* |
|
168 |
* the most common case |
|
169 |
*/ |
|
170 |
instsize = OHDR_SIZE + nindexedinstvars; |
|
2915 | 171 |
if (__CanDoQuickNew(instsize)) { /* OBJECT ALLOCATION */ |
835 | 172 |
__qCheckedNew(newobj, instsize); |
1133 | 173 |
__InstPtr(newobj)->o_class = self; |
249 | 174 |
RETURN (newobj ); |
155 | 175 |
} |
249 | 176 |
} |
1133 | 177 |
nInstVars = __intVal(__ClassInstPtr(self)->c_ninstvars); |
249 | 178 |
instsize = OHDR_SIZE + __OBJS2BYTES__(nInstVars) + nindexedinstvars; |
834 | 179 |
__PROTECT_CONTEXT__ |
2915 | 180 |
__qNew(newobj, instsize); /* OBJECT ALLOCATION */ |
834 | 181 |
__UNPROTECT_CONTEXT__ |
281 | 182 |
if (newobj != nil) { |
1133 | 183 |
__InstPtr(newobj)->o_class = self; |
367 | 184 |
__qSTORE(newobj, self); |
249 | 185 |
if (nInstVars) { |
186 |
/* |
|
187 |
* still have to nil out named instvars ... |
|
188 |
*/ |
|
788
e80f1c42b87b
dont use memset4 if its undefined
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
189 |
#if defined(memset4) && defined(FAST_OBJECT_MEMSET4) |
1133 | 190 |
memset4(__InstPtr(newobj)->i_instvars, nil, nInstVars); |
1 | 191 |
#else |
249 | 192 |
# if defined(FAST_MEMSET) && !defined(NEGATIVE_ADDRESSES) |
193 |
/* |
|
194 |
* knowing that nil is 0 |
|
195 |
*/ |
|
1133 | 196 |
memset(__InstPtr(newobj)->i_instvars, 0, instsize - OHDR_SIZE); |
249 | 197 |
# else |
1133 | 198 |
op = __InstPtr(newobj)->i_instvars; |
249 | 199 |
while (nInstVars--) |
200 |
*op++ = nil; |
|
201 |
# endif |
|
1 | 202 |
#endif |
155 | 203 |
} |
249 | 204 |
RETURN ( newobj ); |
155 | 205 |
} |
206 |
} |
|
1 | 207 |
} |
208 |
%} |
|
209 |
. |
|
12 | 210 |
(anInteger isMemberOf:SmallInteger) ifTrue:[ |
155 | 211 |
(anInteger < 0) ifTrue:[ |
212 |
^ self error:'bad (negative) argument to new' |
|
213 |
]. |
|
214 |
" |
|
215 |
memory allocation failed. |
|
216 |
When we arrive here, there was no memory, even after |
|
217 |
a garbage collect. This means, that the VM wanted to |
|
218 |
get some more memory from the Operatingsystem, which |
|
219 |
was not kind enough to give some. |
|
220 |
" |
|
221 |
^ ObjectMemory allocationFailureSignal raise. |
|
12 | 222 |
]. |
1 | 223 |
^ self basicNew:anInteger |
224 |
! ! |
|
225 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
226 |
!ByteArray class methodsFor:'binary storage'! |
1 | 227 |
|
550 | 228 |
binaryDefinitionFrom: stream manager: manager |
229 |
"get a ByteArray from the binary stream. |
|
3071 | 230 |
ByteArrays are stored as 4-byte size, followed by the bytes. |
231 |
This is only invoked for long bytearrays. |
|
232 |
Short ones are stored with 1byte length." |
|
233 |
||
234 |
|b len| |
|
1 | 235 |
|
550 | 236 |
"take care of subclasses ..." |
237 |
self == ByteArray ifTrue:[ |
|
3071 | 238 |
len := stream nextNumber:4. |
239 |
b := ByteArray uninitializedNew:len. |
|
240 |
stream nextBytes:len into:b startingAt:1. |
|
241 |
^ b |
|
550 | 242 |
]. |
3071 | 243 |
^ super binaryDefinitionFrom:stream manager:manager |
244 |
||
245 |
"Modified: / 2.11.1997 / 16:19:49 / cg" |
|
550 | 246 |
! ! |
247 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
248 |
!ByteArray class methodsFor:'queries'! |
550 | 249 |
|
250 |
isBuiltInClass |
|
1266 | 251 |
"return true if this class is known by the run-time-system. |
252 |
Here, true is returned for myself, false for subclasses." |
|
550 | 253 |
|
254 |
^ self == ByteArray |
|
1266 | 255 |
|
256 |
"Modified: 23.4.1996 / 15:56:25 / cg" |
|
550 | 257 |
! ! |
258 |
||
259 |
!ByteArray methodsFor:'accessing'! |
|
1 | 260 |
|
563 | 261 |
basicAt:index |
262 |
"return the indexed instance variable with index, anInteger |
|
1019 | 263 |
- redefined here to be slighly faster than the default in Object. |
264 |
Q: is it worth the extra code ?" |
|
563 | 265 |
|
266 |
%{ /* NOCONTEXT */ |
|
267 |
||
268 |
REGISTER int indx; |
|
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
269 |
REGISTER OBJ slf; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
270 |
REGISTER OBJ cls; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
271 |
REGISTER int nIndex; |
563 | 272 |
|
273 |
if (__isSmallInteger(index)) { |
|
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
274 |
indx = __intVal(index) - 1; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
275 |
slf = self; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
276 |
if ((cls = __qClass(slf)) != @global(ByteArray)) { |
1384 | 277 |
if (((INT)__ClassInstPtr(cls)->c_flags & __MASKSMALLINT(ARRAYMASK)) |
278 |
!= __MASKSMALLINT(BYTEARRAY)) { |
|
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
279 |
goto fail; |
1019 | 280 |
} |
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
281 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
282 |
} |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
283 |
nIndex = __byteArraySize(slf); |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
284 |
if ((unsigned)indx < (unsigned)nIndex) { |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
285 |
RETURN ( __MKSMALLINT(__ByteArrayInstPtr(slf)->ba_element[indx]) ); |
563 | 286 |
} |
287 |
} |
|
1019 | 288 |
fail: ; |
289 |
%}. |
|
563 | 290 |
^ super basicAt:index |
291 |
! |
|
292 |
||
1 | 293 |
basicAt:index put:value |
1230 | 294 |
"set the indexed instance variable with index, anInteger to value. |
295 |
Returns value (sigh). |
|
1019 | 296 |
- redefined here to be slighly faster than the default in Object. |
297 |
Q: is it worth the extra code ?" |
|
1 | 298 |
|
299 |
%{ /* NOCONTEXT */ |
|
300 |
||
301 |
REGISTER int indx; |
|
302 |
int nIndex; |
|
303 |
int val; |
|
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
304 |
REGISTER OBJ slf; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
305 |
REGISTER OBJ cls; |
1 | 306 |
|
249 | 307 |
if (__bothSmallInteger(index, value)) { |
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
308 |
val = __intVal(value); |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
309 |
if ((unsigned)(val) <= 0xFF /* i.e. (val >= 0) && (val <= 255) */) { |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
310 |
indx = __intVal(index) - 1; |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
311 |
slf = self; |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
312 |
if ((cls = __qClass(slf)) != @global(ByteArray)) { |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
313 |
if (((INT)__ClassInstPtr(cls)->c_flags & __MASKSMALLINT(ARRAYMASK)) |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
314 |
!= __MASKSMALLINT(BYTEARRAY)) { |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
315 |
goto fail; |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
316 |
} |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
317 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
318 |
} |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
319 |
nIndex = __byteArraySize(slf); |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
320 |
if ((unsigned)indx < (unsigned)nIndex) { |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
321 |
__ByteArrayInstPtr(slf)->ba_element[indx] = val; |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
322 |
RETURN ( value ); |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
323 |
} |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
324 |
} |
1 | 325 |
} |
1019 | 326 |
fail: ; |
327 |
%}. |
|
1 | 328 |
^ super basicAt:index put:value |
1230 | 329 |
|
330 |
"Modified: 19.4.1996 / 11:14:40 / cg" |
|
2 | 331 |
! |
332 |
||
54 | 333 |
byteAt:index |
249 | 334 |
"return the byte at index. |
335 |
For ByteArray, this is the same as basicAt:; |
|
336 |
however, for strings or symbols, this returns a numeric byteValue |
|
337 |
instead of a character." |
|
338 |
||
339 |
%{ /* NOCONTEXT */ |
|
340 |
||
341 |
REGISTER int indx; |
|
342 |
int nIndex; |
|
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
343 |
REGISTER OBJ slf; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
344 |
REGISTER OBJ cls; |
54 | 345 |
|
249 | 346 |
if (__isSmallInteger(index)) { |
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
347 |
indx = __intVal(index) - 1; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
348 |
slf = self; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
349 |
if ((cls = __qClass(slf)) != @global(ByteArray)) |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
350 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
351 |
nIndex = __byteArraySize(slf); |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
352 |
if ((unsigned)indx < (unsigned)nIndex) { |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
353 |
RETURN ( __MKSMALLINT(__ByteArrayInstPtr(slf)->ba_element[indx]) ); |
249 | 354 |
} |
355 |
} |
|
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
356 |
%}. |
249 | 357 |
^ (super basicAt:index) asInteger |
54 | 358 |
! |
359 |
||
249 | 360 |
byteAt:index put:value |
361 |
"set the byte at index. For ByteArray, this is the same as basicAt:put:. |
|
362 |
However, for Strings, this expects a byteValue to be stored." |
|
363 |
||
364 |
%{ /* NOCONTEXT */ |
|
365 |
||
366 |
REGISTER int indx; |
|
367 |
int nIndex; |
|
368 |
int val; |
|
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
369 |
REGISTER OBJ slf; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
370 |
REGISTER OBJ cls; |
54 | 371 |
|
249 | 372 |
if (__bothSmallInteger(index, value)) { |
1133 | 373 |
val = __intVal(value); |
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
374 |
if ((unsigned)(val) <= 0xFF /* i.e. (val >= 0) && (val <= 255) */) { |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
375 |
indx = __intVal(index) - 1; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
376 |
slf = self; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
377 |
if ((cls = __qClass(slf)) != ByteArray) |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
378 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
379 |
nIndex = __byteArraySize(slf); |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
380 |
if ((unsigned)indx < (unsigned)nIndex) { |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
381 |
__ByteArrayInstPtr(slf)->ba_element[indx] = val; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
382 |
RETURN ( value ); |
249 | 383 |
} |
384 |
} |
|
385 |
} |
|
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
386 |
%}. |
249 | 387 |
^ super basicAt:index put:value |
54 | 388 |
! |
389 |
||
563 | 390 |
doubleAt:index |
391 |
"return the 8-bytes starting at index as a Float. |
|
392 |
Notice, that (currently) ST/X Floats are what Doubles are in ST-80. |
|
393 |
Notice also, that the bytes are expected to be in this machines |
|
394 |
float representation - if the bytearray originated from another |
|
395 |
machine, some conversion is usually needed." |
|
2 | 396 |
|
563 | 397 |
|newFloat| |
359 | 398 |
|
563 | 399 |
newFloat := Float basicNew. |
400 |
1 to:8 do:[:destIndex| |
|
401 |
newFloat basicAt:destIndex put:(self at:index - 1 + destIndex) |
|
359 | 402 |
]. |
563 | 403 |
^ newFloat. |
359 | 404 |
! |
405 |
||
563 | 406 |
doubleAt:index put:aFloat |
407 |
"store the value of the argument, aFloat into the receiver |
|
408 |
starting at index. |
|
409 |
Notice, that (currently) ST/X Floats are what Doubles are in ST-80. |
|
410 |
Notice also, that the bytes are expected to be in this machines |
|
411 |
float representation - if the bytearray originated from another |
|
412 |
machine, some conversion is usually needed." |
|
54 | 413 |
|
3202 | 414 |
|flt| |
415 |
||
416 |
flt := aFloat asFloat. |
|
563 | 417 |
1 to:8 do:[:srcIndex| |
3202 | 418 |
self at:index - 1 + srcIndex put:(flt basicAt:srcIndex) |
359 | 419 |
]. |
563 | 420 |
^ aFloat |
54 | 421 |
! |
422 |
||
2 | 423 |
doubleWordAt:index |
424 |
"return the 4-bytes starting at index as an (unsigned) Integer. |
|
359 | 425 |
The value is retrieved in the machines natural byte order. |
426 |
Q: should it return a signed value ? (see ByteArray>>signedDoubleWordAt:)" |
|
427 |
||
428 |
%{ /* NOCONTEXT */ |
|
429 |
||
430 |
REGISTER int indx; |
|
431 |
int nIndex; |
|
432 |
union { |
|
2950 | 433 |
unsigned char u_char[4]; |
359 | 434 |
unsigned u_uint; |
435 |
} val; |
|
436 |
OBJ cls; |
|
2950 | 437 |
unsigned char *byteP; |
359 | 438 |
|
439 |
if (__isSmallInteger(index)) { |
|
1133 | 440 |
indx = __intVal(index); |
359 | 441 |
if (indx > 0) { |
1133 | 442 |
if ((cls = __qClass(self)) != @global(ByteArray)) |
443 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
359 | 444 |
nIndex = __byteArraySize(self); |
445 |
if ((indx+3) <= nIndex) { |
|
2950 | 446 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
447 |
val.u_char[0] = byteP[0]; |
|
448 |
val.u_char[1] = byteP[1]; |
|
449 |
val.u_char[2] = byteP[2]; |
|
450 |
val.u_char[3] = byteP[3]; |
|
359 | 451 |
if ((val.u_uint >= 0) && (val.u_uint <= _MAX_INT)) { |
1133 | 452 |
RETURN ( __MKSMALLINT(val.u_uint) ); |
359 | 453 |
} |
1133 | 454 |
RETURN ( __MKULARGEINT(val.u_uint) ); |
359 | 455 |
} |
456 |
} |
|
457 |
} |
|
458 |
%}. |
|
459 |
^ SubscriptOutOfBoundsSignal raise. |
|
460 |
||
461 |
" |
|
462 |
|b| |
|
463 |
||
464 |
b := ByteArray withAll:#(1 2 3 4). |
|
465 |
(b doubleWordAt:1) printStringRadix:16 |
|
466 |
" |
|
467 |
! |
|
468 |
||
469 |
doubleWordAt:index MSB:msb |
|
470 |
"return the 4-bytes starting at index as an (unsigned) Integer. |
|
471 |
The value is retrieved MSB-first, if the msb-arg is true; |
|
472 |
LSB-first otherwise. |
|
473 |
Q: should it return a signed value ? (see ByteArray>>signedDoubleWordAt:)" |
|
2 | 474 |
|
475 |
%{ /* NOCONTEXT */ |
|
476 |
||
477 |
REGISTER int indx; |
|
478 |
int nIndex; |
|
479 |
int val; |
|
249 | 480 |
OBJ cls; |
2950 | 481 |
unsigned char *byteP; |
2 | 482 |
|
249 | 483 |
if (__isSmallInteger(index)) { |
1133 | 484 |
indx = __intVal(index); |
249 | 485 |
if (indx > 0) { |
1133 | 486 |
if ((cls = __qClass(self)) != @global(ByteArray)) |
487 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
249 | 488 |
nIndex = __byteArraySize(self); |
489 |
if ((indx+3) <= nIndex) { |
|
2950 | 490 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
359 | 491 |
if (msb == true) { |
2950 | 492 |
val = byteP[0]; |
493 |
val = (val << 8) + byteP[1]; |
|
494 |
val = (val << 8) + byteP[2]; |
|
495 |
val = (val << 8) + byteP[3]; |
|
359 | 496 |
} else { |
2950 | 497 |
val = byteP[3]; |
498 |
val = (val << 8) + byteP[2]; |
|
499 |
val = (val << 8) + byteP[1]; |
|
500 |
val = (val << 8) + byteP[0]; |
|
359 | 501 |
} |
249 | 502 |
if ((val >= 0) && (val <= _MAX_INT)) { |
1133 | 503 |
RETURN ( __MKSMALLINT(val) ); |
249 | 504 |
} |
1133 | 505 |
RETURN ( __MKULARGEINT(val) ); |
155 | 506 |
} |
507 |
} |
|
2 | 508 |
} |
359 | 509 |
%}. |
510 |
^ SubscriptOutOfBoundsSignal raise. |
|
129 | 511 |
|
512 |
" |
|
513 |
|b| |
|
514 |
||
515 |
b := ByteArray withAll:#(1 2 3 4). |
|
359 | 516 |
(b doubleWordAt:1 MSB:true) printStringRadix:16. |
517 |
(b doubleWordAt:1 MSB:false) printStringRadix:16 |
|
129 | 518 |
" |
2 | 519 |
! |
520 |
||
521 |
doubleWordAt:index put:value |
|
522 |
"set the 4-bytes starting at index from the (unsigned) Integer value. |
|
359 | 523 |
The value should be in the range 0 to 16rFFFFFFFF |
524 |
(for negative values, the stored value is not defined). |
|
525 |
The value is stored in the machines natural byte order. |
|
526 |
Q: should it store signed values ? (see ByteArray signedDoubleWordAt:put:)" |
|
527 |
||
528 |
|t| |
|
529 |
||
530 |
%{ /* NOCONTEXT */ |
|
531 |
||
532 |
REGISTER int indx; |
|
533 |
int nIndex; |
|
534 |
union { |
|
2950 | 535 |
unsigned char u_char[4]; |
359 | 536 |
unsigned u_uint; |
537 |
} val; |
|
538 |
OBJ cls; |
|
2950 | 539 |
unsigned char *byteP; |
359 | 540 |
|
541 |
if (__isSmallInteger(index)) { |
|
542 |
if (__isSmallInteger(value)) { |
|
1133 | 543 |
val.u_uint = __intVal(value); |
359 | 544 |
} else { |
545 |
val.u_uint = __longIntVal(value); |
|
546 |
if (val.u_uint == 0) goto error; |
|
547 |
} |
|
548 |
||
1133 | 549 |
indx = __intVal(index); |
359 | 550 |
if (indx > 0) { |
1133 | 551 |
if ((cls = __qClass(self)) != @global(ByteArray)) |
552 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
359 | 553 |
nIndex = __qSize(self) - OHDR_SIZE; |
554 |
if ((indx+3) <= nIndex) { |
|
2950 | 555 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
556 |
byteP[0] = val.u_char[0]; |
|
557 |
byteP[1] = val.u_char[1]; |
|
558 |
byteP[2] = val.u_char[2]; |
|
559 |
byteP[3] = val.u_char[3]; |
|
359 | 560 |
RETURN ( value ); |
561 |
} |
|
562 |
} |
|
563 |
} |
|
564 |
error: ; |
|
565 |
%}. |
|
566 |
^ SubscriptOutOfBoundsSignal raise. |
|
567 |
||
568 |
" |
|
569 |
|b| |
|
570 |
b := ByteArray new:4. |
|
571 |
b doubleWordAt:1 put:16r04030201. |
|
572 |
b inspect |
|
573 |
" |
|
574 |
! |
|
575 |
||
576 |
doubleWordAt:index put:value MSB:msb |
|
577 |
"set the 4-bytes starting at index from the (unsigned) Integer value. |
|
129 | 578 |
The value must be in the range 0 to 16rFFFFFFFF. |
359 | 579 |
The value is stored MSB-first if msb is true; LSB-first otherwise. |
129 | 580 |
question: should it store signed values ? (see ByteArray signedDoubleWordAt:put:)" |
2 | 581 |
|
582 |
|t| |
|
583 |
||
584 |
%{ /* NOCONTEXT */ |
|
585 |
||
586 |
REGISTER int indx; |
|
587 |
int nIndex; |
|
588 |
int val; |
|
249 | 589 |
OBJ cls; |
2950 | 590 |
unsigned char *byteP; |
2 | 591 |
|
359 | 592 |
if (__isSmallInteger(index)) { |
593 |
if (__isSmallInteger(value)) { |
|
1133 | 594 |
val = __intVal(value); |
359 | 595 |
} else { |
596 |
val = __longIntVal(value); |
|
597 |
if (val == 0) goto error; |
|
598 |
} |
|
1133 | 599 |
indx = __intVal(index); |
359 | 600 |
if (indx > 0) { |
1133 | 601 |
if ((cls = __qClass(self)) != @global(ByteArray)) |
602 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
359 | 603 |
nIndex = __qSize(self) - OHDR_SIZE; |
604 |
if ((indx+3) <= nIndex) { |
|
2950 | 605 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
359 | 606 |
if (msb == true) { |
2950 | 607 |
byteP[3] = val & 0xFF; |
359 | 608 |
val >>= 8; |
2950 | 609 |
byteP[2] = val & 0xFF; |
249 | 610 |
val >>= 8; |
2950 | 611 |
byteP[1] = val & 0xFF; |
249 | 612 |
val >>= 8; |
2950 | 613 |
byteP[0] = val & 0xFF; |
359 | 614 |
} else { |
2950 | 615 |
byteP[0] = val & 0xFF; |
359 | 616 |
val >>= 8; |
2950 | 617 |
byteP[1] = val & 0xFF; |
359 | 618 |
val >>= 8; |
2950 | 619 |
byteP[2] = val & 0xFF; |
249 | 620 |
val >>= 8; |
2950 | 621 |
byteP[3] = val & 0xFF; |
249 | 622 |
} |
359 | 623 |
RETURN ( value ); |
155 | 624 |
} |
625 |
} |
|
2 | 626 |
} |
359 | 627 |
error: ; |
628 |
%}. |
|
629 |
^ SubscriptOutOfBoundsSignal raise. |
|
129 | 630 |
|
631 |
" |
|
632 |
|b| |
|
359 | 633 |
b := ByteArray new:8. |
634 |
b doubleWordAt:1 put:16r04030201 MSB:true. |
|
635 |
b doubleWordAt:5 put:16r04030201 MSB:false. |
|
129 | 636 |
b inspect |
637 |
" |
|
54 | 638 |
! |
639 |
||
563 | 640 |
floatAt:index |
641 |
"return the 4-bytes starting at index as a Float. |
|
642 |
Notice, that (currently) ST/X Floats are what Doubles are in ST-80; |
|
643 |
therefore this method reads a 4-byte float from the byteArray and returns |
|
644 |
a float object which keeps an 8-byte double internally. |
|
645 |
Notice also, that the bytes are expected to be in this machines |
|
646 |
float representation and order - if the bytearray originated from another |
|
647 |
machine, some conversion is usually needed." |
|
648 |
||
649 |
|newFloat| |
|
650 |
||
3202 | 651 |
newFloat := ShortFloat basicNew. |
652 |
1 to:4 do:[:destIndex| |
|
653 |
newFloat basicAt:destIndex put:(self at:index - 1 + destIndex) |
|
563 | 654 |
]. |
655 |
^ newFloat. |
|
656 |
! |
|
657 |
||
658 |
floatAt:index put:aFloat |
|
659 |
"store the 4 bytes of value of the argument, aFloat into the receiver |
|
660 |
starting at index. |
|
3205 | 661 |
Notice, that (currently) ST/X Floats are what Doubles are in ST-80. |
563 | 662 |
Notice also, that the bytes are expected to be in this machines |
663 |
float representation - if the bytearray originated from another |
|
664 |
machine, some conversion is usually needed." |
|
665 |
||
3202 | 666 |
|sflt| |
667 |
||
668 |
sflt := aFloat asShortFloat. |
|
669 |
1 to:4 do:[:srcIndex| |
|
670 |
self at:index - 1 + srcIndex put:(sflt basicAt:srcIndex) |
|
563 | 671 |
]. |
672 |
^ aFloat |
|
673 |
! |
|
674 |
||
675 |
ieeDoubleAt:index |
|
676 |
"retrieve the 8 bytes starting at index as a float. |
|
677 |
The 8 bytes are assumed to be in IEE floating point single precision |
|
678 |
number format." |
|
679 |
||
680 |
" |
|
681 |
currently, we assume that the machines native number format is already |
|
682 |
IEE format - we need some more code here whenever ST/X is ported |
|
683 |
to an IBM 370 or old VAX etc. |
|
684 |
To date, all supported systems use IEE float numbers, so there should be |
|
685 |
no problem. |
|
686 |
" |
|
687 |
^ self doubleAt:index |
|
688 |
! |
|
689 |
||
690 |
ieeDoubleAt:index put:aFloat |
|
691 |
"store the value of the argument, aFloat into the receiver |
|
692 |
starting at index. Storage is in IEE floating point double precision format. |
|
693 |
(i.e. 8 bytes are stored)." |
|
694 |
||
695 |
" |
|
696 |
currently, we assume that the machines native number format is already |
|
697 |
IEE format - we need some more code here whenever ST/X is ported |
|
698 |
to an IBM 370 or old VAX etc. |
|
699 |
To date, all supported systems use IEE float numbers, so there should be |
|
700 |
no problem. |
|
701 |
" |
|
702 |
^ self doubleAt:index put:aFloat |
|
703 |
! |
|
704 |
||
705 |
ieeFloatAt:index |
|
706 |
"retrieve the 4 bytes starting at index as a float. |
|
707 |
The 4 bytes are assumed to be in IEE floating point single precision |
|
708 |
number format." |
|
709 |
||
710 |
" |
|
711 |
currently, we assume that the machines native number format is already |
|
712 |
IEE format - we need some more code here whenever ST/X is ported |
|
713 |
to an IBM 370 or old VAX etc. |
|
714 |
To date, all supported systems use IEE float numbers, so there should be |
|
715 |
no problem. |
|
716 |
" |
|
717 |
^ self floatAt:index |
|
718 |
! |
|
719 |
||
720 |
ieeFloatAt:index put:aFloat |
|
721 |
"store the value of the argument, aFloat into the receiver |
|
722 |
starting at index. Storage is in IEE floating point single precision format. |
|
723 |
(i.e. 4 bytes are stored). Since ST/X floats are really doubles, the low- |
|
724 |
order 4 bytes of the precision is lost." |
|
725 |
||
726 |
" |
|
727 |
currently, we assume that the machines native number format is already |
|
728 |
IEE format - we need some more code here whenever ST/X is ported |
|
729 |
to an IBM 370 or old VAX etc. |
|
730 |
To date, all supported systems use IEE float numbers, so there should be |
|
731 |
no problem. |
|
732 |
" |
|
733 |
^ self floatAt:index put:aFloat |
|
734 |
! |
|
735 |
||
736 |
quadWordAt:index MSB:msb |
|
737 |
"return the 8-bytes starting at index as an (unsigned) Integer. |
|
738 |
Depending on msb, the value is retrieved MSB or LSB-first." |
|
739 |
||
740 |
|l |
|
741 |
bIdx "{ Class: SmallInteger }" |
|
742 |
delta "{ Class: SmallInteger }"| |
|
743 |
||
744 |
l := LargeInteger basicNew numberOfDigits:8. |
|
745 |
msb ifTrue:[ |
|
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
746 |
bIdx := index + 7. |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
747 |
delta := -1 |
563 | 748 |
] ifFalse:[ |
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
749 |
bIdx := index. |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
750 |
delta := 1 |
563 | 751 |
]. |
752 |
1 to:8 do:[:i | |
|
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
753 |
l digitAt:i put:(self basicAt:bIdx). |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
754 |
bIdx := bIdx + delta |
563 | 755 |
]. |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
756 |
^ l compressed |
563 | 757 |
|
758 |
" |
|
759 |
|b| |
|
760 |
||
761 |
b := ByteArray withAll:#(1 2 3 4 5 6 7 8). |
|
762 |
(b quadWordAt:1 MSB:false) printStringRadix:16 |
|
763 |
" |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
764 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
765 |
"Modified: 5.11.1996 / 14:06:21 / cg" |
563 | 766 |
! |
767 |
||
768 |
quadWordAt:index put:anInteger MSB:msb |
|
769 |
"set the 8-bytes starting at index from the (unsigned) Integer value. |
|
770 |
The value must be in the range 0 to 16rFFFFFFFFFFFFFFFF. |
|
771 |
Depending on msb, the value is stored MSB-first or LSB-first." |
|
772 |
||
773 |
|bIdx "{ Class: SmallInteger }" |
|
774 |
delta "{ Class: SmallInteger }"| |
|
775 |
||
776 |
msb ifTrue:[ |
|
777 |
bIdx := index + 7. |
|
778 |
delta := -1 |
|
779 |
] ifFalse:[ |
|
780 |
bIdx := index. |
|
781 |
delta := 1 |
|
782 |
]. |
|
783 |
1 to:8 do:[:i | |
|
784 |
self basicAt:bIdx put:(anInteger digitAt:i). |
|
785 |
bIdx := bIdx + delta. |
|
786 |
]. |
|
787 |
^ anInteger |
|
788 |
||
789 |
" |
|
790 |
|b| |
|
791 |
b := ByteArray new:8. |
|
792 |
b quadWordAtIndex:1 put:16r0807060504030201 MSB:false. |
|
793 |
b inspect |
|
794 |
" |
|
795 |
! |
|
796 |
||
1213
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
797 |
signedByteAt:index |
1505
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
798 |
"return the byte at index as a signed 8 bit value. |
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
799 |
This may be worth a primitive." |
1213
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
800 |
|
1505
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
801 |
^ (self at:index) signExtendedByteValue |
1213
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
802 |
|
1505
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
803 |
"/ |b "{ Class: SmallInteger }"| |
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
804 |
"/ |
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
805 |
"/ b := self at:index. |
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
806 |
"/ (b > 16r7F) ifTrue:[ |
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
807 |
"/ ^ b - 16r100 |
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
808 |
"/ ]. |
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
809 |
"/ ^ b |
1213
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
810 |
|
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
811 |
" |
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
812 |
|b| |
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
813 |
b := ByteArray new:2. |
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
814 |
b at:1 put:16rFF. |
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
815 |
b at:2 put:16r7F. |
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
816 |
b signedByteAt:1 |
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
817 |
" |
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
818 |
|
1505
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
819 |
"Modified: 1.7.1996 / 21:13:53 / cg" |
1213
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
820 |
! |
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
821 |
|
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
822 |
signedByteAt:index put:aSignedByteValue |
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
823 |
"return the byte at index as a signed 8 bit value. |
1505
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
824 |
Return the signedByteValue argument. |
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
825 |
This may be worth a primitive." |
1213
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
826 |
|
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
827 |
|b "{ Class: SmallInteger }"| |
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
828 |
|
1335 | 829 |
aSignedByteValue >= 0 ifTrue:[ |
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
830 |
b := aSignedByteValue |
1213
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
831 |
] ifFalse:[ |
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
832 |
b := 16r100 + aSignedByteValue |
1213
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
833 |
]. |
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
834 |
self at:index put:b. |
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
835 |
^ aSignedByteValue |
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
836 |
|
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
837 |
" |
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
838 |
|b| |
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
839 |
b := ByteArray new:2. |
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
840 |
b signedByteAt:1 put:-1. |
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
841 |
b at:1 |
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
842 |
" |
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
843 |
|
1505
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
844 |
"Modified: 1.7.1996 / 21:12:37 / cg" |
1213
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
845 |
! |
10d17781616f
added #signedByteAt: / #signedByteAt:put:
Claus Gittinger <cg@exept.de>
parents:
1169
diff
changeset
|
846 |
|
54 | 847 |
signedDoubleWordAt:index |
129 | 848 |
"return the 4-bytes starting at index as a signed Integer. |
1505
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
849 |
The value is retrieved in the machines natural byte order. |
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
850 |
This may be worth a primitive." |
54 | 851 |
|
852 |
|w| |
|
853 |
||
854 |
w := self doubleWordAt:index. |
|
1505
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
855 |
(w > (16r7FFFFFFF)) ifTrue:[ |
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
856 |
^ w - (16r100000000) |
54 | 857 |
]. |
858 |
^ w |
|
1505
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
859 |
|
54 | 860 |
" |
861 |
w := self doubleWordAt:index. |
|
862 |
(w > 16r7FFFFFFF) ifTrue:[ |
|
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
863 |
^ w - 16r100000000 |
54 | 864 |
]. |
865 |
^ w |
|
866 |
" |
|
867 |
||
868 |
" |
|
129 | 869 |
|b| |
870 |
b := ByteArray new:4. |
|
871 |
b doubleWordAt:1 put:16rFFFFFFFF. |
|
872 |
(b signedDoubleWordAt:1) |
|
54 | 873 |
" |
1505
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
874 |
|
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
875 |
"Modified: 1.7.1996 / 21:11:28 / cg" |
54 | 876 |
! |
877 |
||
359 | 878 |
signedDoubleWordAt:index MSB:msb |
879 |
"return the 4-bytes starting at index as a signed Integer. |
|
1505
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
880 |
Depending on msb, the value is retrieved MSB-first or LSB-first. |
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
881 |
This may be worth a primitive." |
54 | 882 |
|
359 | 883 |
|w| |
884 |
||
885 |
w := self doubleWordAt:index MSB:msb. |
|
1505
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
886 |
(w > (16r7FFFFFFF)) ifTrue:[ |
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
887 |
^ w - (16r100000000) |
359 | 888 |
]. |
889 |
^ w |
|
890 |
" |
|
891 |
w := self doubleWordAt:index. |
|
892 |
(w > 16r7FFFFFFF) ifTrue:[ |
|
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
893 |
^ w - 16r100000000 |
359 | 894 |
]. |
895 |
^ w |
|
54 | 896 |
" |
359 | 897 |
|
898 |
" |
|
899 |
|b| |
|
900 |
b := ByteArray new:4. |
|
901 |
b doubleWordAt:1 put:16rFFFFFFFF. |
|
902 |
(b signedDoubleWordAt:1) |
|
903 |
" |
|
1505
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
904 |
|
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
905 |
"Modified: 1.7.1996 / 21:11:33 / cg" |
359 | 906 |
! |
907 |
||
908 |
signedDoubleWordAt:index put:value |
|
909 |
"set the 4-bytes starting at index from the signed Integer value. |
|
1505
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
910 |
The value is stored in the machines natural byte order. |
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
911 |
This may be worth a primitive." |
359 | 912 |
|
913 |
|v| |
|
914 |
||
1331
5fdba9507742
oops - signedDoubleWordAt:put: was wrong for 0
Claus Gittinger <cg@exept.de>
parents:
1330
diff
changeset
|
915 |
value >= 0 ifTrue:[ |
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
916 |
v := value |
359 | 917 |
] ifFalse:[ |
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
918 |
v := value + 16r100000000 |
359 | 919 |
]. |
920 |
self doubleWordAt:index put:v. |
|
921 |
^ value |
|
922 |
||
923 |
" |
|
924 |
|b| |
|
925 |
b := ByteArray new:4. |
|
926 |
b signedDoubleWordAt:1 put:-1. |
|
927 |
(b doubleWordAt:1) printStringRadix:16 |
|
928 |
" |
|
1331
5fdba9507742
oops - signedDoubleWordAt:put: was wrong for 0
Claus Gittinger <cg@exept.de>
parents:
1330
diff
changeset
|
929 |
|
1505
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
930 |
"Modified: 1.7.1996 / 21:11:39 / cg" |
359 | 931 |
! |
932 |
||
933 |
signedDoubleWordAt:index put:value MSB:msb |
|
934 |
"set the 4-bytes starting at index from the signed Integer value. |
|
1505
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
935 |
Depending on msb, the value is stored MSB-first or LSB-first. |
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
936 |
This may be worth a primitive." |
359 | 937 |
|
938 |
|v| |
|
939 |
||
1335 | 940 |
value >= 0 ifTrue:[ |
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
941 |
v := value |
359 | 942 |
] ifFalse:[ |
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
943 |
v := value + 16r100000000 |
359 | 944 |
]. |
945 |
self doubleWordAt:index put:v MSB:msb. |
|
946 |
^ value |
|
947 |
||
129 | 948 |
" |
949 |
|b| |
|
950 |
b := ByteArray new:4. |
|
951 |
b signedDoubleWordAt:1 put:-1. |
|
952 |
(b doubleWordAt:1) printStringRadix:16 |
|
953 |
" |
|
1335 | 954 |
|
1505
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
955 |
"Modified: 1.7.1996 / 21:11:46 / cg" |
129 | 956 |
! |
54 | 957 |
|
563 | 958 |
signedWordAt:index |
959 |
"return the 2-bytes starting at index as a signed Integer. |
|
1505
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
960 |
The value is retrieved in the machines natural byte order. |
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
961 |
This may be worth a primitive." |
563 | 962 |
|
1505
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
963 |
^ (self wordAt:index) signExtendedShortValue |
129 | 964 |
|
1505
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
965 |
"/ |w "{ Class: SmallInteger }"| |
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
966 |
"/ |
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
967 |
"/ w := self wordAt:index. |
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
968 |
"/ (w > 16r7FFF) ifTrue:[ |
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
969 |
"/ ^ w - 16r10000 |
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
970 |
"/ ]. |
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
971 |
"/ ^ w |
129 | 972 |
|
563 | 973 |
" |
974 |
|b| |
|
975 |
b := ByteArray new:2. |
|
976 |
b wordAt:1 put:16rFFFF. |
|
977 |
b signedWordAt:1 |
|
978 |
" |
|
1505
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
979 |
|
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
980 |
"Modified: 1.7.1996 / 21:14:38 / cg" |
563 | 981 |
! |
982 |
||
983 |
signedWordAt:index MSB:msb |
|
984 |
"return the 2-bytes starting at index as a signed Integer. |
|
985 |
The value is retrieved MSB-first if the msb-arg is true, |
|
1505
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
986 |
LSB-first otherwise. |
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
987 |
This may be worth a primitive." |
563 | 988 |
|
1505
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
989 |
^ (self wordAt:index MSB:msb) signExtendedShortValue |
563 | 990 |
|
1505
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
991 |
"/ |w "{ Class: SmallInteger }"| |
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
992 |
"/ |
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
993 |
"/ w := self wordAt:index MSB:msb. |
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
994 |
"/ (w > 16r7FFF) ifTrue:[ |
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
995 |
"/ ^ w - 16r10000 |
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
996 |
"/ ]. |
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
997 |
"/ ^ w |
129 | 998 |
|
999 |
" |
|
1000 |
|b| |
|
563 | 1001 |
b := ByteArray new:2. |
1505
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
1002 |
b wordAt:1 put:16r0080. |
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
1003 |
b signedWordAt:1 MSB:true. |
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
1004 |
b signedWordAt:1 MSB:false. |
129 | 1005 |
" |
1505
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
1006 |
|
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
1007 |
"Modified: 1.7.1996 / 21:15:57 / cg" |
129 | 1008 |
! |
1009 |
||
563 | 1010 |
signedWordAt:index put:value |
1011 |
"set the 2-bytes starting at index from the signed Integer value. |
|
1012 |
The stored value must be in the range -32768 .. +32676. |
|
1505
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
1013 |
The value is stored in the machines natural byteorder. |
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
1014 |
This may be worth a primitive." |
129 | 1015 |
|
563 | 1016 |
|v| |
129 | 1017 |
|
1330
41c850b3b550
oops - signedWordAt:put: was wrong for 0
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
1018 |
value >= 0 ifTrue:[ |
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1019 |
v := value |
359 | 1020 |
] ifFalse:[ |
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1021 |
v := 16r10000 + value |
395 | 1022 |
]. |
563 | 1023 |
self wordAt:index put:v. |
1024 |
^ value |
|
129 | 1025 |
|
1026 |
" |
|
1027 |
|b| |
|
1330
41c850b3b550
oops - signedWordAt:put: was wrong for 0
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
1028 |
b := ByteArray new:6. |
563 | 1029 |
b signedWordAt:1 put:-1. |
1030 |
b signedWordAt:3 put:-2. |
|
1330
41c850b3b550
oops - signedWordAt:put: was wrong for 0
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
1031 |
b signedWordAt:5 put:0. |
563 | 1032 |
b inspect |
1033 |
" |
|
1330
41c850b3b550
oops - signedWordAt:put: was wrong for 0
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
1034 |
|
1505
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
1035 |
"Modified: 1.7.1996 / 21:12:07 / cg" |
563 | 1036 |
! |
1037 |
||
1038 |
signedWordAt:index put:value MSB:msb |
|
1039 |
"set the 2-bytes starting at index from the signed Integer value. |
|
1040 |
The stored value must be in the range -32768 .. +32676. |
|
1041 |
The value is stored MSB-first, if the msb-arg is true; |
|
1505
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
1042 |
LSB-first otherwise. |
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
1043 |
This may be worth a primitive." |
563 | 1044 |
|
1045 |
|v| |
|
1046 |
||
1330
41c850b3b550
oops - signedWordAt:put: was wrong for 0
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
1047 |
value >= 0 ifTrue:[ |
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1048 |
v := value |
563 | 1049 |
] ifFalse:[ |
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1050 |
v := 16r10000 + value |
563 | 1051 |
]. |
1052 |
self wordAt:index put:v MSB:msb. |
|
1053 |
^ value |
|
1054 |
||
1055 |
" |
|
1056 |
|b| |
|
1057 |
b := ByteArray new:4. |
|
1058 |
b signedWordAt:1 put:-1. |
|
1059 |
b signedWordAt:3 put:-2. |
|
129 | 1060 |
b inspect |
1061 |
" |
|
1330
41c850b3b550
oops - signedWordAt:put: was wrong for 0
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
1062 |
|
1505
45b665448fac
can now use largeInteger constants
Claus Gittinger <cg@exept.de>
parents:
1472
diff
changeset
|
1063 |
"Modified: 1.7.1996 / 21:12:13 / cg" |
54 | 1064 |
! |
1065 |
||
1649
74626d4c1d96
added string extraction stuff
Claus Gittinger <cg@exept.de>
parents:
1527
diff
changeset
|
1066 |
stringAt:index size:count |
74626d4c1d96
added string extraction stuff
Claus Gittinger <cg@exept.de>
parents:
1527
diff
changeset
|
1067 |
"extract a string, given initial index and number of characters (bytes)" |
74626d4c1d96
added string extraction stuff
Claus Gittinger <cg@exept.de>
parents:
1527
diff
changeset
|
1068 |
|
74626d4c1d96
added string extraction stuff
Claus Gittinger <cg@exept.de>
parents:
1527
diff
changeset
|
1069 |
^ (self copyFrom:index to:(index + count - 1)) asString |
74626d4c1d96
added string extraction stuff
Claus Gittinger <cg@exept.de>
parents:
1527
diff
changeset
|
1070 |
|
74626d4c1d96
added string extraction stuff
Claus Gittinger <cg@exept.de>
parents:
1527
diff
changeset
|
1071 |
"Modified: 9.9.1996 / 15:28:08 / cg" |
74626d4c1d96
added string extraction stuff
Claus Gittinger <cg@exept.de>
parents:
1527
diff
changeset
|
1072 |
"Created: 9.9.1996 / 15:28:48 / cg" |
74626d4c1d96
added string extraction stuff
Claus Gittinger <cg@exept.de>
parents:
1527
diff
changeset
|
1073 |
! |
74626d4c1d96
added string extraction stuff
Claus Gittinger <cg@exept.de>
parents:
1527
diff
changeset
|
1074 |
|
563 | 1075 |
wordAt:index |
1076 |
"return the 2-bytes starting at index as an (unsigned) Integer. |
|
1077 |
The value is retrieved in the machines natural byte order |
|
1078 |
Question: should it be retrieve signed values ? (see ByteArray>>signedWordAt:)" |
|
1079 |
||
1080 |
%{ /* NOCONTEXT */ |
|
54 | 1081 |
|
563 | 1082 |
REGISTER int indx; |
1083 |
int nIndex; |
|
1084 |
union { |
|
2950 | 1085 |
unsigned char u_char[2]; |
563 | 1086 |
unsigned short u_ushort; |
1087 |
} val; |
|
1088 |
OBJ cls; |
|
2950 | 1089 |
unsigned char *byteP; |
54 | 1090 |
|
563 | 1091 |
if (__isSmallInteger(index)) { |
1133 | 1092 |
indx = __intVal(index); |
563 | 1093 |
if (indx > 0) { |
1133 | 1094 |
if ((cls = __qClass(self)) != @global(ByteArray)) |
1095 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
563 | 1096 |
nIndex = __byteArraySize(self); |
1097 |
if ((indx+1) <= nIndex) { |
|
2950 | 1098 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
1099 |
val.u_char[0] = byteP[0]; |
|
1100 |
val.u_char[1] = byteP[1]; |
|
1133 | 1101 |
RETURN ( __MKSMALLINT(val.u_ushort) ); |
563 | 1102 |
} |
1103 |
} |
|
1104 |
} |
|
1105 |
%}. |
|
1106 |
^ SubscriptOutOfBoundsSignal raise. |
|
54 | 1107 |
! |
1108 |
||
563 | 1109 |
wordAt:index MSB:msb |
1110 |
"return the 2-bytes starting at index as an (unsigned) Integer. |
|
1111 |
The value is retrieved MSB (high 8 bits at lower index) if msb is true; |
|
1112 |
LSB-first (i.e. low 8-bits at lower byte index) if its false. |
|
1113 |
Question: should it be retrieve signed values ? (see ByteArray>>signedWordAt:)" |
|
1114 |
||
1115 |
%{ /* NOCONTEXT */ |
|
1116 |
||
1117 |
REGISTER int indx; |
|
1118 |
int nIndex; |
|
1119 |
int val; |
|
1120 |
OBJ cls; |
|
2950 | 1121 |
unsigned char *byteP; |
54 | 1122 |
|
563 | 1123 |
if (__isSmallInteger(index)) { |
1133 | 1124 |
indx = __intVal(index); |
563 | 1125 |
if (indx > 0) { |
1133 | 1126 |
if ((cls = __qClass(self)) != @global(ByteArray)) |
1127 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
563 | 1128 |
nIndex = __byteArraySize(self); |
1129 |
if ((indx+1) <= nIndex) { |
|
2950 | 1130 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
563 | 1131 |
if (msb == true) { |
2950 | 1132 |
val = byteP[0]; |
1133 |
val = (val << 8) + byteP[1]; |
|
563 | 1134 |
} else { |
2950 | 1135 |
val = byteP[1]; |
1136 |
val = (val << 8) + byteP[0]; |
|
563 | 1137 |
} |
1133 | 1138 |
RETURN ( __MKSMALLINT(val) ); |
563 | 1139 |
} |
1140 |
} |
|
1141 |
} |
|
1142 |
%}. |
|
1143 |
^ SubscriptOutOfBoundsSignal raise. |
|
54 | 1144 |
! |
1145 |
||
563 | 1146 |
wordAt:index put:value |
1147 |
"set the 2-bytes starting at index from the (unsigned) Integer value. |
|
1148 |
The stored value must be in the range 0 .. 16rFFFF. |
|
1149 |
The value is stored in the machines natural byteorder. |
|
1150 |
Question: should it accept signed values ? (see ByteArray>>signedWordAt:put:)" |
|
1151 |
||
1152 |
%{ /* NOCONTEXT */ |
|
54 | 1153 |
|
563 | 1154 |
REGISTER int indx; |
1155 |
int nIndex; |
|
1156 |
int v; |
|
1157 |
union { |
|
2950 | 1158 |
unsigned char u_char[2]; |
563 | 1159 |
unsigned short u_ushort; |
1160 |
} val; |
|
1161 |
OBJ cls; |
|
2950 | 1162 |
unsigned char *byteP; |
129 | 1163 |
|
563 | 1164 |
if (__bothSmallInteger(index, value)) { |
1133 | 1165 |
indx = __intVal(index); |
563 | 1166 |
if (indx > 0) { |
1133 | 1167 |
if ((cls = __qClass(self)) != @global(ByteArray)) |
1168 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
563 | 1169 |
nIndex = __byteArraySize(self); |
1170 |
if ((indx+1) <= nIndex) { |
|
1133 | 1171 |
val.u_ushort = v = __intVal(value); |
563 | 1172 |
if ((v & ~0xFFFF) == 0 /* i.e. (val >= 0) && (val <= 0xFFFF) */) { |
2950 | 1173 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
1174 |
byteP[0] = val.u_char[0]; |
|
1175 |
byteP[1] = val.u_char[1]; |
|
563 | 1176 |
RETURN ( value ); |
1177 |
} |
|
1178 |
} |
|
1179 |
} |
|
1180 |
} |
|
1181 |
%}. |
|
1182 |
((value < 0) or:[value > 16rFFFF]) ifTrue:[ |
|
1183 |
^ self elementBoundsError |
|
1184 |
]. |
|
1185 |
^ SubscriptOutOfBoundsSignal raise. |
|
129 | 1186 |
|
1187 |
" |
|
563 | 1188 |
|b| |
1189 |
b := ByteArray new:4. |
|
1190 |
b wordAt:1 put:16r0102. |
|
1191 |
b wordAt:3 put:16r0304. |
|
1192 |
b inspect |
|
129 | 1193 |
" |
1194 |
! |
|
1195 |
||
563 | 1196 |
wordAt:index put:value MSB:msb |
1197 |
"set the 2-bytes starting at index from the (unsigned) Integer value. |
|
1198 |
The stored value must be in the range 0 .. 16rFFFF. |
|
1199 |
The value is stored LSB-first (i.e. the low 8bits are stored at the |
|
1200 |
lower index) if msb is false, MSB-first otherwise. |
|
1201 |
Question: should it accept signed values ? (see ByteArray>>signedWordAt:put:)" |
|
1202 |
||
1203 |
%{ /* NOCONTEXT */ |
|
1204 |
||
1205 |
REGISTER int indx; |
|
1206 |
int nIndex; |
|
1207 |
int val; |
|
1208 |
OBJ cls; |
|
2950 | 1209 |
unsigned char *byteP; |
563 | 1210 |
|
1211 |
if (__bothSmallInteger(index, value)) { |
|
1133 | 1212 |
indx = __intVal(index); |
563 | 1213 |
if (indx > 0) { |
1133 | 1214 |
if ((cls = __qClass(self)) != @global(ByteArray)) |
1215 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
563 | 1216 |
nIndex = __byteArraySize(self); |
1217 |
if ((indx+1) <= nIndex) { |
|
1133 | 1218 |
val = __intVal(value); |
563 | 1219 |
if ((val & ~0xFFFF) == 0 /* i.e. (val >= 0) && (val <= 0xFFFF) */) { |
2950 | 1220 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
563 | 1221 |
if (msb == true) { |
2950 | 1222 |
byteP[1] = val & 0xFF; |
1223 |
byteP[0] = (val>>8) & 0xFF; |
|
563 | 1224 |
} else { |
2950 | 1225 |
byteP[0] = val & 0xFF; |
1226 |
byteP[1] = (val>>8) & 0xFF; |
|
563 | 1227 |
} |
1228 |
RETURN ( value ); |
|
1229 |
} |
|
1230 |
} |
|
1231 |
} |
|
1232 |
} |
|
1233 |
%}. |
|
1234 |
((value < 0) or:[value > 16rFFFF]) ifTrue:[ |
|
1235 |
^ self elementBoundsError |
|
1236 |
]. |
|
1237 |
^ SubscriptOutOfBoundsSignal raise. |
|
129 | 1238 |
|
1239 |
" |
|
563 | 1240 |
|b| |
1241 |
b := ByteArray new:8. |
|
1242 |
b wordAt:1 put:16r0102 MSB:false. |
|
1243 |
b wordAt:3 put:16r0304 MSB:false. |
|
1244 |
b wordAt:5 put:16r0102 MSB:true. |
|
1245 |
b wordAt:7 put:16r0304 MSB:true. |
|
1246 |
b inspect |
|
129 | 1247 |
" |
1649
74626d4c1d96
added string extraction stuff
Claus Gittinger <cg@exept.de>
parents:
1527
diff
changeset
|
1248 |
! |
74626d4c1d96
added string extraction stuff
Claus Gittinger <cg@exept.de>
parents:
1527
diff
changeset
|
1249 |
|
74626d4c1d96
added string extraction stuff
Claus Gittinger <cg@exept.de>
parents:
1527
diff
changeset
|
1250 |
zeroByteStringAt:index maximumSize:count |
74626d4c1d96
added string extraction stuff
Claus Gittinger <cg@exept.de>
parents:
1527
diff
changeset
|
1251 |
"extract a zeroByte-delimited string, given initial index and |
74626d4c1d96
added string extraction stuff
Claus Gittinger <cg@exept.de>
parents:
1527
diff
changeset
|
1252 |
maximum number of characters (bytes)" |
74626d4c1d96
added string extraction stuff
Claus Gittinger <cg@exept.de>
parents:
1527
diff
changeset
|
1253 |
|
74626d4c1d96
added string extraction stuff
Claus Gittinger <cg@exept.de>
parents:
1527
diff
changeset
|
1254 |
|bytes idx| |
74626d4c1d96
added string extraction stuff
Claus Gittinger <cg@exept.de>
parents:
1527
diff
changeset
|
1255 |
|
74626d4c1d96
added string extraction stuff
Claus Gittinger <cg@exept.de>
parents:
1527
diff
changeset
|
1256 |
bytes := self copyFrom:index to:(index + count - 1). |
74626d4c1d96
added string extraction stuff
Claus Gittinger <cg@exept.de>
parents:
1527
diff
changeset
|
1257 |
idx := bytes indexOf:0. |
74626d4c1d96
added string extraction stuff
Claus Gittinger <cg@exept.de>
parents:
1527
diff
changeset
|
1258 |
idx ~~ 0 ifTrue:[ bytes := bytes copyTo:idx-1 ]. |
74626d4c1d96
added string extraction stuff
Claus Gittinger <cg@exept.de>
parents:
1527
diff
changeset
|
1259 |
^ bytes asString |
74626d4c1d96
added string extraction stuff
Claus Gittinger <cg@exept.de>
parents:
1527
diff
changeset
|
1260 |
|
74626d4c1d96
added string extraction stuff
Claus Gittinger <cg@exept.de>
parents:
1527
diff
changeset
|
1261 |
"Created: 9.9.1996 / 15:28:34 / cg" |
1 | 1262 |
! ! |
1263 |
||
550 | 1264 |
!ByteArray methodsFor:'binary storage'! |
1265 |
||
1266 |
storeBinaryDefinitionOn:stream manager:manager |
|
1267 |
"append a binary representation of the receiver onto stream. |
|
3071 | 1268 |
Redefined since short ByteArrays can be stored with a special type code |
1261 | 1269 |
in a more compact way. |
3071 | 1270 |
This is an internal interface for the binary storage mechanism." |
550 | 1271 |
|
2464 | 1272 |
|myClass myBasicSize| |
1273 |
||
550 | 1274 |
"not, if I have named instance variables" |
2464 | 1275 |
(myClass := self class) instSize ~~ 0 ifTrue:[ |
3071 | 1276 |
^ super storeBinaryDefinitionOn:stream manager:manager |
550 | 1277 |
]. |
3071 | 1278 |
|
1279 |
myBasicSize := self basicSize. |
|
1280 |
||
1281 |
"/ can use a more compact representation; |
|
1282 |
"/ but not for subclasses ... |
|
1283 |
||
1284 |
((myClass == ByteArray) |
|
1285 |
and:[myBasicSize <= 255]) ifTrue:[ |
|
1286 |
"/ special encoding: <codeForByteArray> <len> <bytes> ... |
|
1287 |
stream nextPut:(manager codeForByteArray); nextPut:myBasicSize. |
|
1288 |
] ifFalse:[ |
|
1289 |
manager putIdOfClass:myClass on:stream. |
|
1290 |
stream nextNumber:4 put:myBasicSize. |
|
1291 |
]. |
|
2464 | 1292 |
stream nextPutBytes:myBasicSize from:self startingAt:1. |
1261 | 1293 |
|
3071 | 1294 |
"Modified: / 2.11.1997 / 15:28:45 / cg" |
550 | 1295 |
! ! |
1296 |
||
12 | 1297 |
!ByteArray methodsFor:'converting'! |
1298 |
||
1299 |
asByteArray |
|
1300 |
"return the receiver as a byteArray" |
|
1301 |
||
1302 |
"could be an instance of a subclass..." |
|
1303 |
self class == ByteArray ifTrue:[ |
|
155 | 1304 |
^ self |
12 | 1305 |
]. |
1306 |
^ super asByteArray |
|
129 | 1307 |
|
1308 |
" |
|
1309 |
'hello world' asByteArray |
|
1310 |
#(1 2 3 4 5 6 7) asByteArray |
|
1311 |
#(1 2 256 4 5 6 7) asByteArray |
|
1312 |
" |
|
12 | 1313 |
! ! |
1314 |
||
92 | 1315 |
!ByteArray methodsFor:'copying'! |
1316 |
||
1317 |
copyFrom:start to:stop |
|
1318 |
"return the subcollection starting at index start, anInteger and ending |
|
1319 |
at stop, anInteger. |
|
1320 |
- reimplemented here for speed" |
|
1321 |
||
1322 |
%{ /* NOCONTEXT */ |
|
1323 |
||
1324 |
REGISTER unsigned char *srcp; |
|
1325 |
REGISTER unsigned char *dstp; |
|
1326 |
REGISTER int count; |
|
1327 |
int len, index1, index2, sz; |
|
1328 |
OBJ newByteArray; |
|
1329 |
||
1330 |
if (__isByteArray(self) |
|
249 | 1331 |
&& __bothSmallInteger(start, stop)) { |
1332 |
len = __byteArraySize(self); |
|
1133 | 1333 |
index1 = __intVal(start); |
1334 |
index2 = __intVal(stop); |
|
92 | 1335 |
|
155 | 1336 |
if ((index1 <= index2) && (index1 > 0)) { |
1337 |
if (index2 <= len) { |
|
1338 |
count = index2 - index1 + 1; |
|
834 | 1339 |
__PROTECT_CONTEXT__ |
155 | 1340 |
sz = OHDR_SIZE + count; |
2915 | 1341 |
__qNew(newByteArray, sz); /* OBJECT ALLOCATION */ |
834 | 1342 |
__UNPROTECT_CONTEXT__ |
155 | 1343 |
if (newByteArray != nil) { |
1133 | 1344 |
__InstPtr(newByteArray)->o_class = ByteArray; |
1345 |
dstp = __ByteArrayInstPtr(newByteArray)->ba_element; |
|
2648 | 1346 |
srcp = __ByteArrayInstPtr(self)->ba_element + index1 - 1; |
1347 |
||
1348 |
#ifdef bcopy4 |
|
2915 | 1349 |
if ((((unsigned INT)srcp & 3) == 0) |
1350 |
&& (((unsigned INT)dstp & 3) == 0)) { |
|
1351 |
/* copy aligned part */ |
|
1352 |
int nW = count >> 2; |
|
2648 | 1353 |
|
2915 | 1354 |
if (count & 3) { |
1355 |
nW++; |
|
1356 |
} |
|
1357 |
bcopy4(srcp, dstp, nW); |
|
1358 |
RETURN ( newByteArray ); |
|
1359 |
} |
|
2648 | 1360 |
#endif /* bcopy4 */ |
1361 |
||
92 | 1362 |
#ifdef FAST_MEMCPY |
2648 | 1363 |
bcopy(srcp, dstp, count); |
92 | 1364 |
#else |
155 | 1365 |
while (count--) { |
1366 |
*dstp++ = *srcp++; |
|
1367 |
} |
|
92 | 1368 |
#endif |
155 | 1369 |
RETURN ( newByteArray ); |
1370 |
} |
|
1371 |
} |
|
1372 |
} |
|
92 | 1373 |
} |
1374 |
%}. |
|
1375 |
" |
|
1376 |
fall back in case of non-integer index or out-of-bound index; |
|
1377 |
will eventually lead to an out-of-bound signal raise |
|
1378 |
" |
|
1379 |
^ super copyFrom:start to:stop |
|
1380 |
! ! |
|
1381 |
||
1 | 1382 |
!ByteArray methodsFor:'filling and replacing'! |
1383 |
||
155 | 1384 |
from:start to:stop put:aNumber |
1385 |
"fill part of the receiver with aNumber. |
|
1386 |
- reimplemented here for speed" |
|
1387 |
||
1388 |
%{ /* NOCONTEXT */ |
|
1389 |
||
1390 |
REGISTER unsigned char *dstp; |
|
1391 |
REGISTER int count, value; |
|
249 | 1392 |
int len, index1, index2; |
1393 |
OBJ cls; |
|
155 | 1394 |
|
249 | 1395 |
if (__isSmallInteger(aNumber) |
1002
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
1396 |
&& __bothSmallInteger(start, stop) |
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
1397 |
&& __isBytes(self)) { |
2915 | 1398 |
len = __byteArraySize(self); |
1399 |
index1 = __intVal(start); |
|
1400 |
index2 = __intVal(stop); |
|
2866 | 1401 |
|
2915 | 1402 |
dstp = __ByteArrayInstPtr(self)->ba_element + index1 - 1; |
1403 |
if ((cls = __qClass(self)) != @global(ByteArray)) { |
|
1404 |
int nInst; |
|
155 | 1405 |
|
2915 | 1406 |
nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
1407 |
dstp += nInst; |
|
1408 |
len -= nInst; |
|
1409 |
} |
|
249 | 1410 |
|
2915 | 1411 |
value = __intVal(aNumber); |
1412 |
if (((unsigned)value <= 0xFF) /* i.e. (value >= 0) && (value <= 255) */ |
|
1413 |
&& (index1 <= index2) |
|
1414 |
&& (index1 > 0)) { |
|
1415 |
if (index2 <= len) { |
|
1416 |
count = index2 - index1 + 1; |
|
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1417 |
|
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1418 |
#ifdef memset4 |
2915 | 1419 |
if (count > 20) { |
1420 |
while (((unsigned INT)dstp & 3) != 0) { |
|
1421 |
*dstp++ = value; |
|
1422 |
count--; |
|
1423 |
} |
|
1424 |
{ |
|
1425 |
int n4 = count & ~3; |
|
1426 |
int v4, nW; |
|
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1427 |
|
2915 | 1428 |
v4 = (value << 8) | value; |
1429 |
v4 = (v4 << 16) | v4; |
|
1430 |
nW = n4>>2; |
|
1431 |
memset4(dstp, v4, nW); |
|
1432 |
count -= n4; |
|
1433 |
dstp += n4; |
|
1434 |
} |
|
1435 |
while (count--) { |
|
1436 |
*dstp++ = value; |
|
1437 |
} |
|
1438 |
RETURN (self); |
|
1439 |
} |
|
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1440 |
#endif /* memset4 */ |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1441 |
|
155 | 1442 |
#ifdef FAST_MEMSET |
2915 | 1443 |
memset(dstp, value, count); |
155 | 1444 |
#else |
422 | 1445 |
# ifdef UNROLL_LOOPS |
2915 | 1446 |
while (count >= 8) { |
1447 |
dstp[0] = dstp[1] = dstp[2] = dstp[3] = |
|
1448 |
dstp[4] = dstp[5] = dstp[6] = dstp[7] = value; |
|
1449 |
dstp += 8; |
|
1450 |
count -= 8; |
|
1451 |
} |
|
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1452 |
# endif /* UNROLL_LOOPS */ |
2915 | 1453 |
while (count--) { |
1454 |
*dstp++ = value; |
|
1455 |
} |
|
155 | 1456 |
#endif |
2915 | 1457 |
RETURN (self); |
1458 |
} |
|
1459 |
} |
|
155 | 1460 |
} |
1461 |
%}. |
|
1462 |
" |
|
1463 |
fall back in case of non-integer index or out-of-bound index/value; |
|
1464 |
will eventually lead to an out-of-bound signal raise |
|
1465 |
" |
|
1466 |
^ super from:start to:stop put:aNumber |
|
1467 |
! |
|
1468 |
||
249 | 1469 |
replaceBytesFrom:start to:stop with:aCollection startingAt:repStart |
1470 |
"replace elements from another collection, which must be a ByteArray |
|
1471 |
or String." |
|
1 | 1472 |
|
54 | 1473 |
%{ /* NOCONTEXT */ |
1 | 1474 |
|
54 | 1475 |
int nIndex, repNIndex; |
1476 |
int startIndex, stopIndex; |
|
1477 |
REGISTER unsigned char *src; |
|
1478 |
REGISTER int repStartIndex; |
|
1479 |
int repStopIndex, count; |
|
1480 |
REGISTER unsigned char *dst; |
|
249 | 1481 |
OBJ cls; |
1 | 1482 |
|
2894 | 1483 |
#ifndef NO_PRIM_BYTEARR |
249 | 1484 |
if (__isBytes(aCollection) |
1002
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
1485 |
&& __isBytes(self) |
249 | 1486 |
&& __bothSmallInteger(start, stop) |
1487 |
&& __isSmallInteger(repStart)) { |
|
2915 | 1488 |
startIndex = __intVal(start) - 1; |
1489 |
if (startIndex >= 0) { |
|
1490 |
dst = (__ByteArrayInstPtr(self)->ba_element) + startIndex; |
|
1491 |
nIndex = __byteArraySize(self); |
|
2866 | 1492 |
|
2915 | 1493 |
if ((cls = __qClass(self)) != @global(ByteArray)) { |
1494 |
int nInst; |
|
249 | 1495 |
|
2915 | 1496 |
nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
1497 |
dst += nInst; |
|
1498 |
nIndex -= nInst; |
|
1499 |
} |
|
2866 | 1500 |
|
2915 | 1501 |
stopIndex = __intVal(stop) - 1; |
1502 |
count = stopIndex - startIndex + 1; |
|
1503 |
if (count == 0) { |
|
1504 |
RETURN ( self ); |
|
1505 |
} |
|
249 | 1506 |
|
2915 | 1507 |
if ((count > 0) && (stopIndex < nIndex)) { |
1508 |
repStartIndex = __intVal(repStart) - 1; |
|
1509 |
if (repStartIndex >= 0) { |
|
1510 |
repNIndex = __qSize(aCollection) - OHDR_SIZE; |
|
1511 |
src = (__ByteArrayInstPtr(aCollection)->ba_element) + repStartIndex; |
|
1512 |
if ((cls = __qClass(aCollection)) != @global(ByteArray)) { |
|
1513 |
int nInst; |
|
2866 | 1514 |
|
2915 | 1515 |
nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
1516 |
src += nInst; |
|
1517 |
repNIndex -= nInst; |
|
1518 |
} |
|
249 | 1519 |
|
2915 | 1520 |
repStopIndex = repStartIndex + (stopIndex - startIndex); |
1521 |
if (repStopIndex < repNIndex) { |
|
1522 |
if (aCollection == self) { |
|
1523 |
/* take care of overlapping copy */ |
|
1524 |
if (src < dst) { |
|
1525 |
/* must do a reverse copy */ |
|
1526 |
src += count; |
|
1527 |
dst += count; |
|
1528 |
while (count-- > 0) { |
|
1529 |
*--dst = *--src; |
|
1530 |
} |
|
1531 |
RETURN ( self ); |
|
1532 |
} |
|
1533 |
} |
|
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1534 |
|
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1535 |
#ifdef bcopy4 |
2915 | 1536 |
if ((((unsigned INT)src & 3) == 0) |
1537 |
&& (((unsigned INT)dst & 3) == 0)) { |
|
1538 |
/* copy aligned part */ |
|
1539 |
int nW = count >> 2; |
|
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1540 |
|
2915 | 1541 |
bcopy4(src, dst, nW); |
1542 |
if ((count = count & 3) != 0) { |
|
1543 |
/* copy any remaining part */ |
|
1544 |
src += (nW<<2); |
|
1545 |
dst += (nW<<2); |
|
1546 |
while (count--) { |
|
1547 |
*dst++ = *src++; |
|
1548 |
} |
|
1549 |
} |
|
1550 |
RETURN ( self ); |
|
1551 |
} |
|
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1552 |
#endif /* bcopy4 */ |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1553 |
|
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1554 |
#ifdef FAST_MEMCPY |
2915 | 1555 |
bcopy(src, dst, count); |
1 | 1556 |
#else |
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1557 |
# ifdef UNROLL_LOOPS |
2915 | 1558 |
while (count >= 8) { |
1559 |
dst[0] = src[0]; dst[1] = src[1]; |
|
1560 |
dst[2] = src[2]; dst[3] = src[3]; |
|
1561 |
dst[4] = src[4]; dst[5] = src[5]; |
|
1562 |
dst[6] = src[6]; dst[7] = src[7]; |
|
1563 |
dst += 8; src += 8; |
|
1564 |
count -= 8; |
|
1565 |
} |
|
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1566 |
# endif /* UNROLL_LOOPS */ |
2915 | 1567 |
while (count-- > 0) { |
1568 |
*dst++ = *src++; |
|
1569 |
} |
|
1 | 1570 |
#endif |
2915 | 1571 |
RETURN ( self ); |
1572 |
} |
|
1573 |
} |
|
1574 |
} |
|
1575 |
} |
|
54 | 1576 |
} |
2894 | 1577 |
#endif |
437 | 1578 |
%}. |
1579 |
" |
|
1580 |
fall back in case of non-ByteArray argument, |
|
1581 |
or for the error report if any index is invalid |
|
1582 |
" |
|
1583 |
^ super replaceFrom:start to:stop with:aCollection startingAt:repStart |
|
563 | 1584 |
! |
1585 |
||
1586 |
replaceFrom:start to:stop with:aCollection startingAt:repStart |
|
1169 | 1587 |
"replace elements in the receiver between index start and stop, |
1588 |
with elements taken from replacementCollection starting at repStart." |
|
563 | 1589 |
|
1590 |
(aCollection class == self class) ifTrue:[ |
|
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1591 |
^ self replaceBytesFrom:start to:stop with:aCollection startingAt:repStart |
563 | 1592 |
]. |
1593 |
^ super replaceFrom:start to:stop with:aCollection startingAt:repStart |
|
1169 | 1594 |
|
1595 |
"Modified: 13.4.1996 / 12:16:46 / cg" |
|
1 | 1596 |
! ! |
1597 |
||
129 | 1598 |
!ByteArray methodsFor:'image manipulation support'! |
1 | 1599 |
|
2866 | 1600 |
compressPixels:nBitsPerPixel width:width height:height into:aByteArray mapping:aMapByteArray |
129 | 1601 |
"given the receiver with 8-bit pixels, compress them into aByteArray |
1602 |
with nBitsPerPixel-depth pixels. The width/height-arguments are needed |
|
1603 |
to allow for any padding. On the fly, the source bytes are translated |
|
1604 |
using aMapByteArray (if non-nil). |
|
1605 |
Notice that smalltalk indexing begins at 1; thus the map-index for a byte |
|
1606 |
value of n is found in map at:(n + 1). |
|
1607 |
Output bits are filled left-to right, i.e. the first byte in the input |
|
1608 |
corresponds to the high bit(s) if the first byte in the input. |
|
1609 |
This method can be used to convert 8-bit image data to mono, 2-bit and 4-bit |
|
1610 |
bitmaps. |
|
1611 |
It can also be used to compress byte-arrays into bitArrays." |
|
1612 |
||
1613 |
%{ /* NOCONTEXT */ |
|
1614 |
||
1615 |
REGISTER unsigned char *src, *dst; |
|
1616 |
REGISTER int wrun; |
|
1617 |
unsigned char *dstNext; |
|
1472
abaa73dc0e9a
check for destination overrun
Claus Gittinger <cg@exept.de>
parents:
1459
diff
changeset
|
1618 |
unsigned char *dstEnd; |
129 | 1619 |
int bytesPerRow, mask, shift0, shift; |
1620 |
int w, h, hrun; |
|
1621 |
int srcBytes, dstBytes; |
|
1622 |
int bitsPerPixel; |
|
1623 |
int bits; |
|
1624 |
int ncells; |
|
1625 |
unsigned char *map; |
|
1626 |
||
1133 | 1627 |
if ((__qClass(self) == @global(ByteArray)) |
1628 |
&& (__qClass(aByteArray) == @global(ByteArray)) |
|
249 | 1629 |
&& __isSmallInteger(nBitsPerPixel) |
1630 |
&& __bothSmallInteger(height, width)) { |
|
2915 | 1631 |
if ((aMapByteArray != nil) |
1632 |
&& (__Class(aMapByteArray) == @global(ByteArray))) { |
|
1633 |
map = __ByteArrayInstPtr(aMapByteArray)->ba_element; |
|
1634 |
} else { |
|
1635 |
map = (unsigned char *)0; |
|
1636 |
} |
|
129 | 1637 |
|
2915 | 1638 |
bitsPerPixel = __intVal(nBitsPerPixel); |
1639 |
w = __intVal(width); |
|
1640 |
h = __intVal(height); |
|
1641 |
src = __ByteArrayInstPtr(self)->ba_element; |
|
1642 |
dst = __ByteArrayInstPtr(aByteArray)->ba_element; |
|
1643 |
dstEnd = dst + __byteArraySize(aByteArray); |
|
1644 |
switch (bitsPerPixel) { |
|
1645 |
case 1: |
|
1646 |
mask = 0x01; |
|
1647 |
break; |
|
1648 |
case 2: |
|
1649 |
mask = 0x03; |
|
1650 |
break; |
|
1651 |
case 4: |
|
1652 |
mask = 0x0F; |
|
1653 |
break; |
|
1654 |
case 8: |
|
1655 |
mask = 0xFF; |
|
1656 |
break; |
|
1657 |
default: |
|
1658 |
printf("invalid depth in compressPixels\n"); |
|
1659 |
goto fail; |
|
1660 |
} |
|
1661 |
if (map) { |
|
1662 |
/* |
|
1663 |
* if a map is present, it must have entries for |
|
1664 |
* all possible byte-values (i.e. its size must be >= 256) |
|
1665 |
*/ |
|
1666 |
if ((__qSize(aMapByteArray) - OHDR_SIZE) < 256) { |
|
1667 |
printf("invalid map in compressPixels\n"); |
|
1668 |
goto fail; |
|
1669 |
} |
|
1670 |
} |
|
129 | 1671 |
|
2915 | 1672 |
bytesPerRow = (w * bitsPerPixel + 7) / 8; |
1673 |
dstBytes = bytesPerRow * h; |
|
1674 |
srcBytes = w * h; |
|
2866 | 1675 |
|
2915 | 1676 |
if ((__byteArraySize(self) >= srcBytes) |
1677 |
&& (__byteArraySize(aByteArray) >= dstBytes)) { |
|
1678 |
for (hrun=h; hrun; hrun--) { |
|
1679 |
dstNext = dst + bytesPerRow; |
|
1680 |
bits = 0; shift = 8; |
|
1681 |
if (map) { |
|
1682 |
for (wrun=w; wrun; wrun--) { |
|
1683 |
bits = (bits << bitsPerPixel) | (map[*src++] & mask); |
|
1684 |
shift -= bitsPerPixel; |
|
1685 |
if (shift == 0) { |
|
1686 |
if (dst == dstEnd) goto fail; |
|
1687 |
*dst++ = bits; |
|
1688 |
bits = 0; shift = 8; |
|
1689 |
} |
|
1690 |
} |
|
1691 |
} else { |
|
1692 |
for (wrun=w; wrun; wrun--) { |
|
1693 |
bits = (bits << bitsPerPixel) | (*src++ & mask); |
|
1694 |
shift -= bitsPerPixel; |
|
1695 |
if (shift == 0) { |
|
1696 |
if (dst == dstEnd) goto fail; |
|
1697 |
*dst++ = bits; |
|
1698 |
bits = 0; shift = 8; |
|
1699 |
} |
|
1700 |
} |
|
1701 |
} |
|
1702 |
if (shift != 8) { |
|
1703 |
if (dst == dstEnd) goto fail; |
|
1704 |
*dst = bits; |
|
1705 |
} |
|
1706 |
dst = dstNext; |
|
1707 |
} |
|
1708 |
RETURN ( self ); |
|
1709 |
} |
|
129 | 1710 |
} |
1711 |
fail: ; |
|
2866 | 1712 |
%}. |
129 | 1713 |
self primitiveFailed |
1714 |
||
1715 |
"Example1: |
|
1716 |
compress 1 byte-per-pixel bitmap to 1-bit-per-pixel bitmap |
|
1717 |
" |
|
1718 |
" |
|
1719 |
|inBits outBits| |
|
1720 |
||
1721 |
inBits := #[0 0 0 0 1 1 1 1 |
|
2915 | 1722 |
0 0 1 1 0 0 1 1 |
1723 |
0 1 0 1 0 1 0 1 |
|
1724 |
1 1 1 1 0 0 0 0]. |
|
129 | 1725 |
outBits := ByteArray new:4. |
1726 |
inBits compressPixels:1 width:8 height:4 |
|
2915 | 1727 |
into:outBits mapping:nil. |
129 | 1728 |
outBits inspect |
1729 |
" |
|
1730 |
||
1731 |
"Example2: |
|
1732 |
compress byte-array into a bitArray, translating 99 to 0-bits, |
|
1733 |
and 176 to 1-bits. (just a stupid example) |
|
1734 |
" |
|
1735 |
" |
|
1736 |
|inBits outBits map| |
|
1737 |
||
1738 |
inBits := #[176 176 176 176 99 99 99 99 176 176 99 99 176 99 176 99]. |
|
1739 |
map := ByteArray new:256. |
|
1740 |
map at:176+1 put:1. |
|
1741 |
||
1742 |
outBits := ByteArray new:2. |
|
1743 |
inBits compressPixels:1 width:16 height:1 |
|
2915 | 1744 |
into:outBits mapping:map. |
129 | 1745 |
outBits inspect |
1746 |
" |
|
1747 |
||
1748 |
"Example3: |
|
1749 |
compress byte-array into a bitArray, translating everything below 128 to 0-bits, |
|
1750 |
and 128 to 255 to 1-bits.99 to 0-bits (another stupid example) |
|
1751 |
" |
|
1752 |
" |
|
1753 |
|inBits outBits map| |
|
1754 |
||
1755 |
inBits := #[176 176 176 176 99 99 99 99 176 176 99 99 176 99 176 99]. |
|
1756 |
map := ByteArray new:256. |
|
1757 |
map atAll:(128+1 to:255+1) put:1. |
|
1758 |
||
1759 |
outBits := ByteArray new:2. |
|
1760 |
inBits compressPixels:1 width:16 height:1 |
|
2915 | 1761 |
into:outBits mapping:map. |
129 | 1762 |
outBits inspect |
1763 |
" |
|
563 | 1764 |
! |
1765 |
||
2866 | 1766 |
expandPixels:nBitsPerPixel width:width height:height into:aByteArray mapping:aMapByteArray |
563 | 1767 |
"given the receiver with nBitsPerPixel-depth pixels, expand them into |
1768 |
aByteArray with 8-bit pixels. The width/height-arguments are needed |
|
1769 |
to skip any padded src-bits. On the fly, the destination pixels |
|
1770 |
are translated using aMapByteArray (if non-nil). |
|
1771 |
Input bits are read left-to right, i.e. the first byte in the output |
|
1772 |
corresponds to the high bit(s) in the inputs first byte. |
|
1773 |
This is used to display mono, 2-bit and 4-bit bitmaps on grey-scale/color |
|
1774 |
machines. With nBitsPerPixel==8, this is a translate operation. |
|
1775 |
Notice that smalltalk indexing begins at 1; thus the map-index for a byte |
|
1776 |
value of n is found in map at:(n + 1). |
|
1777 |
It can also be used to expand bit-arrays into byteArrays. |
|
1778 |
This method is specialized for ByteArray arguments - it will not handle |
|
1779 |
anything else." |
|
1780 |
||
1781 |
%{ /* NOCONTEXT */ |
|
1782 |
||
1783 |
REGISTER unsigned char *src, *dst; |
|
1784 |
REGISTER int wrun; |
|
1785 |
unsigned char *srcNext; |
|
1786 |
int bytesPerRow, mask, shift0, shift; |
|
1787 |
int w, h, hrun; |
|
1788 |
int srcBytes, dstBytes; |
|
1789 |
int bitsPerPixel; |
|
1790 |
int bits; |
|
1791 |
int ncells; |
|
1792 |
unsigned char *map; |
|
1793 |
||
1133 | 1794 |
if ((__qClass(self) == @global(ByteArray)) |
1795 |
&& (__qClass(aByteArray) == @global(ByteArray)) |
|
563 | 1796 |
&& __isSmallInteger(nBitsPerPixel) |
1797 |
&& __bothSmallInteger(height, width)) { |
|
2915 | 1798 |
if ((aMapByteArray != nil) |
1799 |
&& (__Class(aMapByteArray) == @global(ByteArray))) { |
|
1800 |
map = __ByteArrayInstPtr(aMapByteArray)->ba_element; |
|
1801 |
} else { |
|
1802 |
map = (unsigned char *)0; |
|
1803 |
} |
|
563 | 1804 |
|
2915 | 1805 |
bitsPerPixel = __intVal(nBitsPerPixel); |
1806 |
w = __intVal(width); |
|
1807 |
h = __intVal(height); |
|
1808 |
src = __ByteArrayInstPtr(self)->ba_element; |
|
1809 |
dst = __ByteArrayInstPtr(aByteArray)->ba_element; |
|
1810 |
switch (bitsPerPixel) { |
|
1811 |
case 1: |
|
1812 |
mask = 0x01; |
|
1813 |
break; |
|
1814 |
case 2: |
|
1815 |
mask = 0x03; |
|
1816 |
break; |
|
1817 |
case 4: |
|
1818 |
mask = 0x0F; |
|
1819 |
break; |
|
1820 |
case 8: |
|
1821 |
mask = 0xFF; |
|
1822 |
break; |
|
1823 |
default: |
|
1824 |
printf("invalid depth in expandPixels\n"); |
|
1825 |
goto fail; |
|
1826 |
} |
|
1827 |
ncells = mask + 1; |
|
1828 |
if (map) { |
|
1829 |
/* |
|
1830 |
* if a map is present, it must have the correct size |
|
1831 |
* (i.e. 2 raisedTo:nBitsPerPixel) |
|
1832 |
*/ |
|
1833 |
if ((__qSize(aMapByteArray) - OHDR_SIZE) < ncells) { |
|
1834 |
printf("invalid map in expandPixels\n"); |
|
1835 |
goto fail; |
|
1836 |
} |
|
1837 |
} |
|
563 | 1838 |
|
2915 | 1839 |
bytesPerRow = (w * bitsPerPixel + 7) / 8; |
1840 |
shift0 = 8 - bitsPerPixel; |
|
1841 |
srcBytes = bytesPerRow * h; |
|
1842 |
dstBytes = w * h; |
|
1456 | 1843 |
|
2915 | 1844 |
if ((__byteArraySize(self) >= srcBytes) |
1845 |
&& (__byteArraySize(aByteArray) >= dstBytes)) { |
|
1846 |
for (hrun=h; hrun; hrun--) { |
|
1847 |
srcNext = src + bytesPerRow; |
|
1848 |
shift = shift0; |
|
1849 |
if (map) { |
|
1850 |
if (shift0 == 0) { |
|
1851 |
/* translate only */ |
|
1852 |
for (wrun=w; wrun; wrun--) { |
|
1853 |
bits = *src++; |
|
1854 |
*dst++ = map[bits]; |
|
1855 |
} |
|
1856 |
} else { |
|
1857 |
for (wrun=w; wrun; wrun--) { |
|
1858 |
if (shift == shift0) { |
|
1859 |
bits = *src++; |
|
1860 |
} |
|
1861 |
*dst++ = map[(bits >> shift) & mask]; |
|
1862 |
shift -= bitsPerPixel; |
|
1863 |
if (shift < 0) { |
|
1864 |
shift = shift0; |
|
1865 |
} |
|
1866 |
} |
|
1867 |
} |
|
1868 |
} else { |
|
1869 |
for (wrun=w; wrun; wrun--) { |
|
1870 |
if (shift == shift0) { |
|
1871 |
bits = *src++; |
|
1872 |
} |
|
1873 |
*dst++ = (bits >> shift) & mask; |
|
1874 |
shift -= bitsPerPixel; |
|
1875 |
if (shift < 0) { |
|
1876 |
shift = shift0; |
|
1877 |
} |
|
1878 |
} |
|
1879 |
} |
|
1880 |
src = srcNext; |
|
1881 |
} |
|
1882 |
RETURN ( self ); |
|
1883 |
} |
|
563 | 1884 |
} |
2866 | 1885 |
printf("invalid args\n"); |
1527
af119b16aa16
better handle the translate-only case in #expandPixels:
ca
parents:
1505
diff
changeset
|
1886 |
|
563 | 1887 |
fail: ; |
2866 | 1888 |
%}. |
563 | 1889 |
self primitiveFailed |
1890 |
||
1891 |
"Example1: |
|
1892 |
expand 1-bit-per-pixel bitmap into a 1byte-per-pixel byteArray |
|
1893 |
" |
|
1894 |
" |
|
1895 |
|inBits outBits| |
|
1896 |
||
1897 |
inBits := #[2r11110000 |
|
2915 | 1898 |
2r11001100 |
1899 |
2r01010101 |
|
1900 |
2r00001111]. |
|
563 | 1901 |
outBits := ByteArray new:(8*4). |
1902 |
inBits expandPixels:1 width:8 height:4 |
|
2915 | 1903 |
into:outBits mapping:nil. |
563 | 1904 |
outBits inspect |
1905 |
" |
|
1906 |
||
1907 |
"Example2: |
|
1908 |
expand bit-array into a byteArray, translating 0-bits to 99, |
|
1909 |
1-bits to 176. (just a stupid example) |
|
1910 |
" |
|
1911 |
" |
|
1912 |
|inBits outBits| |
|
1913 |
||
1914 |
inBits := #[2r11110000 2r11001100]. |
|
1915 |
outBits := ByteArray new:16. |
|
1916 |
inBits expandPixels:1 width:16 height:1 |
|
2915 | 1917 |
into:outBits mapping:#[99 176]. |
563 | 1918 |
outBits inspect |
1919 |
" |
|
1920 |
! |
|
1921 |
||
1922 |
invert |
|
1923 |
"invert all bytes - used with image manipulations |
|
1924 |
written as a primitive for speed. |
|
1925 |
Q: is this really needed ?" |
|
1926 |
||
1927 |
%{ /* NOCONTEXT */ |
|
1928 |
||
1929 |
REGISTER unsigned char *dst; |
|
1930 |
REGISTER unsigned long *ldst; |
|
1931 |
REGISTER int cnt; |
|
1932 |
||
1133 | 1933 |
if (__qClass(self) == @global(ByteArray)) { |
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1934 |
cnt = __byteArraySize(self); |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1935 |
dst = __ByteArrayInstPtr(self)->ba_element; |
2768 | 1936 |
if (! ((INT)dst & (sizeof(long)-1))) { |
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1937 |
ldst = (unsigned long *)dst; |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1938 |
while (cnt > 16) { |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1939 |
ldst[0] = ~(ldst[0]); |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1940 |
ldst[1] = ~(ldst[1]); |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1941 |
ldst[2] = ~(ldst[2]); |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1942 |
ldst[3] = ~(ldst[3]); |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1943 |
ldst += 4; |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1944 |
cnt -= 16; |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1945 |
} |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1946 |
while (cnt >= sizeof(long)) { |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1947 |
*ldst = ~(*ldst); |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1948 |
ldst++; |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1949 |
cnt -= sizeof(long); |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1950 |
} |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1951 |
dst = (unsigned char *)ldst; |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1952 |
} |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1953 |
while (cnt--) { |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1954 |
*dst = ~(*dst); |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1955 |
dst++; |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1956 |
} |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1957 |
RETURN ( self ); |
563 | 1958 |
} |
1959 |
%} |
|
1960 |
. |
|
1961 |
self primitiveFailed |
|
1962 |
! |
|
1963 |
||
1964 |
reverse |
|
2585 | 1965 |
"reverse the order of my elements inplace - |
563 | 1966 |
written as a primitive for speed on image manipulations (mirror)" |
1967 |
||
1968 |
%{ /* NOCONTEXT */ |
|
1969 |
||
1970 |
REGISTER unsigned char *p1, *p2; |
|
1971 |
REGISTER int cnt; |
|
1972 |
REGISTER unsigned t; |
|
1973 |
OBJ cls; |
|
1974 |
||
1133 | 1975 |
if (__qClass(self) == @global(ByteArray)) { |
2915 | 1976 |
cnt = __byteArraySize(self); |
1977 |
p1 = __ByteArrayInstPtr(self)->ba_element; |
|
1978 |
p2 = p1 + cnt - 1; |
|
1979 |
while (p1 < p2) { |
|
1980 |
t = *p1; |
|
1981 |
*p1++ = *p2; |
|
1982 |
*p2-- = t; |
|
1983 |
} |
|
1984 |
RETURN ( self ); |
|
563 | 1985 |
} |
1986 |
%}. |
|
1987 |
^ super reverse |
|
2585 | 1988 |
|
1989 |
" |
|
1990 |
#[1 2 3 4 5] reverse |
|
1991 |
#[1 2 3 4] reverse |
|
1992 |
" |
|
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1993 |
! |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1994 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1995 |
swapBytes |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1996 |
"swap bytes inplace - |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1997 |
written as a primitive for speed on image grabbing (if display order is different)" |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1998 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
1999 |
%{ /* NOCONTEXT */ |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2000 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2001 |
REGISTER unsigned char *p; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2002 |
REGISTER int cnt; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2003 |
REGISTER unsigned t; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2004 |
|
1133 | 2005 |
if (__qClass(self) == @global(ByteArray)) { |
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2006 |
cnt = __byteArraySize(self); |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2007 |
cnt = cnt & ~1; /* make it even */ |
1133 | 2008 |
p = __ByteArrayInstPtr(self)->ba_element; |
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2009 |
while (cnt > 0) { |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2010 |
t = p[0]; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2011 |
p[0] = p[1]; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2012 |
p[1] = t; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2013 |
p += 2; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2014 |
cnt -= 2; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2015 |
} |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2016 |
RETURN ( self ); |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2017 |
} |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2018 |
%}. |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2019 |
^ super swapBytes "/ rubbish - there is no one currenly |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2020 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2021 |
" |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2022 |
#[1 2 3 4 5 6 7 8 9 10] copy swapBytes |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2023 |
#[1 2 3 4 5 6 7 8 9 10 11] copy swapBytes |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2024 |
" |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2025 |
! |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2026 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2027 |
swapLongs |
2866 | 2028 |
"swap long bytes inplace - any partial longs at the end |
2029 |
are not swapped. |
|
2030 |
written as a primitive for speed on image grabbing |
|
2031 |
(if display order is different)" |
|
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2032 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2033 |
%{ /* NOCONTEXT */ |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2034 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2035 |
REGISTER unsigned char *p; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2036 |
REGISTER int cnt; |
931 | 2037 |
REGISTER unsigned t; |
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2038 |
|
1133 | 2039 |
if (__qClass(self) == @global(ByteArray)) { |
2915 | 2040 |
cnt = __byteArraySize(self); |
2041 |
cnt = cnt & ~3; /* make it even */ |
|
2042 |
p = __ByteArrayInstPtr(self)->ba_element; |
|
2043 |
while (cnt > 0) { |
|
2044 |
t = p[0]; |
|
2045 |
p[0] = p[3]; |
|
2046 |
p[3] = t; |
|
2047 |
t = p[1]; |
|
2048 |
p[1] = p[2]; |
|
2049 |
p[2] = t; |
|
2050 |
p += 4; |
|
2051 |
cnt -= 4; |
|
2052 |
} |
|
2053 |
RETURN ( self ); |
|
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2054 |
} |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2055 |
%}. |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2056 |
^ super swapLongs "/ rubbish - there is no one currenly |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2057 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2058 |
" |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2059 |
#[1 2 3 4 5 6 7 8 9] copy swapLongs |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2060 |
#[1 2 3 4 5 6 7 8 9 10] copy swapLongs |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2061 |
#[1 2 3 4 5 6 7 8 9 10 11] copy swapLongs |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2062 |
#[1 2 3 4 5 6 7 8 9 10 11 12] copy swapLongs |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2063 |
" |
1 | 2064 |
! ! |
550 | 2065 |
|
2066 |
!ByteArray methodsFor:'printing & storing'! |
|
2067 |
||
2068 |
asPackedString |
|
2069 |
"ST-80 compatibility: encode the receiver into an ascii String |
|
2070 |
with 6bits encoded per character. Each group of 6 bits is encoded |
|
2071 |
as a corresponding character (32+value) and the resulting string |
|
2072 |
is returned. The resulting string is always a multiple of 4 (since |
|
2073 |
24 is the lcm of 6 and 8) and the number of remaining characters is |
|
2074 |
encoded in the last character. |
|
2075 |
ST-80 uses this encoding for Images ... |
|
2076 |
PS: I dont like it ;-)" |
|
2077 |
||
2078 |
|outStream |
|
1974 | 2079 |
index "{ Class:SmallInteger}" |
2080 |
nextIndex "{ Class:SmallInteger}" |
|
2081 |
stop "{ Class:SmallInteger}" |
|
2082 |
n "{ Class:SmallInteger}" |
|
2083 |
mod "{ Class:SmallInteger}"| |
|
550 | 2084 |
|
2085 |
outStream := WriteStream on:String new. |
|
2086 |
index := 1. |
|
2087 |
stop := self size. |
|
2088 |
[index <= stop] whileTrue:[ |
|
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2089 |
"take 3 source bytes" |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2090 |
n := (self at:index) bitShift:16. |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2091 |
(index < stop) ifTrue:[ |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2092 |
nextIndex := index + 1. |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2093 |
n := n bitOr:((self at:nextIndex) bitShift:8). |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2094 |
(nextIndex < stop) ifTrue:[ |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2095 |
n := n bitOr:(self at:(index + 2)). |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2096 |
]. |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2097 |
]. |
550 | 2098 |
|
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2099 |
"took me a while to find that one out ..." |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2100 |
n := n bitXor:16r820820. |
550 | 2101 |
|
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2102 |
outStream nextPut:(Character value:(n bitShift:-18) + 32). |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2103 |
outStream nextPut:(Character value:((n bitShift:-12) bitAnd:16r3F) + 32). |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2104 |
outStream nextPut:(Character value:((n bitShift:-6) bitAnd:16r3F) + 32). |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2105 |
outStream nextPut:(Character value:(n bitAnd:16r3F) + 32). |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2106 |
index := index + 3. |
550 | 2107 |
]. |
1973 | 2108 |
(mod := stop \\ 3) ~~ 0 ifTrue:[ |
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2109 |
outStream position:(outStream position - 1). |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2110 |
outStream nextPut:(Character value:(mod + 96)). |
550 | 2111 |
]. |
2112 |
^ outStream contents |
|
1973 | 2113 |
|
1974 | 2114 |
"Modified: 12.11.1996 / 15:45:02 / cg" |
563 | 2115 |
! |
2116 |
||
2117 |
displayString |
|
1244 | 2118 |
"return a printed representation of the receiver for displaying" |
2119 |
||
563 | 2120 |
^ self storeString |
2121 |
||
2122 |
"Created: 25.10.1995 / 13:33:26 / cg" |
|
1244 | 2123 |
"Modified: 22.4.1996 / 12:54:06 / cg" |
563 | 2124 |
! |
2125 |
||
2126 |
storeOn:aStream |
|
2127 |
"append a printed representation from which the receiver can be |
|
2128 |
reconstructed to aStream. (reimplemented to make it look better)" |
|
2129 |
||
1082 | 2130 |
self class == ByteArray ifTrue:[ "/ care for subclasses |
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2131 |
aStream nextPutAll:'#['. |
2915 | 2132 |
self |
2133 |
do:[:byte | byte storeOn:aStream] |
|
2134 |
separatedBy:[aStream space]. |
|
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2135 |
aStream nextPutAll:']'. |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2136 |
^ self |
563 | 2137 |
]. |
2138 |
^ super storeOn:aStream |
|
2139 |
||
2140 |
" |
|
1082 | 2141 |
#[1 2 3 4 5] storeOn:Transcript |
2142 |
||
2143 |
#[1 2 3 4 5] storeString |
|
2915 | 2144 |
#[1 2 3 4 5] displayString |
1082 | 2145 |
#[1 2 3 4 5] printString |
563 | 2146 |
" |
1082 | 2147 |
|
2915 | 2148 |
"Modified: 12.9.1997 / 22:11:33 / cg" |
550 | 2149 |
! ! |
2150 |
||
2151 |
!ByteArray methodsFor:'queries'! |
|
2152 |
||
2153 |
indexOf:aByte startingAt:start |
|
2154 |
"return the index of the first occurrence of the argument, aByte |
|
2155 |
in the receiver starting at start, anInteger; return 0 if not found. |
|
2156 |
- reimplemented here for speed" |
|
2157 |
||
2158 |
%{ /* NOCONTEXT */ |
|
2159 |
||
2160 |
REGISTER unsigned char *cp; |
|
2161 |
REGISTER int index, byteValue; |
|
2162 |
REGISTER int len; |
|
2163 |
OBJ cls; |
|
2164 |
||
1002
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
2165 |
if (__isSmallInteger(aByte) &&__isBytes(self)) { |
1133 | 2166 |
byteValue = __intVal(aByte); |
550 | 2167 |
|
1019 | 2168 |
if (byteValue & ~0xFF /* i.e. (byteValue < 0) || (byteValue > 255) */) { |
2169 |
/* |
|
2170 |
* searching for something which cannot be found |
|
2171 |
*/ |
|
1133 | 2172 |
RETURN ( __MKSMALLINT(0) ); |
1019 | 2173 |
} |
550 | 2174 |
|
1019 | 2175 |
if (__isSmallInteger(start)) { |
1133 | 2176 |
index = __intVal(start); |
1019 | 2177 |
len = __byteArraySize(self); |
1133 | 2178 |
cp = __ByteArrayInstPtr(self)->ba_element; |
2179 |
if ((cls = __qClass(self)) != @global(ByteArray)) { |
|
1019 | 2180 |
int nInst; |
550 | 2181 |
|
1133 | 2182 |
nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
1019 | 2183 |
cp += nInst; |
2184 |
len -= nInst; |
|
2185 |
} |
|
2186 |
cp += index - 1; |
|
2187 |
while (index <= len) { |
|
2188 |
if (*cp == byteValue) { |
|
1133 | 2189 |
RETURN ( __MKSMALLINT(index) ); |
1019 | 2190 |
} |
2191 |
index++; |
|
2192 |
cp++; |
|
2193 |
} |
|
1133 | 2194 |
RETURN ( __MKSMALLINT(0) ); |
1019 | 2195 |
} |
550 | 2196 |
} |
2197 |
%}. |
|
2198 |
^ super indexOf:aByte startingAt:start |
|
2199 |
||
2200 |
" |
|
2201 |
#[1 2 3 4 5 6 7 8 9 0 1 2 3 4 5] indexOf:0 |
|
2202 |
" |
|
2203 |
! |
|
2204 |
||
1082 | 2205 |
isLiteral |
2206 |
"return true, if the receiver can be used as a literal |
|
2207 |
(i.e. can be used in constant arrays)" |
|
2208 |
||
1244 | 2209 |
"/ no, simply returning true here is a mistake: |
2210 |
"/ it could be a subclass of ByteArray |
|
2211 |
"/ (of which the compiler does not know at all ...) |
|
1082 | 2212 |
|
2213 |
^ self class == ByteArray |
|
1244 | 2214 |
|
2215 |
"Modified: 22.4.1996 / 12:55:30 / cg" |
|
1082 | 2216 |
! |
2217 |
||
563 | 2218 |
max |
2219 |
"return the maximum value in the receiver - |
|
2220 |
redefined to speedup image processing and sound-player |
|
2221 |
(which need a fast method for this on byteArrays)" |
|
550 | 2222 |
|
563 | 2223 |
%{ /* NOCONTEXT */ |
550 | 2224 |
|
2225 |
REGISTER unsigned char *cp; |
|
563 | 2226 |
REGISTER int index, max; |
2227 |
int len; |
|
550 | 2228 |
|
1133 | 2229 |
if (__qClass(self) == @global(ByteArray)) { |
563 | 2230 |
max = 0; |
2231 |
index = 0; |
|
550 | 2232 |
len = __qSize(self) - OHDR_SIZE; |
1133 | 2233 |
cp = &(__ByteArrayInstPtr(self)->ba_element[0]); |
563 | 2234 |
while (++index <= len) { |
2235 |
if (*cp > max) max = *cp; |
|
550 | 2236 |
cp++; |
2237 |
} |
|
1133 | 2238 |
RETURN ( __MKSMALLINT(max) ); |
550 | 2239 |
} |
563 | 2240 |
%}. |
2241 |
^ super max |
|
550 | 2242 |
|
2243 |
" |
|
563 | 2244 |
#[1 2 3 1 2 3 1 2 19] max |
550 | 2245 |
" |
1082 | 2246 |
! ! |
2247 |
||
2248 |
!ByteArray methodsFor:'special queries'! |
|
550 | 2249 |
|
2250 |
usageCounts |
|
2251 |
"return an array filled with value-counts - |
|
2252 |
This is needed in the bitmap/image classes to get info on color usage. |
|
2253 |
(i.e. to build up a histogram of color usage within an image)." |
|
2254 |
||
2255 |
|counts| |
|
2256 |
||
2257 |
counts := Array basicNew:256. |
|
2258 |
||
2259 |
%{ /* STACK: 2000 */ |
|
2260 |
||
2261 |
REGISTER unsigned char *cp; |
|
2262 |
REGISTER int nByte; |
|
2263 |
REGISTER int index; |
|
2264 |
int icounts[256]; |
|
2265 |
||
1133 | 2266 |
if ((__qClass(self) == @global(ByteArray)) && __isArray(counts)) { |
550 | 2267 |
/* |
2268 |
* zero counts |
|
2269 |
*/ |
|
2270 |
for (index=0; index<256; index++) { |
|
2271 |
icounts[index] = 0; |
|
2272 |
} |
|
2273 |
||
2274 |
/* |
|
2275 |
* count |
|
2276 |
*/ |
|
2277 |
nByte = __qSize(self) - OHDR_SIZE; |
|
1133 | 2278 |
cp = &(__ByteArrayInstPtr(self)->ba_element[0]); |
550 | 2279 |
while (nByte--) { |
2280 |
icounts[*cp++]++; |
|
2281 |
} |
|
2282 |
||
2283 |
/* |
|
2284 |
* make it real counts |
|
2285 |
*/ |
|
2286 |
for (index=0; index<256; index++) { |
|
1133 | 2287 |
__ArrayInstPtr(counts)->a_element[index] = __MKSMALLINT(icounts[index]); |
550 | 2288 |
} |
2289 |
RETURN ( counts ); |
|
2290 |
} |
|
2291 |
%} |
|
2292 |
. |
|
2293 |
self primitiveFailed |
|
2294 |
||
2295 |
" |
|
2296 |
#[1 2 3 1 2 3 1 2 250 251 250 251 255] usageCounts |
|
2297 |
" |
|
2298 |
! |
|
2299 |
||
563 | 2300 |
usedValues |
2301 |
"return a new ByteArray with all used values (actually a kind of Set); |
|
2302 |
This is needed specially in the bitmap/Imageclasses to find used colors |
|
2866 | 2303 |
of an image." |
550 | 2304 |
|
563 | 2305 |
%{ /* STACK: 400 */ |
550 | 2306 |
|
2307 |
REGISTER unsigned char *cp; |
|
2866 | 2308 |
unsigned char *endp; |
563 | 2309 |
REGISTER int len; |
2890 | 2310 |
OBJ result; |
2866 | 2311 |
union { |
2915 | 2312 |
unsigned char flags[256]; |
2313 |
int toForceAlignmentOfFlags; |
|
2866 | 2314 |
} f; |
2315 |
||
2316 |
#ifdef TO_BE_MEASURED |
|
2317 |
int coverage = 0; |
|
2318 |
#endif |
|
550 | 2319 |
|
1133 | 2320 |
if (__qClass(self) == @global(ByteArray)) { |
2915 | 2321 |
memset(f.flags, 0, sizeof(f.flags)); |
2322 |
len = __qSize(self) - OHDR_SIZE; |
|
2323 |
cp = &(__ByteArrayInstPtr(self)->ba_element[0]); |
|
2866 | 2324 |
|
2915 | 2325 |
/* for each used byte, set flag */ |
2326 |
while (len > 0) { |
|
2866 | 2327 |
#ifdef TO_BE_MEASURED |
2915 | 2328 |
unsigned byte; |
563 | 2329 |
|
2915 | 2330 |
byte = *cp; |
2331 |
if (! f.flags[byte]) { |
|
2332 |
f.flags[byte] = 1; |
|
2333 |
coverage++; |
|
2334 |
if (coverage == 256) { |
|
2335 |
/* no need to scan rest */ |
|
2336 |
break; |
|
2337 |
} |
|
2338 |
} |
|
2866 | 2339 |
#else |
2915 | 2340 |
f.flags[*cp] = 1; |
2866 | 2341 |
#endif |
2915 | 2342 |
cp++; |
2343 |
len--; |
|
2344 |
} |
|
563 | 2345 |
|
2915 | 2346 |
/* count 1's */ |
2347 |
len = 0; |
|
2348 |
for (cp=f.flags, endp=f.flags+256; cp < endp;) { |
|
2349 |
if ( *((unsigned int *)cp)) { |
|
2350 |
if (cp[0]) len++; |
|
2351 |
if (cp[1]) len++; |
|
2352 |
if (cp[2]) len++; |
|
2353 |
if (cp[3]) len++; |
|
2354 |
} |
|
2355 |
cp += 4; |
|
2356 |
} |
|
1133 | 2357 |
|
2915 | 2358 |
/* create ByteArray of used values */ |
2359 |
result = __BYTEARRAY_UNINITIALIZED_NEW_INT(len); |
|
2360 |
if (result) { |
|
2361 |
cp = __ByteArrayInstPtr(result)->ba_element; |
|
2362 |
for (len=0; len < 256; len++) { |
|
2363 |
if (f.flags[len]) |
|
2364 |
*cp++ = len; |
|
2365 |
} |
|
2366 |
RETURN ( result ); |
|
2367 |
} |
|
550 | 2368 |
} |
2866 | 2369 |
%}. |
2370 |
^ self asIdentitySet asByteArray |
|
550 | 2371 |
|
2372 |
" |
|
563 | 2373 |
#[1 2 3 1 2 3 1 2 3 1 2 3 4 5 6 4 5 6] usedValues |
550 | 2374 |
" |
2375 |
! ! |
|
2376 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
2377 |
!ByteArray class methodsFor:'documentation'! |
628 | 2378 |
|
2379 |
version |
|
3208
2d71538b9fd5
now subclass of UIBytes - which contains common protocol
Claus Gittinger <cg@exept.de>
parents:
3205
diff
changeset
|
2380 |
^ '$Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.93 1998-01-21 16:08:55 cg Exp $' |
628 | 2381 |
! ! |