added withoutTrailingSeparators
authorClaus Gittinger <cg@exept.de>
Tue, 30 Jan 1996 18:14:23 +0100
changeset 915 1ac1c7e942f4
parent 914 d00401a9450e
child 916 a050d17c7e1f
added withoutTrailingSeparators
CharArray.st
CharacterArray.st
--- a/CharArray.st	Sun Jan 28 14:12:25 1996 +0100
+++ b/CharArray.st	Tue Jan 30 18:14:23 1996 +0100
@@ -1235,11 +1235,11 @@
      (sounds complicated ? -> see examples below)."
 
     ^ self 
-        decimalPaddedTo:size 
-        and:afterPeriod 
-        at:decimalCharacter
-        withLeft:(Character space)
-        right:$0
+	decimalPaddedTo:size 
+	and:afterPeriod 
+	at:decimalCharacter
+	withLeft:(Character space)
+	right:$0
 
     "                                                    
      '123' decimalPaddedTo:10 and:3 at:$.      -> '   123    '  
@@ -1268,23 +1268,23 @@
 
     idx := self indexOf:decimalCharacter.
     idx == 0 ifTrue:[
-        "/
-        "/ no decimal point found; adjust string to the left of the period column
-        "/
-        rightPadChar isNil ifTrue:[
-            s := self , (self species new:afterPeriod + 1 withAll:leftPadChar)
-        ] ifFalse:[
-            s:= self , decimalCharacter asString , (self species new:afterPeriod withAll:rightPadChar).
-        ].
+	"/
+	"/ no decimal point found; adjust string to the left of the period column
+	"/
+	rightPadChar isNil ifTrue:[
+	    s := self , (self species new:afterPeriod + 1 withAll:leftPadChar)
+	] ifFalse:[
+	    s:= self , decimalCharacter asString , (self species new:afterPeriod withAll:rightPadChar).
+	].
     ] ifFalse:[
 
-        "/ the number of after-decimalPoint characters
-        n := self size - idx.
-        rightPadChar isNil ifTrue:[
-            s := self , (self species new:afterPeriod - n withAll:leftPadChar).
-        ] ifFalse:[
-            s := self , (self species new:afterPeriod - n withAll:rightPadChar).
-        ].
+	"/ the number of after-decimalPoint characters
+	n := self size - idx.
+	rightPadChar isNil ifTrue:[
+	    s := self , (self species new:afterPeriod - n withAll:leftPadChar).
+	] ifFalse:[
+	    s := self , (self species new:afterPeriod - n withAll:rightPadChar).
+	].
     ].
 
     ^ s leftPaddedTo:size with:leftPadChar
@@ -2062,6 +2062,33 @@
      '       '     withoutSpaces   
      ('  foo' , Character tab asString , '    ') withoutSpaces inspect 
     "
+!
+
+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."
+
+    |index|
+
+    index := self size.
+    [index ~~ 0] whileTrue:[
+	(self at:index) isSeparator ifFalse:[
+	    ^ self copyTo:index
+	].
+	index := index - 1
+    ].
+    ^ ''
+
+    "
+     '    foo    ' withoutTrailingSeparators  
+     'foo    '     withoutTrailingSeparators   
+     '    foo'     withoutTrailingSeparators  
+     '       '     withoutTrailingSeparators   
+     'foo'         withoutTrailingSeparators   
+     ('  ' , Character tab asString , ' foo   ') withoutTrailingSeparators inspect 
+     ('   foo' , Character tab asString) withoutTrailingSeparators inspect 
+    "
 ! !
 
 !CharacterArray methodsFor:'substring searching'!
@@ -2459,5 +2486,5 @@
 !CharacterArray class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Attic/CharArray.st,v 1.48 1996-01-03 23:39:17 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Attic/CharArray.st,v 1.49 1996-01-30 17:14:23 cg Exp $'
 ! !
--- a/CharacterArray.st	Sun Jan 28 14:12:25 1996 +0100
+++ b/CharacterArray.st	Tue Jan 30 18:14:23 1996 +0100
@@ -1235,11 +1235,11 @@
      (sounds complicated ? -> see examples below)."
 
     ^ self 
-        decimalPaddedTo:size 
-        and:afterPeriod 
-        at:decimalCharacter
-        withLeft:(Character space)
-        right:$0
+	decimalPaddedTo:size 
+	and:afterPeriod 
+	at:decimalCharacter
+	withLeft:(Character space)
+	right:$0
 
     "                                                    
      '123' decimalPaddedTo:10 and:3 at:$.      -> '   123    '  
@@ -1268,23 +1268,23 @@
 
     idx := self indexOf:decimalCharacter.
     idx == 0 ifTrue:[
-        "/
-        "/ no decimal point found; adjust string to the left of the period column
-        "/
-        rightPadChar isNil ifTrue:[
-            s := self , (self species new:afterPeriod + 1 withAll:leftPadChar)
-        ] ifFalse:[
-            s:= self , decimalCharacter asString , (self species new:afterPeriod withAll:rightPadChar).
-        ].
+	"/
+	"/ no decimal point found; adjust string to the left of the period column
+	"/
+	rightPadChar isNil ifTrue:[
+	    s := self , (self species new:afterPeriod + 1 withAll:leftPadChar)
+	] ifFalse:[
+	    s:= self , decimalCharacter asString , (self species new:afterPeriod withAll:rightPadChar).
+	].
     ] ifFalse:[
 
-        "/ the number of after-decimalPoint characters
-        n := self size - idx.
-        rightPadChar isNil ifTrue:[
-            s := self , (self species new:afterPeriod - n withAll:leftPadChar).
-        ] ifFalse:[
-            s := self , (self species new:afterPeriod - n withAll:rightPadChar).
-        ].
+	"/ the number of after-decimalPoint characters
+	n := self size - idx.
+	rightPadChar isNil ifTrue:[
+	    s := self , (self species new:afterPeriod - n withAll:leftPadChar).
+	] ifFalse:[
+	    s := self , (self species new:afterPeriod - n withAll:rightPadChar).
+	].
     ].
 
     ^ s leftPaddedTo:size with:leftPadChar
@@ -2062,6 +2062,33 @@
      '       '     withoutSpaces   
      ('  foo' , Character tab asString , '    ') withoutSpaces inspect 
     "
+!
+
+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."
+
+    |index|
+
+    index := self size.
+    [index ~~ 0] whileTrue:[
+	(self at:index) isSeparator ifFalse:[
+	    ^ self copyTo:index
+	].
+	index := index - 1
+    ].
+    ^ ''
+
+    "
+     '    foo    ' withoutTrailingSeparators  
+     'foo    '     withoutTrailingSeparators   
+     '    foo'     withoutTrailingSeparators  
+     '       '     withoutTrailingSeparators   
+     'foo'         withoutTrailingSeparators   
+     ('  ' , Character tab asString , ' foo   ') withoutTrailingSeparators inspect 
+     ('   foo' , Character tab asString) withoutTrailingSeparators inspect 
+    "
 ! !
 
 !CharacterArray methodsFor:'substring searching'!
@@ -2459,5 +2486,5 @@
 !CharacterArray class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.48 1996-01-03 23:39:17 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.49 1996-01-30 17:14:23 cg Exp $'
 ! !