+asCollectionOfWordsDo:
authorClaus Gittinger <cg@exept.de>
Fri, 05 Dec 2003 14:04:48 +0100
changeset 7797 49e53638191f
parent 7796 29285c99ec71
child 7798 99e0e998a4e7
+asCollectionOfWordsDo:
CharacterArray.st
--- a/CharacterArray.st	Wed Dec 03 23:53:15 2003 +0100
+++ b/CharacterArray.st	Fri Dec 05 14:04:48 2003 +0100
@@ -3530,27 +3530,10 @@
      of the receiver. Multiple occurences of whitespace characters will
      be treated like one - i.e. whitespace is skipped."
 
-    |words
-     start  "{ Class:SmallInteger }" 
-     stop   "{ Class:SmallInteger }" 
-     mySize "{ Class:SmallInteger }"|
+    |words|
 
     words := OrderedCollection new.
-    start := 1.
-    mySize := self size.
-    [start <= mySize] whileTrue:[
-	start := self indexOfNonSeparatorStartingAt:start.
-	start == 0 ifTrue:[
-	    ^ words
-	].
-	stop := self indexOfSeparatorStartingAt:start.
-	stop == 0 ifTrue:[
-	    words add:(self copyFrom:start to:mySize).
-	    ^ words
-	].
-	words add:(self copyFrom:start to:(stop - 1)).
-	start := stop
-    ].
+    self asCollectionOfWordsDo:[:w | words add:w].
     ^ words
 
     "
@@ -3562,6 +3545,45 @@
     "
 !
 
+asCollectionOfWordsDo:aBlock
+    "evaluate aBlock for each word (separated by whitespace) of the receiver. 
+     Multiple occurences of whitespace characters will be treated like one 
+     - i.e. whitespace is skipped.
+     Returns the number of words (i.e. the number of invocations of aBlock)."
+
+    |count  "{ Class:SmallInteger }"
+     start  "{ Class:SmallInteger }" 
+     stop   "{ Class:SmallInteger }" 
+     mySize "{ Class:SmallInteger }"|
+
+    count := 0.
+    start := 1.
+    mySize := self size.
+    [start <= mySize] whileTrue:[
+        start := self indexOfNonSeparatorStartingAt:start.
+        start == 0 ifTrue:[
+            ^ count
+        ].
+        stop := self indexOfSeparatorStartingAt:start.
+        stop == 0 ifTrue:[
+            aBlock value:(self copyFrom:start to:mySize).
+            ^ count + 1
+        ].
+        aBlock value:(self copyFrom:start to:(stop - 1)).
+        start := stop.
+        count := count + 1
+    ].
+    ^ count
+
+    "
+     'hello world isnt this nice' asCollectionOfWordsDo:[:w | Transcript showCR:w]
+     '    hello    world   isnt   this   nice  ' asCollectionOfWordsDo:[:w | Transcript showCR:w]
+     'hello' asCollectionOfWordsDo:[:w | Transcript showCR:w]
+     '' asCollectionOfWordsDo:[:w | Transcript showCR:w]
+     '      ' asCollectionOfWordsDo:[:w | Transcript showCR:w]
+    "
+!
+
 asComposedText
     "ST-80 compatibility 
      - ST/X does not (as today) support composedTexts."
@@ -6564,7 +6586,7 @@
 !CharacterArray class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.263 2003-11-25 17:04:23 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.264 2003-12-05 13:04:48 cg Exp $'
 ! !
 
 CharacterArray initialize!