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