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