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