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