" Smalltalk/WriteStream.st -*- Smalltalk -*- " WriteStream : PositionableStream ( writeLimit ) WriteStream reset [ readLimit := readLimit max: position. super reset ] WriteStream resetToStart [ readLimit := position := 0 ] WriteStream on: aCollection [ self := super on: aCollection. readLimit := 0. writeLimit := 0. ] WriteStream nextPut: anObject [ " Insert the argument at the next position in the Stream represented by the receiver. Fail if the collection of this stream is not an Array or a String. Fail if the stream is positioned at its end, or if the position is out of bounds in the collection. Fail if the argument is not of the right type for the collection. " ^position >= writeLimit ifTrue: [self pastEndPut: anObject] ifFalse: [collection at: (position := position + 1) - 1 put: anObject] ] WriteStream pastEndPut: anObject [ " Grow the collection by creating a new bigger collection and then copy over the contents from the old one. We grow by doubling the size but the growth is kept between 20 and 1000000. Finally we put anObject at the current write position. " | oldSize grownCollection | oldSize := collection size. grownCollection := collection new: oldSize + ((oldSize max: 20) min: 1000000). collection := grownCollection replaceFrom: 0 to: oldSize with: collection startingAt: 0. writeLimit := collection size. ^collection at: (position := position + 1) - 1 put: anObject ] WriteStream size [ ^readLimit := readLimit max: position ] WriteStream tab [ self nextPut: $\t ] WriteStream nl [ self nextPut: $\n ] WriteStream cr [ self nextPut: $\r ] WriteStream space [ self nextPut: $ ] WriteStream tab: n [ n timesRepeat: [self tab ] ] WriteStream nl: n [ n timesRepeat: [self nl ] ] WriteStream cr: n [ n timesRepeat: [self cr ] ] WriteStream space: n [ n timesRepeat: [self space] ] WriteStream contents [ readLimit := readLimit max: position. ^super contents ] WriteStream position: anInteger [ readLimit := readLimit max: position. super position: anInteger ] WriteStream writeStream [] "----------------------------------------------------------------" SequenceableCollection writeStream [ ^WriteStream on: self ] SequenceableCollection tokenised: delimiters [ | in out tokens | in := self readStream. out := self new writeStream. tokens := OrderedCollection new. [in atEnd] whileFalse: [ [in atEnd not and: [delimiters includes: in peek]] whileTrue: [in next]. [in atEnd not and: [(delimiters includes: in peek) not]] whileTrue: [out nextPut: in next]. out isEmpty ifFalse: [tokens add: out contents]. out resetToStart ]. ^tokens ]