PositionableStream.st
changeset 16209 f2d1d7b2d649
parent 16195 2bed37c6f731
child 16840 48075991f9d9
equal deleted inserted replaced
16208:eab134bd8b94 16209:f2d1d7b2d649
   113 
   113 
   114     ^ 0
   114     ^ 0
   115 
   115 
   116     "Modified: / 13-07-2006 / 20:36:54 / cg"
   116     "Modified: / 13-07-2006 / 20:36:54 / cg"
   117 ! !
   117 ! !
   118 
       
   119 
   118 
   120 !PositionableStream methodsFor:'Compatibility-Dolphin'!
   119 !PositionableStream methodsFor:'Compatibility-Dolphin'!
   121 
   120 
   122 endChunk
   121 endChunk
   123     self nextPutChunkSeparator
   122     self nextPutChunkSeparator
   389 
   388 
   390     len := aCollection size.
   389     len := aCollection size.
   391     first := aCollection at:1.
   390     first := aCollection at:1.
   392     [self atEnd] whileFalse:[
   391     [self atEnd] whileFalse:[
   393         buffer := self nextAvailable:len.
   392         buffer := self nextAvailable:len.
   394         buffer = aCollection ifTrue:[
       
   395             ^ self
       
   396         ].
       
   397         buffer size == len ifTrue:[
   393         buffer size == len ifTrue:[
       
   394             buffer = aCollection ifTrue:[
       
   395                 ^ self
       
   396             ].
   398             "expect more input"
   397             "expect more input"
   399             idx := buffer indexOf:first startingAt:2.
   398             idx := buffer indexOf:first startingAt:2.
   400             idx == 0 ifFalse:[
   399             idx ~~ 0 ifTrue:[
   401                 self position:(self position - len + idx - 1)
   400                 self skip:(idx - len - 1)
   402             ].
   401             ].
   403         ].
   402         ].
   404     ].
   403     ].
   405     ^ nil
   404     ^ nil
   406 
   405 
   558     "
   557     "
   559 ! !
   558 ! !
   560 
   559 
   561 !PositionableStream methodsFor:'queries'!
   560 !PositionableStream methodsFor:'queries'!
   562 
   561 
   563 endsWith:aSequenceableCollection
   562 endsBeforePositionWith:aSequenceableCollection
   564     "answer true, if the elements in aSequenceableCollection
   563     "answer true, if the elements in aSequenceableCollection
   565      form the end of the stream."
   564      are at the current end of the stream up to position."
   566 
   565 
       
   566     |sz pos|
       
   567 
       
   568     sz := aSequenceableCollection size.
       
   569     pos := self position.
       
   570     pos < sz ifTrue:[
       
   571         ^ false.
       
   572     ].
   567     self contentsSpecies == collection class ifTrue:[
   573     self contentsSpecies == collection class ifTrue:[
   568         |sz|
   574         ^ collection sameContentsFrom:pos+1-sz to:pos as:aSequenceableCollection startingAt:1.
   569 
   575     ].
   570         sz := aSequenceableCollection size.
   576     self position:pos-sz.
   571         position < sz ifTrue:[
   577     ^ (self next:sz) = aSequenceableCollection.
   572             ^ false.
   578 
   573         ].
   579     "
   574         ^ collection sameContentsFrom:position+1-sz to:position as:aSequenceableCollection startingAt:1.
   580         ('' writeStream nextPutAll:'Hello World') endsBeforePositionWith:'World'
   575     ].
   581         ('' writeStream nextPutAll:'Hello World') endsBeforePositionWith:'Hello World'
   576     ^ self contents endsWith:aSequenceableCollection
   582         ('' writeStream nextPutAll:'Hello World') endsBeforePositionWith:'Hello Worldx'
   577 
   583         ('' writeStream nextPutAll:'Hello World') endsBeforePositionWith:'Bla'
   578     "
   584         ('' writeStream) endsBeforePositionWith:'Bla'
   579         ('' writeStream nextPutAll:'Hello World') endsWith:'World'
   585         ('' writeStream) endsBeforePositionWith:''
   580         ('' writeStream nextPutAll:'Hello World') endsWith:'Hello World'
       
   581         ('' writeStream nextPutAll:'Hello World') endsWith:'xHello World'
       
   582         ('' writeStream nextPutAll:'Hello World') endsWith:'Bla'
       
   583         ('' writeStream) endsWith:'Bla'
       
   584         ('' writeStream) endsWith:''
       
   585         ''  endsWith:''
   586         ''  endsWith:''
   586     "
   587     "
   587 ! !
   588 ! !
   588 
   589 
   589 !PositionableStream methodsFor:'reading'!
   590 !PositionableStream methodsFor:'reading'!
   673 
   674 
   674      Note: this behavior is inconsistent with the other upTo.. methods,
   675      Note: this behavior is inconsistent with the other upTo.. methods,
   675            which position after the found item. We implement the method
   676            which position after the found item. We implement the method
   676            this way for the sake of ST80-compatibility."
   677            this way for the sake of ST80-compatibility."
   677 
   678 
   678     |answerStream element last rslt|
   679     |answerStream element last|
   679 
   680 
   680     last := aCollection last.
   681     last := aCollection last.
   681     answerStream := WriteStream on:(self contentsSpecies new).
   682     answerStream := WriteStream on:(self contentsSpecies new:100).
   682     [self atEnd] whileFalse:[
   683     [(element := self nextOrNil) notNil] whileTrue:[
   683         element := self next.
       
   684         answerStream nextPut:element.
   684         answerStream nextPut:element.
   685         element == last ifTrue:[
   685         (element = last and:[answerStream endsBeforePositionWith:aCollection]) ifTrue:[
   686             ((rslt := answerStream contents) endsWith:aCollection) ifTrue:[
   686             |backStep|
   687                 self position:(self position - aCollection size).
   687             backStep := aCollection size negated.
   688                 ^ rslt copyButLast:aCollection size
   688             self skip:backStep.
   689             ]
   689             answerStream skip:backStep.
       
   690             ^ answerStream contents
   690         ].
   691         ].
   691     ].
   692     ].
   692     ^ answerStream contents
   693     ^ answerStream contents
   693 
   694 
   694     "
   695     "
   695      |s|
   696      |s|
   696      s := ReadStream on:'hello world'.
   697      s := ReadStream on:'hello world'.
   697      Transcript show:'<'; show:(s upToAll:'wo'); showCR:'>'. 
   698      Transcript show:'<'; show:(s upToAll_positionBefore:'wo'); showCR:'>'. 
   698      Transcript showCR:s atEnd.
   699      Transcript showCR:s atEnd.
   699      Transcript show:'<'; show:(s upToEnd); showCR:'>'. 
   700      Transcript show:'<'; show:(s upToEnd); showCR:'>'. 
   700     "
   701     "
   701     "
   702     "
   702      |s|
   703      |s|
   703      s := ReadStream on:'hello world'.
   704      s := ReadStream on:'hello world'.
   704      Transcript show:'<'; show:(s upToAll:'wo'); showCR:'>'. 
   705      Transcript show:'<'; show:(s upToAll_positionBefore:'wo'); showCR:'>'. 
   705      Transcript showCR:s atEnd.
   706      Transcript showCR:s atEnd.
   706      Transcript show:'<'; show:(s upToAll:'wo'); showCR:'>'. 
   707      Transcript show:'<'; show:(s upToAll_positionBefore:'wo'); showCR:'>'. 
   707     "
   708     "
   708     "
   709     "
   709      |s|
   710      |s|
   710      s := ReadStream on:'hello world'.
   711      s := ReadStream on:'hello world'.
   711      Transcript show:'<'; show:(s upToAll:'xx'); showCR:'>'. 
   712      Transcript show:'<'; show:(s upToAll_positionBefore:'xx'); showCR:'>'. 
   712      Transcript showCR:s atEnd.
   713      Transcript showCR:s atEnd.
   713      Transcript show:'<'; show:(s upToEnd); showCR:'>'. 
   714      Transcript show:'<'; show:(s upToEnd); showCR:'>'. 
   714     "
   715     "
   715 
   716 
   716     "Modified: / 12.1.1998 / 22:06:42 / cg"
   717     "Modified: / 12.1.1998 / 22:06:42 / cg"
   746 ! !
   747 ! !
   747 
   748 
   748 !PositionableStream class methodsFor:'documentation'!
   749 !PositionableStream class methodsFor:'documentation'!
   749 
   750 
   750 version
   751 version
   751     ^ '$Header: /cvs/stx/stx/libbasic/PositionableStream.st,v 1.165 2014-03-04 12:36:39 stefan Exp $'
   752     ^ '$Header: /cvs/stx/stx/libbasic/PositionableStream.st,v 1.166 2014-03-05 10:02:58 stefan Exp $'
   752 !
   753 !
   753 
   754 
   754 version_CVS
   755 version_CVS
   755     ^ '$Header: /cvs/stx/stx/libbasic/PositionableStream.st,v 1.165 2014-03-04 12:36:39 stefan Exp $'
   756     ^ '$Header: /cvs/stx/stx/libbasic/PositionableStream.st,v 1.166 2014-03-05 10:02:58 stefan Exp $'
   756 ! !
   757 ! !
   757 
   758 
   758 
   759 
   759 PositionableStream initialize!
   760 PositionableStream initialize!