ByteArray.st
changeset 3325 e958a341f41d
parent 3234 4cc58bc6a8ed
child 3345 6cbaa948a813
--- a/ByteArray.st	Fri Mar 06 16:25:24 1998 +0100
+++ b/ByteArray.st	Fri Mar 06 16:26:24 1998 +0100
@@ -66,6 +66,18 @@
 
 !ByteArray class methodsFor:'instance creation'!
 
+from:aByteArray
+        "return new instance which a copy of aByteArray"
+
+        |len bytes|
+        len := aByteArray size.
+        bytes := self new:len.
+        bytes replaceBytesFrom:1 to:len with:aByteArray startingAt:1.
+        ^ bytes
+
+    "Created: / 5.3.1998 / 15:57:52 / stefan"
+!
+
 fromPackedString:aString
     "ST-80 compatibility: decode a byteArray from a packed string in which
      6bits are encoded per character. The argument, aString must be a multiple 
@@ -221,6 +233,19 @@
 	^ ObjectMemory allocationFailureSignal raise.
     ].
     ^ self basicNew:anInteger
+!
+
+with:aByteArray from:start to:stop
+        "return new instance with a copy of aByteArray
+         beginning at index start up to and including index stop"
+
+        |len bytes|
+        len := stop-start+1.
+        bytes := self new:len.
+        bytes replaceBytesFrom:1 to:len with:aByteArray startingAt:start.
+        ^ bytes
+
+    "Modified: / 5.3.1998 / 16:00:18 / stefan"
 ! !
 
 !ByteArray class methodsFor:'binary storage'!
@@ -895,6 +920,67 @@
      will eventually lead to an out-of-bound signal raise
     "
     ^ super copyFrom:start to:stop
+!
+
+symbolFrom:start to:stop
+    "make a symbol from the characters of the subcollection starting 
+     at index start, anInteger and ending at stop, anInteger.
+     This saves us garbage and character copying."
+
+    |sym|
+
+%{  /* STACK:1024 */
+
+    REGISTER unsigned char *srcp;
+    REGISTER unsigned char *endp;
+    REGISTER int count;
+    int len, index1, index2;
+    unsigned char s[1024], savec;
+
+    if (__isByteArray(self) && __bothSmallInteger(start, stop)) {
+        len = __byteArraySize(self);
+        index1 = __intVal(start);
+        index2 = __intVal(stop);
+
+        if ((index1 <= index2) && (index1 > 0) && (index2 <= len)) {
+            count = index2 - index1 + 1;
+            srcp = __ByteArrayInstPtr(self)->ba_element + index1 - 1;
+            if (index2 < len) {
+                /* temporarily stuff in a '\0' */
+                endp = srcp + count + 1;
+                savec = *endp;
+                *endp = '\0';
+                sym = __MKSYMBOL(srcp, 0);
+                endp = __ByteArrayInstPtr(self)->ba_element + index1 + count;
+                *endp = savec;
+            } else if (count < sizeof(s)) {
+                /* not enough space for '\0', copy the bytes */
+                bcopy(srcp, s, count);
+                s[count] = '\0';
+                sym = __MKSYMBOL(s, 0);
+            }
+        }
+        if (sym != nil)
+            RETURN(sym);
+    }
+%}.
+    "
+     fall back in case of non-integer index or out-of-bound index
+     or no memory available;
+     will eventually lead to an out-of-bound signal raise
+    "
+    ^ (super copyFrom:start to:stop) asString asSymbol
+
+
+    "
+     'abcdefghijklmnop' symbolFrom:1 to:3
+     'abcdefghijklmnop' symbolFrom:3 to:16
+     'abcdefghijklmnop' symbolFrom:3 to:17
+    "
+
+
+
+
 ! !
 
 !ByteArray methodsFor:'filling and replacing'!
@@ -1896,5 +1982,5 @@
 !ByteArray class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.95 1998-01-28 15:52:24 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.96 1998-03-06 15:26:24 stefan Exp $'
 ! !