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