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