added #throughAny:
authorClaus Gittinger <cg@exept.de>
Mon, 12 Jan 1998 14:13:13 +0100
changeset 3154 fe3af716a704
parent 3153 d8eed4e5c60f
child 3155 35befc38f4f1
added #throughAny:
Stream.st
--- a/Stream.st	Mon Jan 12 14:12:46 1998 +0100
+++ b/Stream.st	Mon Jan 12 14:13:13 1998 +0100
@@ -315,11 +315,11 @@
 
     signalAtEnd == true ifTrue:[
 	"/ always raise ...
-        ^ EndOfStreamSignal raiseIn:thisContext sender
+	^ EndOfStreamSignal raiseIn:thisContext sender
     ].
     signalAtEnd == false ifTrue:[
 	"/ never raise ...
-        ^ nil
+	^ nil
     ].
 
     ^ EndOfStreamQuerySignal raiseRequestFrom:self
@@ -360,16 +360,16 @@
     |data n|
 
     self binary ifTrue:[
-        data := ByteArray new:count
+	data := ByteArray new:count
     ] ifFalse:[
-        data := String new:count
+	data := String new:count
     ].
     n := self nextBytes:count into:data startingAt:1.
     n ~~ count ifTrue:[
-        n == 0 ifTrue:[
-            ^ nil
-        ].
-        data := data copyTo:n
+	n == 0 ifTrue:[
+	    ^ nil
+	].
+	data := data copyTo:n
     ].
 
     ^ data
@@ -405,8 +405,8 @@
      The receiver must support reading of binary bytes.
 
      Notice: this method is privided here for protocol completeness
-             with externalStreams - it is normally not used with other
-             streams."
+	     with externalStreams - it is normally not used with other
+	     streams."
 
     |n "{Class: SmallInteger }"
      dstIndex|
@@ -414,13 +414,13 @@
     dstIndex := initialIndex.
     n := 0.
     [self atEnd] whileFalse:[
-        n == numBytes ifTrue:[
-            ^ n
-        ].
+	n == numBytes ifTrue:[
+	    ^ n
+	].
 
-        aCollection byteAt:dstIndex put:(self nextByte).
-        dstIndex := dstIndex + 1.
-        n := n + 1.
+	aCollection byteAt:dstIndex put:(self nextByte).
+	dstIndex := dstIndex + 1.
+	n := n + 1.
     ].
     ^ n
 
@@ -448,14 +448,14 @@
     nR := numBytes.
     oR := initialIndex.
     [nR > 0] whileTrue:[
-        n := nR.
-        n > blockSize ifTrue:[n := blockSize].
-        n := self nextBytes:n into:aCollection startingAt:oR.
-        n == 0 ifTrue:[
-            ^ numBytes - nR
-        ].
-        oR := oR + n.
-        nR := nR - n
+	n := nR.
+	n > blockSize ifTrue:[n := blockSize].
+	n := self nextBytes:n into:aCollection startingAt:oR.
+	n == 0 ifTrue:[
+	    ^ numBytes - nR
+	].
+	oR := oR + n.
+	nR := nR - n
     ].
     ^ numBytes
 
@@ -840,8 +840,8 @@
 
     idx := start.
     1 to:count do:[:i |
-        self nextPutByte:(anObject byteAt:idx).
-        idx := idx + 1
+	self nextPutByte:(anObject byteAt:idx).
+	idx := idx + 1
     ].
     ^ count
 
@@ -882,13 +882,13 @@
     lo := (aNumber \\ 16r10000).
 
     msbFlag ifTrue:[
-        "high word first"
-        w1 := hi.
-        w2 := lo.
+	"high word first"
+	w1 := hi.
+	w2 := lo.
     ] ifFalse:[
-        "low word first"
-        w1 := lo.
-        w2 := hi.
+	"low word first"
+	w1 := lo.
+	w2 := hi.
     ].
     self nextPutShort:w1 MSB:false; nextPutShort:w2 MSB:false.
 
@@ -1338,6 +1338,42 @@
     "Modified: 17.5.1996 / 08:51:40 / cg"
 !
 
+throughAny:aCollection
+    "read & return a collection of all objects up-to and including 
+     an element contained in aCollection.
+     (i.e. read until any from aCollection is encountered.)
+     If no such character is encountered, all elements up to the end are read
+     and returned."
+
+    |answerStream element last|
+
+    last := aCollection last.
+    answerStream := WriteStream on:(self contentsSpecies new).
+    [self atEnd] whileFalse:[
+	element := self next.
+	answerStream nextPut:element.
+	(aCollection includes:element) ifTrue:[
+	    ^ answerStream contents
+	].
+    ].
+    ^ answerStream contents
+
+    "
+     |s|
+     s := ReadStream on:#(1 2 3 4 5 6 7 8).
+     Transcript showCR:(s throughAny:#(3 4 5)).  
+     Transcript showCR:s next
+
+     |s|
+     s := ReadStream on:'hello world, this is some text'.
+     Transcript showCR:(s throughAny:'wt').  
+     Transcript showCR:(s throughAny:'wt').  
+     Transcript showCR:s upToEnd.
+    "
+
+    "Modified: / 11.1.1998 / 15:28:04 / cg"
+!
+
 throughAll:aCollection
     "read & return a collection of all objects up-to and including 
      a subcollection given by aCollection.
@@ -1452,14 +1488,14 @@
     last := aCollection last.
     answerStream := WriteStream on:(self contentsSpecies new).
     [self atEnd] whileFalse:[
-        element := self next.
-        answerStream nextPut:element.
-        element == last ifTrue:[
-            ((rslt := answerStream contents) endsWith:aCollection) ifTrue:[
-                self position:(self position - aCollection size).
-                ^ rslt copyWithoutLast:aCollection size
-            ]
-        ].
+	element := self next.
+	answerStream nextPut:element.
+	element == last ifTrue:[
+	    ((rslt := answerStream contents) endsWith:aCollection) ifTrue:[
+		self position:(self position - aCollection size).
+		^ rslt copyWithoutLast:aCollection size
+	    ]
+	].
     ].
     ^ answerStream contents
 
@@ -1495,25 +1531,25 @@
 
     answerStream := WriteStream on:(self contentsSpecies new).
     [self atEnd] whileFalse:[
-        element := self next.
-        (aCollectionOfObjects includes:element) ifTrue: [
-            ^ answerStream contents
-        ].
-        answerStream nextPut:element.
+	element := self next.
+	(aCollectionOfObjects includes:element) ifTrue: [
+	    ^ answerStream contents
+	].
+	answerStream nextPut:element.
     ].
     ^ answerStream contents
 
     "
      |s|
      s := ReadStream on:'hello world'.
-     Transcript showCR:(s upToAll:(Array with:Character space)).
+     Transcript showCR:(s upToAny:(Array with:Character space)).
      Transcript showCR:(s upToEnd)
 
-     'Makefile' asFilename readStream upToAll:($A to:$Z)  
-"
+     'Makefile' asFilename readStream upToAny:($A to:$Z)  
+    "
 
-    "Modified: 26.2.1997 / 12:24:09 / cg"
-    "Created: 30.8.1997 / 03:02:05 / cg"
+    "Created: / 30.8.1997 / 03:02:05 / cg"
+    "Modified: / 11.1.1998 / 15:19:18 / cg"
 !
 
 upToEnd
@@ -1907,6 +1943,6 @@
 !Stream class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Stream.st,v 1.66 1997-11-15 14:59:17 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Stream.st,v 1.67 1998-01-12 13:13:13 cg Exp $'
 ! !
 Stream initialize!