SequenceableCollection.st
changeset 3764 ab05f8d25063
parent 3738 73ae3988bbe2
child 3887 c3c86dcca908
--- a/SequenceableCollection.st	Fri Aug 14 18:19:03 1998 +0200
+++ b/SequenceableCollection.st	Sat Aug 15 10:43:35 1998 +0200
@@ -271,6 +271,50 @@
     "
 
     "Modified: 11.5.1996 / 11:14:05 / cg"
+!
+
+upTo:anElement count:count
+    "return a new collection consisting of the receivers elements upTo
+     (but excluding) count found anElements, i.e. the first found count-1 
+     elements are included in the returned collection; if count < 0 the 
+     procedure is done from the end of the collection backwards.
+     If anElement is not in the receiver, the returned collection
+     will consist of all elements of the receiver"
+
+    |startPos endPos pos found|
+
+    (count == 0 or: [count == 1]) ifTrue:[
+        ^ self upTo:anElement
+    ].
+
+    startPos := 1.
+    endPos   := self size.
+    found    := 0.
+
+    count > 1 ifTrue:[
+        pos := 0.
+        [pos < self size and: [found < count]]
+            whileTrue: [pos := pos + 1. (self at: pos) == anElement ifTrue: [found := found + 1]].
+        found == count ifTrue:[
+            endPos   := pos - 1
+        ]
+    ].     
+
+    count < 0 ifTrue:[
+        pos := self size + 1.
+        [pos > 1 and: [found < count abs]]
+            whileTrue: [pos := pos - 1. (self at: pos) == anElement ifTrue: [found := found + 1]].
+        found == count abs ifTrue:[
+            startPos := pos + 1
+        ]
+    ].    
+
+    ^ self copyFrom: startPos to: endPos
+
+    "
+     'hello1_hello2_hello3' upTo:$_ count:  2     returns first two 'hellos' 
+     'hello1_hello2_hello3' upTo:$_ count: -1     returns last 'hello'
+    "
 ! !
 
 !SequenceableCollection methodsFor:'adding & removing'!
@@ -3997,5 +4041,5 @@
 !SequenceableCollection class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.99 1998-08-06 19:30:41 tz Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.100 1998-08-15 08:43:35 tz Exp $'
 ! !