ByteArray.st
changeset 19645 053a5232234d
parent 19619 beb60d40e286
child 19659 0f585374259a
child 19764 9a79f79d50c4
--- a/ByteArray.st	Sat Apr 23 18:13:55 2016 +0200
+++ b/ByteArray.st	Sun Apr 24 17:51:25 2016 +0200
@@ -165,7 +165,6 @@
 
 
 
-
 !ByteArray class methodsFor:'queries'!
 
 elementByteSize
@@ -2870,6 +2869,101 @@
     "
 !
 
+startsWith:aByteOrByteArray
+    "return true, if the receiver starts with something, aStringOrChar.
+     If the argument is empty, true is returned.
+     Notice, that this is similar to, but slightly different from VW's and Squeak's beginsWith:,
+     which are both inconsistent w.r.t. an empty argument."
+
+%{  /* NOCONTEXT */
+    int len1, len2;
+    REGISTER unsigned char *src1, *src2;
+    unsigned char c;
+    REGISTER OBJ slf = self;
+
+    if (__qIsByteArrayLike(slf) &&__isByteArrayLike(aByteOrByteArray)) {
+        src1 = __byteArrayVal(slf);
+        src2 = __byteArrayVal(aByteOrByteArray);
+
+        if (src1[0] != src2[0]) {
+            if (__qSize(aByteOrByteArray) == OHDR_SIZE) {
+                RETURN (true);
+            }
+            RETURN ( false );
+        }
+
+        len1 = __qSize(slf);
+        len2 = __qSize(aByteOrByteArray);
+        if (len1 < len2) {
+            RETURN ( false );
+        }
+
+# ifdef UINT64
+        while (len2 > (OHDR_SIZE+sizeof(UINT64))) {
+            if ( ((UINT64 *)src1)[0] != ((UINT64 *)src2)[0] ) {
+                RETURN (false);
+            }
+            len2 -= sizeof(UINT64);
+            src1 += sizeof(UINT64);
+            src2 += sizeof(UINT64);
+        }
+# else
+#  ifdef __UNROLL_LOOPS__
+        while (len2 > (OHDR_SIZE+sizeof(INT)*4)) {
+            if ( ((unsigned INT *)src1)[0] != ((unsigned INT *)src2)[0]) {
+                RETURN (false);
+            }
+            if ( ((unsigned INT *)src1)[1] != ((unsigned INT *)src2)[1]) {
+                RETURN (false);
+            }
+            if ( ((unsigned INT *)src1)[2] != ((unsigned INT *)src2)[2]) {
+                RETURN (false);
+            }
+            if ( ((unsigned INT *)src1)[3] != ((unsigned INT *)src2)[3]) {
+                RETURN (false);
+            }
+            len2 -= sizeof(INT)*4;
+            src1 += sizeof(INT)*4;
+            src2 += sizeof(INT)*4;
+        }
+#  endif /* __UNROLL_LOOPS__ */
+# endif /* UINT64 */
+
+        while (len2 > (OHDR_SIZE+sizeof(INT))) {
+            if ( ((unsigned INT *)src1)[0] != ((unsigned INT *)src2)[0]) {
+                RETURN (false);
+            }
+            len2 -= sizeof(INT);
+            src1 += sizeof(INT);
+            src2 += sizeof(INT);
+        }
+
+        for ( ; len2 > (OHDR_SIZE+sizeof(INT)); len2++) {
+            if (*src1++ != *src2++) {
+                RETURN (false);
+            }
+        }    
+        RETURN (true);
+    }
+    if (__isSmallInteger(aByteOrByteArray)) {
+        int val = __intVal(aByteOrByteArray);
+        if (__byteArraySize(slf) > 0) {
+            RETURN ( (__byteArrayVal(slf)[0] == val) ? true : false);
+        }
+        RETURN ( false );
+    }
+%}.
+    ^ super startsWith:aByteOrByteArray
+
+    "
+     #[1 2 3 4 5 6 7 8 9 10] startsWith:#[ 1 2 3 4 5]
+     #[1 2 3 4 5 6 7 8 9 10] startsWith:#[ 0 1 2 3 4 5]
+     #[1 2 3 4 5 6 7 8 9 10] startsWith:#(1 2 3 4 5)
+     #[1 2 3 4 5 6 7 8 9 10] startsWith:1
+     #[1 2 3 4 5 6 7 8 9 10] startsWith:2
+    "
+!
+
 usageCounts
     "return an array filled with value-counts -
      This is needed in the bitmap/image classes to get info on color usage.
@@ -3063,6 +3157,7 @@
     "
 ! !
 
+
 !ByteArray methodsFor:'testing'!
 
 isByteArray
@@ -3087,7 +3182,6 @@
     "Modified: 22.4.1996 / 12:55:30 / cg"
 ! !
 
-
 !ByteArray class methodsFor:'documentation'!
 
 version