author | Claus Gittinger <cg@exept.de> |
Thu, 10 Jul 2014 14:23:23 +0200 | |
changeset 16741 | 81ada4378090 |
parent 16621 | ac0885c03e15 |
child 16795 | 6ceacf5daa62 |
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 |
" |
|
5425 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
3208
2d71538b9fd5
now subclass of UIBytes - which contains common protocol
Claus Gittinger <cg@exept.de>
parents:
3205
diff
changeset
|
14 |
UninterpretedBytes variableByteSubclass:#ByteArray |
1002
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
15 |
instanceVariableNames:'' |
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
16 |
classVariableNames:'' |
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
17 |
poolDictionaries:'' |
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
18 |
category:'Collections-Arrayed' |
1 | 19 |
! |
20 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
21 |
!ByteArray class methodsFor:'documentation'! |
88 | 22 |
|
563 | 23 |
copyright |
24 |
" |
|
25 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
26 |
All Rights Reserved |
|
27 |
||
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
36 |
||
88 | 37 |
documentation |
38 |
" |
|
39 |
ByteArrays store integers in the range 0..255. |
|
129 | 40 |
In contrast to normal arrays (which store pointers to their elements), |
362 | 41 |
byteArrays store the values in a dense & compact way. ByteArrays can |
129 | 42 |
be used to hold the data for bitmaps, images and other bulk data. |
43 |
ByteArrays are also used to store the bytecode-instructions of an |
|
44 |
interpreted method and are used as superclass for Strings. |
|
45 |
||
46 |
ByteArrays can be used as literals i.e. you can enter ByteArray-constants |
|
47 |
as #[ element1 element2 .... elementN] and also use byteArray constants |
|
48 |
as elements in a constant array. |
|
49 |
As in: #( #[1 1 1] #[2 2 2] #[3 3 3]) |
|
50 |
||
51 |
If you have to communicate structure-data (in the C-sense) with external |
|
52 |
programs/data-bases, see a companion class (Structure) in the goodies directory. |
|
53 |
It allows the definition of subclasses of ByteArray, which transparently fetch |
|
54 |
and store C-structure fields. |
|
1266 | 55 |
|
4062 | 56 |
[memory requirements:] |
4661 | 57 |
OBJ-HEADER + size |
4062 | 58 |
|
2145 | 59 |
[warning:] |
4661 | 60 |
read the warning about 'growing fixed size collection' |
61 |
in ArrayedCollection's documentation |
|
2145 | 62 |
|
1290 | 63 |
[author:] |
4661 | 64 |
Claus Gittinger |
1290 | 65 |
|
1266 | 66 |
[See also:] |
4661 | 67 |
Array CharacterArray String |
88 | 68 |
" |
2 | 69 |
! ! |
70 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
71 |
!ByteArray class methodsFor:'instance creation'! |
1 | 72 |
|
73 |
uninitializedNew:anInteger |
|
74 |
"return a new instance of the receiver with uninitialized |
|
75 |
(i.e. undefined) contents. The indexed elements have any random |
|
8908 | 76 |
value. However, any named instance variables are still nilled. |
75 | 77 |
For use, when contents will be set anyway shortly after - this |
78 |
is a bit faster than the regular basicNew:, which clears the bytes. |
|
79 |
Of course, it only makes a difference for very big ByteArrays, such |
|
80 |
as used for images/bitmaps. |
|
81 |
||
82 |
Notice: if you want to port code using uninitializedNew: to another |
|
8908 | 83 |
smalltalk, you have to add an 'uninitializedNew: -> basicNew:'-calling |
75 | 84 |
method to the ByteArray class of the other smalltalk." |
1 | 85 |
|
86 |
%{ /* NOCONTEXT */ |
|
87 |
OBJ newobj; |
|
88 |
INT instsize, nInstVars, nindexedinstvars; |
|
89 |
REGISTER OBJ *op; |
|
90 |
||
249 | 91 |
if (__isSmallInteger(anInteger)) { |
8908 | 92 |
nindexedinstvars = __intVal(anInteger); |
93 |
if (nindexedinstvars >= 0) { |
|
94 |
if (self == ByteArray) { |
|
95 |
/* |
|
96 |
* the most common case |
|
97 |
*/ |
|
98 |
instsize = OHDR_SIZE + nindexedinstvars; |
|
99 |
if (__CanDoQuickNew(instsize)) { /* OBJECT ALLOCATION */ |
|
100 |
__qCheckedNew(newobj, instsize); |
|
101 |
__InstPtr(newobj)->o_class = self; |
|
102 |
__qSTORE(newobj, self); |
|
103 |
RETURN (newobj ); |
|
104 |
} |
|
105 |
} else { |
|
106 |
/* |
|
107 |
* Take care for subclasses like TwoByteString |
|
108 |
*/ |
|
109 |
switch (__smallIntegerVal(__ClassInstPtr(self)->c_flags) & ARRAYMASK) { |
|
110 |
case BYTEARRAY: |
|
111 |
break; |
|
112 |
||
113 |
case WORDARRAY: |
|
114 |
case SWORDARRAY: |
|
115 |
nindexedinstvars *= 2; |
|
116 |
break; |
|
117 |
||
118 |
case LONGARRAY: |
|
119 |
case SLONGARRAY: |
|
120 |
nindexedinstvars *= 4; |
|
121 |
break; |
|
122 |
||
123 |
default: |
|
124 |
/* don't know about this array type, delegate to super */ |
|
125 |
goto out; |
|
126 |
} |
|
127 |
} |
|
128 |
nInstVars = __intVal(__ClassInstPtr(self)->c_ninstvars); |
|
129 |
instsize = OHDR_SIZE + __OBJS2BYTES__(nInstVars) + nindexedinstvars; |
|
130 |
__PROTECT_CONTEXT__ |
|
131 |
__qNew(newobj, instsize); /* OBJECT ALLOCATION */ |
|
132 |
__UNPROTECT_CONTEXT__ |
|
133 |
if (newobj != nil) { |
|
134 |
__InstPtr(newobj)->o_class = self; |
|
135 |
__qSTORE(newobj, self); |
|
136 |
if (nInstVars) { |
|
137 |
/* |
|
138 |
* still have to nil out named instvars ... |
|
139 |
*/ |
|
788
e80f1c42b87b
dont use memset4 if its undefined
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
140 |
#if defined(memset4) && defined(FAST_OBJECT_MEMSET4) |
8908 | 141 |
memset4(__InstPtr(newobj)->i_instvars, nil, nInstVars); |
1 | 142 |
#else |
249 | 143 |
# if defined(FAST_MEMSET) && !defined(NEGATIVE_ADDRESSES) |
8908 | 144 |
/* |
145 |
* knowing that nil is 0 |
|
146 |
*/ |
|
147 |
memset(__InstPtr(newobj)->i_instvars, 0, instsize - OHDR_SIZE); |
|
249 | 148 |
# else |
8908 | 149 |
op = __InstPtr(newobj)->i_instvars; |
150 |
while (nInstVars--) |
|
151 |
*op++ = nil; |
|
249 | 152 |
# endif |
1 | 153 |
#endif |
8908 | 154 |
} |
155 |
RETURN ( newobj ); |
|
156 |
} |
|
157 |
} |
|
1 | 158 |
} |
8159
ac6725a62e17
#uninitializedNew: - take care of WORDARRAYS etc.
Stefan Vogel <sv@exept.de>
parents:
8096
diff
changeset
|
159 |
out:; |
5425 | 160 |
%}. |
1 | 161 |
^ self basicNew:anInteger |
162 |
! ! |
|
163 |
||
11502 | 164 |
|
16741 | 165 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
166 |
!ByteArray class methodsFor:'queries'! |
550 | 167 |
|
13706
62f6489aa2c6
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
13703
diff
changeset
|
168 |
elementByteSize |
62f6489aa2c6
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
13703
diff
changeset
|
169 |
^ 1 |
62f6489aa2c6
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
13703
diff
changeset
|
170 |
|
62f6489aa2c6
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
13703
diff
changeset
|
171 |
"Created: / 15-09-2011 / 14:12:53 / cg" |
62f6489aa2c6
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
13703
diff
changeset
|
172 |
! |
62f6489aa2c6
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
13703
diff
changeset
|
173 |
|
550 | 174 |
isBuiltInClass |
1266 | 175 |
"return true if this class is known by the run-time-system. |
176 |
Here, true is returned for myself, false for subclasses." |
|
550 | 177 |
|
178 |
^ self == ByteArray |
|
1266 | 179 |
|
180 |
"Modified: 23.4.1996 / 15:56:25 / cg" |
|
550 | 181 |
! ! |
182 |
||
16741 | 183 |
|
10884 | 184 |
!ByteArray methodsFor:'Compatibility-Squeak'! |
185 |
||
12144 | 186 |
bitXor:aByteArray |
187 |
"return a new byteArray containing the bitWise-xor of the receiver's and the |
|
188 |
argument's bytes" |
|
189 |
||
15625 | 190 |
|size size1| |
191 |
||
192 |
"size := self size min:aByteArray size" |
|
193 |
size := self size. |
|
194 |
size1 := aByteArray size. |
|
195 |
size1 < size ifTrue:[ |
|
196 |
size := size1. |
|
12144 | 197 |
]. |
15625 | 198 |
|
199 |
^ self copy |
|
200 |
bitXorBytesFrom:1 to:size with:aByteArray startingAt:1; |
|
201 |
yourself. |
|
202 |
||
203 |
" |
|
204 |
#[0 1 2 3 4] bitXor:#[0 1 2 3 4] |
|
205 |
#[0 1 2 3 4] bitXor:#[0 1 2 3] |
|
206 |
" |
|
10884 | 207 |
! ! |
208 |
||
12091 | 209 |
!ByteArray methodsFor:'Compatibility-VW'! |
210 |
||
211 |
asByteString |
|
212 |
^ self asString |
|
213 |
! ! |
|
214 |
||
550 | 215 |
!ByteArray methodsFor:'accessing'! |
1 | 216 |
|
563 | 217 |
basicAt:index |
218 |
"return the indexed instance variable with index, anInteger |
|
1019 | 219 |
- redefined here to be slighly faster than the default in Object. |
220 |
Q: is it worth the extra code ?" |
|
563 | 221 |
|
222 |
%{ /* NOCONTEXT */ |
|
223 |
||
224 |
REGISTER int indx; |
|
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
225 |
REGISTER OBJ slf; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
226 |
REGISTER OBJ cls; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
227 |
REGISTER int nIndex; |
563 | 228 |
|
229 |
if (__isSmallInteger(index)) { |
|
4661 | 230 |
indx = __intVal(index) - 1; |
231 |
slf = self; |
|
232 |
if ((cls = __qClass(slf)) != @global(ByteArray)) { |
|
233 |
if (((INT)__ClassInstPtr(cls)->c_flags & __MASKSMALLINT(ARRAYMASK)) |
|
234 |
!= __MASKSMALLINT(BYTEARRAY)) { |
|
235 |
goto fail; |
|
236 |
} |
|
237 |
if (indx < 0) goto fail; |
|
238 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
239 |
} |
|
240 |
nIndex = __byteArraySize(slf); |
|
241 |
if ((unsigned)indx < (unsigned)nIndex) { |
|
8913 | 242 |
RETURN ( __mkSmallInteger((__ByteArrayInstPtr(slf)->ba_element[indx])) ); |
4661 | 243 |
} |
563 | 244 |
} |
1019 | 245 |
fail: ; |
246 |
%}. |
|
563 | 247 |
^ super basicAt:index |
248 |
! |
|
249 |
||
1 | 250 |
basicAt:index put:value |
1230 | 251 |
"set the indexed instance variable with index, anInteger to value. |
252 |
Returns value (sigh). |
|
1019 | 253 |
- redefined here to be slighly faster than the default in Object. |
254 |
Q: is it worth the extra code ?" |
|
1 | 255 |
|
256 |
%{ /* NOCONTEXT */ |
|
257 |
||
258 |
REGISTER int indx; |
|
259 |
int nIndex; |
|
260 |
int val; |
|
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
261 |
REGISTER OBJ slf; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
262 |
REGISTER OBJ cls; |
1 | 263 |
|
249 | 264 |
if (__bothSmallInteger(index, value)) { |
4661 | 265 |
val = __intVal(value); |
266 |
if ((unsigned)(val) <= 0xFF /* i.e. (val >= 0) && (val <= 255) */) { |
|
267 |
indx = __intVal(index) - 1; |
|
268 |
slf = self; |
|
269 |
if ((cls = __qClass(slf)) != @global(ByteArray)) { |
|
270 |
if (((INT)__ClassInstPtr(cls)->c_flags & __MASKSMALLINT(ARRAYMASK)) |
|
271 |
!= __MASKSMALLINT(BYTEARRAY)) { |
|
272 |
goto fail; |
|
273 |
} |
|
274 |
if (indx < 0) goto fail; |
|
275 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
276 |
} |
|
277 |
nIndex = __byteArraySize(slf); |
|
278 |
if ((unsigned)indx < (unsigned)nIndex) { |
|
279 |
__ByteArrayInstPtr(slf)->ba_element[indx] = val; |
|
280 |
RETURN ( value ); |
|
281 |
} |
|
282 |
} |
|
1 | 283 |
} |
1019 | 284 |
fail: ; |
285 |
%}. |
|
1 | 286 |
^ super basicAt:index put:value |
1230 | 287 |
|
288 |
"Modified: 19.4.1996 / 11:14:40 / cg" |
|
2 | 289 |
! |
290 |
||
4128 | 291 |
bitAt:index |
11502 | 292 |
"return the bit at index (1 based index) as 0 or 1" |
5439 | 293 |
|
294 |
|byteIndex bitIndex0 byte| |
|
4128 | 295 |
|
296 |
%{ /* NOCONTEXT */ |
|
297 |
||
298 |
REGISTER int indx; |
|
299 |
REGISTER int byte; |
|
300 |
int nIndex; |
|
301 |
REGISTER OBJ slf; |
|
302 |
REGISTER OBJ cls; |
|
303 |
||
304 |
if (__isSmallInteger(index)) { |
|
12879 | 305 |
indx = __intVal(index) - 1; |
306 |
slf = self; |
|
307 |
||
308 |
byte = indx / 8; |
|
309 |
indx = indx % 8; |
|
310 |
||
311 |
if ((cls = __qClass(slf)) != @global(ByteArray)) { |
|
312 |
if (indx < 0) goto badIndex; |
|
313 |
byte += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
314 |
} |
|
315 |
nIndex = __byteArraySize(slf); |
|
316 |
if ((unsigned)byte < (unsigned)nIndex) { |
|
317 |
RETURN ( __mkSmallInteger(((__ByteArrayInstPtr(slf)->ba_element[byte] & (1 << indx)) != 0)) ); |
|
318 |
} |
|
4128 | 319 |
} |
4386
ecc88f1c1169
oops - subclasses with instVars allowed for index-less-than-1-access
Claus Gittinger <cg@exept.de>
parents:
4367
diff
changeset
|
320 |
badIndex: ; |
4128 | 321 |
%}. |
5439 | 322 |
byteIndex := ((index-1) // 8) + 1. |
323 |
bitIndex0 := ((index-1) \\ 8). |
|
324 |
byte := self at:byteIndex. |
|
325 |
^ byte bitTest:(1 bitShift:bitIndex0). |
|
4128 | 326 |
|
327 |
" |
|
12879 | 328 |
#[ 1 1 1 1 ] bitAt:9 |
4128 | 329 |
#[ 1 1 1 1 ] bitAt:11 |
330 |
#[ 2 2 2 2 ] bitAt:10 |
|
331 |
" |
|
332 |
! |
|
333 |
||
5439 | 334 |
bitClearAt:index |
8908 | 335 |
"clear the bit at index (index starts with 1)" |
5439 | 336 |
|
337 |
|byteIndex bitIndex0 byte| |
|
4128 | 338 |
|
339 |
%{ /* NOCONTEXT */ |
|
340 |
||
341 |
REGISTER int indx; |
|
342 |
REGISTER int byte; |
|
343 |
int nIndex; |
|
344 |
REGISTER OBJ slf; |
|
345 |
REGISTER OBJ cls; |
|
346 |
||
5439 | 347 |
if (0 /* __isSmallInteger(index) */) { |
8908 | 348 |
indx = __intVal(index) - 1; |
349 |
slf = self; |
|
350 |
||
351 |
byte = indx / 8; |
|
352 |
indx = indx % 8; |
|
353 |
||
354 |
if ((cls = __qClass(slf)) != @global(ByteArray)) { |
|
355 |
if (indx < 0) goto badIndex; |
|
356 |
byte += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
357 |
} |
|
358 |
nIndex = __byteArraySize(slf); |
|
359 |
if ((unsigned)byte < (unsigned)nIndex) { |
|
360 |
__ByteArrayInstPtr(slf)->ba_element[byte] &= ~(1 << indx); |
|
361 |
RETURN (slf); |
|
362 |
} |
|
4128 | 363 |
} |
4386
ecc88f1c1169
oops - subclasses with instVars allowed for index-less-than-1-access
Claus Gittinger <cg@exept.de>
parents:
4367
diff
changeset
|
364 |
badIndex: ; |
4128 | 365 |
%}. |
5439 | 366 |
byteIndex := ((index-1) // 8) + 1. |
367 |
bitIndex0 := ((index-1) \\ 8). |
|
368 |
byte := self at:byteIndex. |
|
369 |
byte := byte bitClear:(1 bitShift:bitIndex0). |
|
370 |
self at:byteIndex put:byte. |
|
371 |
||
372 |
" |
|
8908 | 373 |
#[0 0 0 0] copy bitClearAt:1 |
374 |
#[0 0 0 0] copy bitClearAt:7 |
|
375 |
#[0 0 0 0] copy bitClearAt:8 |
|
376 |
#[0 0 0 0] copy bitClearAt:9 |
|
5439 | 377 |
" |
4128 | 378 |
" |
8908 | 379 |
#[ 0 0 0 0 ] bitSetAt:1 |
380 |
#[ 0 0 0 0 ] bitSetAt:4 |
|
381 |
#[ 0 0 0 0 ] bitSetAt:8 |
|
382 |
#[ 0 0 0 0 ] bitSetAt:9 |
|
383 |
#[ 0 0 0 0 ] bitSetAt:10 |
|
384 |
#[ 0 0 0 0 ] bitSetAt:11 |
|
5439 | 385 |
" |
386 |
||
387 |
||
388 |
||
389 |
! |
|
390 |
||
391 |
bitSetAt:index |
|
8908 | 392 |
"set the bit at index (index starts with 1)" |
5439 | 393 |
|
394 |
|byteIndex bitIndex0 byte| |
|
395 |
||
396 |
%{ /* NOCONTEXT */ |
|
397 |
||
398 |
REGISTER int indx; |
|
399 |
REGISTER int byte; |
|
400 |
int nIndex; |
|
401 |
REGISTER OBJ slf; |
|
402 |
REGISTER OBJ cls; |
|
403 |
||
404 |
if (0 /* __isSmallInteger(index) */) { |
|
8908 | 405 |
indx = __intVal(index) - 1; |
406 |
slf = self; |
|
407 |
||
408 |
byte = indx / 8; |
|
409 |
indx = indx % 8; |
|
410 |
||
411 |
if ((cls = __qClass(slf)) != @global(ByteArray)) { |
|
412 |
if (indx < 0) goto badIndex; |
|
413 |
byte += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
414 |
} |
|
415 |
nIndex = __byteArraySize(slf); |
|
416 |
if ((unsigned)byte < (unsigned)nIndex) { |
|
417 |
__ByteArrayInstPtr(slf)->ba_element[byte] |= (1 << indx); |
|
418 |
RETURN (slf); |
|
419 |
} |
|
5439 | 420 |
} |
421 |
badIndex: ; |
|
422 |
%}. |
|
423 |
byteIndex := ((index-1) // 8) + 1. |
|
424 |
bitIndex0 := ((index-1) \\ 8). |
|
425 |
byte := self at:byteIndex. |
|
426 |
byte := byte bitOr:(1 bitShift:bitIndex0). |
|
427 |
self at:byteIndex put:byte. |
|
428 |
||
429 |
" |
|
8908 | 430 |
#[0 0 0 0] copy bitSetAt:1 |
431 |
#[0 0 0 0] copy bitSetAt:7 |
|
432 |
#[0 0 0 0] copy bitSetAt:8 |
|
433 |
#[0 0 0 0] copy bitSetAt:9 |
|
5439 | 434 |
" |
435 |
" |
|
8908 | 436 |
#[ 0 0 0 0 ] bitSetAt:1 |
437 |
#[ 0 0 0 0 ] bitSetAt:4 |
|
438 |
#[ 0 0 0 0 ] bitSetAt:8 |
|
439 |
#[ 0 0 0 0 ] bitSetAt:9 |
|
440 |
#[ 0 0 0 0 ] bitSetAt:10 |
|
441 |
#[ 0 0 0 0 ] bitSetAt:11 |
|
4128 | 442 |
" |
443 |
||
444 |
||
445 |
||
5554 | 446 |
! ! |
447 |
||
448 |
!ByteArray methodsFor:'accessing-bytes'! |
|
4128 | 449 |
|
54 | 450 |
byteAt:index |
8908 | 451 |
"return the byte at index. |
452 |
For ByteArray, this is the same as basicAt:; |
|
249 | 453 |
however, for strings or symbols, this returns a numeric byteValue |
454 |
instead of a character." |
|
455 |
||
456 |
%{ /* NOCONTEXT */ |
|
457 |
||
458 |
REGISTER int indx; |
|
459 |
int nIndex; |
|
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
460 |
REGISTER OBJ slf; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
461 |
REGISTER OBJ cls; |
54 | 462 |
|
249 | 463 |
if (__isSmallInteger(index)) { |
4661 | 464 |
indx = __intVal(index) - 1; |
465 |
slf = self; |
|
466 |
if ((cls = __qClass(slf)) != @global(ByteArray)) { |
|
467 |
if (indx < 0) goto badIndex; |
|
468 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
469 |
} |
|
470 |
nIndex = __byteArraySize(slf); |
|
471 |
if ((unsigned)indx < (unsigned)nIndex) { |
|
8913 | 472 |
RETURN ( __mkSmallInteger((__ByteArrayInstPtr(slf)->ba_element[indx])) ); |
4661 | 473 |
} |
249 | 474 |
} |
4386
ecc88f1c1169
oops - subclasses with instVars allowed for index-less-than-1-access
Claus Gittinger <cg@exept.de>
parents:
4367
diff
changeset
|
475 |
badIndex: ; |
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
476 |
%}. |
249 | 477 |
^ (super basicAt:index) asInteger |
54 | 478 |
! |
479 |
||
249 | 480 |
byteAt:index put:value |
481 |
"set the byte at index. For ByteArray, this is the same as basicAt:put:. |
|
482 |
However, for Strings, this expects a byteValue to be stored." |
|
483 |
||
484 |
%{ /* NOCONTEXT */ |
|
485 |
||
486 |
REGISTER int indx; |
|
487 |
int nIndex; |
|
488 |
int val; |
|
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
489 |
REGISTER OBJ slf; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
490 |
REGISTER OBJ cls; |
54 | 491 |
|
249 | 492 |
if (__bothSmallInteger(index, value)) { |
4661 | 493 |
val = __intVal(value); |
494 |
if ((unsigned)(val) <= 0xFF /* i.e. (val >= 0) && (val <= 255) */) { |
|
495 |
indx = __intVal(index) - 1; |
|
496 |
slf = self; |
|
497 |
if ((cls = __qClass(slf)) != ByteArray) { |
|
498 |
if (indx < 0) goto badIndex; |
|
499 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
500 |
} |
|
501 |
nIndex = __byteArraySize(slf); |
|
502 |
if ((unsigned)indx < (unsigned)nIndex) { |
|
503 |
__ByteArrayInstPtr(slf)->ba_element[indx] = val; |
|
504 |
RETURN ( value ); |
|
505 |
} |
|
506 |
} |
|
249 | 507 |
} |
4386
ecc88f1c1169
oops - subclasses with instVars allowed for index-less-than-1-access
Claus Gittinger <cg@exept.de>
parents:
4367
diff
changeset
|
508 |
badIndex: ; |
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
509 |
%}. |
249 | 510 |
^ super basicAt:index put:value |
5554 | 511 |
! ! |
512 |
||
513 |
!ByteArray methodsFor:'accessing-longs'! |
|
54 | 514 |
|
2 | 515 |
doubleWordAt:index put:value |
516 |
"set the 4-bytes starting at index from the (unsigned) Integer value. |
|
359 | 517 |
The value should be in the range 0 to 16rFFFFFFFF |
518 |
(for negative values, the stored value is not defined). |
|
519 |
The value is stored in the machines natural byte order. |
|
520 |
Q: should it store signed values ? (see ByteArray signedDoubleWordAt:put:)" |
|
521 |
||
522 |
|t| |
|
523 |
||
524 |
%{ /* NOCONTEXT */ |
|
525 |
||
526 |
REGISTER int indx; |
|
527 |
int nIndex; |
|
528 |
union { |
|
8908 | 529 |
unsigned char u_char[4]; |
530 |
unsigned int u_uint; |
|
359 | 531 |
} val; |
532 |
OBJ cls; |
|
2950 | 533 |
unsigned char *byteP; |
359 | 534 |
|
535 |
if (__isSmallInteger(index)) { |
|
8908 | 536 |
if (__isSmallInteger(value)) { |
537 |
val.u_uint = __intVal(value); |
|
538 |
} else { |
|
539 |
val.u_uint = __longIntVal(value); |
|
540 |
if (val.u_uint == 0) goto error; |
|
541 |
} |
|
542 |
||
543 |
indx = __intVal(index); |
|
544 |
if (indx > 0) { |
|
545 |
if ((cls = __qClass(self)) != @global(ByteArray)) |
|
546 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
547 |
nIndex = __qSize(self) - OHDR_SIZE; |
|
548 |
if ((indx+3) <= nIndex) { |
|
549 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
550 |
#if defined(__i386__) || defined(UNALIGNED_FETCH_OK) |
8908 | 551 |
((unsigned int *)byteP)[0] = val.u_uint; |
3422
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
552 |
#else |
8908 | 553 |
if (((unsigned INT)byteP & 3) == 0) { |
554 |
((unsigned int *)byteP)[0] = val.u_uint; |
|
555 |
} else { |
|
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]; |
|
560 |
} |
|
3422
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
561 |
#endif |
8908 | 562 |
RETURN ( value ); |
563 |
} |
|
564 |
} |
|
359 | 565 |
} |
566 |
error: ; |
|
567 |
%}. |
|
5439 | 568 |
^ super doubleWordAt:index put:value. |
359 | 569 |
|
570 |
" |
|
571 |
|b| |
|
572 |
b := ByteArray new:4. |
|
573 |
b doubleWordAt:1 put:16r04030201. |
|
574 |
b inspect |
|
575 |
" |
|
576 |
! |
|
577 |
||
578 |
doubleWordAt:index put:value MSB:msb |
|
579 |
"set the 4-bytes starting at index from the (unsigned) Integer value. |
|
129 | 580 |
The value must be in the range 0 to 16rFFFFFFFF. |
359 | 581 |
The value is stored MSB-first if msb is true; LSB-first otherwise. |
129 | 582 |
question: should it store signed values ? (see ByteArray signedDoubleWordAt:put:)" |
2 | 583 |
|
584 |
|t| |
|
585 |
||
586 |
%{ /* NOCONTEXT */ |
|
587 |
||
588 |
REGISTER int indx; |
|
589 |
int nIndex; |
|
590 |
int val; |
|
249 | 591 |
OBJ cls; |
2950 | 592 |
unsigned char *byteP; |
2 | 593 |
|
359 | 594 |
if (__isSmallInteger(index)) { |
8908 | 595 |
if (__isSmallInteger(value)) { |
596 |
val = __intVal(value); |
|
597 |
} else { |
|
598 |
val = __longIntVal(value); |
|
599 |
if (val == 0) goto error; |
|
600 |
} |
|
601 |
indx = __intVal(index); |
|
602 |
if (indx > 0) { |
|
603 |
if ((cls = __qClass(self)) != @global(ByteArray)) |
|
604 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
605 |
nIndex = __qSize(self) - OHDR_SIZE; |
|
606 |
if ((indx+3) <= nIndex) { |
|
607 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
|
608 |
if (msb == true) { |
|
609 |
/* |
|
610 |
* most significant byte first (i.e sparc order) |
|
611 |
*/ |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
612 |
#if defined(__MSBFIRST__) |
8908 | 613 |
if (((INT)byteP & 3) == 0) { |
614 |
((int *)byteP)[0] = val; |
|
615 |
} else |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
616 |
#endif |
8908 | 617 |
{ |
618 |
byteP[3] = val & 0xFF; |
|
619 |
val >>= 8; |
|
620 |
byteP[2] = val & 0xFF; |
|
621 |
val >>= 8; |
|
622 |
byteP[1] = val & 0xFF; |
|
623 |
val >>= 8; |
|
624 |
byteP[0] = val & 0xFF; |
|
625 |
} |
|
626 |
} else { |
|
627 |
/* |
|
628 |
* least significant byte first (i.e i386/alpha order) |
|
629 |
*/ |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
630 |
#if defined(__i386__) || (defined(__LSBFIRST__) && defined(UNALIGNED_FETCH_OK)) |
8908 | 631 |
((int *)byteP)[0] = val; |
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
632 |
#else |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
633 |
# if defined(__LSBFIRST__) |
8908 | 634 |
if (((unsigned INT)byteP & 3) == 0) { |
635 |
((int *)byteP)[0] = val; |
|
636 |
} else |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
637 |
# endif |
8908 | 638 |
{ |
639 |
byteP[0] = val & 0xFF; |
|
640 |
val >>= 8; |
|
641 |
byteP[1] = val & 0xFF; |
|
642 |
val >>= 8; |
|
643 |
byteP[2] = val & 0xFF; |
|
644 |
val >>= 8; |
|
645 |
byteP[3] = val & 0xFF; |
|
646 |
} |
|
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
647 |
#endif |
8908 | 648 |
} |
649 |
RETURN ( value ); |
|
650 |
} |
|
651 |
} |
|
2 | 652 |
} |
359 | 653 |
error: ; |
654 |
%}. |
|
5439 | 655 |
^ super doubleWordAt:index put:value MSB:msb |
129 | 656 |
|
657 |
" |
|
658 |
|b| |
|
359 | 659 |
b := ByteArray new:8. |
660 |
b doubleWordAt:1 put:16r04030201 MSB:true. |
|
661 |
b doubleWordAt:5 put:16r04030201 MSB:false. |
|
129 | 662 |
b inspect |
663 |
" |
|
5554 | 664 |
! ! |
665 |
||
666 |
!ByteArray methodsFor:'accessing-shorts'! |
|
54 | 667 |
|
563 | 668 |
wordAt:index |
669 |
"return the 2-bytes starting at index as an (unsigned) Integer. |
|
670 |
The value is retrieved in the machines natural byte order |
|
671 |
Question: should it be retrieve signed values ? (see ByteArray>>signedWordAt:)" |
|
672 |
||
673 |
%{ /* NOCONTEXT */ |
|
54 | 674 |
|
563 | 675 |
REGISTER int indx; |
676 |
int nIndex; |
|
677 |
union { |
|
12879 | 678 |
unsigned char u_char[2]; |
679 |
unsigned short u_ushort; |
|
563 | 680 |
} val; |
2950 | 681 |
unsigned char *byteP; |
54 | 682 |
|
563 | 683 |
if (__isSmallInteger(index)) { |
12879 | 684 |
indx = __intVal(index); |
685 |
if (indx > 0) { |
|
686 |
if (!__isByteArrayLike(self)) |
|
687 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(self))->c_ninstvars)); |
|
688 |
nIndex = __byteArraySize(self); |
|
689 |
if ((indx+1) <= nIndex) { |
|
690 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
691 |
#if defined(__i386__) || defined(UNALIGNED_FETCH_OK) |
12879 | 692 |
val.u_ushort = ((unsigned short *)byteP)[0]; |
3422
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
693 |
#else |
12879 | 694 |
/* |
695 |
* mhmh to be measured: |
|
696 |
* the if may hurt more than the additional |
|
697 |
* memory cycles on some machines ... |
|
698 |
*/ |
|
699 |
if (((INT)byteP & 1) == 0) { |
|
700 |
/* aligned */ |
|
701 |
val.u_ushort = ((unsigned short *)byteP)[0]; |
|
702 |
} else { |
|
703 |
val.u_char[0] = byteP[0]; |
|
704 |
val.u_char[1] = byteP[1]; |
|
705 |
} |
|
3422
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
706 |
#endif |
12879 | 707 |
RETURN ( __mkSmallInteger((val.u_ushort)) ); |
708 |
} |
|
709 |
} |
|
563 | 710 |
} |
711 |
%}. |
|
5439 | 712 |
^ super wordAt:index |
54 | 713 |
! |
714 |
||
563 | 715 |
wordAt:index MSB:msb |
716 |
"return the 2-bytes starting at index as an (unsigned) Integer. |
|
717 |
The value is retrieved MSB (high 8 bits at lower index) if msb is true; |
|
718 |
LSB-first (i.e. low 8-bits at lower byte index) if its false. |
|
719 |
Question: should it be retrieve signed values ? (see ByteArray>>signedWordAt:)" |
|
720 |
||
721 |
%{ /* NOCONTEXT */ |
|
722 |
||
723 |
REGISTER int indx; |
|
724 |
int nIndex; |
|
725 |
int val; |
|
2950 | 726 |
unsigned char *byteP; |
54 | 727 |
|
563 | 728 |
if (__isSmallInteger(index)) { |
12879 | 729 |
indx = __intVal(index); |
730 |
if (indx > 0) { |
|
731 |
if (!__isByteArrayLike(self)) |
|
732 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(self))->c_ninstvars)); |
|
733 |
nIndex = __byteArraySize(self); |
|
734 |
if ((indx+1) <= nIndex) { |
|
735 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
|
736 |
if (msb == true) { |
|
737 |
/* |
|
738 |
* most significant byte first (i.e sparc order) |
|
739 |
*/ |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
740 |
#if defined(__MSBFIRST__) |
12879 | 741 |
/* |
742 |
* mhmh to be measured: |
|
743 |
* the if may hurt more than the additional |
|
744 |
* memory cycles on some machines ... |
|
745 |
*/ |
|
746 |
if (((INT)byteP & 1) == 0) { |
|
747 |
/* aligned */ |
|
748 |
val = ((unsigned short *)byteP)[0]; |
|
749 |
} else |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
750 |
#endif |
12879 | 751 |
{ |
752 |
val = byteP[0]; |
|
753 |
val = (val << 8) + byteP[1]; |
|
754 |
} |
|
755 |
} else { |
|
756 |
/* |
|
757 |
* least significant byte first (i.e i386/alpha order) |
|
758 |
*/ |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
759 |
#if defined(__i386__) || (defined(__LSBFIRST__) && defined(UNALIGNED_FETCH_OK)) |
12879 | 760 |
val = ((unsigned short *)byteP)[0]; |
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
761 |
#else |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
762 |
# if defined(__LSBFIRST__) |
12879 | 763 |
/* |
764 |
* mhmh to be measured: |
|
765 |
* the if may hurt more than the additional |
|
766 |
* memory cycles on some machines ... |
|
767 |
*/ |
|
768 |
if (((INT)byteP & 1) == 0) { |
|
769 |
/* aligned */ |
|
770 |
val = ((unsigned short *)byteP)[0]; |
|
771 |
} else |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
772 |
# endif |
12879 | 773 |
{ |
774 |
val = byteP[1]; |
|
775 |
val = (val << 8) + byteP[0]; |
|
776 |
} |
|
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
777 |
#endif |
12879 | 778 |
} |
779 |
RETURN ( __mkSmallInteger(val) ); |
|
780 |
} |
|
781 |
} |
|
563 | 782 |
} |
783 |
%}. |
|
5439 | 784 |
^ super wordAt:index MSB:msb |
54 | 785 |
! |
786 |
||
563 | 787 |
wordAt:index put:value |
788 |
"set the 2-bytes starting at index from the (unsigned) Integer value. |
|
8908 | 789 |
The stored value must be in the range 0 .. 16rFFFF. |
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
790 |
The value is stored in the machines natural byteorder, |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
791 |
i.e. this method should only be used to fill byteArrays which are |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
792 |
used internally (not passed to other machines). |
563 | 793 |
Question: should it accept signed values ? (see ByteArray>>signedWordAt:put:)" |
794 |
||
795 |
%{ /* NOCONTEXT */ |
|
54 | 796 |
|
563 | 797 |
REGISTER int indx; |
798 |
int nIndex; |
|
799 |
int v; |
|
800 |
union { |
|
12879 | 801 |
unsigned char u_char[2]; |
802 |
unsigned short u_ushort; |
|
563 | 803 |
} val; |
2950 | 804 |
unsigned char *byteP; |
129 | 805 |
|
563 | 806 |
if (__bothSmallInteger(index, value)) { |
12879 | 807 |
indx = __intVal(index); |
808 |
if (indx > 0) { |
|
809 |
if (!__isByteArrayLike(self)) |
|
810 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(self))->c_ninstvars)); |
|
811 |
nIndex = __byteArraySize(self); |
|
812 |
if ((indx+1) <= nIndex) { |
|
813 |
val.u_ushort = v = __intVal(value); |
|
814 |
if ((v & ~0xFFFF) == 0 /* i.e. (val >= 0) && (val <= 0xFFFF) */) { |
|
815 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
816 |
#if defined(__i386__) || defined(UNALIGNED_FETCH_OK) |
12879 | 817 |
((unsigned short *)byteP)[0] = val.u_ushort; |
3422
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
818 |
#else |
12879 | 819 |
/* |
820 |
* mhmh to be measured: |
|
821 |
* the if may hurt more than the additional |
|
822 |
* memory cycles on some machines ... |
|
823 |
*/ |
|
824 |
if (((INT)byteP & 1) == 0) { |
|
825 |
/* aligned */ |
|
826 |
((unsigned short *)byteP)[0] = val.u_ushort; |
|
827 |
} else { |
|
828 |
byteP[0] = val.u_char[0]; |
|
829 |
byteP[1] = val.u_char[1]; |
|
830 |
} |
|
3422
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
831 |
#endif |
12879 | 832 |
RETURN ( value ); |
833 |
} |
|
834 |
} |
|
835 |
} |
|
563 | 836 |
} |
837 |
%}. |
|
5439 | 838 |
^ super wordAt:index put:value |
129 | 839 |
|
840 |
" |
|
563 | 841 |
|b| |
842 |
b := ByteArray new:4. |
|
843 |
b wordAt:1 put:16r0102. |
|
844 |
b wordAt:3 put:16r0304. |
|
8908 | 845 |
b inspect |
129 | 846 |
" |
847 |
! |
|
848 |
||
563 | 849 |
wordAt:index put:value MSB:msb |
850 |
"set the 2-bytes starting at index from the (unsigned) Integer value. |
|
8908 | 851 |
The stored value must be in the range 0 .. 16rFFFF. |
563 | 852 |
The value is stored LSB-first (i.e. the low 8bits are stored at the |
853 |
lower index) if msb is false, MSB-first otherwise. |
|
854 |
Question: should it accept signed values ? (see ByteArray>>signedWordAt:put:)" |
|
855 |
||
856 |
%{ /* NOCONTEXT */ |
|
857 |
||
858 |
REGISTER int indx; |
|
859 |
int nIndex; |
|
860 |
int val; |
|
861 |
OBJ cls; |
|
2950 | 862 |
unsigned char *byteP; |
563 | 863 |
|
864 |
if (__bothSmallInteger(index, value)) { |
|
12879 | 865 |
indx = __intVal(index); |
866 |
if (indx > 0) { |
|
867 |
if (!__isByteArrayLike(self)) |
|
868 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(self))->c_ninstvars)); |
|
869 |
nIndex = __byteArraySize(self); |
|
870 |
if ((indx+1) <= nIndex) { |
|
871 |
val = __intVal(value); |
|
872 |
if ((val & ~0xFFFF) == 0 /* i.e. (val >= 0) && (val <= 0xFFFF) */) { |
|
873 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
|
874 |
if (msb == true) { |
|
875 |
/* |
|
876 |
* most significant byte first (i.e sparc order) |
|
877 |
*/ |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
878 |
#if defined(__MSBFIRST__) |
12879 | 879 |
/* |
880 |
* mhmh to be measured: |
|
881 |
* the if may hurt more than the additional |
|
882 |
* memory cycles on some machines ... |
|
883 |
*/ |
|
884 |
if (((INT)byteP & 1) == 0) { |
|
885 |
/* aligned */ |
|
886 |
((unsigned short *)byteP)[0] = val; |
|
887 |
} else |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
888 |
#endif |
12879 | 889 |
{ |
890 |
byteP[1] = val & 0xFF; |
|
891 |
byteP[0] = (val>>8) & 0xFF; |
|
892 |
} |
|
893 |
} else { |
|
894 |
/* |
|
895 |
* least significant byte first (i.e i386/alpha order) |
|
896 |
*/ |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
897 |
#if defined(__i386__) || (defined(__LSBFIRST__) && defined(UNALIGNED_FETCH_OK)) |
12879 | 898 |
((unsigned short *)byteP)[0] = val; |
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
899 |
#else |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
900 |
# if defined(__LSBFIRST__) |
12879 | 901 |
/* |
902 |
* mhmh to be measured: |
|
903 |
* the if may hurt more than the additional |
|
904 |
* memory cycles on some machines ... |
|
905 |
*/ |
|
906 |
if (((INT)byteP & 1) == 0) { |
|
907 |
/* aligned */ |
|
908 |
((unsigned short *)byteP)[0] = val; |
|
909 |
} else |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
910 |
# endif |
12879 | 911 |
{ |
912 |
byteP[0] = val & 0xFF; |
|
913 |
byteP[1] = (val>>8) & 0xFF; |
|
914 |
} |
|
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
915 |
#endif |
12879 | 916 |
} |
917 |
RETURN ( value ); |
|
918 |
} |
|
919 |
} |
|
920 |
} |
|
563 | 921 |
} |
922 |
%}. |
|
5439 | 923 |
^ super wordAt:index put:value MSB:msb |
129 | 924 |
|
925 |
" |
|
563 | 926 |
|b| |
927 |
b := ByteArray new:8. |
|
928 |
b wordAt:1 put:16r0102 MSB:false. |
|
929 |
b wordAt:3 put:16r0304 MSB:false. |
|
930 |
b wordAt:5 put:16r0102 MSB:true. |
|
931 |
b wordAt:7 put:16r0304 MSB:true. |
|
8908 | 932 |
b inspect |
129 | 933 |
" |
1 | 934 |
! ! |
935 |
||
11502 | 936 |
|
3345 | 937 |
!ByteArray methodsFor:'comparing'! |
938 |
||
939 |
= aByteArray |
|
940 |
"Compare the receiver with the argument and return true if the |
|
941 |
receiver is equal to the argument (i.e. has the same size and elements). |
|
942 |
Otherwise return false." |
|
943 |
||
944 |
%{ /* NOCONTEXT */ |
|
945 |
||
946 |
int l1, l2; |
|
947 |
REGISTER OBJ s = aByteArray; |
|
948 |
unsigned char *cp1, *cp2; |
|
949 |
OBJ cls; |
|
950 |
OBJ myCls; |
|
951 |
INT addrDelta; |
|
952 |
||
953 |
if (s == self) { |
|
12879 | 954 |
RETURN ( true ); |
3345 | 955 |
} |
956 |
if (! __isNonNilObject(s)) { |
|
12879 | 957 |
RETURN ( false ); |
3345 | 958 |
} |
959 |
||
960 |
cls = __qClass(s); |
|
961 |
myCls = __qClass(self); |
|
962 |
||
963 |
if (cls == myCls) { |
|
12879 | 964 |
l2 = __byteArraySize(s); |
965 |
l1 = __byteArraySize(self); |
|
966 |
if (l1 != l2) { |
|
967 |
RETURN ( false ); |
|
968 |
} |
|
969 |
||
970 |
cp1 = __byteArrayVal(self); |
|
971 |
cp2 = __byteArrayVal(s); |
|
972 |
||
973 |
/* |
|
974 |
* care for instances of subclasses ... |
|
975 |
*/ |
|
976 |
if (cls != ByteArray) { |
|
977 |
int n = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
978 |
||
979 |
cp2 += n; |
|
980 |
cp1 += n; |
|
981 |
l1 -= n; |
|
982 |
} |
|
983 |
||
984 |
addrDelta = cp2 - cp1; |
|
985 |
while (l1 >= (sizeof(unsigned INT) * 4)) { |
|
986 |
if (((unsigned INT *)cp1)[0] != ((unsigned INT *)(cp1+addrDelta))[0]) { |
|
987 |
RETURN (false); |
|
988 |
} |
|
989 |
if (((unsigned INT *)cp1)[1] != ((unsigned INT *)(cp1+addrDelta))[1]) { |
|
990 |
RETURN (false); |
|
991 |
} |
|
992 |
if (((unsigned INT *)cp1)[2] != ((unsigned INT *)(cp1+addrDelta))[2]) { |
|
993 |
RETURN (false); |
|
994 |
} |
|
995 |
if (((unsigned INT *)cp1)[3] != ((unsigned INT *)(cp1+addrDelta))[3]) { |
|
996 |
RETURN (false); |
|
997 |
} |
|
998 |
l1 -= sizeof(unsigned INT)*4; |
|
999 |
cp1 += sizeof(unsigned INT)*4; |
|
1000 |
} |
|
1001 |
while (l1 >= sizeof(unsigned INT)) { |
|
1002 |
if (*((unsigned INT *)cp1) != *((unsigned INT *)(cp1+addrDelta))) { |
|
1003 |
RETURN (false); |
|
1004 |
} |
|
1005 |
l1 -= sizeof(unsigned INT); |
|
1006 |
cp1 += sizeof(unsigned INT); |
|
1007 |
} |
|
1008 |
if (l1 >= sizeof(unsigned short)) { |
|
1009 |
if (*((unsigned short *)cp1) != *((unsigned short *)(cp1+addrDelta))) { |
|
1010 |
RETURN (false); |
|
1011 |
} |
|
1012 |
l1 -= sizeof(unsigned short); |
|
1013 |
cp1 += sizeof(unsigned short); |
|
1014 |
} |
|
1015 |
while (l1) { |
|
1016 |
if (*cp1 != *(cp1+addrDelta)) { |
|
1017 |
RETURN (false); |
|
1018 |
} |
|
1019 |
l1--; |
|
1020 |
cp1++; |
|
1021 |
} |
|
1022 |
||
1023 |
RETURN (true); |
|
3345 | 1024 |
} |
1025 |
%}. |
|
1026 |
^ super = aByteArray |
|
1027 |
! ! |
|
1028 |
||
12 | 1029 |
!ByteArray methodsFor:'converting'! |
1030 |
||
1031 |
asByteArray |
|
1032 |
"return the receiver as a byteArray" |
|
1033 |
||
1034 |
"could be an instance of a subclass..." |
|
1035 |
self class == ByteArray ifTrue:[ |
|
155 | 1036 |
^ self |
12 | 1037 |
]. |
1038 |
^ super asByteArray |
|
129 | 1039 |
|
1040 |
" |
|
8908 | 1041 |
'hello world' asByteArray |
1042 |
#(1 2 3 4 5 6 7) asByteArray |
|
1043 |
#(1 2 256 4 5 6 7) asByteArray |
|
129 | 1044 |
" |
4778 | 1045 |
! |
1046 |
||
12453
74715f6d4cbe
added: #asImmutableByteArray
Claus Gittinger <cg@exept.de>
parents:
12276
diff
changeset
|
1047 |
asImmutableByteArray |
74715f6d4cbe
added: #asImmutableByteArray
Claus Gittinger <cg@exept.de>
parents:
12276
diff
changeset
|
1048 |
"return a write-protected copy of myself" |
74715f6d4cbe
added: #asImmutableByteArray
Claus Gittinger <cg@exept.de>
parents:
12276
diff
changeset
|
1049 |
|
74715f6d4cbe
added: #asImmutableByteArray
Claus Gittinger <cg@exept.de>
parents:
12276
diff
changeset
|
1050 |
self assert:(ImmutableByteArray notNil). |
74715f6d4cbe
added: #asImmutableByteArray
Claus Gittinger <cg@exept.de>
parents:
12276
diff
changeset
|
1051 |
^ self copy changeClassTo:ImmutableByteArray |
74715f6d4cbe
added: #asImmutableByteArray
Claus Gittinger <cg@exept.de>
parents:
12276
diff
changeset
|
1052 |
! |
74715f6d4cbe
added: #asImmutableByteArray
Claus Gittinger <cg@exept.de>
parents:
12276
diff
changeset
|
1053 |
|
12276 | 1054 |
asInteger |
1055 |
"convert myself to an integer - the first byte is most significant. |
|
1056 |
This is also in Squeak." |
|
1057 |
||
14135 | 1058 |
^ (LargeInteger digitBytes:self MSB:true) compressed |
12276 | 1059 |
|
1060 |
" |
|
14135 | 1061 |
#[ 2 ] asInteger hexPrintString |
1062 |
#[ 16r1 16r2 ] asInteger hexPrintString |
|
1063 |
#[4 0 0 0 0 0 0 0] asInteger hexPrintString |
|
12276 | 1064 |
" |
1065 |
! |
|
1066 |
||
12621 | 1067 |
asIntegerMSB:isMSBFirst |
1068 |
"convert myself to an integer - the first byte is most significant. |
|
1069 |
This is also in Squeak." |
|
1070 |
||
14135 | 1071 |
^ (LargeInteger digitBytes:self MSB:isMSBFirst) compressed |
12621 | 1072 |
|
1073 |
" |
|
14135 | 1074 |
(#[ 2 ] asIntegerMSB:true) hexPrintString |
1075 |
(#[ 16r1 16r2 ] asIntegerMSB:true) hexPrintString |
|
1076 |
(#[ 16r1 16r2 ] asIntegerMSB:false) hexPrintString |
|
1077 |
(#[4 0 0 0 0 0 0 0] asIntegerMSB:true) hexPrintString |
|
12621 | 1078 |
" |
1079 |
! |
|
1080 |
||
7117 | 1081 |
asPackedString |
1082 |
"ST-80 compatibility: encode the receiver into an ascii String |
|
1083 |
with 6bits encoded per character. Each group of 6 bits is encoded |
|
1084 |
as a corresponding character (32+value) and the resulting string |
|
1085 |
is returned. The resulting string is always a multiple of 4 (since |
|
1086 |
24 is the lcm of 6 and 8) and the number of remaining characters is |
|
1087 |
encoded in the last character. |
|
1088 |
ST-80 uses this encoding for Images ... |
|
1089 |
This is very similar (but not equal) to the algorithm used in RFC1421. |
|
1090 |
PS: I dont like it ;-)" |
|
1091 |
||
8908 | 1092 |
|outStream |
7117 | 1093 |
index "{ Class:SmallInteger}" |
1094 |
nextIndex "{ Class:SmallInteger}" |
|
1095 |
stop "{ Class:SmallInteger}" |
|
1096 |
n "{ Class:SmallInteger}" |
|
1097 |
mod "{ Class:SmallInteger}" |
|
1098 |
cpl| |
|
1099 |
||
1100 |
outStream := WriteStream on:String new. |
|
8908 | 1101 |
index := 1. |
7117 | 1102 |
stop := self size. |
1103 |
||
1104 |
stop > 100 ifTrue:[ |
|
12879 | 1105 |
"/ cg: |
1106 |
"/ initial lineBreak |
|
1107 |
outStream cr. |
|
7117 | 1108 |
]. |
1109 |
cpl := 0. |
|
1110 |
||
1111 |
[index <= stop] whileTrue:[ |
|
12879 | 1112 |
"take 3 source bytes" |
1113 |
n := (self at:index) bitShift:16. |
|
1114 |
(index < stop) ifTrue:[ |
|
1115 |
nextIndex := index + 1. |
|
1116 |
n := n bitOr:((self at:nextIndex) bitShift:8). |
|
1117 |
(nextIndex < stop) ifTrue:[ |
|
1118 |
n := n bitOr:(self at:(index + 2)). |
|
1119 |
]. |
|
1120 |
]. |
|
1121 |
index := index + 3. |
|
1122 |
||
1123 |
"took me a while to find that one out ..." |
|
1124 |
n := n bitXor:16r820820. |
|
1125 |
||
1126 |
outStream nextPut:(Character value:((n bitShift:-18) bitAnd:16r3F) + 32). |
|
1127 |
outStream nextPut:(Character value:((n bitShift:-12) bitAnd:16r3F) + 32). |
|
1128 |
outStream nextPut:(Character value:((n bitShift:-6) bitAnd:16r3F) + 32). |
|
1129 |
outStream nextPut:(Character value:(n bitAnd:16r3F) + 32). |
|
1130 |
||
1131 |
"/ cg: |
|
1132 |
"/ lineBreak after every 120 characters |
|
1133 |
"/ fromPackedString will ignore those |
|
1134 |
cpl := cpl + 4. |
|
1135 |
cpl >= 120 ifTrue:[ |
|
1136 |
outStream cr. |
|
1137 |
cpl := 0. |
|
1138 |
]. |
|
7117 | 1139 |
]. |
1140 |
(mod := stop \\ 3) ~~ 0 ifTrue:[ |
|
12879 | 1141 |
outStream backStep. |
1142 |
outStream nextPut:(Character value:(mod + 96)). |
|
7117 | 1143 |
]. |
1144 |
^ outStream contents |
|
1145 |
||
1146 |
"Modified: 12.11.1996 / 15:45:02 / cg" |
|
1147 |
! |
|
1148 |
||
5900 | 1149 |
asString |
1150 |
"speed up string conversions" |
|
1151 |
||
1152 |
|size| |
|
1153 |
||
5901 | 1154 |
self class == ByteArray ifTrue:[ |
8908 | 1155 |
size := self size. |
1156 |
^ (String uninitializedNew:size) replaceBytesFrom:1 to:size with:self startingAt:1. |
|
5901 | 1157 |
]. |
1158 |
^ super asString |
|
5900 | 1159 |
|
1160 |
" |
|
1161 |
#[60 61 62 63] asString |
|
1162 |
" |
|
1163 |
! |
|
1164 |
||
4807 | 1165 |
decodeAsLiteralArray |
1166 |
"given a literalEncoding in the receiver, |
|
1167 |
create & return the corresponding object. |
|
1168 |
The inverse operation to #literalArrayEncoding." |
|
1169 |
||
1170 |
^ self |
|
1171 |
||
1172 |
! |
|
1173 |
||
4778 | 1174 |
literalArrayEncoding |
1175 |
"encode myself as an array literal, from which a copy of the receiver |
|
1176 |
can be reconstructed with #decodeAsLiteralArray." |
|
1177 |
||
12487 | 1178 |
|cls| |
1179 |
||
1180 |
((cls := self class) == ByteArray or:[cls == ImmutableByteArray]) ifTrue:[ |
|
12879 | 1181 |
^ self |
11063
ca134030dff4
changed #literalArrayEncoding: self is onlz ok for realByteArrays
fm
parents:
11010
diff
changeset
|
1182 |
]. |
ca134030dff4
changed #literalArrayEncoding: self is onlz ok for realByteArrays
fm
parents:
11010
diff
changeset
|
1183 |
^ super literalArrayEncoding |
4778 | 1184 |
|
1185 |
" |
|
8908 | 1186 |
#[1 2 3] literalArrayEncoding |
4778 | 1187 |
" |
12 | 1188 |
! ! |
1189 |
||
92 | 1190 |
!ByteArray methodsFor:'copying'! |
1191 |
||
5024 | 1192 |
copy |
1193 |
"redefined for a bit more speed" |
|
1194 |
||
1195 |
self class == ByteArray ifTrue:[ |
|
8908 | 1196 |
^ self copyFrom:1 to:(self size) |
5024 | 1197 |
]. |
1198 |
^ super copy |
|
1199 |
! |
|
1200 |
||
92 | 1201 |
copyFrom:start to:stop |
1202 |
"return the subcollection starting at index start, anInteger and ending |
|
1203 |
at stop, anInteger. |
|
1204 |
- reimplemented here for speed" |
|
1205 |
||
1206 |
%{ /* NOCONTEXT */ |
|
1207 |
||
1208 |
REGISTER unsigned char *srcp; |
|
1209 |
REGISTER unsigned char *dstp; |
|
1210 |
REGISTER int count; |
|
1211 |
int len, index1, index2, sz; |
|
1212 |
OBJ newByteArray; |
|
1213 |
||
12466
353502b2baa9
__isByteArray() to __isByteArrayLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
12453
diff
changeset
|
1214 |
if (__isByteArrayLike(self) |
249 | 1215 |
&& __bothSmallInteger(start, stop)) { |
12879 | 1216 |
len = __byteArraySize(self); |
1217 |
index1 = __intVal(start); |
|
1218 |
index2 = __intVal(stop); |
|
1219 |
||
1220 |
if ((index1 <= index2) && (index1 > 0)) { |
|
1221 |
if (index2 <= len) { |
|
1222 |
count = index2 - index1 + 1; |
|
1223 |
__PROTECT_CONTEXT__ |
|
1224 |
sz = OHDR_SIZE + count; |
|
1225 |
__qNew(newByteArray, sz); /* OBJECT ALLOCATION */ |
|
1226 |
__UNPROTECT_CONTEXT__ |
|
1227 |
if (newByteArray != nil) { |
|
1228 |
__InstPtr(newByteArray)->o_class = ByteArray; |
|
1229 |
__qSTORE(newByteArray, ByteArray); |
|
1230 |
dstp = __ByteArrayInstPtr(newByteArray)->ba_element; |
|
1231 |
srcp = __ByteArrayInstPtr(self)->ba_element + index1 - 1; |
|
2648 | 1232 |
|
1233 |
#ifdef bcopy4 |
|
12879 | 1234 |
if (((unsigned INT)srcp & 3) == ((unsigned INT)dstp & 3)) { |
1235 |
int nW; |
|
1236 |
||
1237 |
/* copy unaligned part */ |
|
1238 |
while (count && (((unsigned INT)srcp & 3) != 0)) { |
|
1239 |
*dstp++ = *srcp++; |
|
1240 |
count--; |
|
1241 |
} |
|
1242 |
if (count) { |
|
1243 |
/* copy aligned part */ |
|
1244 |
nW = count >> 2; |
|
1245 |
if (count & 3) { |
|
1246 |
nW++; |
|
1247 |
} |
|
1248 |
bcopy4(srcp, dstp, nW); |
|
1249 |
} |
|
1250 |
RETURN ( newByteArray ); |
|
1251 |
} |
|
2648 | 1252 |
#endif /* bcopy4 */ |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
1253 |
#if __POINTER_SIZE__ == 8 |
12879 | 1254 |
if (((unsigned INT)srcp & 7) == ((unsigned INT)dstp & 7)) { |
1255 |
int nW; |
|
1256 |
||
1257 |
/* copy unaligned part */ |
|
1258 |
while (count && (((unsigned INT)srcp & 7) != 0)) { |
|
1259 |
*dstp++ = *srcp++; |
|
1260 |
count--; |
|
1261 |
} |
|
1262 |
/* copy aligned part */ |
|
1263 |
while (count >= 8) { |
|
1264 |
((unsigned INT *)dstp)[0] = ((unsigned INT *)srcp)[0]; |
|
1265 |
dstp += 8; |
|
1266 |
srcp += 8; |
|
1267 |
count -= 8; |
|
1268 |
} |
|
1269 |
/* copy remaining part */ |
|
1270 |
while (count) { |
|
1271 |
*dstp++ = *srcp++; |
|
1272 |
count--; |
|
1273 |
} |
|
1274 |
RETURN ( newByteArray ); |
|
1275 |
} |
|
4943
8e51d34f2b3a
slightly tuned #copyFrom:to: and #replaceBytesFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4941
diff
changeset
|
1276 |
#endif /* bcopy4 */ |
2648 | 1277 |
|
92 | 1278 |
#ifdef FAST_MEMCPY |
12879 | 1279 |
bcopy(srcp, dstp, count); |
92 | 1280 |
#else |
12879 | 1281 |
while (count--) { |
1282 |
*dstp++ = *srcp++; |
|
1283 |
} |
|
92 | 1284 |
#endif |
12879 | 1285 |
RETURN ( newByteArray ); |
1286 |
} |
|
1287 |
} |
|
1288 |
} |
|
92 | 1289 |
} |
1290 |
%}. |
|
1291 |
" |
|
1292 |
fall back in case of non-integer index or out-of-bound index; |
|
1293 |
will eventually lead to an out-of-bound signal raise |
|
1294 |
" |
|
1295 |
^ super copyFrom:start to:stop |
|
4941
7674fdddb1db
better byteArray copy on Intel
Claus Gittinger <cg@exept.de>
parents:
4931
diff
changeset
|
1296 |
|
7674fdddb1db
better byteArray copy on Intel
Claus Gittinger <cg@exept.de>
parents:
4931
diff
changeset
|
1297 |
" |
8908 | 1298 |
#[1 2 3 4 5 6 7 8 9 10] copyFrom:1 to:10 |
1299 |
#[1 2 3 4 5 6 7 8 9 10] copyFrom:5 to:7 |
|
1300 |
||
1301 |
#[1 2 3 4 5 6 7 8 9 10] copyFrom:5 to:11 |
|
1302 |
#[1 2 3 4 5 6 7 8 9 10] copyFrom:0 to:10 |
|
1303 |
#[1 2 3 4 5 6 7 8 9 10] copyFrom:0 to:9 |
|
4941
7674fdddb1db
better byteArray copy on Intel
Claus Gittinger <cg@exept.de>
parents:
4931
diff
changeset
|
1304 |
" |
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1305 |
! |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1306 |
|
5024 | 1307 |
shallowCopy |
1308 |
"redefined for a bit more speed" |
|
1309 |
||
1310 |
self class == ByteArray ifTrue:[ |
|
8908 | 1311 |
^ self copyFrom:1 to:(self size) |
5024 | 1312 |
]. |
1313 |
^ super shallowCopy |
|
1314 |
! |
|
1315 |
||
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1316 |
symbolFrom:start to:stop |
8908 | 1317 |
"make a symbol from the characters of the subcollection starting |
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1318 |
at index start, anInteger and ending at stop, anInteger. |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1319 |
This saves us garbage and character copying." |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1320 |
|
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1321 |
|sym| |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1322 |
|
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1323 |
%{ /* STACK:1024 */ |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1324 |
|
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1325 |
REGISTER unsigned char *srcp; |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1326 |
REGISTER unsigned char *endp; |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1327 |
REGISTER int count; |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1328 |
int len, index1, index2; |
4086 | 1329 |
unsigned char scratchBuffer[1024], savec; |
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1330 |
|
12466
353502b2baa9
__isByteArray() to __isByteArrayLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
12453
diff
changeset
|
1331 |
if (__isByteArrayLike(self) && __bothSmallInteger(start, stop)) { |
12879 | 1332 |
len = __byteArraySize(self); |
1333 |
index1 = __intVal(start); |
|
1334 |
index2 = __intVal(stop); |
|
1335 |
||
1336 |
if ((index1 <= index2) && (index1 > 0) && (index2 <= len)) { |
|
1337 |
count = index2 - index1 + 1; |
|
1338 |
srcp = __ByteArrayInstPtr(self)->ba_element + index1 - 1; |
|
1339 |
if (index2 < len) { |
|
1340 |
/* temporarily stuff in a '\0' */ |
|
1341 |
endp = srcp + count + 1; |
|
1342 |
savec = *endp; |
|
1343 |
*endp = '\0'; |
|
1344 |
sym = __MKSYMBOL(srcp, 0); |
|
1345 |
/* must refetch endp (in case of a GC */ |
|
1346 |
endp = __ByteArrayInstPtr(self)->ba_element + index1 + count; |
|
1347 |
*endp = savec; |
|
1348 |
} else { |
|
1349 |
/* not enough space for '\0', copy the bytes */ |
|
1350 |
if (count < sizeof(scratchBuffer)) { |
|
1351 |
bcopy(srcp, scratchBuffer, count); |
|
1352 |
scratchBuffer[count] = '\0'; |
|
1353 |
sym = __MKSYMBOL(scratchBuffer, 0); |
|
1354 |
} |
|
1355 |
} |
|
1356 |
} |
|
1357 |
if (sym != nil) |
|
1358 |
RETURN(sym); |
|
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1359 |
} |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1360 |
%}. |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1361 |
" |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1362 |
fall back in case of non-integer index or out-of-bound index |
4086 | 1363 |
or not enough stack-memory available; |
1364 |
may eventually lead to an out-of-bound signal raise |
|
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1365 |
" |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1366 |
^ (super copyFrom:start to:stop) asString asSymbol |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1367 |
|
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1368 |
|
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1369 |
" |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1370 |
'abcdefghijklmnop' symbolFrom:1 to:3 |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1371 |
'abcdefghijklmnop' symbolFrom:3 to:16 |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1372 |
'abcdefghijklmnop' symbolFrom:3 to:17 |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1373 |
" |
92 | 1374 |
! ! |
1375 |
||
4154 | 1376 |
!ByteArray methodsFor:'filling & replacing'! |
1 | 1377 |
|
155 | 1378 |
from:start to:stop put:aNumber |
1379 |
"fill part of the receiver with aNumber. |
|
1380 |
- reimplemented here for speed" |
|
1381 |
||
1382 |
%{ /* NOCONTEXT */ |
|
1383 |
||
1384 |
REGISTER unsigned char *dstp; |
|
1385 |
REGISTER int count, value; |
|
249 | 1386 |
int len, index1, index2; |
1387 |
OBJ cls; |
|
155 | 1388 |
|
249 | 1389 |
if (__isSmallInteger(aNumber) |
1002
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
1390 |
&& __bothSmallInteger(start, stop) |
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
1391 |
&& __isBytes(self)) { |
4969 | 1392 |
len = __byteArraySize(self); |
1393 |
index1 = __intVal(start); |
|
1394 |
index2 = __intVal(stop); |
|
1395 |
||
1396 |
dstp = __ByteArrayInstPtr(self)->ba_element + index1 - 1; |
|
1397 |
if ((cls = __qClass(self)) != @global(ByteArray)) { |
|
1398 |
int nInst; |
|
1399 |
||
1400 |
nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
1401 |
dstp += nInst; |
|
1402 |
len -= nInst; |
|
1403 |
} |
|
1404 |
||
1405 |
value = __intVal(aNumber); |
|
1406 |
if (((unsigned)value <= 0xFF) /* i.e. (value >= 0) && (value <= 255) */ |
|
8908 | 1407 |
&& (index1 <= index2) |
4969 | 1408 |
&& (index1 > 0)) { |
1409 |
if (index2 <= len) { |
|
1410 |
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
|
1411 |
|
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1412 |
#ifdef memset4 |
4969 | 1413 |
if (count > 20) { |
1414 |
/* fill unaligned part */ |
|
1415 |
while (((unsigned INT)dstp & 3) != 0) { |
|
1416 |
*dstp++ = value; |
|
1417 |
count--; |
|
1418 |
} |
|
1419 |
/* fill aligned part */ |
|
1420 |
{ |
|
1421 |
int n4 = count & ~3; |
|
1422 |
int v4, nW; |
|
1423 |
||
1424 |
v4 = (value << 8) | value; |
|
1425 |
v4 = (v4 << 16) | v4; |
|
1426 |
nW = n4>>2; |
|
1427 |
memset4(dstp, v4, nW); |
|
1428 |
count -= n4; |
|
1429 |
dstp += n4; |
|
1430 |
} |
|
1431 |
while (count--) { |
|
1432 |
*dstp++ = value; |
|
1433 |
} |
|
1434 |
RETURN (self); |
|
1435 |
} |
|
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1436 |
#endif /* memset4 */ |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1437 |
|
8908 | 1438 |
#if (__POINTER_SIZE__ == 8) |
4969 | 1439 |
{ |
1440 |
INT v8; |
|
1441 |
||
1442 |
v8 = (value << 8) | value; |
|
1443 |
v8 = (v8 << 16) | v8; |
|
1444 |
v8 = (v8 << 32) | v8; |
|
1445 |
||
1446 |
/* fill unaligned part */ |
|
1447 |
while ((count > 0) && (((unsigned INT)dstp & 3) != 0)) { |
|
1448 |
*dstp++ = value; |
|
1449 |
count--; |
|
1450 |
} |
|
1451 |
||
1452 |
if ((count >= 4) && (((unsigned INT)dstp & 7) != 0)) { |
|
1453 |
((unsigned int *)dstp)[0] = v8; |
|
1454 |
dstp += 4; |
|
1455 |
count -= 4; |
|
1456 |
} |
|
1457 |
||
1458 |
/* fill aligned part */ |
|
1459 |
while (count >= 8) { |
|
1460 |
((unsigned INT *)dstp)[0] = v8; |
|
1461 |
dstp += 8; |
|
1462 |
count -= 8; |
|
1463 |
} |
|
1464 |
||
1465 |
/* fill rest */ |
|
1466 |
if (count >= 4) { |
|
1467 |
((unsigned int *)dstp)[0] = v8; |
|
1468 |
dstp += 4; |
|
1469 |
count -= 4; |
|
1470 |
} |
|
1471 |
if (count >= 2) { |
|
1472 |
((unsigned short *)dstp)[0] = v8; |
|
1473 |
dstp += 2; |
|
1474 |
count -= 2; |
|
1475 |
} |
|
1476 |
if (count) { |
|
1477 |
*dstp = value; |
|
1478 |
} |
|
1479 |
RETURN (self); |
|
1480 |
} |
|
8908 | 1481 |
#endif /* 64bit */ |
4945 | 1482 |
|
155 | 1483 |
#ifdef FAST_MEMSET |
4969 | 1484 |
memset(dstp, value, count); |
155 | 1485 |
#else |
8919 | 1486 |
# ifdef __UNROLL_LOOPS__ |
4969 | 1487 |
while (count >= 8) { |
1488 |
dstp[0] = dstp[1] = dstp[2] = dstp[3] = |
|
1489 |
dstp[4] = dstp[5] = dstp[6] = dstp[7] = value; |
|
1490 |
dstp += 8; |
|
1491 |
count -= 8; |
|
1492 |
} |
|
8919 | 1493 |
# endif /* __UNROLL_LOOPS__ */ |
4969 | 1494 |
while (count--) { |
1495 |
*dstp++ = value; |
|
1496 |
} |
|
155 | 1497 |
#endif |
4969 | 1498 |
RETURN (self); |
1499 |
} |
|
1500 |
} |
|
155 | 1501 |
} |
1502 |
%}. |
|
1503 |
" |
|
1504 |
fall back in case of non-integer index or out-of-bound index/value; |
|
1505 |
will eventually lead to an out-of-bound signal raise |
|
1506 |
" |
|
1507 |
^ super from:start to:stop put:aNumber |
|
4945 | 1508 |
|
1509 |
" |
|
8908 | 1510 |
(ByteArray new:10) from:1 to:10 put:1 |
1511 |
(ByteArray new:20) from:10 to:20 put:1 |
|
1512 |
(ByteArray new:20) from:1 to:10 put:1 |
|
4945 | 1513 |
" |
1 | 1514 |
! ! |
1515 |
||
129 | 1516 |
!ByteArray methodsFor:'image manipulation support'! |
1 | 1517 |
|
8908 | 1518 |
bitAndBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart |
4919 | 1519 |
"replace bytes in the receiver with the result of a bitAnd operation. |
1520 |
Warning: this is a destructive operation - elements in the receiver are overwritten." |
|
1521 |
||
1522 |
^ self bitBlitBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart rule:#bitAnd: |
|
1523 |
||
1524 |
" |
|
1525 |
#[1 2 3 4 5 6 7 8] |
|
8908 | 1526 |
bitAndBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 |
4919 | 1527 |
#[1 2 3 4 5 6 7 8] |
4969 | 1528 |
bitAndBytesFrom:1 to:8 with:#[1 1 1 1 1 1 1 1] startingAt:1 |
4919 | 1529 |
" |
1530 |
||
1531 |
! |
|
1532 |
||
1533 |
bitBlitBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart rule:ruleSymbol |
|
1534 |
"perform a special case of an aligned bitBlit operation. |
|
8908 | 1535 |
Bytes in the receiver from dstStart to dstEnd are destructively replaced by the result |
4919 | 1536 |
of some logical operation, as specified by the ruleSymbol. |
1537 |
SourceBytes are fetched starting at sourceOffset. |
|
1538 |
Valid rule symbols are: |
|
16621 | 1539 |
#copy - trivial; same as replaceBytesFrom:to:with:startingAt: |
1540 |
#bitXor: - xoring; byte[dI] = byte[dI] bitXor:(srcByte[sI]) |
|
1541 |
#bitAnd: - anding; byte[dI] = byte[dI] bitAnd:(srcByte[sI]) |
|
1542 |
#bitOr: - oring; byte[dI] = byte[dI] bitOr:(srcByte[sI]) |
|
1543 |
#+ - adding; byte[dI] = (byte[dI] + (srcByte[sI])) mod: 256 |
|
1544 |
#- - subtract; byte[dI] = (byte[dI] - (srcByte[sI])) mod: 256 |
|
4919 | 1545 |
Warning: this is a destructive operation - elements in the receiver are overwritten. |
1546 |
" |
|
1547 |
||
1548 |
|srcIdx| |
|
1549 |
||
1550 |
%{ |
|
12466
353502b2baa9
__isByteArray() to __isByteArrayLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
12453
diff
changeset
|
1551 |
if ((__isByteArrayLike(sourceBytes)) |
4919 | 1552 |
&& (__qClass(self) == ByteArray) |
1553 |
&& __isSmallInteger(dstStart) |
|
1554 |
&& __isSmallInteger(dstEnd) |
|
1555 |
&& __isSmallInteger(sourceStart)) { |
|
16621 | 1556 |
unsigned char *srcP = __ByteArrayInstPtr(sourceBytes)->ba_element; |
1557 |
unsigned char *dstP = __ByteArrayInstPtr(self)->ba_element; |
|
1558 |
int srcLen = __byteArraySize(sourceBytes); |
|
1559 |
int dstLen = __byteArraySize(self); |
|
1560 |
int __srcStart = __intVal(sourceStart); |
|
1561 |
int __dstStart = __intVal(dstStart); |
|
1562 |
int count = __intVal(dstEnd) - __dstStart + 1; |
|
1563 |
||
1564 |
if ((__dstStart >= 1) |
|
1565 |
&& (__srcStart >= 1) |
|
1566 |
&& ((__dstStart + count - 1) <= dstLen) |
|
1567 |
&& ((__srcStart + count - 1) <= srcLen)) { |
|
1568 |
srcP += __srcStart - 1; |
|
1569 |
dstP += __dstStart - 1; |
|
5439 | 1570 |
|
1571 |
#define OP_LOOP_BYTES(OP) \ |
|
16621 | 1572 |
while (count >= 4) { \ |
1573 |
(dstP[0]) OP (srcP[0]); \ |
|
1574 |
(dstP[1]) OP (srcP[1]); \ |
|
1575 |
(dstP[2]) OP (srcP[2]); \ |
|
1576 |
(dstP[3]) OP (srcP[3]); \ |
|
1577 |
srcP += 4; \ |
|
1578 |
dstP += 4; \ |
|
1579 |
count -= 4; \ |
|
1580 |
} \ |
|
5439 | 1581 |
while (count > 0) { \ |
16621 | 1582 |
*dstP OP (*srcP); \ |
1583 |
srcP++; \ |
|
1584 |
dstP++; \ |
|
1585 |
count--; \ |
|
8908 | 1586 |
} |
4919 | 1587 |
|
1588 |
#define OP_LOOP(OP) \ |
|
5439 | 1589 |
while (count >= 16) { \ |
16621 | 1590 |
((unsigned int *)dstP)[0] OP (((unsigned int *)srcP)[0]); \ |
1591 |
((unsigned int *)dstP)[1] OP (((unsigned int *)srcP)[1]); \ |
|
1592 |
((unsigned int *)dstP)[2] OP (((unsigned int *)srcP)[2]); \ |
|
1593 |
((unsigned int *)dstP)[3] OP (((unsigned int *)srcP)[3]); \ |
|
1594 |
srcP += 16; \ |
|
1595 |
dstP += 16; \ |
|
1596 |
count -= 16; \ |
|
4919 | 1597 |
} \ |
5439 | 1598 |
while (count >= 4) { \ |
16621 | 1599 |
((unsigned int *)dstP)[0] OP (((unsigned int *)srcP)[0]); \ |
1600 |
srcP += 4; \ |
|
1601 |
dstP += 4; \ |
|
1602 |
count -= 4; \ |
|
4919 | 1603 |
} \ |
1604 |
while (count > 0) { \ |
|
16621 | 1605 |
*dstP OP (*srcP); \ |
1606 |
srcP++; \ |
|
1607 |
dstP++; \ |
|
1608 |
count--; \ |
|
8908 | 1609 |
} |
1610 |
||
1611 |
||
16621 | 1612 |
if (ruleSymbol == @symbol(bitXor:)) { |
1613 |
OP_LOOP( ^= ) |
|
1614 |
RETURN (self); |
|
1615 |
} |
|
1616 |
if (ruleSymbol == @symbol(bitXorNot:)) { |
|
1617 |
OP_LOOP( ^=~ ) |
|
1618 |
RETURN (self); |
|
1619 |
} |
|
1620 |
if (ruleSymbol == @symbol(bitAnd:)) { |
|
1621 |
OP_LOOP( &= ) |
|
1622 |
RETURN (self); |
|
1623 |
} |
|
1624 |
if (ruleSymbol == @symbol(bitAndNot:)) { |
|
1625 |
OP_LOOP( &=~ ) |
|
1626 |
RETURN (self); |
|
1627 |
} |
|
1628 |
if (ruleSymbol == @symbol(bitOr:)) { |
|
1629 |
OP_LOOP( |= ) |
|
1630 |
RETURN (self); |
|
1631 |
} |
|
1632 |
if (ruleSymbol == @symbol(bitOrNot:)) { |
|
1633 |
OP_LOOP( |=~ ) |
|
1634 |
RETURN (self); |
|
1635 |
} |
|
1636 |
if (ruleSymbol == @symbol(copy)) { |
|
1637 |
OP_LOOP( = ) |
|
1638 |
RETURN (self); |
|
1639 |
} |
|
1640 |
if (ruleSymbol == @symbol(copyNot)) { |
|
1641 |
OP_LOOP( =~ ) |
|
1642 |
RETURN (self); |
|
1643 |
} |
|
1644 |
if (ruleSymbol == @symbol(+)) { |
|
1645 |
OP_LOOP_BYTES( += ) |
|
1646 |
RETURN (self); |
|
1647 |
} |
|
1648 |
if (ruleSymbol == @symbol(-)) { |
|
1649 |
OP_LOOP_BYTES( -= ) |
|
1650 |
RETURN (self); |
|
1651 |
} |
|
1652 |
} |
|
4919 | 1653 |
} |
5549 | 1654 |
#undef OP_LOOP_BYTES |
1655 |
#undef OP_LOOP |
|
1656 |
||
4919 | 1657 |
%}. |
1658 |
ruleSymbol == #copy ifTrue:[ |
|
16621 | 1659 |
self replaceFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart. |
1660 |
^ self |
|
4919 | 1661 |
]. |
1662 |
||
1663 |
srcIdx := sourceStart. |
|
1664 |
dstStart to:dstEnd do:[:dstIdx | |
|
16621 | 1665 |
self at:dstIdx put:((self at:dstIdx) perform:ruleSymbol with:(sourceBytes at:srcIdx)). |
1666 |
srcIdx := srcIdx + 1. |
|
4919 | 1667 |
]. |
1668 |
||
1669 |
" |
|
1670 |
#[1 2 3 4 5 6 7 8] |
|
16621 | 1671 |
bitBlitBytesFrom:1 to:3 with:#[1 2 3 4 5 6 7 8] startingAt:1 rule:#bitXor: |
4919 | 1672 |
#[1 2 3 4 5 6 7 8] |
16621 | 1673 |
bitBlitBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 rule:#bitXor: |
4919 | 1674 |
#[1 2 3 4 5 6 7 8] |
16621 | 1675 |
bitBlitBytesFrom:1 to:8 with:#[1 1 1 1 1 1 1 1] startingAt:1 rule:#bitAnd: |
4919 | 1676 |
#[1 2 3 4 5 6 7 8] |
16621 | 1677 |
bitBlitBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 rule:#+ |
5439 | 1678 |
#[255 0 0 0 0 0 0 0] |
16621 | 1679 |
bitBlitBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 rule:#+ |
4919 | 1680 |
#[1 2 3 4 5 6 7 8] |
16621 | 1681 |
bitBlitBytesFrom:1 to:4 with:#[1 1 1 1 1 1 1 1] startingAt:1 rule:#+ |
4924
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
1682 |
#[1 2 3 4 5 6 7 8] |
16621 | 1683 |
bitBlitBytesFrom:1 to:4 with:#[1 1 1 1 2 2 2 2] startingAt:5 rule:#+ |
5439 | 1684 |
#[1 2 3 4 5 6 7 8] |
16621 | 1685 |
bitBlitBytesFrom:1 to:4 with:#[1 1 1 1 2 2 2 2] startingAt:5 rule:#copyNot |
4919 | 1686 |
|
1687 |
#[1 2 3 4 5 6 7 8] |
|
16621 | 1688 |
bitBlitBytesFrom:1 to:8 with:(1 to:8) startingAt:1 rule:#+ |
5439 | 1689 |
" |
1690 |
! |
|
1691 |
||
1692 |
bitBlitBytesFrom:dstStart to:dstEnd withConstant:sourceByte rule:ruleSymbol |
|
1693 |
"perform a special case of an aligned bitBlit operation. |
|
8908 | 1694 |
Bytes in the receiver from dstStart to dstEnd are destructively replaced by the result |
5439 | 1695 |
of some logical operation, as specified by the ruleSymbol. |
1696 |
Valid rule symbols are: |
|
16620 | 1697 |
#copy - trivial; same as from:to:put: |
1698 |
#bitXor: - xoring; byte[dI] = byte[dI] bitXor:sourceConst |
|
1699 |
#bitAnd: - anding; byte[dI] = byte[dI] bitAnd:sourceConst |
|
1700 |
#bitOr: - oring; byte[dI] = byte[dI] bitOr:sourceConst |
|
1701 |
#+ - adding; byte[dI] = (byte[dI] + sourceConst) mod: 256 |
|
1702 |
#- - subtract; byte[dI] = (byte[dI] - sourceConst) mod: 256 |
|
5439 | 1703 |
Warning: this is a destructive operation - elements in the receiver are overwritten. |
1704 |
" |
|
1705 |
||
1706 |
|srcIdx| |
|
1707 |
||
1708 |
%{ |
|
1709 |
if ((__qClass(self) == ByteArray) |
|
1710 |
&& __isSmallInteger(dstStart) |
|
1711 |
&& __isSmallInteger(dstEnd) |
|
1712 |
&& __isSmallInteger(sourceByte)) { |
|
16620 | 1713 |
unsigned char srcByte = __intVal(sourceByte); |
1714 |
unsigned srcWord; |
|
1715 |
unsigned char *dstP = __ByteArrayInstPtr(self)->ba_element; |
|
1716 |
int dstLen = __byteArraySize(self); |
|
1717 |
int __dstStart = __intVal(dstStart); |
|
1718 |
int count = __intVal(dstEnd) - __dstStart + 1; |
|
1719 |
||
1720 |
srcWord = (srcByte << 8) | srcByte; |
|
1721 |
srcWord = (srcWord << 16) | srcWord; |
|
1722 |
if ((__dstStart >= 1) |
|
1723 |
&& ((__dstStart + count - 1) <= dstLen)) { |
|
1724 |
dstP += __dstStart - 1; |
|
5439 | 1725 |
|
1726 |
#define OP_LOOP_BYTES(OP) \ |
|
16620 | 1727 |
while (count >= 4) { \ |
1728 |
dstP[0] OP srcByte; \ |
|
1729 |
dstP[1] OP srcByte; \ |
|
1730 |
dstP[2] OP srcByte; \ |
|
1731 |
dstP[3] OP srcByte; \ |
|
1732 |
dstP += 4; \ |
|
1733 |
count -= 4; \ |
|
1734 |
} \ |
|
5439 | 1735 |
while (count > 0) { \ |
16620 | 1736 |
*dstP OP srcByte; \ |
1737 |
dstP++; \ |
|
1738 |
count--; \ |
|
8908 | 1739 |
} |
5439 | 1740 |
|
16620 | 1741 |
#define OP_LOOP_INT32(OP) \ |
1742 |
while (count >= 16) { \ |
|
1743 |
((unsigned int *)dstP)[0] OP srcWord; \ |
|
1744 |
((unsigned int *)dstP)[1] OP srcWord; \ |
|
1745 |
((unsigned int *)dstP)[2] OP srcWord; \ |
|
1746 |
((unsigned int *)dstP)[3] OP srcWord; \ |
|
1747 |
dstP += 16; \ |
|
1748 |
count -= 16; \ |
|
1749 |
} \ |
|
1750 |
||
5439 | 1751 |
#define OP_LOOP(OP) \ |
16620 | 1752 |
OP_LOOP_INT32(OP) \ |
5439 | 1753 |
while (count >= 4) { \ |
16620 | 1754 |
((unsigned int *)dstP)[0] OP srcWord; \ |
1755 |
dstP += 4; \ |
|
1756 |
count -= 4; \ |
|
5439 | 1757 |
} \ |
1758 |
while (count > 0) { \ |
|
16620 | 1759 |
*dstP OP srcByte; \ |
1760 |
dstP++; \ |
|
1761 |
count--; \ |
|
8908 | 1762 |
} |
1763 |
||
1764 |
||
16620 | 1765 |
if (ruleSymbol == @symbol(bitXor:)) { |
1766 |
OP_LOOP( ^= ) |
|
1767 |
RETURN (self); |
|
1768 |
} |
|
1769 |
if (ruleSymbol == @symbol(bitXorNot:)) { |
|
1770 |
OP_LOOP( ^=~ ) |
|
1771 |
RETURN (self); |
|
1772 |
} |
|
1773 |
if (ruleSymbol == @symbol(bitAnd:)) { |
|
1774 |
OP_LOOP( &= ) |
|
1775 |
RETURN (self); |
|
1776 |
} |
|
1777 |
if (ruleSymbol == @symbol(bitAndNot:)) { |
|
1778 |
OP_LOOP( &=~ ) |
|
1779 |
RETURN (self); |
|
1780 |
} |
|
1781 |
if (ruleSymbol == @symbol(bitOr:)) { |
|
1782 |
OP_LOOP( |= ) |
|
1783 |
RETURN (self); |
|
1784 |
} |
|
1785 |
if (ruleSymbol == @symbol(bitOrNot:)) { |
|
1786 |
OP_LOOP( |=~ ) |
|
1787 |
RETURN (self); |
|
1788 |
} |
|
1789 |
if (ruleSymbol == @symbol(copy)) { |
|
1790 |
OP_LOOP( = ) |
|
1791 |
RETURN (self); |
|
1792 |
} |
|
1793 |
if (ruleSymbol == @symbol(copyNot)) { |
|
1794 |
OP_LOOP( =~ ) |
|
1795 |
RETURN (self); |
|
1796 |
} |
|
1797 |
if (ruleSymbol == @symbol(+)) { |
|
1798 |
OP_LOOP_BYTES( += ) |
|
1799 |
RETURN (self); |
|
1800 |
} |
|
1801 |
if (ruleSymbol == @symbol(-)) { |
|
1802 |
OP_LOOP_BYTES( -= ) |
|
1803 |
RETURN (self); |
|
1804 |
} |
|
1805 |
} |
|
5439 | 1806 |
} |
5549 | 1807 |
#undef OP_LOOP_BYTES |
1808 |
#undef OP_LOOP |
|
5439 | 1809 |
%}. |
1810 |
ruleSymbol == #copy ifTrue:[ |
|
16620 | 1811 |
self from:dstStart to:dstEnd put:sourceByte. |
1812 |
^ self |
|
5439 | 1813 |
]. |
1814 |
||
1815 |
dstStart to:dstEnd do:[:dstIdx | |
|
16620 | 1816 |
self at:dstIdx put:((self at:dstIdx) perform:ruleSymbol with:sourceByte). |
5439 | 1817 |
]. |
1818 |
||
1819 |
" |
|
1820 |
#[1 2 3 4 5 6 7 8] |
|
16620 | 1821 |
bitBlitBytesFrom:1 to:3 withConstant:1 rule:#bitXor: |
5439 | 1822 |
#[1 2 3 4 5 6 7 8] |
16620 | 1823 |
bitBlitBytesFrom:1 to:8 withConstant:1 rule:#bitXor: |
5439 | 1824 |
#[1 2 3 4 5 6 7 8] |
16620 | 1825 |
bitBlitBytesFrom:1 to:8 withConstant:1 rule:#bitAnd: |
5439 | 1826 |
#[1 2 3 4 5 6 7 8] |
16620 | 1827 |
bitBlitBytesFrom:1 to:8 withConstant:1 rule:#+ |
5439 | 1828 |
#[255 0 0 0 0 0 0 0] |
16620 | 1829 |
bitBlitBytesFrom:1 to:8 withConstant:1 rule:#+ |
5439 | 1830 |
#[1 2 3 4 5 6 7 8] |
16620 | 1831 |
bitBlitBytesFrom:1 to:4 withConstant:1 rule:#+ |
5439 | 1832 |
#[1 2 3 4 5 6 7 8] |
16620 | 1833 |
bitBlitBytesFrom:1 to:4 withConstant:1 rule:#- |
5439 | 1834 |
#[1 2 3 4 5 6 7 8] |
16620 | 1835 |
bitBlitBytesFrom:1 to:4 withConstant:1 rule:#copyNot |
5439 | 1836 |
|
1837 |
#[1 2 3 4 5 6 7 8] |
|
16620 | 1838 |
bitBlitBytesFrom:1 to:8 withConstant:1 rule:#+ |
4919 | 1839 |
" |
1840 |
! |
|
1841 |
||
8908 | 1842 |
bitOrBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart |
4919 | 1843 |
"replace bytes in the receiver with the result of a bitOr operation. |
1844 |
Warning: this is a destructive operation - elements in the receiver are overwritten." |
|
1845 |
||
1846 |
^ self bitBlitBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart rule:#bitOr: |
|
1847 |
||
1848 |
" |
|
1849 |
#[1 2 3 4 5 6 7 8] |
|
8908 | 1850 |
bitOrBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 |
4919 | 1851 |
#[1 2 3 4 5 6 7 8] |
4969 | 1852 |
bitOrBytesFrom:1 to:8 with:#[1 1 1 1 1 1 1 1] startingAt:1 |
4919 | 1853 |
" |
1854 |
||
1855 |
! |
|
1856 |
||
8908 | 1857 |
bitXorBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart |
4919 | 1858 |
"replace bytes in the receiver with the result of an bitXor operation. |
1859 |
Warning: this is a destructive operation - elements in the receiver are overwritten." |
|
1860 |
||
1861 |
^ self bitBlitBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart rule:#bitXor: |
|
1862 |
||
1863 |
" |
|
1864 |
#[1 2 3 4 5 6 7 8] |
|
4969 | 1865 |
bitXorBytesFrom:1 to:3 with:#[1 2 3 4 5 6 7 8] startingAt:1 |
4919 | 1866 |
#[1 2 3 4 5 6 7 8] |
8908 | 1867 |
bitXorBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 |
4919 | 1868 |
#[1 2 3 4 5 6 7 8] |
4969 | 1869 |
bitXorBytesFrom:1 to:8 with:#[1 1 1 1 1 1 1 1] startingAt:1 |
4919 | 1870 |
" |
1871 |
||
1872 |
! |
|
1873 |
||
2866 | 1874 |
compressPixels:nBitsPerPixel width:width height:height into:aByteArray mapping:aMapByteArray |
129 | 1875 |
"given the receiver with 8-bit pixels, compress them into aByteArray |
1876 |
with nBitsPerPixel-depth pixels. The width/height-arguments are needed |
|
1877 |
to allow for any padding. On the fly, the source bytes are translated |
|
1878 |
using aMapByteArray (if non-nil). |
|
1879 |
Notice that smalltalk indexing begins at 1; thus the map-index for a byte |
|
1880 |
value of n is found in map at:(n + 1). |
|
1881 |
Output bits are filled left-to right, i.e. the first byte in the input |
|
1882 |
corresponds to the high bit(s) if the first byte in the input. |
|
8908 | 1883 |
This method can be used to convert 8-bit image data to mono, 2-bit and 4-bit |
129 | 1884 |
bitmaps. |
1885 |
It can also be used to compress byte-arrays into bitArrays." |
|
1886 |
||
1887 |
%{ /* NOCONTEXT */ |
|
1888 |
||
1889 |
REGISTER unsigned char *src, *dst; |
|
1890 |
REGISTER int wrun; |
|
1891 |
unsigned char *dstNext; |
|
1472
abaa73dc0e9a
check for destination overrun
Claus Gittinger <cg@exept.de>
parents:
1459
diff
changeset
|
1892 |
unsigned char *dstEnd; |
129 | 1893 |
int bytesPerRow, mask, shift0, shift; |
1894 |
int w, h, hrun; |
|
1895 |
int srcBytes, dstBytes; |
|
1896 |
int bitsPerPixel; |
|
1897 |
int bits; |
|
1898 |
int ncells; |
|
1899 |
unsigned char *map; |
|
1900 |
||
8908 | 1901 |
if ((__qClass(self) == @global(ByteArray)) |
1133 | 1902 |
&& (__qClass(aByteArray) == @global(ByteArray)) |
249 | 1903 |
&& __isSmallInteger(nBitsPerPixel) |
1904 |
&& __bothSmallInteger(height, width)) { |
|
2915 | 1905 |
if ((aMapByteArray != nil) |
1906 |
&& (__Class(aMapByteArray) == @global(ByteArray))) { |
|
1907 |
map = __ByteArrayInstPtr(aMapByteArray)->ba_element; |
|
1908 |
} else { |
|
1909 |
map = (unsigned char *)0; |
|
1910 |
} |
|
129 | 1911 |
|
2915 | 1912 |
bitsPerPixel = __intVal(nBitsPerPixel); |
1913 |
w = __intVal(width); |
|
1914 |
h = __intVal(height); |
|
1915 |
src = __ByteArrayInstPtr(self)->ba_element; |
|
1916 |
dst = __ByteArrayInstPtr(aByteArray)->ba_element; |
|
1917 |
dstEnd = dst + __byteArraySize(aByteArray); |
|
1918 |
switch (bitsPerPixel) { |
|
1919 |
case 1: |
|
1920 |
mask = 0x01; |
|
1921 |
break; |
|
1922 |
case 2: |
|
1923 |
mask = 0x03; |
|
1924 |
break; |
|
1925 |
case 4: |
|
1926 |
mask = 0x0F; |
|
1927 |
break; |
|
1928 |
case 8: |
|
1929 |
mask = 0xFF; |
|
1930 |
break; |
|
1931 |
default: |
|
13703 | 1932 |
console_printf("invalid depth in compressPixels\n"); |
2915 | 1933 |
goto fail; |
1934 |
} |
|
1935 |
if (map) { |
|
1936 |
/* |
|
1937 |
* if a map is present, it must have entries for |
|
1938 |
* all possible byte-values (i.e. its size must be >= 256) |
|
1939 |
*/ |
|
1940 |
if ((__qSize(aMapByteArray) - OHDR_SIZE) < 256) { |
|
13703 | 1941 |
console_printf("invalid map in compressPixels\n"); |
2915 | 1942 |
goto fail; |
1943 |
} |
|
1944 |
} |
|
129 | 1945 |
|
2915 | 1946 |
bytesPerRow = (w * bitsPerPixel + 7) / 8; |
1947 |
dstBytes = bytesPerRow * h; |
|
1948 |
srcBytes = w * h; |
|
2866 | 1949 |
|
2915 | 1950 |
if ((__byteArraySize(self) >= srcBytes) |
1951 |
&& (__byteArraySize(aByteArray) >= dstBytes)) { |
|
1952 |
for (hrun=h; hrun; hrun--) { |
|
1953 |
dstNext = dst + bytesPerRow; |
|
1954 |
bits = 0; shift = 8; |
|
1955 |
if (map) { |
|
1956 |
for (wrun=w; wrun; wrun--) { |
|
1957 |
bits = (bits << bitsPerPixel) | (map[*src++] & mask); |
|
1958 |
shift -= bitsPerPixel; |
|
1959 |
if (shift == 0) { |
|
1960 |
if (dst == dstEnd) goto fail; |
|
1961 |
*dst++ = bits; |
|
1962 |
bits = 0; shift = 8; |
|
1963 |
} |
|
1964 |
} |
|
1965 |
} else { |
|
1966 |
for (wrun=w; wrun; wrun--) { |
|
1967 |
bits = (bits << bitsPerPixel) | (*src++ & mask); |
|
1968 |
shift -= bitsPerPixel; |
|
1969 |
if (shift == 0) { |
|
1970 |
if (dst == dstEnd) goto fail; |
|
1971 |
*dst++ = bits; |
|
1972 |
bits = 0; shift = 8; |
|
1973 |
} |
|
1974 |
} |
|
1975 |
} |
|
1976 |
if (shift != 8) { |
|
1977 |
if (dst == dstEnd) goto fail; |
|
1978 |
*dst = bits; |
|
1979 |
} |
|
1980 |
dst = dstNext; |
|
1981 |
} |
|
1982 |
RETURN ( self ); |
|
1983 |
} |
|
129 | 1984 |
} |
1985 |
fail: ; |
|
2866 | 1986 |
%}. |
129 | 1987 |
self primitiveFailed |
1988 |
||
1989 |
"Example1: |
|
1990 |
compress 1 byte-per-pixel bitmap to 1-bit-per-pixel bitmap |
|
1991 |
" |
|
1992 |
" |
|
1993 |
|inBits outBits| |
|
1994 |
||
1995 |
inBits := #[0 0 0 0 1 1 1 1 |
|
2915 | 1996 |
0 0 1 1 0 0 1 1 |
1997 |
0 1 0 1 0 1 0 1 |
|
1998 |
1 1 1 1 0 0 0 0]. |
|
129 | 1999 |
outBits := ByteArray new:4. |
2000 |
inBits compressPixels:1 width:8 height:4 |
|
2915 | 2001 |
into:outBits mapping:nil. |
129 | 2002 |
outBits inspect |
2003 |
" |
|
2004 |
||
2005 |
"Example2: |
|
2006 |
compress byte-array into a bitArray, translating 99 to 0-bits, |
|
2007 |
and 176 to 1-bits. (just a stupid example) |
|
2008 |
" |
|
2009 |
" |
|
2010 |
|inBits outBits map| |
|
2011 |
||
2012 |
inBits := #[176 176 176 176 99 99 99 99 176 176 99 99 176 99 176 99]. |
|
2013 |
map := ByteArray new:256. |
|
2014 |
map at:176+1 put:1. |
|
2015 |
||
2016 |
outBits := ByteArray new:2. |
|
8908 | 2017 |
inBits compressPixels:1 width:16 height:1 |
2915 | 2018 |
into:outBits mapping:map. |
129 | 2019 |
outBits inspect |
2020 |
" |
|
2021 |
||
2022 |
"Example3: |
|
2023 |
compress byte-array into a bitArray, translating everything below 128 to 0-bits, |
|
2024 |
and 128 to 255 to 1-bits.99 to 0-bits (another stupid example) |
|
2025 |
" |
|
2026 |
" |
|
2027 |
|inBits outBits map| |
|
2028 |
||
2029 |
inBits := #[176 176 176 176 99 99 99 99 176 176 99 99 176 99 176 99]. |
|
2030 |
map := ByteArray new:256. |
|
2031 |
map atAll:(128+1 to:255+1) put:1. |
|
2032 |
||
2033 |
outBits := ByteArray new:2. |
|
8908 | 2034 |
inBits compressPixels:1 width:16 height:1 |
2915 | 2035 |
into:outBits mapping:map. |
129 | 2036 |
outBits inspect |
2037 |
" |
|
563 | 2038 |
! |
2039 |
||
2866 | 2040 |
expandPixels:nBitsPerPixel width:width height:height into:aByteArray mapping:aMapByteArray |
563 | 2041 |
"given the receiver with nBitsPerPixel-depth pixels, expand them into |
2042 |
aByteArray with 8-bit pixels. The width/height-arguments are needed |
|
2043 |
to skip any padded src-bits. On the fly, the destination pixels |
|
2044 |
are translated using aMapByteArray (if non-nil). |
|
2045 |
Input bits are read left-to right, i.e. the first byte in the output |
|
2046 |
corresponds to the high bit(s) in the inputs first byte. |
|
2047 |
This is used to display mono, 2-bit and 4-bit bitmaps on grey-scale/color |
|
2048 |
machines. With nBitsPerPixel==8, this is a translate operation. |
|
2049 |
Notice that smalltalk indexing begins at 1; thus the map-index for a byte |
|
2050 |
value of n is found in map at:(n + 1). |
|
2051 |
It can also be used to expand bit-arrays into byteArrays. |
|
2052 |
This method is specialized for ByteArray arguments - it will not handle |
|
2053 |
anything else." |
|
2054 |
||
2055 |
%{ /* NOCONTEXT */ |
|
2056 |
||
2057 |
REGISTER unsigned char *src, *dst; |
|
2058 |
REGISTER int wrun; |
|
2059 |
unsigned char *srcNext; |
|
2060 |
int bytesPerRow, mask, shift0, shift; |
|
2061 |
int w, h, hrun; |
|
2062 |
int srcBytes, dstBytes; |
|
2063 |
int bitsPerPixel; |
|
2064 |
int bits; |
|
2065 |
int ncells; |
|
2066 |
unsigned char *map; |
|
2067 |
||
8908 | 2068 |
if ((__qClass(self) == @global(ByteArray)) |
1133 | 2069 |
&& (__qClass(aByteArray) == @global(ByteArray)) |
563 | 2070 |
&& __isSmallInteger(nBitsPerPixel) |
2071 |
&& __bothSmallInteger(height, width)) { |
|
8908 | 2072 |
if ((aMapByteArray != nil) |
2073 |
&& (__Class(aMapByteArray) == @global(ByteArray))) { |
|
2074 |
map = __ByteArrayInstPtr(aMapByteArray)->ba_element; |
|
2075 |
} else { |
|
2076 |
map = (unsigned char *)0; |
|
2077 |
} |
|
2078 |
||
2079 |
bitsPerPixel = __intVal(nBitsPerPixel); |
|
2080 |
w = __intVal(width); |
|
2081 |
h = __intVal(height); |
|
2082 |
src = __ByteArrayInstPtr(self)->ba_element; |
|
2083 |
dst = __ByteArrayInstPtr(aByteArray)->ba_element; |
|
2084 |
switch (bitsPerPixel) { |
|
2085 |
case 1: |
|
2086 |
mask = 0x01; |
|
2087 |
break; |
|
2088 |
case 2: |
|
2089 |
mask = 0x03; |
|
2090 |
break; |
|
2091 |
case 4: |
|
2092 |
mask = 0x0F; |
|
2093 |
break; |
|
2094 |
case 8: |
|
2095 |
mask = 0xFF; |
|
2096 |
break; |
|
2097 |
default: |
|
13703 | 2098 |
console_printf("expandPixels: invalid depth\n"); |
8908 | 2099 |
goto fail; |
2100 |
} |
|
2101 |
ncells = mask + 1; |
|
2102 |
if (map) { |
|
2103 |
/* |
|
2104 |
* if a map is present, it must have the correct size |
|
2105 |
* (i.e. 2 raisedTo:nBitsPerPixel) |
|
2106 |
*/ |
|
2107 |
if ((__qSize(aMapByteArray) - OHDR_SIZE) < ncells) { |
|
13703 | 2108 |
console_printf("expandPixels: invalid map\n"); |
8908 | 2109 |
goto fail; |
2110 |
} |
|
2111 |
} |
|
2112 |
||
2113 |
bytesPerRow = (w * bitsPerPixel + 7) / 8; |
|
2114 |
shift0 = 8 - bitsPerPixel; |
|
2115 |
srcBytes = bytesPerRow * h; |
|
2116 |
dstBytes = w * h; |
|
2117 |
||
2118 |
if ((__byteArraySize(self) >= srcBytes) |
|
2119 |
&& (__byteArraySize(aByteArray) >= dstBytes)) { |
|
2120 |
for (hrun=h; hrun; hrun--) { |
|
2121 |
srcNext = src + bytesPerRow; |
|
2122 |
shift = shift0; |
|
2123 |
if (map) { |
|
2124 |
if (shift0 == 0) { |
|
2125 |
/* translate only */ |
|
2126 |
for (wrun=w; wrun; wrun--) { |
|
2127 |
bits = *src++; |
|
2128 |
*dst++ = map[bits]; |
|
2129 |
} |
|
2130 |
} else { |
|
2131 |
for (wrun=w; wrun; wrun--) { |
|
2132 |
if (shift == shift0) { |
|
2133 |
bits = *src++; |
|
2134 |
} |
|
2135 |
*dst++ = map[(bits >> shift) & mask]; |
|
2136 |
shift -= bitsPerPixel; |
|
2137 |
if (shift < 0) { |
|
2138 |
shift = shift0; |
|
2139 |
} |
|
2140 |
} |
|
2141 |
} |
|
2142 |
} else { |
|
2143 |
for (wrun=w; wrun; wrun--) { |
|
2144 |
if (shift == shift0) { |
|
2145 |
bits = *src++; |
|
2146 |
} |
|
2147 |
*dst++ = (bits >> shift) & mask; |
|
2148 |
shift -= bitsPerPixel; |
|
2149 |
if (shift < 0) { |
|
2150 |
shift = shift0; |
|
2151 |
} |
|
2152 |
} |
|
2153 |
} |
|
2154 |
src = srcNext; |
|
2155 |
} |
|
2156 |
RETURN ( self ); |
|
2157 |
} |
|
13703 | 2158 |
console_printf("expandPixels: buffer size: self:%d expect at least:%d\n", |
8908 | 2159 |
__byteArraySize(self), srcBytes); |
13703 | 2160 |
console_printf("expandPixels: buffer size: arg:%d expect at least:%d\n", |
8908 | 2161 |
__byteArraySize(aByteArray), dstBytes); |
563 | 2162 |
} |
13703 | 2163 |
console_printf("expandPixels: invalid args\n"); |
1527
af119b16aa16
better handle the translate-only case in #expandPixels:
ca
parents:
1505
diff
changeset
|
2164 |
|
563 | 2165 |
fail: ; |
2866 | 2166 |
%}. |
563 | 2167 |
self primitiveFailed |
2168 |
||
2169 |
"Example1: |
|
2170 |
expand 1-bit-per-pixel bitmap into a 1byte-per-pixel byteArray |
|
2171 |
" |
|
2172 |
" |
|
2173 |
|inBits outBits| |
|
2174 |
||
8908 | 2175 |
inBits := #[2r11110000 |
2176 |
2r11001100 |
|
2177 |
2r01010101 |
|
2178 |
2r00001111]. |
|
563 | 2179 |
outBits := ByteArray new:(8*4). |
2180 |
inBits expandPixels:1 width:8 height:4 |
|
8908 | 2181 |
into:outBits mapping:nil. |
563 | 2182 |
outBits inspect |
2183 |
" |
|
2184 |
||
2185 |
"Example2: |
|
2186 |
expand bit-array into a byteArray, translating 0-bits to 99, |
|
2187 |
1-bits to 176. (just a stupid example) |
|
2188 |
" |
|
2189 |
" |
|
2190 |
|inBits outBits| |
|
2191 |
||
2192 |
inBits := #[2r11110000 2r11001100]. |
|
2193 |
outBits := ByteArray new:16. |
|
8908 | 2194 |
inBits expandPixels:1 width:16 height:1 |
2195 |
into:outBits mapping:#[99 176]. |
|
563 | 2196 |
outBits inspect |
2197 |
" |
|
4978 | 2198 |
|
2199 |
"This Can also be used to extract nibbles ..." |
|
2200 |
" |
|
2201 |
|inBits outBits| |
|
2202 |
||
8908 | 2203 |
inBits := #[ 16r12 16r34 16r56 16r78 16r9A 16rBC 16rDE 16rF0 ]. |
4978 | 2204 |
outBits := ByteArray new:(inBits size * 2). |
2205 |
inBits expandPixels:4 width:outBits size height:1 into:outBits mapping:nil. |
|
2206 |
outBits inspect |
|
2207 |
" |
|
2208 |
||
563 | 2209 |
! |
2210 |
||
2211 |
invert |
|
2212 |
"invert all bytes - used with image manipulations |
|
2213 |
written as a primitive for speed. |
|
2214 |
Q: is this really needed ?" |
|
2215 |
||
2216 |
%{ /* NOCONTEXT */ |
|
2217 |
REGISTER unsigned char *dst; |
|
2218 |
REGISTER unsigned long *ldst; |
|
2219 |
REGISTER int cnt; |
|
2220 |
||
1133 | 2221 |
if (__qClass(self) == @global(ByteArray)) { |
16621 | 2222 |
cnt = __byteArraySize(self); |
2223 |
dst = __ByteArrayInstPtr(self)->ba_element; |
|
2224 |
if (((INT)dst & (sizeof(long)-1)) == 0) { // aligned |
|
2225 |
ldst = (unsigned long *)dst; |
|
2226 |
while (cnt >= (sizeof(long))*4) { |
|
2227 |
ldst[0] = ~(ldst[0]); |
|
2228 |
ldst[1] = ~(ldst[1]); |
|
2229 |
ldst[2] = ~(ldst[2]); |
|
2230 |
ldst[3] = ~(ldst[3]); |
|
2231 |
ldst += 4; |
|
2232 |
cnt -= (sizeof(long))*4; |
|
2233 |
} |
|
2234 |
while (cnt >= sizeof(long)) { |
|
2235 |
*ldst = ~(*ldst); |
|
2236 |
ldst++; |
|
2237 |
cnt -= sizeof(long); |
|
2238 |
} |
|
2239 |
dst = (unsigned char *)ldst; |
|
2240 |
} |
|
2241 |
while (cnt--) { |
|
2242 |
*dst = ~(*dst); |
|
2243 |
dst++; |
|
2244 |
} |
|
2245 |
RETURN ( self ); |
|
563 | 2246 |
} |
4927 | 2247 |
%}. |
5439 | 2248 |
self bitBlitBytesFrom:1 to:self size withConstant:16rFF rule:#bitXor: |
4927 | 2249 |
|
2250 |
" |
|
2251 |
#[1 2 3 4 5 6 7 8 9 10] copy invert |
|
8908 | 2252 |
#[1 2 3 4 5 6 7 8 9 10] copy |
16621 | 2253 |
bitBlitBytesFrom:1 to:10 withConstant:16rFF rule:#bitXor: |
2254 |
||
2255 |
|l| |
|
2256 |
l := ByteArray fromHexString:'0102030405060708090a0b0c0d0e0f1112131415161718191a1b1c1d1e1f'. |
|
2257 |
Time millisecondsToRun:[ |
|
2258 |
1000000 timesRepeat:[ l invert ]. |
|
2259 |
] |
|
4927 | 2260 |
" |
563 | 2261 |
! |
2262 |
||
2263 |
reverse |
|
8908 | 2264 |
"reverse the order of my elements inplace - |
7338 | 2265 |
WARNING: this is a destructive operation, which modifies the receiver. |
12879 | 2266 |
Please use reversed (with a d) for a functional version. |
7338 | 2267 |
Written as a primitive for speed on image manipulations (mirror)" |
563 | 2268 |
|
2269 |
%{ /* NOCONTEXT */ |
|
2270 |
||
2271 |
REGISTER unsigned char *p1, *p2; |
|
2272 |
REGISTER int cnt; |
|
2273 |
REGISTER unsigned t; |
|
2274 |
OBJ cls; |
|
2275 |
||
1133 | 2276 |
if (__qClass(self) == @global(ByteArray)) { |
12879 | 2277 |
cnt = __byteArraySize(self); |
2278 |
p1 = __ByteArrayInstPtr(self)->ba_element; |
|
2279 |
p2 = p1 + cnt - 1; |
|
4924
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2280 |
|
4927 | 2281 |
#if defined(__BSWAP) |
12879 | 2282 |
/* |
2283 |
* can we use the bswap instruction ? |
|
2284 |
* notice - not all CPUs have it (the HAS_BSWAP checks this). |
|
2285 |
*/ |
|
2286 |
if (__HAS_BSWAP() |
|
2287 |
&& ((cnt & 3) == 0)) { |
|
2288 |
unsigned int *ip1, *ip2; |
|
2289 |
||
2290 |
ip1 = (unsigned int *)p1; |
|
2291 |
ip2 = (unsigned int *)(p2 - 3); |
|
2292 |
||
2293 |
ip2 -= 7; |
|
2294 |
while (ip1 <= ip2) { |
|
2295 |
int t1, t2; |
|
2296 |
||
2297 |
t1 = ip1[0]; |
|
2298 |
t2 = ip2[7]; |
|
2299 |
ip2[7] = __BSWAP(t1); |
|
2300 |
ip1[0] = __BSWAP(t2); |
|
2301 |
||
2302 |
t1 = ip1[1]; |
|
2303 |
t2 = ip2[6]; |
|
2304 |
ip2[6] = __BSWAP(t1); |
|
2305 |
ip1[1] = __BSWAP(t2); |
|
2306 |
||
2307 |
t1 = ip1[2]; |
|
2308 |
t2 = ip2[5]; |
|
2309 |
ip2[5] = __BSWAP(t1); |
|
2310 |
ip1[2] = __BSWAP(t2); |
|
2311 |
||
2312 |
t1 = ip1[3]; |
|
2313 |
t2 = ip2[4]; |
|
2314 |
ip2[4] = __BSWAP(t1); |
|
2315 |
ip1[3] = __BSWAP(t2); |
|
2316 |
||
2317 |
ip1 += 4; |
|
2318 |
ip2 -= 4; |
|
2319 |
} |
|
2320 |
ip2 += 7; |
|
2321 |
||
2322 |
while (ip1 < ip2) { |
|
2323 |
int t; |
|
2324 |
||
2325 |
t = __BSWAP(*ip1); |
|
2326 |
*ip1++ = __BSWAP(*ip2); |
|
2327 |
*ip2-- = t; |
|
2328 |
} |
|
2329 |
||
2330 |
if (ip1 == ip2) { |
|
2331 |
int t; |
|
2332 |
t = *ip1; |
|
2333 |
t = __BSWAP(t); |
|
2334 |
*ip1 = t; |
|
2335 |
} |
|
2336 |
RETURN ( self ); |
|
2337 |
} |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
2338 |
#endif /* __i386__ && __GNUC__ */ |
4927 | 2339 |
|
12879 | 2340 |
p2 -= 7; |
2341 |
while (p1 <= p2) { |
|
2342 |
t = p1[0]; |
|
2343 |
p1[0] = p2[7]; |
|
2344 |
p2[7] = t; |
|
2345 |
||
2346 |
t = p1[1]; |
|
2347 |
p1[1] = p2[6]; |
|
2348 |
p2[6] = t; |
|
2349 |
||
2350 |
t = p1[2]; |
|
2351 |
p1[2] = p2[5]; |
|
2352 |
p2[5] = t; |
|
2353 |
||
2354 |
t = p1[3]; |
|
2355 |
p1[3] = p2[4]; |
|
2356 |
p2[4] = t; |
|
2357 |
||
2358 |
p1 += 4; |
|
2359 |
p2 -= 4; |
|
2360 |
} |
|
2361 |
p2 += 7; |
|
2362 |
||
2363 |
while (p1 < p2) { |
|
2364 |
t = *p1; |
|
2365 |
*p1++ = *p2; |
|
2366 |
*p2-- = t; |
|
2367 |
} |
|
2368 |
RETURN ( self ); |
|
563 | 2369 |
} |
2370 |
%}. |
|
2371 |
^ super reverse |
|
2585 | 2372 |
|
2373 |
" |
|
8908 | 2374 |
#[1 2 3 4 5] reverse |
2375 |
#[1 2 3 4] reverse |
|
2376 |
#[1 2 3 4 5] reverse |
|
2377 |
#[1 2 3 4 5 6] reverse |
|
2378 |
#[1 2 3 4 5 6 7] reverse |
|
2379 |
#[1 2 3 4 5 6 7 8] reverse |
|
2380 |
#[1 2 3 4 5 6 7 8 9] reverse |
|
2381 |
#[1 2 3 4 5 6 7 8 9 10] reverse |
|
2382 |
#[1 2 3 4 5 6 7 8 9 10 11 12] reverse |
|
2383 |
#[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16] reverse |
|
2384 |
#[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20] reverse |
|
2385 |
(1 to:255) asByteArray reverse |
|
4924
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2386 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2387 |
1 to:1024 do:[:i| |
12879 | 2388 |
|bytes test rBytes| |
2389 |
||
2390 |
bytes := ((1 to:i) asArray collect:[:i | i bitAnd:255]) asByteArray. |
|
2391 |
test := ((i to:1 by:-1) asArray collect:[:i | i bitAnd:255]) asByteArray. |
|
2392 |
rBytes := bytes copy. |
|
2393 |
rBytes reverse ~= test ifTrue:[ |
|
2394 |
self halt |
|
2395 |
]. |
|
2396 |
rBytes := bytes copy. |
|
2397 |
rBytes reverse reverse ~= bytes ifTrue:[ |
|
2398 |
self halt |
|
2399 |
] |
|
4924
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2400 |
]. |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2401 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2402 |
Time millisecondsToRun:[ |
12879 | 2403 |
10000000 timesRepeat:[ |
2404 |
#[1 2 3 4 5 6 7 8] reverse |
|
2405 |
] |
|
8908 | 2406 |
] |
4924
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2407 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2408 |
|b| |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2409 |
b := (0 to:255) asByteArray. |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2410 |
Time millisecondsToRun:[ |
12879 | 2411 |
10000000 timesRepeat:[ |
2412 |
b reverse |
|
2413 |
] |
|
8908 | 2414 |
] |
2585 | 2415 |
" |
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2416 |
! |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2417 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2418 |
swapBytes |
8908 | 2419 |
"swap bytes inplace - |
15274 | 2420 |
Expects that the receiver has an even number of bytes; |
2421 |
if not, only the pairs excluding the last byte are swapped. |
|
2422 |
written as a primitive for speed on image grabbing (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
|
2423 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2424 |
%{ /* NOCONTEXT */ |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2425 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2426 |
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
|
2427 |
REGISTER int cnt; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2428 |
REGISTER unsigned t; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2429 |
|
1133 | 2430 |
if (__qClass(self) == @global(ByteArray)) { |
15274 | 2431 |
cnt = __byteArraySize(self); |
2432 |
cnt = cnt & ~1; /* make it even */ |
|
2433 |
p = __ByteArrayInstPtr(self)->ba_element; |
|
2434 |
||
2435 |
while (cnt >= sizeof(INT)) { |
|
2436 |
unsigned INT i = ((unsigned INT *)p)[0]; |
|
2437 |
||
2438 |
#if __POINTER_SIZE__ == 8 |
|
2439 |
i = ((i>>8) & 0x00FF00FF00FF00FF) | ((i & 0x00FF00FF00FF00FF) << 8); |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
2440 |
#else |
15274 | 2441 |
i = ((i>>8) & 0x00FF00FF) | ((i & 0x00FF00FF) << 8); |
2442 |
#endif /* __POINTER_SIZE__ */ |
|
2443 |
((unsigned INT *)p)[0] = i; |
|
2444 |
p += sizeof(INT); |
|
2445 |
cnt -= sizeof(INT); |
|
2446 |
} |
|
2447 |
while (cnt > 0) { |
|
2448 |
unsigned short s; |
|
2449 |
||
2450 |
s = ((unsigned short *)p)[0]; |
|
2451 |
s = (s >> 8) | (s << 8); |
|
2452 |
((unsigned short *)p)[0] = s; |
|
2453 |
p += 2; |
|
2454 |
cnt -= 2; |
|
2455 |
} |
|
2456 |
RETURN ( self ); |
|
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2457 |
} |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2458 |
%}. |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2459 |
^ 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
|
2460 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2461 |
" |
15274 | 2462 |
#[1 2 3 4 5 6 7 8 9 10] copy swapBytes -> #[2 1 4 3 6 5 8 7 10 9] |
2463 |
#[1 2 3 4 5 6 7 8 9 10 11] copy swapBytes -> #[2 1 4 3 6 5 8 7 10 9 11] |
|
2464 |
#[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18] copy swapBytes |
|
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2465 |
" |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2466 |
! |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2467 |
|
4983 | 2468 |
swapIndex:i1 and:i2 |
2469 |
"spap the bytes with i1 and i2" |
|
2470 |
||
2471 |
%{ /* NOCONTEXT */ |
|
2472 |
||
2473 |
REGISTER unsigned char *p; |
|
2474 |
unsigned int __i1, __i2; |
|
15330 | 2475 |
int sz; |
4983 | 2476 |
unsigned int t; |
2477 |
||
2478 |
if (__qClass(self) == @global(ByteArray) && __bothSmallInteger(i1, i2)) { |
|
15330 | 2479 |
__i1 = __intVal(i1) - 1; |
2480 |
__i2 = __intVal(i2) - 1; |
|
2481 |
sz = __byteArraySize(self); |
|
2482 |
p = __ByteArrayInstPtr(self)->ba_element; |
|
2483 |
if (__i1 < sz && __i2 < sz) { |
|
2484 |
t = p[__i1]; |
|
2485 |
p[__i1] = p[__i2]; |
|
2486 |
p[__i2] = t; |
|
2487 |
} |
|
2488 |
RETURN ( self ); |
|
4983 | 2489 |
} |
2490 |
%}. |
|
11265 | 2491 |
^ super swapIndex:i1 and:i2 "/ rubbish - there is no one currently |
4983 | 2492 |
|
2493 |
" |
|
8908 | 2494 |
#[1 2 3 4 5 6 7 8 9 10] copy swapIndex:1 and:10 |
2495 |
#[1 2 3 4 5 6 7 8 9 10 11] copy swapIndex:5 and:6 |
|
4983 | 2496 |
" |
2497 |
! |
|
2498 |
||
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2499 |
swapLongs |
8908 | 2500 |
"swap long bytes inplace |
4927 | 2501 |
- any partial longs at the end are not swapped." |
2502 |
||
2503 |
self swapLongsFrom:1 to:self size |
|
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2504 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2505 |
" |
8908 | 2506 |
#[1 2 3 4 5 6 7 8 9] copy swapLongs |
2507 |
#[1 2 3 4 5 6 7 8 9 10] copy swapLongs |
|
2508 |
#[1 2 3 4 5 6 7 8 9 10 11] copy swapLongs |
|
2509 |
#[1 2 3 4 5 6 7 8 9 10 11 12] copy swapLongs |
|
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2510 |
" |
4365 | 2511 |
! |
2512 |
||
2513 |
swapLongsFrom:startIndex to:endIndex |
|
8908 | 2514 |
"swap longs inplace |
2515 |
- any partial longs at the end are not swapped. |
|
4367
3d481db099ec
oops - swapLongsFrom:to: was wrong
Claus Gittinger <cg@exept.de>
parents:
4365
diff
changeset
|
2516 |
Swapping is from startIndex to (exclusiv) endIndex; |
3d481db099ec
oops - swapLongsFrom:to: was wrong
Claus Gittinger <cg@exept.de>
parents:
4365
diff
changeset
|
2517 |
indexing starts at 1." |
4365 | 2518 |
|
2519 |
%{ /* NOCONTEXT */ |
|
2520 |
||
2521 |
REGISTER unsigned char *p; |
|
2522 |
REGISTER int limit; |
|
2523 |
REGISTER unsigned t; |
|
2524 |
||
2525 |
if ((__qClass(self) == @global(ByteArray)) |
|
2526 |
&& __isSmallInteger(startIndex) |
|
2527 |
&& __isSmallInteger(endIndex)) { |
|
4969 | 2528 |
int __idx = __intVal(startIndex); |
2529 |
int __endIdx = __intVal(endIndex); |
|
2530 |
||
2531 |
limit = __byteArraySize(self); |
|
2532 |
if (__endIdx < limit) { |
|
2533 |
limit = __endIdx; |
|
2534 |
} |
|
2535 |
p = __ByteArrayInstPtr(self)->ba_element; |
|
2536 |
p = p + __idx - 1; |
|
2537 |
||
2538 |
limit = limit - 4 + 1; |
|
4927 | 2539 |
|
2540 |
#if defined(__BSWAP) |
|
4969 | 2541 |
/* |
2542 |
* can we use the bswap instruction ? |
|
2543 |
* notice - not all CPUs have it (the HAS_BSWAP checks this). |
|
2544 |
*/ |
|
2545 |
if (__HAS_BSWAP() |
|
2546 |
&& (((unsigned int)p & 3) == 0)) { |
|
2547 |
unsigned int *ip; |
|
2548 |
ip = (unsigned int *)p; |
|
2549 |
||
2550 |
while (__idx <= limit) { |
|
2551 |
*ip = __BSWAP(*ip); |
|
2552 |
ip++; |
|
2553 |
__idx += 4; |
|
2554 |
} |
|
2555 |
RETURN (self); |
|
2556 |
} |
|
4927 | 2557 |
#endif /* __BSWAP */ |
2558 |
||
4969 | 2559 |
while (__idx <= limit) { |
2560 |
t = p[0]; |
|
2561 |
p[0] = p[3]; |
|
2562 |
p[3] = t; |
|
2563 |
t = p[1]; |
|
2564 |
p[1] = p[2]; |
|
2565 |
p[2] = t; |
|
2566 |
p += 4; |
|
2567 |
__idx += 4; |
|
2568 |
} |
|
2569 |
RETURN ( self ); |
|
4365 | 2570 |
} |
2571 |
%}. |
|
2572 |
^ super swapLongsFrom:startIndex to:endIndex "/ rubbish - there is no one currenly |
|
2573 |
||
2574 |
" |
|
8908 | 2575 |
#[1 2 3 4 5 6 7 8 9] copy swapLongsFrom:1 to:3 |
2576 |
#[1 2 3 4 5 6 7 8 9] copy swapLongsFrom:1 to:4 |
|
2577 |
#[1 2 3 4 5 6 7 8 9] copy swapLongsFrom:1 to:5 |
|
2578 |
#[1 2 3 4 5 6 7 8 9] copy swapLongsFrom:1 to:6 |
|
2579 |
#[1 2 3 4 5 6 7 8 9] copy swapLongsFrom:1 to:7 |
|
2580 |
#[1 2 3 4 5 6 7 8 9] copy swapLongsFrom:1 to:8 |
|
2581 |
#[1 2 3 4 5 6 7 8 9 10] copy swapLongsFrom:1 to:11 |
|
2582 |
#[1 2 3 4 5 6 7 8 9 10 11] copy swapLongsFrom:1 to:12 |
|
2583 |
#[1 2 3 4 5 6 7 8 9 10 11 12] copy swapLongsFrom:1 to:13 |
|
2584 |
#[1 2 3 4 5 6 7 8 9] copy swapLongsFrom:5 to:10 |
|
4365 | 2585 |
" |
1 | 2586 |
! ! |
550 | 2587 |
|
10028 | 2588 |
|
550 | 2589 |
!ByteArray methodsFor:'printing & storing'! |
2590 |
||
13078
7934a86b395b
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
12932
diff
changeset
|
2591 |
displayOn:aGCOrStream |
1244 | 2592 |
"return a printed representation of the receiver for displaying" |
2593 |
||
16741 | 2594 |
(self class == ByteArray and:[aGCOrStream isStream]) ifTrue:[ |
2595 |
self storeOn:aGCOrStream. |
|
2596 |
^ self |
|
6513
d7bc1d1dab4b
displayString fix for subclasses
Claus Gittinger <cg@exept.de>
parents:
6125
diff
changeset
|
2597 |
]. |
13078
7934a86b395b
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
12932
diff
changeset
|
2598 |
^ super displayOn:aGCOrStream |
563 | 2599 |
|
2600 |
"Created: 25.10.1995 / 13:33:26 / cg" |
|
1244 | 2601 |
"Modified: 22.4.1996 / 12:54:06 / cg" |
563 | 2602 |
! |
2603 |
||
7172 | 2604 |
hexPrintOn:aStream |
7245 | 2605 |
"print as hex string, eg: 'FF0243'. |
2606 |
This string can be used in #fromHexString: to recreate the byteArray" |
|
7172 | 2607 |
|
2608 |
self do:[:byte| |
|
8908 | 2609 |
byte printOn:aStream base:16 size:2 fill:$0. |
7172 | 2610 |
]. |
2611 |
||
2612 |
" |
|
2613 |
#[1 2 3 4 10 17] hexPrintOn:Transcript |
|
2614 |
" |
|
7245 | 2615 |
|
2616 |
" |
|
2617 |
|s| |
|
2618 |
s := String streamContents:[:s | #[1 2 3 4 10 17] hexPrintOn:s]. |
|
8908 | 2619 |
ByteArray fromHexString:s |
7245 | 2620 |
" |
7172 | 2621 |
! |
2622 |
||
9379 | 2623 |
hexPrintOn:aStream withSeparator:aSeparatorStringOrCharacter |
2624 |
"print as hex string, eg: 'FF:02:43'" |
|
2625 |
||
2626 |
self do:[:byte| |
|
12879 | 2627 |
byte printOn:aStream base:16 size:2 fill:$0. |
9379 | 2628 |
] separatedBy:[ |
12879 | 2629 |
aSeparatorStringOrCharacter printOn:aStream |
9379 | 2630 |
]. |
2631 |
||
2632 |
" |
|
2633 |
#[1 2 3 4 10 17] hexPrintOn:Transcript withSeparator:$: |
|
2634 |
" |
|
2635 |
! |
|
2636 |
||
9368 | 2637 |
hexPrintString |
2638 |
"print as hex string, eg: 'FF0243'. |
|
2639 |
This string can be used in #fromHexString: to recreate the byteArray" |
|
2640 |
||
2641 |
|s| |
|
2642 |
||
2643 |
s := '' writeStream. |
|
12932 | 2644 |
self do:[:byteOrCharacter | |
13703 | 2645 |
|byte| |
2646 |
byte := byteOrCharacter isCharacter |
|
2647 |
ifTrue:[byteOrCharacter codePoint] |
|
2648 |
ifFalse:[byteOrCharacter]. |
|
2649 |
byte printOn:s base:16 size:2 fill:$0. |
|
9368 | 2650 |
]. |
2651 |
^ s contents. |
|
2652 |
||
2653 |
" |
|
2654 |
#[1 2 3 4 10 17] hexPrintString |
|
2655 |
" |
|
2656 |
||
2657 |
" |
|
2658 |
ByteArray fromHexString:#[1 2 3 4 10 17] hexPrintString |
|
2659 |
" |
|
12932 | 2660 |
|
2661 |
"Modified: / 03-07-2010 / 01:59:19 / cg" |
|
9368 | 2662 |
! |
2663 |
||
9379 | 2664 |
hexPrintStringWithSeparator:aSeparatorStringOrCharacter |
2665 |
"print as hex string, eg: 'FF:02:43'." |
|
2666 |
||
2667 |
|s| |
|
2668 |
||
2669 |
s := '' writeStream. |
|
2670 |
self hexPrintOn:s withSeparator:aSeparatorStringOrCharacter. |
|
2671 |
^ s contents. |
|
2672 |
||
2673 |
" |
|
2674 |
#[1 2 3 4 10 17] hexPrintStringWithSeparator:$: |
|
11860 | 2675 |
#[1 2 3 4 10 17] hexPrintStringWithSeparator:Character space |
9379 | 2676 |
" |
2677 |
! |
|
2678 |
||
5019
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2679 |
printOn:aStream |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2680 |
"append a printed representation to aStream" |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2681 |
|
12487 | 2682 |
|cls| |
2683 |
||
2684 |
"/ care for subclasses |
|
12879 | 2685 |
((cls := self class) == ByteArray or:[cls == ImmutableByteArray]) ifTrue:[ |
2686 |
aStream nextPutAll:'#['. |
|
2687 |
self |
|
2688 |
do:[:byte | byte printOn:aStream] |
|
2689 |
separatedBy:[aStream space]. |
|
2690 |
aStream nextPut:$]. |
|
2691 |
^ self |
|
5019
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2692 |
]. |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2693 |
^ super printOn:aStream |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2694 |
|
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2695 |
" |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2696 |
#[1 2 3 4 5] printOn:Transcript |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2697 |
|
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2698 |
|
8908 | 2699 |
#[1 2 3 4 5] storeString |
2700 |
#[1 2 3 4 5] displayString |
|
2701 |
#[1 2 3 4 5] printString |
|
5019
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2702 |
" |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2703 |
|
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2704 |
"Modified: / 12.9.1997 / 22:11:33 / cg" |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2705 |
"Modified: / 17.3.1999 / 17:01:31 / stefan" |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2706 |
! |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2707 |
|
4050 | 2708 |
printOn:aStream base:radix |
2709 |
"append a printed representation to aStream in the given number base." |
|
2710 |
||
6125 | 2711 |
^ self printOn:aStream base:radix showRadix:false |
2712 |
||
2713 |
" |
|
2714 |
#[1 2 3 4 5] printOn:Transcript base:2 |
|
2715 |
'Hello World' printOn:Transcript base:2 |
|
2716 |
||
8908 | 2717 |
#[1 2 3 4 5] storeString |
2718 |
#[1 2 3 4 5] displayString |
|
2719 |
#[1 2 3 4 5] printString |
|
6125 | 2720 |
" |
2721 |
||
2722 |
"Modified: / 17.3.1999 / 17:01:31 / stefan" |
|
2723 |
"Modified: / 31.10.2001 / 09:43:56 / cg" |
|
2724 |
! |
|
2725 |
||
2726 |
printOn:aStream base:radix showRadix:showRadix |
|
2727 |
"append a printed representation to aStream in the given number base." |
|
2728 |
||
12487 | 2729 |
|cls| |
2730 |
||
2731 |
((cls := self class) == ByteArray or:[cls == ImmutableByteArray]) ifTrue:[ |
|
12879 | 2732 |
"/ care for subclasses |
2733 |
aStream nextPutAll:'#['. |
|
2734 |
self |
|
2735 |
do:[:byte | byte printOn:aStream base:radix showRadix:showRadix] |
|
2736 |
separatedBy:[aStream space]. |
|
2737 |
aStream nextPut:$]. |
|
2738 |
^ self |
|
4050 | 2739 |
]. |
5019
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2740 |
^ self printOn:aStream |
4050 | 2741 |
|
2742 |
" |
|
5019
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2743 |
#[1 2 3 4 5] printOn:Transcript base:2 |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2744 |
'Hello World' printOn:Transcript base:2 |
4050 | 2745 |
|
8908 | 2746 |
#[1 2 3 4 5] storeString |
2747 |
#[1 2 3 4 5] displayString |
|
2748 |
#[1 2 3 4 5] printString |
|
4050 | 2749 |
" |
2750 |
||
2751 |
"Modified: / 12.9.1997 / 22:11:33 / cg" |
|
2752 |
"Modified: / 17.3.1999 / 17:01:31 / stefan" |
|
6125 | 2753 |
"Created: / 31.10.2001 / 09:43:41 / cg" |
4050 | 2754 |
! |
2755 |
||
563 | 2756 |
storeOn:aStream |
2757 |
"append a printed representation from which the receiver can be |
|
2758 |
reconstructed to aStream. (reimplemented to make it look better)" |
|
2759 |
||
12487 | 2760 |
|cls| |
2761 |
||
12879 | 2762 |
((cls := self class) == ByteArray or:[cls == ImmutableByteArray]) ifTrue:[ |
2763 |
"/ care for subclasses |
|
2764 |
aStream nextPutAll:'#['. |
|
2765 |
self |
|
2766 |
do:[:byte | byte storeOn:aStream] |
|
2767 |
separatedBy:[aStream space]. |
|
2768 |
aStream nextPutAll:']'. |
|
2769 |
^ self |
|
563 | 2770 |
]. |
2771 |
^ super storeOn:aStream |
|
2772 |
||
2773 |
" |
|
8908 | 2774 |
#[1 2 3 4 5] storeOn:Transcript |
2775 |
||
2776 |
#[1 2 3 4 5] storeString |
|
2777 |
#[1 2 3 4 5] displayString |
|
2778 |
#[1 2 3 4 5] printString |
|
563 | 2779 |
" |
1082 | 2780 |
|
2915 | 2781 |
"Modified: 12.9.1997 / 22:11:33 / cg" |
550 | 2782 |
! ! |
2783 |
||
7822 | 2784 |
!ByteArray methodsFor:'queries'! |
1082 | 2785 |
|
563 | 2786 |
max |
2787 |
"return the maximum value in the receiver - |
|
8908 | 2788 |
redefined to speedup image processing and sound-player |
563 | 2789 |
(which need a fast method for this on byteArrays)" |
550 | 2790 |
|
563 | 2791 |
%{ /* NOCONTEXT */ |
550 | 2792 |
|
2793 |
REGISTER unsigned char *cp; |
|
563 | 2794 |
REGISTER int index, max; |
2795 |
int len; |
|
550 | 2796 |
|
14135 | 2797 |
if (__isByteArrayLike(self)) { |
2798 |
max = 0; |
|
2799 |
index = 0; |
|
2800 |
len = __qSize(self) - OHDR_SIZE; |
|
2801 |
cp = &(__ByteArrayInstPtr(self)->ba_element[0]); |
|
2802 |
||
2803 |
while (++index <= len) { |
|
2804 |
unsigned int byte; |
|
2805 |
||
2806 |
byte = *cp; |
|
2807 |
cp++; |
|
2808 |
if (byte > max) { |
|
2809 |
max = byte; |
|
2810 |
if (byte == 255) break; |
|
2811 |
} |
|
2812 |
} |
|
2813 |
RETURN ( __mkSmallInteger(max) ); |
|
550 | 2814 |
} |
563 | 2815 |
%}. |
2816 |
^ super max |
|
550 | 2817 |
|
2818 |
" |
|
8908 | 2819 |
#[1 2 3 1 2 3 1 2 19] max |
550 | 2820 |
" |
5554 | 2821 |
! |
550 | 2822 |
|
10819 | 2823 |
referencesAny:aCollection |
10821 | 2824 |
"redefined to speed up searching when many byteArray instances are present" |
2825 |
||
10819 | 2826 |
self class == ByteArray ifTrue:[^ false]. |
2827 |
^ super referencesAny:aCollection |
|
2828 |
! |
|
2829 |
||
550 | 2830 |
usageCounts |
2831 |
"return an array filled with value-counts - |
|
2832 |
This is needed in the bitmap/image classes to get info on color usage. |
|
2833 |
(i.e. to build up a histogram of color usage within an image)." |
|
2834 |
||
2835 |
|counts| |
|
2836 |
||
2837 |
counts := Array basicNew:256. |
|
2838 |
||
2839 |
%{ /* STACK: 2000 */ |
|
2840 |
||
2841 |
REGISTER unsigned char *cp; |
|
2842 |
REGISTER int nByte; |
|
2843 |
REGISTER int index; |
|
8913 | 2844 |
INT icounts[256]; |
550 | 2845 |
|
12768
4c56c6dd923d
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12719
diff
changeset
|
2846 |
if (__isByteArrayLike(self) && __isArray(counts)) { |
12879 | 2847 |
/* |
2848 |
* zero counts |
|
2849 |
*/ |
|
2850 |
for (index=0; index<256; index++) { |
|
2851 |
icounts[index] = 0; |
|
2852 |
} |
|
2853 |
||
2854 |
/* |
|
2855 |
* count |
|
2856 |
*/ |
|
2857 |
nByte = __qSize(self) - OHDR_SIZE; |
|
2858 |
cp = &(__ByteArrayInstPtr(self)->ba_element[0]); |
|
2859 |
while (nByte--) { |
|
2860 |
icounts[*cp++]++; |
|
2861 |
} |
|
2862 |
||
2863 |
/* |
|
2864 |
* make it real counts |
|
2865 |
*/ |
|
2866 |
for (index=0; index<256; index++) { |
|
2867 |
__ArrayInstPtr(counts)->a_element[index] = __mkSmallInteger(icounts[index]); |
|
2868 |
} |
|
2869 |
RETURN ( counts ); |
|
550 | 2870 |
} |
2871 |
%} |
|
2872 |
. |
|
2873 |
self primitiveFailed |
|
2874 |
||
2875 |
" |
|
8908 | 2876 |
#[1 2 3 1 2 3 1 2 250 251 250 251 255] usageCounts |
550 | 2877 |
" |
2878 |
! |
|
2879 |
||
563 | 2880 |
usedValues |
2881 |
"return a new ByteArray with all used values (actually a kind of Set); |
|
8908 | 2882 |
This is needed specially in the bitmap/Imageclasses to find used colors |
2866 | 2883 |
of an image." |
550 | 2884 |
|
563 | 2885 |
%{ /* STACK: 400 */ |
550 | 2886 |
|
2887 |
REGISTER unsigned char *cp; |
|
2866 | 2888 |
unsigned char *endp; |
563 | 2889 |
REGISTER int len; |
2890 | 2890 |
OBJ result; |
2866 | 2891 |
union { |
12879 | 2892 |
unsigned char flags[256]; |
2893 |
int toForceAlignmentOfFlags; |
|
2866 | 2894 |
} f; |
2895 |
||
2896 |
#ifdef TO_BE_MEASURED |
|
2897 |
int coverage = 0; |
|
2898 |
#endif |
|
550 | 2899 |
|
12768
4c56c6dd923d
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12719
diff
changeset
|
2900 |
if (__isByteArrayLike(self)) { |
12879 | 2901 |
memset(f.flags, 0, sizeof(f.flags)); |
2902 |
len = __qSize(self) - OHDR_SIZE; |
|
2903 |
cp = &(__ByteArrayInstPtr(self)->ba_element[0]); |
|
2904 |
||
2905 |
/* for each used byte, set flag */ |
|
2906 |
while (len > 0) { |
|
2866 | 2907 |
#ifdef TO_BE_MEASURED |
12879 | 2908 |
unsigned byte; |
2909 |
||
2910 |
byte = *cp; |
|
2911 |
if (! f.flags[byte]) { |
|
2912 |
f.flags[byte] = 1; |
|
2913 |
coverage++; |
|
2914 |
if (coverage == 256) { |
|
2915 |
/* no need to scan rest */ |
|
2916 |
break; |
|
2917 |
} |
|
2918 |
} |
|
2866 | 2919 |
#else |
12879 | 2920 |
f.flags[*cp] = 1; |
2866 | 2921 |
#endif |
12879 | 2922 |
cp++; |
2923 |
len--; |
|
2924 |
} |
|
2925 |
||
2926 |
/* count 1's */ |
|
2927 |
len = 0; |
|
2928 |
for (cp=f.flags, endp=f.flags+256; cp < endp;) { |
|
2929 |
if ( *((unsigned int *)cp)) { |
|
2930 |
if (cp[0]) len++; |
|
2931 |
if (cp[1]) len++; |
|
2932 |
if (cp[2]) len++; |
|
2933 |
if (cp[3]) len++; |
|
2934 |
} |
|
2935 |
cp += 4; |
|
2936 |
} |
|
2937 |
||
2938 |
/* create ByteArray of used values */ |
|
2939 |
result = __BYTEARRAY_UNINITIALIZED_NEW_INT(len); |
|
2940 |
if (result) { |
|
2941 |
cp = __ByteArrayInstPtr(result)->ba_element; |
|
2942 |
for (len=0; len < 256; len++) { |
|
2943 |
if (f.flags[len]) |
|
2944 |
*cp++ = len; |
|
2945 |
} |
|
2946 |
RETURN ( result ); |
|
2947 |
} |
|
550 | 2948 |
} |
2866 | 2949 |
%}. |
2950 |
^ self asIdentitySet asByteArray |
|
550 | 2951 |
|
2952 |
" |
|
8908 | 2953 |
#[1 2 3 1 2 3 1 2 3 1 2 3 4 5 6 4 5 6] usedValues |
550 | 2954 |
" |
2955 |
! ! |
|
2956 |
||
16741 | 2957 |
|
7822 | 2958 |
!ByteArray methodsFor:'searching'! |
2959 |
||
2960 |
indexOf:aByte startingAt:start |
|
2961 |
"return the index of the first occurrence of the argument, aByte |
|
2962 |
in the receiver starting at start, anInteger; return 0 if not found. |
|
2963 |
- reimplemented here for speed" |
|
2964 |
||
2965 |
%{ /* NOCONTEXT */ |
|
2966 |
||
2967 |
REGISTER unsigned char *cp; |
|
8913 | 2968 |
REGISTER INT index; |
2969 |
REGISTER int byteValue; |
|
7822 | 2970 |
REGISTER int len; |
2971 |
OBJ cls; |
|
2972 |
||
2973 |
if (__isSmallInteger(aByte) &&__isBytes(self)) { |
|
14135 | 2974 |
byteValue = __intVal(aByte); |
2975 |
||
2976 |
if (byteValue & ~0xFF /* i.e. (byteValue < 0) || (byteValue > 255) */) { |
|
2977 |
/* |
|
2978 |
* searching for something which cannot be found |
|
2979 |
*/ |
|
2980 |
RETURN ( __mkSmallInteger(0) ); |
|
2981 |
} |
|
2982 |
||
2983 |
if (__isSmallInteger(start)) { |
|
2984 |
index = __intVal(start); |
|
2985 |
len = __byteArraySize(self); |
|
2986 |
cp = __ByteArrayInstPtr(self)->ba_element; |
|
2987 |
if ((cls = __qClass(self)) != @global(ByteArray)) { |
|
2988 |
int nInst; |
|
2989 |
||
2990 |
nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
2991 |
cp += nInst; |
|
2992 |
len -= nInst; |
|
2993 |
} |
|
2994 |
cp += index - 1; |
|
14382
dcbff20f0a14
changed: #indexOf:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14135
diff
changeset
|
2995 |
#ifdef __UNROLL_LOOPS__ |
dcbff20f0a14
changed: #indexOf:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14135
diff
changeset
|
2996 |
while ((index+4) < len) { |
dcbff20f0a14
changed: #indexOf:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14135
diff
changeset
|
2997 |
if (cp[0] == byteValue) { RETURN ( __mkSmallInteger(index) ); } |
dcbff20f0a14
changed: #indexOf:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14135
diff
changeset
|
2998 |
if (cp[1] == byteValue) { RETURN ( __mkSmallInteger(index+1) ); } |
dcbff20f0a14
changed: #indexOf:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14135
diff
changeset
|
2999 |
if (cp[2] == byteValue) { RETURN ( __mkSmallInteger(index+2) ); } |
dcbff20f0a14
changed: #indexOf:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14135
diff
changeset
|
3000 |
if (cp[3] == byteValue) { RETURN ( __mkSmallInteger(index+3) ); } |
dcbff20f0a14
changed: #indexOf:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14135
diff
changeset
|
3001 |
index += 4; |
dcbff20f0a14
changed: #indexOf:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14135
diff
changeset
|
3002 |
cp += 4; |
dcbff20f0a14
changed: #indexOf:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14135
diff
changeset
|
3003 |
} |
dcbff20f0a14
changed: #indexOf:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14135
diff
changeset
|
3004 |
#endif |
14135 | 3005 |
while (index <= len) { |
3006 |
if (*cp == byteValue) { |
|
3007 |
RETURN ( __mkSmallInteger(index) ); |
|
3008 |
} |
|
3009 |
index++; |
|
3010 |
cp++; |
|
3011 |
} |
|
3012 |
RETURN ( __mkSmallInteger(0) ); |
|
3013 |
} |
|
7822 | 3014 |
} |
3015 |
%}. |
|
3016 |
^ super indexOf:aByte startingAt:start |
|
3017 |
||
3018 |
" |
|
14135 | 3019 |
#[1 2 3 4 5 6 7 8 9 0 1 2 3 4 5] indexOf:0 startingAt:1 |
7822 | 3020 |
" |
3021 |
! ! |
|
3022 |
||
16741 | 3023 |
|
5554 | 3024 |
!ByteArray methodsFor:'testing'! |
3025 |
||
3026 |
isByteArray |
|
3027 |
"return true, if the receiver is some kind of bytearray; |
|
3028 |
true is returned here - the method is redefined from Object." |
|
3029 |
||
3030 |
^ true |
|
3031 |
||
3032 |
||
3033 |
! |
|
3034 |
||
3035 |
isLiteral |
|
3036 |
"return true, if the receiver can be used as a literal constant in ST syntax |
|
3037 |
(i.e. can be used in constant arrays)" |
|
3038 |
||
3039 |
"/ no, simply returning true here is a mistake: |
|
8908 | 3040 |
"/ it could be a subclass of ByteArray |
5554 | 3041 |
"/ (of which the compiler does not know at all ...) |
3042 |
||
3043 |
^ self class == ByteArray |
|
3044 |
||
3045 |
"Modified: 22.4.1996 / 12:55:30 / cg" |
|
3046 |
! ! |
|
3047 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3048 |
!ByteArray class methodsFor:'documentation'! |
628 | 3049 |
|
3050 |
version |
|
16741 | 3051 |
^ '$Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.214 2014-07-10 12:23:23 cg Exp $' |
12091 | 3052 |
! |
3053 |
||
3054 |
version_CVS |
|
16741 | 3055 |
^ '$Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.214 2014-07-10 12:23:23 cg Exp $' |
628 | 3056 |
! ! |
15274 | 3057 |