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