ByteArray.st
branchjv
changeset 19431 3e697e4bcbf5
parent 19412 1e842c25e51e
parent 19414 4cf7fc881b10
child 19496 7613c0fb5f3c
--- a/ByteArray.st	Wed Mar 23 07:50:28 2016 +0000
+++ b/ByteArray.st	Thu Mar 24 07:05:30 2016 +0100
@@ -1,3 +1,5 @@
+"{ Encoding: utf8 }"
+
 "
  COPYRIGHT (c) 1989 by Claus Gittinger
 	      All Rights Reserved
@@ -489,20 +491,21 @@
     REGISTER OBJ cls;
 
     if (__bothSmallInteger(index, value)) {
-	val = __intVal(value);
-	if ((unsigned)(val) <= 0xFF /* i.e. (val >= 0) && (val <= 255) */) {
-	    indx = __intVal(index) - 1;
-	    slf = self;
-	    if ((cls = __qClass(slf)) != ByteArray) {
-		if (indx < 0) goto badIndex;
-		indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
-	    }
-	    nIndex = __byteArraySize(slf);
-	    if ((unsigned)indx < (unsigned)nIndex) {
-		__ByteArrayInstPtr(slf)->ba_element[indx] = val;
-		RETURN ( value );
-	    }
-	}
+        val = __intVal(value);
+        if ((unsigned)(val) <= 0xFF /* i.e. (val >= 0) && (val <= 255) */) {
+            indx = __intVal(index) - 1;
+            slf = self;
+            if ((cls = __qClass(slf)) != ByteArray) {
+                if (indx < 0) goto badIndex;
+                if (cls == ImmutableByteArray) goto badIndex; 
+                indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
+            }
+            nIndex = __byteArraySize(slf);
+            if ((unsigned)indx < (unsigned)nIndex) {
+                __ByteArrayInstPtr(slf)->ba_element[indx] = val;
+                RETURN ( value );
+            }
+        }
     }
 badIndex: ;
 %}.
@@ -522,7 +525,7 @@
 
 %{  /* NOCONTEXT */
 
-    REGISTER int indx;
+    REGISTER INT indx;
     int nIndex;
     union {
         unsigned char u_char[4];
@@ -541,8 +544,10 @@
 
         indx = __intVal(index);
         if (indx > 0) {
-            if ((cls = __qClass(self)) != @global(ByteArray))
+            if ((cls = __qClass(self)) != @global(ByteArray)) {
+                if (cls == ImmutableByteArray) goto error;
                 indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
+            }
             nIndex = __qSize(self) - OHDR_SIZE;
             if ((indx+3) <= nIndex) {
                 byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1;
@@ -584,7 +589,7 @@
 
 %{  /* NOCONTEXT */
 
-    REGISTER int indx;
+    REGISTER INT indx;
     int nIndex;
     int val;
     OBJ cls;
@@ -599,8 +604,10 @@
         }
         indx = __intVal(index);
         if (indx > 0) {
-            if ((cls = __qClass(self)) != @global(ByteArray))
+            if ((cls = __qClass(self)) != @global(ByteArray)) {
+                if (cls == ImmutableByteArray) goto error;
                 indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
+            }        
             nIndex = __qSize(self) - OHDR_SIZE;
             if ((indx+3) <= nIndex) {
                 byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1;
@@ -674,7 +681,7 @@
 
 %{  /* NOCONTEXT */
 
-    REGISTER int indx;
+    REGISTER INT indx;
     int nIndex;
     union {
         unsigned char u_char[2];
@@ -714,8 +721,7 @@
      Question: should it be retrieve signed values ? (see ByteArray>>signedWordAt:)"
 
 %{  /* NOCONTEXT */
-
-    REGISTER int indx;
+    REGISTER INT indx;
     int nIndex;
     int val;
     unsigned char *byteP;
@@ -792,7 +798,7 @@
 
 %{  /* NOCONTEXT */
 
-    REGISTER int indx;
+    REGISTER INT indx;
     int nIndex;
     int v;
     union {
@@ -804,8 +810,11 @@
     if (__bothSmallInteger(index, value)) {
         indx = __intVal(index);
         if (indx > 0) {
-            if (!__isByteArrayLike(self))
-                indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(self))->c_ninstvars));
+            if (!__isByteArray(self)) {
+                OBJ cls = __qClass(self);
+                if (cls == ImmutableByteArray) goto immutable;
+                indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
+            }
             nIndex = __byteArraySize(self);
             if ((indx+1) <= nIndex) {
                 val.u_ushort = v = __intVal(value);
@@ -832,8 +841,9 @@
             }
         }
     }
+immutable: ;
 %}.
-    ^ super unsignedInt16At:index put:value
+    ^ self unsignedInt16At:index put:value MSB:IsBigEndian
 
     "
      |b|
@@ -856,7 +866,7 @@
 
 %{  /* NOCONTEXT */
 
-    REGISTER int indx;
+    REGISTER INT indx;
     int nIndex;
     int val;
     OBJ cls;
@@ -865,8 +875,11 @@
     if (__bothSmallInteger(index, value)) {
         indx = __intVal(index);
         if (indx > 0) {
-            if (!__isByteArrayLike(self))
-                indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(self))->c_ninstvars));
+            if (!__isByteArray(self)) {
+                OBJ cls = __qClass(self);
+                if (cls == ImmutableByteArray) goto immutable;
+                indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
+            }        
             nIndex = __byteArraySize(self);
             if ((indx+1) <= nIndex) {
                 val = __intVal(value);
@@ -920,6 +933,7 @@
             }
         }
     }
+immutable: ;
 %}.
     ^ super unsignedInt16At:index put:value MSB:msb
 
@@ -1204,13 +1218,12 @@
 !
 
 beUnsigned
-    "thats what I am (but I don't know if this is true for subclasses."
+    "that's what I am (but I don't know if this is true for subclasses."
 
     self class == ByteArray ifTrue:[
         ^ self.
-    ] ifFalse:[
-        self shouldNotImplement.
     ].
+    self shouldNotImplement.
 
     "
         #[ 1 2 3 128 255] copy beUnsigned
@@ -2471,7 +2484,7 @@
 !
 
 swapBytes
-    "swap bytes inplace -
+    "swap bytes (of int16s) inplace -
      Expects that the receiver has an even number of bytes;
      if not, only the pairs excluding the last byte are swapped.
      written as a primitive for speed on image grabbing (if display order is different)."
@@ -2483,32 +2496,32 @@
     REGISTER unsigned t;
 
     if (__qClass(self) == @global(ByteArray)) {
-	cnt = __byteArraySize(self);
-	cnt = cnt & ~1; /* make it even */
-	p = __ByteArrayInstPtr(self)->ba_element;
-
-	while (cnt >= sizeof(INT)) {
-	    unsigned INT i = ((unsigned INT *)p)[0];
+        cnt = __byteArraySize(self);
+        cnt = cnt & ~1; /* make it even */
+        p = __ByteArrayInstPtr(self)->ba_element;
+
+        while (cnt >= sizeof(INT)) {
+            unsigned INT i = ((unsigned INT *)p)[0];
 
 #if __POINTER_SIZE__ == 8
-	    i = ((i>>8) & 0x00FF00FF00FF00FF) | ((i & 0x00FF00FF00FF00FF) << 8);
+            i = ((i>>8) & 0x00FF00FF00FF00FF) | ((i & 0x00FF00FF00FF00FF) << 8);
 #else
-	    i = ((i>>8) & 0x00FF00FF) | ((i & 0x00FF00FF) << 8);
+            i = ((i>>8) & 0x00FF00FF) | ((i & 0x00FF00FF) << 8);
 #endif /* __POINTER_SIZE__ */
-	    ((unsigned INT *)p)[0] = i;
-	    p += sizeof(INT);
-	    cnt -= sizeof(INT);
-	}
-	while (cnt > 0) {
-	    unsigned short s;
-
-	    s = ((unsigned short *)p)[0];
-	    s = (s >> 8) | (s << 8);
-	    ((unsigned short *)p)[0] = s;
-	    p += 2;
-	    cnt -= 2;
-	}
-	RETURN ( self );
+            ((unsigned INT *)p)[0] = i;
+            p += sizeof(INT);
+            cnt -= sizeof(INT);
+        }
+        while (cnt > 0) {
+            unsigned short s;
+
+            s = ((unsigned short *)p)[0];
+            s = (s >> 8) | (s << 8);
+            ((unsigned short *)p)[0] = s;
+            p += 2;
+            cnt -= 2;
+        }
+        RETURN ( self );
     }
 %}.
     ^ super swapBytes "/ rubbish - there is no one currenly
@@ -2566,7 +2579,7 @@
 !
 
 swapLongsFrom:startIndex to:endIndex
-    "swap longs inplace
+    "swap longs (int32s) inplace
      - any partial longs at the end are not swapped.
      Swapping is from startIndex to (exclusiv) endIndex;
      indexing starts at 1."
@@ -2580,48 +2593,48 @@
     if ((__qClass(self) == @global(ByteArray))
      && __isSmallInteger(startIndex)
      && __isSmallInteger(endIndex)) {
-	int __idx = __intVal(startIndex);
-	int __endIdx = __intVal(endIndex);
-
-	limit = __byteArraySize(self);
-	if (__endIdx < limit) {
-	    limit = __endIdx;
-	}
-	p = __ByteArrayInstPtr(self)->ba_element;
-	p = p + __idx - 1;
-
-	limit = limit - 4 + 1;
+        int __idx = __intVal(startIndex);
+        int __endIdx = __intVal(endIndex);
+
+        limit = __byteArraySize(self);
+        if (__endIdx < limit) {
+            limit = __endIdx;
+        }
+        p = __ByteArrayInstPtr(self)->ba_element;
+        p = p + __idx - 1;
+
+        limit = limit - 4 + 1;
 
 #if defined(__BSWAP)
-	/*
-	 * can we use the bswap instruction ?
-	 * notice - not all CPUs have it (the HAS_BSWAP checks this).
-	 */
-	if (__HAS_BSWAP()
-	 && (((unsigned int)p & 3) == 0)) {
-	    unsigned int *ip;
-	    ip = (unsigned int *)p;
-
-	    while (__idx <= limit) {
-		*ip = __BSWAP(*ip);
-		ip++;
-		__idx += 4;
-	    }
-	    RETURN (self);
-	}
+        /*
+         * can we use the bswap instruction ?
+         * notice - not all CPUs have it (the HAS_BSWAP checks this).
+         */
+        if (__HAS_BSWAP()
+         && (((unsigned int)p & 3) == 0)) {
+            unsigned int *ip;
+            ip = (unsigned int *)p;
+
+            while (__idx <= limit) {
+                *ip = __BSWAP(*ip);
+                ip++;
+                __idx += 4;
+            }
+            RETURN (self);
+        }
 #endif /* __BSWAP */
 
-	while (__idx <= limit) {
-	    t = p[0];
-	    p[0] = p[3];
-	    p[3] = t;
-	    t = p[1];
-	    p[1] = p[2];
-	    p[2] = t;
-	    p += 4;
-	    __idx += 4;
-	}
-	RETURN ( self );
+        while (__idx <= limit) {
+            t = p[0];
+            p[0] = p[3];
+            p[3] = t;
+            t = p[1];
+            p[1] = p[2];
+            p[2] = t;
+            p += 4;
+            __idx += 4;
+        }
+        RETURN ( self );
     }
 %}.
     ^ super swapLongsFrom:startIndex to:endIndex "/ rubbish - there is no one currenly
@@ -2827,28 +2840,31 @@
     int len;
 
     if (__isByteArrayLike(self)) {
-	max = 0;
-	index = 0;
-	len = __qSize(self) - OHDR_SIZE;
-	cp = &(__ByteArrayInstPtr(self)->ba_element[0]);
-
-	while (++index <= len) {
-	    unsigned int byte;
-
-	    byte = *cp;
-	    cp++;
-	    if (byte > max) {
-		max = byte;
-		if (byte == 255) break;
-	    }
-	}
-	RETURN ( __mkSmallInteger(max) );
+        max = 0;
+        index = 0;
+        len = __qSize(self) - OHDR_SIZE;
+        cp = &(__ByteArrayInstPtr(self)->ba_element[0]);
+
+        if (len > 0) {
+            while (++index <= len) {
+                unsigned int byte;
+
+                byte = *cp;
+                cp++;
+                if (byte > max) {
+                    max = byte;
+                    if (byte == 255) break;
+                }
+            }
+            RETURN ( __mkSmallInteger(max) );
+        }
     }
 %}.
     ^ super max
 
     "
      #[1 2 3 1 2 3 1 2 19] max
+     #[] max
     "
 !
 
@@ -2979,7 +2995,6 @@
     "
 ! !
 
-
 !ByteArray methodsFor:'searching'!
 
 indexOf:aByte startingAt:start