author | Claus Gittinger <cg@exept.de> |
Wed, 15 Aug 2018 23:30:26 +0200 | |
changeset 23286 | 12a32821f2f1 |
parent 22883 | 711de6b11ff1 |
child 23626 | a08a4299109d |
permissions | -rw-r--r-- |
21377 | 1 |
"{ Encoding: utf8 }" |
2 |
||
1 | 3 |
" |
4 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
22261 | 5 |
All Rights Reserved |
1 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
6492 | 14 |
"{ Package: 'stx:libbasic' }" |
15 |
||
18284 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
18 |
ArrayedCollection subclass:#UninterpretedBytes |
20372 | 19 |
instanceVariableNames:'' |
20 |
classVariableNames:'IsBigEndian' |
|
21 |
poolDictionaries:'' |
|
22 |
category:'Collections-Abstract' |
|
1 | 23 |
! |
24 |
||
3433 | 25 |
!UninterpretedBytes primitiveDefinitions! |
26 |
%{ |
|
27 |
/* |
|
28 |
* Notice: I am abstract, and my subclasses may be anything. |
|
29 |
* Therefore, the code must always handle the fallback case |
|
30 |
* where the receiver is neither an ExternalBytes nor a ByteArray. |
|
31 |
* (which are, however, the most common) |
|
32 |
* |
|
33 |
* macro to fetch my byte address and size-in-bytes; |
|
34 |
* convenient for inline-C code. |
|
35 |
* (yes, C is bad ...) |
|
36 |
*/ |
|
37 |
#define __fetchBytePointerAndSize__(o, pPtr, pSize) \ |
|
38 |
{\ |
|
39 |
if (__isNonNilObject(o)) { \ |
|
22261 | 40 |
if (__isByteArrayLike(o)) { \ |
41 |
*(pPtr) = (unsigned char *)__ByteArrayInstPtr(o)->ba_element; \ |
|
42 |
*(pSize) = __byteArraySize(o); \ |
|
43 |
} else if (__qIsExternalBytesLike(o)) { \ |
|
44 |
OBJ __sz__ = __externalBytesSize(o); \ |
|
45 |
if (__isSmallInteger(__sz__)) { \ |
|
46 |
*(pSize) = __intVal(__sz__); \ |
|
47 |
*(pPtr) = (unsigned char *)(__externalBytesAddress(o)); \ |
|
48 |
} else { \ |
|
49 |
*(pSize) = 0; \ |
|
50 |
*(pPtr) = (unsigned char *)0; \ |
|
51 |
} \ |
|
52 |
} else { \ |
|
22264 | 53 |
/* here we need to differ 32bit and 64bit */ \ |
54 |
/* because on 32bit machines we need to take care about the alignment */ \ |
|
55 |
/* but sadly, the same code is wrong for 64bit machines */ \ |
|
56 |
if (__POINTER_SIZE__ == 8) {\ |
|
57 |
*(pSize) /* nInstBytes */ = OHDR_SIZE + __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(o))->c_ninstvars)); \ |
|
58 |
} else { \ |
|
22261 | 59 |
if (__isFloatArray(o)) { \ |
60 |
*(pSize) = sizeof(struct __FloatArray) - sizeof(float) + __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(o))->c_ninstvars)); \ |
|
61 |
} else { \ |
|
62 |
if (__isDoubleArray(o)) { \ |
|
63 |
*(pSize) = sizeof(struct __DoubleArray) - sizeof(double) + __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(o))->c_ninstvars)); \ |
|
64 |
} else { \ |
|
65 |
if (__isLongLongsOrSignedLongLongs(o)) { \ |
|
66 |
*(pSize) = sizeof(struct __LongIntegerArray) - sizeof(__uint64__) + __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(o))->c_ninstvars)); \ |
|
67 |
} else { \ |
|
68 |
*(pSize) /* nInstBytes */ = OHDR_SIZE + __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(o))->c_ninstvars)); \ |
|
69 |
} \ |
|
70 |
} \ |
|
71 |
} \ |
|
22264 | 72 |
} \ |
22261 | 73 |
*(pPtr) = (char *)(__InstPtr(self)) + *(pSize) /* nInstBytes */; \ |
74 |
*(pSize) = __qSize(self) - *(pSize) /* nInstBytes */; \ |
|
75 |
} \ |
|
3433 | 76 |
} else { \ |
22261 | 77 |
*(pSize) = 0; \ |
78 |
*(pPtr) = (unsigned char *)0; \ |
|
3433 | 79 |
} \ |
80 |
} |
|
81 |
||
82 |
%} |
|
83 |
! ! |
|
84 |
||
68 | 85 |
!UninterpretedBytes class methodsFor:'documentation'! |
1 | 86 |
|
88 | 87 |
copyright |
88 |
" |
|
89 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
22261 | 90 |
All Rights Reserved |
88 | 91 |
|
92 |
This software is furnished under a license and may be used |
|
93 |
only in accordance with the terms of that license and with the |
|
94 |
inclusion of the above copyright notice. This software may not |
|
95 |
be provided or otherwise made available to, or used by, any |
|
96 |
other person. No title to or ownership of the software is |
|
97 |
hereby transferred. |
|
98 |
" |
|
99 |
! |
|
100 |
||
68 | 101 |
documentation |
102 |
" |
|
13575 | 103 |
UninterpretedBytes provides the common protocol for byte-storage |
104 |
containers; concrete subclasses are |
|
22261 | 105 |
ByteArray (which store the bytes within the Smalltalk object memory) |
106 |
String (knows that the bytes represent characters) |
|
13575 | 107 |
and |
22261 | 108 |
ExternalBytes (which store the bytes in the malloc-heap). |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
109 |
|
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
110 |
UninterpretedBytes itself is abstract, so no instances of it can be created. |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
111 |
|
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
112 |
[See also:] |
22261 | 113 |
ByteArray String ExternalBytes |
1294 | 114 |
|
115 |
[author:] |
|
22261 | 116 |
Claus Gittinger |
3433 | 117 |
|
118 |
[Notice:] |
|
22261 | 119 |
Notice the confusion due to multiple methods with the same |
120 |
functionality (i.e. 'xxxx:MSB:' vs. 'xxxx:bigEndian:'). |
|
121 |
The reason is that at the time this class was written, |
|
122 |
ST80 did not offer protocol to specify the byteOrder, and |
|
123 |
ST/X provided methods ending in 'MSB:' for this. |
|
124 |
In the meanwhile, VW added protocol ending in 'bigEndian:', |
|
125 |
which has been added here for compatibility. |
|
126 |
(certainly a point, where an ansi-standard will help) |
|
68 | 127 |
" |
128 |
! ! |
|
1 | 129 |
|
18284 | 130 |
!UninterpretedBytes class methodsFor:'initialization'! |
131 |
||
132 |
initialize |
|
133 |
IsBigEndian := self isBigEndian. |
|
134 |
! ! |
|
135 |
||
3363
3bb61f364fe3
added #from: and #swapLongAt: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
3323
diff
changeset
|
136 |
!UninterpretedBytes class methodsFor:'instance creation'! |
3bb61f364fe3
added #from: and #swapLongAt: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
3323
diff
changeset
|
137 |
|
8995
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
138 |
from:aByteArray |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
139 |
"return new instance which is a copy of aByteArray" |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
140 |
|
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
141 |
|len bytes| |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
142 |
|
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
143 |
len := aByteArray size. |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
144 |
bytes := self new:len. |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
145 |
bytes replaceBytesFrom:1 to:len with:aByteArray startingAt:1. |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
146 |
^ bytes |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
147 |
|
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
148 |
" |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
149 |
String from:#[40 41 42] |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
150 |
String with:#[40 41 42 43 44 45 46 47 48 49 50] from:2 to:5 |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
151 |
" |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
152 |
! |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
153 |
|
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
154 |
fromHexString:aString |
15717 | 155 |
"decode a byteArray from a hex string (as generated by hexPrintOn:)" |
8995
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
156 |
|
12551 | 157 |
| sz bytes s hi lo | |
8995
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
158 |
|
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
159 |
sz := aString size. |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
160 |
sz == 0 ifTrue:[^ self new]. |
14970 | 161 |
sz odd ifTrue:[ ConversionError raiseWith:aString errorString:'invalid hex string (odd size)' ]. |
8995
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
162 |
|
12551 | 163 |
bytes := self new: sz // 2. |
8995
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
164 |
s := aString readStream. |
12551 | 165 |
1 to: sz // 2 do: [ :idx | |
22261 | 166 |
hi := s next digitValue. |
167 |
lo := s next digitValue. |
|
168 |
bytes at:idx put: ((hi bitShift:4) bitOr: lo) |
|
8995
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
169 |
]. |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
170 |
^ bytes |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
171 |
|
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
172 |
" |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
173 |
ByteArray fromHexString:'1234FEFF' |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
174 |
ExternalBytes fromHexString:'1234FEFF' |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
175 |
" |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
176 |
" |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
177 |
|s| |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
178 |
s := String streamContents:[:s | #[1 2 3] hexPrintOn:s]. |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
179 |
ByteArray fromHexString:s |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
180 |
" |
12551 | 181 |
" |
182 |
Time millisecondsToRun:[ |
|
22261 | 183 |
1000000 timesRepeat:[ ByteArray fromHexString:'1234FEFF1234FEFF1234FEFF1234FEFF' ] |
12551 | 184 |
]. |
185 |
" |
|
15717 | 186 |
|
187 |
"Modified (comment): / 28-08-2013 / 20:40:04 / cg" |
|
8995
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
188 |
! |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
189 |
|
11874
aebca3972774
+fromHexStringWithSeparators
Claus Gittinger <cg@exept.de>
parents:
11852
diff
changeset
|
190 |
fromHexStringWithSeparators:aString |
aebca3972774
+fromHexStringWithSeparators
Claus Gittinger <cg@exept.de>
parents:
11852
diff
changeset
|
191 |
"read a bytearray from a printed string representation, where |
aebca3972774
+fromHexStringWithSeparators
Claus Gittinger <cg@exept.de>
parents:
11852
diff
changeset
|
192 |
individual bytes are encoded as two hex digits, optionally separated by whiteSpace. |
aebca3972774
+fromHexStringWithSeparators
Claus Gittinger <cg@exept.de>
parents:
11852
diff
changeset
|
193 |
See also fromHexString:, which does something similar, but does not allow for spaces" |
aebca3972774
+fromHexStringWithSeparators
Claus Gittinger <cg@exept.de>
parents:
11852
diff
changeset
|
194 |
|
aebca3972774
+fromHexStringWithSeparators
Claus Gittinger <cg@exept.de>
parents:
11852
diff
changeset
|
195 |
^ self streamContents:[:outStream | |
22261 | 196 |
|inStream h| |
197 |
||
198 |
inStream := aString readStream. |
|
199 |
||
200 |
[ |
|
201 |
inStream skipSeparators. |
|
202 |
inStream atEnd |
|
203 |
] whileFalse:[ |
|
204 |
h := inStream next:2. |
|
205 |
outStream nextPut:(Integer readFrom:h base:16). |
|
206 |
]. |
|
11874
aebca3972774
+fromHexStringWithSeparators
Claus Gittinger <cg@exept.de>
parents:
11852
diff
changeset
|
207 |
]. |
aebca3972774
+fromHexStringWithSeparators
Claus Gittinger <cg@exept.de>
parents:
11852
diff
changeset
|
208 |
|
aebca3972774
+fromHexStringWithSeparators
Claus Gittinger <cg@exept.de>
parents:
11852
diff
changeset
|
209 |
" |
aebca3972774
+fromHexStringWithSeparators
Claus Gittinger <cg@exept.de>
parents:
11852
diff
changeset
|
210 |
ByteArray fromHexString:'1234FEFF' |
aebca3972774
+fromHexStringWithSeparators
Claus Gittinger <cg@exept.de>
parents:
11852
diff
changeset
|
211 |
ByteArray fromHexStringWithSeparators:' 12 34 FE FF' |
aebca3972774
+fromHexStringWithSeparators
Claus Gittinger <cg@exept.de>
parents:
11852
diff
changeset
|
212 |
" |
aebca3972774
+fromHexStringWithSeparators
Claus Gittinger <cg@exept.de>
parents:
11852
diff
changeset
|
213 |
! |
aebca3972774
+fromHexStringWithSeparators
Claus Gittinger <cg@exept.de>
parents:
11852
diff
changeset
|
214 |
|
8995
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
215 |
fromPackedString:aString |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
216 |
"ST-80 compatibility: decode a byteArray from a packed string in which |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
217 |
6bits are encoded per character. The argument, aString must be a multiple |
18633 | 218 |
of 4 in size (since 24 is the lcm of 6 and 8). |
219 |
Every 6 bit packet is encoded as a character in 32..95. |
|
220 |
Characters below 32 are ignored (so line breaks can be inserted at any place). |
|
18649
e9d831015328
fix: signedLongIntVal returns an INT
Claus Gittinger <cg@exept.de>
parents:
18633
diff
changeset
|
221 |
An addition final byte defines how many bytes of the last triple are valid. |
18633 | 222 |
This is somewhat like the radix-encoding used in good old PDP11 times ;-) |
8995
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
223 |
ST-80 uses this encoding for Images ... |
16798 | 224 |
This is a base64 encoding, very similar (but not equal) to the algorithm used in RFC1421. |
18346 | 225 |
PS: It took a while to figure that one out ... |
18633 | 226 |
PPS: I don't like it ;-)" |
8995
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
227 |
|
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
228 |
|index "{ Class: SmallInteger }" |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
229 |
dstIndex "{ Class: SmallInteger }" |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
230 |
stop "{ Class: SmallInteger }" |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
231 |
sixBits "{ Class: SmallInteger }" |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
232 |
n "{ Class: SmallInteger }" |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
233 |
sz "{ Class: SmallInteger }" |
12551 | 234 |
last bytes| |
8995
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
235 |
|
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
236 |
sz := aString size. |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
237 |
sz == 0 ifTrue:[^ self new]. |
12551 | 238 |
sz := sz - (aString count:[:ch | ch codePoint < 32]). |
8995
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
239 |
|
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
240 |
stop := sz // 4 * 3. |
12551 | 241 |
"the size modulo 3 is encoded in the last character, if it is in the |
8995
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
242 |
range 97 .. otherwise, its exact." |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
243 |
|
12551 | 244 |
last := aString last codePoint. |
245 |
last > 96 ifTrue:[ |
|
22261 | 246 |
stop := stop - 3 + (last - 96) |
8995
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
247 |
]. |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
248 |
bytes := self new:stop. |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
249 |
|
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
250 |
index := 1. dstIndex := 1. |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
251 |
[dstIndex <= stop] whileTrue:[ |
22261 | 252 |
"/ take 4 characters ... |
253 |
"/ allow a line break before each group of 4 |
|
254 |
sixBits := (aString at:index) codePoint. |
|
255 |
[sixBits < 32] whileTrue:[ |
|
256 |
index := index + 1. |
|
257 |
sixBits := (aString at:index) codePoint. |
|
258 |
]. |
|
259 |
sixBits := sixBits bitAnd:16r3F. |
|
260 |
n := sixBits. |
|
261 |
||
262 |
"/ self assert:(aString at:index+1) codePoint >= 32. |
|
263 |
sixBits := (aString at:index+1) codePoint bitAnd:16r3F. |
|
264 |
n := (n bitShift:6) + sixBits. |
|
265 |
||
266 |
"/ self assert:(aString at:index+2) codePoint >= 32. |
|
267 |
sixBits := (aString at:index+2) codePoint bitAnd:16r3F. |
|
268 |
n := (n bitShift:6) + sixBits. |
|
269 |
||
270 |
"/ self assert:(aString at:index+3) codePoint >= 32. |
|
271 |
sixBits := (aString at:index+3) codePoint bitAnd:16r3F. |
|
272 |
n := (n bitShift:6) + sixBits. |
|
273 |
||
274 |
index := index + 4. |
|
275 |
||
276 |
"/ now have 24 bits in n |
|
277 |
||
278 |
bytes at:dstIndex put:(n bitShift:-16). |
|
279 |
||
280 |
dstIndex < stop ifTrue:[ |
|
281 |
bytes at:dstIndex+1 put:((n bitShift:-8) bitAnd:16rFF). |
|
282 |
dstIndex+2 <= stop ifTrue:[ |
|
283 |
bytes at:dstIndex+2 put:(n bitAnd:16rFF). |
|
284 |
] |
|
285 |
]. |
|
286 |
dstIndex := dstIndex + 3. |
|
8995
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
287 |
]. |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
288 |
^ bytes |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
289 |
|
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
290 |
" |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
291 |
ByteArray fromPackedString:(#[1 1 1 1] asPackedString) |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
292 |
ByteArray fromPackedString:(#[1 1 1 1 1] asPackedString) |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
293 |
ByteArray fromPackedString:(#[1 1 1 1 1 1] asPackedString) |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
294 |
ByteArray fromPackedString:(#[1 1 1 1 1 1 1] asPackedString) |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
295 |
ByteArray fromPackedString:(#[1 1 1 1 1 1 1 1] asPackedString) |
18633 | 296 |
ByteArray fromPackedString:((ByteArray new:256) asPackedString) |
297 |
ByteArray fromPackedString:((ByteArray new:128) asPackedString) |
|
298 |
ByteArray fromPackedString:((ByteArray new:129) asPackedString) |
|
299 |
ByteArray fromPackedString:((ByteArray new:130) asPackedString) |
|
300 |
ByteArray fromPackedString:((ByteArray new:131) asPackedString) |
|
301 |
ByteArray fromPackedString:((ByteArray new:132) asPackedString) |
|
302 |
ByteArray fromPackedString:((ByteArray new:64) asPackedString) |
|
303 |
||
304 |
0 to:256 do:[:l | |
|
22261 | 305 |
|orig copy| |
306 |
||
307 |
0 to:255 do:[:fill | |
|
308 |
orig := ByteArray new:l withAll:fill. |
|
309 |
copy := ByteArray fromPackedString:(orig asPackedString). |
|
310 |
self assert:(orig = copy). |
|
311 |
] |
|
18633 | 312 |
] |
8995
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
313 |
" |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
314 |
|
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
315 |
"Modified: / 6.3.1997 / 15:28:52 / cg" |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
316 |
"Modified: / 18.12.1997 / 17:17:11 / stefan" |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
317 |
! |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
318 |
|
18284 | 319 |
uninitializedNew:anInteger |
320 |
"return a new instance of the receiver with uninitialized |
|
321 |
(i.e. undefined) contents. The indexed elements have any random |
|
322 |
value. However, any named instance variables are still nilled. |
|
323 |
For use, when contents will be set anyway shortly after - this |
|
324 |
is a bit faster than the regular basicNew:, which clears the bytes. |
|
325 |
Of course, it only makes a difference for very big ByteArrays, such |
|
326 |
as used for images/bitmaps. |
|
327 |
||
328 |
Notice: if you want to port code using uninitializedNew: to another |
|
329 |
smalltalk, you have to add an 'uninitializedNew: -> basicNew:'-calling |
|
330 |
method to the ByteArray class of the other smalltalk." |
|
331 |
||
332 |
%{ /* NOCONTEXT */ |
|
333 |
OBJ newobj; |
|
334 |
INT instsize, nInstVars, nindexedinstvars; |
|
335 |
REGISTER OBJ *op; |
|
336 |
||
337 |
if (__isSmallInteger(anInteger)) { |
|
22261 | 338 |
nindexedinstvars = __intVal(anInteger); |
339 |
if (nindexedinstvars >= 0) { |
|
340 |
if (self == ByteArray) { |
|
341 |
/* |
|
342 |
* the most common case |
|
343 |
*/ |
|
344 |
instsize = OHDR_SIZE + nindexedinstvars; |
|
345 |
if (__CanDoQuickNew(instsize)) { /* OBJECT ALLOCATION */ |
|
346 |
__qCheckedNew(newobj, instsize); |
|
347 |
__InstPtr(newobj)->o_class = self; |
|
348 |
__qSTORE(newobj, self); |
|
349 |
RETURN (newobj ); |
|
350 |
} |
|
351 |
} else { |
|
352 |
/* |
|
353 |
* Take care for subclasses like TwoByteString |
|
354 |
*/ |
|
355 |
switch (__smallIntegerVal(__ClassInstPtr(self)->c_flags) & ARRAYMASK) { |
|
356 |
case BYTEARRAY: |
|
357 |
break; |
|
358 |
||
359 |
case WORDARRAY: |
|
360 |
case SWORDARRAY: |
|
361 |
nindexedinstvars *= 2; |
|
362 |
break; |
|
363 |
||
364 |
case LONGARRAY: |
|
365 |
case SLONGARRAY: |
|
366 |
nindexedinstvars *= 4; |
|
367 |
break; |
|
368 |
||
369 |
default: |
|
370 |
/* don't know about this array type, delegate to super */ |
|
371 |
goto out; |
|
372 |
} |
|
373 |
} |
|
374 |
nInstVars = __intVal(__ClassInstPtr(self)->c_ninstvars); |
|
375 |
instsize = OHDR_SIZE + __OBJS2BYTES__(nInstVars) + nindexedinstvars; |
|
376 |
__PROTECT_CONTEXT__ |
|
377 |
__qNew(newobj, instsize); /* OBJECT ALLOCATION */ |
|
378 |
__UNPROTECT_CONTEXT__ |
|
379 |
if (newobj != nil) { |
|
380 |
__InstPtr(newobj)->o_class = self; |
|
381 |
__qSTORE(newobj, self); |
|
382 |
if (nInstVars) { |
|
383 |
/* |
|
384 |
* still have to nil out named instvars ... |
|
385 |
*/ |
|
18284 | 386 |
#if defined(memset4) && defined(FAST_OBJECT_MEMSET4) |
22261 | 387 |
memset4(__InstPtr(newobj)->i_instvars, nil, nInstVars); |
18284 | 388 |
#else |
389 |
# if defined(FAST_MEMSET) && !defined(NEGATIVE_ADDRESSES) |
|
22261 | 390 |
/* |
391 |
* knowing that nil is 0 |
|
392 |
*/ |
|
393 |
memset(__InstPtr(newobj)->i_instvars, 0, instsize - OHDR_SIZE); |
|
18284 | 394 |
# else |
22261 | 395 |
op = __InstPtr(newobj)->i_instvars; |
396 |
while (nInstVars--) |
|
397 |
*op++ = nil; |
|
18284 | 398 |
# endif |
399 |
#endif |
|
22261 | 400 |
} |
401 |
RETURN ( newobj ); |
|
402 |
} |
|
403 |
} |
|
18284 | 404 |
} |
405 |
out:; |
|
406 |
%}. |
|
407 |
^ self basicNew:anInteger |
|
408 |
! |
|
409 |
||
8995
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
410 |
with:aByteArray from:start to:stop |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
411 |
"return new instance with a copy of aByteArray |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
412 |
beginning at index start up to and including index stop" |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
413 |
|
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
414 |
|len bytes| |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
415 |
|
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
416 |
len := stop-start+1. |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
417 |
bytes := self new:len. |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
418 |
bytes replaceBytesFrom:1 to:len with:aByteArray startingAt:start. |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
419 |
^ bytes |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
420 |
|
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
421 |
" |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
422 |
String with:#[40 41 42 43 44 45 46 47 48 49 50] from:2 to:5 |
ab9905b339bb
Moved some methods from ByteArray -> UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
8986
diff
changeset
|
423 |
" |
3363
3bb61f364fe3
added #from: and #swapLongAt: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
3323
diff
changeset
|
424 |
! ! |
3bb61f364fe3
added #from: and #swapLongAt: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
3323
diff
changeset
|
425 |
|
21830 | 426 |
!UninterpretedBytes class methodsFor:'Compatibility-Squeak'! |
427 |
||
428 |
readHexFrom:aString |
|
429 |
"same as fromHexString: for squeak/Pharo compatibility" |
|
22261 | 430 |
|
21830 | 431 |
^ self fromHexString:aString |
432 |
||
433 |
" |
|
434 |
(ByteArray readHexFrom: 'C3A1C3A5C3A6C3B1C386C2A5C3BC') |
|
435 |
" |
|
436 |
||
437 |
"Created: / 18-06-2017 / 18:01:18 / cg" |
|
438 |
! ! |
|
439 |
||
1 | 440 |
!UninterpretedBytes class methodsFor:'queries'! |
441 |
||
8893 | 442 |
isAbstract |
11220 | 443 |
"Return if this class is an abstract class. |
444 |
True is returned for UninterpretedBytes here; false for subclasses. |
|
19461 | 445 |
Abstract subclasses must redefine this again." |
11220 | 446 |
|
8893 | 447 |
^ self == UninterpretedBytes |
448 |
! |
|
449 |
||
1 | 450 |
isBigEndian |
77 | 451 |
"return true, if words/shorts store the most-significant |
13575 | 452 |
byte first (MSB), false if least-sign.-first (LSB). |
4019
c9284ca27a4a
tuned some doubleWordAt methods;
Claus Gittinger <cg@exept.de>
parents:
3936
diff
changeset
|
453 |
I.e. false for vax, intel; true for m68k, sun. |
c9284ca27a4a
tuned some doubleWordAt methods;
Claus Gittinger <cg@exept.de>
parents:
3936
diff
changeset
|
454 |
|
c9284ca27a4a
tuned some doubleWordAt methods;
Claus Gittinger <cg@exept.de>
parents:
3936
diff
changeset
|
455 |
Notice: UninterpretedBytes isBigEndian |
22261 | 456 |
this is inlined both by stc and the jit compiler" |
1 | 457 |
|
458 |
%{ /* NOCONTEXT */ |
|
459 |
||
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8893
diff
changeset
|
460 |
#if defined(__MSBFIRST__) |
3936 | 461 |
RETURN (true); |
462 |
#else |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8893
diff
changeset
|
463 |
# if defined(__LSBFIRST__) |
4275 | 464 |
RETURN (false); |
465 |
# else |
|
1 | 466 |
/* |
20816 | 467 |
* I don't like ifdefs - you always forget some ... |
3433 | 468 |
* therefore we look into a structure at run-time. |
469 |
* (also, there are CPUs around [mips], where the byteorder |
|
470 |
* is programmable, and which come in different flavours) |
|
471 |
* |
|
13575 | 472 |
* NOTICE: |
473 |
* both the JIT and stc may inline this to a |
|
3433 | 474 |
* constant for systems where this is known. |
1 | 475 |
*/ |
476 |
union { |
|
22261 | 477 |
unsigned int u_l; |
478 |
char u_c[sizeof(int)]; |
|
1 | 479 |
} u; |
480 |
||
481 |
u.u_l = 0x87654321; |
|
77 | 482 |
if (u.u_c[0] == 0x21) RETURN (false); |
483 |
RETURN (true); |
|
4275 | 484 |
# endif |
3936 | 485 |
#endif |
18346 | 486 |
%}. |
487 |
^ false "/ an arbitrary default |
|
488 |
||
3433 | 489 |
" |
490 |
UninterpretedBytes isBigEndian |
|
491 |
" |
|
3209 | 492 |
! |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
493 |
|
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
494 |
isBuiltInClass |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
495 |
"return true if this class is known by the run-time-system. |
3323 | 496 |
Here, true is returned, since UninterpretedBytes is the superclass of |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
497 |
some builtIn classes (ByteArray & ExternalBytes)" |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
498 |
|
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
499 |
^ self == UninterpretedBytes |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
500 |
|
3323 | 501 |
"Modified: / 23.4.1996 / 15:56:25 / cg" |
502 |
"Modified: / 5.3.1998 / 14:56:22 / stefan" |
|
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
503 |
! ! |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
504 |
|
19349 | 505 |
!UninterpretedBytes methodsFor:'Compatibility'! |
506 |
||
507 |
doubleWordAt:index |
|
508 |
"return the 4-bytes starting at index as an (unsigned) Integer. |
|
509 |
The index is a smalltalk index (i.e. 1-based). |
|
22161 | 510 |
The value is retrieved in the machine's natural byte order." |
19349 | 511 |
|
512 |
^ self unsignedInt32At:index MSB:IsBigEndian |
|
513 |
||
514 |
" |
|
515 |
|b| |
|
516 |
||
517 |
b := ByteArray withAll:#(1 2 3 4). |
|
518 |
(b doubleWordAt:1) printStringRadix:16 |
|
519 |
" |
|
520 |
||
22161 | 521 |
"Modified: / 05-03-1998 / 14:57:35 / stefan" |
522 |
"Modified (comment): / 04-08-2017 / 11:15:29 / cg" |
|
19349 | 523 |
! |
524 |
||
525 |
doubleWordAt:index MSB:msb |
|
526 |
"return the 4-bytes starting at index as an (unsigned) Integer. |
|
527 |
The index is a smalltalk index (i.e. 1-based). |
|
528 |
The value is retrieved MSB-first, if the msb-arg is true; |
|
529 |
LSB-first otherwise." |
|
530 |
||
531 |
^ self unsignedInt32At:index MSB:msb |
|
532 |
||
533 |
" |
|
534 |
|b| |
|
535 |
||
536 |
b := ByteArray withAll:#(1 2 3 4). |
|
537 |
(b doubleWordAt:1 MSB:true) printStringRadix:16. |
|
538 |
(b doubleWordAt:1 MSB:false) printStringRadix:16 |
|
539 |
" |
|
540 |
! |
|
541 |
||
542 |
doubleWordAt:byteIndex put:anInteger |
|
543 |
"set the 4-bytes starting at index from the (unsigned) Integer value. |
|
544 |
The index is a smalltalk index (i.e. 1-based). |
|
545 |
The value should be in the range 0 to 16rFFFFFFFF |
|
546 |
(for negative values, the stored value is not defined). |
|
22161 | 547 |
The value is stored in the machine's natural byte order." |
19349 | 548 |
|
549 |
^ self unsignedInt32At:byteIndex put:anInteger MSB:IsBigEndian |
|
550 |
||
551 |
" |
|
552 |
|b| |
|
553 |
b := ByteArray new:4. |
|
554 |
b doubleWordAt:1 put:16r04030201. |
|
555 |
b inspect |
|
556 |
" |
|
557 |
||
22161 | 558 |
"Modified: / 05-03-1998 / 14:57:48 / stefan" |
559 |
"Modified (comment): / 04-08-2017 / 11:15:38 / cg" |
|
19349 | 560 |
! |
561 |
||
562 |
doubleWordAt:byteIndex put:anInteger MSB:msb |
|
563 |
"set the 4-bytes starting at index from the (unsigned) Integer value. |
|
564 |
The index is a smalltalk index (i.e. 1-based). |
|
565 |
The value must be in the range 0 to 16rFFFFFFFF. |
|
566 |
The value is stored MSB-first if msb is true; LSB-first otherwise." |
|
567 |
||
568 |
^ self unsignedInt32At:byteIndex put:anInteger MSB:msb |
|
569 |
||
570 |
" |
|
571 |
|b| |
|
572 |
b := ByteArray new:8. |
|
573 |
b doubleWordAt:1 put:16r04030201 MSB:true. |
|
574 |
b doubleWordAt:5 put:16r04030201 MSB:false. |
|
575 |
b inspect |
|
576 |
" |
|
577 |
||
578 |
"Modified: / 21.1.1998 / 17:43:34 / cg" |
|
579 |
"Modified: / 5.3.1998 / 11:42:17 / stefan" |
|
580 |
! |
|
581 |
||
582 |
doubleWordAtDoubleWordIndex:int32Index |
|
583 |
"return the unsigned long (int32) at index, anInteger. |
|
584 |
Fetching in the machine's natural byte order. |
|
585 |
Indices are 1-based and scaled as appropriate to allow |
|
586 |
accessing the memory as an array of doubleWord entries. |
|
587 |
(i.e. indices are 1, 2, ...)" |
|
588 |
||
589 |
^ self doubleWordAtDoubleWordIndex:int32Index MSB:IsBigEndian |
|
590 |
||
591 |
"Created: / 21.1.1998 / 17:43:53 / cg" |
|
592 |
"Modified: / 5.3.1998 / 14:58:06 / stefan" |
|
593 |
! |
|
594 |
||
595 |
doubleWordAtDoubleWordIndex:int32Index MSB:msb |
|
596 |
"return the unsigned long (int32) at index, anInteger. |
|
597 |
Fetching is MSB if msb is true, LSB otherwise. |
|
598 |
Indices are 1-based and scaled as appropriate to allow |
|
599 |
accessing the memory as an array of doubleWord entries. |
|
600 |
(i.e. indices are 1, 2, ...)" |
|
601 |
||
602 |
^ self unsignedInt32At:((int32Index - 1) * 4 + 1) MSB:msb |
|
603 |
||
604 |
"Created: / 21.1.1998 / 17:44:07 / cg" |
|
605 |
! |
|
606 |
||
607 |
doubleWordAtDoubleWordIndex:int32Index put:anInteger |
|
608 |
"set the long at index, anInteger. |
|
609 |
Storing in the machines natural byte order. |
|
610 |
Indices are 1-based and scaled as appropriate to allow |
|
611 |
accessing the memory as an array of doubleWord entries. |
|
612 |
(i.e. indices are 1, 2, ...)" |
|
613 |
||
614 |
^ self doubleWordAtDoubleWordIndex:int32Index put:anInteger MSB:IsBigEndian |
|
615 |
||
616 |
"Created: / 21.1.1998 / 17:44:13 / cg" |
|
617 |
"Modified: / 5.3.1998 / 14:58:19 / stefan" |
|
618 |
! |
|
619 |
||
620 |
doubleWordAtDoubleWordIndex:int32Index put:anInteger MSB:msb |
|
621 |
"set the long at index, anInteger. |
|
622 |
Storing is MSB if msb is true, LSB otherwise. |
|
623 |
Indices are 1-based and scaled as appropriate to allow |
|
624 |
accessing the memory as an array of doubleWord entries. |
|
625 |
(i.e. indices are 1, 2, ...)" |
|
626 |
||
627 |
^ self unsignedInt32At:((int32Index - 1) * 4 + 1) put:anInteger MSB:msb |
|
628 |
||
629 |
"Created: / 21.1.1998 / 17:44:19 / cg" |
|
630 |
! |
|
631 |
||
632 |
int16At:byteIndex |
|
633 |
"return the 2-bytes starting at index as a signed Integer. |
|
634 |
The index is a smalltalk index (i.e. 1-based). |
|
22161 | 635 |
The value is retrieved in the machine's natural byte order. |
19349 | 636 |
This may be worth a primitive." |
637 |
||
638 |
^ self signedInt16At:byteIndex |
|
639 |
||
640 |
" |
|
641 |
|b| |
|
642 |
b := ByteArray new:2. |
|
643 |
b wordAt:1 put:16rFFFF. |
|
644 |
b signedWordAt:1 |
|
645 |
" |
|
646 |
||
22161 | 647 |
"Modified: / 01-07-1996 / 21:14:38 / cg" |
648 |
"Modified (comment): / 04-08-2017 / 11:15:48 / cg" |
|
19349 | 649 |
! |
650 |
||
651 |
int16At:byteIndex MSB:msb |
|
652 |
"return the 2-bytes starting at index as a signed Integer. |
|
653 |
The index is a smalltalk index (i.e. 1-based). |
|
654 |
The value is retrieved in the machines natural byte order. |
|
655 |
This may be worth a primitive." |
|
656 |
||
657 |
^ self signedInt16At:byteIndex MSB:msb |
|
658 |
||
659 |
" |
|
660 |
|b| |
|
661 |
b := ByteArray new:2. |
|
662 |
b wordAt:1 put:16rFFFF. |
|
663 |
b signedWordAt:1 |
|
664 |
" |
|
665 |
||
666 |
"Modified: 1.7.1996 / 21:14:38 / cg" |
|
667 |
! |
|
668 |
||
669 |
int16At:index put:anInteger |
|
670 |
"set the 2-bytes starting at index from the signed Integer value. |
|
671 |
The index is a smalltalk index (i.e. 1-based). |
|
672 |
The stored value must be in the range -32768 .. +32676. |
|
673 |
The value is stored in the machine's natural byteorder" |
|
674 |
||
675 |
^ self signedInt16At:index put:anInteger MSB:IsBigEndian |
|
676 |
||
677 |
" |
|
678 |
|b| |
|
679 |
b := ByteArray new:4. |
|
680 |
b shortAt:1 put:1 bigEndian:true. |
|
681 |
b shortAt:3 put:1 bigEndian:false. |
|
682 |
b inspect |
|
683 |
" |
|
684 |
||
685 |
"Modified: / 1.7.1996 / 21:12:07 / cg" |
|
686 |
"Created: / 5.3.1998 / 11:02:05 / stefan" |
|
687 |
! |
|
688 |
||
689 |
int16At:index put:anInteger MSB:bigEndian |
|
690 |
"set the 2-bytes starting at index from the signed Integer value. |
|
691 |
The index is a smalltalk index (i.e. 1-based). |
|
692 |
The stored value must be in the range -32768 .. +32676. |
|
693 |
The value is stored in the byteorder given by bigEndian. |
|
694 |
This may be worth a primitive." |
|
695 |
||
696 |
||
697 |
^ self signedInt16At:index put:anInteger MSB:bigEndian |
|
698 |
||
699 |
" |
|
700 |
|b| |
|
701 |
b := ByteArray new:4. |
|
702 |
b shortAt:1 put:1 bigEndian:true. |
|
703 |
b shortAt:3 put:1 bigEndian:false. |
|
704 |
b inspect |
|
705 |
" |
|
706 |
||
707 |
"Modified: / 1.7.1996 / 21:12:07 / cg" |
|
708 |
"Created: / 5.3.1998 / 11:02:05 / stefan" |
|
709 |
! |
|
710 |
||
711 |
longAt:index |
|
712 |
"return the 4-bytes starting at index as a signed Integer. |
|
713 |
The index is a smalltalk index (i.e. 1-based). |
|
22161 | 714 |
The value is retrieved in the machine's natural byte order, |
19349 | 715 |
therefore, this should only be used for byte-data which is |
716 |
only used inside this machine. |
|
717 |
To setup data packets which are to be sent to other machines, |
|
718 |
or stored into a file, always use longAt:MSB: and specify |
|
719 |
a definite byteOrder." |
|
720 |
||
721 |
^ self signedInt32At:index |
|
722 |
||
723 |
" |
|
724 |
|b| |
|
725 |
b := ByteArray new:4. |
|
726 |
b unsignedLongAt:1 put:16rFFFFFFFF. |
|
727 |
(b longAt:1) |
|
728 |
" |
|
729 |
||
22161 | 730 |
"Modified: / 01-07-1996 / 21:11:28 / cg" |
731 |
"Modified: / 05-03-1998 / 12:06:28 / stefan" |
|
732 |
"Modified (comment): / 04-08-2017 / 11:15:58 / cg" |
|
19349 | 733 |
! |
734 |
||
735 |
longAt:index bigEndian:msb |
|
736 |
"return the 4-bytes starting at index as a signed Integer. |
|
737 |
The index is a smalltalk index (i.e. 1-based). |
|
738 |
Depending on msb, the value is retrieved MSB-first or LSB-first. |
|
739 |
This may be worth a primitive." |
|
740 |
||
741 |
^ self signedInt32At:index MSB:msb |
|
742 |
||
743 |
" |
|
744 |
|b| |
|
745 |
b := ByteArray new:4. |
|
746 |
b unsignedLongAt:1 put:16rFFFFFFFF. |
|
747 |
(b longAt:1) |
|
748 |
" |
|
749 |
||
750 |
"Modified: / 1.7.1996 / 21:11:33 / cg" |
|
751 |
"Created: / 5.3.1998 / 14:02:03 / stefan" |
|
752 |
! |
|
753 |
||
754 |
longAt:index put:value |
|
755 |
"set the 4-bytes starting at index from the signed Integer value. |
|
756 |
The index is a smalltalk index (i.e. 1-based). |
|
757 |
The value is stored in the machine's natural byte order." |
|
758 |
||
759 |
^ self signedInt32At:index put:value MSB:IsBigEndian |
|
760 |
||
761 |
" |
|
762 |
|b| |
|
763 |
b := ByteArray new:4. |
|
764 |
b longAt:1 put:-1. |
|
765 |
(b unsignedLongAt:1) printStringRadix:16 |
|
766 |
" |
|
767 |
||
768 |
"Modified: / 1.7.1996 / 21:11:39 / cg" |
|
769 |
"Created: / 5.3.1998 / 10:57:18 / stefan" |
|
770 |
! |
|
771 |
||
772 |
longAt:byteIndex put:anInteger bigEndian:msb |
|
773 |
"store a signed long (32bit) integer. |
|
774 |
The index is a smalltalk index (i.e. 1-based)." |
|
775 |
||
776 |
^ self signedInt32At:byteIndex put:anInteger MSB:msb |
|
777 |
||
778 |
"Created: / 9.5.1998 / 01:10:24 / cg" |
|
779 |
"Modified: / 9.5.1998 / 01:13:34 / cg" |
|
780 |
! |
|
781 |
||
782 |
longLongAt:index |
|
783 |
"return the 8-bytes starting at index as a signed Integer. |
|
784 |
The index is a smalltalk index (i.e. 1-based). |
|
22161 | 785 |
The value is retrieved in the machine's natural byte order. |
19349 | 786 |
This may be worth a primitive." |
787 |
||
788 |
^ self signedInt64At:index MSB:IsBigEndian |
|
789 |
||
790 |
" |
|
791 |
|b| |
|
792 |
b := ByteArray new:4. |
|
793 |
b unsignedLongLongAt:1 put:16rFFFFFFFFFFFFFFFF. |
|
794 |
(b longLongAt:1) |
|
795 |
" |
|
796 |
||
22161 | 797 |
"Modified: / 01-07-1996 / 21:11:28 / cg" |
798 |
"Created: / 05-03-1998 / 14:40:05 / stefan" |
|
799 |
"Modified (comment): / 04-08-2017 / 11:16:06 / cg" |
|
19349 | 800 |
! |
801 |
||
802 |
longLongAt:index bigEndian:msb |
|
803 |
"return the 8-bytes starting at index as a signed Integer. |
|
804 |
The index is a smalltalk index (i.e. 1-based). |
|
805 |
The value is retrieved in the given byte order. |
|
806 |
This may be worth a primitive." |
|
807 |
||
808 |
^ self signedInt64At:index MSB:msb |
|
809 |
||
810 |
" |
|
811 |
|b| |
|
812 |
b := ByteArray new:4. |
|
813 |
b unsignedLongLongAt:1 put:16rFFFFFFFFFFFFFFFF. |
|
814 |
(b longLongAt:1 msb:true) |
|
815 |
" |
|
816 |
||
817 |
"Modified: / 5.3.1998 / 12:06:28 / stefan" |
|
818 |
"Created: / 5.3.1998 / 14:40:54 / stefan" |
|
819 |
"Modified: / 9.5.1998 / 01:10:59 / cg" |
|
820 |
! |
|
821 |
||
822 |
longLongAt:byteIndex put:anInteger |
|
823 |
"store a signed longLong (64bit) integer. |
|
824 |
The index is a smalltalk index (i.e. 1-based). |
|
22161 | 825 |
The value is stored in the machine's natural byte order. |
19349 | 826 |
Same as #signedQuadWordAt:put: - for ST80 compatibility." |
827 |
||
828 |
^ self signedInt64At:byteIndex put:anInteger MSB:IsBigEndian |
|
22161 | 829 |
|
830 |
"Modified (comment): / 04-08-2017 / 11:16:36 / cg" |
|
19349 | 831 |
! |
832 |
||
833 |
longLongAt:byteIndex put:anInteger bigEndian:msb |
|
834 |
"store a signed longLong (64bit) integer. |
|
835 |
The index is a smalltalk index (i.e. 1-based). |
|
836 |
Same as #signedQuadWordAt:put: - for ST80 compatibility." |
|
837 |
||
838 |
^ self signedInt64At:byteIndex put:anInteger MSB:msb |
|
839 |
||
840 |
"Created: / 9.5.1998 / 01:10:24 / cg" |
|
841 |
"Modified: / 9.5.1998 / 01:13:34 / cg" |
|
842 |
! |
|
843 |
||
844 |
quadWordAt:index MSB:msb |
|
845 |
"return the 8-bytes starting at index as an (unsigned) Integer. |
|
846 |
The index is a smalltalk index (i.e. 1-based). |
|
847 |
Depending on msb, the value is retrieved MSB or LSB-first." |
|
848 |
||
22261 | 849 |
^ self unsignedInt64At:index MSB:msb |
19349 | 850 |
|
851 |
" |
|
852 |
|b| |
|
853 |
||
854 |
b := ByteArray withAll:#(1 2 3 4 5 6 7 8). |
|
855 |
(b quadWordAt:1 MSB:false) printStringRadix:16 |
|
856 |
" |
|
857 |
||
858 |
"Modified: 5.11.1996 / 14:06:21 / cg" |
|
859 |
! |
|
860 |
||
861 |
quadWordAt:index put:anInteger MSB:msb |
|
862 |
"set the 8-bytes starting at index from the (unsigned) Integer value. |
|
863 |
The index is a smalltalk index (i.e. 1-based). |
|
864 |
The value must be in the range 0 to 16rFFFFFFFFFFFFFFFF. |
|
865 |
Depending on msb, the value is stored MSB-first or LSB-first." |
|
866 |
||
867 |
^ self unsignedInt64At:index put:anInteger MSB:msb |
|
868 |
||
869 |
" |
|
870 |
|b| |
|
871 |
b := ByteArray new:8. |
|
872 |
b quadWordAtIndex:1 put:16r0807060504030201 MSB:false. |
|
873 |
b inspect |
|
874 |
" |
|
875 |
! |
|
876 |
||
877 |
shortAt:index |
|
878 |
"return the 2-bytes starting at index as a signed Integer. |
|
879 |
The index is a smalltalk index (i.e. 1-based). |
|
22161 | 880 |
The value is retrieved in the machine's natural byte order. |
19349 | 881 |
This may be worth a primitive. |
882 |
This is the ST80 equivalent of #signedWordAt:" |
|
883 |
||
19368 | 884 |
^ self signedInt16At:index MSB:IsBigEndian |
19349 | 885 |
|
886 |
" |
|
887 |
|b| |
|
888 |
b := ByteArray new:2. |
|
889 |
b unsignedShortAt:1 put:16rFFFF. |
|
890 |
b shortAt:1 |
|
891 |
" |
|
892 |
||
22161 | 893 |
"Modified: / 01-07-1996 / 21:14:38 / cg" |
894 |
"Created: / 05-03-1998 / 10:59:57 / stefan" |
|
895 |
"Modified: / 05-03-1998 / 23:39:38 / stefan" |
|
896 |
"Modified (comment): / 04-08-2017 / 11:16:20 / cg" |
|
19349 | 897 |
! |
898 |
||
899 |
shortAt:index bigEndian:msb |
|
900 |
"return the 2-bytes starting at index as a signed Integer. |
|
901 |
The index is a smalltalk index (i.e. 1-based). |
|
902 |
The value is retrieved MSB-first, if the msb-arg is true; |
|
903 |
LSB-first otherwise. |
|
904 |
This is the ST80 equivalent of #signedWordAt:" |
|
905 |
||
19368 | 906 |
^ self signedInt16At:index MSB:msb |
19349 | 907 |
|
908 |
" |
|
909 |
|b| |
|
910 |
b := ByteArray new:2. |
|
911 |
b unsignedShortAt:1 put:16rFFFF. |
|
912 |
b shortAt:1 |
|
913 |
" |
|
914 |
||
915 |
"Modified: / 1.7.1996 / 21:14:38 / cg" |
|
916 |
"Created: / 5.3.1998 / 23:41:21 / stefan" |
|
917 |
! |
|
918 |
||
919 |
shortAt:index put:value |
|
920 |
"set the 2-bytes starting at index from the signed Integer value. |
|
921 |
The index is a smalltalk index (i.e. 1-based). |
|
922 |
The stored value must be in the range -32768 .. +32676. |
|
22161 | 923 |
The value is stored in the machine's natural byteorder. |
19349 | 924 |
This may be worth a primitive. |
925 |
This is the ST80 equivalent of #signedWordAt:put:" |
|
926 |
||
927 |
^ self signedInt16At:index put:value MSB:IsBigEndian |
|
928 |
||
929 |
" |
|
930 |
|b| |
|
931 |
b := ByteArray new:6. |
|
932 |
b shortAt:1 put:-1. |
|
933 |
b shortAt:3 put:-2. |
|
934 |
b shortAt:5 put:0. |
|
935 |
b inspect |
|
936 |
" |
|
937 |
||
22161 | 938 |
"Modified: / 01-07-1996 / 21:12:07 / cg" |
939 |
"Created: / 05-03-1998 / 11:02:05 / stefan" |
|
940 |
"Modified (comment): / 04-08-2017 / 11:16:46 / cg" |
|
19349 | 941 |
! |
942 |
||
943 |
shortAt:index put:value bigEndian:bigEndian |
|
944 |
"set the 2-bytes starting at index from the signed Integer value. |
|
945 |
The index is a smalltalk index (i.e. 1-based). |
|
946 |
The stored value must be in the range -32768 .. +32676. |
|
947 |
The value is stored in the byteorder given by bigEndian. |
|
948 |
This may be worth a primitive." |
|
949 |
||
950 |
^ self signedInt16At:index put:value MSB:IsBigEndian |
|
951 |
||
952 |
" |
|
953 |
|b| |
|
954 |
b := ByteArray new:4. |
|
955 |
b shortAt:1 put:1 bigEndian:true. |
|
956 |
b shortAt:3 put:1 bigEndian:false. |
|
957 |
b inspect |
|
958 |
" |
|
959 |
||
960 |
"Modified: / 1.7.1996 / 21:12:07 / cg" |
|
961 |
"Created: / 5.3.1998 / 11:02:05 / stefan" |
|
962 |
! |
|
963 |
||
964 |
signedDoubleWordAt:index |
|
965 |
"return the 4-bytes starting at index as a signed Integer. |
|
966 |
The index is a smalltalk index (i.e. 1-based). |
|
22161 | 967 |
The value is retrieved in the machine's natural byte order. |
19349 | 968 |
This may be worth a primitive." |
969 |
||
970 |
^ self signedInt32At:index MSB:IsBigEndian |
|
971 |
||
972 |
" |
|
973 |
|b| |
|
974 |
b := ByteArray new:4. |
|
975 |
b doubleWordAt:1 put:16rFFFFFFFF. |
|
976 |
(b signedDoubleWordAt:1) |
|
977 |
" |
|
978 |
" |
|
979 |
|b| |
|
980 |
b := ByteArray new:4. |
|
981 |
b signedDoubleWordAt:1 put:-1. |
|
982 |
(b doubleWordAt:1) |
|
983 |
" |
|
984 |
||
22161 | 985 |
"Modified: / 01-07-1996 / 21:11:28 / cg" |
986 |
"Modified (comment): / 04-08-2017 / 11:16:52 / cg" |
|
19349 | 987 |
! |
988 |
||
989 |
signedDoubleWordAt:index MSB:msb |
|
990 |
"return the 4-bytes starting at index as a (signed) Integer. |
|
991 |
The index is a smalltalk index (i.e. 1-based). |
|
992 |
The value is retrieved MSB-first, if the msb-arg is true; |
|
993 |
LSB-first otherwise." |
|
994 |
||
995 |
^ self signedInt32At:index MSB:msb |
|
996 |
||
997 |
" |
|
998 |
|b| |
|
999 |
||
1000 |
b := ByteArray withAll:#(1 2 3 4). |
|
1001 |
(b signedDoubleWordAt:1 MSB:true) printStringRadix:16. |
|
1002 |
(b signedDoubleWordAt:1 MSB:false) printStringRadix:16 |
|
1003 |
" |
|
1004 |
! |
|
1005 |
||
1006 |
signedDoubleWordAt:index put:value |
|
1007 |
"set the 4-bytes starting at index from the signed Integer value. |
|
1008 |
The index is a smalltalk index (i.e. 1-based). |
|
22161 | 1009 |
The value is stored in the machine's natural byte order. |
19349 | 1010 |
This may be worth a primitive." |
1011 |
||
1012 |
^ self signedInt32At:index put:value MSB:IsBigEndian |
|
1013 |
||
1014 |
" |
|
1015 |
|b| |
|
1016 |
b := ByteArray new:4. |
|
1017 |
b signedDoubleWordAt:1 put:-1. |
|
1018 |
(b doubleWordAt:1) printStringRadix:16 |
|
1019 |
" |
|
1020 |
||
22161 | 1021 |
"Modified: / 01-07-1996 / 21:11:39 / cg" |
1022 |
"Modified (comment): / 04-08-2017 / 11:16:56 / cg" |
|
19349 | 1023 |
! |
1024 |
||
1025 |
signedDoubleWordAt:index put:value MSB:msb |
|
1026 |
"set the 4-bytes starting at index from the signed Integer value. |
|
1027 |
The index is a smalltalk index (i.e. 1-based). |
|
1028 |
Depending on msb, the value is stored MSB-first or LSB-first. |
|
1029 |
This may be worth a primitive." |
|
1030 |
||
1031 |
^ self signedInt32At:index put:value MSB:msb |
|
1032 |
||
1033 |
" |
|
1034 |
|b| |
|
1035 |
b := ByteArray new:4. |
|
1036 |
b signedDoubleWordAt:1 put:-1. |
|
1037 |
(b doubleWordAt:1) printStringRadix:16 |
|
1038 |
" |
|
1039 |
||
1040 |
"Modified: 1.7.1996 / 21:11:46 / cg" |
|
15087 | 1041 |
! |
1042 |
||
1043 |
signedLongAt:index |
|
1044 |
"return the 4-bytes starting at index as a signed Integer. |
|
1045 |
The index is a smalltalk index (i.e. 1-based). |
|
19415 | 1046 |
The value is retrieved in the machine's natural byte order." |
1047 |
||
1048 |
^ self signedInt32At:index |
|
15087 | 1049 |
|
1050 |
" |
|
1051 |
|b| |
|
1052 |
b := ByteArray new:4. |
|
1053 |
b unsignedLongAt:1 put:16rFFFFFFFF. |
|
1054 |
b signedLongAt:1 |
|
1055 |
" |
|
1056 |
||
1057 |
"Modified: 1.7.1996 / 21:14:38 / cg" |
|
1058 |
! |
|
1059 |
||
1060 |
signedLongAt:index put:newValue |
|
19415 | 1061 |
"store a 4-bytes signed value starting at index. |
15087 | 1062 |
The index is a smalltalk index (i.e. 1-based). |
19415 | 1063 |
The value is in the machine's natural byte order." |
1064 |
||
1065 |
^ self signedInt32At:index put:newValue |
|
15087 | 1066 |
|
1067 |
" |
|
1068 |
|b| |
|
1069 |
b := ByteArray new:4. |
|
1070 |
b signedLongAt:1 put:-1. |
|
1071 |
b unsignedLongAt:1 |
|
1072 |
" |
|
1073 |
||
1074 |
"Modified: 1.7.1996 / 21:14:38 / cg" |
|
19349 | 1075 |
! |
1076 |
||
1077 |
signedWordAt:index |
|
1078 |
"return the 2-bytes starting at index as a signed Integer. |
|
1079 |
The index is a smalltalk index (i.e. 1-based). |
|
22161 | 1080 |
The value is retrieved in the machine's natural byte order. |
19349 | 1081 |
This may be worth a primitive." |
1082 |
||
1083 |
^ (self unsignedInt16At:index MSB:IsBigEndian) signExtendedShortValue |
|
1084 |
||
1085 |
" |
|
1086 |
|b| |
|
1087 |
b := ByteArray new:2. |
|
1088 |
b wordAt:1 put:16rFFFF. |
|
1089 |
b signedWordAt:1 |
|
1090 |
" |
|
1091 |
||
22161 | 1092 |
"Modified: / 01-07-1996 / 21:14:38 / cg" |
1093 |
"Modified (comment): / 04-08-2017 / 11:17:02 / cg" |
|
19349 | 1094 |
! |
1095 |
||
1096 |
signedWordAt:index MSB:msb |
|
1097 |
"return the 2-bytes starting at index as a signed Integer. |
|
1098 |
The index is a smalltalk index (i.e. 1-based). |
|
1099 |
The value is retrieved MSB-first if the msb-arg is true, |
|
1100 |
LSB-first otherwise. |
|
1101 |
This may be worth a primitive." |
|
1102 |
||
1103 |
^ (self unsignedInt16At:index MSB:msb) signExtendedShortValue |
|
1104 |
||
1105 |
" |
|
1106 |
|b| |
|
1107 |
b := ByteArray new:2. |
|
1108 |
b wordAt:1 put:16r0080. |
|
1109 |
b signedWordAt:1 MSB:true. |
|
1110 |
b signedWordAt:1 MSB:false. |
|
1111 |
" |
|
1112 |
||
1113 |
"Modified: 1.7.1996 / 21:15:57 / cg" |
|
1114 |
! |
|
1115 |
||
1116 |
signedWordAt:byteIndex put:anInteger |
|
1117 |
"set the 2-bytes starting at index from the signed Integer value. |
|
1118 |
The index is a smalltalk index (i.e. 1-based). |
|
1119 |
The stored value must be in the range -32768 .. +32676. |
|
1120 |
The value is stored in the machine's natural byteorder." |
|
1121 |
||
1122 |
^ self signedInt16At:byteIndex put:anInteger MSB:IsBigEndian |
|
1123 |
||
1124 |
" |
|
1125 |
|b| |
|
1126 |
b := ByteArray new:6. |
|
1127 |
b shortAt:1 put:-1. |
|
1128 |
b shortAt:3 put:-2. |
|
1129 |
b shortAt:5 put:0. |
|
1130 |
b inspect |
|
1131 |
" |
|
1132 |
||
1133 |
"Modified: / 1.7.1996 / 21:12:07 / cg" |
|
1134 |
"Modified: / 5.3.1998 / 11:01:30 / stefan" |
|
1135 |
! |
|
1136 |
||
1137 |
signedWordAt:byteIndex put:anInteger MSB:msb |
|
1138 |
"set the 2-bytes starting at index from the signed Integer value. |
|
1139 |
The index is a smalltalk index (i.e. 1-based). |
|
1140 |
The stored value must be in the range -32768 .. +32676. |
|
1141 |
The value is stored MSB-first, if the msb-arg is true; |
|
1142 |
LSB-first otherwise." |
|
1143 |
||
19415 | 1144 |
^ self signedInt16At:byteIndex put:anInteger MSB:msb |
19349 | 1145 |
|
1146 |
" |
|
1147 |
|b| |
|
1148 |
b := ByteArray new:4. |
|
1149 |
b signedWordAt:1 put:-1. |
|
1150 |
b signedWordAt:3 put:-2. |
|
1151 |
b inspect |
|
1152 |
" |
|
1153 |
||
1154 |
"Modified: 1.7.1996 / 21:12:13 / cg" |
|
1155 |
! |
|
1156 |
||
1157 |
unsignedLongAt:index |
|
1158 |
"return the 4-bytes starting at index as an (unsigned) Integer. |
|
1159 |
The index is a smalltalk index (i.e. 1-based). |
|
1160 |
The value is retrieved in the machine's natural byte order. |
|
1161 |
Subclasses may redefine this for better performance. |
|
1162 |
Same as doubleWordAt: for protocol completeness" |
|
1163 |
||
1164 |
^ self unsignedInt32At:index MSB:IsBigEndian |
|
1165 |
||
1166 |
" |
|
1167 |
|b| |
|
1168 |
||
1169 |
b := ByteArray withAll:#(1 2 3 4). |
|
1170 |
(b unsignedLongAt:1) printStringRadix:16 |
|
1171 |
" |
|
1172 |
||
1173 |
"Created: / 5.3.1998 / 11:56:53 / stefan" |
|
1174 |
"Modified: / 5.3.1998 / 14:58:48 / stefan" |
|
1175 |
! |
|
1176 |
||
1177 |
unsignedLongAt:index bigEndian:msb |
|
1178 |
"return the 4-bytes starting at index as an (unsigned) Integer. |
|
1179 |
The index is a smalltalk index (i.e. 1-based). |
|
1180 |
The value is retrieved MSB-first, if the msb-arg is true; |
|
1181 |
LSB-first otherwise. |
|
1182 |
Subclasses may redefine this for better performance. |
|
1183 |
Same as doubleWordAt:MSB: for protocol completeness" |
|
1184 |
||
1185 |
^ self unsignedInt32At:index MSB:msb |
|
1186 |
||
1187 |
" |
|
1188 |
|b| |
|
1189 |
||
1190 |
b := ByteArray withAll:#(1 2 3 4). |
|
1191 |
(b unsignedLongAt:1 bigEndian:true) printStringRadix:16. |
|
1192 |
(b unsignedLongAt:1 bigEndian:false) printStringRadix:16 |
|
1193 |
" |
|
1194 |
||
1195 |
"Modified: / 21.1.1998 / 17:42:30 / cg" |
|
1196 |
"Created: / 5.3.1998 / 11:46:05 / stefan" |
|
1197 |
! |
|
1198 |
||
1199 |
unsignedLongAt:index put:value |
|
1200 |
"set the 4-bytes starting at index from the (unsigned) Integer value. |
|
1201 |
The index is a smalltalk index (i.e. 1-based). |
|
1202 |
The value should be in the range 0 to 16rFFFFFFFF |
|
1203 |
(for negative values, the stored value is not defined). |
|
22161 | 1204 |
The value is stored in the machine's natural byte order. |
19349 | 1205 |
Subclasses may redefine this for better performance. |
1206 |
Same as doubleWordAt:put: for protocol completeness" |
|
1207 |
||
1208 |
^ self unsignedInt32At:index put:value MSB:IsBigEndian |
|
1209 |
||
1210 |
" |
|
1211 |
|b| |
|
1212 |
b := ByteArray new:4. |
|
1213 |
b unsignedLongAt:1 put:16r04030201. |
|
1214 |
b inspect |
|
1215 |
" |
|
1216 |
||
22161 | 1217 |
"Created: / 05-03-1998 / 11:57:44 / stefan" |
1218 |
"Modified: / 05-03-1998 / 14:58:59 / stefan" |
|
1219 |
"Modified (comment): / 04-08-2017 / 11:17:11 / cg" |
|
19349 | 1220 |
! |
1221 |
||
1222 |
unsignedLongAt:index put:aNumber bigEndian:msb |
|
1223 |
"set the 4-bytes starting at index from the (unsigned) Integer value. |
|
1224 |
The index is a smalltalk index (i.e. 1-based). |
|
1225 |
The value must be in the range 0 to 16rFFFFFFFF. |
|
1226 |
The value is stored MSB-first if msb is true; LSB-first otherwise. |
|
1227 |
Subclasses may redefine this for better performance. |
|
1228 |
Same as doubleWordAt:put:MSB: for protocol completeness" |
|
1229 |
||
1230 |
^ self unsignedInt32At:index put:aNumber MSB:msb |
|
1231 |
||
1232 |
" |
|
1233 |
|b| |
|
1234 |
b := ByteArray new:8. |
|
1235 |
b unsignedLongAt:1 put:16r04030201 bigEndian:true. |
|
1236 |
(b unsignedLongAt:1 bigEndian:false) printStringRadix:16 |
|
1237 |
" |
|
1238 |
||
1239 |
"Modified: / 21.1.1998 / 17:43:34 / cg" |
|
1240 |
"Created: / 5.3.1998 / 11:43:53 / stefan" |
|
1241 |
"Modified: / 5.3.1998 / 11:47:30 / stefan" |
|
1242 |
! |
|
1243 |
||
1244 |
unsignedLongLongAt:index bigEndian:msb |
|
1245 |
"return the 8-bytes starting at index as an (unsigned) Integer. |
|
1246 |
The index is a smalltalk index (i.e. 1-based). |
|
1247 |
Depending on msb, the value is retrieved MSB or LSB-first." |
|
1248 |
||
1249 |
^ self unsignedInt64At:index MSB:msb |
|
1250 |
||
1251 |
" |
|
1252 |
|b| |
|
1253 |
||
1254 |
b := ByteArray withAll:#(1 2 3 4 5 6 7 8). |
|
1255 |
(b unsignedLongLongAt:1 bigEndian:false) printStringRadix:16 |
|
1256 |
" |
|
1257 |
||
1258 |
"Modified: / 5.11.1996 / 14:06:21 / cg" |
|
1259 |
"Modified: / 5.3.1998 / 14:04:44 / stefan" |
|
1260 |
! |
|
1261 |
||
1262 |
unsignedLongLongAt:index put:anInteger |
|
1263 |
"set the 8-bytes starting at index from the (unsigned) Integer value. |
|
1264 |
The index is a smalltalk index (i.e. 1-based). |
|
1265 |
The value must be in the range 0 to 16rFFFFFFFFFFFFFFFF. |
|
22161 | 1266 |
The value is stored in the machine's natural byte order." |
19349 | 1267 |
|
1268 |
^ self unsignedInt64At:index put:anInteger MSB:IsBigEndian |
|
1269 |
||
22161 | 1270 |
"Created: / 05-03-1998 / 14:44:00 / stefan" |
1271 |
"Modified (comment): / 04-08-2017 / 11:17:23 / cg" |
|
19349 | 1272 |
! |
1273 |
||
1274 |
unsignedLongLongAt:index put:anInteger bigEndian:msb |
|
1275 |
"set the 8-bytes starting at index from the (unsigned) Integer value. |
|
1276 |
The index is a smalltalk index (i.e. 1-based). |
|
1277 |
The value must be in the range 0 to 16rFFFFFFFFFFFFFFFF. |
|
1278 |
Depending on msb, the value is stored MSB-first or LSB-first." |
|
1279 |
||
1280 |
^ self unsignedInt64At:index put:anInteger MSB:msb |
|
1281 |
||
1282 |
" |
|
1283 |
|b| |
|
1284 |
b := ByteArray new:8. |
|
1285 |
b unsignedLongLongAt:1 put:16r0807060504030201 bigEndian:false. |
|
1286 |
b inspect |
|
1287 |
" |
|
1288 |
||
1289 |
"Created: / 5.3.1998 / 14:06:02 / stefan" |
|
1290 |
! |
|
1291 |
||
1292 |
unsignedShortAt:index |
|
1293 |
"return the 2-bytes starting at index as an (unsigned) Integer. |
|
1294 |
The index is a smalltalk index (i.e. 1-based). |
|
22161 | 1295 |
The value is retrieved in the machine's natural byte order |
19349 | 1296 |
Subclasses may redefine this for better performance. |
1297 |
This is the ST80 equivalent of #wordAt:" |
|
1298 |
||
1299 |
||
1300 |
^ self unsignedInt16At:index MSB:IsBigEndian |
|
1301 |
||
22161 | 1302 |
"Created: / 05-03-1998 / 11:38:25 / stefan" |
1303 |
"Modified: / 05-03-1998 / 14:59:25 / stefan" |
|
1304 |
"Modified (comment): / 04-08-2017 / 11:17:28 / cg" |
|
19349 | 1305 |
! |
1306 |
||
1307 |
unsignedShortAt:index bigEndian:msb |
|
1308 |
"return the 2-bytes starting at index as an (unsigned) Integer. |
|
1309 |
The index is a smalltalk index (i.e. 1-based). |
|
1310 |
The value is retrieved MSB-first (high 8 bits at lower index) if msb is true; |
|
21437 | 1311 |
LSB-first (i.e. low 8-bits at lower byte index) if it's false)" |
19349 | 1312 |
|
1313 |
^ self unsignedInt16At:index MSB:msb |
|
1314 |
||
21437 | 1315 |
"Modified: / 21-01-1998 / 17:46:07 / cg" |
1316 |
"Created: / 05-03-1998 / 11:49:29 / stefan" |
|
1317 |
"Modified (comment): / 13-02-2017 / 20:34:05 / cg" |
|
19349 | 1318 |
! |
1319 |
||
1320 |
unsignedShortAt:index put:value |
|
1321 |
"set the 2-bytes starting at index from the (unsigned) Integer value. |
|
1322 |
The index is a smalltalk index (i.e. 1-based). |
|
1323 |
The stored value must be in the range 0 .. 16rFFFF. |
|
22161 | 1324 |
The value is stored in the machine's natural byteorder." |
19349 | 1325 |
|
1326 |
^ self unsignedInt16At:index put:value |
|
1327 |
||
1328 |
" |
|
1329 |
|b| |
|
1330 |
b := ByteArray new:4. |
|
1331 |
b unsignedShortAt:1 put:16r0102. |
|
1332 |
b unsignedShortAt:3 put:16r0304. |
|
1333 |
b inspect |
|
1334 |
" |
|
1335 |
||
22161 | 1336 |
"Created: / 05-03-1998 / 11:54:52 / stefan" |
1337 |
"Modified: / 05-03-1998 / 14:59:38 / stefan" |
|
1338 |
"Modified (comment): / 04-08-2017 / 11:17:31 / cg" |
|
19349 | 1339 |
! |
1340 |
||
1341 |
unsignedShortAt:index put:value bigEndian:msb |
|
1342 |
"set the 2-bytes starting at index from the (unsigned) Integer value. |
|
1343 |
The index is a smalltalk index (i.e. 1-based). |
|
1344 |
The stored value must be in the range 0 .. 16rFFFF. |
|
1345 |
The value is stored LSB-first (i.e. the low 8bits are stored at the |
|
1346 |
lower index) if msb is false, MSB-first otherwise" |
|
1347 |
||
1348 |
^ self unsignedInt16At:index put:value MSB:msb |
|
1349 |
||
1350 |
" |
|
1351 |
|b| |
|
1352 |
b := ByteArray new:8. |
|
1353 |
b unsignedShortAt:1 put:16r0102 bigEndian:false. |
|
1354 |
b unsignedShortAt:3 put:16r0304 bigEndian:false. |
|
1355 |
b unsignedShortAt:5 put:16r0102 bigEndian:true. |
|
1356 |
b unsignedShortAt:7 put:16r0304 bigEndian:true. |
|
1357 |
b inspect |
|
1358 |
" |
|
1359 |
||
1360 |
"Modified: / 21.1.1998 / 17:48:15 / cg" |
|
1361 |
"Modified: / 5.3.1998 / 11:52:28 / stefan" |
|
1362 |
! |
|
1363 |
||
1364 |
wordAt:index |
|
1365 |
"return the 2-bytes starting at index as an (unsigned) Integer. |
|
1366 |
The index is a smalltalk index (i.e. 1-based). |
|
22161 | 1367 |
The value is retrieved in the machine's natural byte order |
19349 | 1368 |
Subclasses may redefine this for better performance." |
1369 |
||
1370 |
^ self unsignedInt16At:index MSB:IsBigEndian |
|
1371 |
||
22161 | 1372 |
"Modified: / 05-03-1998 / 14:59:51 / stefan" |
1373 |
"Modified (comment): / 04-08-2017 / 11:17:35 / cg" |
|
19349 | 1374 |
! |
1375 |
||
1376 |
wordAt:index MSB:msb |
|
1377 |
"return the 2-bytes starting at index as an (unsigned) Integer. |
|
1378 |
The index is a smalltalk index (i.e. 1-based). |
|
1379 |
The value is retrieved MSB (high 8 bits at lower index) if msb is true; |
|
21437 | 1380 |
LSB-first (i.e. low 8-bits at lower byte index) if it's false. |
22261 | 1381 |
Notice: |
1382 |
the index is a byte index; thus, this allows for unaligned access to |
|
1383 |
words on any boundary. |
|
19349 | 1384 |
Question: should it be retrieve signed values ? (see ByteArray>>signedWordAt:)" |
1385 |
||
1386 |
^ self unsignedInt16At:index MSB:msb |
|
21437 | 1387 |
|
1388 |
"Modified (comment): / 13-02-2017 / 20:34:09 / cg" |
|
19349 | 1389 |
! |
1390 |
||
1391 |
wordAt:index put:value |
|
1392 |
"set the 2-bytes starting at index from the (unsigned) Integer value. |
|
1393 |
The index is a smalltalk index (i.e. 1-based). |
|
1394 |
The stored value must be in the range 0 .. 16rFFFF. |
|
22161 | 1395 |
The value is stored in the machine's natural byteorder. |
19349 | 1396 |
Question: should it accept signed values ? (see ByteArray>>signedWordAt:put:)" |
1397 |
||
1398 |
^ self unsignedInt16At:index put:value MSB:IsBigEndian |
|
1399 |
||
1400 |
" |
|
1401 |
|b| |
|
1402 |
b := ByteArray new:4. |
|
1403 |
b wordAt:1 put:16r0102. |
|
1404 |
b wordAt:3 put:16r0304. |
|
1405 |
b inspect |
|
1406 |
" |
|
1407 |
||
22161 | 1408 |
"Modified: / 05-03-1998 / 15:00:03 / stefan" |
1409 |
"Modified (comment): / 04-08-2017 / 11:17:39 / cg" |
|
19349 | 1410 |
! |
1411 |
||
1412 |
wordAt:index put:value MSB:msb |
|
1413 |
"set the 2-bytes starting at index from the (unsigned) Integer value. |
|
1414 |
The index is a smalltalk index (i.e. 1-based). |
|
1415 |
The stored value must be in the range 0 .. 16rFFFF. |
|
1416 |
The value is stored LSB-first (i.e. the low 8bits are stored at the |
|
1417 |
lower index) if msb is false, MSB-first otherwise. |
|
1418 |
Question: should it accept signed values ? (see ByteArray>>signedWordAt:put:)" |
|
1419 |
||
1420 |
^ self unsignedInt16At:index put:value MSB:msb |
|
1421 |
||
1422 |
" |
|
1423 |
b := ByteArray new:8. |
|
1424 |
b wordAt:1 put:16r0102 MSB:false. |
|
1425 |
b wordAt:3 put:16r0304 MSB:false. |
|
1426 |
b wordAt:5 put:16r0102 MSB:true. |
|
1427 |
b wordAt:7 put:16r0304 MSB:true. |
|
1428 |
b inspect |
|
1429 |
" |
|
1430 |
||
1431 |
"Modified: / 21.1.1998 / 17:48:15 / cg" |
|
1432 |
! |
|
1433 |
||
1434 |
wordAtWordIndex:int16Index |
|
1435 |
"return the unsigned short (uint16) at index, anInteger. |
|
22161 | 1436 |
Fetching in the machine's natural byte order. |
19349 | 1437 |
Indices are 1-based and scaled as appropriate to allow |
1438 |
accessing the memory as an array of word entries. |
|
1439 |
(i.e. indices are 1, 2, ...)" |
|
1440 |
||
1441 |
^ self unsignedInt16At:int16Index MSB:IsBigEndian |
|
1442 |
||
22161 | 1443 |
"Created: / 21-01-1998 / 17:48:26 / cg" |
1444 |
"Modified: / 05-03-1998 / 15:00:16 / stefan" |
|
1445 |
"Modified (comment): / 04-08-2017 / 11:17:43 / cg" |
|
19349 | 1446 |
! |
1447 |
||
1448 |
wordAtWordIndex:int16Index MSB:msb |
|
1449 |
"return the unsigned short (uint16) at index, anInteger. |
|
1450 |
Fetching is MSB if msb is true, LSB otherwise. |
|
1451 |
Indices are 1-based and scaled as appropriate to allow |
|
1452 |
accessing the memory as an array of word entries. |
|
1453 |
(i.e. indices are 1, 2, ...)" |
|
1454 |
||
1455 |
^ self unsignedInt16At:((int16Index - 1) * 2 + 1) MSB:msb |
|
1456 |
||
1457 |
"Created: / 21.1.1998 / 17:48:30 / cg" |
|
1458 |
! |
|
1459 |
||
1460 |
wordAtWordIndex:int16Index put:anInteger |
|
1461 |
"set the unsigned short (uint16) at index, anInteger. |
|
1462 |
Storing in the machine's natural byte order. |
|
1463 |
Indices are 1-based and scaled as appropriate to allow |
|
1464 |
accessing the memory as an array of word entries. |
|
1465 |
(i.e. indices are 1, 2, ...)" |
|
1466 |
||
1467 |
^ self unsignedInt16At:int16Index put:anInteger MSB:IsBigEndian |
|
1468 |
||
1469 |
"Created: / 21.1.1998 / 17:48:34 / cg" |
|
1470 |
"Modified: / 5.3.1998 / 15:00:27 / stefan" |
|
1471 |
! |
|
1472 |
||
1473 |
wordAtWordIndex:int16Index put:anInteger MSB:msb |
|
1474 |
"set the short at index, anInteger. |
|
1475 |
Storing is MSB if msb is true, LSB otherwise. |
|
1476 |
Indices are 1-based and scaled as appropriate to allow |
|
1477 |
accessing the memory as an array of word entries. |
|
1478 |
(i.e. indices are 1, 2, ...)" |
|
1479 |
||
1480 |
^ self unsignedInt16At:((int16Index - 1) * 2 + 1) put:anInteger MSB:msb |
|
1481 |
||
1482 |
"Created: / 21.1.1998 / 17:48:38 / cg" |
|
1483 |
! ! |
|
1484 |
||
1485 |
!UninterpretedBytes methodsFor:'Compatibility-Squeak'! |
|
1486 |
||
1487 |
copyFromByteArray:aByteArray |
|
1488 |
"copy as much as possible from aByteArray" |
|
1489 |
||
1490 |
self replaceBytesFrom:1 to:(self size min:aByteArray size) with:aByteArray startingAt:1 |
|
12769
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
1491 |
! ! |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
1492 |
|
9185 | 1493 |
!UninterpretedBytes methodsFor:'Compatibility-V''Age'! |
1494 |
||
1495 |
uint32At:zeroBasedIndex |
|
1496 |
"return the 4-bytes starting at index as (unsigned) Integer. |
|
19415 | 1497 |
WARNING: The index is a C index (i.e. 0-based). |
19301 | 1498 |
The value is retrieved in the machine's natural byte order. |
19415 | 1499 |
Similar to unsignedInt32At:, except for the index base" |
1500 |
||
1501 |
^ self unsignedInt32At:zeroBasedIndex+1 |
|
9185 | 1502 |
|
1503 |
" |
|
1504 |
|b| |
|
1505 |
||
1506 |
b := ByteArray withAll:#(0 0 0 0). |
|
1507 |
b uint32At:0 put:16r12345678. |
|
1508 |
b uint32At:0. |
|
1509 |
b |
|
1510 |
" |
|
1511 |
! |
|
1512 |
||
1513 |
uint32At:zeroBasedIndex put:anInteger |
|
1514 |
"set the 4-bytes starting at index to the value given by (unsigned) Integer. |
|
19415 | 1515 |
WARNING: The index is a C index (i.e. 0-based). |
1516 |
The value is stored in the machine's natural byte order. |
|
1517 |
Similar to unsignedInt32At:put:, except for the index base" |
|
1518 |
||
1519 |
^ self unsignedInt32At:zeroBasedIndex+1 put:anInteger |
|
9185 | 1520 |
|
1521 |
" |
|
1522 |
|b| |
|
1523 |
||
1524 |
b := ByteArray withAll:#(0 0 0 0). |
|
1525 |
b uint32At:0 put:16r12345678. |
|
1526 |
b |
|
1527 |
" |
|
1528 |
! ! |
|
1529 |
||
22161 | 1530 |
!UninterpretedBytes methodsFor:'accessing-128bit ints'! |
1531 |
||
1532 |
signedInt128At:index MSB:msb |
|
1533 |
"return the 16-bytes starting at index as a signed Integer. |
|
1534 |
The index is a smalltalk index (i.e. 1-based). |
|
1535 |
The value is retrieved in the given byte order. |
|
1536 |
This may be worth a primitive." |
|
1537 |
||
1538 |
|w| |
|
1539 |
||
1540 |
w := self unsignedInt128At:index MSB:msb. |
|
1541 |
(w > (16r7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)) ifTrue:[ |
|
22261 | 1542 |
^ w - (16r100000000000000000000000000000000) |
22161 | 1543 |
]. |
1544 |
^ w |
|
1545 |
||
1546 |
" |
|
1547 |
|b| |
|
1548 |
b := ByteArray new:16. |
|
1549 |
b unsignedInt128At:1 put:16rFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF MSB:true. |
|
1550 |
(b signedInt128At:1 MSB:true) |
|
1551 |
" |
|
1552 |
||
1553 |
"Created: / 04-08-2017 / 11:27:31 / cg" |
|
1554 |
! |
|
1555 |
||
1556 |
signedInt128At:byteIndex put:anInteger MSB:msb |
|
1557 |
"store a signed 128bit integer. |
|
1558 |
The index is a smalltalk index (i.e. 1-based)." |
|
1559 |
||
1560 |
|v| |
|
1561 |
||
1562 |
v := anInteger. |
|
1563 |
anInteger < 0 ifTrue:[ |
|
22261 | 1564 |
v := v + 16r100000000000000000000000000000000 |
22161 | 1565 |
]. |
1566 |
self unsignedInt128At:byteIndex put:v MSB:msb. |
|
1567 |
^ anInteger |
|
1568 |
||
1569 |
" |
|
1570 |
|b| |
|
1571 |
b := ByteArray new:16. |
|
1572 |
b signedInt128At:1 put:-1 MSB:true. |
|
1573 |
(b unsignedInt128At:1 MSB:true) |
|
1574 |
" |
|
1575 |
||
1576 |
"Created: / 04-08-2017 / 11:08:59 / cg" |
|
1577 |
! |
|
1578 |
||
1579 |
unsignedInt128At:byteIndex MSB:msb |
|
1580 |
"return the 16-bytes starting at index as an unsigned integer. |
|
1581 |
The index is a smalltalk index (i.e. 1-based). |
|
1582 |
Depending on msb, the value is retrieved MSB or LSB-first." |
|
1583 |
||
1584 |
|l |
|
1585 |
bIdx "{ Class: SmallInteger }" |
|
1586 |
delta "{ Class: SmallInteger }"| |
|
1587 |
||
1588 |
l := LargeInteger basicNew numberOfDigits:16. |
|
1589 |
msb ifTrue:[ |
|
22261 | 1590 |
bIdx := byteIndex + 15. |
1591 |
delta := -1 |
|
22161 | 1592 |
] ifFalse:[ |
22261 | 1593 |
bIdx := byteIndex. |
1594 |
delta := 1 |
|
22161 | 1595 |
]. |
1596 |
1 to:16 do:[:i | |
|
22261 | 1597 |
l digitAt:i put:(self byteAt:bIdx). |
1598 |
bIdx := bIdx + delta |
|
22161 | 1599 |
]. |
1600 |
^ l compressed |
|
1601 |
||
1602 |
" |
|
1603 |
|b| |
|
1604 |
||
1605 |
b := ByteArray withAll:#(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16). |
|
1606 |
(b unsignedInt128At:1 MSB:false) printStringRadix:16 |
|
1607 |
" |
|
1608 |
||
1609 |
"Created: / 04-08-2017 / 11:12:22 / cg" |
|
1610 |
! |
|
1611 |
||
1612 |
unsignedInt128At:byteIndex put:anInteger MSB:msb |
|
1613 |
"set the 18-bytes starting at index from the (unsigned) Integer value. |
|
1614 |
The index is a smalltalk index (i.e. 1-based). |
|
1615 |
The value must be in the range 0 to 16rFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF. |
|
1616 |
Depending on msb, the value is stored MSB-first or LSB-first." |
|
1617 |
||
1618 |
|bIdx "{ Class: SmallInteger }" |
|
1619 |
delta "{ Class: SmallInteger }"| |
|
1620 |
||
22261 | 1621 |
((anInteger < 0) |
22161 | 1622 |
or:[anInteger > 16rFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF]) ifTrue:[ |
22261 | 1623 |
^ self elementBoundsError:anInteger |
22161 | 1624 |
]. |
1625 |
||
1626 |
msb ifTrue:[ |
|
22261 | 1627 |
bIdx := byteIndex + 15. |
1628 |
delta := -1 |
|
22161 | 1629 |
] ifFalse:[ |
22261 | 1630 |
bIdx := byteIndex. |
1631 |
delta := 1 |
|
22161 | 1632 |
]. |
1633 |
1 to:16 do:[:i | |
|
22261 | 1634 |
self byteAt:bIdx put:(anInteger digitAt:i). |
1635 |
bIdx := bIdx + delta. |
|
22161 | 1636 |
]. |
1637 |
^ anInteger |
|
1638 |
||
1639 |
" |
|
1640 |
|b| |
|
1641 |
b := ByteArray new:16. |
|
1642 |
b unsignedInt128At:1 put:16r100F0E0D0C0B0A090807060504030201 MSB:false. |
|
1643 |
b inspect |
|
1644 |
" |
|
1645 |
||
1646 |
"Created: / 04-08-2017 / 11:11:02 / cg" |
|
1647 |
! ! |
|
1648 |
||
16796 | 1649 |
!UninterpretedBytes methodsFor:'accessing-arbitrary-long ints'! |
1650 |
||
19349 | 1651 |
nativeIntAt:index |
22261 | 1652 |
"return the 4- or 8-bytes (depending on the native integer/pointer size) |
19349 | 1653 |
starting at index as a signed Integer. |
1654 |
The index is a smalltalk index (i.e. 1-based). |
|
22161 | 1655 |
The value is retrieved in the machine's natural byte order, |
19349 | 1656 |
therefore, this should only be used for byte-data which is |
1657 |
only used inside this machine." |
|
1658 |
||
1659 |
|w| |
|
1660 |
||
1661 |
%{ |
|
1662 |
/* |
|
1663 |
* handle the most common cases fast ... |
|
1664 |
*/ |
|
1665 |
if (__isSmallInteger(index)) { |
|
22261 | 1666 |
unsigned char *cp; |
1667 |
INT sz; |
|
1668 |
||
1669 |
__fetchBytePointerAndSize__(self, &cp, &sz); |
|
1670 |
if (cp) { |
|
1671 |
unsigned INT idx = ((unsigned INT)__intVal(index)) - 1; |
|
1672 |
||
1673 |
if ((idx+(sizeof(INT)-1)) < sz) { |
|
1674 |
cp += idx; |
|
19349 | 1675 |
#if defined(__i386__) |
22261 | 1676 |
/* |
1677 |
* aligned or not, we don't care (i386 can do both) |
|
1678 |
*/ |
|
1679 |
{ |
|
1680 |
INT iVal = ((INT *)cp)[0]; |
|
1681 |
||
1682 |
RETURN (__MKINT(iVal)); |
|
1683 |
} |
|
19349 | 1684 |
#else |
22261 | 1685 |
/* |
1686 |
* aligned |
|
1687 |
*/ |
|
1688 |
if (((INT)cp & (sizeof(INT)-1)) == 0) { |
|
1689 |
INT iVal = ((INT *)cp)[0]; |
|
1690 |
||
1691 |
RETURN (__MKINT(iVal)); |
|
1692 |
} |
|
19349 | 1693 |
#endif |
22261 | 1694 |
} |
1695 |
} |
|
19349 | 1696 |
} |
1697 |
%}. |
|
19402 | 1698 |
SmallInteger maxBytes == 8 ifTrue:[ |
22261 | 1699 |
^ self signedInt64At:index |
19349 | 1700 |
]. |
1701 |
^ self signedInt32At:index |
|
1702 |
||
1703 |
" |
|
1704 |
|b| |
|
1705 |
b := ByteArray new:8. |
|
1706 |
b nativeIntAt:1 put:SmallInteger maxVal. |
|
1707 |
b nativeIntAt:1 |
|
1708 |
" |
|
22161 | 1709 |
|
1710 |
"Modified (comment): / 04-08-2017 / 11:18:01 / cg" |
|
19349 | 1711 |
! |
1712 |
||
1713 |
nativeIntAt:index put:value |
|
1714 |
"set the 4- or 8-bytes (depending on INT-/pointer size) starting at index from the signed Integer value. |
|
1715 |
The index is a smalltalk index (i.e. 1-based). |
|
1716 |
The value is stored in the machine's natural byte order." |
|
1717 |
||
1718 |
%{ /* NOCONTEXT */ |
|
1719 |
/* |
|
1720 |
* handle the most common cases fast ... |
|
1721 |
*/ |
|
1722 |
if (__isSmallInteger(index)) { |
|
22261 | 1723 |
unsigned char *cp; |
1724 |
INT sz; |
|
1725 |
||
1726 |
__fetchBytePointerAndSize__(self, &cp, &sz); |
|
1727 |
if (cp) { |
|
1728 |
unsigned INT idx = ((unsigned INT)__intVal(index)) - 1; |
|
1729 |
||
1730 |
if ((idx+(sizeof(INT)-1)) < sz) { |
|
1731 |
cp += idx; |
|
1732 |
/* |
|
1733 |
* aligned |
|
1734 |
*/ |
|
1735 |
if (((INT)cp & (sizeof(INT)-1)) == 0) { |
|
1736 |
INT __v; |
|
1737 |
||
1738 |
if (__isSmallInteger(value)) { |
|
1739 |
// how about a range check? |
|
1740 |
((INT *)cp)[0] = (INT)(__intVal(value)); |
|
1741 |
RETURN (value); |
|
1742 |
} |
|
1743 |
if ((__v = __signedLongIntVal(value)) != 0) { |
|
1744 |
// how about a range check? |
|
1745 |
((INT *)cp)[0] = (INT)(__v); |
|
1746 |
RETURN (value); |
|
1747 |
} |
|
1748 |
} |
|
1749 |
} |
|
1750 |
} |
|
19349 | 1751 |
} |
1752 |
%}. |
|
19402 | 1753 |
SmallInteger maxBytes == 8 ifTrue:[ |
22261 | 1754 |
^ self signedInt64At:index put:value MSB:IsBigEndian |
19349 | 1755 |
]. |
22261 | 1756 |
^ self signedInt32At:index put:value MSB:IsBigEndian |
19349 | 1757 |
|
1758 |
" |
|
1759 |
|b| |
|
1760 |
b := ByteArray new:8. |
|
1761 |
b nativeIntAt:1 put:SmallInteger maxVal. |
|
22261 | 1762 |
(b nativeIntAt:1) |
19349 | 1763 |
" |
1764 |
! |
|
1765 |
||
19402 | 1766 |
signedIntegerAt:index length:len bigEndian:bigEndian |
19197 | 1767 |
"return the n-byte signed integer starting at index. |
1768 |
With n=1, this returns the single signed byte's value, |
|
1769 |
n=2, a signed short, n=4 a signed int etc. |
|
1770 |
Useful to extract arbitrary long integers" |
|
1771 |
||
19402 | 1772 |
|val highByte |
1773 |
iIndex "{ Class:SmallInteger }" |
|
1774 |
last "{ Class:SmallInteger }"| |
|
1775 |
||
1776 |
iIndex := index. |
|
1777 |
last := iIndex + len - 1. |
|
19197 | 1778 |
|
1779 |
val := 0. |
|
1780 |
bigEndian ifTrue:[ |
|
22261 | 1781 |
highByte := self at:iIndex. |
1782 |
iIndex to:last do:[:i | |
|
1783 |
val := (val<<8) + (self byteAt:i) |
|
1784 |
] |
|
19197 | 1785 |
] ifFalse:[ |
22261 | 1786 |
highByte := self at:last. |
1787 |
last to:iIndex by:-1 do:[:i | |
|
1788 |
val := (val<<8) + (self byteAt:i) |
|
1789 |
] |
|
19197 | 1790 |
]. |
1791 |
(highByte bitTest:16r80) ifTrue:[ |
|
22261 | 1792 |
^ val - (1 bitShift:(len*8)) |
1793 |
]. |
|
19197 | 1794 |
^ val |
1795 |
||
1796 |
" |
|
1797 |
|b| |
|
1798 |
b := #[ 16r01 16rFF 16r00 16r04 16r05 ]. |
|
1799 |
(b signedIntegerAt:2 length:2 bigEndian:false). ' -> 255 (00FF) '. |
|
22261 | 1800 |
(b signedIntegerAt:2 length:2 bigEndian:true). ' -> -256 (FF00) '. |
19197 | 1801 |
|
1802 |
b := #[ 16r01 16r00 16rFF 16r04 16r05 ]. |
|
1803 |
(b signedIntegerAt:2 length:2 bigEndian:false). ' -> -256 (FF00) '. |
|
22261 | 1804 |
(b signedIntegerAt:2 length:2 bigEndian:true). ' -> 255 (00FF) '. |
19197 | 1805 |
|
1806 |
b := #[ 16r01 16r7F 16r00 16r04 16r05 ]. |
|
1807 |
(b signedIntegerAt:2 length:2 bigEndian:false). ' -> 127 (007F) '. |
|
22261 | 1808 |
(b signedIntegerAt:2 length:2 bigEndian:true). ' -> 32512 (7F00) '. |
1809 |
" |
|
1810 |
||
19197 | 1811 |
" |
1812 |
|b| |
|
1813 |
b := #[ 16r01 16r02 16r03 16r04 16r05 ]. |
|
1814 |
(b signedIntegerAt:2 length:4 bigEndian:false). |
|
1815 |
(b signedIntegerAt:2 length:4 bigEndian:true). |
|
1816 |
||
1817 |
b := #[ 16r01 16r82 16r03 16r04 16r05 ]. |
|
1818 |
(b signedIntegerAt:2 length:4 bigEndian:false). |
|
1819 |
(b signedIntegerAt:2 length:4 bigEndian:true). |
|
1820 |
||
1821 |
b := #[ 16r01 16r82 16r03 16r04 16r85 ]. |
|
1822 |
(b signedIntegerAt:2 length:4 bigEndian:false). |
|
1823 |
(b signedIntegerAt:2 length:4 bigEndian:true). |
|
1824 |
" |
|
1825 |
! |
|
1826 |
||
19402 | 1827 |
unsignedIntegerAt:index length:len bigEndian:bigEndian |
16796 | 1828 |
"return the n-byte unsigned integer starting at index. |
1829 |
With n=1, this returns the single byte's value, |
|
22161 | 1830 |
n=2, an unsigned short, n=4 an unsigned int32 etc. |
16796 | 1831 |
Useful to extract arbitrary long integers" |
1832 |
||
19402 | 1833 |
|val |
1834 |
iIndex "{ Class:SmallInteger }" |
|
1835 |
last "{ Class:SmallInteger }"| |
|
1836 |
||
1837 |
iIndex := index. |
|
1838 |
last := iIndex + len - 1. |
|
16796 | 1839 |
|
1840 |
val := 0. |
|
18346 | 1841 |
bigEndian ifTrue:[ |
22261 | 1842 |
iIndex to:last do:[:i | |
1843 |
val := (val<<8) + (self byteAt:i) |
|
1844 |
] |
|
18346 | 1845 |
] ifFalse:[ |
22261 | 1846 |
last to:iIndex by:-1 do:[:i | |
1847 |
val := (val<<8) + (self byteAt:i) |
|
1848 |
] |
|
16796 | 1849 |
]. |
1850 |
^ val |
|
1851 |
||
1852 |
" |
|
1853 |
|b| |
|
1854 |
b := #[ 16r01 16r02 16r03 16r04 16r05 ]. |
|
18346 | 1855 |
(b unsignedIntegerAt:2 length:4 bigEndian:false). |
1856 |
(b unsignedIntegerAt:2 length:4 bigEndian:true). |
|
16796 | 1857 |
" |
22161 | 1858 |
|
1859 |
"Modified (comment): / 04-08-2017 / 11:14:21 / cg" |
|
19301 | 1860 |
! |
1861 |
||
19402 | 1862 |
unsignedIntegerAt:index put:newValue length:len bigEndian:bigEndian |
19301 | 1863 |
"store the n-byte unsigned integer starting at index. |
1864 |
With n=1, this stores a single byte's value, |
|
22161 | 1865 |
n=2, an unsigned short, n=4 an unsigned int32 etc. |
19301 | 1866 |
Useful to replace arbitrary long integers" |
1867 |
||
19402 | 1868 |
|val |
1869 |
iIndex "{ Class:SmallInteger }" |
|
1870 |
last "{ Class:SmallInteger }"| |
|
1871 |
||
1872 |
iIndex := index. |
|
1873 |
last := iIndex + len - 1. |
|
19301 | 1874 |
|
1875 |
val := newValue. |
|
1876 |
bigEndian ifTrue:[ |
|
22261 | 1877 |
iIndex to:last do:[:i | |
1878 |
self byteAt:i put:(val bitAnd:16rFF). |
|
1879 |
val := val bitShift:-8. |
|
1880 |
] |
|
19301 | 1881 |
] ifFalse:[ |
22261 | 1882 |
last to:iIndex by:-1 do:[:i | |
1883 |
self byteAt:i put:(val bitAnd:16rFF). |
|
1884 |
val := val bitShift:-8. |
|
1885 |
] |
|
19301 | 1886 |
]. |
1887 |
||
1888 |
" |
|
1889 |
|b| |
|
1890 |
b := #[ 16r01 16r02 16r03 16r04 16r05 ] copy. |
|
1891 |
(b unsignedIntegerAt:2 put:16r11223344 length:3 bigEndian:false). b. |
|
1892 |
(b unsignedIntegerAt:2 put:16r11223344 length:3 bigEndian:true). b. |
|
1893 |
" |
|
22161 | 1894 |
|
1895 |
"Modified (comment): / 04-08-2017 / 11:14:32 / cg" |
|
16796 | 1896 |
! ! |
1897 |
||
3459 | 1898 |
!UninterpretedBytes methodsFor:'accessing-bytes'! |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
1899 |
|
4422 | 1900 |
bcdByteAt:index |
13724 | 1901 |
"return the bcd-value for a byte at index in the range 0..99. |
1902 |
BCD treats nibbles (4-bit) as an encoded decimal number's digits |
|
1903 |
(i.e. the value n is encoded as: ((n // 10) * 16) + (n \\ 10)" |
|
4422 | 1904 |
|
4425 | 1905 |
^ (self byteAt:index) decodeFromBCD |
4422 | 1906 |
|
1907 |
" |
|
22261 | 1908 |
#[ 16r55 ] bcdByteAt:1 |
1909 |
#[ 16r99 ] bcdByteAt:1 |
|
19352 | 1910 |
#[ 16rAA ] bcdByteAt:1 |
4422 | 1911 |
" |
13724 | 1912 |
|
1913 |
"Modified (comment): / 26-09-2011 / 11:57:33 / cg" |
|
4422 | 1914 |
! |
1915 |
||
1916 |
bcdByteAt:index put:aNumber |
|
13724 | 1917 |
"set the byte at index as bcd-value in the range 0..99. |
1918 |
BCD treats nibbles (4-bit) as an encoded decimal number's digits |
|
1919 |
(i.e. the value n is encoded as: ((n // 10) * 16) + (n \\ 10)" |
|
4422 | 1920 |
|
1921 |
(aNumber between:0 and:99) ifFalse:[ |
|
22261 | 1922 |
self elementBoundsError:aNumber. |
4422 | 1923 |
]. |
4425 | 1924 |
^ self byteAt:index put:aNumber encodeAsBCD |
4422 | 1925 |
|
1926 |
" |
|
13575 | 1927 |
(((ByteArray new:1) bcdByteAt:1 put:55; yourself) at:1) hexPrintString |
1928 |
(((ByteArray new:1) bcdByteAt:1 put:99; yourself) at:1) hexPrintString |
|
1929 |
(((ByteArray new:1) bcdByteAt:1 put:100; yourself) at:1) hexPrintString |
|
1930 |
(((ByteArray new:1) bcdByteAt:1 put:-1; yourself) at:1) hexPrintString |
|
4422 | 1931 |
" |
13724 | 1932 |
|
1933 |
"Modified (comment): / 26-09-2011 / 11:57:36 / cg" |
|
21377 | 1934 |
"Modified: / 07-02-2017 / 20:12:04 / stefan" |
4422 | 1935 |
! |
1936 |
||
19351 | 1937 |
byteAt:byteIndex |
1938 |
"return the byte at byteIndex as an unsigned 8 bit value in the range 0..255. |
|
1939 |
The index is a smalltalk index (i.e. 1-based)." |
|
1940 |
||
21377 | 1941 |
%{ /* NOCONTEXT */ |
19351 | 1942 |
if (__isSmallInteger(byteIndex)) { |
22261 | 1943 |
unsigned char *cp; |
1944 |
INT sz; |
|
1945 |
||
1946 |
__fetchBytePointerAndSize__(self, &cp, &sz); |
|
1947 |
if (cp) { |
|
1948 |
unsigned INT idx = ((unsigned INT)__intVal(byteIndex)) - 1; |
|
1949 |
||
1950 |
if (idx < sz) { |
|
1951 |
unsigned char ch = cp[idx] & 0xFF; |
|
1952 |
RETURN (__mkSmallInteger( ch )); |
|
1953 |
} |
|
1954 |
} |
|
19351 | 1955 |
} |
1956 |
%}. |
|
1957 |
||
1958 |
^ self at:byteIndex |
|
3459 | 1959 |
|
1960 |
" |
|
1961 |
|b| |
|
21377 | 1962 |
b := String new:3. |
1963 |
b byteAt:1 put:16rFF. |
|
1964 |
b byteAt:2 put:16r7F. |
|
1965 |
b byteAt:3 put:16r80. |
|
22261 | 1966 |
b byteAt:1. |
1967 |
b byteAt:2. |
|
1968 |
b byteAt:3. |
|
21377 | 1969 |
|
1970 |
|b| |
|
1971 |
b := ExternalBytes new:3. |
|
1972 |
b byteAt:1 put:16rFF. |
|
1973 |
b byteAt:2 put:16r7F. |
|
1974 |
b byteAt:3 put:16r80. |
|
22261 | 1975 |
b byteAt:1. |
1976 |
b byteAt:2. |
|
1977 |
b byteAt:3. |
|
19351 | 1978 |
" |
1979 |
||
1980 |
"Modified: / 01-07-1996 / 21:13:53 / cg" |
|
1981 |
"Modified (comment): / 26-09-2011 / 11:57:14 / cg" |
|
21377 | 1982 |
"Modified (comment): / 07-02-2017 / 19:49:13 / stefan" |
19351 | 1983 |
! |
1984 |
||
1985 |
byteAt:byteIndex put:anInteger |
|
1986 |
"set the byte at byteIndex as an unsigned 8 bit value in the range 0..255. |
|
1987 |
The index is a smalltalk index (i.e. 1-based)." |
|
1988 |
||
21377 | 1989 |
%{ /* NOCONTEXT */ |
19351 | 1990 |
if (__isSmallInteger(byteIndex) && __isSmallInteger(anInteger)) { |
22261 | 1991 |
unsigned char *cp; |
1992 |
INT sz; |
|
1993 |
INT val = __intVal(anInteger); |
|
1994 |
||
1995 |
if ( ((unsigned INT)val) <= 0xFF ) { |
|
1996 |
__fetchBytePointerAndSize__(self, &cp, &sz); |
|
1997 |
if (cp) { |
|
1998 |
unsigned INT idx = ((unsigned INT)__intVal(byteIndex)) - 1; |
|
1999 |
||
2000 |
if (idx < sz) { |
|
2001 |
cp[idx] = val & 0xFF; |
|
2002 |
RETURN (anInteger); |
|
2003 |
} |
|
2004 |
} |
|
2005 |
} |
|
19351 | 2006 |
} |
2007 |
%}. |
|
2008 |
||
2009 |
^ self at:byteIndex put:anInteger |
|
2010 |
||
2011 |
" |
|
2012 |
|b| |
|
21377 | 2013 |
b := String new:3. |
19351 | 2014 |
b byteAt:1 put:16rFF. |
2015 |
b byteAt:2 put:16r7F. |
|
2016 |
b byteAt:3 put:16r80. |
|
22261 | 2017 |
b byteAt:1. |
2018 |
b byteAt:2. |
|
2019 |
b byteAt:3. |
|
21377 | 2020 |
" |
2021 |
||
2022 |
"Modified (comment): / 07-02-2017 / 19:32:26 / stefan" |
|
19351 | 2023 |
! |
2024 |
||
2025 |
signedByteAt:byteIndex |
|
2026 |
"return the byte at byteIndex as a signed 8 bit value in the range -128..+127. |
|
2027 |
The index is a smalltalk index (i.e. 1-based). |
|
2028 |
This may be worth a primitive." |
|
2029 |
||
21377 | 2030 |
%{ /* NOCONTEXT */ |
19351 | 2031 |
/* |
2032 |
* handle the most common cases fast ... |
|
2033 |
*/ |
|
2034 |
if (__isSmallInteger(byteIndex)) { |
|
22261 | 2035 |
unsigned char *cp; |
2036 |
INT sz; |
|
2037 |
||
2038 |
__fetchBytePointerAndSize__(self, &cp, &sz); |
|
2039 |
if (cp) { |
|
2040 |
unsigned INT idx = ((unsigned INT)__intVal(byteIndex)) - 1; |
|
2041 |
char ch; |
|
2042 |
||
2043 |
if (idx < sz) { |
|
2044 |
cp += idx; |
|
2045 |
ch = cp[0]; |
|
19351 | 2046 |
# ifndef HAS_SIGNED_CHAR |
22261 | 2047 |
if ( (unsigned int)ch >= 0x80 ) { |
2048 |
ch = ch - 0x100; |
|
2049 |
} |
|
19351 | 2050 |
#endif |
22261 | 2051 |
RETURN (__mkSmallInteger( ch )); |
2052 |
} |
|
2053 |
} |
|
19351 | 2054 |
} |
2055 |
%}. |
|
2056 |
||
2057 |
^ (self byteAt:byteIndex) signExtendedByteValue |
|
2058 |
||
2059 |
" |
|
2060 |
|b| |
|
2061 |
b := ByteArray new:3. |
|
2062 |
b at:1 put:16rFF. |
|
2063 |
b at:2 put:16r7F. |
|
2064 |
b at:3 put:16r80. |
|
22261 | 2065 |
b signedByteAt:1. |
2066 |
b signedByteAt:2. |
|
2067 |
b signedByteAt:3. |
|
3459 | 2068 |
" |
2069 |
||
13724 | 2070 |
"Modified: / 01-07-1996 / 21:13:53 / cg" |
2071 |
"Modified (comment): / 26-09-2011 / 11:57:14 / cg" |
|
21377 | 2072 |
"Modified: / 07-02-2017 / 19:25:03 / stefan" |
3459 | 2073 |
! |
2074 |
||
21377 | 2075 |
signedByteAt:byteIndex put:aSignedByteValue |
2076 |
"set the byte at byteIndex to aSignedByteValue in the range -128 .. 255 |
|
3459 | 2077 |
The index is a smalltalk index (i.e. 1-based). |
21377 | 2078 |
Return the signedByteValue argument." |
2079 |
||
2080 |
|b| |
|
2081 |
||
2082 |
b := aSignedByteValue. |
|
2083 |
b < 0 ifTrue:[ |
|
22261 | 2084 |
b := 16r100 + b |
3459 | 2085 |
]. |
21377 | 2086 |
self byteAt:byteIndex put:b. |
3459 | 2087 |
^ aSignedByteValue |
2088 |
||
2089 |
" |
|
2090 |
|b| |
|
2091 |
b := ByteArray new:2. |
|
2092 |
b signedByteAt:1 put:-1. |
|
21377 | 2093 |
b at:1. |
2094 |
b signedByteAt:1. |
|
2095 |
||
2096 |
|b| |
|
2097 |
b := ByteArray new:2. |
|
2098 |
b signedByteAt:1 put:-1.0. |
|
3459 | 2099 |
" |
2100 |
||
13724 | 2101 |
"Modified: / 01-07-1996 / 21:12:37 / cg" |
2102 |
"Modified (comment): / 26-09-2011 / 11:57:18 / cg" |
|
21377 | 2103 |
"Modified (comment): / 07-02-2017 / 20:03:46 / stefan" |
3459 | 2104 |
! ! |
2105 |
||
2106 |
!UninterpretedBytes methodsFor:'accessing-floats & doubles'! |
|
2107 |
||
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2108 |
doubleAt:index |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2109 |
"return the 8-bytes starting at index as a Float. |
3433 | 2110 |
The index is a smalltalk index (i.e. 1-based). |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2111 |
Notice, that (currently) ST/X Floats are what Doubles are in ST-80. |
19415 | 2112 |
Notice also, that the bytes are expected to be in this machine's |
19629 | 2113 |
float representation and byte order - if the bytearray originated from another |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2114 |
machine, some conversion is usually needed." |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2115 |
|
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2116 |
|newFloat| |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2117 |
|
3433 | 2118 |
%{ |
2119 |
/* |
|
2120 |
* handle the most common cases fast ... |
|
2121 |
*/ |
|
2122 |
if (__isSmallInteger(index)) { |
|
22261 | 2123 |
unsigned char *cp; |
2124 |
INT sz; |
|
2125 |
||
2126 |
__fetchBytePointerAndSize__(self, &cp, &sz); |
|
2127 |
if (cp) { |
|
2128 |
INT idx = __intVal(index) - 1; |
|
2129 |
||
2130 |
if ((idx >= 0) && ((idx+(sizeof(double)-1)) < sz)) { |
|
2131 |
cp += idx; |
|
2132 |
/* |
|
2133 |
* aligned |
|
2134 |
*/ |
|
2135 |
if (((INT)cp & (sizeof(double)-1)) == 0) { |
|
2136 |
double dVal = ((double *)cp)[0]; |
|
2137 |
OBJ f; |
|
2138 |
||
2139 |
__qMKFLOAT(f, dVal); |
|
2140 |
RETURN (f); |
|
2141 |
} |
|
2142 |
} |
|
2143 |
} |
|
3433 | 2144 |
} |
2145 |
%}. |
|
2146 |
||
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2147 |
newFloat := Float basicNew. |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2148 |
1 to:8 do:[:destIndex| |
22261 | 2149 |
newFloat basicAt:destIndex put:(self byteAt:(index - 1 + destIndex)) |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2150 |
]. |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2151 |
^ newFloat. |
3433 | 2152 |
|
2153 |
" |
|
2154 |
|b| |
|
2155 |
||
2156 |
b := ByteArray new:20. |
|
2157 |
b doubleAt:1 put:(Float pi). |
|
2158 |
Transcript showCR:b. |
|
2159 |
Transcript showCR:(b doubleAt:1) |
|
2160 |
" |
|
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2161 |
! |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2162 |
|
3446
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2163 |
doubleAt:index MSB:msb |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2164 |
"return the 8-bytes starting at index as a Float. |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2165 |
The index is a smalltalk index (i.e. 1-based). |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2166 |
Notice, that (currently) ST/X Floats are what Doubles are in ST-80. |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2167 |
Notice also, that the bytes are expected to be in this machines |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2168 |
float representation - if the bytearray originated from another |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2169 |
machine, some conversion is usually needed." |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2170 |
|
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2171 |
|newFloat| |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2172 |
|
18284 | 2173 |
msb == IsBigEndian ifTrue:[ |
22261 | 2174 |
^ self doubleAt:index. |
3446
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2175 |
]. |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2176 |
|
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2177 |
newFloat := Float basicNew. |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2178 |
1 to:8 do:[:destIndex| |
22261 | 2179 |
newFloat basicAt:(9-destIndex) put:(self byteAt:(index - 1 + destIndex)) |
3446
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2180 |
]. |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2181 |
^ newFloat. |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2182 |
|
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2183 |
"Created: / 15.5.1998 / 17:21:45 / cg" |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2184 |
! |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2185 |
|
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2186 |
doubleAt:index put:aFloat |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2187 |
"store the value of the argument, aFloat into the receiver |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2188 |
starting at index. |
3433 | 2189 |
The index is a smalltalk index (i.e. 1-based). |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2190 |
Notice, that (currently) ST/X Floats are what Doubles are in ST-80. |
19415 | 2191 |
Notice also, that the bytes are expected to be in this machine's |
19629 | 2192 |
float representation and byte order - if the bytearray originated from another |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2193 |
machine, some conversion is usually needed." |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2194 |
|
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2195 |
|flt| |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2196 |
|
3433 | 2197 |
%{ |
2198 |
/* |
|
2199 |
* handle the most common cases fast ... |
|
2200 |
*/ |
|
16320 | 2201 |
if (__isSmallInteger(index)) { |
22261 | 2202 |
unsigned char *cp; |
2203 |
INT sz; |
|
2204 |
||
2205 |
__fetchBytePointerAndSize__(self, &cp, &sz); |
|
2206 |
if (cp) { |
|
2207 |
INT idx = __intVal(index) - 1; |
|
2208 |
||
2209 |
if ((idx >= 0) && ((idx+(sizeof(double)-1)) < sz)) { |
|
2210 |
cp += idx; |
|
2211 |
/* |
|
2212 |
* aligned |
|
2213 |
*/ |
|
2214 |
if (((INT)cp & (sizeof(double)-1)) == 0) { |
|
2215 |
if (__isFloat(aFloat)) { |
|
2216 |
((double *)cp)[0] = __floatVal(aFloat); |
|
2217 |
RETURN (aFloat); |
|
2218 |
} |
|
2219 |
if (__isShortFloat(aFloat)) { |
|
2220 |
((double *)cp)[0] = (double)(__shortFloatVal(aFloat)); |
|
2221 |
RETURN (aFloat); |
|
2222 |
} |
|
2223 |
if (__isSmallInteger(aFloat)) { |
|
2224 |
((double *)cp)[0] = (double)(__intVal(aFloat)); |
|
2225 |
RETURN (aFloat); |
|
2226 |
} |
|
2227 |
} |
|
2228 |
} |
|
2229 |
} |
|
3433 | 2230 |
} |
2231 |
%}. |
|
2232 |
||
16320 | 2233 |
flt := aFloat asFloat. |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2234 |
1 to:8 do:[:srcIndex| |
22261 | 2235 |
self byteAt:(index - 1 + srcIndex) put:(flt basicAt:srcIndex) |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2236 |
]. |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2237 |
^ aFloat |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2238 |
! |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2239 |
|
3446
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2240 |
doubleAt:index put:aFloat MSB:msb |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2241 |
"store the value of the argument, aFloat into the receiver |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2242 |
starting at index. |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2243 |
The index is a smalltalk index (i.e. 1-based). |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2244 |
Notice, that (currently) ST/X Floats are what Doubles are in ST-80. |
19415 | 2245 |
Notice also, that the bytes are expected to be in this machine's |
3446
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2246 |
float representation - if the bytearray originated from another |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2247 |
machine, some conversion is usually needed." |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2248 |
|
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2249 |
|flt| |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2250 |
|
18284 | 2251 |
msb == IsBigEndian ifTrue:[ |
22261 | 2252 |
^ self doubleAt:index put:aFloat. |
3446
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2253 |
]. |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2254 |
|
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2255 |
flt := aFloat asFloat. |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2256 |
1 to:8 do:[:srcIndex| |
22261 | 2257 |
self byteAt:(index - 1 + srcIndex) put:(flt basicAt:(9-srcIndex)) |
3446
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2258 |
]. |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2259 |
^ aFloat |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2260 |
|
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2261 |
"Created: / 15.5.1998 / 17:22:27 / cg" |
3447 | 2262 |
"Modified: / 15.5.1998 / 17:26:29 / cg" |
3446
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2263 |
! |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2264 |
|
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2265 |
floatAt:index |
3434 | 2266 |
"return the 4-bytes starting at index as a ShortFloat. |
3433 | 2267 |
The index is a smalltalk index (i.e. 1-based). |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2268 |
Notice, that (currently) ST/X Floats are what Doubles are in ST-80; |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2269 |
therefore this method reads a 4-byte float from the byteArray and returns |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2270 |
a float object which keeps an 8-byte double internally. |
19415 | 2271 |
Notice also, that the bytes are expected to be in this machine's |
19629 | 2272 |
float representation and byte order - if the bytearray originated from another |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2273 |
machine, some conversion is usually needed." |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2274 |
|
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2275 |
|newFloat| |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2276 |
|
3434 | 2277 |
%{ |
2278 |
/* |
|
2279 |
* handle the most common cases fast ... |
|
2280 |
*/ |
|
2281 |
if (__isSmallInteger(index)) { |
|
22261 | 2282 |
unsigned char *cp; |
2283 |
INT sz; |
|
2284 |
||
2285 |
__fetchBytePointerAndSize__(self, &cp, &sz); |
|
2286 |
if (cp) { |
|
2287 |
INT idx = __intVal(index) - 1; |
|
2288 |
||
2289 |
if ((idx >= 0) && ((idx+(sizeof(float)-1)) < sz)) { |
|
2290 |
cp += idx; |
|
2291 |
/* |
|
2292 |
* aligned |
|
2293 |
*/ |
|
2294 |
if (((INT)cp & (sizeof(float)-1)) == 0) { |
|
2295 |
float fVal = ((float *)cp)[0]; |
|
2296 |
OBJ f; |
|
2297 |
||
2298 |
__qMKSFLOAT(f, fVal); |
|
2299 |
RETURN (f); |
|
2300 |
} |
|
2301 |
} |
|
2302 |
} |
|
3434 | 2303 |
} |
2304 |
%}. |
|
2305 |
||
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2306 |
newFloat := ShortFloat basicNew. |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2307 |
1 to:4 do:[:destIndex| |
22261 | 2308 |
newFloat basicAt:destIndex put:(self byteAt:(index - 1 + destIndex)) |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2309 |
]. |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2310 |
^ newFloat. |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2311 |
! |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2312 |
|
3446
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2313 |
floatAt:index MSB:msb |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2314 |
"return the 4-bytes starting at index as a ShortFloat. |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2315 |
The index is a smalltalk index (i.e. 1-based). |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2316 |
Notice, that (currently) ST/X Floats are what Doubles are in ST-80; |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2317 |
therefore this method reads a 4-byte float from the byteArray and returns |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2318 |
a float object which keeps an 8-byte double internally. |
19415 | 2319 |
Notice also, that the bytes are expected to be in this machine's |
3446
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2320 |
float representation and order - if the bytearray originated from another |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2321 |
machine, some conversion is usually needed." |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2322 |
|
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2323 |
|newFloat| |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2324 |
|
18284 | 2325 |
msb == IsBigEndian ifTrue:[ |
22261 | 2326 |
^ self floatAt:index |
3446
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2327 |
]. |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2328 |
|
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2329 |
newFloat := ShortFloat basicNew. |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2330 |
1 to:4 do:[:destIndex| |
22261 | 2331 |
newFloat basicAt:(5-destIndex) put:(self byteAt:(index - 1 + destIndex)) |
3446
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2332 |
]. |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2333 |
^ newFloat. |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2334 |
|
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2335 |
"Modified: / 15.5.1998 / 17:20:19 / cg" |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2336 |
"Created: / 15.5.1998 / 17:20:35 / cg" |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2337 |
! |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2338 |
|
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2339 |
floatAt:index put:aFloat |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2340 |
"store the 4 bytes of value of the argument, aFloat into the receiver |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2341 |
starting at index. |
3433 | 2342 |
The index is a smalltalk index (i.e. 1-based). |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2343 |
Notice, that (currently) ST/X Floats are what Doubles are in ST-80. |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2344 |
Notice also, that the bytes are expected to be in this machines |
19629 | 2345 |
float representation and byte order - if the bytearray originated from another |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2346 |
machine, some conversion is usually needed." |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2347 |
|
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2348 |
|sflt| |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2349 |
|
3434 | 2350 |
%{ |
2351 |
/* |
|
2352 |
* handle the most common cases fast ... |
|
2353 |
*/ |
|
16320 | 2354 |
if (__isSmallInteger(index)) { |
22261 | 2355 |
unsigned char *cp; |
2356 |
INT sz; |
|
2357 |
||
2358 |
__fetchBytePointerAndSize__(self, &cp, &sz); |
|
2359 |
if (cp) { |
|
2360 |
INT idx = __intVal(index) - 1; |
|
2361 |
||
2362 |
if ((idx >= 0) && ((idx+(sizeof(float)-1)) < sz)) { |
|
2363 |
cp += idx; |
|
2364 |
/* |
|
2365 |
* aligned |
|
2366 |
*/ |
|
2367 |
if (((INT)cp & (sizeof(float)-1)) == 0) { |
|
2368 |
if (__isShortFloat(aFloat)) { |
|
2369 |
((float *)cp)[0] = __shortFloatVal(aFloat); |
|
2370 |
RETURN (self); |
|
2371 |
} |
|
2372 |
if (__isFloat(aFloat)) { |
|
2373 |
((float *)cp)[0] = (float)__floatVal(aFloat); |
|
2374 |
RETURN (self); |
|
2375 |
} |
|
2376 |
if (__isSmallInteger(aFloat)) { |
|
2377 |
((float *)cp)[0] = (float)__intVal(aFloat); |
|
2378 |
RETURN (self); |
|
2379 |
} |
|
2380 |
// bail out to smalltalk code |
|
2381 |
} |
|
2382 |
} |
|
2383 |
} |
|
3434 | 2384 |
} |
2385 |
%}. |
|
2386 |
||
16320 | 2387 |
sflt := aFloat asShortFloat. |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2388 |
1 to:4 do:[:srcIndex| |
22261 | 2389 |
self byteAt:index - 1 + srcIndex put:(sflt basicAt:srcIndex) |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2390 |
]. |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2391 |
! |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2392 |
|
3446
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2393 |
floatAt:index put:aFloat MSB:msb |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2394 |
"store the 4 bytes of value of the argument, aFloat into the receiver |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2395 |
starting at index. |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2396 |
The index is a smalltalk index (i.e. 1-based). |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2397 |
Notice, that (currently) ST/X Floats are what Doubles are in ST-80. |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2398 |
Notice also, that the bytes are expected to be in this machines |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2399 |
float representation - if the bytearray originated from another |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2400 |
machine, some conversion is usually needed." |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2401 |
|
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2402 |
|sflt| |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2403 |
|
18284 | 2404 |
msb == IsBigEndian ifTrue:[ |
22261 | 2405 |
self floatAt:index put:aFloat. |
2406 |
^ self. |
|
3446
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2407 |
]. |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2408 |
|
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2409 |
sflt := aFloat asShortFloat. |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2410 |
1 to:4 do:[:srcIndex| |
22261 | 2411 |
self byteAt:(index - 1 + srcIndex) put:(sflt basicAt:(5-srcIndex)) |
3446
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2412 |
]. |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2413 |
|
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2414 |
"Created: / 15.5.1998 / 17:20:41 / cg" |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2415 |
! |
1a8c314b5d7d
added #floatAt:MSB: , #floatAt:put:MSB: , #doubleAt:MSB: and #doubleAt:put:MSB:
Claus Gittinger <cg@exept.de>
parents:
3444
diff
changeset
|
2416 |
|
3323 | 2417 |
ieeeDoubleAt:index |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2418 |
"retrieve the 8 bytes starting at index as a float. |
3433 | 2419 |
The index is a smalltalk index (i.e. 1-based). |
3323 | 2420 |
The 8 bytes are assumed to be in IEEE floating point single precision |
19629 | 2421 |
number format in the native byte order." |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2422 |
|
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2423 |
" |
19415 | 2424 |
currently, we assume that the machine's native number format is already |
3323 | 2425 |
IEEE format - we need some more code here whenever ST/X is ported |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2426 |
to an IBM 370 or old VAX etc. |
3323 | 2427 |
To date, all supported systems use IEEE float numbers, so there should be |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2428 |
no problem. |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2429 |
" |
9258 | 2430 |
Float isIEEEFormat ifFalse:[self error:'unsupported operation']. |
3433 | 2431 |
|
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2432 |
^ self doubleAt:index |
3323 | 2433 |
|
2434 |
"Created: / 5.3.1998 / 10:50:03 / stefan" |
|
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2435 |
! |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2436 |
|
3323 | 2437 |
ieeeDoubleAt:index put:aFloat |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2438 |
"store the value of the argument, aFloat into the receiver |
3433 | 2439 |
The index is a smalltalk index (i.e. 1-based). |
3323 | 2440 |
starting at index. Storage is in IEEE floating point double precision format. |
19629 | 2441 |
(i.e. 8 bytes are stored in the native byte order)." |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2442 |
|
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2443 |
" |
19415 | 2444 |
currently, we assume that the machine's native number format is already |
3323 | 2445 |
IEEE format - we need some more code here whenever ST/X is ported |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2446 |
to an IBM 370 or old VAX etc. |
3323 | 2447 |
To date, all supported systems use IEEE float numbers, so there should be |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2448 |
no problem. |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2449 |
" |
9258 | 2450 |
Float isIEEEFormat ifFalse:[self error:'unsupported operation']. |
3433 | 2451 |
|
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2452 |
^ self doubleAt:index put:aFloat |
3323 | 2453 |
|
2454 |
"Created: / 5.3.1998 / 10:50:26 / stefan" |
|
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2455 |
! |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2456 |
|
3323 | 2457 |
ieeeFloatAt:index |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2458 |
"retrieve the 4 bytes starting at index as a float. |
3433 | 2459 |
The index is a smalltalk index (i.e. 1-based). |
3323 | 2460 |
The 4 bytes are assumed to be in IEEE floating point single precision |
19629 | 2461 |
number format in the native byte order." |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2462 |
|
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2463 |
" |
19415 | 2464 |
currently, we assume that the machine's native number format is already |
3323 | 2465 |
IEEE format - we need some more code here whenever ST/X is ported |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2466 |
to an IBM 370 or old VAX etc. |
3323 | 2467 |
To date, all supported systems use IEEE float numbers, so there should be |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2468 |
no problem. |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2469 |
" |
9258 | 2470 |
ShortFloat isIEEEFormat ifFalse:[self error:'unsupported operation']. |
3433 | 2471 |
|
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2472 |
^ self floatAt:index |
3323 | 2473 |
|
2474 |
"Created: / 5.3.1998 / 10:50:45 / stefan" |
|
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2475 |
! |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2476 |
|
3323 | 2477 |
ieeeFloatAt:index put:aFloat |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2478 |
"store the value of the argument, aFloat into the receiver |
13575 | 2479 |
starting at index, which is a smalltalk index (i.e. 1-based). |
3433 | 2480 |
Storage is in IEEE floating point single precision format. |
22261 | 2481 |
(i.e. 4 bytes are stored in the native byte order). |
2482 |
Since ST/X floats are really doubles, |
|
19629 | 2483 |
the low- order 4 bytes of the precision are lost." |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2484 |
|
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2485 |
" |
19415 | 2486 |
currently, we assume that the machine's native number format is already |
3323 | 2487 |
IEEE format - we need some more code here whenever ST/X is ported |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2488 |
to an IBM 370 or old VAX etc. |
3323 | 2489 |
To date, all supported systems use IEEE float numbers, so there should be |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2490 |
no problem. |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2491 |
" |
9258 | 2492 |
ShortFloat isIEEEFormat ifFalse:[self error:'unsupported operation']. |
3433 | 2493 |
|
16320 | 2494 |
self floatAt:index put:aFloat |
3323 | 2495 |
|
2496 |
"Created: / 5.3.1998 / 10:51:11 / stefan" |
|
22055 | 2497 |
! |
2498 |
||
2499 |
longDoubleAt:index |
|
2500 |
"return the 16-bytes starting at index as a LongDouble. |
|
2501 |
The index is a smalltalk index (i.e. 1-based). |
|
2502 |
||
2503 |
Notice, that the C-type long double might have different sizes on different |
|
2504 |
machines and may only use part of the 16 bytes; |
|
22261 | 2505 |
i.e. 10bytes (80bit) as on intel CPUS, 12 bytes (96bits) or 16 bytes (128bits). |
22055 | 2506 |
|
2507 |
Notice also, that the bytes are expected to be in this machine's |
|
22261 | 2508 |
long double representation and byte order |
22055 | 2509 |
- if the bytearray originated from another |
2510 |
machine, some conversion is usually needed." |
|
2511 |
||
2512 |
|newFloat| |
|
2513 |
||
2514 |
newFloat := LongFloat basicNew. |
|
2515 |
1 to:16 do:[:destIndex| |
|
22261 | 2516 |
newFloat basicAt:destIndex put:(self byteAt:(index - 1 + destIndex)) |
22055 | 2517 |
]. |
2518 |
^ newFloat. |
|
2519 |
||
2520 |
" |
|
2521 |
|b| |
|
2522 |
||
2523 |
b := ByteArray new:20. |
|
2524 |
b longDoubleAt:1 put:(LongFloat pi). |
|
2525 |
Transcript showCR:b. |
|
2526 |
Transcript showCR:(b longDoubleAt:1) |
|
2527 |
" |
|
2528 |
||
2529 |
"Created: / 18-07-2017 / 11:31:27 / cg" |
|
2530 |
! |
|
2531 |
||
2532 |
longDoubleAt:index MSB:msb |
|
2533 |
"return the 16-bytes starting at index as a LongDouble. |
|
2534 |
The index is a smalltalk index (i.e. 1-based). |
|
2535 |
||
2536 |
Notice, that the C-type long double has different sizes on different |
|
2537 |
machines and may only use part of the 16 bytes; |
|
22261 | 2538 |
i.e. 10bytes (80bit) as on intel CPUS, 12 bytes (96bits) or 16 bytes (128bits). |
22055 | 2539 |
|
2540 |
Notice also, that the bytes are expected to be in this machine's |
|
2541 |
long double representation - if the bytearray originated from another |
|
2542 |
machine, some conversion is usually needed." |
|
2543 |
||
2544 |
|newFloat| |
|
2545 |
||
2546 |
msb == IsBigEndian ifTrue:[ |
|
22261 | 2547 |
^ self longDoubleAt:index. |
22055 | 2548 |
]. |
2549 |
||
2550 |
newFloat := LongFloat basicNew. |
|
2551 |
1 to:16 do:[:destIndex| |
|
22261 | 2552 |
newFloat basicAt:(17-destIndex) put:(self byteAt:(index - 1 + destIndex)) |
22055 | 2553 |
]. |
2554 |
^ newFloat. |
|
2555 |
||
2556 |
"Created: / 18-07-2017 / 11:30:42 / cg" |
|
2557 |
! |
|
2558 |
||
2559 |
longDoubleAt:index put:aLongFloat |
|
2560 |
"store the value of the argument, aLongFloat as 16 bytes into the receiver |
|
2561 |
starting at index. |
|
2562 |
The index is a smalltalk index (i.e. 1-based). |
|
2563 |
LongFloats are the machine's long double numbers. |
|
22261 | 2564 |
|
22055 | 2565 |
Notice that the bytes are expected to be in this machine's |
2566 |
float representation and byte order - if the bytearray originated from another |
|
2567 |
machine, some conversion is usually needed." |
|
2568 |
||
2569 |
|flt| |
|
2570 |
||
2571 |
flt := aLongFloat asLongFloat. |
|
2572 |
1 to:16 do:[:srcIndex| |
|
22261 | 2573 |
self byteAt:(index - 1 + srcIndex) put:(flt basicAt:srcIndex) |
22055 | 2574 |
]. |
2575 |
^ aLongFloat |
|
2576 |
||
2577 |
"Created: / 18-07-2017 / 11:33:17 / cg" |
|
2578 |
! |
|
2579 |
||
2580 |
longDoubleAt:index put:aLongFloat MSB:msb |
|
2581 |
"store the value of the argument, aLongFloat as 16 bytes into the receiver |
|
2582 |
starting at index. |
|
2583 |
The index is a smalltalk index (i.e. 1-based). |
|
2584 |
Notice that the bytes are expected to be in this machine's |
|
2585 |
float representation - if the bytearray originated from another |
|
2586 |
machine, some conversion is usually needed." |
|
2587 |
||
2588 |
|flt| |
|
2589 |
||
2590 |
msb == IsBigEndian ifTrue:[ |
|
22261 | 2591 |
^ self longDoubleAt:index put:aLongFloat. |
22055 | 2592 |
]. |
2593 |
||
2594 |
flt := aLongFloat asLongFloat. |
|
2595 |
1 to:16 do:[:srcIndex| |
|
22261 | 2596 |
self byteAt:(index - 1 + srcIndex) put:(flt basicAt:(17-srcIndex)) |
22055 | 2597 |
]. |
2598 |
^ aLongFloat |
|
2599 |
||
2600 |
"Created: / 18-07-2017 / 11:33:59 / cg" |
|
3459 | 2601 |
! ! |
2602 |
||
13724 | 2603 |
!UninterpretedBytes methodsFor:'accessing-longlongs (64bit)'! |
3459 | 2604 |
|
19349 | 2605 |
signedInt64At:index |
3459 | 2606 |
"return the 8-bytes starting at index as a signed Integer. |
2607 |
The index is a smalltalk index (i.e. 1-based). |
|
22161 | 2608 |
The value is retrieved in the machine's natural byte order. |
3459 | 2609 |
This may be worth a primitive." |
2610 |
||
2611 |
|w| |
|
2612 |
||
19629 | 2613 |
w := self unsignedInt64At:index MSB:(UninterpretedBytes isBigEndian). |
3459 | 2614 |
(w > (16r7FFFFFFFFFFFFFFF)) ifTrue:[ |
22261 | 2615 |
^ w - (16r10000000000000000) |
3459 | 2616 |
]. |
2617 |
^ w |
|
2618 |
||
2619 |
" |
|
2620 |
|b| |
|
22161 | 2621 |
b := ByteArray new:8. |
2622 |
b unsignedInt64At:1 put:16rFFFFFFFFFFFFFFFF MSB:true. |
|
2623 |
(b signedInt64At:1 MSB:true) |
|
2624 |
" |
|
2625 |
||
2626 |
"Modified: / 01-07-1996 / 21:11:28 / cg" |
|
2627 |
"Created: / 05-03-1998 / 14:40:05 / stefan" |
|
2628 |
"Modified (comment): / 04-08-2017 / 11:29:37 / cg" |
|
3459 | 2629 |
! |
2630 |
||
19349 | 2631 |
signedInt64At:index MSB:msb |
3459 | 2632 |
"return the 8-bytes starting at index as a signed Integer. |
2633 |
The index is a smalltalk index (i.e. 1-based). |
|
2634 |
The value is retrieved in the given byte order. |
|
2635 |
This may be worth a primitive." |
|
2636 |
||
2637 |
|w| |
|
2638 |
||
19349 | 2639 |
w := self unsignedInt64At:index MSB:msb. |
3459 | 2640 |
(w > (16r7FFFFFFFFFFFFFFF)) ifTrue:[ |
22261 | 2641 |
^ w - (16r10000000000000000) |
3459 | 2642 |
]. |
2643 |
^ w |
|
2644 |
||
2645 |
" |
|
2646 |
|b| |
|
22161 | 2647 |
b := ByteArray new:8. |
2648 |
b unsignedInt64At:1 put:16rFFFFFFFFFFFFFFFF MSB:true. |
|
2649 |
(b signedInt64At:1 MSB:true) |
|
2650 |
" |
|
2651 |
||
2652 |
"Modified: / 05-03-1998 / 12:06:28 / stefan" |
|
2653 |
"Created: / 05-03-1998 / 14:40:54 / stefan" |
|
2654 |
"Modified: / 09-05-1998 / 01:10:59 / cg" |
|
2655 |
"Modified (comment): / 04-08-2017 / 11:29:59 / cg" |
|
3459 | 2656 |
! |
2657 |
||
22261 | 2658 |
signedInt64At:byteIndex put:anInteger |
19349 | 2659 |
"store a signed longLong (64bit) integer. |
2660 |
The index is a smalltalk index (i.e. 1-based). |
|
22161 | 2661 |
The value is stored in the machine's natural byte order. |
19349 | 2662 |
Same as #signedQuadWordAt:put: - for ST80 compatibility." |
2663 |
||
19629 | 2664 |
^ self signedInt64At:byteIndex put:anInteger MSB:(UninterpretedBytes isBigEndian) |
22161 | 2665 |
|
2666 |
"Modified (comment): / 04-08-2017 / 11:19:09 / cg" |
|
19349 | 2667 |
! |
2668 |
||
2669 |
signedInt64At:byteIndex put:anInteger MSB:msb |
|
3459 | 2670 |
"store a signed longLong (64bit) integer. |
2671 |
The index is a smalltalk index (i.e. 1-based). |
|
2672 |
Same as #signedQuadWordAt:put: - for ST80 compatibility." |
|
2673 |
||
7813 | 2674 |
|v| |
3459 | 2675 |
|
7813 | 2676 |
v := anInteger. |
2677 |
anInteger < 0 ifTrue:[ |
|
22261 | 2678 |
v := v + 16r10000000000000000 |
7813 | 2679 |
]. |
19349 | 2680 |
self unsignedInt64At:byteIndex put:v MSB:msb. |
2681 |
^ anInteger |
|
3459 | 2682 |
|
2683 |
"Created: / 9.5.1998 / 01:10:24 / cg" |
|
2684 |
"Modified: / 9.5.1998 / 01:13:34 / cg" |
|
2685 |
! |
|
2686 |
||
19629 | 2687 |
signedInt64AtLSB:byteIndex |
2688 |
"return the 8-bytes starting at index as a signed 64bit Integer. |
|
2689 |
The index is a smalltalk index (i.e. 1-based). |
|
2690 |
The value is retrieved with least significant byte first" |
|
2691 |
||
2692 |
^ self signedInt64At:byteIndex MSB:false |
|
2693 |
! |
|
2694 |
||
2695 |
signedInt64AtLSB:byteIndex put:anInteger |
|
2696 |
"set the 8-bytes starting at index from the signed Integer anInteger. |
|
2697 |
The index is a smalltalk index (i.e. 1-based). |
|
2698 |
The integer is stored with least significant byte first." |
|
2699 |
||
2700 |
^ self signedInt64At:byteIndex put:anInteger MSB:false |
|
2701 |
! |
|
2702 |
||
2703 |
signedInt64AtMSB:byteIndex |
|
2704 |
"return the 8-bytes starting at index as a signed 64bit Integer. |
|
2705 |
The index is a smalltalk index (i.e. 1-based). |
|
2706 |
The value is retrieved with most significant byte first" |
|
2707 |
||
2708 |
^ self signedInt64At:byteIndex MSB:true |
|
2709 |
! |
|
2710 |
||
2711 |
signedInt64AtMSB:byteIndex put:anInteger |
|
2712 |
"set the 8-bytes starting at index from the signed Integer anInteger. |
|
2713 |
The index is a smalltalk index (i.e. 1-based). |
|
2714 |
The integer is stored with least significant byte first." |
|
2715 |
||
2716 |
^ self signedInt64At:byteIndex put:anInteger MSB:true |
|
2717 |
! |
|
2718 |
||
19349 | 2719 |
unsignedInt64At:byteIndex |
2720 |
"return the 8-bytes starting at index in the machine's native |
|
2721 |
byteorder as an unsigned integer. |
|
22161 | 2722 |
The value is retrieved in the machine's natural byte order. |
19349 | 2723 |
The index is a smalltalk index (i.e. 1-based)" |
2724 |
||
19629 | 2725 |
^ self unsignedInt64At:byteIndex MSB:(UninterpretedBytes isBigEndian) |
19349 | 2726 |
|
2727 |
" |
|
2728 |
|b| |
|
2729 |
||
2730 |
b := ByteArray withAll:#(1 2 3 4 5 6 7 8). |
|
22161 | 2731 |
(b unsignedInt64At:1) printStringRadix:16 |
2732 |
" |
|
2733 |
||
2734 |
"Modified: / 05-11-1996 / 14:06:21 / cg" |
|
2735 |
"Modified: / 05-03-1998 / 14:04:44 / stefan" |
|
2736 |
"Modified (comment): / 04-08-2017 / 11:19:21 / cg" |
|
19349 | 2737 |
! |
2738 |
||
2739 |
unsignedInt64At:byteIndex MSB:msb |
|
2740 |
"return the 8-bytes starting at index as an unsigned integer. |
|
3459 | 2741 |
The index is a smalltalk index (i.e. 1-based). |
2742 |
Depending on msb, the value is retrieved MSB or LSB-first." |
|
2743 |
||
13575 | 2744 |
|l |
3459 | 2745 |
bIdx "{ Class: SmallInteger }" |
2746 |
delta "{ Class: SmallInteger }"| |
|
2747 |
||
2748 |
l := LargeInteger basicNew numberOfDigits:8. |
|
2749 |
msb ifTrue:[ |
|
22261 | 2750 |
bIdx := byteIndex + 7. |
2751 |
delta := -1 |
|
3459 | 2752 |
] ifFalse:[ |
22261 | 2753 |
bIdx := byteIndex. |
2754 |
delta := 1 |
|
3459 | 2755 |
]. |
2756 |
1 to:8 do:[:i | |
|
22261 | 2757 |
l digitAt:i put:(self byteAt:bIdx). |
2758 |
bIdx := bIdx + delta |
|
3459 | 2759 |
]. |
2760 |
^ l compressed |
|
2761 |
||
2762 |
" |
|
2763 |
|b| |
|
2764 |
||
2765 |
b := ByteArray withAll:#(1 2 3 4 5 6 7 8). |
|
22161 | 2766 |
(b unsignedInt64At:1 MSB:false) printStringRadix:16 |
2767 |
" |
|
22261 | 2768 |
|
22161 | 2769 |
" |
2770 |
|b| |
|
2771 |
b := ByteArray new:8. |
|
2772 |
b signedInt64At:1 put:-1 MSB:true. |
|
2773 |
(b unsignedInt64At:1 MSB:true) |
|
2774 |
" |
|
2775 |
||
2776 |
"Modified: / 05-11-1996 / 14:06:21 / cg" |
|
2777 |
"Modified: / 05-03-1998 / 14:04:44 / stefan" |
|
2778 |
"Modified (comment): / 04-08-2017 / 11:30:34 / cg" |
|
3459 | 2779 |
! |
2780 |
||
22261 | 2781 |
unsignedInt64At:byteIndex put:anInteger |
3459 | 2782 |
"set the 8-bytes starting at index from the (unsigned) Integer value. |
2783 |
The index is a smalltalk index (i.e. 1-based). |
|
2784 |
The value must be in the range 0 to 16rFFFFFFFFFFFFFFFF. |
|
19349 | 2785 |
The value is stored in the machine's natural byteorder." |
2786 |
||
19629 | 2787 |
^ self unsignedInt64At:byteIndex put:anInteger MSB:(UninterpretedBytes isBigEndian) |
19349 | 2788 |
|
2789 |
" |
|
2790 |
|b| |
|
2791 |
b := ByteArray new:10. |
|
2792 |
b unsignedInt64At:1 put:16r0807060504030201 MSB:false. |
|
2793 |
b unsignedInt64At:1 put:16r0807060504030201 MSB:true. |
|
2794 |
b inspect |
|
2795 |
" |
|
2796 |
||
2797 |
"Created: / 5.3.1998 / 14:06:02 / stefan" |
|
3459 | 2798 |
! |
2799 |
||
19349 | 2800 |
unsignedInt64At:byteIndex put:anInteger MSB:msb |
3459 | 2801 |
"set the 8-bytes starting at index from the (unsigned) Integer value. |
2802 |
The index is a smalltalk index (i.e. 1-based). |
|
2803 |
The value must be in the range 0 to 16rFFFFFFFFFFFFFFFF. |
|
2804 |
Depending on msb, the value is stored MSB-first or LSB-first." |
|
2805 |
||
2806 |
|bIdx "{ Class: SmallInteger }" |
|
2807 |
delta "{ Class: SmallInteger }"| |
|
2808 |
||
22261 | 2809 |
((anInteger < 0) |
2810 |
or:[anInteger class ~~ SmallInteger |
|
2811 |
and:[anInteger > 16rFFFFFFFFFFFFFFFF]]) ifTrue:[ |
|
2812 |
^ self elementBoundsError:anInteger |
|
3459 | 2813 |
]. |
2814 |
||
2815 |
msb ifTrue:[ |
|
22261 | 2816 |
bIdx := byteIndex + 7. |
2817 |
delta := -1 |
|
3459 | 2818 |
] ifFalse:[ |
22261 | 2819 |
bIdx := byteIndex. |
2820 |
delta := 1 |
|
3459 | 2821 |
]. |
2822 |
1 to:8 do:[:i | |
|
22261 | 2823 |
self byteAt:bIdx put:(anInteger digitAt:i). |
2824 |
bIdx := bIdx + delta. |
|
3459 | 2825 |
]. |
2826 |
^ anInteger |
|
2827 |
||
2828 |
" |
|
2829 |
|b| |
|
2830 |
b := ByteArray new:8. |
|
22161 | 2831 |
b unsignedInt64At:1 put:16r0807060504030201 MSB:false. |
3459 | 2832 |
b inspect |
2833 |
" |
|
2834 |
||
22161 | 2835 |
"Created: / 05-03-1998 / 14:06:02 / stefan" |
2836 |
"Modified (comment): / 04-08-2017 / 11:13:59 / cg" |
|
19629 | 2837 |
! |
2838 |
||
2839 |
unsignedInt64AtLSB:byteIndex |
|
2840 |
"return the 8-bytes starting at index as an unsigned 64bit Integer. |
|
2841 |
The index is a smalltalk index (i.e. 1-based). |
|
2842 |
The value is retrieved with most significant byte first" |
|
2843 |
||
2844 |
^ self unsignedInt64At:byteIndex MSB:false |
|
2845 |
! |
|
2846 |
||
2847 |
unsignedInt64AtLSB:byteIndex put:anInteger |
|
2848 |
"set the 8-bytes starting at index from the unsigned Integer anInteger. |
|
2849 |
The index is a smalltalk index (i.e. 1-based). |
|
2850 |
The integer is stored with least significant byte first." |
|
2851 |
||
2852 |
^ self unsignedInt64At:byteIndex put:anInteger MSB:false |
|
2853 |
! |
|
2854 |
||
2855 |
unsignedInt64AtMSB:byteIndex |
|
2856 |
"return the 8-bytes starting at index as an unsigned 64bit Integer. |
|
2857 |
The index is a smalltalk index (i.e. 1-based). |
|
2858 |
The value is retrieved with most significant byte first" |
|
2859 |
||
2860 |
^ self unsignedInt64At:byteIndex MSB:true |
|
2861 |
! |
|
2862 |
||
2863 |
unsignedInt64AtMSB:byteIndex put:anInteger |
|
2864 |
"set the 8-bytes starting at index from the unsigned Integer anInteger. |
|
2865 |
The index is a smalltalk index (i.e. 1-based). |
|
2866 |
The integer is stored with least significant byte first." |
|
2867 |
||
2868 |
^ self unsignedInt64At:byteIndex put:anInteger MSB:true |
|
3459 | 2869 |
! ! |
2870 |
||
13724 | 2871 |
!UninterpretedBytes methodsFor:'accessing-longs (32bit)'! |
3459 | 2872 |
|
19349 | 2873 |
signedInt32At:byteIndex |
2874 |
"return the 4-bytes starting at byteIndex as a signed Integer. |
|
3433 | 2875 |
The index is a smalltalk index (i.e. 1-based). |
19349 | 2876 |
The value is retrieved in the machine's natural byte order, |
2877 |
therefore, this should only be used for byte-data which is |
|
2878 |
only used inside this machine. |
|
19415 | 2879 |
To setup binary data packets which are to be sent to other machines, |
2880 |
or stored into a file, always use the corresponding xxx:MSB: method |
|
2881 |
and specify a definite byteOrder." |
|
19349 | 2882 |
|
2883 |
|w| |
|
2884 |
||
2885 |
%{ |
|
2886 |
/* |
|
2887 |
* handle the most common cases fast ... |
|
2888 |
*/ |
|
2889 |
if (__isSmallInteger(byteIndex)) { |
|
22261 | 2890 |
unsigned char *cp; |
2891 |
INT sz; |
|
2892 |
||
2893 |
__fetchBytePointerAndSize__(self, &cp, &sz); |
|
2894 |
if (cp) { |
|
2895 |
INT idx = __intVal(byteIndex) - 1; |
|
2896 |
||
2897 |
if ((idx >= 0) && ((idx+(4-1)) < sz)) { |
|
2898 |
int iVal; |
|
2899 |
||
2900 |
cp += idx; |
|
19349 | 2901 |
#if defined(__i386__) |
22261 | 2902 |
/* |
2903 |
* aligned or not, we don't care (i386 can do both) |
|
2904 |
*/ |
|
2905 |
{ |
|
2906 |
iVal = ((int *)cp)[0]; |
|
2907 |
RETURN (__MKINT(iVal)); |
|
2908 |
} |
|
19349 | 2909 |
#else |
19415 | 2910 |
# if defined(__x86_64__) |
22261 | 2911 |
/* |
2912 |
* aligned or not, we don't care (i386 can do both) |
|
2913 |
*/ |
|
2914 |
{ |
|
2915 |
iVal = ((int *)cp)[0]; |
|
2916 |
RETURN (__mkSmallInteger(iVal)); |
|
2917 |
} |
|
19415 | 2918 |
# else |
22261 | 2919 |
/* |
2920 |
* aligned ? |
|
2921 |
*/ |
|
2922 |
if (((INT)cp & (sizeof(int)-1)) == 0) { |
|
2923 |
iVal = ((int *)cp)[0]; |
|
2924 |
} else { |
|
19415 | 2925 |
# ifdef __LSBFIRST__ |
22261 | 2926 |
iVal = cp[0] & 0xFF; |
2927 |
iVal += (cp[1] & 0xFF)<<8; |
|
2928 |
iVal += (cp[2] & 0xFF)<<16; |
|
2929 |
iVal += (cp[3] & 0xFF)<<24; |
|
19415 | 2930 |
# else |
2931 |
# ifdef __MSBFIRST__ |
|
22261 | 2932 |
iVal = cp[0] & 0xFF; |
2933 |
iVal = (iVal<<8)+(cp[1] & 0xFF); |
|
2934 |
iVal = (iVal<<8)+(cp[2] & 0xFF); |
|
2935 |
iVal = (iVal<<8)+(cp[3] & 0xFF); |
|
19415 | 2936 |
# else |
22261 | 2937 |
{ |
2938 |
union { |
|
2939 |
int i; |
|
2940 |
char c[4]; |
|
2941 |
} u; |
|
2942 |
u.c[0] = cp[0]; |
|
2943 |
u.c[1] = cp[1]; |
|
2944 |
u.c[2] = cp[2]; |
|
2945 |
u.c[3] = cp[3]; |
|
2946 |
iVal = u.i; |
|
2947 |
} |
|
19415 | 2948 |
# endif |
2949 |
# endif |
|
2950 |
||
2951 |
# if __POINTER_SIZE__ == 8 |
|
22261 | 2952 |
RETURN (__mkSmallInteger(iVal)); |
19415 | 2953 |
# else |
22261 | 2954 |
RETURN (__MKINT(iVal)); |
19415 | 2955 |
# endif |
22261 | 2956 |
} |
19349 | 2957 |
# endif |
2958 |
#endif |
|
22261 | 2959 |
} |
2960 |
} |
|
19349 | 2961 |
} |
2962 |
%}. |
|
2963 |
||
19415 | 2964 |
^ self signedInt32At:byteIndex MSB:IsBigEndian. |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2965 |
|
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2966 |
" |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2967 |
|b| |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2968 |
b := ByteArray new:4. |
19349 | 2969 |
b unsignedLongAt:1 put:16rFFFFFFFF. |
2970 |
(b longAt:1) |
|
4515 | 2971 |
" |
19349 | 2972 |
|
2973 |
"Modified: / 1.7.1996 / 21:11:28 / cg" |
|
2974 |
"Modified: / 5.3.1998 / 12:06:28 / stefan" |
|
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2975 |
! |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
2976 |
|
19349 | 2977 |
signedInt32At:byteIndex MSB:msb |
2978 |
"return the 4-bytes starting at byteIndex as a (signed) Integer. |
|
2979 |
The byteIndex is a smalltalk index (i.e. 1-based). |
|
4515 | 2980 |
The value is retrieved MSB-first, if the msb-arg is true; |
2981 |
LSB-first otherwise." |
|
2982 |
||
13575 | 2983 |
|val |
4515 | 2984 |
ival "{ Class: SmallInteger }" |
2985 |
i "{ Class: SmallInteger }" |
|
19349 | 2986 |
bHH "{ Class: SmallInteger }" |
2987 |
bHL "{ Class: SmallInteger }" |
|
2988 |
bLH "{ Class: SmallInteger }" |
|
2989 |
bLL "{ Class: SmallInteger }"| |
|
4515 | 2990 |
|
2991 |
%{ |
|
2992 |
/* |
|
2993 |
* handle the most common cases fast ... |
|
2994 |
*/ |
|
19349 | 2995 |
if (__isSmallInteger(byteIndex)) { |
22261 | 2996 |
unsigned char *cp; |
2997 |
INT sz; |
|
2998 |
||
2999 |
__fetchBytePointerAndSize__(self, &cp, &sz); |
|
3000 |
if (cp) { |
|
3001 |
INT idx = __intVal(byteIndex) - 1; |
|
3002 |
int iVal; |
|
3003 |
||
3004 |
cp += idx; |
|
3005 |
if ((idx >= 0) && ((idx+(sizeof(int)-1)) < sz)) { |
|
3006 |
||
3007 |
if (msb == true) { |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8893
diff
changeset
|
3008 |
#if defined(__MSBFIRST__) |
22261 | 3009 |
if (((INT)cp & (sizeof(int)-1))== 0) { |
3010 |
/* |
|
3011 |
* aligned |
|
3012 |
*/ |
|
3013 |
iVal = ((int *)cp)[0]; |
|
3014 |
} else |
|
4515 | 3015 |
#endif |
22261 | 3016 |
{ |
3017 |
iVal = cp[0]; |
|
3018 |
iVal = (iVal << 8) | cp[1]; |
|
3019 |
iVal = (iVal << 8) | cp[2]; |
|
3020 |
iVal = (iVal << 8) | cp[3]; |
|
3021 |
} |
|
3022 |
} else { |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8893
diff
changeset
|
3023 |
#if defined(__i386__) || (defined(UNALIGNED_FETCH_OK) && defined(__LSBFIRST__)) |
22261 | 3024 |
/* |
3025 |
* aligned or not - we don't care |
|
3026 |
* (i386 can fetch unaligned) |
|
3027 |
*/ |
|
3028 |
iVal = ((int *)cp)[0]; |
|
4515 | 3029 |
#else |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8893
diff
changeset
|
3030 |
# if defined(__LSBFIRST__) |
22261 | 3031 |
if (((INT)cp & (sizeof(int)-1))== 0) { |
3032 |
/* |
|
3033 |
* aligned |
|
3034 |
*/ |
|
3035 |
iVal = ((int *)cp)[0]; |
|
3036 |
} else |
|
4515 | 3037 |
# endif |
22261 | 3038 |
{ |
3039 |
iVal = cp[3]; |
|
3040 |
iVal = (iVal << 8) | cp[2]; |
|
3041 |
iVal = (iVal << 8) | cp[1]; |
|
3042 |
iVal = (iVal << 8) | cp[0]; |
|
3043 |
} |
|
4515 | 3044 |
#endif |
22261 | 3045 |
} |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8893
diff
changeset
|
3046 |
#if __POINTER_SIZE__ == 8 |
22261 | 3047 |
RETURN (__mkSmallInteger(iVal)); |
4515 | 3048 |
#else |
22261 | 3049 |
RETURN (__MKINT(iVal)); |
4515 | 3050 |
#endif |
22261 | 3051 |
} |
3052 |
} |
|
4515 | 3053 |
} |
3054 |
%}. |
|
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
3055 |
|
19415 | 3056 |
val := self unsignedInt32At:byteIndex MSB:msb. |
4515 | 3057 |
(val > (16r7FFFFFFF)) ifTrue:[ |
22261 | 3058 |
^ val - (16r100000000) |
4515 | 3059 |
]. |
3060 |
^ val |
|
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
3061 |
|
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
3062 |
" |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
3063 |
|b| |
4515 | 3064 |
|
3065 |
b := ByteArray withAll:#(1 2 3 4). |
|
22261 | 3066 |
(b signedInt32At:1 MSB:true) printStringRadix:16. |
19349 | 3067 |
(b signedInt32At:1 MSB:false) printStringRadix:16 |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
3068 |
" |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
3069 |
! |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
3070 |
|
19349 | 3071 |
signedInt32At:byteIndex put:anInteger |
3072 |
"set the 4-bytes starting at index from the signed Integer anInteger. |
|
3433 | 3073 |
The index is a smalltalk index (i.e. 1-based). |
19349 | 3074 |
The integer is stored in the machine's natural byte order." |
3075 |
||
3076 |
^ self signedInt32At:byteIndex put:anInteger MSB:IsBigEndian |
|
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
3077 |
|
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
3078 |
" |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
3079 |
|b| |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
3080 |
b := ByteArray new:4. |
19349 | 3081 |
b longAt:1 put:-1. |
3082 |
(b unsignedLongAt:1) printStringRadix:16 |
|
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
3083 |
" |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
3084 |
|
19349 | 3085 |
"Modified: / 1.7.1996 / 21:11:39 / cg" |
3086 |
"Created: / 5.3.1998 / 10:57:18 / stefan" |
|
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
3087 |
! |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
3088 |
|
19349 | 3089 |
signedInt32At:byteIndex put:anInteger MSB:msb |
3090 |
"set the 4-bytes starting at byteIndex from the signed Integer value. |
|
3091 |
The byteIndex is a smalltalk index (i.e. 1-based). |
|
3092 |
||
3093 |
This is the ST80 version of #signedDoubleWordAt:put:" |
|
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
3094 |
|
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
3095 |
|v| |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
3096 |
|
19349 | 3097 |
%{ |
3098 |
/* |
|
3099 |
* handle the most common case fast ... |
|
3100 |
*/ |
|
3101 |
if (__isSmallInteger(byteIndex)) { |
|
22261 | 3102 |
unsigned char *cp; |
3103 |
INT sz; |
|
3104 |
||
3105 |
__fetchBytePointerAndSize__(self, &cp, &sz); |
|
3106 |
if (cp) { |
|
3107 |
INT idx = __intVal(byteIndex) - 1; |
|
3108 |
||
3109 |
if ((idx >= 0) && ((idx+3) < sz)) { |
|
3110 |
cp += idx; |
|
3111 |
||
3112 |
if (__isSmallInteger(anInteger)) { |
|
3113 |
INT __v = __intVal(anInteger); |
|
19349 | 3114 |
|
3115 |
# if __POINTER_SIZE__ == 8 |
|
22261 | 3116 |
if ((__v < -0x80000000L) || (__v > 0x7FFFFFFF)) { |
3117 |
goto badArg; |
|
3118 |
} |
|
19349 | 3119 |
# endif |
22261 | 3120 |
if (((INT)cp & 3) == 0) { |
3121 |
/* |
|
3122 |
* aligned |
|
3123 |
*/ |
|
3124 |
if ( |
|
19349 | 3125 |
# ifdef __LSBFIRST__ |
22261 | 3126 |
(msb == false) |
19349 | 3127 |
# else |
3128 |
# ifdef __MSBFIRST__ |
|
22261 | 3129 |
(msb == true) |
19349 | 3130 |
# else |
22261 | 3131 |
(0) |
19349 | 3132 |
# endif |
3133 |
# endif |
|
22261 | 3134 |
) { |
3135 |
((int *)cp)[0] = (int)__v; |
|
3136 |
RETURN (anInteger); |
|
3137 |
} |
|
3138 |
} |
|
3139 |
if (msb == false) { |
|
3140 |
cp[0] = __v & 0xFF; |
|
3141 |
cp[1] = (__v>>8) & 0xFF; |
|
3142 |
cp[2] = (__v>>16) & 0xFF; |
|
3143 |
cp[3] = (__v>>24) & 0xFF; |
|
3144 |
} else { |
|
3145 |
cp[0] = (__v>>24) & 0xFF; |
|
3146 |
cp[1] = (__v>>16) & 0xFF; |
|
3147 |
cp[2] = (__v>>8) & 0xFF; |
|
3148 |
cp[3] = __v & 0xFF; |
|
3149 |
} |
|
3150 |
RETURN (anInteger); |
|
3151 |
} |
|
3152 |
} |
|
3153 |
} |
|
19349 | 3154 |
} |
3155 |
badArg: ; |
|
3156 |
%}. |
|
3157 |
||
3158 |
anInteger >= 0 ifTrue:[ |
|
22261 | 3159 |
v := anInteger |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
3160 |
] ifFalse:[ |
22261 | 3161 |
v := anInteger + 16r100000000 |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
3162 |
]. |
19349 | 3163 |
self unsignedInt32At:byteIndex put:v MSB:msb. |
3164 |
^ anInteger |
|
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
3165 |
|
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
3166 |
" |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
3167 |
|b| |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
3168 |
b := ByteArray new:4. |
19349 | 3169 |
b longAt:1 put:-1. |
3170 |
(b unsignedLongAt:1) printStringRadix:16 |
|
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
3171 |
" |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
3172 |
|
19349 | 3173 |
"Modified: / 1.7.1996 / 21:11:39 / cg" |
3174 |
"Created: / 5.3.1998 / 10:57:18 / stefan" |
|
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
3175 |
! |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
3176 |
|
19629 | 3177 |
signedInt32AtLSB:byteIndex |
3178 |
"return the 4-bytes starting at index as a signed 32bit Integer. |
|
3179 |
The index is a smalltalk index (i.e. 1-based). |
|
3180 |
The value is retrieved with least significant byte first" |
|
3181 |
||
3182 |
^ self signedInt32At:byteIndex MSB:false |
|
3183 |
! |
|
3184 |
||
3185 |
signedInt32AtLSB:byteIndex put:anInteger |
|
3186 |
"set the 4-bytes starting at index from the signed Integer anInteger. |
|
3187 |
The index is a smalltalk index (i.e. 1-based). |
|
3188 |
The integer is stored with least significant byte first." |
|
3189 |
||
3190 |
^ self signedInt32At:byteIndex put:anInteger MSB:false |
|
3191 |
! |
|
3192 |
||
3193 |
signedInt32AtMSB:byteIndex |
|
3194 |
"return the 4-bytes starting at index as a signed 32bit Integer. |
|
3195 |
The index is a smalltalk index (i.e. 1-based). |
|
3196 |
The value is retrieved with most significant byte first" |
|
3197 |
||
3198 |
^ self signedInt32At:byteIndex MSB:true |
|
3199 |
! |
|
3200 |
||
3201 |
signedInt32AtMSB:byteIndex put:anInteger |
|
3202 |
"set the 4-bytes starting at index from the signed Integer anInteger. |
|
3203 |
The index is a smalltalk index (i.e. 1-based). |
|
3204 |
The integer is stored with most significant byte first." |
|
3205 |
||
3206 |
^ self signedInt32At:byteIndex put:anInteger MSB:true |
|
3207 |
! |
|
3208 |
||
19349 | 3209 |
unsignedInt32At:byteIndex |
3323 | 3210 |
"return the 4-bytes starting at index as an (unsigned) Integer. |
3433 | 3211 |
The index is a smalltalk index (i.e. 1-based). |
19415 | 3212 |
The value is retrieved in the machine's natural byte order." |
19349 | 3213 |
|
3214 |
^ self unsignedInt32At:byteIndex MSB:IsBigEndian |
|
3323 | 3215 |
|
3216 |
" |
|
3217 |
|b| |
|
3218 |
||
3219 |
b := ByteArray withAll:#(1 2 3 4). |
|
22261 | 3220 |
(b unsignedInt32At:1) printStringRadix:16 |
3323 | 3221 |
" |
3222 |
||
19349 | 3223 |
"Modified: / 5.3.1998 / 14:57:35 / stefan" |
3323 | 3224 |
! |
3225 |
||
19349 | 3226 |
unsignedInt32At:byteIndex MSB:msb |
3227 |
"return the 4-bytes starting at index as an (unsigned) Integer. |
|
3433 | 3228 |
The index is a smalltalk index (i.e. 1-based). |
19349 | 3229 |
The value is retrieved MSB-first, if the msb-arg is true; |
3230 |
LSB-first otherwise." |
|
3231 |
||
3232 |
|val |
|
3233 |
ival "{ Class: SmallInteger }" |
|
3234 |
i "{ Class: SmallInteger }" |
|
3235 |
bHH "{ Class: SmallInteger }" |
|
3236 |
bHL "{ Class: SmallInteger }" |
|
3237 |
bLH "{ Class: SmallInteger }" |
|
3238 |
bLL "{ Class: SmallInteger }"| |
|
3239 |
||
3240 |
%{ |
|
3241 |
/* |
|
3242 |
* handle the most common cases fast ... |
|
3243 |
*/ |
|
3244 |
if (__isSmallInteger(byteIndex)) { |
|
22261 | 3245 |
unsigned char *cp; |
3246 |
INT sz; |
|
3247 |
||
3248 |
__fetchBytePointerAndSize__(self, &cp, &sz); |
|
3249 |
if (cp) { |
|
3250 |
INT idx = __intVal(byteIndex) - 1; |
|
3251 |
unsigned int iVal; |
|
3252 |
||
3253 |
if ((idx >= 0) && ((idx+(sizeof(int)-1)) < sz)) { |
|
3254 |
cp += idx; |
|
3255 |
||
3256 |
if (msb == true) { |
|
19349 | 3257 |
#if defined(__MSBFIRST__) |
22261 | 3258 |
if (((INT)cp & (sizeof(int)-1))== 0) { |
3259 |
/* |
|
3260 |
* aligned |
|
3261 |
*/ |
|
3262 |
iVal = ((unsigned int *)cp)[0]; |
|
3263 |
} else |
|
19349 | 3264 |
#endif |
22261 | 3265 |
{ |
3266 |
iVal = cp[0]; |
|
3267 |
iVal = (iVal << 8) | cp[1]; |
|
3268 |
iVal = (iVal << 8) | cp[2]; |
|
3269 |
iVal = (iVal << 8) | cp[3]; |
|
3270 |
} |
|
3271 |
} else { |
|
19349 | 3272 |
#if defined(__i386__) || (defined(UNALIGNED_FETCH_OK) && defined(__LSBFIRST__)) |
22261 | 3273 |
/* |
3274 |
* aligned or not - we don't care |
|
3275 |
* (i386 can fetch unaligned) |
|
3276 |
*/ |
|
3277 |
iVal = ((unsigned int *)cp)[0]; |
|
19349 | 3278 |
#else |
3279 |
# if defined(__LSBFIRST__) |
|
22261 | 3280 |
if (((INT)cp & (sizeof(int)-1))== 0) { |
3281 |
/* |
|
3282 |
* aligned |
|
3283 |
*/ |
|
3284 |
iVal = ((unsigned int *)cp)[0]; |
|
3285 |
} else |
|
19349 | 3286 |
# endif |
22261 | 3287 |
{ |
3288 |
iVal = cp[3]; |
|
3289 |
iVal = (iVal << 8) | cp[2]; |
|
3290 |
iVal = (iVal << 8) | cp[1]; |
|
3291 |
iVal = (iVal << 8) | cp[0]; |
|
3292 |
} |
|
19349 | 3293 |
#endif |
22261 | 3294 |
} |
19349 | 3295 |
#if __POINTER_SIZE__ == 8 |
22261 | 3296 |
RETURN (__mkSmallInteger(iVal)); |
19349 | 3297 |
#else |
22261 | 3298 |
RETURN (__MKUINT(iVal)); |
19349 | 3299 |
#endif |
22261 | 3300 |
} |
3301 |
} |
|
19349 | 3302 |
} |
3303 |
%}. |
|
3304 |
||
3305 |
"/ fallBack code - non ByteArray-like receiver |
|
3306 |
"/ or funny byteIndex |
|
3307 |
||
3308 |
i := byteIndex. |
|
3309 |
msb ifFalse:[ |
|
22261 | 3310 |
bLL := self byteAt:i. |
3311 |
bLH := self byteAt:(i+1). |
|
3312 |
bHL := self byteAt:(i+2). |
|
3313 |
bHH := self byteAt:(i+3). |
|
3314 |
] ifTrue:[ |
|
3315 |
bHH := self byteAt:i. |
|
3316 |
bHL := self byteAt:(i+1). |
|
3317 |
bLH := self byteAt:(i+2). |
|
3318 |
bLL := self byteAt:(i+3). |
|
19349 | 3319 |
]. |
3320 |
ival := (bHH bitShift:8) + bHL. |
|
3321 |
ival := (ival bitShift:8) + bLH. |
|
3322 |
val := (ival bitShift:8) + bLL. |
|
3323 |
^ val |
|
3323 | 3324 |
|
3325 |
" |
|
3326 |
|b| |
|
19349 | 3327 |
|
3328 |
b := ByteArray withAll:#(1 2 3 4). |
|
3329 |
(b unsignedInt32At:1 MSB:true) printStringRadix:16. |
|
3330 |
(b unsignedInt32At:1 MSB:false) printStringRadix:16 |
|
3323 | 3331 |
" |
3332 |
! |
|
3333 |
||
19349 | 3334 |
unsignedInt32At:byteIndex put:anInteger |
3335 |
"set the 4-bytes starting at index from the (unsigned) integer value. |
|
3433 | 3336 |
The index is a smalltalk index (i.e. 1-based). |
3323 | 3337 |
The value must be in the range 0 to 16rFFFFFFFF. |
19349 | 3338 |
The value is stored in the machine's native byte order" |
3339 |
||
3340 |
^ self unsignedInt32At:byteIndex put:anInteger MSB:IsBigEndian |
|
3323 | 3341 |
|
3342 |
" |
|
3343 |
|b| |
|
3344 |
b := ByteArray new:8. |
|
19349 | 3345 |
b doubleWordAt:1 put:16r04030201 MSB:true. |
3346 |
b doubleWordAt:5 put:16r04030201 MSB:false. |
|
3347 |
b inspect |
|
3323 | 3348 |
" |
3349 |
||
3350 |
"Modified: / 21.1.1998 / 17:43:34 / cg" |
|
19349 | 3351 |
"Modified: / 5.3.1998 / 11:42:17 / stefan" |
3352 |
! |
|
3353 |
||
3354 |
unsignedInt32At:byteIndex put:anInteger MSB:msb |
|
19415 | 3355 |
"set the 4-bytes starting at byteIndex from the unsigned Integer value. |
3356 |
The byteIndex is a smalltalk index (i.e. 1-based). |
|
3357 |
||
3358 |
This is the ST80 version of #doubleWordAt:put:" |
|
3359 |
||
3360 |
|v i b1 b2 b3 b4| |
|
3361 |
||
3362 |
%{ |
|
3363 |
/* |
|
3364 |
* handle the most common case fast ... |
|
3365 |
*/ |
|
3366 |
if (__isSmallInteger(byteIndex)) { |
|
22261 | 3367 |
unsigned char *cp; |
3368 |
INT sz; |
|
3369 |
||
3370 |
__fetchBytePointerAndSize__(self, &cp, &sz); |
|
3371 |
if (cp) { |
|
3372 |
INT idx = __intVal(byteIndex) - 1; |
|
3373 |
||
3374 |
if ((idx >= 0) && ((idx+3) < sz)) { |
|
3375 |
cp += idx; |
|
3376 |
||
3377 |
if (__isSmallInteger(anInteger)) { |
|
3378 |
INT __v = __intVal(anInteger); |
|
19415 | 3379 |
|
3380 |
# if __POINTER_SIZE__ == 8 |
|
22261 | 3381 |
if ((__v < 0) || (__v > 0xFFFFFFFF)) { |
3382 |
goto badArg; |
|
3383 |
} |
|
19415 | 3384 |
# endif |
22261 | 3385 |
if (((INT)cp & 3) == 0) { |
3386 |
/* |
|
3387 |
* aligned |
|
3388 |
*/ |
|
3389 |
if ( |
|
19415 | 3390 |
# ifdef __LSBFIRST__ |
22261 | 3391 |
(msb == false) |
19415 | 3392 |
# else |
3393 |
# ifdef __MSBFIRST__ |
|
22261 | 3394 |
(msb == true) |
19415 | 3395 |
# else |
22261 | 3396 |
(0) |
19415 | 3397 |
# endif |
3398 |
# endif |
|
22261 | 3399 |
) { |
3400 |
((int *)cp)[0] = (int)__v; |
|
3401 |
RETURN (anInteger); |
|
3402 |
} |
|
3403 |
} |
|
3404 |
if (msb == false) { |
|
3405 |
cp[0] = __v & 0xFF; |
|
3406 |
cp[1] = (__v>>8) & 0xFF; |
|
3407 |
cp[2] = (__v>>16) & 0xFF; |
|
3408 |
cp[3] = (__v>>24) & 0xFF; |
|
3409 |
} else { |
|
3410 |
cp[0] = (__v>>24) & 0xFF; |
|
3411 |
cp[1] = (__v>>16) & 0xFF; |
|
3412 |
cp[2] = (__v>>8) & 0xFF; |
|
3413 |
cp[3] = __v & 0xFF; |
|
3414 |
} |
|
3415 |
RETURN (anInteger); |
|
3416 |
} |
|
3417 |
} |
|
3418 |
} |
|
19415 | 3419 |
} |
3420 |
badArg: ; |
|
3421 |
%}. |
|
19349 | 3422 |
|
3423 |
((anInteger < 0) or:[anInteger > 16rFFFFFFFF]) ifTrue:[ |
|
22261 | 3424 |
^ self elementBoundsError:anInteger |
19349 | 3425 |
]. |
3426 |
||
3427 |
i := byteIndex. |
|
3428 |
msb ifTrue:[ |
|
22261 | 3429 |
b1 := (anInteger digitAt:4). |
3430 |
b2 := (anInteger digitAt:3). |
|
3431 |
b3 := (anInteger digitAt:2). |
|
3432 |
b4 := (anInteger digitAt:1). |
|
19349 | 3433 |
] ifFalse:[ |
22261 | 3434 |
b1 := (anInteger digitAt:1). |
3435 |
b2 := (anInteger digitAt:2). |
|
3436 |
b3 := (anInteger digitAt:3). |
|
3437 |
b4 := (anInteger digitAt:4). |
|
19349 | 3438 |
]. |
3439 |
self byteAt:i put:b1. |
|
3440 |
self byteAt:(i+1) put:b2. |
|
3441 |
self byteAt:(i+2) put:b3. |
|
3442 |
self byteAt:(i+3) put:b3. |
|
3443 |
^ anInteger |
|
3459 | 3444 |
|
3445 |
" |
|
3446 |
|b| |
|
19415 | 3447 |
b := ByteArray new:4. |
3448 |
b signedInt32At:1 put:-1. |
|
22261 | 3449 |
(b unsignedInt32At:1) printStringRadix:16 |
19415 | 3450 |
" |
3451 |
" |
|
3452 |
|b| |
|
3453 |
b := ByteArray new:4. |
|
3454 |
b unsignedInt32At:1 put:16rFFFFFFFF. |
|
22261 | 3455 |
(b signedInt32At:1) |
19415 | 3456 |
" |
19629 | 3457 |
! |
3458 |
||
3459 |
unsignedInt32AtLSB:byteIndex |
|
3460 |
"return the 4-bytes starting at index as an unsigned 32bit Integer. |
|
3461 |
The index is a smalltalk index (i.e. 1-based). |
|
3462 |
The value is retrieved with least significant byte first" |
|
3463 |
||
3464 |
^ self unsignedInt32At:byteIndex MSB:false |
|
3465 |
! |
|
3466 |
||
3467 |
unsignedInt32AtLSB:byteIndex put:anInteger |
|
3468 |
"set the 4-bytes starting at index from the unsigned Integer anInteger. |
|
3469 |
The index is a smalltalk index (i.e. 1-based). |
|
3470 |
The integer is stored with least significant byte first." |
|
3471 |
||
3472 |
^ self unsignedInt32At:byteIndex put:anInteger MSB:false |
|
3473 |
! |
|
3474 |
||
3475 |
unsignedInt32AtMSB:byteIndex |
|
3476 |
"return the 4-bytes starting at index as an unsigned 32bit Integer. |
|
3477 |
The index is a smalltalk index (i.e. 1-based). |
|
3478 |
The value is retrieved with most significant byte first" |
|
3479 |
||
3480 |
^ self unsignedInt32At:byteIndex MSB:true |
|
3481 |
! |
|
3482 |
||
3483 |
unsignedInt32AtMSB:byteIndex put:anInteger |
|
3484 |
"set the 4-bytes starting at index from the unsigned Integer anInteger. |
|
3485 |
The index is a smalltalk index (i.e. 1-based). |
|
3486 |
The integer is stored with most significant byte first." |
|
3487 |
||
3488 |
^ self unsignedInt32At:byteIndex put:anInteger MSB:true |
|
19349 | 3489 |
! ! |
3490 |
||
20961 | 3491 |
!UninterpretedBytes methodsFor:'accessing-pointers'! |
3492 |
||
3493 |
pointerAt:byteIndex |
|
3494 |
"get a pointer starting at byteIndex as ExternalAddress. |
|
3495 |
The byteIndex is a smalltalk index (i.e. 1-based). |
|
3496 |
Only aligned accesses are allowed. |
|
22883 | 3497 |
The pointer is of native cpu's size (4 or 8 bytes). |
3498 |
This returns an external adress." |
|
3499 |
||
3500 |
|failReason| |
|
20961 | 3501 |
|
3502 |
%{ |
|
3503 |
if (__isSmallInteger(byteIndex)) { |
|
22883 | 3504 |
unsigned char *cp; |
3505 |
INT sz; |
|
3506 |
||
3507 |
__fetchBytePointerAndSize__(self, &cp, &sz); |
|
3508 |
if (cp) { |
|
3509 |
INT idx = __smallIntegerVal(byteIndex) - 1; |
|
3510 |
char *pointer; |
|
3511 |
||
3512 |
if ((idx >= 0) && ((idx+(sizeof(pointer)-1)) < sz)) { |
|
3513 |
cp += idx; |
|
3514 |
/* |
|
3515 |
* aligned |
|
3516 |
*/ |
|
3517 |
if (((INT)cp & (sizeof(pointer)-1)) == 0) { |
|
3518 |
pointer = ((char **)cp)[0]; |
|
3519 |
RETURN (__MKEXTERNALADDRESS(pointer)); |
|
3520 |
} |
|
3521 |
// fprintf(stderr, "cp UNALIGNED (%"_lx_")\n", (INT)cp); |
|
3522 |
failReason = @symbol(unaligned); |
|
3523 |
} else { |
|
3524 |
// fprintf(stderr, "idx(%"_ld_")+(sizeof(pointer)-1) (%d) >= sz (%"_ld_")\n", |
|
3525 |
// idx, (int)(sizeof(pointer)-1), sz); |
|
3526 |
failReason = @symbol(invalidIndex); |
|
3527 |
} |
|
3528 |
} else { |
|
3529 |
// fprintf(stderr, "cp is NULL\n"); |
|
3530 |
failReason = @symbol(nullPointer); |
|
3531 |
} |
|
20961 | 3532 |
} else { |
22883 | 3533 |
// fprintf(stderr, "non integer index\n"); |
3534 |
failReason = @symbol(invalidIndex); |
|
20961 | 3535 |
} |
3536 |
bad:; |
|
3537 |
%}. |
|
22883 | 3538 |
^ self reportError:failReason with:byteIndex |
20961 | 3539 |
|
3540 |
" |
|
3541 |
|b| |
|
3542 |
b := ByteArray new:(ExternalAddress pointerSize). |
|
3543 |
b pointerAt:1 put:(ExternalAddress newAddress:16r12345678). |
|
3544 |
Transcript showCR:((b unsignedInt32At:1) printStringRadix:16). |
|
3545 |
Transcript showCR:((b pointerAt:1)). |
|
3546 |
||
3547 |
|b| |
|
3548 |
b := ByteArray new:(ExternalAddress pointerSize). |
|
3549 |
b pointerAt:1 put:(ExternalAddress newAddress:16r12345678abcdef). |
|
3550 |
Transcript showCR:((b unsignedInt64At:1) printStringRadix:16). |
|
3551 |
Transcript showCR:((b pointerAt:1)). |
|
3552 |
" |
|
3553 |
||
3554 |
"Modified (comment): / 14-11-2016 / 17:32:23 / cg" |
|
3555 |
! |
|
3556 |
||
3557 |
pointerAt:byteIndex put:value |
|
3558 |
"set the pointer starting at byteIndex from the integer or externalAddress value. |
|
3559 |
The byteIndex is a smalltalk index (i.e. 1-based). |
|
3560 |
Only aligned accesses are allowed. |
|
3561 |
The pointer is of native cpu's size (4 or 8 bytes). |
|
3562 |
The value may be either an ExternalAddress, ExternalBytes or an Integer" |
|
3563 |
||
22883 | 3564 |
|failReason| |
3565 |
||
20961 | 3566 |
%{ |
3567 |
OBJ *pointer; |
|
3568 |
||
3569 |
if (__isExternalAddressLike(value)) { |
|
22883 | 3570 |
pointer = __externalAddressVal(value); |
20961 | 3571 |
} else if (__isExternalBytesLike(value)) { |
22883 | 3572 |
pointer = __externalBytesVal(value); |
3573 |
if (pointer == (OBJ *)0) |
|
3574 |
pointer = 0; |
|
20961 | 3575 |
} else if (value == nil) { |
22883 | 3576 |
pointer = 0; |
20961 | 3577 |
} else if (__isSmallInteger(value)) { |
22883 | 3578 |
pointer = (OBJ *)__intVal(value); |
20961 | 3579 |
} else { |
22883 | 3580 |
if ((pointer = (OBJ *)__unsignedLongIntVal(value)) == 0) { |
3581 |
// fprintf(stderr, "not a largeInt\n"); |
|
3582 |
failReason = @symbol(badValue); |
|
3583 |
goto bad; |
|
3584 |
} |
|
20961 | 3585 |
} |
3586 |
||
3587 |
if (__isSmallInteger(byteIndex)) { |
|
22883 | 3588 |
unsigned char *cp; |
3589 |
INT sz; |
|
3590 |
||
3591 |
__fetchBytePointerAndSize__(self, &cp, &sz); |
|
3592 |
if (cp) { |
|
3593 |
INT idx = __smallIntegerVal(byteIndex) - 1; |
|
3594 |
||
3595 |
if ((idx >= 0) && ((idx+(sizeof(pointer)-1)) < sz)) { |
|
3596 |
cp += idx; |
|
3597 |
/* |
|
3598 |
* aligned |
|
3599 |
*/ |
|
3600 |
if (((INT)cp & (sizeof(pointer)-1)) == 0) { |
|
3601 |
((char **)cp)[0] = (char *) pointer; |
|
3602 |
RETURN (value); |
|
3603 |
} |
|
3604 |
// fprintf(stderr, "cp unaligned\n"); |
|
3605 |
failReason = @symbol(unaligned); |
|
3606 |
} else { |
|
3607 |
// fprintf(stderr, "idx out of bounds\n"); |
|
3608 |
failReason = @symbol(invalidIndex); |
|
3609 |
} |
|
3610 |
} else { |
|
3611 |
// fprintf(stderr, "cp is null\n"); |
|
3612 |
failReason = @symbol(nullPointer); |
|
3613 |
} |
|
3614 |
} else { |
|
3615 |
// fprintf(stderr, "byteIndex not a smallInt\n"); |
|
3616 |
failReason = @symbol(invalidIndex); |
|
20961 | 3617 |
} |
3618 |
bad:; |
|
3619 |
%}. |
|
22883 | 3620 |
^ self reportError:failReason with:byteIndex |
20961 | 3621 |
|
3622 |
" |
|
3623 |
|b| |
|
3624 |
b := ByteArray new:ExternalAddress pointerSize. |
|
3625 |
b pointerAt:1 put:(ExternalAddress newAddress:16r12345678). |
|
3626 |
(b unsignedLongAt:1) printStringRadix:16 |
|
3627 |
" |
|
3628 |
||
3629 |
"Created: / 05-03-1998 / 10:57:18 / stefan" |
|
3630 |
"Modified (comment): / 14-11-2016 / 17:28:27 / cg" |
|
3631 |
! |
|
3632 |
||
3633 |
pointerValueAt:byteIndex |
|
3634 |
"get a pointer value starting at byteIndex as unsigned integer. |
|
3635 |
The byteIndex is a smalltalk index (i.e. 1-based). |
|
3636 |
Only aligned accesses are allowed. |
|
3637 |
The pointer is of native cpu's size (4 or 8 bytes). |
|
3638 |
This returns an int with sizeof the machines's native pointer (4 or 8 bytes)" |
|
3639 |
||
22883 | 3640 |
|failReason| |
20961 | 3641 |
%{ |
3642 |
if (__isSmallInteger(byteIndex)) { |
|
22883 | 3643 |
unsigned char *cp; |
3644 |
INT sz; |
|
3645 |
||
3646 |
__fetchBytePointerAndSize__(self, &cp, &sz); |
|
3647 |
if (cp) { |
|
3648 |
INT idx = __smallIntegerVal(byteIndex) - 1; |
|
3649 |
char *pointer; |
|
3650 |
||
3651 |
if ((idx >= 0) && ((idx+(sizeof(pointer)-1)) < sz)) { |
|
3652 |
cp += idx; |
|
3653 |
/* |
|
3654 |
* aligned |
|
3655 |
*/ |
|
3656 |
if (((INT)cp & (sizeof(pointer)-1)) == 0) { |
|
3657 |
pointer = ((char **)cp)[0]; |
|
3658 |
RETURN (__MKUINT((INT)(pointer))); |
|
3659 |
} |
|
3660 |
// printf("cp UNALIGNED (%"_lx_")\n", (INT)cp); |
|
3661 |
failReason = @symbol(unaligned); |
|
3662 |
} else { |
|
3663 |
// printf("idx(%"_ld_")+(sizeof(pointer)-1) (%d) >= sz (%"_ld_")\n", |
|
3664 |
// idx, (int)(sizeof(pointer)-1), sz); |
|
3665 |
failReason = @symbol(invalidIndex); |
|
3666 |
} |
|
3667 |
} else { |
|
3668 |
// fprintf(stderr, "cp is NULL\n"); |
|
3669 |
failReason = @symbol(nullPointer); |
|
3670 |
} |
|
20961 | 3671 |
} else { |
22883 | 3672 |
// fprintf(stderr, "non integer index\n"); |
3673 |
failReason = @symbol(invalidIndex); |
|
20961 | 3674 |
} |
3675 |
bad:; |
|
3676 |
%}. |
|
3677 |
||
22883 | 3678 |
^ self reportError:failReason with:byteIndex |
20961 | 3679 |
|
3680 |
" |
|
3681 |
|b| |
|
3682 |
b := ByteArray new:(ExternalAddress pointerSize). |
|
3683 |
b pointerAt:1 put:(ExternalAddress newAddress:16r12345678). |
|
3684 |
Transcript showCR:((b unsignedLongAt:1) printStringRadix:16). |
|
3685 |
Transcript showCR:((b pointerAt:1)). |
|
3686 |
Transcript showCR:((b pointerValueAt:1)). |
|
3687 |
" |
|
3688 |
||
3689 |
"Modified (comment): / 14-11-2016 / 17:28:33 / cg" |
|
3690 |
! ! |
|
3691 |
||
19349 | 3692 |
!UninterpretedBytes methodsFor:'accessing-shorts (16bit)'! |
3693 |
||
3694 |
signedInt16At:byteIndex |
|
3459 | 3695 |
"return the 2-bytes starting at index as a signed Integer. |
3696 |
The index is a smalltalk index (i.e. 1-based). |
|
19415 | 3697 |
The value is retrieved in the machine's natural byte order." |
3459 | 3698 |
|
19349 | 3699 |
^ (self unsignedInt16At:byteIndex) signExtendedShortValue |
3323 | 3700 |
|
3701 |
" |
|
3702 |
|b| |
|
3459 | 3703 |
b := ByteArray new:2. |
3704 |
b wordAt:1 put:16rFFFF. |
|
13575 | 3705 |
b signedWordAt:1 |
3323 | 3706 |
" |
3707 |
||
3459 | 3708 |
"Modified: 1.7.1996 / 21:14:38 / cg" |
3323 | 3709 |
! |
3710 |
||
19349 | 3711 |
signedInt16At:byteIndex MSB:msb |
3459 | 3712 |
"return the 2-bytes starting at index as a signed Integer. |
3433 | 3713 |
The index is a smalltalk index (i.e. 1-based). |
19366 | 3714 |
The value is retrieved MSB (high 8 bits at lower index) if msb is true; |
3715 |
LSB-first (i.e. low 8-bits at lower byte index) if it's false. |
|
22261 | 3716 |
Notice: |
3717 |
the index is a byte index; thus, this allows for unaligned access to |
|
3718 |
words on any boundary." |
|
19366 | 3719 |
|
3720 |
|b1 "{ Class: SmallInteger }" |
|
3721 |
b2 "{ Class: SmallInteger }"| |
|
3722 |
||
3723 |
%{ |
|
3724 |
/* |
|
3725 |
* handle the most common cases fast ... |
|
3726 |
*/ |
|
3727 |
if (__isSmallInteger(byteIndex)) { |
|
22261 | 3728 |
unsigned char *cp; |
3729 |
INT sz; |
|
3730 |
||
3731 |
__fetchBytePointerAndSize__(self, &cp, &sz); |
|
3732 |
if (cp) { |
|
3733 |
INT idx = __intVal(byteIndex) - 1; |
|
3734 |
||
3735 |
if ((idx >= 0) && ((idx+(2-1)) < sz)) { |
|
3736 |
short sVal; |
|
3737 |
||
3738 |
cp += idx; |
|
3739 |
if (msb == false) { |
|
19366 | 3740 |
#if defined(__i386__) || (defined(__LSBFIRST__) && defined(UNALIGNED_FETCH_OK)) |
22261 | 3741 |
/* |
3742 |
* aligned or not, we don't care (i386 can do both) |
|
3743 |
*/ |
|
3744 |
sVal = ((short *)cp)[0]; |
|
19366 | 3745 |
#else |
22261 | 3746 |
sVal = (cp[0] & 0xFF) | ((cp[1] & 0xFF) << 8); |
19366 | 3747 |
#endif |
22261 | 3748 |
} else { |
3749 |
sVal = ((cp[0] & 0xFF) << 8) | (cp[1] & 0xFF); |
|
3750 |
} |
|
3751 |
RETURN (__mkSmallInteger(sVal)); |
|
3752 |
} |
|
3753 |
} |
|
19366 | 3754 |
} |
3755 |
%}. |
|
3756 |
||
3757 |
b1 := self byteAt:byteIndex. |
|
3758 |
b2 := self byteAt:(byteIndex + 1). |
|
3759 |
msb ifTrue:[ |
|
22261 | 3760 |
^ ((b1 bitShift:8) + b2) signExtendedShortValue |
19366 | 3761 |
]. |
3762 |
^ ((b2 bitShift:8) + b1) signExtendedShortValue |
|
3459 | 3763 |
! |
3764 |
||
19349 | 3765 |
signedInt16At:index put:anInteger |
3459 | 3766 |
"set the 2-bytes starting at index from the signed Integer value. |
3767 |
The index is a smalltalk index (i.e. 1-based). |
|
3768 |
The stored value must be in the range -32768 .. +32676. |
|
19349 | 3769 |
The value is stored in the machine's natural byte order." |
3770 |
||
3771 |
^ self signedInt16At:index put:anInteger MSB:IsBigEndian |
|
3459 | 3772 |
|
3773 |
" |
|
3774 |
|b| |
|
19349 | 3775 |
b := ByteArray new:4. |
3776 |
b signedInt16At:1 put:-2. |
|
3777 |
b signedInt16At:3 put:-3. |
|
3323 | 3778 |
b inspect |
3779 |
" |
|
3780 |
||
19349 | 3781 |
"Modified: 1.7.1996 / 21:12:13 / cg" |
3459 | 3782 |
! |
3783 |
||
19366 | 3784 |
signedInt16At:byteIndex put:anInteger MSB:msb |
3785 |
"set the 2-bytes starting at byteIndex from the signed integer value. |
|
3786 |
The byteIndex is a smalltalk index (i.e. 1-based). |
|
3459 | 3787 |
The stored value must be in the range -32768 .. +32676. |
3788 |
The value is stored MSB-first, if the msb-arg is true; |
|
19366 | 3789 |
LSB-first otherwise." |
3790 |
||
3791 |
%{ /* NOCONTEXT */ |
|
3792 |
/* |
|
3793 |
* handle the most common case fast ... |
|
3794 |
*/ |
|
3795 |
if (__isSmallInteger(byteIndex)) { |
|
22261 | 3796 |
unsigned char *cp; |
3797 |
INT sz; |
|
3798 |
||
3799 |
__fetchBytePointerAndSize__(self, &cp, &sz); |
|
3800 |
if (cp) { |
|
3801 |
INT idx = __intVal(byteIndex) - 1; |
|
3802 |
||
3803 |
if ((idx >= 0) && ((idx+1) < sz)) { |
|
3804 |
cp += idx; |
|
3805 |
||
3806 |
if (__isSmallInteger(anInteger)) { |
|
3807 |
INT __v = __intVal(anInteger); |
|
3808 |
||
3809 |
if ((__v < -0x8000L) || (__v > 0x7FFF)) { |
|
3810 |
goto badArg; |
|
3811 |
} |
|
3812 |
if (msb == false) { |
|
19366 | 3813 |
#if defined(__i386__) || (defined(__LSBFIRST__) && defined(UNALIGNED_FETCH_OK)) |
22261 | 3814 |
((short *)cp)[0] = (short)__v; |
19366 | 3815 |
#else |
22261 | 3816 |
cp[0] = __v & 0xFF; |
3817 |
cp[1] = (__v >> 8) & 0xFF; |
|
19366 | 3818 |
#endif |
22261 | 3819 |
} else { |
3820 |
cp[0] = (__v >> 8) & 0xFF; |
|
3821 |
cp[1] = __v & 0xFF; |
|
3822 |
} |
|
3823 |
RETURN (anInteger); |
|
3824 |
} |
|
3825 |
} |
|
3826 |
} |
|
19366 | 3827 |
} |
3828 |
badArg: ; |
|
3829 |
%}. |
|
19349 | 3830 |
anInteger >= 0 ifTrue:[ |
22261 | 3831 |
self unsignedInt16At:byteIndex put:anInteger MSB:msb. |
3459 | 3832 |
] ifFalse:[ |
22261 | 3833 |
self unsignedInt16At:byteIndex put:(16r10000 + anInteger) MSB:msb. |
3459 | 3834 |
]. |
19349 | 3835 |
^ anInteger |
3459 | 3836 |
|
3837 |
" |
|
3838 |
|b| |
|
3839 |
b := ByteArray new:4. |
|
19366 | 3840 |
b signedInt16At:1 put:-1. |
3841 |
b signedInt16At:3 put:-2. |
|
3459 | 3842 |
b inspect |
3843 |
" |
|
3844 |
||
3845 |
"Modified: 1.7.1996 / 21:12:13 / cg" |
|
3323 | 3846 |
! |
3847 |
||
19629 | 3848 |
signedInt16AtLSB:byteIndex |
3849 |
"return the 2-bytes starting at index as a signed Integer. |
|
3850 |
The index is a smalltalk index (i.e. 1-based). |
|
3851 |
The value is retrieved with least significant byte first" |
|
3852 |
||
3853 |
^ self signedInt16At:byteIndex MSB:false |
|
3854 |
||
3855 |
" |
|
3856 |
|b| |
|
3857 |
b := ByteArray new:2. |
|
3858 |
b wordAt:1 put:16rFFFE. |
|
3859 |
b signedInt16AtLSB:1. |
|
3860 |
b signedInt16AtMSB:1. |
|
3861 |
" |
|
3862 |
||
3863 |
"Modified: 1.7.1996 / 21:14:38 / cg" |
|
3864 |
! |
|
3865 |
||
3866 |
signedInt16AtLSB:index put:anInteger |
|
3867 |
"set the 2-bytes starting at index from the signed Integer value. |
|
3868 |
The index is a smalltalk index (i.e. 1-based). |
|
3869 |
The stored value must be in the range 0 .. 16rFFFF. |
|
3870 |
The value is stored with least significant byte first" |
|
3871 |
||
3872 |
^ self signedInt16At:index put:anInteger MSB:false |
|
3873 |
||
3874 |
" |
|
3875 |
|b| |
|
3876 |
b := ByteArray new:4. |
|
3877 |
b signedInt16At:1 put:16r0102. |
|
3878 |
b signedInt16At:3 put:16r0304. |
|
3879 |
b inspect |
|
3880 |
" |
|
3881 |
||
3882 |
"Created: / 5.3.1998 / 11:54:52 / stefan" |
|
3883 |
"Modified: / 5.3.1998 / 14:59:38 / stefan" |
|
3884 |
! |
|
3885 |
||
3886 |
signedInt16AtMSB:byteIndex |
|
3887 |
"return the 2-bytes starting at index as a signed Integer. |
|
3888 |
The index is a smalltalk index (i.e. 1-based). |
|
3889 |
The value is retrieved with most significant byte first" |
|
3890 |
||
3891 |
^ self signedInt16At:byteIndex MSB:true |
|
3892 |
||
3893 |
" |
|
3894 |
|b| |
|
3895 |
b := ByteArray new:2. |
|
3896 |
b wordAt:1 put:16rFFFE. |
|
3897 |
b signedInt16AtLSB:1. |
|
3898 |
b signedInt16AtMSB:1. |
|
3899 |
" |
|
3900 |
||
3901 |
"Modified: 1.7.1996 / 21:14:38 / cg" |
|
3902 |
! |
|
3903 |
||
3904 |
signedInt16AtMSB:index put:anInteger |
|
3905 |
"set the 2-bytes starting at index from the signed Integer value. |
|
3906 |
The index is a smalltalk index (i.e. 1-based). |
|
3907 |
The stored value must be in the range 0 .. 16rFFFF. |
|
3908 |
The value is stored with most significant byte first" |
|
3909 |
||
3910 |
^ self signedInt16At:index put:anInteger MSB:true |
|
3911 |
||
3912 |
" |
|
3913 |
|b| |
|
3914 |
b := ByteArray new:4. |
|
3915 |
b signedInt16At:1 put:16r0102. |
|
3916 |
b signedInt16At:3 put:16r0304. |
|
3917 |
b inspect |
|
3918 |
" |
|
3919 |
||
3920 |
"Created: / 5.3.1998 / 11:54:52 / stefan" |
|
3921 |
"Modified: / 5.3.1998 / 14:59:38 / stefan" |
|
3922 |
! |
|
3923 |
||
19349 | 3924 |
unsignedInt16At:index |
3323 | 3925 |
"return the 2-bytes starting at index as an (unsigned) Integer. |
3433 | 3926 |
The index is a smalltalk index (i.e. 1-based). |
19415 | 3927 |
The value is retrieved in the machine's natural byte order" |
19349 | 3928 |
|
3929 |
^ self unsignedInt16At:index MSB:IsBigEndian |
|
3323 | 3930 |
! |
3931 |
||
19366 | 3932 |
unsignedInt16At:byteIndex MSB:msb |
3323 | 3933 |
"return the 2-bytes starting at index as an (unsigned) Integer. |
3433 | 3934 |
The index is a smalltalk index (i.e. 1-based). |
19349 | 3935 |
The value is retrieved MSB (high 8 bits at lower index) if msb is true; |
19366 | 3936 |
LSB-first (i.e. low 8-bits at lower byte index) if it's false. |
22261 | 3937 |
Notice: |
3938 |
the index is a byte index; thus, this allows for unaligned access to |
|
3939 |
words on any boundary." |
|
3323 | 3940 |
|
13575 | 3941 |
|b1 "{ Class: SmallInteger }" |
3323 | 3942 |
b2 "{ Class: SmallInteger }"| |
3943 |
||
19366 | 3944 |
%{ |
3945 |
/* |
|
3946 |
* handle the most common cases fast ... |
|
3947 |
*/ |
|
3948 |
if (__isSmallInteger(byteIndex)) { |
|
22261 | 3949 |
unsigned char *cp; |
3950 |
INT sz; |
|
3951 |
||
3952 |
__fetchBytePointerAndSize__(self, &cp, &sz); |
|
3953 |
if (cp) { |
|
3954 |
INT idx = __intVal(byteIndex) - 1; |
|
3955 |
||
3956 |
if ((idx >= 0) && ((idx+(2-1)) < sz)) { |
|
3957 |
int iVal; |
|
3958 |
||
3959 |
cp += idx; |
|
3960 |
if (msb == false) { |
|
19366 | 3961 |
#if defined(__i386__) || (defined(__LSBFIRST__) && defined(UNALIGNED_FETCH_OK)) |
22261 | 3962 |
/* |
3963 |
* aligned or not, we don't care (i386 can do both) |
|
3964 |
*/ |
|
3965 |
iVal = ((unsigned short *)cp)[0]; |
|
19366 | 3966 |
#else |
22261 | 3967 |
iVal = (cp[0] & 0xFF) | ((cp[1] & 0xFF) << 8); |
19366 | 3968 |
#endif |
22261 | 3969 |
} else { |
3970 |
iVal = ((cp[0] & 0xFF) << 8) | (cp[1] & 0xFF); |
|
3971 |
} |
|
3972 |
RETURN (__mkSmallInteger(iVal)); |
|
3973 |
} |
|
3974 |
} |
|
19366 | 3975 |
} |
3976 |
%}. |
|
3977 |
||
3978 |
b1 := self byteAt:byteIndex. |
|
3979 |
b2 := self byteAt:(byteIndex + 1). |
|
3323 | 3980 |
msb ifTrue:[ |
22261 | 3981 |
^ (b1 bitShift:8) + b2 |
3323 | 3982 |
]. |
3983 |
^ (b2 bitShift:8) + b1 |
|
19415 | 3984 |
|
3985 |
" |
|
3986 |
#[ 16rFF 16r00 ] unsignedInt16At:1 MSB:true |
|
3987 |
#[ 16rFF 16r00 ] unsignedInt16At:1 MSB:false |
|
3988 |
||
3989 |
#[ 16rFF 16r00 ] unsignedInt16At:2 MSB:true |
|
3990 |
#[ 16rFF 16r00 ] unsignedInt16At:2 MSB:false |
|
3991 |
" |
|
3323 | 3992 |
! |
3993 |
||
19349 | 3994 |
unsignedInt16At:index put:anInteger |
3323 | 3995 |
"set the 2-bytes starting at index from the (unsigned) Integer value. |
3433 | 3996 |
The index is a smalltalk index (i.e. 1-based). |
13575 | 3997 |
The stored value must be in the range 0 .. 16rFFFF. |
19415 | 3998 |
The value is stored in the machine's natural byteorder." |
3323 | 3999 |
|
19349 | 4000 |
^ self unsignedInt16At:index put:anInteger MSB:IsBigEndian |
3323 | 4001 |
|
4002 |
" |
|
4003 |
|b| |
|
4004 |
b := ByteArray new:4. |
|
19349 | 4005 |
b unsignedInt16At:1 put:16r0102. |
4006 |
b unsignedInt16At:3 put:16r0304. |
|
13575 | 4007 |
b inspect |
3323 | 4008 |
" |
4009 |
||
4010 |
"Created: / 5.3.1998 / 11:54:52 / stefan" |
|
4011 |
"Modified: / 5.3.1998 / 14:59:38 / stefan" |
|
4012 |
! |
|
4013 |
||
19366 | 4014 |
unsignedInt16At:byteIndex put:anInteger MSB:msb |
3323 | 4015 |
"set the 2-bytes starting at index from the (unsigned) Integer value. |
3433 | 4016 |
The index is a smalltalk index (i.e. 1-based). |
13575 | 4017 |
The stored value must be in the range 0 .. 16rFFFF. |
3323 | 4018 |
The value is stored LSB-first (i.e. the low 8bits are stored at the |
4019 |
lower index) if msb is false, MSB-first otherwise" |
|
4020 |
||
4021 |
|b1 b2 |
|
4022 |
iVal "{ Class: SmallInteger }"| |
|
4023 |
||
19366 | 4024 |
%{ /* NOCONTEXT */ |
4025 |
/* |
|
4026 |
* handle the most common case fast ... |
|
4027 |
*/ |
|
4028 |
if (__isSmallInteger(byteIndex)) { |
|
22261 | 4029 |
unsigned char *cp; |
4030 |
INT sz; |
|
4031 |
||
4032 |
__fetchBytePointerAndSize__(self, &cp, &sz); |
|
4033 |
// printf("cp=%"_lx_"\n", (INT)cp); |
|
4034 |
if (cp) { |
|
4035 |
INT idx = __intVal(byteIndex) - 1; |
|
4036 |
||
4037 |
if ((idx >= 0) && ((idx+1) < sz)) { |
|
4038 |
cp += idx; |
|
4039 |
||
4040 |
if (__isSmallInteger(anInteger)) { |
|
4041 |
INT __v = __intVal(anInteger); |
|
4042 |
||
4043 |
if (((unsigned INT)__v) > 0xFFFF) { |
|
4044 |
goto badArg; |
|
4045 |
} |
|
4046 |
if (msb == false) { |
|
19366 | 4047 |
#if defined(__i386__) || (defined(__LSBFIRST__) && defined(UNALIGNED_FETCH_OK)) |
22261 | 4048 |
((unsigned short *)cp)[0] = (unsigned short)__v; |
19366 | 4049 |
#else |
22261 | 4050 |
cp[0] = __v & 0xFF; |
4051 |
cp[1] = (__v >> 8) & 0xFF; |
|
19366 | 4052 |
#endif |
22261 | 4053 |
} else { |
4054 |
cp[0] = (__v >> 8) & 0xFF; |
|
4055 |
cp[1] = __v & 0xFF; |
|
4056 |
} |
|
4057 |
RETURN (anInteger); |
|
4058 |
} |
|
4059 |
} |
|
4060 |
} |
|
19366 | 4061 |
} |
4062 |
badArg: ; |
|
4063 |
%}. |
|
19349 | 4064 |
iVal := anInteger. |
3323 | 4065 |
((iVal < 0) or:[iVal > 16rFFFF]) ifTrue:[ |
22261 | 4066 |
^ self elementBoundsError:iVal |
3323 | 4067 |
]. |
4068 |
msb ifTrue:[ |
|
22261 | 4069 |
b1 := ((iVal bitShift:-8) bitAnd:16rFF). |
4070 |
b2 := (iVal bitAnd:16rFF). |
|
3323 | 4071 |
] ifFalse:[ |
22261 | 4072 |
b1 := (iVal bitAnd:16rFF). |
4073 |
b2 := ((iVal bitShift:-8) bitAnd:16rFF). |
|
3323 | 4074 |
]. |
19366 | 4075 |
self byteAt:byteIndex put:b1. |
4076 |
self byteAt:byteIndex+1 put:b2. |
|
19349 | 4077 |
^ anInteger |
3323 | 4078 |
|
4079 |
" |
|
4080 |
|b| |
|
4081 |
b := ByteArray new:8. |
|
19366 | 4082 |
b unsignedInt16At:1 put:16r0102 MSB:false. |
4083 |
b unsignedInt16At:3 put:16r0304 MSB:false. |
|
4084 |
b unsignedInt16At:5 put:16r0102 MSB:true. |
|
4085 |
b unsignedInt16At:7 put:16r0304 MSB:true. |
|
13575 | 4086 |
b inspect |
3323 | 4087 |
" |
4088 |
||
4089 |
"Modified: / 21.1.1998 / 17:48:15 / cg" |
|
4090 |
"Modified: / 5.3.1998 / 11:52:28 / stefan" |
|
19629 | 4091 |
! |
4092 |
||
4093 |
unsignedInt16AtLSB:byteIndex |
|
4094 |
"return the 2-bytes starting at index as an unsigned Integer. |
|
4095 |
The index is a smalltalk index (i.e. 1-based). |
|
4096 |
The value is retrieved with least significant byte first" |
|
4097 |
||
4098 |
^ self unsignedInt16At:byteIndex MSB:false |
|
4099 |
||
4100 |
" |
|
4101 |
|b| |
|
4102 |
b := ByteArray new:2. |
|
4103 |
b wordAt:1 put:16rFFFE. |
|
4104 |
b unsignedInt16AtLSB:1. |
|
4105 |
b unsignedInt16AtMSB:1. |
|
4106 |
" |
|
4107 |
||
4108 |
"Modified: 1.7.1996 / 21:14:38 / cg" |
|
4109 |
! |
|
4110 |
||
4111 |
unsignedInt16AtLSB:index put:anInteger |
|
4112 |
"set the 2-bytes starting at index from the (unsigned) Integer value. |
|
4113 |
The index is a smalltalk index (i.e. 1-based). |
|
4114 |
The stored value must be in the range 0 .. 16rFFFF. |
|
4115 |
The value is stored with least significant byte first" |
|
4116 |
||
4117 |
^ self unsignedInt16At:index put:anInteger MSB:false |
|
4118 |
||
4119 |
" |
|
4120 |
|b| |
|
4121 |
b := ByteArray new:4. |
|
4122 |
b unsignedInt16At:1 put:16r0102. |
|
4123 |
b unsignedInt16At:3 put:16r0304. |
|
4124 |
b inspect |
|
4125 |
" |
|
4126 |
||
4127 |
"Created: / 5.3.1998 / 11:54:52 / stefan" |
|
4128 |
"Modified: / 5.3.1998 / 14:59:38 / stefan" |
|
4129 |
! |
|
4130 |
||
4131 |
unsignedInt16AtMSB:byteIndex |
|
4132 |
"return the 2-bytes starting at index as an unsigned Integer. |
|
4133 |
The index is a smalltalk index (i.e. 1-based). |
|
4134 |
The value is retrieved with most significant byte first" |
|
4135 |
||
4136 |
^ self unsignedInt16At:byteIndex MSB:true |
|
4137 |
||
4138 |
" |
|
4139 |
|b| |
|
4140 |
b := ByteArray new:2. |
|
4141 |
b wordAt:1 put:16rFFFF. |
|
4142 |
b signedWordAt:1 |
|
4143 |
" |
|
4144 |
||
4145 |
"Modified: 1.7.1996 / 21:14:38 / cg" |
|
4146 |
! |
|
4147 |
||
4148 |
unsignedInt16AtMSB:index put:anInteger |
|
4149 |
"set the 2-bytes starting at index from the (unsigned) Integer value. |
|
4150 |
The index is a smalltalk index (i.e. 1-based). |
|
4151 |
The stored value must be in the range 0 .. 16rFFFF. |
|
4152 |
The value is stored with most significant byte first" |
|
4153 |
||
4154 |
^ self unsignedInt16At:index put:anInteger MSB:true |
|
4155 |
||
4156 |
" |
|
4157 |
|b| |
|
4158 |
b := ByteArray new:4. |
|
4159 |
b unsignedInt16At:1 put:16r0102. |
|
4160 |
b unsignedInt16At:3 put:16r0304. |
|
4161 |
b inspect |
|
4162 |
" |
|
4163 |
||
4164 |
"Created: / 5.3.1998 / 11:54:52 / stefan" |
|
4165 |
"Modified: / 5.3.1998 / 14:59:38 / stefan" |
|
3459 | 4166 |
! ! |
4167 |
||
4168 |
!UninterpretedBytes methodsFor:'accessing-strings'! |
|
4169 |
||
4170 |
stringAt:index |
|
4171 |
"return a string starting at index up to the 0-byte. |
|
4172 |
The index is a smalltalk index (i.e. 1-based)." |
|
4173 |
||
4174 |
|stream i "{ Class: SmallInteger }" c| |
|
4175 |
||
10676
252a445bf47d
#stringAt:* now works on Strings, too
Stefan Vogel <sv@exept.de>
parents:
10675
diff
changeset
|
4176 |
stream := WriteStream on:(String new:40). |
3459 | 4177 |
i := index. |
10676
252a445bf47d
#stringAt:* now works on Strings, too
Stefan Vogel <sv@exept.de>
parents:
10675
diff
changeset
|
4178 |
[(c := self byteAt:i) ~~ 0] whileTrue:[ |
22261 | 4179 |
stream nextPut:(Character value:c). |
4180 |
i := i + 1. |
|
3459 | 4181 |
]. |
4182 |
^ stream contents |
|
4183 |
||
10676
252a445bf47d
#stringAt:* now works on Strings, too
Stefan Vogel <sv@exept.de>
parents:
10675
diff
changeset
|
4184 |
" |
13575 | 4185 |
#[71 72 73 74 75 76 77 0] stringAt:1 |
4186 |
#[71 72 73 74 75 76 77 0] stringAt:2 |
|
10676
252a445bf47d
#stringAt:* now works on Strings, too
Stefan Vogel <sv@exept.de>
parents:
10675
diff
changeset
|
4187 |
'1234567890' stringAt:2 |
252a445bf47d
#stringAt:* now works on Strings, too
Stefan Vogel <sv@exept.de>
parents:
10675
diff
changeset
|
4188 |
" |
3459 | 4189 |
! |
4190 |
||
4191 |
stringAt:index put:aString |
|
19196 | 4192 |
"copy aString to the receiver, starting at index up to |
7815 | 4193 |
(and including) the 0-byte (which is always written). |
3459 | 4194 |
The index is a smalltalk index (i.e. 1-based)." |
4195 |
||
4196 |
|i "{ Class: SmallInteger }"| |
|
4197 |
||
4198 |
i := index. |
|
4199 |
aString do:[:aChar | |
|
22261 | 4200 |
self byteAt:i put:aChar codePoint. |
4201 |
i := i + 1. |
|
3459 | 4202 |
]. |
10676
252a445bf47d
#stringAt:* now works on Strings, too
Stefan Vogel <sv@exept.de>
parents:
10675
diff
changeset
|
4203 |
self byteAt:i put:0. |
3459 | 4204 |
^ aString |
4205 |
||
4206 |
" |
|
4207 |
|bytes| |
|
4208 |
||
4209 |
bytes := ExternalBytes new:10. |
|
4210 |
bytes stringAt:1 put:'hello'. |
|
4211 |
1 to:bytes size do:[:i | |
|
22261 | 4212 |
Transcript showCR:(bytes at:i) |
10676
252a445bf47d
#stringAt:* now works on Strings, too
Stefan Vogel <sv@exept.de>
parents:
10675
diff
changeset
|
4213 |
]. |
252a445bf47d
#stringAt:* now works on Strings, too
Stefan Vogel <sv@exept.de>
parents:
10675
diff
changeset
|
4214 |
" |
252a445bf47d
#stringAt:* now works on Strings, too
Stefan Vogel <sv@exept.de>
parents:
10675
diff
changeset
|
4215 |
|
252a445bf47d
#stringAt:* now works on Strings, too
Stefan Vogel <sv@exept.de>
parents:
10675
diff
changeset
|
4216 |
" |
252a445bf47d
#stringAt:* now works on Strings, too
Stefan Vogel <sv@exept.de>
parents:
10675
diff
changeset
|
4217 |
(String new:20) stringAt:1 put:'hello'; stringAt:6 put:' world'; yourself |
3459 | 4218 |
" |
4219 |
||
4220 |
"Created: / 21.1.1998 / 17:45:02 / cg" |
|
4221 |
! |
|
4222 |
||
7812 | 4223 |
stringAt:index put:aString size:maxSize |
19196 | 4224 |
"copy aString to the receiver, starting at index up to either maxSize characters, |
7812 | 4225 |
or (and including) the 0-byte, whichever is encountered first. |
7815 | 4226 |
The final 0-byte is only written, if the string is shorter than maxSize. |
7812 | 4227 |
The index is a smalltalk index (i.e. 1-based)." |
4228 |
||
4229 |
|remaining "{ Class: SmallInteger }" |
|
4230 |
i "{ Class: SmallInteger }"| |
|
4231 |
||
4232 |
remaining := maxSize. |
|
7816 | 4233 |
remaining <= 0 ifTrue:[^ aString]. |
7812 | 4234 |
|
4235 |
i := index. |
|
4236 |
aString do:[:aChar | |
|
22261 | 4237 |
self byteAt:i put:aChar codePoint. |
4238 |
i := i + 1. |
|
4239 |
remaining := remaining - 1. |
|
4240 |
remaining <= 0 ifTrue:[^ aString]. |
|
7812 | 4241 |
]. |
10676
252a445bf47d
#stringAt:* now works on Strings, too
Stefan Vogel <sv@exept.de>
parents:
10675
diff
changeset
|
4242 |
self byteAt:i put:0. |
7812 | 4243 |
^ aString |
4244 |
||
4245 |
" |
|
4246 |
|bytes| |
|
4247 |
||
4248 |
bytes := ExternalBytes new:10. |
|
4249 |
bytes stringAt:1 put:'hello' size:3. |
|
4250 |
1 to:bytes size do:[:i | |
|
22261 | 4251 |
Transcript showCR:(bytes at:i) |
7812 | 4252 |
] |
4253 |
" |
|
4254 |
" |
|
4255 |
|bytes| |
|
4256 |
||
4257 |
bytes := ByteArray new:10 withAll:16rFF. |
|
4258 |
bytes stringAt:1 put:'he' size:3. |
|
4259 |
1 to:bytes size do:[:i | |
|
22261 | 4260 |
Transcript showCR:(bytes at:i) |
7812 | 4261 |
] |
4262 |
" |
|
4263 |
||
10676
252a445bf47d
#stringAt:* now works on Strings, too
Stefan Vogel <sv@exept.de>
parents:
10675
diff
changeset
|
4264 |
" |
252a445bf47d
#stringAt:* now works on Strings, too
Stefan Vogel <sv@exept.de>
parents:
10675
diff
changeset
|
4265 |
(String new:20) stringAt:1 put:'hello' size:3 ; stringAt:4 put:' world' size:4; yourself |
252a445bf47d
#stringAt:* now works on Strings, too
Stefan Vogel <sv@exept.de>
parents:
10675
diff
changeset
|
4266 |
" |
252a445bf47d
#stringAt:* now works on Strings, too
Stefan Vogel <sv@exept.de>
parents:
10675
diff
changeset
|
4267 |
|
252a445bf47d
#stringAt:* now works on Strings, too
Stefan Vogel <sv@exept.de>
parents:
10675
diff
changeset
|
4268 |
|
7812 | 4269 |
"Created: / 21.1.1998 / 17:45:02 / cg" |
4270 |
! |
|
4271 |
||
3459 | 4272 |
stringAt:index size:maxSize |
4273 |
"return a string starting at index up to maxSize, or a 0-byte. |
|
4274 |
The index is a smalltalk index (i.e. 1-based)." |
|
4275 |
||
13575 | 4276 |
|stream c |
10676
252a445bf47d
#stringAt:* now works on Strings, too
Stefan Vogel <sv@exept.de>
parents:
10675
diff
changeset
|
4277 |
max "{ Class: SmallInteger }" |
252a445bf47d
#stringAt:* now works on Strings, too
Stefan Vogel <sv@exept.de>
parents:
10675
diff
changeset
|
4278 |
start "{ Class: SmallInteger }"| |
3459 | 4279 |
|
4280 |
stream := WriteStream on:(String new:maxSize). |
|
10676
252a445bf47d
#stringAt:* now works on Strings, too
Stefan Vogel <sv@exept.de>
parents:
10675
diff
changeset
|
4281 |
start := index. |
252a445bf47d
#stringAt:* now works on Strings, too
Stefan Vogel <sv@exept.de>
parents:
10675
diff
changeset
|
4282 |
max := start + maxSize - 1. |
252a445bf47d
#stringAt:* now works on Strings, too
Stefan Vogel <sv@exept.de>
parents:
10675
diff
changeset
|
4283 |
|
252a445bf47d
#stringAt:* now works on Strings, too
Stefan Vogel <sv@exept.de>
parents:
10675
diff
changeset
|
4284 |
start to:max do:[:eachIndex| |
22261 | 4285 |
c := self byteAt:eachIndex. |
4286 |
c == 0 ifTrue:[ |
|
4287 |
^ stream contents |
|
4288 |
]. |
|
4289 |
stream nextPut:(Character value:c). |
|
3459 | 4290 |
]. |
4291 |
^ stream contents |
|
4292 |
||
10676
252a445bf47d
#stringAt:* now works on Strings, too
Stefan Vogel <sv@exept.de>
parents:
10675
diff
changeset
|
4293 |
" |
252a445bf47d
#stringAt:* now works on Strings, too
Stefan Vogel <sv@exept.de>
parents:
10675
diff
changeset
|
4294 |
#[71 72 73 74 75 76 77] stringAt:1 size:7 |
252a445bf47d
#stringAt:* now works on Strings, too
Stefan Vogel <sv@exept.de>
parents:
10675
diff
changeset
|
4295 |
#[71 72 73 74 75 76 77] stringAt:2 size:6 |
252a445bf47d
#stringAt:* now works on Strings, too
Stefan Vogel <sv@exept.de>
parents:
10675
diff
changeset
|
4296 |
'1234567890' stringAt:2 size:6 |
252a445bf47d
#stringAt:* now works on Strings, too
Stefan Vogel <sv@exept.de>
parents:
10675
diff
changeset
|
4297 |
" |
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
4298 |
! |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
4299 |
|
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
4300 |
zeroByteStringAt:index maximumSize:count |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
4301 |
"extract a zeroByte-delimited string, given initial index and |
3433 | 4302 |
maximum number of characters (bytes). |
4303 |
The index is a smalltalk index (i.e. 1-based)." |
|
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
4304 |
|
19415 | 4305 |
|bytes endIndex idx| |
4306 |
||
4307 |
endIndex := self indexOf:0 startingAt:index. |
|
4308 |
endIndex == 0 ifTrue:[ |
|
22261 | 4309 |
endIndex := self size + 1 |
19415 | 4310 |
]. |
4311 |
endIndex := (endIndex min: (index + count)) - 1. |
|
4312 |
bytes := self copyFrom:index to:endIndex. |
|
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
4313 |
^ bytes asString |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
4314 |
|
19415 | 4315 |
" |
4316 |
#[ 1 2 3 4 5 6 7 8 ] zeroByteStringAt:2 maximumSize:10 |
|
4317 |
#[ 1 2 3 4 5 0 6 7 8 ] zeroByteStringAt:2 maximumSize:10 |
|
4318 |
#[ 1 2 3 4 5 0 6 7 8 ] zeroByteStringAt:2 maximumSize:3 |
|
4319 |
#[ 1 2 3 4 5 0 6 7 8 ] zeroByteStringAt:2 maximumSize:4 |
|
4320 |
" |
|
3207
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
4321 |
! ! |
a6e3c98e2a8e
merged common protocol of ByteArray and ExternalBytes into this class.
Claus Gittinger <cg@exept.de>
parents:
1294
diff
changeset
|
4322 |
|
12951 | 4323 |
!UninterpretedBytes methodsFor:'converting'! |
4324 |
||
4325 |
asExternalBytes |
|
22261 | 4326 |
"in earlier times, this use to return protected memory |
22161 | 4327 |
(i.e. it would not be garbage collected, and the user had to free it manually). |
4328 |
This was changed to now return garbage collected memory." |
|
4329 |
||
4330 |
^ self asExternalBytesUnprotected. |
|
4331 |
||
12951 | 4332 |
" |
4333 |
#[1 2 3 4 5 6 7] asExternalBytes |
|
4334 |
'Hello World' asExternalBytes |
|
14142 | 4335 |
'Hello World' asUnicodeString asExternalBytes |
12951 | 4336 |
" |
22161 | 4337 |
|
4338 |
"Modified (comment): / 04-08-2017 / 11:25:00 / cg" |
|
14132 | 4339 |
! |
4340 |
||
14155 | 4341 |
asExternalBytesUnprotected |
22161 | 4342 |
"Like asExternalBytes, but does not protect the bytes from the collector, |
22261 | 4343 |
so the bytes are GARBAGE-COLLECTED |
22161 | 4344 |
(i.e. free is called when the smalltalk object is no longer referenced)." |
14155 | 4345 |
|
4346 |
|bytes sz| |
|
4347 |
||
4348 |
sz := self byteSize. |
|
15836 | 4349 |
bytes := ExternalBytes unprotectedNew:sz. |
14155 | 4350 |
bytes replaceFrom:1 to:sz with:self startingAt:1. |
4351 |
^ bytes |
|
4352 |
||
4353 |
" |
|
4354 |
|x| |
|
4355 |
x := 'fooBar' asExternalBytesUnprotected. |
|
4356 |
ObjectMemory garbageCollect |
|
4357 |
" |
|
4358 |
||
4359 |
"Created: / 05-06-2012 / 14:11:35 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
15836 | 4360 |
"Modified: / 30-11-2013 / 11:42:21 / cg" |
22161 | 4361 |
"Modified (comment): / 04-08-2017 / 11:22:58 / cg" |
14155 | 4362 |
! |
4363 |
||
18600 | 4364 |
asSingleByteString |
4365 |
"return the receiver converted to a 'normal' string. |
|
4366 |
Raises an error if unrepresentable characters are encountered. |
|
21474 | 4367 |
See also: #asSingleByteStringIfPossible and #asSingleByteStringReplaceInvalidWith:" |
18600 | 4368 |
|
4369 |
^ String fromString:self |
|
4370 |
||
4371 |
" |
|
4372 |
#[60 61 62 63] asSingleByteString |
|
18616 | 4373 |
#[60 61 62 63] asExternalBytes asSingleByteString |
18969 | 4374 |
#[67 68 69 70] asIntegerArray asSingleByteString |
21474 | 4375 |
'abc' asText asSingleByteString |
18600 | 4376 |
(Unicode16String with:(Character value:16rFF)) asSingleByteString |
4377 |
(Unicode16String with:(Character value:16rFFFF)) asSingleByteString |
|
4378 |
" |
|
21474 | 4379 |
|
4380 |
"Modified (comment): / 16-02-2017 / 20:25:14 / stefan" |
|
18600 | 4381 |
! |
4382 |
||
18969 | 4383 |
asSingleByteStringIfPossible |
4384 |
"if possible, return the receiver converted to a 'normal' string. |
|
4385 |
It is only possible, if there are no characters with codePoints above 255 in the receiver. |
|
4386 |
If not possible, the (wideString) receiver is returned." |
|
4387 |
||
4388 |
self containsNon8BitElements ifTrue:[^ self asString]. |
|
4389 |
^ self asSingleByteString. |
|
4390 |
||
4391 |
" |
|
4392 |
#[67 68 69 70] asSingleByteStringIfPossible |
|
4393 |
#[67 68 69 70] asIntegerArray asSingleByteStringIfPossible |
|
4394 |
'hello' asUnicodeString asSingleByteStringIfPossible |
|
4395 |
" |
|
4396 |
! |
|
4397 |
||
14132 | 4398 |
asUUID |
4399 |
^ UUID fromBytes:self |
|
12951 | 4400 |
! ! |
4401 |
||
18616 | 4402 |
!UninterpretedBytes methodsFor:'encoding & decoding'! |
4403 |
||
4404 |
utf8Decoded |
|
4405 |
"Interpreting myself as an UTF-8 representation, decode and return the decoded string." |
|
4406 |
||
4407 |
^ CharacterArray decodeFromUTF8:self. |
|
4408 |
||
4409 |
" |
|
4410 |
#[16rC8 16rA0] utf8Decoded |
|
4411 |
#[16rC8 16rA0] asString utf8Decoded |
|
4412 |
#[16rC8 16rA0] asExternalBytes utf8Decoded |
|
4413 |
(Character value:16r220) utf8Encoded utf8Decoded |
|
4414 |
||
4415 |
(Character value:16r800) utf8Encoded |
|
4416 |
(Character value:16r220) utf8Encoded utf8Decoded |
|
4417 |
" |
|
4418 |
||
4419 |
"test: |
|
4420 |
||
21377 | 4421 |
1 to:16r10FFFF do:[:codepoint | |
22261 | 4422 |
|utf8Encoding original readBack| |
4423 |
||
4424 |
original := (Character value:codepoint) asString. |
|
4425 |
utf8Encoding := original utf8Encoded. |
|
4426 |
readBack := utf8Encoding utf8Decoded. |
|
4427 |
readBack ~= original ifTrue:[ |
|
4428 |
self halt |
|
4429 |
] |
|
18616 | 4430 |
] |
4431 |
" |
|
21377 | 4432 |
|
4433 |
"Modified (comment): / 07-02-2017 / 17:36:08 / stefan" |
|
18616 | 4434 |
! |
4435 |
||
4436 |
utf8DecodedWithTwoByteCharactersReplacedBy:replacementCharacter |
|
4437 |
"Interpreting myself as an UTF-8 representation, decode and return |
|
4438 |
the decoded string. Suppress all 2-byte (above 16rFF) characters, |
|
4439 |
and replace them with replacementCharacter" |
|
4440 |
||
22405 | 4441 |
|in out| |
18616 | 4442 |
|
4443 |
self containsNon7BitAscii ifFalse:[ |
|
22405 | 4444 |
^ self asSingleByteString "plain ASCII" |
18616 | 4445 |
]. |
4446 |
||
22405 | 4447 |
out := WriteStream on:(String uninitializedNew:self utf8DecodedSize). |
18616 | 4448 |
in := self readStream. |
4449 |
[in atEnd] whileFalse:[ |
|
22405 | 4450 |
|c| |
4451 |
||
4452 |
c := Character utf8DecodeFrom:in. |
|
4453 |
c codePoint > 16rFF ifTrue:[ |
|
4454 |
c := replacementCharacter |
|
4455 |
]. |
|
4456 |
out nextPut:c. |
|
18616 | 4457 |
]. |
4458 |
^ out contents |
|
4459 |
||
4460 |
" |
|
4461 |
(Character value:16r220) utf8Encoded |
|
22405 | 4462 |
utf8DecodedWithTwoByteCharactersReplacedBy:(Character space) |
18616 | 4463 |
|
4464 |
(Character value:16r220) utf8Encoded asExternalBytes copyButLast |
|
22405 | 4465 |
utf8DecodedWithTwoByteCharactersReplacedBy:(Character space) |
4466 |
" |
|
4467 |
||
4468 |
"Modified (comment): / 02-01-2018 / 18:54:18 / stefan" |
|
18616 | 4469 |
! ! |
4470 |
||
4782 | 4471 |
!UninterpretedBytes methodsFor:'filling & replacing'! |
4472 |
||
12769
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4473 |
replaceBytesFrom:start to:stop with:aCollection startingAt:repStart |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4474 |
"replace elements from another collection, which must be a ByteArray- |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4475 |
like collection. |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4476 |
|
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4477 |
Notice: This operation modifies the receiver, NOT a copy; |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4478 |
therefore the change may affect all others referencing the receiver." |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4479 |
|
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4480 |
%{ /* NOCONTEXT */ |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4481 |
|
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4482 |
int nIndex, repNIndex; |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4483 |
int startIndex, stopIndex; |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4484 |
REGISTER unsigned char *src; |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4485 |
REGISTER int repStartIndex; |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4486 |
int repStopIndex, count; |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4487 |
REGISTER unsigned char *dst; |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4488 |
OBJ cls; |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4489 |
|
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4490 |
#ifndef NO_PRIM_BYTEARR |
14082 | 4491 |
if ((__isBytes(aCollection) || __isExternalBytesLike(aCollection)) |
14142 | 4492 |
&& (__isBytes(self) || __isWords(self)) |
12769
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4493 |
&& __bothSmallInteger(start, stop) |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4494 |
&& __isSmallInteger(repStart)) { |
22261 | 4495 |
startIndex = __intVal(start) - 1; |
4496 |
if (startIndex >= 0) { |
|
4497 |
dst = (__ByteArrayInstPtr(self)->ba_element) + startIndex; |
|
4498 |
nIndex = __byteArraySize(self); |
|
4499 |
||
4500 |
if ((cls = __qClass(self)) != @global(ByteArray)) { |
|
4501 |
int nInst; |
|
4502 |
||
4503 |
nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
4504 |
dst += nInst; |
|
4505 |
nIndex -= nInst; |
|
4506 |
} |
|
4507 |
||
4508 |
stopIndex = __intVal(stop) - 1; |
|
4509 |
count = stopIndex - startIndex + 1; |
|
4510 |
if (count == 0) { |
|
4511 |
RETURN ( self ); |
|
4512 |
} |
|
4513 |
||
4514 |
if ((count > 0) && (stopIndex < nIndex)) { |
|
4515 |
repStartIndex = __intVal(repStart) - 1; |
|
4516 |
if (repStartIndex >= 0) { |
|
4517 |
if (__isExternalBytesLike(aCollection)) { |
|
4518 |
OBJ sz; |
|
4519 |
||
4520 |
src = __externalAddressVal(aCollection); |
|
4521 |
if (src == 0) goto fallBack; |
|
4522 |
||
4523 |
sz = __externalBytesSize(aCollection); |
|
4524 |
if (__isSmallInteger(sz)) { |
|
4525 |
repNIndex = __smallIntegerVal(sz); |
|
4526 |
} else { |
|
4527 |
repNIndex = repStopIndex+1; /* always enough */ |
|
4528 |
} |
|
4529 |
src = src + repStartIndex; |
|
4530 |
} else { |
|
4531 |
if (__isStringLike(aCollection)) { |
|
4532 |
repNIndex = __stringSize(aCollection); |
|
4533 |
} else { |
|
4534 |
repNIndex = __qSize(aCollection) - OHDR_SIZE; |
|
4535 |
} |
|
4536 |
src = (__ByteArrayInstPtr(aCollection)->ba_element) + repStartIndex; |
|
4537 |
if ((cls = __qClass(aCollection)) != @global(ByteArray)) { |
|
4538 |
int nInst; |
|
4539 |
||
4540 |
nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
4541 |
src += nInst; |
|
4542 |
repNIndex -= nInst; |
|
4543 |
} |
|
4544 |
} |
|
4545 |
repStopIndex = repStartIndex + (stopIndex - startIndex); |
|
4546 |
if (repStopIndex < repNIndex) { |
|
4547 |
if (aCollection == self) { |
|
4548 |
/* take care of overlapping copy */ |
|
4549 |
if (src < dst) { |
|
4550 |
/* must do a reverse copy */ |
|
4551 |
src += count; |
|
4552 |
dst += count; |
|
4553 |
while (count-- > 0) { |
|
4554 |
*--dst = *--src; |
|
4555 |
} |
|
4556 |
RETURN ( self ); |
|
4557 |
} |
|
4558 |
} |
|
12769
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4559 |
|
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4560 |
# ifdef bcopy4 |
22261 | 4561 |
if (((unsigned INT)src & 3) == ((unsigned INT)dst & 3)) { |
4562 |
int nW; |
|
4563 |
||
4564 |
/* copy unaligned part */ |
|
4565 |
while (count && ((unsigned INT)src & 3)) { |
|
4566 |
*dst++ = *src++; |
|
4567 |
count--; |
|
4568 |
} |
|
4569 |
||
4570 |
if (count > 0) { |
|
4571 |
/* copy aligned part */ |
|
4572 |
nW = count >> 2; |
|
4573 |
bcopy4(src, dst, nW); |
|
4574 |
if ((count = count & 3) != 0) { |
|
4575 |
/* copy any remaining part */ |
|
4576 |
src += (nW<<2); |
|
4577 |
dst += (nW<<2); |
|
4578 |
while (count--) { |
|
4579 |
*dst++ = *src++; |
|
4580 |
} |
|
4581 |
} |
|
4582 |
} |
|
4583 |
RETURN ( self ); |
|
4584 |
} |
|
12769
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4585 |
# else |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4586 |
# if __POINTER_SIZE__ == 8 |
22261 | 4587 |
if (((unsigned INT)src & 7) == ((unsigned INT)dst & 7)) { |
4588 |
/* copy unaligned part */ |
|
4589 |
while (count && ((unsigned INT)src & 7)) { |
|
4590 |
*dst++ = *src++; |
|
4591 |
count--; |
|
4592 |
} |
|
4593 |
||
4594 |
/* copy aligned part */ |
|
4595 |
while (count >= 8) { |
|
4596 |
((unsigned INT *)dst)[0] = ((unsigned INT *)src)[0]; |
|
4597 |
dst += 8; |
|
4598 |
src += 8; |
|
4599 |
count -= 8; |
|
4600 |
} |
|
4601 |
while (count--) { |
|
4602 |
*dst++ = *src++; |
|
4603 |
} |
|
4604 |
RETURN ( self ); |
|
4605 |
} |
|
12769
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4606 |
# endif /* 64bit */ |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4607 |
# endif /* bcopy4 */ |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4608 |
|
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4609 |
# ifdef FAST_MEMCPY |
22261 | 4610 |
bcopy(src, dst, count); |
12769
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4611 |
# else |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4612 |
# ifdef __UNROLL_LOOPS__ |
22261 | 4613 |
while (count >= 8) { |
4614 |
dst[0] = src[0]; dst[1] = src[1]; |
|
4615 |
dst[2] = src[2]; dst[3] = src[3]; |
|
4616 |
dst[4] = src[4]; dst[5] = src[5]; |
|
4617 |
dst[6] = src[6]; dst[7] = src[7]; |
|
4618 |
dst += 8; src += 8; |
|
4619 |
count -= 8; |
|
4620 |
} |
|
12769
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4621 |
# endif /* __UNROLL_LOOPS__ */ |
22261 | 4622 |
while (count-- > 0) { |
4623 |
*dst++ = *src++; |
|
4624 |
} |
|
12769
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4625 |
# endif |
22261 | 4626 |
RETURN ( self ); |
4627 |
} |
|
4628 |
} |
|
4629 |
} |
|
4630 |
} |
|
12769
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4631 |
} |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4632 |
fallBack: ; |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4633 |
#endif |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4634 |
%}. |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4635 |
" |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4636 |
fall back in case of non-ByteArray argument, |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4637 |
or for the error report if any index is invalid |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4638 |
" |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4639 |
self slowReplaceBytesFrom:start to:stop with:aCollection startingAt:repStart |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4640 |
|
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4641 |
" |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4642 |
#[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16] |
22261 | 4643 |
copy |
4644 |
replaceFrom:1 to:8 |
|
4645 |
with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160] |
|
4646 |
startingAt:1 |
|
12769
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4647 |
|
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4648 |
#[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16] |
22261 | 4649 |
copy |
4650 |
replaceFrom:3 to:10 |
|
4651 |
with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160] |
|
4652 |
startingAt:1 |
|
12769
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4653 |
|
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4654 |
#[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16] |
22261 | 4655 |
copy |
4656 |
replaceFrom:3 to:4 |
|
4657 |
with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160] |
|
4658 |
startingAt:1 |
|
12769
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4659 |
|
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4660 |
#[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16] |
22261 | 4661 |
copy |
4662 |
replaceFrom:0 to:9 |
|
4663 |
with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160] |
|
4664 |
startingAt:1 |
|
12769
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4665 |
|
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4666 |
#[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16] |
22261 | 4667 |
copy |
4668 |
replaceFrom:1 to:10 |
|
4669 |
with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160] |
|
4670 |
startingAt:0 |
|
12769
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4671 |
" |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4672 |
! |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4673 |
|
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4674 |
replaceBytesFrom:startIndex with:replacementCollection startingAt:repStartIndex |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4675 |
"replace elements from another collection, which must be |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4676 |
byte-array-like. |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4677 |
|
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4678 |
Notice: This operation modifies the receiver, NOT a copy; |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4679 |
therefore the change may affect all others referencing the receiver." |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4680 |
|
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4681 |
^ self |
22261 | 4682 |
replaceBytesFrom:startIndex |
4683 |
to:(startIndex + replacementCollection size - repStartIndex) |
|
4684 |
with:replacementCollection |
|
4685 |
startingAt:repStartIndex |
|
12769
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4686 |
|
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4687 |
" |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4688 |
args: startIndex : <integer> |
22261 | 4689 |
replacementCollection : <collection of <bytes> > |
4690 |
repStartIndex : <integer> |
|
12769
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4691 |
|
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4692 |
returns: self |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4693 |
" |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4694 |
|
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4695 |
"Created: / 27.7.1998 / 16:56:46 / cg" |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4696 |
"Modified: / 27.7.1998 / 16:58:38 / cg" |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4697 |
! |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4698 |
|
13902 | 4699 |
replaceBytesWith:replacementCollection |
4700 |
"replace elements from another collection, which must be byte-array-like. |
|
4701 |
Replace stops at whichever collection is smaller. |
|
4702 |
||
4703 |
Notice: This operation modifies the receiver, NOT a copy; |
|
4704 |
therefore the change may affect all others referencing the receiver." |
|
4705 |
||
4706 |
^ self |
|
22261 | 4707 |
replaceBytesFrom:1 |
4708 |
to:(replacementCollection sizeInBytes min:self sizeInBytes) |
|
4709 |
with:replacementCollection |
|
4710 |
startingAt:1 |
|
13902 | 4711 |
|
4712 |
" |
|
15004 | 4713 |
(ByteArray new:10) replaceBytesWith:'hello' |
4714 |
(ByteArray new:10) replaceBytesWith:'hello world bla bla bla' |
|
13902 | 4715 |
" |
4716 |
||
4717 |
"Created: / 09-01-2012 / 16:18:10 / cg" |
|
22215 | 4718 |
"Modified: / 28-08-2017 / 02:17:23 / cg" |
13902 | 4719 |
! |
4720 |
||
12769
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4721 |
replaceFrom:startIndex to:stopIndex with:aCollection startingAt:repStartIndex |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4722 |
"replace elements in the receiver between index start and stop, |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4723 |
with elements taken from replacementCollection starting at repStart. |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4724 |
Return the receiver. |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4725 |
|
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4726 |
Notice: This operation modifies the receiver, NOT a copy; |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4727 |
therefore the change may affect all others referencing the receiver." |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4728 |
|
22261 | 4729 |
(self class isBytes |
22229 | 4730 |
and:[aCollection class == self class or:[aCollection isSingleByteCollection]] |
4731 |
) ifTrue:[ |
|
22261 | 4732 |
"can do it fast: just copy the plain bytes" |
4733 |
^ self replaceBytesFrom:startIndex to:stopIndex with:aCollection startingAt:repStartIndex |
|
7252
82cd08881a2a
abstract replaceBytes fallback is required (lead to recursion)
Claus Gittinger <cg@exept.de>
parents:
7218
diff
changeset
|
4734 |
]. |
12769
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4735 |
^ super replaceFrom:startIndex to:stopIndex with:aCollection startingAt:repStartIndex |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4736 |
|
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4737 |
" |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4738 |
args: startIndex : <integer> |
22261 | 4739 |
stopIndex : <integer> |
4740 |
replacementCollection : <collection of <bytes> > |
|
4741 |
repStartIndex : <integer> |
|
12769
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4742 |
|
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4743 |
returns: self |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4744 |
" |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4745 |
|
14131
3b0cb7751a71
changed: #replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14082
diff
changeset
|
4746 |
"Modified: / 08-05-2012 / 13:23:27 / cg" |
4782 | 4747 |
! ! |
4748 |
||
12757 | 4749 |
!UninterpretedBytes methodsFor:'hashing'! |
4750 |
||
4751 |
computeXorHashFrom:startIndex to:endIndex |
|
20372 | 4752 |
"compute and answer the 32bit SmallInteger-Hash of the bytes |
12757 | 4753 |
from startIndex to endIndex. |
4754 |
If endindex = 0 or endIndex > size, hash up the size. |
|
4755 |
||
4756 |
NOTE: startIndex and endIndex are only hints about what should be hashed. |
|
22261 | 4757 |
In fact, more bytes could be involved in hashing. |
4758 |
SO ARRAYS MUST BE EQUAL TO HASH TO THE SAME VALUE. |
|
20372 | 4759 |
|
4760 |
Also NOTE: |
|
22261 | 4761 |
used to return a 32bit hash on 32bit machines and a 64bit integer on 64bit cpus. |
4762 |
changed to return the same for all (in case hash values are used for other purposes)." |
|
12757 | 4763 |
|
4764 |
|w| |
|
4765 |
||
4766 |
%{ |
|
4767 |
if (__bothSmallInteger(startIndex, endIndex)) { |
|
22261 | 4768 |
unsigned char *cp; |
4769 |
INT sz; |
|
4770 |
||
4771 |
__fetchBytePointerAndSize__(self, &cp, &sz); |
|
4772 |
if (cp) { |
|
4773 |
INT sidx = ((unsigned INT)__smallIntegerVal(startIndex)) - 1; |
|
4774 |
INT eidx = ((unsigned INT)__smallIntegerVal(endIndex)) - 1; |
|
4775 |
// # define H_INT INT |
|
20372 | 4776 |
// # define _MAX_H_INT _MAX_INT; |
22261 | 4777 |
# define H_INT int |
20372 | 4778 |
# define _MAX_H_INT 0x3FFFFFFF |
4779 |
||
22261 | 4780 |
unsigned char *ep; |
4781 |
unsigned H_INT hash = 0, hash2 = 0, carry; |
|
4782 |
int i; |
|
4783 |
||
4784 |
if (eidx < 0 || eidx >= sz) eidx = sz - 1; |
|
4785 |
if (sidx > eidx) sidx = eidx; |
|
4786 |
if (sidx < 0) { |
|
4787 |
RETURN(__mkSmallInteger(0)); |
|
4788 |
} |
|
4789 |
||
4790 |
ep = cp + eidx; |
|
4791 |
cp += sidx; |
|
12757 | 4792 |
|
4793 |
#if 0 |
|
22261 | 4794 |
/* |
4795 |
* On LSB-First (little endian) cpus, |
|
4796 |
* this code does not produce the same result |
|
4797 |
* if the same bytes are at different positions |
|
4798 |
*/ |
|
4799 |
||
4800 |
if ((H_INT)cp & (sizeof(H_INT)-1)) { |
|
4801 |
/* not aligned */ |
|
4802 |
||
4803 |
for (i=0; cp <= ep; cp++) { |
|
4804 |
hash2 = (hash2 << 8) | *cp; |
|
4805 |
if (++i == sizeof(H_INT)) { |
|
4806 |
hash ^= hash2; |
|
4807 |
i = hash2 = 0; |
|
4808 |
} |
|
4809 |
} |
|
4810 |
} else { |
|
4811 |
/* aligned */ |
|
4812 |
for (; cp+sizeof(H_INT) <= ep; cp += sizeof(H_INT)) { |
|
4813 |
hash ^= *(unsigned H_INT *)cp; |
|
4814 |
} |
|
4815 |
for (; cp <= ep; cp++) { |
|
4816 |
hash2 = (hash2 << 8) | *cp; |
|
4817 |
} |
|
4818 |
} |
|
12757 | 4819 |
#else |
22261 | 4820 |
for (i=0; cp <= ep-sizeof(H_INT); cp += sizeof(H_INT)) { |
4821 |
hash2 = cp[0]; |
|
4822 |
hash2 = (hash2 << 8) | cp[1]; |
|
4823 |
hash2 = (hash2 << 8) | cp[2]; |
|
4824 |
hash2 = (hash2 << 8) | cp[3]; |
|
20373 | 4825 |
# if 0 |
22261 | 4826 |
if (sizeof(H_INT) == 8) { |
4827 |
hash2 = (hash2 << 8) | cp[4]; |
|
4828 |
hash2 = (hash2 << 8) | cp[5]; |
|
4829 |
hash2 = (hash2 << 8) | cp[6]; |
|
4830 |
hash2 = (hash2 << 8) | cp[7]; |
|
4831 |
} |
|
20373 | 4832 |
# endif |
22261 | 4833 |
/* |
4834 |
* multiply by large prime to scramble bits and |
|
4835 |
* to avoid a 0 result from |
|
4836 |
* #[1 2 3 4 1 2 3 4] computeXorHashFrom:1 to:8. |
|
4837 |
*/ |
|
4838 |
hash ^= (hash * 31415821) ^ hash2; |
|
4839 |
} |
|
4840 |
for (hash2 = 0; cp <= ep; cp++) { |
|
4841 |
hash2 = (hash2 << 8) | *cp; |
|
4842 |
} |
|
12757 | 4843 |
#endif |
22261 | 4844 |
hash ^= (hash * 31415821) ^ hash2; |
4845 |
||
4846 |
/* |
|
4847 |
* fold the high bits not fitting into a H_INT |
|
4848 |
*/ |
|
4849 |
carry = hash & ~_MAX_H_INT; |
|
4850 |
if (carry) { |
|
4851 |
hash = (hash & _MAX_H_INT) ^ (carry >> 8); |
|
4852 |
} |
|
4853 |
||
4854 |
RETURN(__mkSmallInteger(hash)); |
|
4855 |
} |
|
12757 | 4856 |
} |
4857 |
%}. |
|
4858 |
||
4859 |
^ self primitiveFailed |
|
4860 |
||
4861 |
" |
|
4862 |
#[1 2 3 4] computeXorHashFrom:1 to:4. |
|
4863 |
#[1 2 3 4] computeXorHashFrom:1 to:32. |
|
4864 |
#[1 2 3 4] computeXorHashFrom:1 to:0. |
|
4865 |
#[1 2 3 4 5] computeXorHashFrom:1 to:4. |
|
4866 |
#[1 2 3 4 1 2 3 4] computeXorHashFrom:1 to:8. |
|
22261 | 4867 |
#[1 2 3 4 5 6 7 8] computeXorHashFrom:2 to:8. |
12757 | 4868 |
#[2 3 4 5 6 7 8] computeXorHashFrom:1 to:7. |
19554 | 4869 |
#[2 3 4 5 6 7 8] computeXorHashFrom:1 to:8. |
4870 |
" |
|
4871 |
! |
|
4872 |
||
4873 |
hash |
|
4874 |
|sz| |
|
4875 |
||
4876 |
sz := self size. |
|
4877 |
sz <= 32 ifTrue:[ |
|
22381 | 4878 |
^ self computeXorHashFrom:1 to:sz. |
19554 | 4879 |
]. |
22381 | 4880 |
|
4881 |
"/ the code below is actually not doing what was intended (to take the hashes of the first 16 |
|
4882 |
"/ and the last 16 bytes. |
|
4883 |
"/ It does actually take the last 17 bytes (due to a mistake of the original programmer...) |
|
4884 |
"/ However, we will not change it, but keep it that way, in case the hashvalue already found |
|
4885 |
"/ its way into some dictionary (as key-index). |
|
4886 |
"/ It does not really hurt anyway, so there is no need to change it. |
|
22261 | 4887 |
^ (sz bitXor:(self computeXorHashFrom:1 to:16)) bitXor:(self computeXorHashFrom:sz-16 to:sz) |
4888 |
||
4889 |
" |
|
22381 | 4890 |
#[1 2 3 4] hash |
4891 |
#[1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 ] hash |
|
4892 |
||
4893 |
#[1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 |
|
4894 |
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 ] hash |
|
4895 |
||
4896 |
#[1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 |
|
4897 |
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1] hash |
|
4898 |
" |
|
4899 |
||
4900 |
"Modified (format): / 28-11-2017 / 15:03:36 / cg" |
|
12757 | 4901 |
! ! |
4902 |
||
12769
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4903 |
!UninterpretedBytes methodsFor:'image manipulation support'! |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4904 |
|
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4905 |
copyReverse |
22102 | 4906 |
<resource: #obsolete> |
12769
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4907 |
"create a copy of myself with elements reversed in order" |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4908 |
|
22102 | 4909 |
self obsoleteMethodWarning:'use #reversed'. |
4910 |
||
4911 |
^ self reversed |
|
12769
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4912 |
|
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4913 |
" |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4914 |
#[1 2 3 4 5] copyReverse |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4915 |
#[1 2 3 4] copyReverse |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4916 |
" |
22102 | 4917 |
|
4918 |
"Modified: / 25-07-2017 / 17:10:02 / stefan" |
|
19415 | 4919 |
! |
4920 |
||
4921 |
swapBytes |
|
4922 |
"swap bytes (of int16s) inplace - |
|
4923 |
Expects that the receiver has an even number of bytes; |
|
4924 |
if not, only the pairs excluding the last byte are swapped" |
|
4925 |
||
4926 |
|b1 lastIndex "{ Class: SmallInteger }"| |
|
4927 |
||
4928 |
lastIndex := self size-1. |
|
4929 |
1 to:lastIndex by:2 do:[:idx | |
|
22261 | 4930 |
b1 := self byteAt:idx. |
4931 |
self byteAt:idx put:(self byteAt:idx+1). |
|
4932 |
self byteAt:idx+1 put:b1. |
|
19415 | 4933 |
]. |
4934 |
||
4935 |
" |
|
4936 |
#[1 2 3 4 5] swapBytes |
|
4937 |
#[1 2 3 4] swapBytes |
|
4938 |
" |
|
12769
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4939 |
! ! |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
4940 |
|
14131
3b0cb7751a71
changed: #replaceFrom:to:with:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14082
diff
changeset
|
4941 |
|
3363
3bb61f364fe3
added #from: and #swapLongAt: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
3323
diff
changeset
|
4942 |
!UninterpretedBytes methodsFor:'misc'! |
3bb61f364fe3
added #from: and #swapLongAt: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
3323
diff
changeset
|
4943 |
|
22504 | 4944 |
copyToEndInto:aStream |
4945 |
"copy all of myself into aStream. Compatibility with Stream" |
|
4946 |
||
4947 |
aStream nextPutAll:self. |
|
4948 |
||
4949 |
"Created: / 23-01-2018 / 18:43:40 / stefan" |
|
4950 |
! |
|
4951 |
||
13575 | 4952 |
swapLongAt:byteIndex |
3433 | 4953 |
"swap the byteOrder of a long. |
4954 |
The index is a smalltalk index (i.e. 1-based)." |
|
3363
3bb61f364fe3
added #from: and #swapLongAt: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
3323
diff
changeset
|
4955 |
|
3bb61f364fe3
added #from: and #swapLongAt: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
3323
diff
changeset
|
4956 |
|t| |
3bb61f364fe3
added #from: and #swapLongAt: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
3323
diff
changeset
|
4957 |
|
3bb61f364fe3
added #from: and #swapLongAt: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
3323
diff
changeset
|
4958 |
t := self byteAt:byteIndex. |
3bb61f364fe3
added #from: and #swapLongAt: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
3323
diff
changeset
|
4959 |
self byteAt:byteIndex put:(self byteAt:(byteIndex + 3)). |
3bb61f364fe3
added #from: and #swapLongAt: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
3323
diff
changeset
|
4960 |
self byteAt:(byteIndex + 3) put:t. |
3bb61f364fe3
added #from: and #swapLongAt: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
3323
diff
changeset
|
4961 |
t := self byteAt:(byteIndex + 1). |
3bb61f364fe3
added #from: and #swapLongAt: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
3323
diff
changeset
|
4962 |
self byteAt:(byteIndex + 1) put:(self byteAt:(byteIndex + 2)). |
3bb61f364fe3
added #from: and #swapLongAt: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
3323
diff
changeset
|
4963 |
self byteAt:(byteIndex + 2) put:t |
3bb61f364fe3
added #from: and #swapLongAt: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
3323
diff
changeset
|
4964 |
|
3bb61f364fe3
added #from: and #swapLongAt: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
3323
diff
changeset
|
4965 |
"Created: / 3.4.1998 / 13:37:01 / cg" |
3bb61f364fe3
added #from: and #swapLongAt: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
3323
diff
changeset
|
4966 |
! ! |
3bb61f364fe3
added #from: and #swapLongAt: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
3323
diff
changeset
|
4967 |
|
18664 | 4968 |
!UninterpretedBytes methodsFor:'printing & storing'! |
4969 |
||
4970 |
hexPrintOn:aStream |
|
4971 |
"print as hex string, eg: 'FF0243'. |
|
4972 |
This string can be used in #fromHexString: to recreate the byteArray" |
|
4973 |
||
4974 |
self hexPrintOn:aStream withSeparator:nil |
|
4975 |
||
4976 |
" |
|
4977 |
#[1 2 3 4 10 17] hexPrintOn:Transcript |
|
4978 |
" |
|
4979 |
||
4980 |
" |
|
4981 |
|s| |
|
4982 |
s := String streamContents:[:s | #[1 2 3 4 10 17] hexPrintOn:s]. |
|
4983 |
ByteArray fromHexString:s |
|
4984 |
" |
|
4985 |
! |
|
4986 |
||
4987 |
hexPrintOn:aStream withSeparator:aSeparatorStringOrCharacterOrNil |
|
4988 |
"print as hex string with separators, eg: 'FF:02:43'" |
|
4989 |
||
4990 |
|first| |
|
4991 |
||
4992 |
first := true. |
|
4993 |
1 to:self size do:[:idx | |
|
22261 | 4994 |
aSeparatorStringOrCharacterOrNil notNil ifTrue:[ |
4995 |
first ifFalse:[ |
|
4996 |
aSeparatorStringOrCharacterOrNil printOn:aStream |
|
4997 |
] ifTrue:[ |
|
4998 |
first := false. |
|
4999 |
]. |
|
5000 |
]. |
|
5001 |
(self byteAt:idx) printOn:aStream base:16 size:2 fill:$0. |
|
18664 | 5002 |
]. |
5003 |
||
5004 |
" |
|
5005 |
#[1 2 3 4 10 17] hexPrintOn:Transcript withSeparator:$: |
|
5006 |
#[1 2 3 4 10 17] hexPrintOn:Transcript withSeparator:(Character space) |
|
5007 |
#[1 2 3 4 10 17] hexPrintOn:Transcript withSeparator:'-' |
|
5008 |
#[1 2 3 4 10 17] hexPrintOn:Transcript withSeparator:nil |
|
20177 | 5009 |
'hello' hexPrintOn:Transcript withSeparator:'.' |
18664 | 5010 |
" |
5011 |
! |
|
5012 |
||
5013 |
hexPrintString |
|
5014 |
"print as hex string, eg: 'FF0243'. |
|
5015 |
This string can be used in #fromHexString: to recreate the byteArray" |
|
5016 |
||
5017 |
^ self hexPrintStringWithSeparator:nil |
|
5018 |
||
5019 |
" |
|
5020 |
#[1 2 3 4 10 17] hexPrintString |
|
5021 |
ByteArray fromHexString:#[1 2 3 4 10 17] hexPrintString |
|
20176 | 5022 |
'hello' hexPrintString |
18664 | 5023 |
" |
5024 |
||
5025 |
"Modified: / 03-07-2010 / 01:59:19 / cg" |
|
5026 |
! |
|
5027 |
||
5028 |
hexPrintStringWithSeparator:aSeparatorStringOrCharacterOrNil |
|
5029 |
"print as hex string, eg: 'FF:02:43'." |
|
5030 |
||
5031 |
^ String |
|
22261 | 5032 |
streamContents:[:s | |
5033 |
self hexPrintOn:s withSeparator:aSeparatorStringOrCharacterOrNil. |
|
5034 |
] |
|
18664 | 5035 |
|
5036 |
" |
|
5037 |
#[1 2 3 4 10 17] hexPrintStringWithSeparator:$: |
|
5038 |
#[1 2 3 4 10 17] hexPrintStringWithSeparator:Character space |
|
5039 |
#[1 2 3 4 10 17] hexPrintStringWithSeparator:' - ' |
|
5040 |
#[1 2 3 4 10 17] hexPrintStringWithSeparator:nil |
|
20176 | 5041 |
'hello' hexPrintStringWithSeparator:'.' |
18664 | 5042 |
" |
5043 |
! ! |
|
5044 |
||
12769
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
5045 |
!UninterpretedBytes methodsFor:'private'! |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
5046 |
|
22883 | 5047 |
reportError:failReason with:parameter |
5048 |
"common helper" |
|
5049 |
||
5050 |
(failReason == #invalidIndex) ifTrue:[ |
|
5051 |
^ self indexNotIntegerOrOutOfBounds:parameter |
|
5052 |
]. |
|
5053 |
failReason == #nullPointer ifTrue:[ |
|
5054 |
^ self error:'free or unallocated object referenced' |
|
5055 |
]. |
|
5056 |
failReason == #unaligned ifTrue:[ |
|
5057 |
^ self error:'unaligned index'. |
|
5058 |
]. |
|
5059 |
self primitiveFailed:failReason. |
|
5060 |
! |
|
5061 |
||
18969 | 5062 |
slowReplaceBytesFrom:startArg to:stopArg with:sourceBytes startingAt:sourceIndex |
12769
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
5063 |
"fallback if primitive code fails" |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
5064 |
|
18969 | 5065 |
|srcIdx "{ Class:SmallInteger }" |
5066 |
start "{ Class:SmallInteger }" |
|
5067 |
stop "{ Class:SmallInteger }"| |
|
5068 |
||
5069 |
start := startArg. |
|
5070 |
stop := stopArg. |
|
12769
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
5071 |
srcIdx := sourceIndex. |
18969 | 5072 |
|
12769
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
5073 |
start to:stop do:[:dstIdx | |
22261 | 5074 |
self byteAt:dstIdx put:(sourceBytes byteAt:srcIdx). |
5075 |
srcIdx := srcIdx + 1 |
|
12769
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
5076 |
]. |
22216 | 5077 |
|
5078 |
"Modified: / 28-08-2017 / 02:25:53 / cg" |
|
12769
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
5079 |
! ! |
435668a20ddd
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12757
diff
changeset
|
5080 |
|
3323 | 5081 |
!UninterpretedBytes methodsFor:'queries'! |
5082 |
||
18616 | 5083 |
containsNon7BitAscii |
18969 | 5084 |
"return true, if the underlying collection contains elements longer than 7 bits |
18616 | 5085 |
(i.e. if it is non-ascii)" |
5086 |
||
5087 |
|sz "{ Class:SmallInteger }"| |
|
5088 |
||
5089 |
sz := self size. |
|
5090 |
1 to:sz do:[:idx| |
|
22261 | 5091 |
(self at:idx) > 16r7F ifTrue:[ |
5092 |
^ true. |
|
5093 |
]. |
|
18969 | 5094 |
]. |
5095 |
^ false. |
|
5096 |
! |
|
5097 |
||
5098 |
containsNon8BitElements |
|
5099 |
"return true, if the underlying structure contains elements larger than a single byte" |
|
5100 |
||
5101 |
|sz "{ Class:SmallInteger }"| |
|
5102 |
||
5103 |
sz := self size. |
|
5104 |
1 to:sz do:[:idx| |
|
22261 | 5105 |
(self at:idx) > 16rFF ifTrue:[ |
5106 |
^ true. |
|
5107 |
]. |
|
18616 | 5108 |
]. |
5109 |
^ false. |
|
5110 |
! |
|
5111 |
||
7218
b798be6d632f
elementBoundsError -> elementBoundsError:
Claus Gittinger <cg@exept.de>
parents:
7197
diff
changeset
|
5112 |
defaultElement |
b798be6d632f
elementBoundsError -> elementBoundsError:
Claus Gittinger <cg@exept.de>
parents:
7197
diff
changeset
|
5113 |
^ 0 |
b798be6d632f
elementBoundsError -> elementBoundsError:
Claus Gittinger <cg@exept.de>
parents:
7197
diff
changeset
|
5114 |
! |
b798be6d632f
elementBoundsError -> elementBoundsError:
Claus Gittinger <cg@exept.de>
parents:
7197
diff
changeset
|
5115 |
|
21320 | 5116 |
isValidUTF8 |
21323 | 5117 |
"returns true, if the receiver contains a valid UTF8 encoded string" |
22261 | 5118 |
|
21323 | 5119 |
|trailing "{ Class: SmallInteger }"| |
5120 |
||
21320 | 5121 |
trailing := 0. |
5122 |
||
5123 |
1 to:self size do:[:idx | |
|
22261 | 5124 |
|byte "{ Class: SmallInteger }" | |
5125 |
||
5126 |
byte := self byteAt:idx. |
|
5127 |
trailing ~~ 0 ifTrue:[ |
|
5128 |
(byte bitAnd:2r11000000) == 2r10000000 ifFalse:[^ false]. |
|
5129 |
trailing := trailing - 1. |
|
5130 |
] ifFalse:[ |
|
5131 |
(byte bitAnd:16r80) == 0 ifTrue:[ |
|
5132 |
"/ continue |
|
5133 |
] ifFalse:[ |
|
5134 |
(byte bitAnd:2r11100000) == 2r11000000 ifTrue:[ |
|
5135 |
"/ strict: should not be encoded this way (could have used a shorter sequence) |
|
5136 |
(byte bitAnd:2r00011110) == 0 ifTrue:[ |
|
5137 |
^ false |
|
5138 |
]. |
|
5139 |
trailing := 1. |
|
5140 |
] ifFalse:[ |
|
5141 |
(byte bitAnd:2r11110000) == 2r11100000 ifTrue:[ |
|
5142 |
trailing := 2. |
|
5143 |
] ifFalse:[ |
|
5144 |
(byte bitAnd:2r11111000) == 2r11110000 ifTrue:[ |
|
5145 |
trailing := 3. |
|
5146 |
] ifFalse:[ |
|
5147 |
(byte bitAnd:2r11111100) == 2r11111000 ifTrue:[ |
|
5148 |
trailing := 4. |
|
5149 |
] ifFalse:[ |
|
5150 |
(byte bitAnd:2r11111110) == 2r11111100 ifTrue:[ |
|
5151 |
trailing := 5. |
|
5152 |
] ifFalse:[ |
|
5153 |
^ false |
|
5154 |
]. |
|
5155 |
]. |
|
5156 |
]. |
|
5157 |
]. |
|
5158 |
]. |
|
5159 |
]. |
|
5160 |
]. |
|
21320 | 5161 |
]. |
5162 |
^ trailing == 0 |
|
5163 |
||
5164 |
" |
|
5165 |
'abc' isValidUTF8 |
|
21377 | 5166 |
'abcöäü' isValidUTF8 |
5167 |
'abcöäü' utf8Encoded isValidUTF8 |
|
21323 | 5168 |
(Character value:16r800) utf8Encoded isValidUTF8 |
5169 |
(Character value:16r1000) utf8Encoded isValidUTF8 |
|
22261 | 5170 |
|
21320 | 5171 |
1 to:255 do:[:c1 | |
22261 | 5172 |
1 to:255 do:[:c2 | |
5173 |
1 to:255 do:[:c3 | |
|
5174 |
self assert:(c1 asCharacter , c2 asCharacter , c3 asCharacter) utf8Encoded isValidUTF8 |
|
5175 |
] |
|
5176 |
] |
|
21323 | 5177 |
] |
5178 |
||
5179 |
|s| |
|
5180 |
1 to:10000 do:[:c1 | |
|
22261 | 5181 |
1 to:255 do:[:c2 | |
5182 |
s := (c1 asCharacter , c2 asCharacter). |
|
5183 |
self assert:s utf8Encoded isValidUTF8 |
|
5184 |
] |
|
5185 |
] |
|
21320 | 5186 |
" |
5187 |
! |
|
5188 |
||
18616 | 5189 |
referencesAny:aCollection |
5190 |
"redefined to speed up searching when many of my instances are present" |
|
5191 |
||
5192 |
%{ /* NOCONTEXT */ |
|
5193 |
if (__mkSmallInteger(0) == __ClassInstPtr(__qClass(self))->c_ninstvars) { |
|
22261 | 5194 |
/* I am only bytes */ |
5195 |
RETURN(false) |
|
18616 | 5196 |
} |
5197 |
%}. |
|
5198 |
^ super referencesAny:aCollection |
|
5199 |
||
5200 |
" |
|
22261 | 5201 |
'abc' referencesAny:#() |
18616 | 5202 |
" |
5203 |
! |
|
5204 |
||
3323 | 5205 |
sizeInBytes |
5206 |
"return the number of 8-bit bytes in the receiver. |
|
22217 | 5207 |
This is needed since subclasses may redefine #size (TwoByteString)" |
3323 | 5208 |
|
5209 |
^ super size |
|
5210 |
||
5211 |
"Created: / 5.3.1998 / 10:41:13 / stefan" |
|
21377 | 5212 |
! |
5213 |
||
22217 | 5214 |
utf8DecodedSize |
22420 | 5215 |
"return the number of characters needed when this string is |
22405 | 5216 |
decoded from UTF-8" |
22261 | 5217 |
|
22217 | 5218 |
|sz "{ Class:SmallInteger }" |
5219 |
cnt "{ Class:SmallInteger }"| |
|
21377 | 5220 |
|
22261 | 5221 |
sz := self size. |
21377 | 5222 |
cnt := 0. |
5223 |
||
5224 |
1 to:sz do:[:idx| |
|
22405 | 5225 |
"/ count the number of UTF-8 start bytes |
5226 |
((self byteAt:idx) bitAnd:16rC0) ~~ 16r80 ifTrue:[ |
|
5227 |
cnt := cnt+1. |
|
5228 |
]. |
|
21377 | 5229 |
]. |
5230 |
^ cnt. |
|
5231 |
||
5232 |
" |
|
5233 |
'hello world' asByteArray utf8DecodedSize |
|
5234 |
'ä' utf8Encoded asByteArray utf8DecodedSize |
|
5235 |
'äΣΔΨӕἤῴ' utf8Encoded asByteArray utf8DecodedSize |
|
5236 |
" |
|
5237 |
||
5238 |
"Created: / 07-02-2017 / 15:03:07 / stefan" |
|
5239 |
"Modified: / 07-02-2017 / 19:14:06 / stefan" |
|
22405 | 5240 |
"Modified (comment): / 02-01-2018 / 18:30:27 / stefan" |
22420 | 5241 |
"Modified (comment): / 15-01-2018 / 08:29:10 / mawalch" |
3323 | 5242 |
! ! |
5243 |
||
8986 | 5244 |
!UninterpretedBytes methodsFor:'testing'! |
5245 |
||
5246 |
isByteCollection |
|
5247 |
"return true, if the receiver has access methods for bytes; |
|
22229 | 5248 |
This is different from 'self class isBytes', |
8986 | 5249 |
true is returned here - the method is redefined from Object." |
5250 |
||
5251 |
^ true |
|
9005 | 5252 |
! |
5253 |
||
5254 |
isNonByteCollection |
|
5255 |
"return true, if the receiver is some kind of collection, but not a String, ByteArray etc.; |
|
5256 |
false is returned here - the method is redefined from Collection." |
|
5257 |
||
5258 |
^ false |
|
22229 | 5259 |
! |
5260 |
||
5261 |
isSingleByteCollection |
|
5262 |
"return true, if the receiver has access methods for bytes; |
|
5263 |
i.e. #at: and #at:put: accesses a byte and are equivalent to #byteAt: and byteAt:put: |
|
22261 | 5264 |
and #replaceFrom:to: is equivalent to #replaceBytesFrom:to:. |
22229 | 5265 |
This is different from 'self class isBytes', |
5266 |
true is returned here - the method is redefined from Object." |
|
5267 |
||
5268 |
^ true |
|
8986 | 5269 |
! ! |
5270 |
||
11009
fb66915c5bb5
changed #acceptVisitor:with: - moved to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
10676
diff
changeset
|
5271 |
!UninterpretedBytes methodsFor:'visiting'! |
fb66915c5bb5
changed #acceptVisitor:with: - moved to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
10676
diff
changeset
|
5272 |
|
fb66915c5bb5
changed #acceptVisitor:with: - moved to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
10676
diff
changeset
|
5273 |
acceptVisitor:aVisitor with:aParameter |
16719 | 5274 |
"dispatch for visitor pattern; send #visitByteArray:with: to aVisitor." |
11009
fb66915c5bb5
changed #acceptVisitor:with: - moved to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
10676
diff
changeset
|
5275 |
|
fb66915c5bb5
changed #acceptVisitor:with: - moved to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
10676
diff
changeset
|
5276 |
^ aVisitor visitByteArray:self with:aParameter |
fb66915c5bb5
changed #acceptVisitor:with: - moved to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
10676
diff
changeset
|
5277 |
! ! |
fb66915c5bb5
changed #acceptVisitor:with: - moved to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
10676
diff
changeset
|
5278 |
|
695 | 5279 |
!UninterpretedBytes class methodsFor:'documentation'! |
5280 |
||
5281 |
version |
|
18600 | 5282 |
^ '$Header$' |
12253
c933c6fcdeef
changed: #fromHexStringWithSeparators:
Claus Gittinger <cg@exept.de>
parents:
11973
diff
changeset
|
5283 |
! |
c933c6fcdeef
changed: #fromHexStringWithSeparators:
Claus Gittinger <cg@exept.de>
parents:
11973
diff
changeset
|
5284 |
|
c933c6fcdeef
changed: #fromHexStringWithSeparators:
Claus Gittinger <cg@exept.de>
parents:
11973
diff
changeset
|
5285 |
version_CVS |
18600 | 5286 |
^ '$Header$' |
695 | 5287 |
! ! |
15087 | 5288 |
|
18284 | 5289 |
|
5290 |
UninterpretedBytes initialize! |