#TUNING by stefan
class: Stream
comment/format in:
#throughAny:
#upTo:
#upTo:into:
changed:
#nextLine (send #== instead of #=)
#through:
#throughElementForWhich:
#upToAllExcluding:
#upToEnd
Avoid #atEnd (use #nextOrNil)
--- a/Stream.st Tue Oct 22 20:26:38 2019 +0200
+++ b/Stream.st Tue Oct 22 20:27:37 2019 +0200
@@ -222,11 +222,6 @@
^ self == Stream
! !
-!Stream methodsFor:'*petitparser-core-converting'!
-
-asPetitStream
- ^ self contents asPetitStream
-! !
!Stream methodsFor:'Compatibility-Dolphin'!
@@ -3296,8 +3291,7 @@
|answerStream element|
answerStream := self contentsSpecies writeStream.
- [self atEnd] whileFalse:[
- element := self next.
+ [(element := self nextOrNil) isNil or:[self atEnd]] whileFalse:[
answerStream nextPut:element.
(element = anObject) ifTrue: [
^ answerStream contents
@@ -3324,6 +3318,7 @@
"Modified: / 17-05-1996 / 08:51:40 / cg"
"Modified: / 10-01-2018 / 18:30:17 / stefan"
+ "Modified: / 22-10-2019 / 18:52:28 / Stefan Vogel"
!
throughAll:aCollection
@@ -3380,8 +3375,7 @@
|answerStream element|
answerStream := self contentsSpecies writeStream.
- [self atEnd] whileFalse:[
- element := self next.
+ [(element := self nextOrNil) isNil and:[self atEnd]] whileFalse:[
answerStream nextPut:element.
(aCollection includes:element) ifTrue:[
^ answerStream contents
@@ -3391,7 +3385,7 @@
"
|s|
- s := ReadStream on:#(1 2 3 4 5 6 7 8).
+ s := ReadStream on:#(1 nil 2 3 4 5 6 7 8).
Transcript showCR:(s throughAny:#(3 4 5)).
Transcript showCR:s next
@@ -3404,6 +3398,7 @@
"Modified: / 11-01-1998 / 15:28:04 / cg"
"Modified: / 10-01-2018 / 18:30:25 / stefan"
+ "Modified (comment): / 22-10-2019 / 19:15:20 / Stefan Vogel"
!
throughElementForWhich:aBlock
@@ -3415,8 +3410,7 @@
answerStream := self contentsSpecies writeStream.
- [self atEnd] whileFalse:[
- element := self next.
+ [(element := self nextOrNil) isNil and:[self atEnd]] whileFalse:[
answerStream nextPut:element.
(aBlock value:element) ifTrue: [
^ answerStream contents
@@ -3426,10 +3420,11 @@
"
#(1 2 3 4 5 6 7 8 9 10) readStream
- throughElementForWhich:[:el | el > 5];
+ throughElementForWhich:[:el | el > 5].
"
"Modified: / 10-01-2018 / 18:30:30 / stefan"
+ "Modified (comment): / 22-10-2019 / 19:13:58 / Stefan Vogel"
!
upTo:anObject
@@ -3453,7 +3448,7 @@
"
|s|
- s := '12345678901234567890' readStream.
+ s := '12345678901234567890' asArray readStream.
s readLimit:5.
s upTo:$3.
self assert:(s position = 3).
@@ -3502,12 +3497,12 @@
(ReadStream on:'line 1
line 2') upTo:Character cr
- 'Makefile' asFilename readStream upTo:Character cr;upTo:Character cr
+ 'Make.proto' asFilename readStream upTo:Character cr; upTo:Character cr
"
"Modified: / 12-01-1998 / 21:58:38 / cg"
"Modified: / 10-01-2018 / 18:30:36 / stefan"
- "Modified (comment): / 24-09-2019 / 14:35:36 / Stefan Vogel"
+ "Modified (comment): / 22-10-2019 / 19:41:51 / Stefan Vogel"
!
upTo:separatingObject into:aStream
@@ -3525,23 +3520,39 @@
separatingObject isImmediate ifTrue:[
"speed uo for anObject being an Integer or a Character <= 255
- identity compare is inlined and a lot faster"
- [self atEnd not and:[(element := self next) ~~ separatingObject ]] whileTrue:[
+ [((element := self nextOrNil) notNil or:[self atEnd not])
+ and:[element ~~ separatingObject]] whileTrue:[
aStream nextPut:element.
].
] ifFalse:[
- [self atEnd not and:[(element := self next) ~= separatingObject ]] whileTrue:[
+ [((element := self nextOrNil) notNil or:[self atEnd not])
+ and:[element ~= separatingObject]] whileTrue:[
aStream nextPut:element.
].
].
"
- |s|
- s := ReadStream on:'hello world world'.
- Transcript show:'<'; show:(s upTo:$w); showCR:'>'.
- Transcript show:'<'; show:(s upToEnd); showCR:'>'.
+ |s w|
+ w := #() writeStream.
+ s := ReadStream on:#(1 2 3 4 5 'bla' 6 nil 7 8 9 theEnd).
+ s upTo:2 into:w.
+ w contents.
+ w reset. s reset.
+
+ s upTo:'bla' into:w.
+ w contents.
+ w reset. s reset.
+
+ s upTo:nil into:w.
+ w contents.
+ w reset. s reset.
+
+ s upTo:#nothing into:w.
+ w contents.
+ w reset. s reset.
"
- "Modified: / 30-09-2019 / 14:00:13 / Stefan Vogel"
+ "Modified (comment): / 22-10-2019 / 19:09:12 / Stefan Vogel"
!
upToAllExcluding:aCollection
@@ -3559,8 +3570,7 @@
last := aCollection last.
answerStream := ReadWriteStream on:(self contentsSpecies new).
- [self atEnd] whileFalse:[
- element := self next.
+ [(element := self nextOrNil) isNil and:[self atEnd]] whileFalse:[
answerStream nextPut:element.
element = last ifTrue:[
(answerStream endsWith:aCollection) ifTrue:[
@@ -3589,17 +3599,18 @@
"Created: / 15-06-1998 / 19:11:31 / cg"
"Modified: / 10-01-2018 / 23:48:49 / stefan"
+ "Modified: / 22-10-2019 / 19:12:06 / Stefan Vogel"
!
upToEnd
"return a collection of the elements up-to the end.
Return an empty collection, if the stream-end is already at the end."
- |answerStream|
+ |answerStream element|
answerStream := self contentsSpecies writeStream.
- [self atEnd] whileFalse:[
- answerStream nextPut:self next.
+ [(element := self nextOrNil) isNil and:[self atEnd]] whileFalse:[
+ answerStream nextPut:element.
].
^ answerStream contents
@@ -3612,6 +3623,7 @@
"Modified: / 15-05-1996 / 18:00:39 / cg"
"Modified (format): / 10-01-2018 / 18:30:53 / stefan"
+ "Modified: / 22-10-2019 / 19:12:46 / Stefan Vogel"
! !
!Stream methodsFor:'reading-numbers'!
@@ -3712,13 +3724,14 @@
].
answerStream := self contentsSpecies writeStream:80.
self upTo:Character cr into:answerStream.
- (answerStream size ~~ 0 and:[answerStream last = Character return]) ifTrue:[
+ (answerStream size ~~ 0 and:[answerStream last == Character return]) ifTrue:[
answerStream backStep.
].
^ answerStream contents
"Modified: / 19-05-1998 / 17:26:25 / cg"
"Modified: / 10-01-2018 / 18:35:11 / stefan"
+ "Modified: / 22-10-2019 / 19:37:56 / Stefan Vogel"
! !