SequenceableCollection.st
branchjv
changeset 23406 cffed6749990
parent 23208 3ec0d6cb2a7b
child 24907 373f37af2306
--- a/SequenceableCollection.st	Thu Oct 11 16:44:49 2018 +0200
+++ b/SequenceableCollection.st	Tue May 29 14:58:41 2018 +0200
@@ -2707,6 +2707,54 @@
     "Modified (format): / 24-01-2017 / 18:57:57 / stefan"
 !
 
+asCollectionOfSubCollectionsSeparatedByAnyForWhich:aBlock withSeparatorsIncluded:aBoolean
+    "return a collection containing the subCollection
+     (separated by elements for which aBlock evaluates to true) of the receiver.
+     This allows breaking up strings using an arbitrary condition.
+     withSeparatorsIncluded: will include the separator in the subCollection"
+
+    |words
+     start  "{ Class:SmallInteger }"
+     stop   "{ Class:SmallInteger }"
+     mySize "{ Class:SmallInteger }"|
+
+    words := self speciesForSubcollection new.
+    start := 1.
+    mySize := self size.
+    [start <= mySize] whileTrue:[
+        aBoolean ifTrue:[ 
+            stop := self findFirst:aBlock startingAt:(start + 1).     
+        ] ifFalse:[ 
+            "skip multiple separators"
+            [ aBlock value:(self at:start)] whileTrue:[
+                start := start + 1 .
+                start > mySize ifTrue:[
+                    ^ words
+                ]
+            ].
+            stop := self findFirst:aBlock startingAt:start.     
+        ].
+
+        stop == 0 ifTrue:[
+            words add:(self copyFrom:start to:mySize).
+            ^ words
+        ].
+        words add:(self copyFrom:start to:(stop - 1)).
+        start := stop
+    ].
+    ^ words
+
+    "
+     'helloWorldHereICome' asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch isUppercase ] withSeparatorsIncluded: false
+     'helloWorldHereICome' asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch isUppercase ] withSeparatorsIncluded: true
+
+     '1Tim2Jones3Iva4Tom'  asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch isDigit ] withSeparatorsIncluded: false
+     '1Tim2Jones3Iva4Tom'  asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch isDigit ] withSeparatorsIncluded: true
+    "
+
+    "Created: / 29-05-2018 / 13:58:54 / svestkap"
+!
+
 asSequenceableCollection
     "return myself as a SequenceableCollection.
      I am already a SequenceableCollection."
@@ -3427,6 +3475,25 @@
     "Modified (format): / 29-05-2018 / 13:24:06 / svestkap"
 !
 
+splitByAnyForWhich:aBlock withSeparatorIncluded:aBoolean
+    "return a collection containing the subCollection
+     (separated by elements for which aBlock evaluates to true) of the receiver.
+     This allows breaking up strings using an arbitrary condition.
+     withSeparatorsIncluded: will include the separator in the subCollection"    
+    
+    ^ self asCollectionOfSubCollectionsSeparatedByAnyForWhich:aBlock withSeparatorsIncluded:aBoolean
+
+    "
+     'helloWorldHereICome' asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch isUppercase ] withSeparatorsIncluded: false
+     'helloWorldHereICome' asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch isUppercase ] withSeparatorsIncluded: true
+
+     '1Tim2Jones3Iva4Tom'  asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch isDigit ] withSeparatorsIncluded: false
+     '1Tim2Jones3Iva4Tom'  asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch isDigit ] withSeparatorsIncluded: true
+    "
+
+    "Created: / 29-05-2018 / 14:01:45 / svestkap"
+!
+
 splitForSize:pieceSize
     "slice into pieces; return a collection containing pieces of size pieceSize from the receiver.
      The last piece may be smaller, if the receiver's size is not a multiple of pieceSize."