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