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