added a skipThroughAll:, which does not depend on being positionable.
authorClaus Gittinger <cg@exept.de>
Sat, 11 Jan 1997 19:11:04 +0100
changeset 2152 6a42beaf7cc5
parent 2151 5f1fd65f8c3b
child 2153 244e36cbbd9b
added a skipThroughAll:, which does not depend on being positionable.
Stream.st
--- a/Stream.st	Sat Jan 11 18:54:18 1997 +0100
+++ b/Stream.st	Sat Jan 11 19:11:04 1997 +0100
@@ -388,19 +388,44 @@
 !
 
 nextBytes:numBytes into:aCollection startingAt:initialIndex
-    "return the next numBytes from the stream.
-     The receiver must support reading of binary bytes."
+    "return the next numBytes from the stream. If the end is
+     reached before, only that many bytes are copyied into the
+     collection.
+     Returns the number of bytes that have been actually read.
+     The receiver must support reading of binary bytes.
 
-    |dstIndex|
+     Notice: this method is privided here for protocol completeness
+             with externalStreams - it is normally not used with other
+             streams."
+
+    |n "{Class: SmallInteger }"
+     dstIndex|
 
     dstIndex := initialIndex.
-    1 to:numBytes do:[:index |
+    n := 0.
+    [self atEnd] whileFalse:[
+        n == numBytes ifTrue:[
+            ^ n
+        ].
+
         aCollection at:dstIndex put:(self nextByte).
         dstIndex := dstIndex + 1.
+        n := n + 1.
     ].
-    ^ numBytes
+    ^ n
+
+    "
+     |s n buffer|
+
+     buffer := ByteArray new:10.
 
-    "Created: 13.9.1996 / 18:12:47 / cg"
+     s := ReadStream on:#[1 2 3 4 5 6 7 8 9].
+     s next:3.
+     n := s nextBytes:9 into:buffer startingAt:1.
+     Transcript showCR:('n = %1; buffer = <%2>' bindWith:n with:buffer)
+    "
+
+    "Modified: 11.1.1997 / 17:42:01 / cg"
 !
 
 nextLine
@@ -1047,6 +1072,55 @@
     "
 !
 
+skipThroughAll:aCollection
+    "skip for and through the sequence given by the argument, aCollection;
+     return nil if not found, the receiver otherwise. 
+     On a successful match, the next read will return elements after aCollection;
+     if no match was found, the receiver will be positioned at the end."
+
+    |buffer l first idx|
+
+    l := aCollection size.
+    first := aCollection at:1.
+    [self atEnd] whileFalse:[
+        buffer isNil ifTrue:[
+            buffer := self nextAvailable:l.
+        ].
+        buffer = aCollection ifTrue:[
+            ^ self
+        ].
+        idx := buffer indexOf:first startingAt:2.
+        idx == 0 ifTrue:[
+            buffer := nil
+        ] ifFalse:[
+            buffer := (buffer copyFrom:idx) , (self nextAvailable:(idx - 1))
+        ]
+    ].
+    ^ nil
+
+    "
+     |s|
+     s := ReadStream on:'12345678901234567890'.
+     s skipThroughAll:'901'.
+     s upToEnd                    
+    "
+    "
+     |s|
+     s := ReadStream on:'12345678901234567890'.
+     s skipThroughAll:'1234'.
+     s upToEnd                    
+    "
+    "
+     |s|
+     s := ReadStream on:'12345678901234567890'.
+     s skipThroughAll:'999'.
+     s atEnd                    
+    "
+
+    "Created: 11.1.1997 / 18:55:13 / cg"
+    "Modified: 11.1.1997 / 19:09:06 / cg"
+!
+
 through:anObject
     "read a collection of all objects up-to anObject and return these
      elements, including anObject. 
@@ -1500,6 +1574,6 @@
 !Stream class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Stream.st,v 1.52 1996-11-08 22:54:02 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Stream.st,v 1.53 1997-01-11 18:11:04 cg Exp $'
 ! !
 Stream initialize!