--- a/ByteArray.st Sat Feb 11 17:42:24 1995 +0100
+++ b/ByteArray.st Wed Feb 15 11:22:05 1995 +0100
@@ -21,7 +21,7 @@
COPYRIGHT (c) 1989 by Claus Gittinger
All Rights Reserved
-$Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.21 1995-02-05 21:28:17 claus Exp $
+$Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.22 1995-02-15 10:21:21 claus Exp $
'!
!ByteArray class methodsFor:'documentation'!
@@ -42,7 +42,7 @@
version
"
-$Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.21 1995-02-05 21:28:17 claus Exp $
+$Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.22 1995-02-15 10:21:21 claus Exp $
"
!
@@ -125,49 +125,47 @@
REGISTER OBJ *op;
extern OBJ new();
- if (_isSmallInteger(anInteger)) {
+ if (__isSmallInteger(anInteger)) {
nindexedinstvars = _intVal(anInteger);
if (nindexedinstvars >= 0) {
-/*
- * not needed - always true
- * if ((_intVal(_ClassInstPtr(self)->c_flags) & ARRAYMASK) == BYTEARRAY)
- */
- {
- if (self == ByteArray) {
- /*
- * the most common case
- */
- instsize = OHDR_SIZE + nindexedinstvars;
- if (_CanDoQuickNew(instsize)) {
- _qCheckedNew(newobj, instsize);
- _InstPtr(newobj)->o_class = self;
- RETURN (newobj );
- }
+ if (self == ByteArray) {
+ /*
+ * the most common case
+ */
+ instsize = OHDR_SIZE + nindexedinstvars;
+ if (_CanDoQuickNew(instsize)) {
+ _qCheckedNew(newobj, instsize);
+ _InstPtr(newobj)->o_class = self;
+ RETURN (newobj );
}
- nInstVars = _intVal(_ClassInstPtr(self)->c_ninstvars);
- instsize = OHDR_SIZE + __OBJS2BYTES__(nInstVars) + nindexedinstvars;
- PROTECT_CONTEXT
- _qNew(newobj, instsize, SENDER);
- UNPROTECT_CONTEXT
- if (newobj) {
- _InstPtr(newobj)->o_class = self;
- if (nInstVars) {
- /*
- * still have to nil out named instvars ...
- */
-#if defined(FAST_MEMSET) && !defined(NEGATIVE_ADDRESSES)
- /*
- * knowing that nil is 0
- */
- memset(_InstPtr(newobj)->i_instvars, 0, instsize - OHDR_SIZE);
+ }
+ nInstVars = _intVal(_ClassInstPtr(self)->c_ninstvars);
+ instsize = OHDR_SIZE + __OBJS2BYTES__(nInstVars) + nindexedinstvars;
+ PROTECT_CONTEXT
+ _qNew(newobj, instsize, SENDER);
+ UNPROTECT_CONTEXT
+ if (newobj) {
+ _InstPtr(newobj)->o_class = self;
+ if (nInstVars) {
+ /*
+ * still have to nil out named instvars ...
+ */
+#if defined(FAST_OBJECT_MEMSET4)
+ memset4(_InstPtr(newobj)->i_instvars, nil, nInstVars);
#else
- op = _InstPtr(newobj)->i_instvars;
- while (nInstVars--)
- *op++ = nil;
+# if defined(FAST_MEMSET) && !defined(NEGATIVE_ADDRESSES)
+ /*
+ * knowing that nil is 0
+ */
+ memset(_InstPtr(newobj)->i_instvars, 0, instsize - OHDR_SIZE);
+# else
+ op = _InstPtr(newobj)->i_instvars;
+ while (nInstVars--)
+ *op++ = nil;
+# endif
#endif
- }
- RETURN ( newobj );
}
+ RETURN ( newobj );
}
}
}
@@ -187,7 +185,6 @@
^ ObjectMemory allocationFailureSignal raise.
].
^ self basicNew:anInteger
-
! !
!ByteArray methodsFor:'accessing'!
@@ -202,14 +199,15 @@
int nIndex;
OBJ cls;
- if (_isSmallInteger(index)) {
+ if (__isSmallInteger(index)) {
indx = _intVal(index);
-
- if ((cls = _qClass(self)) != ByteArray)
- indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars));
- nIndex = _qSize(self) - OHDR_SIZE;
- if ((indx > 0) && (indx <= nIndex)) {
- RETURN ( _MKSMALLINT(_ByteArrayInstPtr(self)->ba_element[indx - 1]) );
+ if (indx > 0) {
+ if ((cls = _qClass(self)) != ByteArray)
+ indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars));
+ nIndex = __byteArraySize(self);
+ if (indx <= nIndex) {
+ RETURN ( _MKSMALLINT(_ByteArrayInstPtr(self)->ba_element[indx - 1]) );
+ }
}
}
%}
@@ -228,16 +226,18 @@
int val;
OBJ cls;
- if (_isSmallInteger(index) && _isSmallInteger(value)) {
+ if (__bothSmallInteger(index, value)) {
val = _intVal(value);
if ((val & ~0xFF) == 0 /* i.e. (val >= 0) && (val <= 255) */) {
indx = _intVal(index);
- if ((cls = _qClass(self)) != ByteArray)
- indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars));
- nIndex = _qSize(self) - OHDR_SIZE;
- if ((indx > 0) && (indx <= nIndex)) {
- _ByteArrayInstPtr(self)->ba_element[indx - 1] = val;
- RETURN ( value );
+ if (indx > 0) {
+ if ((cls = _qClass(self)) != ByteArray)
+ indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars));
+ nIndex = __byteArraySize(self);
+ if (indx <= nIndex) {
+ _ByteArrayInstPtr(self)->ba_element[indx - 1] = val;
+ RETURN ( value );
+ }
}
}
}
@@ -247,17 +247,62 @@
!
byteAt:index
- "return the byte at index
- - for ST-80 compatibility"
+ "return the byte at index.
+ For ByteArray, this is the same as basicAt:;
+ however, for strings or symbols, this returns a numeric byteValue
+ instead of a character."
+
+%{ /* NOCONTEXT */
+
+ REGISTER int indx;
+ int nIndex;
+ OBJ cls;
- ^ self at:index
+ if (__isSmallInteger(index)) {
+ indx = _intVal(index);
+ if (indx > 0) {
+ if ((cls = _qClass(self)) != ByteArray)
+ indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars));
+ nIndex = __byteArraySize(self);
+ if (indx <= nIndex) {
+ RETURN ( _MKSMALLINT(_ByteArrayInstPtr(self)->ba_element[indx - 1]) );
+ }
+ }
+ }
+%}
+.
+ ^ (super basicAt:index) asInteger
!
-byteAt:index put:aByteValuedInteger
- "return the byte at index
- - for ST-80 compatibility"
+byteAt:index put:value
+ "set the byte at index. For ByteArray, this is the same as basicAt:put:.
+ However, for Strings, this expects a byteValue to be stored."
+
+%{ /* NOCONTEXT */
+
+ REGISTER int indx;
+ int nIndex;
+ int val;
+ OBJ cls;
- ^ self at:index put:aByteValuedInteger
+ if (__bothSmallInteger(index, value)) {
+ val = _intVal(value);
+ if ((val & ~0xFF) == 0 /* i.e. (val >= 0) && (val <= 255) */) {
+ indx = _intVal(index);
+ if (indx > 0) {
+ if ((cls = _qClass(self)) != ByteArray)
+ indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars));
+ nIndex = __byteArraySize(self);
+ if (indx <= nIndex) {
+ _ByteArrayInstPtr(self)->ba_element[indx - 1] = val;
+ RETURN ( value );
+ }
+ }
+ }
+ }
+%}
+.
+ ^ super basicAt:index put:value
!
wordAt:index
@@ -270,16 +315,19 @@
REGISTER int indx;
int nIndex;
int val;
+ OBJ cls;
- if (_isSmallInteger(index)) {
+ if (__isSmallInteger(index)) {
indx = _intVal(index);
- if (_qClass(self) != ByteArray)
- indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(_qClass(self))->c_ninstvars));
- nIndex = _qSize(self) - OHDR_SIZE;
- if ((indx > 0) && ((indx+1) <= nIndex)) {
- val = _ByteArrayInstPtr(self)->ba_element[indx+1-1];
- val = (val << 8) + _ByteArrayInstPtr(self)->ba_element[indx-1];
- RETURN ( _MKSMALLINT(val) );
+ if (indx > 0) {
+ if ((cls = _qClass(self)) != ByteArray)
+ indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars));
+ nIndex = __byteArraySize(self);
+ if ((indx+1) <= nIndex) {
+ val = _ByteArrayInstPtr(self)->ba_element[indx+1-1];
+ val = (val << 8) + _ByteArrayInstPtr(self)->ba_element[indx-1];
+ RETURN ( _MKSMALLINT(val) );
+ }
}
}
%}
@@ -299,18 +347,21 @@
REGISTER int indx;
int nIndex;
int val;
+ OBJ cls;
- if (_isSmallInteger(index) && _isSmallInteger(value)) {
+ if (__bothSmallInteger(index, value)) {
indx = _intVal(index);
- if (_qClass(self) != ByteArray)
- indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(_qClass(self))->c_ninstvars));
- nIndex = _qSize(self) - OHDR_SIZE;
- if ((indx > 0) && ((indx+1) <= nIndex)) {
- val = _intVal(value);
- if ((val >= 0) && (val <= 0xFFFF)) {
- _ByteArrayInstPtr(self)->ba_element[indx-1] = val & 0xFF;
- _ByteArrayInstPtr(self)->ba_element[indx+1-1] = (val>>8) & 0xFF;
- RETURN ( value );
+ if (indx > 0) {
+ if ((cls = _qClass(self)) != ByteArray)
+ indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars));
+ nIndex = __byteArraySize(self);
+ if ((indx+1) <= nIndex) {
+ val = _intVal(value);
+ if ((val & ~0xFFFF) == 0 /* i.e. (val >= 0) && (val <= 0xFFFF) */) {
+ _ByteArrayInstPtr(self)->ba_element[indx-1] = val & 0xFF;
+ _ByteArrayInstPtr(self)->ba_element[indx+1-1] = (val>>8) & 0xFF;
+ RETURN ( value );
+ }
}
}
}
@@ -381,21 +432,24 @@
REGISTER int indx;
int nIndex;
int val;
+ OBJ cls;
- if (_isSmallInteger(index)) {
+ if (__isSmallInteger(index)) {
indx = _intVal(index);
- if (_qClass(self) != ByteArray)
- indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(_qClass(self))->c_ninstvars));
- nIndex = _qSize(self) - OHDR_SIZE;
- if ((indx > 0) && ((indx+3) <= nIndex)) {
- val = _ByteArrayInstPtr(self)->ba_element[indx+3-1];
- val = (val << 8) + _ByteArrayInstPtr(self)->ba_element[indx+2-1];
- val = (val << 8) + _ByteArrayInstPtr(self)->ba_element[indx+1-1];
- val = (val << 8) + _ByteArrayInstPtr(self)->ba_element[indx-1];
- if ((val >= 0) && (val <= _MAX_INT)) {
- RETURN ( _MKSMALLINT(val) );
+ if (indx > 0) {
+ if ((cls = _qClass(self)) != ByteArray)
+ indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars));
+ nIndex = __byteArraySize(self);
+ if ((indx+3) <= nIndex) {
+ val = _ByteArrayInstPtr(self)->ba_element[indx+3-1];
+ val = (val << 8) + _ByteArrayInstPtr(self)->ba_element[indx+2-1];
+ val = (val << 8) + _ByteArrayInstPtr(self)->ba_element[indx+1-1];
+ val = (val << 8) + _ByteArrayInstPtr(self)->ba_element[indx-1];
+ if ((val >= 0) && (val <= _MAX_INT)) {
+ RETURN ( _MKSMALLINT(val) );
+ }
+ RETURN ( _MKULARGEINT(val) );
}
- RETURN ( _MKULARGEINT(val) );
}
}
/*
@@ -427,23 +481,26 @@
REGISTER int indx;
int nIndex;
int val;
+ OBJ cls;
- if (_isSmallInteger(index) && _isSmallInteger(value)) {
+ if (__bothSmallInteger(index, value)) {
val = _intVal(value);
if (val >= 0) {
indx = _intVal(index);
- if (_qClass(self) != ByteArray)
- indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(_qClass(self))->c_ninstvars));
- nIndex = _qSize(self) - OHDR_SIZE;
- if ((indx > 0) && ((indx+3) <= nIndex)) {
- _ByteArrayInstPtr(self)->ba_element[indx-1] = val & 0xFF;
- val >>= 8;
- _ByteArrayInstPtr(self)->ba_element[indx+1-1] = val & 0xFF;
- val >>= 8;
- _ByteArrayInstPtr(self)->ba_element[indx+2-1] = val & 0xFF;
- val >>= 8;
- _ByteArrayInstPtr(self)->ba_element[indx+3-1] = val & 0xFF;
- RETURN ( value );
+ if (indx > 0) {
+ if ((cls = _qClass(self)) != ByteArray)
+ indx += __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars));
+ nIndex = _qSize(self) - OHDR_SIZE;
+ if ((indx+3) <= nIndex) {
+ _ByteArrayInstPtr(self)->ba_element[indx-1] = val & 0xFF;
+ val >>= 8;
+ _ByteArrayInstPtr(self)->ba_element[indx+1-1] = val & 0xFF;
+ val >>= 8;
+ _ByteArrayInstPtr(self)->ba_element[indx+2-1] = val & 0xFF;
+ val >>= 8;
+ _ByteArrayInstPtr(self)->ba_element[indx+3-1] = val & 0xFF;
+ RETURN ( value );
+ }
}
}
}
@@ -761,8 +818,9 @@
REGISTER unsigned char *cp;
REGISTER int index, byteValue;
REGISTER int len;
+ OBJ cls;
- if (! _isSmallInteger(aByte)) {
+ if (! __isSmallInteger(aByte)) {
/*
* searching for something which cannot be found
*/
@@ -778,14 +836,14 @@
RETURN ( _MKSMALLINT(0) );
}
- if (_isSmallInteger(start)) {
+ if (__isSmallInteger(start)) {
index = _intVal(start);
- len = _qSize(self) - OHDR_SIZE;
- cp = &(_ByteArrayInstPtr(self)->ba_element[0]);
- if (_qClass(self) != ByteArray) {
+ len = __byteArraySize(self);
+ cp = _ByteArrayInstPtr(self)->ba_element;
+ if ((cls = _qClass(self)) != ByteArray) {
int nInst;
- nInst = __OBJS2BYTES__(_intVal(_ClassInstPtr(_qClass(self))->c_ninstvars));
+ nInst = __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars));
cp += nInst;
len -= nInst;
}
@@ -961,9 +1019,8 @@
OBJ newByteArray;
if (__isByteArray(self)
- && _isSmallInteger(start)
- && _isSmallInteger(stop)) {
- len = _byteArraySize(self);
+ && __bothSmallInteger(start, stop)) {
+ len = __byteArraySize(self);
index1 = _intVal(start);
index2 = _intVal(stop);
@@ -1006,20 +1063,25 @@
%{ /* NOCONTEXT */
-#ifndef FAST_MEMSET
REGISTER unsigned char *dstp;
-#endif
REGISTER int count, value;
- int len, index1, index2, sz;
+ int len, index1, index2;
+ int nInst;
+ OBJ cls;
- if (__isByteArray(self)
- && _isSmallInteger(aNumber)
- && _isSmallInteger(start)
- && _isSmallInteger(stop)) {
- len = _byteArraySize(self);
+ if (__isSmallInteger(aNumber)
+ && __bothSmallInteger(start, stop)) {
+ len = __byteArraySize(self);
index1 = _intVal(start);
index2 = _intVal(stop);
+ dstp = _ByteArrayInstPtr(self)->ba_element + index1 - 1;
+ if ((cls = _qClass(self)) != ByteArray) {
+ nInst = __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars));
+ dstp += nInst;
+ len -= nInst;
+ }
+
value = _intVal(aNumber);
if ((value >= 0) && (value <= 255)
&& (index1 <= index2)
@@ -1027,9 +1089,8 @@
if (index2 <= len) {
count = index2 - index1 + 1;
#ifdef FAST_MEMSET
- memset(_ByteArrayInstPtr(self)->ba_element + index1 - 1, count, value);
+ memset(dstp, count, value);
#else
- dstp = _ByteArrayInstPtr(self)->ba_element + index1 - 1;
while (count--) {
*dstp++ = value;
}
@@ -1047,7 +1108,17 @@
!
replaceFrom:start to:stop with:aCollection startingAt:repStart
- "reimplemented for speed"
+ "replace elements from another collection"
+
+ (aCollection class == self class) ifTrue:[
+ ^ self replaceBytesFrom:start to:stop with:aCollection startingAt:repStart
+ ].
+ ^ super replaceFrom:start to:stop with:aCollection startingAt:repStart
+!
+
+replaceBytesFrom:start to:stop with:aCollection startingAt:repStart
+ "replace elements from another collection, which must be a ByteArray
+ or String."
%{ /* NOCONTEXT */
@@ -1057,15 +1128,23 @@
REGISTER int repStartIndex;
int repStopIndex, count;
REGISTER unsigned char *dst;
+ OBJ cls;
+ int nInst;
- if ((_qClass(self) == ByteArray)
- && (_Class(aCollection) == ByteArray)
- && _isSmallInteger(start)
- && _isSmallInteger(stop)
- && _isSmallInteger(repStart)) {
+ if (__isBytes(aCollection)
+ && __bothSmallInteger(start, stop)
+ && __isSmallInteger(repStart)) {
startIndex = _intVal(start) - 1;
if (startIndex >= 0) {
- nIndex = _qSize(self) - OHDR_SIZE;
+ dst = (_ByteArrayInstPtr(self)->ba_element) + startIndex;
+ nIndex = __byteArraySize(self);
+
+ if ((cls = _qClass(self)) != ByteArray) {
+ nInst = __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars));
+ dst += nInst;
+ nIndex -= nInst;
+ }
+
stopIndex = _intVal(stop) - 1;
count = stopIndex - startIndex + 1;
if (count == 0) {
@@ -1075,11 +1154,15 @@
repStartIndex = _intVal(repStart) - 1;
if (repStartIndex >= 0) {
repNIndex = _qSize(aCollection) - OHDR_SIZE;
+ src = (_ByteArrayInstPtr(aCollection)->ba_element) + repStartIndex;
+ if ((cls = _qClass(aCollection)) != ByteArray) {
+ nInst = __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars));
+ src += nInst;
+ repNIndex -= nInst;
+ }
+
repStopIndex = repStartIndex + (stopIndex - startIndex);
if (repStopIndex < repNIndex) {
- src = &(_ByteArrayInstPtr(aCollection)->ba_element[repStartIndex]);
- dst = &(_ByteArrayInstPtr(self)->ba_element[startIndex]);
-
if (aCollection == self) {
/* take care of overlapping copy */
if (src < dst) {
@@ -1123,7 +1206,7 @@
}
%}
.
- ^ super replaceFrom:start to:stop with:aCollection startingAt:repStart
+ ^ super replaceBytesFrom:start to:stop with:aCollection startingAt:repStart
! !
!ByteArray methodsFor:'image manipulation support'!
@@ -1140,7 +1223,7 @@
REGISTER int cnt;
if (_qClass(self) == ByteArray) {
- cnt = _qSize(self) - OHDR_SIZE;
+ cnt = __byteArraySize(self);
dst = _ByteArrayInstPtr(self)->ba_element;
if (! ((int)dst & (sizeof(long)-1))) {
ldst = (unsigned long *)dst;
@@ -1171,22 +1254,24 @@
REGISTER unsigned char *p1, *p2;
REGISTER int cnt;
REGISTER unsigned t;
+ OBJ cls;
- if (_qClass(self) == ByteArray) {
- cnt = _qSize(self) - OHDR_SIZE;
- p1 = _ByteArrayInstPtr(self)->ba_element;
- p2 = _ByteArrayInstPtr(self)->ba_element + cnt - 1;
- while (cnt > 0) {
- t = *p1;
- *p1++ = *p2;
- *p2-- = t;
- cnt-=2;
- }
- RETURN ( self );
+ cnt = __byteArraySize(self);
+ p1 = _ByteArrayInstPtr(self)->ba_element;
+ if ((cls = _qClass(self)) != ByteArray) {
+ t = __OBJS2BYTES__(_intVal(_ClassInstPtr(cls)->c_ninstvars));
+ p1 += t;
+ cnt -= t;
}
+ p2 = p1 + cnt - 1;
+ while (cnt > 0) {
+ t = *p1;
+ *p1++ = *p2;
+ *p2-- = t;
+ cnt-=2;
+ }
+ RETURN ( self );
%}
-.
- ^ super reverse
!
expandPixels:nBitsPerPixel width:width height:height into:aByteArray
@@ -1221,9 +1306,8 @@
if ((_qClass(self) == ByteArray)
&& (_qClass(aByteArray) == ByteArray)
- && _isSmallInteger(nBitsPerPixel)
- && _isSmallInteger(height)
- && _isSmallInteger(width)) {
+ && __isSmallInteger(nBitsPerPixel)
+ && __bothSmallInteger(height, width)) {
if ((aMapByteArray != nil)
&& (_Class(aMapByteArray) == ByteArray)) {
map = _ByteArrayInstPtr(aMapByteArray)->ba_element;
@@ -1267,8 +1351,8 @@
srcBytes = bytesPerRow * h;
dstBytes = w * h;
- if (((_qSize(self) - OHDR_SIZE) >= srcBytes)
- && ((_qSize(aByteArray) - OHDR_SIZE) >= dstBytes)) {
+ if ((__byteArraySize(self) >= srcBytes)
+ && (__byteArraySize(aByteArray) >= dstBytes)) {
for (hrun=h; hrun; hrun--) {
srcNext = src + bytesPerRow;
shift = shift0;
@@ -1366,9 +1450,8 @@
if ((_qClass(self) == ByteArray)
&& (_qClass(aByteArray) == ByteArray)
- && _isSmallInteger(nBitsPerPixel)
- && _isSmallInteger(height)
- && _isSmallInteger(width)) {
+ && __isSmallInteger(nBitsPerPixel)
+ && __bothSmallInteger(height, width)) {
if ((aMapByteArray != nil)
&& (_Class(aMapByteArray) == ByteArray)) {
map = _ByteArrayInstPtr(aMapByteArray)->ba_element;
@@ -1410,8 +1493,8 @@
dstBytes = bytesPerRow * h;
srcBytes = w * h;
- if (((_qSize(self) - OHDR_SIZE) >= srcBytes)
- && ((_qSize(aByteArray) - OHDR_SIZE) >= dstBytes)) {
+ if ((__byteArraySize(self) >= srcBytes)
+ && (__byteArraySize(aByteArray) >= dstBytes)) {
for (hrun=h; hrun; hrun--) {
dstNext = dst + bytesPerRow;
bits = 0; shift = 8;