added #nextUpTo: - occasionally, you want to read the separator
authorClaus Gittinger <cg@exept.de>
Fri, 24 Jan 1997 14:10:21 +0100
changeset 2253 2ba9ca59f25d
parent 2252 ece20502eaf2
child 2254 5e3cb9e7e682
added #nextUpTo: - occasionally, you want to read the separator
Stream.st
--- a/Stream.st	Fri Jan 24 11:33:07 1997 +0100
+++ b/Stream.st	Fri Jan 24 14:10:21 1997 +0100
@@ -1012,6 +1012,53 @@
     "Modified: 15.5.1996 / 17:58:57 / cg"
 !
 
+nextUpTo:anObject
+    "read a collection of all objects up-to anObject and return these
+     elements, but excluding anObject. 
+     The next read operation will return anObject.
+     If anObject is not encountered, all elements up to the end are read
+     and returned, and the stream is positioned at the end.
+     Compare this with #upTo: which positions behind anObject"
+
+    |answerStream element|
+
+    answerStream := WriteStream on:(self contentsSpecies new).
+    [self atEnd] whileFalse:[
+        element := self peek.
+        (element = anObject) ifTrue: [
+            ^ answerStream contents
+        ].
+        answerStream nextPut:element.
+        self next.
+    ].
+    ^ answerStream contents
+
+    "
+     |s|
+     s := ReadStream on:#(1 2 3 4 5 6 7 8).
+     Transcript showCR:(s nextUpTo:4).  
+     Transcript showCR:s next
+
+     |s|
+     s := ReadStream on:#(1 2 3 4 5 6 7 8).
+     Transcript showCR:(s upTo:4).  
+     Transcript showCR:s next
+
+     |s|
+     s := ReadStream on:#(1 2 3 4 5 6 7 8).
+     Transcript showCR:(s nextUpTo:9).  
+     Transcript showCR:s next
+
+     |s|
+     s := ReadStream on:#(1 2 3 4 5 6 7 8).
+     Transcript showCR:(s upTo:9).  
+     Transcript showCR:s next
+    "
+
+    "Created: 24.1.1997 / 14:08:35 / cg"
+    "Modified: 24.1.1997 / 14:09:49 / cg"
+!
+
 skip:count 
     "skip count objects, return the receiver"
 
@@ -1574,6 +1621,6 @@
 !Stream class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Stream.st,v 1.53 1997-01-11 18:11:04 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Stream.st,v 1.54 1997-01-24 13:10:21 cg Exp $'
 ! !
 Stream initialize!