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