class: CharacterArray
authorClaus Gittinger <cg@exept.de>
Thu, 28 Mar 2013 17:40:50 +0100
changeset 14992 ee87364e3bf5
parent 14991 92336b82bdb1
child 14993 bbabce626acf
class: CharacterArray added: #withSeparatorsCompacted comment/format in: #trimBlanks
CharacterArray.st
--- a/CharacterArray.st	Thu Mar 28 16:55:44 2013 +0100
+++ b/CharacterArray.st	Thu Mar 28 17:40:50 2013 +0100
@@ -858,8 +858,7 @@
 !
 
 trimBlanks
-    "return a copy of the receiver without leading
-     and trailing spaces.
+    "return a copy of the receiver without leading and trailing spaces.
      This is an ST/V compatibility method."
 
     ^ self withoutSpaces
@@ -5339,6 +5338,44 @@
     "Modified: 2.4.1997 / 18:13:04 / cg"
 !
 
+withSeparatorsCompacted
+    "return a new string with each sequence of whiteSpace replaced by a single space character.
+     Preserves a leading/trailing space."
+
+    ^ self species streamContents:[:s |
+        |skipping|
+
+        skipping := false.
+        1 to:self size do:[:idx |
+            |char|
+
+            char := self at:idx.
+            skipping ifTrue:[
+                char isSeparator ifFalse:[
+                    s nextPut:char.
+                    skipping := false.
+                ]
+            ] ifFalse:[
+                char isSeparator ifTrue:[
+                    s nextPut:(Character space).
+                    skipping := true
+                ] ifFalse:[
+                    s nextPut:char
+                ].
+            ]
+        ]
+    ]
+
+    "
+     'hello wwww'         withSeparatorsCompacted  
+     'hello    wwww'      withSeparatorsCompacted  
+     '  hello wwww'       withSeparatorsCompacted  
+     '  hello wwww   '    withSeparatorsCompacted  
+     '  hello    wwww   ' withSeparatorsCompacted  
+     'hel   lo www   w'   withSeparatorsCompacted  
+    "
+!
+
 withTabs
     "return a string consisting of the receivers characters
      where leading spaces are replaced by tabulator characters (assuming 8-col tabs).
@@ -6306,11 +6343,11 @@
 !CharacterArray class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.495 2013-03-13 14:04:42 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.496 2013-03-28 16:40:50 cg Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.495 2013-03-13 14:04:42 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.496 2013-03-28 16:40:50 cg Exp $'
 ! !