Stream.st
changeset 24858 d13988bf2f1e
parent 24841 10cdb3672d2b
child 24862 1129d5b1f9cb
equal deleted inserted replaced
24857:7929c3e8bb69 24858:d13988bf2f1e
   220 
   220 
   221 isAbstract
   221 isAbstract
   222     ^ self == Stream
   222     ^ self == Stream
   223 ! !
   223 ! !
   224 
   224 
   225 !Stream methodsFor:'*petitparser-core-converting'!
       
   226 
       
   227 asPetitStream
       
   228 	^ self contents asPetitStream
       
   229 ! !
       
   230 
   225 
   231 !Stream methodsFor:'Compatibility-Dolphin'!
   226 !Stream methodsFor:'Compatibility-Dolphin'!
   232 
   227 
   233 display:someObject
   228 display:someObject
   234     "dolphin compatibility"
   229     "dolphin compatibility"
  3294      value."
  3289      value."
  3295 
  3290 
  3296     |answerStream element|
  3291     |answerStream element|
  3297 
  3292 
  3298     answerStream := self contentsSpecies writeStream.
  3293     answerStream := self contentsSpecies writeStream.
  3299     [self atEnd] whileFalse:[
  3294     [(element := self nextOrNil) isNil or:[self atEnd]] whileFalse:[
  3300         element := self next.
       
  3301         answerStream nextPut:element.
  3295         answerStream nextPut:element.
  3302         (element = anObject) ifTrue: [
  3296         (element = anObject) ifTrue: [
  3303             ^ answerStream contents
  3297             ^ answerStream contents
  3304         ]
  3298         ]
  3305     ].
  3299     ].
  3322      Transcript showCR:(s upToEnd)
  3316      Transcript showCR:(s upToEnd)
  3323     "
  3317     "
  3324 
  3318 
  3325     "Modified: / 17-05-1996 / 08:51:40 / cg"
  3319     "Modified: / 17-05-1996 / 08:51:40 / cg"
  3326     "Modified: / 10-01-2018 / 18:30:17 / stefan"
  3320     "Modified: / 10-01-2018 / 18:30:17 / stefan"
       
  3321     "Modified: / 22-10-2019 / 18:52:28 / Stefan Vogel"
  3327 !
  3322 !
  3328 
  3323 
  3329 throughAll:aCollection
  3324 throughAll:aCollection
  3330     "read & return a collection of all objects up-to and including
  3325     "read & return a collection of all objects up-to and including
  3331      a subcollection given by aCollection.
  3326      a subcollection given by aCollection.
  3378      and returned."
  3373      and returned."
  3379 
  3374 
  3380     |answerStream element|
  3375     |answerStream element|
  3381 
  3376 
  3382     answerStream := self contentsSpecies writeStream.
  3377     answerStream := self contentsSpecies writeStream.
  3383     [self atEnd] whileFalse:[
  3378     [(element := self nextOrNil) isNil and:[self atEnd]] whileFalse:[
  3384         element := self next.
       
  3385         answerStream nextPut:element.
  3379         answerStream nextPut:element.
  3386         (aCollection includes:element) ifTrue:[
  3380         (aCollection includes:element) ifTrue:[
  3387             ^ answerStream contents
  3381             ^ answerStream contents
  3388         ].
  3382         ].
  3389     ].
  3383     ].
  3390     ^ answerStream contents
  3384     ^ answerStream contents
  3391 
  3385 
  3392     "
  3386     "
  3393      |s|
  3387      |s|
  3394      s := ReadStream on:#(1 2 3 4 5 6 7 8).
  3388      s := ReadStream on:#(1 nil 2 3 4 5 6 7 8).
  3395      Transcript showCR:(s throughAny:#(3 4 5)).
  3389      Transcript showCR:(s throughAny:#(3 4 5)).
  3396      Transcript showCR:s next
  3390      Transcript showCR:s next
  3397 
  3391 
  3398      |s|
  3392      |s|
  3399      s := ReadStream on:'hello world, this is some text'.
  3393      s := ReadStream on:'hello world, this is some text'.
  3402      Transcript showCR:s upToEnd.
  3396      Transcript showCR:s upToEnd.
  3403     "
  3397     "
  3404 
  3398 
  3405     "Modified: / 11-01-1998 / 15:28:04 / cg"
  3399     "Modified: / 11-01-1998 / 15:28:04 / cg"
  3406     "Modified: / 10-01-2018 / 18:30:25 / stefan"
  3400     "Modified: / 10-01-2018 / 18:30:25 / stefan"
       
  3401     "Modified (comment): / 22-10-2019 / 19:15:20 / Stefan Vogel"
  3407 !
  3402 !
  3408 
  3403 
  3409 throughElementForWhich:aBlock
  3404 throughElementForWhich:aBlock
  3410     "read elements until aBlock returns true for an element.
  3405     "read elements until aBlock returns true for an element.
  3411      Return the collected elements including that element.
  3406      Return the collected elements including that element.
  3413 
  3408 
  3414     |answerStream element|
  3409     |answerStream element|
  3415 
  3410 
  3416     answerStream := self contentsSpecies writeStream.
  3411     answerStream := self contentsSpecies writeStream.
  3417 
  3412 
  3418     [self atEnd] whileFalse:[
  3413     [(element := self nextOrNil) isNil and:[self atEnd]] whileFalse:[
  3419         element := self next.
       
  3420         answerStream nextPut:element.
  3414         answerStream nextPut:element.
  3421         (aBlock value:element) ifTrue: [
  3415         (aBlock value:element) ifTrue: [
  3422             ^ answerStream contents
  3416             ^ answerStream contents
  3423         ]
  3417         ]
  3424     ].
  3418     ].
  3425     ^ answerStream contents
  3419     ^ answerStream contents
  3426 
  3420 
  3427     "
  3421     "
  3428      #(1 2 3 4 5 6 7 8 9 10) readStream
  3422      #(1 2 3 4 5 6 7 8 9 10) readStream
  3429         throughElementForWhich:[:el | el > 5];
  3423         throughElementForWhich:[:el | el > 5].
  3430     "
  3424     "
  3431 
  3425 
  3432     "Modified: / 10-01-2018 / 18:30:30 / stefan"
  3426     "Modified: / 10-01-2018 / 18:30:30 / stefan"
       
  3427     "Modified (comment): / 22-10-2019 / 19:13:58 / Stefan Vogel"
  3433 !
  3428 !
  3434 
  3429 
  3435 upTo:anObject
  3430 upTo:anObject
  3436     "read a collection of all objects up-to anObject and return these
  3431     "read a collection of all objects up-to anObject and return these
  3437      elements, but excluding anObject.
  3432      elements, but excluding anObject.
  3451     self upTo:anObject into:answerStream.
  3446     self upTo:anObject into:answerStream.
  3452     ^ answerStream contents
  3447     ^ answerStream contents
  3453 
  3448 
  3454     "
  3449     "
  3455      |s|
  3450      |s|
  3456      s := '12345678901234567890' readStream.
  3451      s := '12345678901234567890' asArray readStream.
  3457      s readLimit:5.
  3452      s readLimit:5.
  3458      s upTo:$3. 
  3453      s upTo:$3. 
  3459      self assert:(s position = 3).
  3454      self assert:(s position = 3).
  3460      s upTo:$x.    
  3455      s upTo:$x.    
  3461      self assert:(s position = 5).     
  3456      self assert:(s position = 5).     
  3500      (ReadStream on:#(1 2 3 4 5 6)) upTo:4
  3495      (ReadStream on:#(1 2 3 4 5 6)) upTo:4
  3501 
  3496 
  3502      (ReadStream on:'line 1
  3497      (ReadStream on:'line 1
  3503                      line 2') upTo:Character cr
  3498                      line 2') upTo:Character cr
  3504 
  3499 
  3505      'Makefile' asFilename readStream upTo:Character cr;upTo:Character cr
  3500      'Make.proto' asFilename readStream upTo:Character cr; upTo:Character cr
  3506     "
  3501     "
  3507 
  3502 
  3508     "Modified: / 12-01-1998 / 21:58:38 / cg"
  3503     "Modified: / 12-01-1998 / 21:58:38 / cg"
  3509     "Modified: / 10-01-2018 / 18:30:36 / stefan"
  3504     "Modified: / 10-01-2018 / 18:30:36 / stefan"
  3510     "Modified (comment): / 24-09-2019 / 14:35:36 / Stefan Vogel"
  3505     "Modified (comment): / 22-10-2019 / 19:41:51 / Stefan Vogel"
  3511 !
  3506 !
  3512 
  3507 
  3513 upTo:separatingObject into:aStream
  3508 upTo:separatingObject into:aStream
  3514     "read a collection of all objects up-to separatingObject and append these
  3509     "read a collection of all objects up-to separatingObject and append these
  3515      elements to aStream, but excluding anObject. 
  3510      elements to aStream, but excluding anObject. 
  3523     |element|
  3518     |element|
  3524 
  3519 
  3525     separatingObject isImmediate ifTrue:[
  3520     separatingObject isImmediate ifTrue:[
  3526         "speed uo for anObject being an Integer or a Character <= 255
  3521         "speed uo for anObject being an Integer or a Character <= 255
  3527          - identity compare is inlined and a lot faster"
  3522          - identity compare is inlined and a lot faster"
  3528         [self atEnd not and:[(element := self next) ~~ separatingObject ]] whileTrue:[
  3523         [((element := self nextOrNil) notNil or:[self atEnd not]) 
       
  3524          and:[element ~~ separatingObject]] whileTrue:[
  3529             aStream nextPut:element.
  3525             aStream nextPut:element.
  3530         ].
  3526         ].
  3531     ] ifFalse:[
  3527     ] ifFalse:[
  3532         [self atEnd not and:[(element := self next) ~= separatingObject ]] whileTrue:[
  3528         [((element := self nextOrNil) notNil or:[self atEnd not]) 
       
  3529          and:[element ~= separatingObject]] whileTrue:[
  3533             aStream nextPut:element.
  3530             aStream nextPut:element.
  3534         ].
  3531         ].
  3535     ].
  3532     ].
  3536 
  3533 
  3537     "
  3534     "
  3538      |s|
  3535      |s w|
  3539      s := ReadStream on:'hello world world'.
  3536      w := #() writeStream.
  3540      Transcript show:'<'; show:(s upTo:$w); showCR:'>'.
  3537      s := ReadStream on:#(1 2 3 4 5 'bla' 6 nil 7 8 9 theEnd).
  3541      Transcript show:'<'; show:(s upToEnd); showCR:'>'.
  3538      s upTo:2 into:w.
  3542     "
  3539      w contents.
  3543 
  3540      w reset. s reset.
  3544     "Modified: / 30-09-2019 / 14:00:13 / Stefan Vogel"
  3541 
       
  3542      s upTo:'bla' into:w.
       
  3543      w contents.
       
  3544      w reset. s reset.
       
  3545 
       
  3546      s upTo:nil into:w.
       
  3547      w contents.
       
  3548      w reset. s reset.
       
  3549 
       
  3550      s upTo:#nothing into:w.
       
  3551      w contents.
       
  3552      w reset. s reset.
       
  3553     "
       
  3554 
       
  3555     "Modified (comment): / 22-10-2019 / 19:09:12 / Stefan Vogel"
  3545 !
  3556 !
  3546 
  3557 
  3547 upToAllExcluding:aCollection
  3558 upToAllExcluding:aCollection
  3548     "read a collection of all objects up-to a sequence of elements given in aCollection,
  3559     "read a collection of all objects up-to a sequence of elements given in aCollection,
  3549      and return these elements, but excluding the matching ones.
  3560      and return these elements, but excluding the matching ones.
  3557 
  3568 
  3558     |answerStream element last|
  3569     |answerStream element last|
  3559 
  3570 
  3560     last := aCollection last.
  3571     last := aCollection last.
  3561     answerStream := ReadWriteStream on:(self contentsSpecies new).
  3572     answerStream := ReadWriteStream on:(self contentsSpecies new).
  3562     [self atEnd] whileFalse:[
  3573     [(element := self nextOrNil) isNil and:[self atEnd]] whileFalse:[
  3563         element := self next.
       
  3564         answerStream nextPut:element.
  3574         answerStream nextPut:element.
  3565         element = last ifTrue:[
  3575         element = last ifTrue:[
  3566             (answerStream endsWith:aCollection) ifTrue:[
  3576             (answerStream endsWith:aCollection) ifTrue:[
  3567                 |pos|
  3577                 |pos|
  3568                 pos := answerStream position.
  3578                 pos := answerStream position.
  3587      Transcript show:'<'; show:(s upToEnd); showCR:'>'.
  3597      Transcript show:'<'; show:(s upToEnd); showCR:'>'.
  3588     "
  3598     "
  3589 
  3599 
  3590     "Created: / 15-06-1998 / 19:11:31 / cg"
  3600     "Created: / 15-06-1998 / 19:11:31 / cg"
  3591     "Modified: / 10-01-2018 / 23:48:49 / stefan"
  3601     "Modified: / 10-01-2018 / 23:48:49 / stefan"
       
  3602     "Modified: / 22-10-2019 / 19:12:06 / Stefan Vogel"
  3592 !
  3603 !
  3593 
  3604 
  3594 upToEnd
  3605 upToEnd
  3595     "return a collection of the elements up-to the end.
  3606     "return a collection of the elements up-to the end.
  3596      Return an empty collection, if the stream-end is already at the end."
  3607      Return an empty collection, if the stream-end is already at the end."
  3597 
  3608 
  3598     |answerStream|
  3609     |answerStream element|
  3599 
  3610 
  3600     answerStream := self contentsSpecies writeStream.
  3611     answerStream := self contentsSpecies writeStream.
  3601     [self atEnd] whileFalse:[
  3612     [(element := self nextOrNil) isNil and:[self atEnd]] whileFalse:[
  3602         answerStream nextPut:self next.
  3613         answerStream nextPut:element.
  3603     ].
  3614     ].
  3604     ^ answerStream contents
  3615     ^ answerStream contents
  3605 
  3616 
  3606     "
  3617     "
  3607      (ReadStream on:'1234567890') upToEnd
  3618      (ReadStream on:'1234567890') upToEnd
  3610      ('12' readStream) next; next; upToEnd
  3621      ('12' readStream) next; next; upToEnd
  3611     "
  3622     "
  3612 
  3623 
  3613     "Modified: / 15-05-1996 / 18:00:39 / cg"
  3624     "Modified: / 15-05-1996 / 18:00:39 / cg"
  3614     "Modified (format): / 10-01-2018 / 18:30:53 / stefan"
  3625     "Modified (format): / 10-01-2018 / 18:30:53 / stefan"
       
  3626     "Modified: / 22-10-2019 / 19:12:46 / Stefan Vogel"
  3615 ! !
  3627 ! !
  3616 
  3628 
  3617 !Stream methodsFor:'reading-numbers'!
  3629 !Stream methodsFor:'reading-numbers'!
  3618 
  3630 
  3619 nextDecimalInteger
  3631 nextDecimalInteger
  3710     self atEnd ifTrue:[
  3722     self atEnd ifTrue:[
  3711         ^ self pastEndRead
  3723         ^ self pastEndRead
  3712     ].
  3724     ].
  3713     answerStream := self contentsSpecies writeStream:80.
  3725     answerStream := self contentsSpecies writeStream:80.
  3714     self upTo:Character cr into:answerStream.
  3726     self upTo:Character cr into:answerStream.
  3715     (answerStream size ~~ 0 and:[answerStream last = Character return]) ifTrue:[
  3727     (answerStream size ~~ 0 and:[answerStream last == Character return]) ifTrue:[
  3716         answerStream backStep.
  3728         answerStream backStep.
  3717     ].
  3729     ].
  3718     ^ answerStream contents
  3730     ^ answerStream contents
  3719 
  3731 
  3720     "Modified: / 19-05-1998 / 17:26:25 / cg"
  3732     "Modified: / 19-05-1998 / 17:26:25 / cg"
  3721     "Modified: / 10-01-2018 / 18:35:11 / stefan"
  3733     "Modified: / 10-01-2018 / 18:35:11 / stefan"
       
  3734     "Modified: / 22-10-2019 / 19:37:56 / Stefan Vogel"
  3722 ! !
  3735 ! !
  3723 
  3736 
  3724 
  3737 
  3725 !Stream methodsFor:'stream-to-stream copy'!
  3738 !Stream methodsFor:'stream-to-stream copy'!
  3726 
  3739