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