#REFACTORING by stefan
class: Stream
changed (tuning):
#throughAll:
#upToAllExcluding:
move methods sewnding #peek to PeekableStream
--- a/Stream.st Thu Dec 15 13:57:29 2016 +0100
+++ b/Stream.st Thu Dec 15 13:58:52 2016 +0100
@@ -3244,42 +3244,6 @@
"Modified: 11.1.1997 / 19:09:06 / cg"
!
-skipUntil:aBlock
- "skip all elements for which aBlock returns false.
- Return true if more elements can be read, false if eof has been reached."
-
- [self atEnd] whileFalse:[
- (aBlock value: self peek) ifTrue:[^ true].
- self next
- ].
- ^ false
-
- "
- #(1 2 3 4 5 6 7 8 9 10) readStream
- skipUntil:[:el | el >= 5];
- next
- "
-!
-
-skipWhile:aBlock
- "skip all elements for which aBlock returns true. Return true if more elements can be read,
- false if eof has been reached."
-
- [self atEnd] whileFalse:[
- (aBlock value: self peek) ifFalse:[^ true].
- self next
- ].
- ^ false
-
- "
- #(1 2 3 4 5 6 7 8 9 10) readStream
- skipWhile:[:el | el <= 5];
- next
- "
-
- "Created: / 23-09-2011 / 13:32:40 / cg"
-!
-
through:anObject
"read a collection of all objects up-to anObject and return these
elements, including anObject.
@@ -3330,16 +3294,15 @@
If aCollection is not encountered, all elements up to the end are read
and returned."
- |answerStream element last rslt|
+ |answerStream element last|
last := aCollection last.
answerStream := WriteStream on:(self contentsSpecies new).
- [self atEnd] whileFalse:[
- element := self next.
+ [(element := self nextOrNil) isNil and:[self atEnd]] whileFalse:[
answerStream nextPut:element.
element == last ifTrue:[
- ((rslt := answerStream contents) endsWith:aCollection) ifTrue:[
- ^ rslt
+ (answerStream endsWith:aCollection) ifTrue:[
+ ^ answerStream contents
]
].
].
@@ -3521,10 +3484,10 @@
last := aCollection last.
answerStream := ReadWriteStream on:(self contentsSpecies new).
- [(element := self nextOrNil) notNil] whileTrue:[
+ [(element := self nextOrNil) isNil and:[self atEnd]] whileFalse:[
answerStream nextPut:element.
element == last ifTrue:[
- (answerStream contents endsWith:aCollection) ifTrue:[
+ (answerStream endsWith:aCollection) ifTrue:[
|pos|
pos := answerStream position.
answerStream reset.
@@ -3545,99 +3508,6 @@
"Created: / 15.6.1998 / 19:11:31 / cg"
!
-upToAny:aCollectionOfObjects
- "read a collection of all objects up-to a element which is contained in
- aCollectionOfObjects and return these elements, but excluding the matching one.
- The next read operation will return the element AFTER anObject.
- If no such element is encountered, all elements up to the end are read
- and returned.
- Compare this with #throughAll: which also reads up to some object
- and also positions behind it, but DOES include it in the returned
- value."
-
- |result|
-
- result := self upToBeforeAny:aCollectionOfObjects.
- self atEnd ifFalse:[
- self next.
- ].
- ^ result
-
- "
- |s|
- s := ReadStream on:'hello world'.
- Transcript showCR:(s upToAny:(Array with:Character space)).
- Transcript showCR:(s upToEnd)
-
- 'Makefile' asFilename readStream upToAny:($A to:$Z)
- "
-
- "Created: / 30.8.1997 / 03:02:05 / cg"
- "Modified: / 11.1.1998 / 15:19:18 / cg"
-!
-
-upToBeforeAny:aCollectionOfObjects
- "read a collection of all objects up-to a element which is contained in
- aCollectionOfObjects and return these elements, but excluding the matching one.
- The next read operation will return the matching element.
- If no such element is encountered, all elements up to the end are read
- and returned.
- This returns the exact same as upToAny: would, but leaves the stream's position so that
- the next read returns the matching delimiter instead of skipping it.
- Caveat: this is the one which should have been called upTo: in the first place;
- however, it seems now too late for a change."
-
- |answerStream element|
-
- answerStream := WriteStream on:(self contentsSpecies new).
- [self atEnd] whileFalse:[
- element := self peek.
- (aCollectionOfObjects includes:element) ifTrue: [
- ^ answerStream contents
- ].
- answerStream nextPut:element.
- self next.
- ].
- ^ answerStream contents
-
- "
- |s|
- s := ReadStream on:'hello world'.
- Transcript showCR:(s upToBeforeAny:(Array with:Character space)).
- Transcript showCR:(s upToEnd)
-
- 'Make.proto' asFilename readStream upToBeforeAny:($A to:$Z)
- "
-
- "Created: / 30.8.1997 / 03:02:05 / cg"
- "Modified: / 11.1.1998 / 15:19:18 / cg"
-!
-
-upToElementForWhich:aBlock
- "read elements until aBlock returns true for an element.
- Return the collected elements excluding that element.
- Leave the stream positioned for the next read to return that element.
- If no element matches, all elements up to the end are returned"
-
- |answerStream next|
-
- answerStream := WriteStream on:(self contentsSpecies new).
-
- [
- self atEnd
- or:[ (aBlock value: (next := self peek)) ]
- ] whileFalse:[
- answerStream nextPut:next.
- self next.
- ].
- ^ answerStream contents
-
- "
- #(1 2 3 4 5 6 7 8 9 10) readStream
- upToElementForWhich:[:el | el > 5]
- "
-!
-
upToEnd
"return a collection of the elements up-to the end.
Return an empty collection, if the stream-end is already at the end."