PeekableStream.st
branchjv
changeset 18217 d222015cc39c
parent 18120 e3a375d5f6a8
parent 18213 c0d479ac5af6
child 18274 042d13555f1f
equal deleted inserted replaced
18212:d8a2b0f3efff 18217:d222015cc39c
       
     1 "{ Encoding: utf8 }"
       
     2 
     1 "
     3 "
     2  COPYRIGHT (c) 1994 by Claus Gittinger
     4  COPYRIGHT (c) 1994 by Claus Gittinger
     3 	      All Rights Reserved
     5 	      All Rights Reserved
     4 
     6 
     5  This software is furnished under a license and may be used
     7  This software is furnished under a license and may be used
   135      This exception was added to make it easier to edit primitive code with 
   137      This exception was added to make it easier to edit primitive code with 
   136      external editors. However, this means, that other Smalltalks cannot always 
   138      external editors. However, this means, that other Smalltalks cannot always 
   137      read chunks containing primitive code 
   139      read chunks containing primitive code 
   138      - but that doesn't really matter, since C-primitives are an ST/X feature anyway."
   140      - but that doesn't really matter, since C-primitives are an ST/X feature anyway."
   139 
   141 
   140     |theString chunkSeparator newString done thisChar nextChar 
   142     |buffer theString chunkSeparator newString done thisChar nextChar 
   141      atBeginOfLine inPrimitive  hasCR hasLF
   143      atBeginOfLine inPrimitive  hasCR hasLF
   142      index    "{ Class:SmallInteger }"
   144      index    "{ Class:SmallInteger }"
   143      currSize "{ Class:SmallInteger }" |
   145      currSize "{ Class:SmallInteger }" |
   144 
   146 
   145     chunkSeparator := ChunkSeparator.
   147     chunkSeparator := ChunkSeparator.
   146     currSize := 500.
   148     buffer := CharacterWriteStream on:(String new:100).
   147     theString := String new:currSize.
       
   148     self skipSeparators.
   149     self skipSeparators.
   149     thisChar := self nextOrNil.
   150     thisChar := self nextOrNil.
   150     index := 0.
       
   151     done := false.
   151     done := false.
   152     atBeginOfLine := true.
   152     atBeginOfLine := true.
   153     inPrimitive := false.
   153     inPrimitive := false.
   154     hasCR := hasLF := false.
   154     hasCR := hasLF := false.
   155 
   155 
   156     [done not and:[thisChar notNil]] whileTrue:[
   156     [done not and:[thisChar notNil]] whileTrue:[
   157         ((index + 2) <= currSize) ifFalse:[
       
   158             newString := String new:(currSize * 2).
       
   159             newString replaceFrom:1 to:currSize with:theString.
       
   160             currSize := currSize * 2.
       
   161             theString := newString
       
   162         ].
       
   163 
       
   164         "match primitive only at beginning of line 
   157         "match primitive only at beginning of line 
   165          (ExternalStream>>#nextChunk did this, although stc allows primitive to start anywhere)"
   158          (ExternalStream>>#nextChunk did this, although stc allows primitive to start anywhere)"
   166 
   159 
   167         (atBeginOfLine and:[thisChar == $%]) ifTrue:[
   160         (atBeginOfLine and:[thisChar == $%]) ifTrue:[
   168             nextChar := self peekOrNil.
   161             nextChar := self peekOrNil.
   169             (nextChar == ${ ) ifTrue:[
   162             (nextChar == ${ ) ifTrue:[
   170                 inPrimitive := true.
   163                 inPrimitive := true.
   171                 index := index + 1.
   164                 buffer nextPut:thisChar.
   172                 theString at:index put:thisChar.
       
   173                 thisChar := self next
   165                 thisChar := self next
   174             ] ifFalse:[
   166             ] ifFalse:[
   175                 (nextChar == $} ) ifTrue:[
   167                 (nextChar == $} ) ifTrue:[
   176                     inPrimitive := false.
   168                     inPrimitive := false.
   177                     index := index + 1.
   169                     buffer nextPut:thisChar.
   178                     theString at:index put:thisChar.
       
   179                     thisChar := self next
   170                     thisChar := self next
   180                 ]
   171                 ]
   181             ]
   172             ]
   182         ] ifFalse:[
   173         ] ifFalse:[
   183             "chunk can not end in primitive code"
   174             "chunk can not end in primitive code"
   207             ] ifFalse:[thisChar == Character lf ifTrue:[
   198             ] ifFalse:[thisChar == Character lf ifTrue:[
   208                 hasLF := true.
   199                 hasLF := true.
   209                 atBeginOfLine := true.
   200                 atBeginOfLine := true.
   210             ]].
   201             ]].
   211                 
   202                 
   212             index := index + 1.
   203             buffer nextPut:thisChar.
   213             theString at:index put:thisChar.
       
   214             thisChar := self nextOrNil.
   204             thisChar := self nextOrNil.
   215         ].
   205         ].
   216     ].
   206     ].
   217 
   207 
   218     (index == 0) ifTrue:[^ ''].
   208     theString := buffer contents.
   219 
       
   220     theString := theString copyTo:index.
       
   221     (hasCR and:[hasLF not]) ifTrue:[
   209     (hasCR and:[hasLF not]) ifTrue:[
   222         "map all CR in a CR only file to NL (ExternalStream>>#nextChunk did this)"
   210         "map all CR in a CR only file to NL (ExternalStream>>#nextChunk did this)"
   223         theString replaceAll:Character cr with:Character nl.
   211         theString replaceAll:Character cr with:Character nl.
   224     ].
   212     ].
   225 
   213 
   855 ! !
   843 ! !
   856 
   844 
   857 !PeekableStream class methodsFor:'documentation'!
   845 !PeekableStream class methodsFor:'documentation'!
   858 
   846 
   859 version
   847 version
   860     ^ '$Header: /cvs/stx/stx/libbasic/PeekableStream.st,v 1.53 2015-03-23 15:15:46 cg Exp $'
   848     ^ '$Header: /cvs/stx/stx/libbasic/PeekableStream.st,v 1.54 2015-04-14 11:04:28 cg Exp $'
   861 !
   849 !
   862 
   850 
   863 version_CVS
   851 version_CVS
   864     ^ '$Header: /cvs/stx/stx/libbasic/PeekableStream.st,v 1.53 2015-03-23 15:15:46 cg Exp $'
   852     ^ '$Header: /cvs/stx/stx/libbasic/PeekableStream.st,v 1.54 2015-04-14 11:04:28 cg Exp $'
   865 ! !
   853 ! !
   866 
   854 
   867 
   855 
   868 PeekableStream initialize!
   856 PeekableStream initialize!