String.st
changeset 5407 d6729266a95b
parent 5312 ac5719fafe43
child 5556 1056cc5d6ce0
--- a/String.st	Fri Jun 23 10:18:11 2000 +0200
+++ b/String.st	Fri Jun 23 10:21:17 2000 +0200
@@ -10,6 +10,8 @@
  hereby transferred.
 "
 
+"{ Package: 'stx:libbasic' }"
+
 CharacterArray variableByteSubclass:#String
 	instanceVariableNames:''
 	classVariableNames:''
@@ -230,7 +232,7 @@
      invalid argument, or out-of-memory:
      use error handling in superclass
     "
-    ^ (super basicNew:anInteger) atAllPut:(Character space)
+    ^ (super basicNew:anInteger+1) atAllPut:(Character space)
 !
 
 new:n
@@ -404,23 +406,20 @@
     REGISTER OBJ slf, cls;
 
     if (__isSmallInteger(index)) {
-	slf = self;
-	cls = __qClass(slf);
-	indx = __intVal(index) - 1;
-	if (cls != String) {
-	    if (indx < 0) goto badIndex;
-	    indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
-	}
-	if ((unsigned)indx < (unsigned)(__stringSize(slf))) {
-	    RETURN ( __MKCHARACTER(__stringVal(slf)[indx] & 0xFF) );
-	}
+        slf = self;
+        cls = __qClass(slf);
+        indx = __intVal(index) - 1;
+        if (cls != String) {
+            if (indx < 0) goto badIndex;
+            indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
+        }
+        if ((unsigned)indx < (unsigned)(__stringSize(slf))) {
+            RETURN ( __MKCHARACTER(__stringVal(slf)[indx] & 0xFF) );
+        }
     }
 badIndex: ;
 %}.
-    index isInteger ifFalse:[
-	^ self indexNotInteger:index
-    ].
-    ^ self subscriptBoundsError:index
+    ^ self basicAt:index
 !
 
 at:index put:aCharacter
@@ -438,44 +437,20 @@
     slf = self;
 
     if (__isString(slf)) {
-	if (__isCharacter(aCharacter)) {
-	    value = __intVal(_characterVal(aCharacter));
-	    if (((unsigned)value <= 0xFF)
-	     && __isSmallInteger(index)) {
-		indx = __intVal(index) - 1;
-		if ((unsigned)indx < (unsigned)(__stringSize(slf))) {
-		    __stringVal(slf)[indx] = value;
-		    RETURN ( aCharacter );
-		}
-	    }
-	}
+        if (__isCharacter(aCharacter)) {
+            value = __intVal(_characterVal(aCharacter));
+            if (((unsigned)value <= 0xFF)
+             && __isSmallInteger(index)) {
+                indx = __intVal(index) - 1;
+                if ((unsigned)indx < (unsigned)(__stringSize(slf))) {
+                    __stringVal(slf)[indx] = value;
+                    RETURN ( aCharacter );
+                }
+            }
+        }
     }
 %}.
-    (self isMemberOf:String) ifFalse:[
-	^ super at:index put:aCharacter
-    ].
-
-    (aCharacter isMemberOf:Character) ifFalse:[
-	"
-	 tried to store something which is not a character
-	"
-	^ self elementNotCharacter
-    ].
-    (aCharacter asciiValue between:1 and:255) ifFalse:[
-	"
-	 tried to store a multibyte character
-	"
-	^ self elementBoundsError
-    ].
-    "
-     invalid index
-    "
-    index isInteger ifFalse:[
-	^ self indexNotInteger:index
-    ].
-    ^ self subscriptBoundsError:index
-
-    "Modified: 19.4.1996 / 11:40:27 / cg"
+    ^ self basicAt:index put:aCharacter
 !
 
 basicAt:index
@@ -488,23 +463,26 @@
     REGISTER OBJ slf, cls;
 
     if (__isSmallInteger(index)) {
-	slf = self;
-	cls = __qClass(slf);
-	indx = __intVal(index) - 1;
-	if (cls != String) {
-	    if (indx < 0) goto badIndex;
-	    indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
-	}
-	if ((unsigned)indx < (unsigned)(__stringSize(slf))) {
-	    RETURN ( __MKCHARACTER(__stringVal(slf)[indx] & 0xFF) );
-	}
+        slf = self;
+        cls = __qClass(slf);
+        indx = __intVal(index) - 1;
+        if (cls != String) {
+            if (indx < 0) goto badIndex;
+            indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
+        }
+        if ((unsigned)indx < (unsigned)(__stringSize(slf))) {
+            RETURN ( __MKCHARACTER(__stringVal(slf)[indx] & 0xFF) );
+        }
     }
 badIndex: ;
 %}.
     index isInteger ifFalse:[
-	^ self indexNotInteger:index
+        ^ self indexNotInteger:index
     ].
-    ^ self subscriptBoundsError:index
+    index == super basicSize ifTrue:[
+        ^ self subscriptBoundsError:index
+    ].
+    ^ Character value:(super basicAt:index)
 !
 
 basicAt:index put:aCharacter
@@ -521,44 +499,47 @@
     slf = self;
 
     if (__isCharacter(aCharacter)) {
-	value = __intVal(_characterVal(aCharacter));
-	if (((unsigned)value <= 0xFF)
-	 && __isSmallInteger(index)) {
-	    cls = __qClass(slf);
-	    indx = __intVal(index) - 1;
-	    if (cls != String) {
-		if (indx < 0) goto badIndex;
-		indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
-	    }
-	    if ((unsigned)indx < (unsigned)(__stringSize(slf))) {
-		__stringVal(slf)[indx] = value;
-		RETURN ( aCharacter );
-	    }
-	}
+        value = __intVal(_characterVal(aCharacter));
+        if (((unsigned)value <= 0xFF)
+         && __isSmallInteger(index)) {
+            cls = __qClass(slf);
+            indx = __intVal(index) - 1;
+            if (cls != String) {
+                if (indx < 0) goto badIndex;
+                indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
+            }
+            if ((unsigned)indx < (unsigned)(__stringSize(slf))) {
+                __stringVal(slf)[indx] = value;
+                RETURN ( aCharacter );
+            }
+        }
     }
 badIndex: ;
 %}.
     (aCharacter isMemberOf:Character) ifFalse:[
-	"
-	 tried to store something which is not a character
-	"
-	^ self elementNotCharacter
+        "
+         tried to store something which is not a character
+        "
+        ^ self elementNotCharacter
     ].
     (aCharacter asciiValue between:1 and:255) ifFalse:[
-	"
-	 tried to store a multibyte character
-	"
-	^ self elementBoundsError
+        "
+         tried to store a multibyte character
+        "
+        ^ self elementBoundsError
     ].
     "
      invalid index
     "
     index isInteger ifFalse:[
-	^ self indexNotInteger:index
+        ^ self indexNotInteger:index
     ].
-    ^ self subscriptBoundsError:index
-
-    "Modified: 19.4.1996 / 11:15:48 / cg"
+    index == super basicSize ifTrue:[
+        ^ self subscriptBoundsError:index
+    ].
+    super basicAt:index put:aCharacter asciiValue.
+    ^ aCharacter 
+
 !
 
 basicSize
@@ -572,11 +553,13 @@
 
     cls = __qClass(slf);
     if (cls == String) {
-	RETURN ( __MKSMALLINT(__stringSize(slf)) );
+        RETURN ( __MKSMALLINT(__stringSize(slf)) );
     }
     RETURN ( __MKSMALLINT(__stringSize(slf)
-			  - __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars))));
-%}
+                          - __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars))));
+%}.
+    ^ super basicSize - 1
+
 !
 
 size
@@ -591,11 +574,12 @@
     slf = self;
     cls = __qClass(slf);
     if (cls == String) {
-	RETURN ( __MKSMALLINT(__stringSize(slf)) );
+        RETURN ( __MKSMALLINT(__stringSize(slf)) );
     }
     RETURN ( __MKSMALLINT(__stringSize(slf)
-			 - __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars))));
-%}
+                         - __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars))));
+%}.
+    ^ self basicSize
 ! !
 
 !String methodsFor:'binary storage'!
@@ -650,31 +634,33 @@
     OBJ cls;
 
     if (__isCharacter(aCharacter)) {
-	cp = __stringVal(self);
-	if ((cls = __qClass(self)) != String)
-	    cp += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
+        cp = __stringVal(self);
+        if ((cls = __qClass(self)) != String)
+            cp += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
 #ifdef FAST_STRCHR
-	cp = (unsigned char *) strchr(cp, __intVal(_characterVal(aCharacter)));
-	if (cp) {
-	    RETURN ( __MKSMALLINT(cp - __stringVal(self) + 1) );
-	}
+        cp = (unsigned char *) strchr(cp, __intVal(_characterVal(aCharacter)));
+        if (cp) {
+            RETURN ( __MKSMALLINT(cp - __stringVal(self) + 1) );
+        }
 #else
-	byteValue = __intVal(_characterVal(aCharacter));
-	index = 1;
-	while (c = *cp++) {
-	    if (c == byteValue) { RETURN ( __MKSMALLINT(index) ); }
-	    index++;
-	}
+        byteValue = __intVal(_characterVal(aCharacter));
+        index = 1;
+        while (c = *cp++) {
+            if (c == byteValue) { RETURN ( __MKSMALLINT(index) ); }
+            index++;
+        }
 #endif
-	RETURN ( __MKSMALLINT(0));
+        RETURN ( __MKSMALLINT(0));
     }
+    /* with identity compares, only characters can be in myself */
+    RETURN ( __MKSMALLINT(0));
 %}.
-    "/ with identity compares, only characters can be in myself
-    ^ 0
+    ^ self primitiveFailed
 
     "
      'hello world' indexOf:(Character space)                  
      'hello world' indexOf:$A                      
+     'hello world' indexOf:1                      
     "
 !
 
@@ -732,21 +718,20 @@
     OBJ cls;
 
     if (__isString(aCollection)) {
-	matchP = __stringVal(aCollection);
-	cp = __stringVal(self);
-	if ((cls = __qClass(self)) != String)
-	    cp += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
-
-	while (*cp) {
-	    if (strchr(matchP, *cp)) {
-		RETURN ( true );
-	    }
-	    cp++;
-	}
-	RETURN ( false );
+        matchP = __stringVal(aCollection);
+        cp = __stringVal(self);
+        if ((cls = __qClass(self)) != String)
+            cp += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
+
+        while (*cp) {
+            if (strchr(matchP, *cp)) {
+                RETURN ( true );
+            }
+            cp++;
+        }
+        RETURN ( false );
     }
-%}
-.
+%}.
     ^ super includesAny:aCollection
 
     "
@@ -983,26 +968,27 @@
 
     index = __intVal(start);
     if (index <= 0) {
-	index = 1;
+        index = 1;
     }
     if ((cls = __qClass(self)) != String)
-	index += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
+        index += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
     len = __stringSize(self);
     if (index > len) {
-	RETURN ( __MKSMALLINT(0) );
+        RETURN ( __MKSMALLINT(0) );
     }
     cp = __stringVal(self) + index - 1;
     while (c = *cp++) {
 #ifndef NON_ASCII       /* i.e. EBCDIC ;-) */
-	if (c > ' ')
+        if (c > ' ')
 #endif
-	if ((c != ' ') && (c != '\t') && (c != '\n')
-	 && (c != '\r') && (c != '\f')) {
-	    RETURN ( __MKSMALLINT(cp - __stringVal(self)) );
-	}
+        if ((c != ' ') && (c != '\t') && (c != '\n')
+         && (c != '\r') && (c != '\f')) {
+            RETURN ( __MKSMALLINT(cp - __stringVal(self)) );
+        }
     }
+    RETURN ( __MKSMALLINT(0) );
 %}.
-    ^ 0
+    ^ super indexOfNonSeparatorStartingAt:start
 
     "
      'hello world' indexOfNonWhiteSpaceStartingAt:3 
@@ -1022,27 +1008,27 @@
 
     index = __intVal(start);
     if (index <= 0) {
-	index = 1;
+        index = 1;
     }
     if ((cls = __qClass(self)) != String)
-	index += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
+        index += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
     len = __stringSize(self);
     if (index > len) {
-	RETURN ( __MKSMALLINT(0) );
+        RETURN ( __MKSMALLINT(0) );
     }
     cp = __stringVal(self) + index - 1;
     while (c = *cp++) {
 #ifndef NON_ASCII       /* i.e. EBCDIC ;-) */
-	if (c <= ' ')
+        if (c <= ' ')
 #endif
-	if ((c == ' ') || (c == '\t') || (c == '\n')
-	 || (c == '\r') || (c == '\f')) {
-	    RETURN ( __MKSMALLINT(cp - __stringVal(self)) );
-	}
+        if ((c == ' ') || (c == '\t') || (c == '\n')
+         || (c == '\r') || (c == '\f')) {
+            RETURN ( __MKSMALLINT(cp - __stringVal(self)) );
+        }
     }
-%}
-.
-    ^ 0
+    RETURN ( __MKSMALLINT(0) );
+%}.
+    ^ super indexOfSeparatorStartingAt:start
 
     "
      'hello world' indexOfSeparatorStartingAt:3 
@@ -1494,7 +1480,7 @@
     "if a symbol with the receivers characters is already known, return it.
      Otherwise, return nil. This can be used to query for an existing
      symbol and is the same as
-	self knownAsSymbol ifTrue:[self asSymbol] ifFalse:[nil]
+        self knownAsSymbol ifTrue:[self asSymbol] ifFalse:[nil]
      but slightly faster, since the symbol lookup operation is only
      performed once."
 
@@ -1504,13 +1490,13 @@
 
     cls = __qClass(self);
     if (cls != String) {
-	indx = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
+        indx = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
     } else {
-	indx = 0;
+        indx = 0;
     }
     RETURN ( __SYMBOL_OR_NIL(__stringVal(self) + indx));
-%}
-
+%}.
+    self primitiveFailed
     "
      'hello' asSymbolIfInterned
      'fooBarBaz' asSymbolIfInterned
@@ -2730,12 +2716,13 @@
 
     cls = __qClass(self);
     if (cls != String) {
-	indx = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
+        indx = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
     } else {
-	indx = 0;
+        indx = 0;
     }
     RETURN ( __KNOWNASSYMBOL(__stringVal(self) + indx) );
-%}
+%}.
+    self primitiveFailed
 
     "
      'hello' knownAsSymbol     
@@ -2860,27 +2847,28 @@
 
     src = __stringVal(self);
     if ((cls = __qClass(self)) != String)
-	src += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
+        src += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
 
 #ifndef NON_ASCII
 # ifdef UINT64
     while (*((UINT64 *)src) == 0x2020202020202020L) {
-	src += 8;
+        src += 8;
     }
 # endif /* UINT64 */
 
     while (*((unsigned *)src) == 0x20202020) {
-	src += 4;
+        src += 4;
     }
 #endif /* ascii */
 
     while (c = *src++) {
-	if (c != ' ') {
-	    RETURN ( false );
-	}
+        if (c != ' ') {
+            RETURN ( false );
+        }
     }
+    RETURN ( true );
 %}.
-    ^ true
+    ^ super isBlank
 !
 
 levenshteinTo:aString s:substWeight c:caseWeight i:insrtWeight d:deleteWeight
@@ -3105,5 +3093,5 @@
 !String class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/String.st,v 1.151 2000-03-21 12:56:10 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/String.st,v 1.152 2000-06-23 08:20:15 cg Exp $'
 ! !