author | Stefan Vogel <sv@exept.de> |
Wed, 02 Dec 2015 11:09:21 +0100 | |
changeset 18966 | 3bc73a4cdae6 |
parent 18665 | fd08070e4467 |
child 18971 | 13360506ef81 |
child 19048 | 0929f428048d |
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 |
||
17377 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
3208
2d71538b9fd5
now subclass of UIBytes - which contains common protocol
Claus Gittinger <cg@exept.de>
parents:
3205
diff
changeset
|
16 |
UninterpretedBytes variableByteSubclass:#ByteArray |
1002
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
17 |
instanceVariableNames:'' |
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
18 |
classVariableNames:'' |
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
19 |
poolDictionaries:'' |
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
20 |
category:'Collections-Arrayed' |
1 | 21 |
! |
22 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
23 |
!ByteArray class methodsFor:'documentation'! |
88 | 24 |
|
563 | 25 |
copyright |
26 |
" |
|
27 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
28 |
All Rights Reserved |
|
29 |
||
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
88 | 39 |
documentation |
40 |
" |
|
41 |
ByteArrays store integers in the range 0..255. |
|
129 | 42 |
In contrast to normal arrays (which store pointers to their elements), |
362 | 43 |
byteArrays store the values in a dense & compact way. ByteArrays can |
129 | 44 |
be used to hold the data for bitmaps, images and other bulk data. |
45 |
ByteArrays are also used to store the bytecode-instructions of an |
|
46 |
interpreted method and are used as superclass for Strings. |
|
47 |
||
48 |
ByteArrays can be used as literals i.e. you can enter ByteArray-constants |
|
49 |
as #[ element1 element2 .... elementN] and also use byteArray constants |
|
50 |
as elements in a constant array. |
|
51 |
As in: #( #[1 1 1] #[2 2 2] #[3 3 3]) |
|
52 |
||
53 |
If you have to communicate structure-data (in the C-sense) with external |
|
54 |
programs/data-bases, see a companion class (Structure) in the goodies directory. |
|
55 |
It allows the definition of subclasses of ByteArray, which transparently fetch |
|
56 |
and store C-structure fields. |
|
1266 | 57 |
|
4062 | 58 |
[memory requirements:] |
4661 | 59 |
OBJ-HEADER + size |
4062 | 60 |
|
2145 | 61 |
[warning:] |
4661 | 62 |
read the warning about 'growing fixed size collection' |
63 |
in ArrayedCollection's documentation |
|
2145 | 64 |
|
1290 | 65 |
[author:] |
4661 | 66 |
Claus Gittinger |
1290 | 67 |
|
1266 | 68 |
[See also:] |
4661 | 69 |
Array CharacterArray String |
88 | 70 |
" |
2 | 71 |
! ! |
72 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
73 |
!ByteArray class methodsFor:'instance creation'! |
1 | 74 |
|
75 |
uninitializedNew:anInteger |
|
76 |
"return a new instance of the receiver with uninitialized |
|
77 |
(i.e. undefined) contents. The indexed elements have any random |
|
8908 | 78 |
value. However, any named instance variables are still nilled. |
75 | 79 |
For use, when contents will be set anyway shortly after - this |
80 |
is a bit faster than the regular basicNew:, which clears the bytes. |
|
81 |
Of course, it only makes a difference for very big ByteArrays, such |
|
82 |
as used for images/bitmaps. |
|
83 |
||
84 |
Notice: if you want to port code using uninitializedNew: to another |
|
8908 | 85 |
smalltalk, you have to add an 'uninitializedNew: -> basicNew:'-calling |
75 | 86 |
method to the ByteArray class of the other smalltalk." |
1 | 87 |
|
88 |
%{ /* NOCONTEXT */ |
|
89 |
OBJ newobj; |
|
90 |
INT instsize, nInstVars, nindexedinstvars; |
|
91 |
REGISTER OBJ *op; |
|
92 |
||
249 | 93 |
if (__isSmallInteger(anInteger)) { |
8908 | 94 |
nindexedinstvars = __intVal(anInteger); |
95 |
if (nindexedinstvars >= 0) { |
|
96 |
if (self == ByteArray) { |
|
97 |
/* |
|
98 |
* the most common case |
|
99 |
*/ |
|
100 |
instsize = OHDR_SIZE + nindexedinstvars; |
|
101 |
if (__CanDoQuickNew(instsize)) { /* OBJECT ALLOCATION */ |
|
102 |
__qCheckedNew(newobj, instsize); |
|
103 |
__InstPtr(newobj)->o_class = self; |
|
104 |
__qSTORE(newobj, self); |
|
105 |
RETURN (newobj ); |
|
106 |
} |
|
107 |
} else { |
|
108 |
/* |
|
109 |
* Take care for subclasses like TwoByteString |
|
110 |
*/ |
|
111 |
switch (__smallIntegerVal(__ClassInstPtr(self)->c_flags) & ARRAYMASK) { |
|
112 |
case BYTEARRAY: |
|
113 |
break; |
|
114 |
||
115 |
case WORDARRAY: |
|
116 |
case SWORDARRAY: |
|
117 |
nindexedinstvars *= 2; |
|
118 |
break; |
|
119 |
||
120 |
case LONGARRAY: |
|
121 |
case SLONGARRAY: |
|
122 |
nindexedinstvars *= 4; |
|
123 |
break; |
|
124 |
||
125 |
default: |
|
126 |
/* don't know about this array type, delegate to super */ |
|
127 |
goto out; |
|
128 |
} |
|
129 |
} |
|
130 |
nInstVars = __intVal(__ClassInstPtr(self)->c_ninstvars); |
|
131 |
instsize = OHDR_SIZE + __OBJS2BYTES__(nInstVars) + nindexedinstvars; |
|
132 |
__PROTECT_CONTEXT__ |
|
133 |
__qNew(newobj, instsize); /* OBJECT ALLOCATION */ |
|
134 |
__UNPROTECT_CONTEXT__ |
|
135 |
if (newobj != nil) { |
|
136 |
__InstPtr(newobj)->o_class = self; |
|
137 |
__qSTORE(newobj, self); |
|
138 |
if (nInstVars) { |
|
139 |
/* |
|
140 |
* still have to nil out named instvars ... |
|
141 |
*/ |
|
788
e80f1c42b87b
dont use memset4 if its undefined
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
142 |
#if defined(memset4) && defined(FAST_OBJECT_MEMSET4) |
8908 | 143 |
memset4(__InstPtr(newobj)->i_instvars, nil, nInstVars); |
1 | 144 |
#else |
249 | 145 |
# if defined(FAST_MEMSET) && !defined(NEGATIVE_ADDRESSES) |
8908 | 146 |
/* |
147 |
* knowing that nil is 0 |
|
148 |
*/ |
|
149 |
memset(__InstPtr(newobj)->i_instvars, 0, instsize - OHDR_SIZE); |
|
249 | 150 |
# else |
8908 | 151 |
op = __InstPtr(newobj)->i_instvars; |
152 |
while (nInstVars--) |
|
153 |
*op++ = nil; |
|
249 | 154 |
# endif |
1 | 155 |
#endif |
8908 | 156 |
} |
157 |
RETURN ( newobj ); |
|
158 |
} |
|
159 |
} |
|
1 | 160 |
} |
8159
ac6725a62e17
#uninitializedNew: - take care of WORDARRAYS etc.
Stefan Vogel <sv@exept.de>
parents:
8096
diff
changeset
|
161 |
out:; |
5425 | 162 |
%}. |
1 | 163 |
^ self basicNew:anInteger |
164 |
! ! |
|
165 |
||
11502 | 166 |
|
18966 | 167 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
168 |
!ByteArray class methodsFor:'queries'! |
550 | 169 |
|
13706
62f6489aa2c6
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
13703
diff
changeset
|
170 |
elementByteSize |
16851
e7212682103b
comment/format in: #elementByteSize
Claus Gittinger <cg@exept.de>
parents:
16807
diff
changeset
|
171 |
"for bit-like containers, return the number of bytes stored per element. |
e7212682103b
comment/format in: #elementByteSize
Claus Gittinger <cg@exept.de>
parents:
16807
diff
changeset
|
172 |
Here, 1 is returned" |
e7212682103b
comment/format in: #elementByteSize
Claus Gittinger <cg@exept.de>
parents:
16807
diff
changeset
|
173 |
|
13706
62f6489aa2c6
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
13703
diff
changeset
|
174 |
^ 1 |
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 |
"Created: / 15-09-2011 / 14:12:53 / cg" |
62f6489aa2c6
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
13703
diff
changeset
|
177 |
! |
62f6489aa2c6
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
13703
diff
changeset
|
178 |
|
550 | 179 |
isBuiltInClass |
1266 | 180 |
"return true if this class is known by the run-time-system. |
181 |
Here, true is returned for myself, false for subclasses." |
|
550 | 182 |
|
183 |
^ self == ByteArray |
|
1266 | 184 |
|
185 |
"Modified: 23.4.1996 / 15:56:25 / cg" |
|
550 | 186 |
! ! |
187 |
||
18966 | 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:[ |
|
18655 | 201 |
size := size1. |
12144 | 202 |
]. |
15625 | 203 |
|
204 |
^ self copy |
|
18655 | 205 |
bitXorBytesFrom:1 to:size with:aByteArray startingAt:1; |
206 |
yourself. |
|
15625 | 207 |
|
208 |
" |
|
18655 | 209 |
#[0 1 2 3 4] bitXor:#[0 1 2 3 4] |
210 |
#[0 1 2 3 4] bitXor:#[0 1 2 3] |
|
15625 | 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 copy changeClassTo:ImmutableByteArray |
74715f6d4cbe
added: #asImmutableByteArray
Claus Gittinger <cg@exept.de>
parents:
12276
diff
changeset
|
1056 |
! |
74715f6d4cbe
added: #asImmutableByteArray
Claus Gittinger <cg@exept.de>
parents:
12276
diff
changeset
|
1057 |
|
12276 | 1058 |
asInteger |
1059 |
"convert myself to an integer - the first byte is most significant. |
|
1060 |
This is also in Squeak." |
|
1061 |
||
16795 | 1062 |
^ self asIntegerMSB:true |
12276 | 1063 |
|
1064 |
" |
|
18655 | 1065 |
#[ 2 ] asInteger hexPrintString |
1066 |
#[ 16r1 16r2 ] asInteger hexPrintString |
|
1067 |
#[4 0 0 0 0 0 0 0] asInteger hexPrintString |
|
12276 | 1068 |
" |
1069 |
! |
|
1070 |
||
12621 | 1071 |
asIntegerMSB:isMSBFirst |
1072 |
"convert myself to an integer - the first byte is most significant. |
|
1073 |
This is also in Squeak." |
|
1074 |
||
14135 | 1075 |
^ (LargeInteger digitBytes:self MSB:isMSBFirst) compressed |
12621 | 1076 |
|
1077 |
" |
|
18655 | 1078 |
(#[ 2 ] asIntegerMSB:true) hexPrintString |
1079 |
(#[ 16r1 16r2 ] asIntegerMSB:true) hexPrintString |
|
1080 |
(#[ 16r1 16r2 ] asIntegerMSB:false) hexPrintString |
|
1081 |
(#[4 0 0 0 0 0 0 0] asIntegerMSB:true) hexPrintString |
|
12621 | 1082 |
" |
1083 |
! |
|
1084 |
||
7117 | 1085 |
asPackedString |
1086 |
"ST-80 compatibility: encode the receiver into an ascii String |
|
1087 |
with 6bits encoded per character. Each group of 6 bits is encoded |
|
1088 |
as a corresponding character (32+value) and the resulting string |
|
1089 |
is returned. The resulting string is always a multiple of 4 (since |
|
1090 |
24 is the lcm of 6 and 8) and the number of remaining characters is |
|
1091 |
encoded in the last character. |
|
16799 | 1092 |
ST-80 uses this encoding for Images... |
1093 |
This is a base64 encoding, very similar (but not equal) to the algorithm used in RFC1421. |
|
1094 |
PS: I don't like it ;-) |
|
1095 |
See also: fromPackedString: - the reverse operation" |
|
7117 | 1096 |
|
8908 | 1097 |
|outStream |
7117 | 1098 |
index "{ Class:SmallInteger}" |
1099 |
nextIndex "{ Class:SmallInteger}" |
|
1100 |
stop "{ Class:SmallInteger}" |
|
1101 |
n "{ Class:SmallInteger}" |
|
1102 |
mod "{ Class:SmallInteger}" |
|
1103 |
cpl| |
|
1104 |
||
18584 | 1105 |
outStream := WriteStream on:(String new:((self size + 2) * 4 // 3)). |
8908 | 1106 |
index := 1. |
7117 | 1107 |
stop := self size. |
1108 |
||
1109 |
stop > 100 ifTrue:[ |
|
18655 | 1110 |
"/ cg: |
1111 |
"/ initial lineBreak |
|
1112 |
outStream cr. |
|
7117 | 1113 |
]. |
1114 |
cpl := 0. |
|
1115 |
||
1116 |
[index <= stop] whileTrue:[ |
|
18655 | 1117 |
"take 3 source bytes" |
1118 |
n := (self at:index) bitShift:16. |
|
1119 |
(index < stop) ifTrue:[ |
|
1120 |
nextIndex := index + 1. |
|
1121 |
n := n bitOr:((self at:nextIndex) bitShift:8). |
|
1122 |
(nextIndex < stop) ifTrue:[ |
|
1123 |
n := n bitOr:(self at:(index + 2)). |
|
1124 |
]. |
|
1125 |
]. |
|
1126 |
index := index + 3. |
|
1127 |
||
1128 |
"took me a while to find that one out ..." |
|
1129 |
n := n bitXor:16r820820. |
|
1130 |
||
1131 |
outStream nextPut:(Character value:((n bitShift:-18) bitAnd:16r3F) + 32). |
|
1132 |
outStream nextPut:(Character value:((n bitShift:-12) bitAnd:16r3F) + 32). |
|
1133 |
outStream nextPut:(Character value:((n bitShift:-6) bitAnd:16r3F) + 32). |
|
1134 |
outStream nextPut:(Character value:(n bitAnd:16r3F) + 32). |
|
1135 |
||
1136 |
"/ cg: |
|
1137 |
"/ lineBreak after every 120 characters |
|
1138 |
"/ fromPackedString will ignore those |
|
1139 |
cpl := cpl + 4. |
|
1140 |
cpl >= 120 ifTrue:[ |
|
1141 |
outStream cr. |
|
1142 |
cpl := 0. |
|
1143 |
]. |
|
7117 | 1144 |
]. |
1145 |
(mod := stop \\ 3) ~~ 0 ifTrue:[ |
|
18655 | 1146 |
outStream backStep. |
1147 |
outStream nextPut:(Character value:(mod + 96)). |
|
7117 | 1148 |
]. |
1149 |
^ outStream contents |
|
1150 |
||
16799 | 1151 |
" |
1152 |
#[ 16r00 16r01 16r02 16r04 16r08 16r10 16r20 16r40 16r80 ] asPackedString |
|
1153 |
" |
|
1154 |
||
7117 | 1155 |
"Modified: 12.11.1996 / 15:45:02 / cg" |
1156 |
! |
|
1157 |
||
17377 | 1158 |
beImmutable |
1159 |
"make myself write-protected" |
|
1160 |
||
1161 |
self changeClassTo:ImmutableByteArray |
|
1162 |
! |
|
1163 |
||
4807 | 1164 |
decodeAsLiteralArray |
1165 |
"given a literalEncoding in the receiver, |
|
1166 |
create & return the corresponding object. |
|
1167 |
The inverse operation to #literalArrayEncoding." |
|
1168 |
||
1169 |
^ self |
|
1170 |
||
1171 |
! |
|
1172 |
||
4778 | 1173 |
literalArrayEncoding |
1174 |
"encode myself as an array literal, from which a copy of the receiver |
|
1175 |
can be reconstructed with #decodeAsLiteralArray." |
|
1176 |
||
12487 | 1177 |
|cls| |
1178 |
||
1179 |
((cls := self class) == ByteArray or:[cls == ImmutableByteArray]) ifTrue:[ |
|
12879 | 1180 |
^ self |
11063
ca134030dff4
changed #literalArrayEncoding: self is onlz ok for realByteArrays
fm
parents:
11010
diff
changeset
|
1181 |
]. |
ca134030dff4
changed #literalArrayEncoding: self is onlz ok for realByteArrays
fm
parents:
11010
diff
changeset
|
1182 |
^ super literalArrayEncoding |
4778 | 1183 |
|
1184 |
" |
|
8908 | 1185 |
#[1 2 3] literalArrayEncoding |
4778 | 1186 |
" |
12 | 1187 |
! ! |
1188 |
||
92 | 1189 |
!ByteArray methodsFor:'copying'! |
1190 |
||
5024 | 1191 |
copy |
1192 |
"redefined for a bit more speed" |
|
1193 |
||
1194 |
self class == ByteArray ifTrue:[ |
|
8908 | 1195 |
^ self copyFrom:1 to:(self size) |
5024 | 1196 |
]. |
1197 |
^ super copy |
|
1198 |
! |
|
1199 |
||
92 | 1200 |
copyFrom:start to:stop |
1201 |
"return the subcollection starting at index start, anInteger and ending |
|
1202 |
at stop, anInteger. |
|
1203 |
- reimplemented here for speed" |
|
1204 |
||
1205 |
%{ /* NOCONTEXT */ |
|
1206 |
||
1207 |
REGISTER unsigned char *srcp; |
|
1208 |
REGISTER unsigned char *dstp; |
|
1209 |
REGISTER int count; |
|
1210 |
int len, index1, index2, sz; |
|
1211 |
OBJ newByteArray; |
|
1212 |
||
12466
353502b2baa9
__isByteArray() to __isByteArrayLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
12453
diff
changeset
|
1213 |
if (__isByteArrayLike(self) |
249 | 1214 |
&& __bothSmallInteger(start, stop)) { |
12879 | 1215 |
len = __byteArraySize(self); |
1216 |
index1 = __intVal(start); |
|
1217 |
index2 = __intVal(stop); |
|
1218 |
||
1219 |
if ((index1 <= index2) && (index1 > 0)) { |
|
1220 |
if (index2 <= len) { |
|
1221 |
count = index2 - index1 + 1; |
|
1222 |
__PROTECT_CONTEXT__ |
|
1223 |
sz = OHDR_SIZE + count; |
|
1224 |
__qNew(newByteArray, sz); /* OBJECT ALLOCATION */ |
|
1225 |
__UNPROTECT_CONTEXT__ |
|
1226 |
if (newByteArray != nil) { |
|
1227 |
__InstPtr(newByteArray)->o_class = ByteArray; |
|
1228 |
__qSTORE(newByteArray, ByteArray); |
|
1229 |
dstp = __ByteArrayInstPtr(newByteArray)->ba_element; |
|
1230 |
srcp = __ByteArrayInstPtr(self)->ba_element + index1 - 1; |
|
2648 | 1231 |
|
1232 |
#ifdef bcopy4 |
|
12879 | 1233 |
if (((unsigned INT)srcp & 3) == ((unsigned INT)dstp & 3)) { |
1234 |
int nW; |
|
1235 |
||
1236 |
/* copy unaligned part */ |
|
1237 |
while (count && (((unsigned INT)srcp & 3) != 0)) { |
|
1238 |
*dstp++ = *srcp++; |
|
1239 |
count--; |
|
1240 |
} |
|
1241 |
if (count) { |
|
1242 |
/* copy aligned part */ |
|
1243 |
nW = count >> 2; |
|
1244 |
if (count & 3) { |
|
1245 |
nW++; |
|
1246 |
} |
|
1247 |
bcopy4(srcp, dstp, nW); |
|
1248 |
} |
|
1249 |
RETURN ( newByteArray ); |
|
1250 |
} |
|
2648 | 1251 |
#endif /* bcopy4 */ |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
1252 |
#if __POINTER_SIZE__ == 8 |
12879 | 1253 |
if (((unsigned INT)srcp & 7) == ((unsigned INT)dstp & 7)) { |
1254 |
int nW; |
|
1255 |
||
1256 |
/* copy unaligned part */ |
|
1257 |
while (count && (((unsigned INT)srcp & 7) != 0)) { |
|
1258 |
*dstp++ = *srcp++; |
|
1259 |
count--; |
|
1260 |
} |
|
1261 |
/* copy aligned part */ |
|
1262 |
while (count >= 8) { |
|
1263 |
((unsigned INT *)dstp)[0] = ((unsigned INT *)srcp)[0]; |
|
1264 |
dstp += 8; |
|
1265 |
srcp += 8; |
|
1266 |
count -= 8; |
|
1267 |
} |
|
1268 |
/* copy remaining part */ |
|
1269 |
while (count) { |
|
1270 |
*dstp++ = *srcp++; |
|
1271 |
count--; |
|
1272 |
} |
|
1273 |
RETURN ( newByteArray ); |
|
1274 |
} |
|
4943
8e51d34f2b3a
slightly tuned #copyFrom:to: and #replaceBytesFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
4941
diff
changeset
|
1275 |
#endif /* bcopy4 */ |
2648 | 1276 |
|
92 | 1277 |
#ifdef FAST_MEMCPY |
12879 | 1278 |
bcopy(srcp, dstp, count); |
92 | 1279 |
#else |
12879 | 1280 |
while (count--) { |
1281 |
*dstp++ = *srcp++; |
|
1282 |
} |
|
92 | 1283 |
#endif |
12879 | 1284 |
RETURN ( newByteArray ); |
1285 |
} |
|
1286 |
} |
|
1287 |
} |
|
92 | 1288 |
} |
1289 |
%}. |
|
1290 |
" |
|
1291 |
fall back in case of non-integer index or out-of-bound index; |
|
1292 |
will eventually lead to an out-of-bound signal raise |
|
1293 |
" |
|
1294 |
^ super copyFrom:start to:stop |
|
4941
7674fdddb1db
better byteArray copy on Intel
Claus Gittinger <cg@exept.de>
parents:
4931
diff
changeset
|
1295 |
|
7674fdddb1db
better byteArray copy on Intel
Claus Gittinger <cg@exept.de>
parents:
4931
diff
changeset
|
1296 |
" |
8908 | 1297 |
#[1 2 3 4 5 6 7 8 9 10] copyFrom:1 to:10 |
1298 |
#[1 2 3 4 5 6 7 8 9 10] copyFrom:5 to:7 |
|
1299 |
||
1300 |
#[1 2 3 4 5 6 7 8 9 10] copyFrom:5 to:11 |
|
1301 |
#[1 2 3 4 5 6 7 8 9 10] copyFrom:0 to:10 |
|
1302 |
#[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
|
1303 |
" |
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1304 |
! |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1305 |
|
5024 | 1306 |
shallowCopy |
1307 |
"redefined for a bit more speed" |
|
1308 |
||
1309 |
self class == ByteArray ifTrue:[ |
|
8908 | 1310 |
^ self copyFrom:1 to:(self size) |
5024 | 1311 |
]. |
1312 |
^ super shallowCopy |
|
1313 |
! |
|
1314 |
||
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1315 |
symbolFrom:start to:stop |
8908 | 1316 |
"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
|
1317 |
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
|
1318 |
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
|
1319 |
|
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1320 |
|sym| |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1321 |
|
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1322 |
%{ /* STACK:1024 */ |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1323 |
|
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1324 |
REGISTER unsigned char *srcp; |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1325 |
REGISTER unsigned char *endp; |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1326 |
REGISTER int count; |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1327 |
int len, index1, index2; |
4086 | 1328 |
unsigned char scratchBuffer[1024], savec; |
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1329 |
|
12466
353502b2baa9
__isByteArray() to __isByteArrayLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
12453
diff
changeset
|
1330 |
if (__isByteArrayLike(self) && __bothSmallInteger(start, stop)) { |
12879 | 1331 |
len = __byteArraySize(self); |
1332 |
index1 = __intVal(start); |
|
1333 |
index2 = __intVal(stop); |
|
1334 |
||
1335 |
if ((index1 <= index2) && (index1 > 0) && (index2 <= len)) { |
|
1336 |
count = index2 - index1 + 1; |
|
1337 |
srcp = __ByteArrayInstPtr(self)->ba_element + index1 - 1; |
|
1338 |
if (index2 < len) { |
|
1339 |
/* temporarily stuff in a '\0' */ |
|
1340 |
endp = srcp + count + 1; |
|
1341 |
savec = *endp; |
|
1342 |
*endp = '\0'; |
|
1343 |
sym = __MKSYMBOL(srcp, 0); |
|
1344 |
/* must refetch endp (in case of a GC */ |
|
1345 |
endp = __ByteArrayInstPtr(self)->ba_element + index1 + count; |
|
1346 |
*endp = savec; |
|
1347 |
} else { |
|
1348 |
/* not enough space for '\0', copy the bytes */ |
|
1349 |
if (count < sizeof(scratchBuffer)) { |
|
1350 |
bcopy(srcp, scratchBuffer, count); |
|
1351 |
scratchBuffer[count] = '\0'; |
|
1352 |
sym = __MKSYMBOL(scratchBuffer, 0); |
|
1353 |
} |
|
1354 |
} |
|
1355 |
} |
|
1356 |
if (sym != nil) |
|
1357 |
RETURN(sym); |
|
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1358 |
} |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1359 |
%}. |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1360 |
" |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1361 |
fall back in case of non-integer index or out-of-bound index |
4086 | 1362 |
or not enough stack-memory available; |
1363 |
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
|
1364 |
" |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1365 |
^ (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
|
1366 |
|
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1367 |
|
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1368 |
" |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1369 |
'abcdefghijklmnop' symbolFrom:1 to:3 |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1370 |
'abcdefghijklmnop' symbolFrom:3 to:16 |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1371 |
'abcdefghijklmnop' symbolFrom:3 to:17 |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1372 |
" |
92 | 1373 |
! ! |
1374 |
||
4154 | 1375 |
!ByteArray methodsFor:'filling & replacing'! |
1 | 1376 |
|
155 | 1377 |
from:start to:stop put:aNumber |
1378 |
"fill part of the receiver with aNumber. |
|
1379 |
- reimplemented here for speed" |
|
1380 |
||
1381 |
%{ /* NOCONTEXT */ |
|
1382 |
||
1383 |
REGISTER unsigned char *dstp; |
|
1384 |
REGISTER int count, value; |
|
249 | 1385 |
int len, index1, index2; |
1386 |
OBJ cls; |
|
155 | 1387 |
|
249 | 1388 |
if (__isSmallInteger(aNumber) |
1002
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
1389 |
&& __bothSmallInteger(start, stop) |
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
1390 |
&& __isBytes(self)) { |
4969 | 1391 |
len = __byteArraySize(self); |
1392 |
index1 = __intVal(start); |
|
1393 |
index2 = __intVal(stop); |
|
1394 |
||
1395 |
dstp = __ByteArrayInstPtr(self)->ba_element + index1 - 1; |
|
1396 |
if ((cls = __qClass(self)) != @global(ByteArray)) { |
|
1397 |
int nInst; |
|
1398 |
||
1399 |
nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
1400 |
dstp += nInst; |
|
1401 |
len -= nInst; |
|
1402 |
} |
|
1403 |
||
1404 |
value = __intVal(aNumber); |
|
1405 |
if (((unsigned)value <= 0xFF) /* i.e. (value >= 0) && (value <= 255) */ |
|
8908 | 1406 |
&& (index1 <= index2) |
4969 | 1407 |
&& (index1 > 0)) { |
1408 |
if (index2 <= len) { |
|
1409 |
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
|
1410 |
|
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1411 |
#ifdef memset4 |
4969 | 1412 |
if (count > 20) { |
1413 |
/* fill unaligned part */ |
|
1414 |
while (((unsigned INT)dstp & 3) != 0) { |
|
1415 |
*dstp++ = value; |
|
1416 |
count--; |
|
1417 |
} |
|
1418 |
/* fill aligned part */ |
|
1419 |
{ |
|
1420 |
int n4 = count & ~3; |
|
1421 |
int v4, nW; |
|
1422 |
||
1423 |
v4 = (value << 8) | value; |
|
1424 |
v4 = (v4 << 16) | v4; |
|
1425 |
nW = n4>>2; |
|
1426 |
memset4(dstp, v4, nW); |
|
1427 |
count -= n4; |
|
1428 |
dstp += n4; |
|
1429 |
} |
|
1430 |
while (count--) { |
|
1431 |
*dstp++ = value; |
|
1432 |
} |
|
1433 |
RETURN (self); |
|
1434 |
} |
|
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1435 |
#endif /* memset4 */ |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1436 |
|
8908 | 1437 |
#if (__POINTER_SIZE__ == 8) |
4969 | 1438 |
{ |
1439 |
INT v8; |
|
1440 |
||
1441 |
v8 = (value << 8) | value; |
|
1442 |
v8 = (v8 << 16) | v8; |
|
1443 |
v8 = (v8 << 32) | v8; |
|
1444 |
||
1445 |
/* fill unaligned part */ |
|
1446 |
while ((count > 0) && (((unsigned INT)dstp & 3) != 0)) { |
|
1447 |
*dstp++ = value; |
|
1448 |
count--; |
|
1449 |
} |
|
1450 |
||
1451 |
if ((count >= 4) && (((unsigned INT)dstp & 7) != 0)) { |
|
1452 |
((unsigned int *)dstp)[0] = v8; |
|
1453 |
dstp += 4; |
|
1454 |
count -= 4; |
|
1455 |
} |
|
1456 |
||
1457 |
/* fill aligned part */ |
|
1458 |
while (count >= 8) { |
|
1459 |
((unsigned INT *)dstp)[0] = v8; |
|
1460 |
dstp += 8; |
|
1461 |
count -= 8; |
|
1462 |
} |
|
1463 |
||
1464 |
/* fill rest */ |
|
1465 |
if (count >= 4) { |
|
1466 |
((unsigned int *)dstp)[0] = v8; |
|
1467 |
dstp += 4; |
|
1468 |
count -= 4; |
|
1469 |
} |
|
1470 |
if (count >= 2) { |
|
1471 |
((unsigned short *)dstp)[0] = v8; |
|
1472 |
dstp += 2; |
|
1473 |
count -= 2; |
|
1474 |
} |
|
1475 |
if (count) { |
|
1476 |
*dstp = value; |
|
1477 |
} |
|
1478 |
RETURN (self); |
|
1479 |
} |
|
8908 | 1480 |
#endif /* 64bit */ |
4945 | 1481 |
|
155 | 1482 |
#ifdef FAST_MEMSET |
4969 | 1483 |
memset(dstp, value, count); |
155 | 1484 |
#else |
8919 | 1485 |
# ifdef __UNROLL_LOOPS__ |
4969 | 1486 |
while (count >= 8) { |
1487 |
dstp[0] = dstp[1] = dstp[2] = dstp[3] = |
|
1488 |
dstp[4] = dstp[5] = dstp[6] = dstp[7] = value; |
|
1489 |
dstp += 8; |
|
1490 |
count -= 8; |
|
1491 |
} |
|
8919 | 1492 |
# endif /* __UNROLL_LOOPS__ */ |
4969 | 1493 |
while (count--) { |
1494 |
*dstp++ = value; |
|
1495 |
} |
|
155 | 1496 |
#endif |
4969 | 1497 |
RETURN (self); |
1498 |
} |
|
1499 |
} |
|
155 | 1500 |
} |
1501 |
%}. |
|
1502 |
" |
|
1503 |
fall back in case of non-integer index or out-of-bound index/value; |
|
1504 |
will eventually lead to an out-of-bound signal raise |
|
1505 |
" |
|
1506 |
^ super from:start to:stop put:aNumber |
|
4945 | 1507 |
|
1508 |
" |
|
8908 | 1509 |
(ByteArray new:10) from:1 to:10 put:1 |
1510 |
(ByteArray new:20) from:10 to:20 put:1 |
|
1511 |
(ByteArray new:20) from:1 to:10 put:1 |
|
4945 | 1512 |
" |
1 | 1513 |
! ! |
1514 |
||
129 | 1515 |
!ByteArray methodsFor:'image manipulation support'! |
1 | 1516 |
|
8908 | 1517 |
bitAndBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart |
4919 | 1518 |
"replace bytes in the receiver with the result of a bitAnd operation. |
1519 |
Warning: this is a destructive operation - elements in the receiver are overwritten." |
|
1520 |
||
1521 |
^ self bitBlitBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart rule:#bitAnd: |
|
1522 |
||
1523 |
" |
|
1524 |
#[1 2 3 4 5 6 7 8] |
|
8908 | 1525 |
bitAndBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 |
4919 | 1526 |
#[1 2 3 4 5 6 7 8] |
4969 | 1527 |
bitAndBytesFrom:1 to:8 with:#[1 1 1 1 1 1 1 1] startingAt:1 |
4919 | 1528 |
" |
1529 |
||
1530 |
! |
|
1531 |
||
1532 |
bitBlitBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart rule:ruleSymbol |
|
1533 |
"perform a special case of an aligned bitBlit operation. |
|
8908 | 1534 |
Bytes in the receiver from dstStart to dstEnd are destructively replaced by the result |
4919 | 1535 |
of some logical operation, as specified by the ruleSymbol. |
1536 |
SourceBytes are fetched starting at sourceOffset. |
|
1537 |
Valid rule symbols are: |
|
18655 | 1538 |
#copy - trivial; same as replaceBytesFrom:to:with:startingAt: |
1539 |
#bitXor: - xoring; byte[dI] = byte[dI] bitXor:(srcByte[sI]) |
|
1540 |
#bitAnd: - anding; byte[dI] = byte[dI] bitAnd:(srcByte[sI]) |
|
1541 |
#bitOr: - oring; byte[dI] = byte[dI] bitOr:(srcByte[sI]) |
|
1542 |
#+ - adding; byte[dI] = (byte[dI] + (srcByte[sI])) mod: 256 |
|
1543 |
#- - subtract; byte[dI] = (byte[dI] - (srcByte[sI])) mod: 256 |
|
4919 | 1544 |
Warning: this is a destructive operation - elements in the receiver are overwritten. |
1545 |
" |
|
1546 |
||
1547 |
|srcIdx| |
|
1548 |
||
1549 |
%{ |
|
12466
353502b2baa9
__isByteArray() to __isByteArrayLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
12453
diff
changeset
|
1550 |
if ((__isByteArrayLike(sourceBytes)) |
4919 | 1551 |
&& (__qClass(self) == ByteArray) |
1552 |
&& __isSmallInteger(dstStart) |
|
1553 |
&& __isSmallInteger(dstEnd) |
|
1554 |
&& __isSmallInteger(sourceStart)) { |
|
18655 | 1555 |
unsigned char *srcP = __ByteArrayInstPtr(sourceBytes)->ba_element; |
1556 |
unsigned char *dstP = __ByteArrayInstPtr(self)->ba_element; |
|
1557 |
int srcLen = __byteArraySize(sourceBytes); |
|
1558 |
int dstLen = __byteArraySize(self); |
|
1559 |
int __srcStart = __intVal(sourceStart); |
|
1560 |
int __dstStart = __intVal(dstStart); |
|
1561 |
int count = __intVal(dstEnd) - __dstStart + 1; |
|
1562 |
||
1563 |
if ((__dstStart >= 1) |
|
1564 |
&& (__srcStart >= 1) |
|
1565 |
&& ((__dstStart + count - 1) <= dstLen) |
|
1566 |
&& ((__srcStart + count - 1) <= srcLen)) { |
|
1567 |
srcP += __srcStart - 1; |
|
1568 |
dstP += __dstStart - 1; |
|
5439 | 1569 |
|
1570 |
#define OP_LOOP_BYTES(OP) \ |
|
16621 | 1571 |
while (count >= 4) { \ |
18655 | 1572 |
(dstP[0]) OP (srcP[0]); \ |
1573 |
(dstP[1]) OP (srcP[1]); \ |
|
1574 |
(dstP[2]) OP (srcP[2]); \ |
|
1575 |
(dstP[3]) OP (srcP[3]); \ |
|
1576 |
srcP += 4; \ |
|
1577 |
dstP += 4; \ |
|
1578 |
count -= 4; \ |
|
16621 | 1579 |
} \ |
5439 | 1580 |
while (count > 0) { \ |
18655 | 1581 |
*dstP OP (*srcP); \ |
1582 |
srcP++; \ |
|
1583 |
dstP++; \ |
|
1584 |
count--; \ |
|
8908 | 1585 |
} |
4919 | 1586 |
|
1587 |
#define OP_LOOP(OP) \ |
|
5439 | 1588 |
while (count >= 16) { \ |
18655 | 1589 |
((unsigned int *)dstP)[0] OP (((unsigned int *)srcP)[0]); \ |
1590 |
((unsigned int *)dstP)[1] OP (((unsigned int *)srcP)[1]); \ |
|
1591 |
((unsigned int *)dstP)[2] OP (((unsigned int *)srcP)[2]); \ |
|
1592 |
((unsigned int *)dstP)[3] OP (((unsigned int *)srcP)[3]); \ |
|
1593 |
srcP += 16; \ |
|
1594 |
dstP += 16; \ |
|
1595 |
count -= 16; \ |
|
4919 | 1596 |
} \ |
5439 | 1597 |
while (count >= 4) { \ |
18655 | 1598 |
((unsigned int *)dstP)[0] OP (((unsigned int *)srcP)[0]); \ |
1599 |
srcP += 4; \ |
|
1600 |
dstP += 4; \ |
|
1601 |
count -= 4; \ |
|
4919 | 1602 |
} \ |
1603 |
while (count > 0) { \ |
|
18655 | 1604 |
*dstP OP (*srcP); \ |
1605 |
srcP++; \ |
|
1606 |
dstP++; \ |
|
1607 |
count--; \ |
|
8908 | 1608 |
} |
1609 |
||
1610 |
||
18655 | 1611 |
if (ruleSymbol == @symbol(bitXor:)) { |
1612 |
OP_LOOP( ^= ) |
|
1613 |
RETURN (self); |
|
1614 |
} |
|
1615 |
if (ruleSymbol == @symbol(bitXorNot:)) { |
|
1616 |
OP_LOOP( ^=~ ) |
|
1617 |
RETURN (self); |
|
1618 |
} |
|
1619 |
if (ruleSymbol == @symbol(bitAnd:)) { |
|
1620 |
OP_LOOP( &= ) |
|
1621 |
RETURN (self); |
|
1622 |
} |
|
1623 |
if (ruleSymbol == @symbol(bitAndNot:)) { |
|
1624 |
OP_LOOP( &=~ ) |
|
1625 |
RETURN (self); |
|
1626 |
} |
|
1627 |
if (ruleSymbol == @symbol(bitOr:)) { |
|
1628 |
OP_LOOP( |= ) |
|
1629 |
RETURN (self); |
|
1630 |
} |
|
1631 |
if (ruleSymbol == @symbol(bitOrNot:)) { |
|
1632 |
OP_LOOP( |=~ ) |
|
1633 |
RETURN (self); |
|
1634 |
} |
|
1635 |
if (ruleSymbol == @symbol(copy)) { |
|
1636 |
OP_LOOP( = ) |
|
1637 |
RETURN (self); |
|
1638 |
} |
|
1639 |
if (ruleSymbol == @symbol(copyNot)) { |
|
1640 |
OP_LOOP( =~ ) |
|
1641 |
RETURN (self); |
|
1642 |
} |
|
1643 |
if (ruleSymbol == @symbol(+)) { |
|
1644 |
OP_LOOP_BYTES( += ) |
|
1645 |
RETURN (self); |
|
1646 |
} |
|
1647 |
if (ruleSymbol == @symbol(-)) { |
|
1648 |
OP_LOOP_BYTES( -= ) |
|
1649 |
RETURN (self); |
|
1650 |
} |
|
1651 |
} |
|
4919 | 1652 |
} |
5549 | 1653 |
#undef OP_LOOP_BYTES |
1654 |
#undef OP_LOOP |
|
1655 |
||
4919 | 1656 |
%}. |
1657 |
ruleSymbol == #copy ifTrue:[ |
|
18655 | 1658 |
self replaceFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart. |
1659 |
^ self |
|
4919 | 1660 |
]. |
1661 |
||
1662 |
srcIdx := sourceStart. |
|
1663 |
dstStart to:dstEnd do:[:dstIdx | |
|
18655 | 1664 |
self at:dstIdx put:((self at:dstIdx) perform:ruleSymbol with:(sourceBytes at:srcIdx)). |
1665 |
srcIdx := srcIdx + 1. |
|
4919 | 1666 |
]. |
1667 |
||
1668 |
" |
|
1669 |
#[1 2 3 4 5 6 7 8] |
|
18655 | 1670 |
bitBlitBytesFrom:1 to:3 with:#[1 2 3 4 5 6 7 8] startingAt:1 rule:#bitXor: |
4919 | 1671 |
#[1 2 3 4 5 6 7 8] |
18655 | 1672 |
bitBlitBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 rule:#bitXor: |
4919 | 1673 |
#[1 2 3 4 5 6 7 8] |
18655 | 1674 |
bitBlitBytesFrom:1 to:8 with:#[1 1 1 1 1 1 1 1] startingAt:1 rule:#bitAnd: |
4919 | 1675 |
#[1 2 3 4 5 6 7 8] |
18655 | 1676 |
bitBlitBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 rule:#+ |
5439 | 1677 |
#[255 0 0 0 0 0 0 0] |
18655 | 1678 |
bitBlitBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 rule:#+ |
4919 | 1679 |
#[1 2 3 4 5 6 7 8] |
18655 | 1680 |
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
|
1681 |
#[1 2 3 4 5 6 7 8] |
18655 | 1682 |
bitBlitBytesFrom:1 to:4 with:#[1 1 1 1 2 2 2 2] startingAt:5 rule:#+ |
5439 | 1683 |
#[1 2 3 4 5 6 7 8] |
18655 | 1684 |
bitBlitBytesFrom:1 to:4 with:#[1 1 1 1 2 2 2 2] startingAt:5 rule:#copyNot |
4919 | 1685 |
|
1686 |
#[1 2 3 4 5 6 7 8] |
|
18655 | 1687 |
bitBlitBytesFrom:1 to:8 with:(1 to:8) startingAt:1 rule:#+ |
5439 | 1688 |
" |
1689 |
! |
|
1690 |
||
1691 |
bitBlitBytesFrom:dstStart to:dstEnd withConstant:sourceByte rule:ruleSymbol |
|
1692 |
"perform a special case of an aligned bitBlit operation. |
|
8908 | 1693 |
Bytes in the receiver from dstStart to dstEnd are destructively replaced by the result |
5439 | 1694 |
of some logical operation, as specified by the ruleSymbol. |
1695 |
Valid rule symbols are: |
|
18655 | 1696 |
#copy - trivial; same as from:to:put: |
1697 |
#bitXor: - xoring; byte[dI] = byte[dI] bitXor:sourceConst |
|
1698 |
#bitAnd: - anding; byte[dI] = byte[dI] bitAnd:sourceConst |
|
1699 |
#bitOr: - oring; byte[dI] = byte[dI] bitOr:sourceConst |
|
1700 |
#+ - adding; byte[dI] = (byte[dI] + sourceConst) mod: 256 |
|
1701 |
#- - subtract; byte[dI] = (byte[dI] - sourceConst) mod: 256 |
|
5439 | 1702 |
Warning: this is a destructive operation - elements in the receiver are overwritten. |
1703 |
" |
|
1704 |
||
1705 |
|srcIdx| |
|
1706 |
||
1707 |
%{ |
|
1708 |
if ((__qClass(self) == ByteArray) |
|
1709 |
&& __isSmallInteger(dstStart) |
|
1710 |
&& __isSmallInteger(dstEnd) |
|
1711 |
&& __isSmallInteger(sourceByte)) { |
|
18655 | 1712 |
unsigned char srcByte = __intVal(sourceByte); |
1713 |
unsigned srcWord; |
|
1714 |
unsigned char *dstP = __ByteArrayInstPtr(self)->ba_element; |
|
1715 |
int dstLen = __byteArraySize(self); |
|
1716 |
int __dstStart = __intVal(dstStart); |
|
1717 |
int count = __intVal(dstEnd) - __dstStart + 1; |
|
1718 |
||
1719 |
srcWord = (srcByte << 8) | srcByte; |
|
1720 |
srcWord = (srcWord << 16) | srcWord; |
|
1721 |
if ((__dstStart >= 1) |
|
1722 |
&& ((__dstStart + count - 1) <= dstLen)) { |
|
1723 |
dstP += __dstStart - 1; |
|
5439 | 1724 |
|
1725 |
#define OP_LOOP_BYTES(OP) \ |
|
16620 | 1726 |
while (count >= 4) { \ |
18655 | 1727 |
dstP[0] OP srcByte; \ |
1728 |
dstP[1] OP srcByte; \ |
|
1729 |
dstP[2] OP srcByte; \ |
|
1730 |
dstP[3] OP srcByte; \ |
|
1731 |
dstP += 4; \ |
|
1732 |
count -= 4; \ |
|
16620 | 1733 |
} \ |
5439 | 1734 |
while (count > 0) { \ |
18655 | 1735 |
*dstP OP srcByte; \ |
1736 |
dstP++; \ |
|
1737 |
count--; \ |
|
8908 | 1738 |
} |
5439 | 1739 |
|
16620 | 1740 |
#define OP_LOOP_INT32(OP) \ |
1741 |
while (count >= 16) { \ |
|
18655 | 1742 |
((unsigned int *)dstP)[0] OP srcWord; \ |
1743 |
((unsigned int *)dstP)[1] OP srcWord; \ |
|
1744 |
((unsigned int *)dstP)[2] OP srcWord; \ |
|
1745 |
((unsigned int *)dstP)[3] OP srcWord; \ |
|
1746 |
dstP += 16; \ |
|
1747 |
count -= 16; \ |
|
16620 | 1748 |
} \ |
1749 |
||
5439 | 1750 |
#define OP_LOOP(OP) \ |
16620 | 1751 |
OP_LOOP_INT32(OP) \ |
5439 | 1752 |
while (count >= 4) { \ |
18655 | 1753 |
((unsigned int *)dstP)[0] OP srcWord; \ |
1754 |
dstP += 4; \ |
|
1755 |
count -= 4; \ |
|
5439 | 1756 |
} \ |
1757 |
while (count > 0) { \ |
|
18655 | 1758 |
*dstP OP srcByte; \ |
1759 |
dstP++; \ |
|
1760 |
count--; \ |
|
8908 | 1761 |
} |
1762 |
||
1763 |
||
18655 | 1764 |
if (ruleSymbol == @symbol(bitXor:)) { |
1765 |
OP_LOOP( ^= ) |
|
1766 |
RETURN (self); |
|
1767 |
} |
|
1768 |
if (ruleSymbol == @symbol(bitXorNot:)) { |
|
1769 |
OP_LOOP( ^=~ ) |
|
1770 |
RETURN (self); |
|
1771 |
} |
|
1772 |
if (ruleSymbol == @symbol(bitAnd:)) { |
|
1773 |
OP_LOOP( &= ) |
|
1774 |
RETURN (self); |
|
1775 |
} |
|
1776 |
if (ruleSymbol == @symbol(bitAndNot:)) { |
|
1777 |
OP_LOOP( &=~ ) |
|
1778 |
RETURN (self); |
|
1779 |
} |
|
1780 |
if (ruleSymbol == @symbol(bitOr:)) { |
|
1781 |
OP_LOOP( |= ) |
|
1782 |
RETURN (self); |
|
1783 |
} |
|
1784 |
if (ruleSymbol == @symbol(bitOrNot:)) { |
|
1785 |
OP_LOOP( |=~ ) |
|
1786 |
RETURN (self); |
|
1787 |
} |
|
1788 |
if (ruleSymbol == @symbol(copy)) { |
|
1789 |
OP_LOOP( = ) |
|
1790 |
RETURN (self); |
|
1791 |
} |
|
1792 |
if (ruleSymbol == @symbol(copyNot)) { |
|
1793 |
OP_LOOP( =~ ) |
|
1794 |
RETURN (self); |
|
1795 |
} |
|
1796 |
if (ruleSymbol == @symbol(+)) { |
|
1797 |
OP_LOOP_BYTES( += ) |
|
1798 |
RETURN (self); |
|
1799 |
} |
|
1800 |
if (ruleSymbol == @symbol(-)) { |
|
1801 |
OP_LOOP_BYTES( -= ) |
|
1802 |
RETURN (self); |
|
1803 |
} |
|
1804 |
} |
|
5439 | 1805 |
} |
5549 | 1806 |
#undef OP_LOOP_BYTES |
1807 |
#undef OP_LOOP |
|
5439 | 1808 |
%}. |
1809 |
ruleSymbol == #copy ifTrue:[ |
|
18655 | 1810 |
self from:dstStart to:dstEnd put:sourceByte. |
1811 |
^ self |
|
5439 | 1812 |
]. |
1813 |
||
1814 |
dstStart to:dstEnd do:[:dstIdx | |
|
18655 | 1815 |
self at:dstIdx put:((self at:dstIdx) perform:ruleSymbol with:sourceByte). |
5439 | 1816 |
]. |
1817 |
||
1818 |
" |
|
1819 |
#[1 2 3 4 5 6 7 8] |
|
18655 | 1820 |
bitBlitBytesFrom:1 to:3 withConstant:1 rule:#bitXor: |
5439 | 1821 |
#[1 2 3 4 5 6 7 8] |
18655 | 1822 |
bitBlitBytesFrom:1 to:8 withConstant:1 rule:#bitXor: |
5439 | 1823 |
#[1 2 3 4 5 6 7 8] |
18655 | 1824 |
bitBlitBytesFrom:1 to:8 withConstant:1 rule:#bitAnd: |
5439 | 1825 |
#[1 2 3 4 5 6 7 8] |
18655 | 1826 |
bitBlitBytesFrom:1 to:8 withConstant:1 rule:#+ |
5439 | 1827 |
#[255 0 0 0 0 0 0 0] |
18655 | 1828 |
bitBlitBytesFrom:1 to:8 withConstant:1 rule:#+ |
5439 | 1829 |
#[1 2 3 4 5 6 7 8] |
18655 | 1830 |
bitBlitBytesFrom:1 to:4 withConstant:1 rule:#+ |
5439 | 1831 |
#[1 2 3 4 5 6 7 8] |
18655 | 1832 |
bitBlitBytesFrom:1 to:4 withConstant:1 rule:#- |
5439 | 1833 |
#[1 2 3 4 5 6 7 8] |
18655 | 1834 |
bitBlitBytesFrom:1 to:4 withConstant:1 rule:#copyNot |
5439 | 1835 |
|
1836 |
#[1 2 3 4 5 6 7 8] |
|
18655 | 1837 |
bitBlitBytesFrom:1 to:8 withConstant:1 rule:#+ |
4919 | 1838 |
" |
1839 |
! |
|
1840 |
||
8908 | 1841 |
bitOrBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart |
4919 | 1842 |
"replace bytes in the receiver with the result of a bitOr operation. |
1843 |
Warning: this is a destructive operation - elements in the receiver are overwritten." |
|
1844 |
||
1845 |
^ self bitBlitBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart rule:#bitOr: |
|
1846 |
||
1847 |
" |
|
1848 |
#[1 2 3 4 5 6 7 8] |
|
8908 | 1849 |
bitOrBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 |
4919 | 1850 |
#[1 2 3 4 5 6 7 8] |
4969 | 1851 |
bitOrBytesFrom:1 to:8 with:#[1 1 1 1 1 1 1 1] startingAt:1 |
4919 | 1852 |
" |
1853 |
||
1854 |
! |
|
1855 |
||
8908 | 1856 |
bitXorBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart |
4919 | 1857 |
"replace bytes in the receiver with the result of an bitXor operation. |
1858 |
Warning: this is a destructive operation - elements in the receiver are overwritten." |
|
1859 |
||
1860 |
^ self bitBlitBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart rule:#bitXor: |
|
1861 |
||
1862 |
" |
|
1863 |
#[1 2 3 4 5 6 7 8] |
|
4969 | 1864 |
bitXorBytesFrom:1 to:3 with:#[1 2 3 4 5 6 7 8] startingAt:1 |
4919 | 1865 |
#[1 2 3 4 5 6 7 8] |
8908 | 1866 |
bitXorBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 |
4919 | 1867 |
#[1 2 3 4 5 6 7 8] |
4969 | 1868 |
bitXorBytesFrom:1 to:8 with:#[1 1 1 1 1 1 1 1] startingAt:1 |
4919 | 1869 |
" |
1870 |
||
1871 |
! |
|
1872 |
||
2866 | 1873 |
compressPixels:nBitsPerPixel width:width height:height into:aByteArray mapping:aMapByteArray |
129 | 1874 |
"given the receiver with 8-bit pixels, compress them into aByteArray |
1875 |
with nBitsPerPixel-depth pixels. The width/height-arguments are needed |
|
1876 |
to allow for any padding. On the fly, the source bytes are translated |
|
1877 |
using aMapByteArray (if non-nil). |
|
1878 |
Notice that smalltalk indexing begins at 1; thus the map-index for a byte |
|
1879 |
value of n is found in map at:(n + 1). |
|
1880 |
Output bits are filled left-to right, i.e. the first byte in the input |
|
1881 |
corresponds to the high bit(s) if the first byte in the input. |
|
8908 | 1882 |
This method can be used to convert 8-bit image data to mono, 2-bit and 4-bit |
129 | 1883 |
bitmaps. |
1884 |
It can also be used to compress byte-arrays into bitArrays." |
|
1885 |
||
1886 |
%{ /* NOCONTEXT */ |
|
1887 |
||
1888 |
REGISTER unsigned char *src, *dst; |
|
1889 |
REGISTER int wrun; |
|
1890 |
unsigned char *dstNext; |
|
1472
abaa73dc0e9a
check for destination overrun
Claus Gittinger <cg@exept.de>
parents:
1459
diff
changeset
|
1891 |
unsigned char *dstEnd; |
129 | 1892 |
int bytesPerRow, mask, shift0, shift; |
1893 |
int w, h, hrun; |
|
1894 |
int srcBytes, dstBytes; |
|
1895 |
int bitsPerPixel; |
|
1896 |
int bits; |
|
1897 |
int ncells; |
|
1898 |
unsigned char *map; |
|
1899 |
||
8908 | 1900 |
if ((__qClass(self) == @global(ByteArray)) |
1133 | 1901 |
&& (__qClass(aByteArray) == @global(ByteArray)) |
249 | 1902 |
&& __isSmallInteger(nBitsPerPixel) |
1903 |
&& __bothSmallInteger(height, width)) { |
|
2915 | 1904 |
if ((aMapByteArray != nil) |
1905 |
&& (__Class(aMapByteArray) == @global(ByteArray))) { |
|
1906 |
map = __ByteArrayInstPtr(aMapByteArray)->ba_element; |
|
1907 |
} else { |
|
1908 |
map = (unsigned char *)0; |
|
1909 |
} |
|
129 | 1910 |
|
2915 | 1911 |
bitsPerPixel = __intVal(nBitsPerPixel); |
1912 |
w = __intVal(width); |
|
1913 |
h = __intVal(height); |
|
1914 |
src = __ByteArrayInstPtr(self)->ba_element; |
|
1915 |
dst = __ByteArrayInstPtr(aByteArray)->ba_element; |
|
1916 |
dstEnd = dst + __byteArraySize(aByteArray); |
|
1917 |
switch (bitsPerPixel) { |
|
1918 |
case 1: |
|
1919 |
mask = 0x01; |
|
1920 |
break; |
|
1921 |
case 2: |
|
1922 |
mask = 0x03; |
|
1923 |
break; |
|
1924 |
case 4: |
|
1925 |
mask = 0x0F; |
|
1926 |
break; |
|
1927 |
case 8: |
|
1928 |
mask = 0xFF; |
|
1929 |
break; |
|
1930 |
default: |
|
13703 | 1931 |
console_printf("invalid depth in compressPixels\n"); |
2915 | 1932 |
goto fail; |
1933 |
} |
|
1934 |
if (map) { |
|
1935 |
/* |
|
1936 |
* if a map is present, it must have entries for |
|
1937 |
* all possible byte-values (i.e. its size must be >= 256) |
|
1938 |
*/ |
|
1939 |
if ((__qSize(aMapByteArray) - OHDR_SIZE) < 256) { |
|
13703 | 1940 |
console_printf("invalid map in compressPixels\n"); |
2915 | 1941 |
goto fail; |
1942 |
} |
|
1943 |
} |
|
129 | 1944 |
|
2915 | 1945 |
bytesPerRow = (w * bitsPerPixel + 7) / 8; |
1946 |
dstBytes = bytesPerRow * h; |
|
1947 |
srcBytes = w * h; |
|
2866 | 1948 |
|
2915 | 1949 |
if ((__byteArraySize(self) >= srcBytes) |
1950 |
&& (__byteArraySize(aByteArray) >= dstBytes)) { |
|
1951 |
for (hrun=h; hrun; hrun--) { |
|
1952 |
dstNext = dst + bytesPerRow; |
|
1953 |
bits = 0; shift = 8; |
|
1954 |
if (map) { |
|
1955 |
for (wrun=w; wrun; wrun--) { |
|
1956 |
bits = (bits << bitsPerPixel) | (map[*src++] & mask); |
|
1957 |
shift -= bitsPerPixel; |
|
1958 |
if (shift == 0) { |
|
1959 |
if (dst == dstEnd) goto fail; |
|
1960 |
*dst++ = bits; |
|
1961 |
bits = 0; shift = 8; |
|
1962 |
} |
|
1963 |
} |
|
1964 |
} else { |
|
1965 |
for (wrun=w; wrun; wrun--) { |
|
1966 |
bits = (bits << bitsPerPixel) | (*src++ & mask); |
|
1967 |
shift -= bitsPerPixel; |
|
1968 |
if (shift == 0) { |
|
1969 |
if (dst == dstEnd) goto fail; |
|
1970 |
*dst++ = bits; |
|
1971 |
bits = 0; shift = 8; |
|
1972 |
} |
|
1973 |
} |
|
1974 |
} |
|
1975 |
if (shift != 8) { |
|
1976 |
if (dst == dstEnd) goto fail; |
|
1977 |
*dst = bits; |
|
1978 |
} |
|
1979 |
dst = dstNext; |
|
1980 |
} |
|
1981 |
RETURN ( self ); |
|
1982 |
} |
|
129 | 1983 |
} |
1984 |
fail: ; |
|
2866 | 1985 |
%}. |
129 | 1986 |
self primitiveFailed |
1987 |
||
1988 |
"Example1: |
|
1989 |
compress 1 byte-per-pixel bitmap to 1-bit-per-pixel bitmap |
|
1990 |
" |
|
1991 |
" |
|
1992 |
|inBits outBits| |
|
1993 |
||
1994 |
inBits := #[0 0 0 0 1 1 1 1 |
|
2915 | 1995 |
0 0 1 1 0 0 1 1 |
1996 |
0 1 0 1 0 1 0 1 |
|
1997 |
1 1 1 1 0 0 0 0]. |
|
129 | 1998 |
outBits := ByteArray new:4. |
1999 |
inBits compressPixels:1 width:8 height:4 |
|
2915 | 2000 |
into:outBits mapping:nil. |
129 | 2001 |
outBits inspect |
2002 |
" |
|
2003 |
||
2004 |
"Example2: |
|
2005 |
compress byte-array into a bitArray, translating 99 to 0-bits, |
|
2006 |
and 176 to 1-bits. (just a stupid example) |
|
2007 |
" |
|
2008 |
" |
|
2009 |
|inBits outBits map| |
|
2010 |
||
2011 |
inBits := #[176 176 176 176 99 99 99 99 176 176 99 99 176 99 176 99]. |
|
2012 |
map := ByteArray new:256. |
|
2013 |
map at:176+1 put:1. |
|
2014 |
||
2015 |
outBits := ByteArray new:2. |
|
8908 | 2016 |
inBits compressPixels:1 width:16 height:1 |
2915 | 2017 |
into:outBits mapping:map. |
129 | 2018 |
outBits inspect |
2019 |
" |
|
2020 |
||
2021 |
"Example3: |
|
2022 |
compress byte-array into a bitArray, translating everything below 128 to 0-bits, |
|
2023 |
and 128 to 255 to 1-bits.99 to 0-bits (another stupid example) |
|
2024 |
" |
|
2025 |
" |
|
2026 |
|inBits outBits map| |
|
2027 |
||
2028 |
inBits := #[176 176 176 176 99 99 99 99 176 176 99 99 176 99 176 99]. |
|
2029 |
map := ByteArray new:256. |
|
2030 |
map atAll:(128+1 to:255+1) put:1. |
|
2031 |
||
2032 |
outBits := ByteArray new:2. |
|
8908 | 2033 |
inBits compressPixels:1 width:16 height:1 |
2915 | 2034 |
into:outBits mapping:map. |
129 | 2035 |
outBits inspect |
2036 |
" |
|
563 | 2037 |
! |
2038 |
||
2866 | 2039 |
expandPixels:nBitsPerPixel width:width height:height into:aByteArray mapping:aMapByteArray |
563 | 2040 |
"given the receiver with nBitsPerPixel-depth pixels, expand them into |
2041 |
aByteArray with 8-bit pixels. The width/height-arguments are needed |
|
2042 |
to skip any padded src-bits. On the fly, the destination pixels |
|
2043 |
are translated using aMapByteArray (if non-nil). |
|
2044 |
Input bits are read left-to right, i.e. the first byte in the output |
|
2045 |
corresponds to the high bit(s) in the inputs first byte. |
|
2046 |
This is used to display mono, 2-bit and 4-bit bitmaps on grey-scale/color |
|
2047 |
machines. With nBitsPerPixel==8, this is a translate operation. |
|
2048 |
Notice that smalltalk indexing begins at 1; thus the map-index for a byte |
|
2049 |
value of n is found in map at:(n + 1). |
|
2050 |
It can also be used to expand bit-arrays into byteArrays. |
|
2051 |
This method is specialized for ByteArray arguments - it will not handle |
|
2052 |
anything else." |
|
2053 |
||
2054 |
%{ /* NOCONTEXT */ |
|
2055 |
||
2056 |
REGISTER unsigned char *src, *dst; |
|
2057 |
REGISTER int wrun; |
|
2058 |
unsigned char *srcNext; |
|
2059 |
int bytesPerRow, mask, shift0, shift; |
|
2060 |
int w, h, hrun; |
|
2061 |
int srcBytes, dstBytes; |
|
2062 |
int bitsPerPixel; |
|
2063 |
int bits; |
|
2064 |
int ncells; |
|
2065 |
unsigned char *map; |
|
2066 |
||
8908 | 2067 |
if ((__qClass(self) == @global(ByteArray)) |
1133 | 2068 |
&& (__qClass(aByteArray) == @global(ByteArray)) |
563 | 2069 |
&& __isSmallInteger(nBitsPerPixel) |
2070 |
&& __bothSmallInteger(height, width)) { |
|
8908 | 2071 |
if ((aMapByteArray != nil) |
2072 |
&& (__Class(aMapByteArray) == @global(ByteArray))) { |
|
2073 |
map = __ByteArrayInstPtr(aMapByteArray)->ba_element; |
|
2074 |
} else { |
|
2075 |
map = (unsigned char *)0; |
|
2076 |
} |
|
2077 |
||
2078 |
bitsPerPixel = __intVal(nBitsPerPixel); |
|
2079 |
w = __intVal(width); |
|
2080 |
h = __intVal(height); |
|
2081 |
src = __ByteArrayInstPtr(self)->ba_element; |
|
2082 |
dst = __ByteArrayInstPtr(aByteArray)->ba_element; |
|
2083 |
switch (bitsPerPixel) { |
|
2084 |
case 1: |
|
2085 |
mask = 0x01; |
|
2086 |
break; |
|
2087 |
case 2: |
|
2088 |
mask = 0x03; |
|
2089 |
break; |
|
2090 |
case 4: |
|
2091 |
mask = 0x0F; |
|
2092 |
break; |
|
2093 |
case 8: |
|
2094 |
mask = 0xFF; |
|
2095 |
break; |
|
2096 |
default: |
|
13703 | 2097 |
console_printf("expandPixels: invalid depth\n"); |
8908 | 2098 |
goto fail; |
2099 |
} |
|
2100 |
ncells = mask + 1; |
|
2101 |
if (map) { |
|
2102 |
/* |
|
2103 |
* if a map is present, it must have the correct size |
|
2104 |
* (i.e. 2 raisedTo:nBitsPerPixel) |
|
2105 |
*/ |
|
2106 |
if ((__qSize(aMapByteArray) - OHDR_SIZE) < ncells) { |
|
13703 | 2107 |
console_printf("expandPixels: invalid map\n"); |
8908 | 2108 |
goto fail; |
2109 |
} |
|
2110 |
} |
|
2111 |
||
2112 |
bytesPerRow = (w * bitsPerPixel + 7) / 8; |
|
2113 |
shift0 = 8 - bitsPerPixel; |
|
2114 |
srcBytes = bytesPerRow * h; |
|
2115 |
dstBytes = w * h; |
|
2116 |
||
2117 |
if ((__byteArraySize(self) >= srcBytes) |
|
2118 |
&& (__byteArraySize(aByteArray) >= dstBytes)) { |
|
2119 |
for (hrun=h; hrun; hrun--) { |
|
2120 |
srcNext = src + bytesPerRow; |
|
2121 |
shift = shift0; |
|
2122 |
if (map) { |
|
2123 |
if (shift0 == 0) { |
|
2124 |
/* translate only */ |
|
2125 |
for (wrun=w; wrun; wrun--) { |
|
2126 |
bits = *src++; |
|
2127 |
*dst++ = map[bits]; |
|
2128 |
} |
|
2129 |
} else { |
|
2130 |
for (wrun=w; wrun; wrun--) { |
|
2131 |
if (shift == shift0) { |
|
2132 |
bits = *src++; |
|
2133 |
} |
|
2134 |
*dst++ = map[(bits >> shift) & mask]; |
|
2135 |
shift -= bitsPerPixel; |
|
2136 |
if (shift < 0) { |
|
2137 |
shift = shift0; |
|
2138 |
} |
|
2139 |
} |
|
2140 |
} |
|
2141 |
} else { |
|
2142 |
for (wrun=w; wrun; wrun--) { |
|
2143 |
if (shift == shift0) { |
|
2144 |
bits = *src++; |
|
2145 |
} |
|
2146 |
*dst++ = (bits >> shift) & mask; |
|
2147 |
shift -= bitsPerPixel; |
|
2148 |
if (shift < 0) { |
|
2149 |
shift = shift0; |
|
2150 |
} |
|
2151 |
} |
|
2152 |
} |
|
2153 |
src = srcNext; |
|
2154 |
} |
|
2155 |
RETURN ( self ); |
|
2156 |
} |
|
18655 | 2157 |
console_printf("expandPixels: buffer size: self:%"_ld_" expect at least:%"_ld_"\n", |
2158 |
(INT)(__byteArraySize(self)), (INT)srcBytes); |
|
2159 |
console_printf("expandPixels: buffer size: arg:%"_ld_" expect at least:%"_ld_"\n", |
|
2160 |
(INT)(__byteArraySize(aByteArray)), (INT)dstBytes); |
|
563 | 2161 |
} |
13703 | 2162 |
console_printf("expandPixels: invalid args\n"); |
1527
af119b16aa16
better handle the translate-only case in #expandPixels:
ca
parents:
1505
diff
changeset
|
2163 |
|
563 | 2164 |
fail: ; |
2866 | 2165 |
%}. |
563 | 2166 |
self primitiveFailed |
2167 |
||
2168 |
"Example1: |
|
2169 |
expand 1-bit-per-pixel bitmap into a 1byte-per-pixel byteArray |
|
2170 |
" |
|
2171 |
" |
|
2172 |
|inBits outBits| |
|
2173 |
||
8908 | 2174 |
inBits := #[2r11110000 |
2175 |
2r11001100 |
|
2176 |
2r01010101 |
|
2177 |
2r00001111]. |
|
563 | 2178 |
outBits := ByteArray new:(8*4). |
2179 |
inBits expandPixels:1 width:8 height:4 |
|
8908 | 2180 |
into:outBits mapping:nil. |
563 | 2181 |
outBits inspect |
2182 |
" |
|
2183 |
||
2184 |
"Example2: |
|
2185 |
expand bit-array into a byteArray, translating 0-bits to 99, |
|
2186 |
1-bits to 176. (just a stupid example) |
|
2187 |
" |
|
2188 |
" |
|
2189 |
|inBits outBits| |
|
2190 |
||
2191 |
inBits := #[2r11110000 2r11001100]. |
|
2192 |
outBits := ByteArray new:16. |
|
8908 | 2193 |
inBits expandPixels:1 width:16 height:1 |
2194 |
into:outBits mapping:#[99 176]. |
|
563 | 2195 |
outBits inspect |
2196 |
" |
|
4978 | 2197 |
|
2198 |
"This Can also be used to extract nibbles ..." |
|
2199 |
" |
|
2200 |
|inBits outBits| |
|
2201 |
||
8908 | 2202 |
inBits := #[ 16r12 16r34 16r56 16r78 16r9A 16rBC 16rDE 16rF0 ]. |
4978 | 2203 |
outBits := ByteArray new:(inBits size * 2). |
2204 |
inBits expandPixels:4 width:outBits size height:1 into:outBits mapping:nil. |
|
2205 |
outBits inspect |
|
2206 |
" |
|
2207 |
||
563 | 2208 |
! |
2209 |
||
2210 |
invert |
|
2211 |
"invert all bytes - used with image manipulations |
|
2212 |
written as a primitive for speed. |
|
2213 |
Q: is this really needed ?" |
|
2214 |
||
2215 |
%{ /* NOCONTEXT */ |
|
2216 |
REGISTER unsigned char *dst; |
|
2217 |
REGISTER unsigned long *ldst; |
|
2218 |
REGISTER int cnt; |
|
2219 |
||
1133 | 2220 |
if (__qClass(self) == @global(ByteArray)) { |
18655 | 2221 |
cnt = __byteArraySize(self); |
2222 |
dst = __ByteArrayInstPtr(self)->ba_element; |
|
2223 |
if (((INT)dst & (sizeof(long)-1)) == 0) { // aligned |
|
2224 |
ldst = (unsigned long *)dst; |
|
2225 |
while (cnt >= (sizeof(long))*4) { |
|
2226 |
ldst[0] = ~(ldst[0]); |
|
2227 |
ldst[1] = ~(ldst[1]); |
|
2228 |
ldst[2] = ~(ldst[2]); |
|
2229 |
ldst[3] = ~(ldst[3]); |
|
2230 |
ldst += 4; |
|
2231 |
cnt -= (sizeof(long))*4; |
|
2232 |
} |
|
2233 |
while (cnt >= sizeof(long)) { |
|
2234 |
*ldst = ~(*ldst); |
|
2235 |
ldst++; |
|
2236 |
cnt -= sizeof(long); |
|
2237 |
} |
|
2238 |
dst = (unsigned char *)ldst; |
|
2239 |
} |
|
2240 |
while (cnt--) { |
|
2241 |
*dst = ~(*dst); |
|
2242 |
dst++; |
|
2243 |
} |
|
2244 |
RETURN ( self ); |
|
563 | 2245 |
} |
4927 | 2246 |
%}. |
5439 | 2247 |
self bitBlitBytesFrom:1 to:self size withConstant:16rFF rule:#bitXor: |
4927 | 2248 |
|
2249 |
" |
|
2250 |
#[1 2 3 4 5 6 7 8 9 10] copy invert |
|
8908 | 2251 |
#[1 2 3 4 5 6 7 8 9 10] copy |
18655 | 2252 |
bitBlitBytesFrom:1 to:10 withConstant:16rFF rule:#bitXor: |
16621 | 2253 |
|
2254 |
|l| |
|
18655 | 2255 |
l := ByteArray fromHexString:'0102030405060708090a0b0c0d0e0f1112131415161718191a1b1c1d1e1f'. |
16621 | 2256 |
Time millisecondsToRun:[ |
18655 | 2257 |
1000000 timesRepeat:[ l invert ]. |
2258 |
] |
|
4927 | 2259 |
" |
563 | 2260 |
! |
2261 |
||
2262 |
reverse |
|
8908 | 2263 |
"reverse the order of my elements inplace - |
7338 | 2264 |
WARNING: this is a destructive operation, which modifies the receiver. |
12879 | 2265 |
Please use reversed (with a d) for a functional version. |
7338 | 2266 |
Written as a primitive for speed on image manipulations (mirror)" |
563 | 2267 |
|
2268 |
%{ /* NOCONTEXT */ |
|
2269 |
||
2270 |
REGISTER unsigned char *p1, *p2; |
|
2271 |
REGISTER int cnt; |
|
2272 |
REGISTER unsigned t; |
|
2273 |
OBJ cls; |
|
2274 |
||
1133 | 2275 |
if (__qClass(self) == @global(ByteArray)) { |
12879 | 2276 |
cnt = __byteArraySize(self); |
2277 |
p1 = __ByteArrayInstPtr(self)->ba_element; |
|
2278 |
p2 = p1 + cnt - 1; |
|
4924
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2279 |
|
4927 | 2280 |
#if defined(__BSWAP) |
12879 | 2281 |
/* |
2282 |
* can we use the bswap instruction ? |
|
2283 |
* notice - not all CPUs have it (the HAS_BSWAP checks this). |
|
2284 |
*/ |
|
2285 |
if (__HAS_BSWAP() |
|
2286 |
&& ((cnt & 3) == 0)) { |
|
2287 |
unsigned int *ip1, *ip2; |
|
2288 |
||
2289 |
ip1 = (unsigned int *)p1; |
|
2290 |
ip2 = (unsigned int *)(p2 - 3); |
|
2291 |
||
2292 |
ip2 -= 7; |
|
2293 |
while (ip1 <= ip2) { |
|
2294 |
int t1, t2; |
|
2295 |
||
2296 |
t1 = ip1[0]; |
|
2297 |
t2 = ip2[7]; |
|
2298 |
ip2[7] = __BSWAP(t1); |
|
2299 |
ip1[0] = __BSWAP(t2); |
|
2300 |
||
2301 |
t1 = ip1[1]; |
|
2302 |
t2 = ip2[6]; |
|
2303 |
ip2[6] = __BSWAP(t1); |
|
2304 |
ip1[1] = __BSWAP(t2); |
|
2305 |
||
2306 |
t1 = ip1[2]; |
|
2307 |
t2 = ip2[5]; |
|
2308 |
ip2[5] = __BSWAP(t1); |
|
2309 |
ip1[2] = __BSWAP(t2); |
|
2310 |
||
2311 |
t1 = ip1[3]; |
|
2312 |
t2 = ip2[4]; |
|
2313 |
ip2[4] = __BSWAP(t1); |
|
2314 |
ip1[3] = __BSWAP(t2); |
|
2315 |
||
2316 |
ip1 += 4; |
|
2317 |
ip2 -= 4; |
|
2318 |
} |
|
2319 |
ip2 += 7; |
|
2320 |
||
2321 |
while (ip1 < ip2) { |
|
2322 |
int t; |
|
2323 |
||
2324 |
t = __BSWAP(*ip1); |
|
2325 |
*ip1++ = __BSWAP(*ip2); |
|
2326 |
*ip2-- = t; |
|
2327 |
} |
|
2328 |
||
2329 |
if (ip1 == ip2) { |
|
2330 |
int t; |
|
2331 |
t = *ip1; |
|
2332 |
t = __BSWAP(t); |
|
2333 |
*ip1 = t; |
|
2334 |
} |
|
2335 |
RETURN ( self ); |
|
2336 |
} |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
2337 |
#endif /* __i386__ && __GNUC__ */ |
4927 | 2338 |
|
12879 | 2339 |
p2 -= 7; |
2340 |
while (p1 <= p2) { |
|
2341 |
t = p1[0]; |
|
2342 |
p1[0] = p2[7]; |
|
2343 |
p2[7] = t; |
|
2344 |
||
2345 |
t = p1[1]; |
|
2346 |
p1[1] = p2[6]; |
|
2347 |
p2[6] = t; |
|
2348 |
||
2349 |
t = p1[2]; |
|
2350 |
p1[2] = p2[5]; |
|
2351 |
p2[5] = t; |
|
2352 |
||
2353 |
t = p1[3]; |
|
2354 |
p1[3] = p2[4]; |
|
2355 |
p2[4] = t; |
|
2356 |
||
2357 |
p1 += 4; |
|
2358 |
p2 -= 4; |
|
2359 |
} |
|
2360 |
p2 += 7; |
|
2361 |
||
2362 |
while (p1 < p2) { |
|
2363 |
t = *p1; |
|
2364 |
*p1++ = *p2; |
|
2365 |
*p2-- = t; |
|
2366 |
} |
|
2367 |
RETURN ( self ); |
|
563 | 2368 |
} |
2369 |
%}. |
|
2370 |
^ super reverse |
|
2585 | 2371 |
|
2372 |
" |
|
8908 | 2373 |
#[1 2 3 4 5] reverse |
2374 |
#[1 2 3 4] reverse |
|
2375 |
#[1 2 3 4 5] reverse |
|
2376 |
#[1 2 3 4 5 6] reverse |
|
2377 |
#[1 2 3 4 5 6 7] reverse |
|
2378 |
#[1 2 3 4 5 6 7 8] reverse |
|
2379 |
#[1 2 3 4 5 6 7 8 9] reverse |
|
2380 |
#[1 2 3 4 5 6 7 8 9 10] reverse |
|
2381 |
#[1 2 3 4 5 6 7 8 9 10 11 12] reverse |
|
2382 |
#[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16] reverse |
|
2383 |
#[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20] reverse |
|
2384 |
(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
|
2385 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2386 |
1 to:1024 do:[:i| |
12879 | 2387 |
|bytes test rBytes| |
2388 |
||
2389 |
bytes := ((1 to:i) asArray collect:[:i | i bitAnd:255]) asByteArray. |
|
2390 |
test := ((i to:1 by:-1) asArray collect:[:i | i bitAnd:255]) asByteArray. |
|
2391 |
rBytes := bytes copy. |
|
2392 |
rBytes reverse ~= test ifTrue:[ |
|
2393 |
self halt |
|
2394 |
]. |
|
2395 |
rBytes := bytes copy. |
|
2396 |
rBytes reverse reverse ~= bytes ifTrue:[ |
|
2397 |
self halt |
|
2398 |
] |
|
4924
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2399 |
]. |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2400 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2401 |
Time millisecondsToRun:[ |
12879 | 2402 |
10000000 timesRepeat:[ |
2403 |
#[1 2 3 4 5 6 7 8] reverse |
|
2404 |
] |
|
8908 | 2405 |
] |
4924
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2406 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2407 |
|b| |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2408 |
b := (0 to:255) asByteArray. |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2409 |
Time millisecondsToRun:[ |
12879 | 2410 |
10000000 timesRepeat:[ |
2411 |
b reverse |
|
2412 |
] |
|
8908 | 2413 |
] |
2585 | 2414 |
" |
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2415 |
! |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2416 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2417 |
swapBytes |
8908 | 2418 |
"swap bytes inplace - |
18655 | 2419 |
Expects that the receiver has an even number of bytes; |
15274 | 2420 |
if not, only the pairs excluding the last byte are swapped. |
2421 |
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
|
2422 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2423 |
%{ /* NOCONTEXT */ |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2424 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2425 |
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
|
2426 |
REGISTER int cnt; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2427 |
REGISTER unsigned t; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2428 |
|
1133 | 2429 |
if (__qClass(self) == @global(ByteArray)) { |
18655 | 2430 |
cnt = __byteArraySize(self); |
2431 |
cnt = cnt & ~1; /* make it even */ |
|
2432 |
p = __ByteArrayInstPtr(self)->ba_element; |
|
2433 |
||
2434 |
while (cnt >= sizeof(INT)) { |
|
2435 |
unsigned INT i = ((unsigned INT *)p)[0]; |
|
15274 | 2436 |
|
2437 |
#if __POINTER_SIZE__ == 8 |
|
18655 | 2438 |
i = ((i>>8) & 0x00FF00FF00FF00FF) | ((i & 0x00FF00FF00FF00FF) << 8); |
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
2439 |
#else |
18655 | 2440 |
i = ((i>>8) & 0x00FF00FF) | ((i & 0x00FF00FF) << 8); |
15274 | 2441 |
#endif /* __POINTER_SIZE__ */ |
18655 | 2442 |
((unsigned INT *)p)[0] = i; |
2443 |
p += sizeof(INT); |
|
2444 |
cnt -= sizeof(INT); |
|
2445 |
} |
|
2446 |
while (cnt > 0) { |
|
2447 |
unsigned short s; |
|
2448 |
||
2449 |
s = ((unsigned short *)p)[0]; |
|
2450 |
s = (s >> 8) | (s << 8); |
|
2451 |
((unsigned short *)p)[0] = s; |
|
2452 |
p += 2; |
|
2453 |
cnt -= 2; |
|
2454 |
} |
|
2455 |
RETURN ( self ); |
|
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2456 |
} |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2457 |
%}. |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2458 |
^ 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
|
2459 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2460 |
" |
15274 | 2461 |
#[1 2 3 4 5 6 7 8 9 10] copy swapBytes -> #[2 1 4 3 6 5 8 7 10 9] |
2462 |
#[1 2 3 4 5 6 7 8 9 10 11] copy swapBytes -> #[2 1 4 3 6 5 8 7 10 9 11] |
|
18655 | 2463 |
#[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
|
2464 |
" |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2465 |
! |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2466 |
|
4983 | 2467 |
swapIndex:i1 and:i2 |
2468 |
"spap the bytes with i1 and i2" |
|
2469 |
||
2470 |
%{ /* NOCONTEXT */ |
|
2471 |
||
2472 |
REGISTER unsigned char *p; |
|
2473 |
unsigned int __i1, __i2; |
|
15330 | 2474 |
int sz; |
4983 | 2475 |
unsigned int t; |
2476 |
||
2477 |
if (__qClass(self) == @global(ByteArray) && __bothSmallInteger(i1, i2)) { |
|
18655 | 2478 |
__i1 = __intVal(i1) - 1; |
2479 |
__i2 = __intVal(i2) - 1; |
|
2480 |
sz = __byteArraySize(self); |
|
2481 |
p = __ByteArrayInstPtr(self)->ba_element; |
|
2482 |
if (__i1 < sz && __i2 < sz) { |
|
2483 |
t = p[__i1]; |
|
2484 |
p[__i1] = p[__i2]; |
|
2485 |
p[__i2] = t; |
|
2486 |
} |
|
2487 |
RETURN ( self ); |
|
4983 | 2488 |
} |
2489 |
%}. |
|
11265 | 2490 |
^ super swapIndex:i1 and:i2 "/ rubbish - there is no one currently |
4983 | 2491 |
|
2492 |
" |
|
8908 | 2493 |
#[1 2 3 4 5 6 7 8 9 10] copy swapIndex:1 and:10 |
2494 |
#[1 2 3 4 5 6 7 8 9 10 11] copy swapIndex:5 and:6 |
|
4983 | 2495 |
" |
2496 |
! |
|
2497 |
||
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2498 |
swapLongs |
8908 | 2499 |
"swap long bytes inplace |
4927 | 2500 |
- any partial longs at the end are not swapped." |
2501 |
||
2502 |
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
|
2503 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2504 |
" |
8908 | 2505 |
#[1 2 3 4 5 6 7 8 9] copy swapLongs |
2506 |
#[1 2 3 4 5 6 7 8 9 10] copy swapLongs |
|
2507 |
#[1 2 3 4 5 6 7 8 9 10 11] copy swapLongs |
|
2508 |
#[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
|
2509 |
" |
4365 | 2510 |
! |
2511 |
||
2512 |
swapLongsFrom:startIndex to:endIndex |
|
8908 | 2513 |
"swap longs inplace |
2514 |
- 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
|
2515 |
Swapping is from startIndex to (exclusiv) endIndex; |
3d481db099ec
oops - swapLongsFrom:to: was wrong
Claus Gittinger <cg@exept.de>
parents:
4365
diff
changeset
|
2516 |
indexing starts at 1." |
4365 | 2517 |
|
2518 |
%{ /* NOCONTEXT */ |
|
2519 |
||
2520 |
REGISTER unsigned char *p; |
|
2521 |
REGISTER int limit; |
|
2522 |
REGISTER unsigned t; |
|
2523 |
||
2524 |
if ((__qClass(self) == @global(ByteArray)) |
|
2525 |
&& __isSmallInteger(startIndex) |
|
2526 |
&& __isSmallInteger(endIndex)) { |
|
4969 | 2527 |
int __idx = __intVal(startIndex); |
2528 |
int __endIdx = __intVal(endIndex); |
|
2529 |
||
2530 |
limit = __byteArraySize(self); |
|
2531 |
if (__endIdx < limit) { |
|
2532 |
limit = __endIdx; |
|
2533 |
} |
|
2534 |
p = __ByteArrayInstPtr(self)->ba_element; |
|
2535 |
p = p + __idx - 1; |
|
2536 |
||
2537 |
limit = limit - 4 + 1; |
|
4927 | 2538 |
|
2539 |
#if defined(__BSWAP) |
|
4969 | 2540 |
/* |
2541 |
* can we use the bswap instruction ? |
|
2542 |
* notice - not all CPUs have it (the HAS_BSWAP checks this). |
|
2543 |
*/ |
|
2544 |
if (__HAS_BSWAP() |
|
2545 |
&& (((unsigned int)p & 3) == 0)) { |
|
2546 |
unsigned int *ip; |
|
2547 |
ip = (unsigned int *)p; |
|
2548 |
||
2549 |
while (__idx <= limit) { |
|
2550 |
*ip = __BSWAP(*ip); |
|
2551 |
ip++; |
|
2552 |
__idx += 4; |
|
2553 |
} |
|
2554 |
RETURN (self); |
|
2555 |
} |
|
4927 | 2556 |
#endif /* __BSWAP */ |
2557 |
||
4969 | 2558 |
while (__idx <= limit) { |
2559 |
t = p[0]; |
|
2560 |
p[0] = p[3]; |
|
2561 |
p[3] = t; |
|
2562 |
t = p[1]; |
|
2563 |
p[1] = p[2]; |
|
2564 |
p[2] = t; |
|
2565 |
p += 4; |
|
2566 |
__idx += 4; |
|
2567 |
} |
|
2568 |
RETURN ( self ); |
|
4365 | 2569 |
} |
2570 |
%}. |
|
2571 |
^ super swapLongsFrom:startIndex to:endIndex "/ rubbish - there is no one currenly |
|
2572 |
||
2573 |
" |
|
8908 | 2574 |
#[1 2 3 4 5 6 7 8 9] copy swapLongsFrom:1 to:3 |
2575 |
#[1 2 3 4 5 6 7 8 9] copy swapLongsFrom:1 to:4 |
|
2576 |
#[1 2 3 4 5 6 7 8 9] copy swapLongsFrom:1 to:5 |
|
2577 |
#[1 2 3 4 5 6 7 8 9] copy swapLongsFrom:1 to:6 |
|
2578 |
#[1 2 3 4 5 6 7 8 9] copy swapLongsFrom:1 to:7 |
|
2579 |
#[1 2 3 4 5 6 7 8 9] copy swapLongsFrom:1 to:8 |
|
2580 |
#[1 2 3 4 5 6 7 8 9 10] copy swapLongsFrom:1 to:11 |
|
2581 |
#[1 2 3 4 5 6 7 8 9 10 11] copy swapLongsFrom:1 to:12 |
|
2582 |
#[1 2 3 4 5 6 7 8 9 10 11 12] copy swapLongsFrom:1 to:13 |
|
2583 |
#[1 2 3 4 5 6 7 8 9] copy swapLongsFrom:5 to:10 |
|
4365 | 2584 |
" |
1 | 2585 |
! ! |
550 | 2586 |
|
10028 | 2587 |
|
550 | 2588 |
!ByteArray methodsFor:'printing & storing'! |
2589 |
||
13078
7934a86b395b
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
12932
diff
changeset
|
2590 |
displayOn:aGCOrStream |
1244 | 2591 |
"return a printed representation of the receiver for displaying" |
2592 |
||
17535 | 2593 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
2594 |
"/ ST/X (and some old ST80's) mean: draw-yourself on a GC. |
|
18596 | 2595 |
|cls| |
2596 |
||
2597 |
cls := self class. |
|
2598 |
((cls == ByteArray or:[cls == ImmutableByteArray]) and:[aGCOrStream isStream]) ifTrue:[ |
|
18655 | 2599 |
self storeOn:aGCOrStream. |
2600 |
^ self |
|
6513
d7bc1d1dab4b
displayString fix for subclasses
Claus Gittinger <cg@exept.de>
parents:
6125
diff
changeset
|
2601 |
]. |
13078
7934a86b395b
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
12932
diff
changeset
|
2602 |
^ super displayOn:aGCOrStream |
563 | 2603 |
|
2604 |
"Created: 25.10.1995 / 13:33:26 / cg" |
|
1244 | 2605 |
"Modified: 22.4.1996 / 12:54:06 / cg" |
563 | 2606 |
! |
2607 |
||
5019
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2608 |
printOn:aStream |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2609 |
"append a printed representation to aStream" |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2610 |
|
12487 | 2611 |
|cls| |
2612 |
||
2613 |
"/ care for subclasses |
|
12879 | 2614 |
((cls := self class) == ByteArray or:[cls == ImmutableByteArray]) ifTrue:[ |
2615 |
aStream nextPutAll:'#['. |
|
2616 |
self |
|
2617 |
do:[:byte | byte printOn:aStream] |
|
2618 |
separatedBy:[aStream space]. |
|
2619 |
aStream nextPut:$]. |
|
2620 |
^ self |
|
5019
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2621 |
]. |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2622 |
^ super printOn:aStream |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2623 |
|
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2624 |
" |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2625 |
#[1 2 3 4 5] printOn:Transcript |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2626 |
|
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2627 |
|
8908 | 2628 |
#[1 2 3 4 5] storeString |
2629 |
#[1 2 3 4 5] displayString |
|
2630 |
#[1 2 3 4 5] printString |
|
5019
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2631 |
" |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2632 |
|
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2633 |
"Modified: / 12.9.1997 / 22:11:33 / cg" |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2634 |
"Modified: / 17.3.1999 / 17:01:31 / stefan" |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2635 |
! |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2636 |
|
4050 | 2637 |
printOn:aStream base:radix |
2638 |
"append a printed representation to aStream in the given number base." |
|
2639 |
||
6125 | 2640 |
^ self printOn:aStream base:radix showRadix:false |
2641 |
||
2642 |
" |
|
2643 |
#[1 2 3 4 5] printOn:Transcript base:2 |
|
2644 |
'Hello World' printOn:Transcript base:2 |
|
2645 |
||
8908 | 2646 |
#[1 2 3 4 5] storeString |
2647 |
#[1 2 3 4 5] displayString |
|
2648 |
#[1 2 3 4 5] printString |
|
6125 | 2649 |
" |
2650 |
||
2651 |
"Modified: / 17.3.1999 / 17:01:31 / stefan" |
|
2652 |
"Modified: / 31.10.2001 / 09:43:56 / cg" |
|
2653 |
! |
|
2654 |
||
2655 |
printOn:aStream base:radix showRadix:showRadix |
|
2656 |
"append a printed representation to aStream in the given number base." |
|
2657 |
||
12487 | 2658 |
|cls| |
2659 |
||
2660 |
((cls := self class) == ByteArray or:[cls == ImmutableByteArray]) ifTrue:[ |
|
12879 | 2661 |
"/ care for subclasses |
2662 |
aStream nextPutAll:'#['. |
|
2663 |
self |
|
2664 |
do:[:byte | byte printOn:aStream base:radix showRadix:showRadix] |
|
2665 |
separatedBy:[aStream space]. |
|
2666 |
aStream nextPut:$]. |
|
2667 |
^ self |
|
4050 | 2668 |
]. |
5019
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2669 |
^ self printOn:aStream |
4050 | 2670 |
|
2671 |
" |
|
5019
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2672 |
#[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
|
2673 |
'Hello World' printOn:Transcript base:2 |
4050 | 2674 |
|
8908 | 2675 |
#[1 2 3 4 5] storeString |
2676 |
#[1 2 3 4 5] displayString |
|
2677 |
#[1 2 3 4 5] printString |
|
4050 | 2678 |
" |
2679 |
||
2680 |
"Modified: / 12.9.1997 / 22:11:33 / cg" |
|
2681 |
"Modified: / 17.3.1999 / 17:01:31 / stefan" |
|
6125 | 2682 |
"Created: / 31.10.2001 / 09:43:41 / cg" |
4050 | 2683 |
! |
2684 |
||
563 | 2685 |
storeOn:aStream |
2686 |
"append a printed representation from which the receiver can be |
|
2687 |
reconstructed to aStream. (reimplemented to make it look better)" |
|
2688 |
||
12487 | 2689 |
|cls| |
2690 |
||
12879 | 2691 |
((cls := self class) == ByteArray or:[cls == ImmutableByteArray]) ifTrue:[ |
2692 |
"/ care for subclasses |
|
2693 |
aStream nextPutAll:'#['. |
|
2694 |
self |
|
2695 |
do:[:byte | byte storeOn:aStream] |
|
2696 |
separatedBy:[aStream space]. |
|
2697 |
aStream nextPutAll:']'. |
|
2698 |
^ self |
|
563 | 2699 |
]. |
2700 |
^ super storeOn:aStream |
|
2701 |
||
2702 |
" |
|
8908 | 2703 |
#[1 2 3 4 5] storeOn:Transcript |
2704 |
||
2705 |
#[1 2 3 4 5] storeString |
|
2706 |
#[1 2 3 4 5] displayString |
|
2707 |
#[1 2 3 4 5] printString |
|
563 | 2708 |
" |
1082 | 2709 |
|
2915 | 2710 |
"Modified: 12.9.1997 / 22:11:33 / cg" |
550 | 2711 |
! ! |
2712 |
||
7822 | 2713 |
!ByteArray methodsFor:'queries'! |
1082 | 2714 |
|
16804 | 2715 |
containsNon7BitAscii |
2716 |
"return true, if any byte in the receiver has the 7th bit on. |
|
2717 |
This my look as a too specific operation to be put here, |
|
2718 |
put it is very helpful for UTF8 string reading (Java class reader), |
|
2719 |
to quickly determine, if UTF8 decoding is needed or not. |
|
18596 | 2720 |
As most strings in a class file are in fact only containing 7bit ascii, |
16804 | 2721 |
this should speedup class file reading considerably" |
2722 |
||
16805 | 2723 |
%{ /* NOCONTEXT */ |
16804 | 2724 |
REGISTER unsigned char *cp; |
2725 |
REGISTER unsigned char *endP; |
|
2726 |
||
18611 | 2727 |
if (__isByteArrayLike(self)) { |
18655 | 2728 |
cp = &(__ByteArrayInstPtr(self)->ba_element[0]); |
2729 |
endP = cp + __byteArraySize(self); |
|
16806 | 2730 |
#if __POINTER_SIZE__ == 8 |
18655 | 2731 |
while (cp+8 < endP) { |
2732 |
if ( ((unsigned INT *)cp)[0] & 0x8080808080808080) RETURN( true ); |
|
2733 |
cp += 8; |
|
2734 |
} |
|
16806 | 2735 |
#endif |
18655 | 2736 |
while (cp+4 < endP) { |
2737 |
if ( ((unsigned int *)cp)[0] & 0x80808080) RETURN( true ); |
|
2738 |
cp += 4; |
|
2739 |
} |
|
2740 |
while (cp < endP) { |
|
2741 |
if (*cp++ & 0x80) RETURN( true ); |
|
2742 |
} |
|
2743 |
RETURN ( false ); |
|
16804 | 2744 |
} |
2745 |
%} |
|
2746 |
. |
|
2747 |
^ self contains:[:b | b bitTest:16r80]. |
|
2748 |
||
2749 |
" |
|
2750 |
#[1 2 3 1 2 3 1 2 127 ] containsNon7BitAscii |
|
2751 |
#[1 2 3 1 2 3 1 2 250 251 250 251 255] containsNon7BitAscii |
|
2752 |
" |
|
2753 |
! |
|
2754 |
||
18966 | 2755 |
containsNon8BitElements |
2756 |
"return true, if one of my elements is larger than a single byte. |
|
2757 |
Per definition not." |
|
2758 |
||
2759 |
^ false. |
|
2760 |
! |
|
2761 |
||
563 | 2762 |
max |
2763 |
"return the maximum value in the receiver - |
|
8908 | 2764 |
redefined to speedup image processing and sound-player |
563 | 2765 |
(which need a fast method for this on byteArrays)" |
550 | 2766 |
|
563 | 2767 |
%{ /* NOCONTEXT */ |
550 | 2768 |
|
2769 |
REGISTER unsigned char *cp; |
|
563 | 2770 |
REGISTER int index, max; |
2771 |
int len; |
|
550 | 2772 |
|
14135 | 2773 |
if (__isByteArrayLike(self)) { |
18655 | 2774 |
max = 0; |
2775 |
index = 0; |
|
2776 |
len = __qSize(self) - OHDR_SIZE; |
|
2777 |
cp = &(__ByteArrayInstPtr(self)->ba_element[0]); |
|
2778 |
||
2779 |
while (++index <= len) { |
|
2780 |
unsigned int byte; |
|
2781 |
||
2782 |
byte = *cp; |
|
2783 |
cp++; |
|
2784 |
if (byte > max) { |
|
2785 |
max = byte; |
|
2786 |
if (byte == 255) break; |
|
2787 |
} |
|
2788 |
} |
|
2789 |
RETURN ( __mkSmallInteger(max) ); |
|
550 | 2790 |
} |
563 | 2791 |
%}. |
2792 |
^ super max |
|
550 | 2793 |
|
2794 |
" |
|
8908 | 2795 |
#[1 2 3 1 2 3 1 2 19] max |
550 | 2796 |
" |
5554 | 2797 |
! |
550 | 2798 |
|
2799 |
usageCounts |
|
2800 |
"return an array filled with value-counts - |
|
2801 |
This is needed in the bitmap/image classes to get info on color usage. |
|
2802 |
(i.e. to build up a histogram of color usage within an image)." |
|
2803 |
||
2804 |
|counts| |
|
2805 |
||
2806 |
counts := Array basicNew:256. |
|
2807 |
||
2808 |
%{ /* STACK: 2000 */ |
|
2809 |
||
2810 |
REGISTER unsigned char *cp; |
|
2811 |
REGISTER int nByte; |
|
2812 |
REGISTER int index; |
|
8913 | 2813 |
INT icounts[256]; |
550 | 2814 |
|
12768
4c56c6dd923d
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12719
diff
changeset
|
2815 |
if (__isByteArrayLike(self) && __isArray(counts)) { |
12879 | 2816 |
/* |
2817 |
* zero counts |
|
2818 |
*/ |
|
2819 |
for (index=0; index<256; index++) { |
|
2820 |
icounts[index] = 0; |
|
2821 |
} |
|
2822 |
||
2823 |
/* |
|
2824 |
* count |
|
2825 |
*/ |
|
2826 |
nByte = __qSize(self) - OHDR_SIZE; |
|
2827 |
cp = &(__ByteArrayInstPtr(self)->ba_element[0]); |
|
2828 |
while (nByte--) { |
|
2829 |
icounts[*cp++]++; |
|
2830 |
} |
|
2831 |
||
2832 |
/* |
|
2833 |
* make it real counts |
|
2834 |
*/ |
|
2835 |
for (index=0; index<256; index++) { |
|
2836 |
__ArrayInstPtr(counts)->a_element[index] = __mkSmallInteger(icounts[index]); |
|
2837 |
} |
|
2838 |
RETURN ( counts ); |
|
550 | 2839 |
} |
2840 |
%} |
|
2841 |
. |
|
2842 |
self primitiveFailed |
|
2843 |
||
2844 |
" |
|
8908 | 2845 |
#[1 2 3 1 2 3 1 2 250 251 250 251 255] usageCounts |
550 | 2846 |
" |
2847 |
! |
|
2848 |
||
563 | 2849 |
usedValues |
2850 |
"return a new ByteArray with all used values (actually a kind of Set); |
|
8908 | 2851 |
This is needed specially in the bitmap/Imageclasses to find used colors |
2866 | 2852 |
of an image." |
550 | 2853 |
|
563 | 2854 |
%{ /* STACK: 400 */ |
550 | 2855 |
|
2856 |
REGISTER unsigned char *cp; |
|
2866 | 2857 |
unsigned char *endp; |
563 | 2858 |
REGISTER int len; |
2890 | 2859 |
OBJ result; |
2866 | 2860 |
union { |
12879 | 2861 |
unsigned char flags[256]; |
2862 |
int toForceAlignmentOfFlags; |
|
2866 | 2863 |
} f; |
2864 |
||
2865 |
#ifdef TO_BE_MEASURED |
|
2866 |
int coverage = 0; |
|
2867 |
#endif |
|
550 | 2868 |
|
12768
4c56c6dd923d
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12719
diff
changeset
|
2869 |
if (__isByteArrayLike(self)) { |
12879 | 2870 |
memset(f.flags, 0, sizeof(f.flags)); |
2871 |
len = __qSize(self) - OHDR_SIZE; |
|
2872 |
cp = &(__ByteArrayInstPtr(self)->ba_element[0]); |
|
2873 |
||
2874 |
/* for each used byte, set flag */ |
|
2875 |
while (len > 0) { |
|
2866 | 2876 |
#ifdef TO_BE_MEASURED |
12879 | 2877 |
unsigned byte; |
2878 |
||
2879 |
byte = *cp; |
|
2880 |
if (! f.flags[byte]) { |
|
2881 |
f.flags[byte] = 1; |
|
2882 |
coverage++; |
|
2883 |
if (coverage == 256) { |
|
2884 |
/* no need to scan rest */ |
|
2885 |
break; |
|
2886 |
} |
|
2887 |
} |
|
2866 | 2888 |
#else |
12879 | 2889 |
f.flags[*cp] = 1; |
2866 | 2890 |
#endif |
12879 | 2891 |
cp++; |
2892 |
len--; |
|
2893 |
} |
|
2894 |
||
2895 |
/* count 1's */ |
|
2896 |
len = 0; |
|
2897 |
for (cp=f.flags, endp=f.flags+256; cp < endp;) { |
|
2898 |
if ( *((unsigned int *)cp)) { |
|
2899 |
if (cp[0]) len++; |
|
2900 |
if (cp[1]) len++; |
|
2901 |
if (cp[2]) len++; |
|
2902 |
if (cp[3]) len++; |
|
2903 |
} |
|
2904 |
cp += 4; |
|
2905 |
} |
|
2906 |
||
2907 |
/* create ByteArray of used values */ |
|
2908 |
result = __BYTEARRAY_UNINITIALIZED_NEW_INT(len); |
|
2909 |
if (result) { |
|
2910 |
cp = __ByteArrayInstPtr(result)->ba_element; |
|
2911 |
for (len=0; len < 256; len++) { |
|
2912 |
if (f.flags[len]) |
|
2913 |
*cp++ = len; |
|
2914 |
} |
|
2915 |
RETURN ( result ); |
|
2916 |
} |
|
550 | 2917 |
} |
2866 | 2918 |
%}. |
2919 |
^ self asIdentitySet asByteArray |
|
550 | 2920 |
|
2921 |
" |
|
8908 | 2922 |
#[1 2 3 1 2 3 1 2 3 1 2 3 4 5 6 4 5 6] usedValues |
550 | 2923 |
" |
2924 |
! ! |
|
2925 |
||
18966 | 2926 |
|
7822 | 2927 |
!ByteArray methodsFor:'searching'! |
2928 |
||
2929 |
indexOf:aByte startingAt:start |
|
2930 |
"return the index of the first occurrence of the argument, aByte |
|
2931 |
in the receiver starting at start, anInteger; return 0 if not found. |
|
2932 |
- reimplemented here for speed" |
|
2933 |
||
2934 |
%{ /* NOCONTEXT */ |
|
2935 |
||
2936 |
REGISTER unsigned char *cp; |
|
8913 | 2937 |
REGISTER INT index; |
2938 |
REGISTER int byteValue; |
|
7822 | 2939 |
REGISTER int len; |
2940 |
OBJ cls; |
|
2941 |
||
2942 |
if (__isSmallInteger(aByte) &&__isBytes(self)) { |
|
18655 | 2943 |
byteValue = __intVal(aByte); |
2944 |
||
2945 |
if (byteValue & ~0xFF /* i.e. (byteValue < 0) || (byteValue > 255) */) { |
|
2946 |
/* |
|
2947 |
* searching for something which cannot be found |
|
2948 |
*/ |
|
2949 |
RETURN ( __mkSmallInteger(0) ); |
|
2950 |
} |
|
2951 |
||
2952 |
if (__isSmallInteger(start)) { |
|
2953 |
index = __intVal(start); |
|
2954 |
len = __byteArraySize(self); |
|
2955 |
cp = __ByteArrayInstPtr(self)->ba_element; |
|
2956 |
if ((cls = __qClass(self)) != @global(ByteArray)) { |
|
2957 |
int nInst; |
|
2958 |
||
2959 |
nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
2960 |
cp += nInst; |
|
2961 |
len -= nInst; |
|
2962 |
} |
|
2963 |
cp += index - 1; |
|
14382
dcbff20f0a14
changed: #indexOf:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14135
diff
changeset
|
2964 |
#ifdef __UNROLL_LOOPS__ |
18655 | 2965 |
while ((index+4) < len) { |
2966 |
if (cp[0] == byteValue) { RETURN ( __mkSmallInteger(index) ); } |
|
2967 |
if (cp[1] == byteValue) { RETURN ( __mkSmallInteger(index+1) ); } |
|
2968 |
if (cp[2] == byteValue) { RETURN ( __mkSmallInteger(index+2) ); } |
|
2969 |
if (cp[3] == byteValue) { RETURN ( __mkSmallInteger(index+3) ); } |
|
2970 |
index += 4; |
|
2971 |
cp += 4; |
|
2972 |
} |
|
14382
dcbff20f0a14
changed: #indexOf:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14135
diff
changeset
|
2973 |
#endif |
18655 | 2974 |
while (index <= len) { |
2975 |
if (*cp == byteValue) { |
|
2976 |
RETURN ( __mkSmallInteger(index) ); |
|
2977 |
} |
|
2978 |
index++; |
|
2979 |
cp++; |
|
2980 |
} |
|
2981 |
RETURN ( __mkSmallInteger(0) ); |
|
2982 |
} |
|
7822 | 2983 |
} |
2984 |
%}. |
|
2985 |
^ super indexOf:aByte startingAt:start |
|
2986 |
||
2987 |
" |
|
14135 | 2988 |
#[1 2 3 4 5 6 7 8 9 0 1 2 3 4 5] indexOf:0 startingAt:1 |
7822 | 2989 |
" |
2990 |
! ! |
|
2991 |
||
18966 | 2992 |
|
5554 | 2993 |
!ByteArray methodsFor:'testing'! |
2994 |
||
2995 |
isByteArray |
|
2996 |
"return true, if the receiver is some kind of bytearray; |
|
2997 |
true is returned here - the method is redefined from Object." |
|
2998 |
||
2999 |
^ true |
|
3000 |
||
3001 |
||
3002 |
! |
|
3003 |
||
3004 |
isLiteral |
|
3005 |
"return true, if the receiver can be used as a literal constant in ST syntax |
|
3006 |
(i.e. can be used in constant arrays)" |
|
3007 |
||
3008 |
"/ no, simply returning true here is a mistake: |
|
8908 | 3009 |
"/ it could be a subclass of ByteArray |
5554 | 3010 |
"/ (of which the compiler does not know at all ...) |
3011 |
||
3012 |
^ self class == ByteArray |
|
3013 |
||
3014 |
"Modified: 22.4.1996 / 12:55:30 / cg" |
|
3015 |
! ! |
|
3016 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3017 |
!ByteArray class methodsFor:'documentation'! |
628 | 3018 |
|
3019 |
version |
|
18584 | 3020 |
^ '$Header$' |
12091 | 3021 |
! |
3022 |
||
3023 |
version_CVS |
|
18584 | 3024 |
^ '$Header$' |
628 | 3025 |
! ! |
15274 | 3026 |