String.st
changeset 25098 787f66dc3405
parent 25018 6024e5e63311
child 25350 8f0794692a17
--- a/String.st	Thu Dec 12 13:46:19 2019 +0100
+++ b/String.st	Thu Dec 12 14:20:34 2019 +0100
@@ -4227,51 +4227,51 @@
     REGISTER unsigned char *ep0;
 
     /* ignore instances of subclasses ... */
-    if (__qClass(self) == String) {
-	cp = cp0 = __stringVal(self);
-
-	/*
-	 * find first non-whiteSpace from beginning
-	 */
+    if (__isStringLike(self)) {
+        cp = cp0 = __stringVal(self);
+
+        /*
+         * find first non-whiteSpace from beginning
+         */
 #ifdef UINT64
-	while (*((UINT64 *)cp) == 0x2020202020202020L) {
-	    cp += 8;
-	}
+        while (*((UINT64 *)cp) == 0x2020202020202020L) {
+            cp += 8;
+        }
 #endif
-	while (*((unsigned *)cp) == 0x20202020) {
-	    cp += 4;
-	}
-	while ((c = *cp)
-	 && (c <= ' ')
-	 && ((c == ' ') || (c == '\n') || (c == '\t')
-			|| (c == '\r') || (c == '\f'))
-	) {
-	    cp++;
-	}
-
-	/*
-	 * find first non-whiteSpace from end
-	 */
-	ep = ep0 = cp0 + __stringSize(self) - 1;
-	while ((ep >= cp) && (*ep == ' ')) ep--;
-	c = *ep;
-	while ((ep >= cp) &&
-	       (c <= ' ') &&
-	       ((c == ' ') || (c == '\n') || (c == '\t')
-			   || (c == '\r') || (c == '\f'))) {
-	    ep--;
-	    c = *ep;
-	}
-
-	/*
-	 * no whiteSpace ?
-	 */
-	if ((cp == cp0) && (ep == ep0)) {
-	    RETURN(self);
-	}
-
-	startIndex = __mkSmallInteger(cp - cp0 + 1);
-	endIndex = __mkSmallInteger(ep - cp0 + 1);
+        while (*((unsigned *)cp) == 0x20202020) {
+            cp += 4;
+        }
+        while ((c = *cp)
+         && (c <= ' ')
+         && ((c == ' ') || (c == '\n') || (c == '\t')
+                        || (c == '\r') || (c == '\f'))
+        ) {
+            cp++;
+        }
+
+        /*
+         * find first non-whiteSpace from end
+         */
+        ep = ep0 = cp0 + __stringSize(self) - 1;
+        while ((ep >= cp) && (*ep == ' ')) ep--;
+        c = *ep;
+        while ((ep >= cp) &&
+               (c <= ' ') &&
+               ((c == ' ') || (c == '\n') || (c == '\t')
+                           || (c == '\r') || (c == '\f'))) {
+            ep--;
+            c = *ep;
+        }
+
+        /*
+         * no whiteSpace ?
+         */
+        if ((cp == cp0) && (ep == ep0)) {
+            RETURN(self);
+        }
+
+        startIndex = __mkSmallInteger(cp - cp0 + 1);
+        endIndex = __mkSmallInteger(ep - cp0 + 1);
     }
 %}.
     startIndex == 0 ifTrue:[^ super withoutSeparators].
@@ -4288,6 +4288,8 @@
      '    hello    ' withoutSeparators
      '        ' withoutSeparators
     "
+
+    "Modified: / 12-12-2019 / 14:16:51 / Stefan Vogel"
 !
 
 withoutSpaces
@@ -4308,37 +4310,37 @@
     unsigned char *ep0;
 
     /* ignore instances of subclasses ... */
-    if (__qClass(self) == String) {
-	cp = cp0 = __stringVal(self);
-
-	/*
-	 * find first non-blank from beginning
-	 */
+    if (__isStringLike(self)) {
+        cp = cp0 = __stringVal(self);
+
+        /*
+         * find first non-blank from beginning
+         */
 #ifdef UINT64
-	while (*((UINT64 *)cp) == 0x2020202020202020L) {
-	    cp += 8;
-	}
+        while (*((UINT64 *)cp) == 0x2020202020202020L) {
+            cp += 8;
+        }
 #endif /* UINT64 */
-	while (*((unsigned *)cp) == 0x20202020) {
-	    cp += 4;
-	}
-	while (*cp == ' ') cp++;
-
-	/*
-	 * find first non-blank from end
-	 */
-	ep = ep0 = cp0 + __stringSize(self) - 1;
-	while ((ep >= cp) && (*ep == ' ')) ep--;
-
-	/*
-	 * no blanks ?
-	 */
-	if ((cp == cp0) && (ep == ep0)) {
-	    RETURN(self);
-	}
-
-	startIndex = __mkSmallInteger(cp - cp0 + 1);
-	endIndex = __mkSmallInteger(ep - cp0 + 1);
+        while (*((unsigned *)cp) == 0x20202020) {
+            cp += 4;
+        }
+        while (*cp == ' ') cp++;
+
+        /*
+         * find first non-blank from end
+         */
+        ep = ep0 = cp0 + __stringSize(self) - 1;
+        while ((ep >= cp) && (*ep == ' ')) ep--;
+
+        /*
+         * no blanks ?
+         */
+        if ((cp == cp0) && (ep == ep0)) {
+            RETURN(self);
+        }
+
+        startIndex = __mkSmallInteger(cp - cp0 + 1);
+        endIndex = __mkSmallInteger(ep - cp0 + 1);
     }
 %}.
     startIndex == 0 ifTrue:[^ super withoutSpaces].
@@ -4355,6 +4357,71 @@
      '    hello    ' withoutSpaces
      '        ' withoutSpaces
     "
+
+    "Modified: / 12-12-2019 / 14:16:58 / Stefan Vogel"
+!
+
+withoutTrailingSeparators
+    "return a copy of myself without trailing separators.
+     Notice: this does remove tabs, newline or any other whitespace.
+     Returns an empty string, if the receiver consist only of whitespace."
+
+    |endIndex   "{ Class: SmallInteger }"|
+
+    endIndex := -1.
+%{
+    REGISTER unsigned char *cp0;
+    REGISTER unsigned char *ep;
+    REGISTER unsigned char c;
+    REGISTER unsigned char *ep0;
+
+    /* ignore instances of subclasses ... */
+    if (__isStringLike(self)) {
+        cp0 = __stringVal(self);
+
+        /*
+         * find first non-whiteSpace from end
+         */
+        ep = ep0 = cp0 + __stringSize(self) - 1;
+        while ((ep >= cp0) && (*ep == ' ')) ep--;
+        c = *ep;
+        while ((ep >= cp0) &&
+               (c <= ' ') &&
+               ((c == ' ') || (c == '\n') || (c == '\t')
+                           || (c == '\r') || (c == '\f'))) {
+            ep--;
+            c = *ep;
+        }
+
+        /*
+         * no whiteSpace ?
+         */
+        if (ep == ep0) {
+            RETURN(self);
+        }
+
+        endIndex = __mkSmallInteger(ep - cp0 + 1);
+    }
+%}.
+    endIndex == -1 ifTrue:[^ super withoutTrailingSeparators].
+    endIndex == 0 ifTrue:[^ ''].
+    ^ self copyFrom:1 to:endIndex
+
+
+    "
+     '    foo    ' withoutTrailingSeparators
+     'foo    '     withoutTrailingSeparators
+     '    foo'     withoutTrailingSeparators
+     '       '     withoutTrailingSeparators
+     'foo'         withoutTrailingSeparators
+     'f'           withoutTrailingSeparators
+     'f '          withoutTrailingSeparators
+     ''            withoutTrailingSeparators
+     ('  ' , Character tab , ' foo   ') withoutTrailingSeparators inspect
+     ('   foo' , Character tab) withoutTrailingSeparators inspect
+    "
+
+    "Created: / 12-12-2019 / 14:00:23 / Stefan Vogel"
 ! !
 
 !String methodsFor:'printing & storing'!