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