substreams/extensions.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Wed, 01 Feb 2012 00:41:14 +0000
changeset 99 677c81c943e4
parent 75 7c5f31b85ace
child 107 de5fa7bcc01a
permissions -rw-r--r--
build files regenerated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
67
mkobetic
parents: 45
diff changeset
     1
"{ Package: 'stx:goodies/xtreams/substreams' }"
mkobetic
parents: 45
diff changeset
     2
mkobetic
parents: 45
diff changeset
     3
!
4
20abcbb9eb33 first cut
Martin Kobetic <mkobetic@gmail.com>
parents:
diff changeset
     4
75
mkobetic
parents: 67
diff changeset
     5
!Block methodsFor:'xtreams'!
45
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
     6
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
     7
streamingReadMatching: aStream inclusive: tail
75
mkobetic
parents: 67
diff changeset
     8
        ^Xtreams::TestReadSubstream on: aStream test: self inclusive: tail
45
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
     9
! !
75
mkobetic
parents: 67
diff changeset
    10
!Block methodsFor:'xtreams'!
45
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    11
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    12
streamingWriteMatching: aStream inclusive: tail
75
mkobetic
parents: 67
diff changeset
    13
        ^Xtreams::TestWriteSubstream on: aStream test: self inclusive: tail
45
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    14
! !
67
mkobetic
parents: 45
diff changeset
    15
!Object methodsFor:'xtreams'!
45
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    16
67
mkobetic
parents: 45
diff changeset
    17
streamingReadMatching: aStream inclusive: tail
mkobetic
parents: 45
diff changeset
    18
	^[:each | each == self] streamingReadMatching: aStream inclusive: tail
mkobetic
parents: 45
diff changeset
    19
! !
mkobetic
parents: 45
diff changeset
    20
!Object methodsFor:'xtreams'!
mkobetic
parents: 45
diff changeset
    21
mkobetic
parents: 45
diff changeset
    22
streamingWriteMatching: aStream inclusive: tail
mkobetic
parents: 45
diff changeset
    23
	^[:each | each == self] streamingWriteMatching: aStream inclusive: tail
mkobetic
parents: 45
diff changeset
    24
! !
75
mkobetic
parents: 67
diff changeset
    25
!SequenceableCollection methodsFor:'xtreams'!
45
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    26
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    27
streamingMatchPrefixFunction
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    28
	"Compute the array that tells us how far we need to back up when a match fails. This is using the Knuth-Morris-Pratt matching algorithm"
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    29
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    30
	| backtrack partialMatch |
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    31
	backtrack := Array new: self size + 1. 
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    32
	backtrack at: 1 put: 1; at: 2 put: 1.
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    33
	partialMatch := 1.
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    34
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    35
	2 to: self size do: [:index |
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    36
		| current |
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    37
		current := self at: index.
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    38
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    39
		"If there's a mismatch, back up to the previous partial match and see if the match can continue from there. If not, repeat until we find a match or hit the beginning"
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    40
		[partialMatch > 1 and: [(self at: partialMatch) ~= current]]
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    41
			whileTrue: [partialMatch := backtrack at: partialMatch - 1].
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    42
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    43
		(self at: partialMatch) = current
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    44
			ifTrue: [partialMatch := partialMatch + 1].
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    45
		backtrack at: index put: partialMatch].
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    46
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    47
	^backtrack
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    48
! !
75
mkobetic
parents: 67
diff changeset
    49
!SequenceableCollection methodsFor:'xtreams'!
45
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    50
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    51
streamingReadMatching: aStream inclusive: tail
75
mkobetic
parents: 67
diff changeset
    52
        "Return a stream that produces substreams that separate by @pattern and include the pattern in the result if @tail is true.
mkobetic
parents: 67
diff changeset
    53
         It uses the Knuth-Morris-Pratt algorithm, from Cormen et al.'s Algorithms, page 871. See also Gusfield's Alorithms on Strings, Trees and Sequences, page 23. This algorithm has the advantage in stream matching that it reads every character in the the text to be matched against exactly once, and never backtracks."
45
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    54
75
mkobetic
parents: 67
diff changeset
    55
        | backtrack |
mkobetic
parents: 67
diff changeset
    56
        backtrack := self streamingMatchPrefixFunction.
mkobetic
parents: 67
diff changeset
    57
        ^Xtreams::MatchReadSubstream on: aStream pattern: self backtrack: backtrack inclusive: tail
45
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    58
! !
75
mkobetic
parents: 67
diff changeset
    59
!SequenceableCollection methodsFor:'xtreams'!
45
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    60
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    61
streamingWriteMatching: aStream inclusive: tail
75
mkobetic
parents: 67
diff changeset
    62
        "Return a stream that produces substreams that separate by @pattern and include the pattern in the result if @tail is true.
mkobetic
parents: 67
diff changeset
    63
         It uses the Knuth-Morris-Pratt algorithm, from Cormen et al.'s Algorithms, page 871. See also Gusfield's Alorithms on Strings, Trees and Sequences, page 23. This algorithm has the advantage in stream matching that it reads every character in the the text to be matched against exactly once, and never backtracks."
45
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    64
75
mkobetic
parents: 67
diff changeset
    65
        | backtrack |
mkobetic
parents: 67
diff changeset
    66
        backtrack := self streamingMatchPrefixFunction.
mkobetic
parents: 67
diff changeset
    67
        ^Xtreams::MatchWriteSubstream on: aStream pattern: self backtrack: backtrack inclusive: tail
45
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    68
! !
67
mkobetic
parents: 45
diff changeset
    69
!Xtreams::ReadStream methodsFor:'substreaming'!
45
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
    70
67
mkobetic
parents: 45
diff changeset
    71
, aReadStream
mkobetic
parents: 45
diff changeset
    72
	"Return a read stream that combines self and @aReadStream into a single stream.
mkobetic
parents: 45
diff changeset
    73
	""
mkobetic
parents: 45
diff changeset
    74
		((1 to: 5) reading, (6 to: 10) reading) rest
mkobetic
parents: 45
diff changeset
    75
	""
mkobetic
parents: 45
diff changeset
    76
		| files |
mkobetic
parents: 45
diff changeset
    77
		files := '/pub/vw7.8' asFilename reading.
mkobetic
parents: 45
diff changeset
    78
		[ | fn | fn := files get. fn isDirectory ifTrue: [ files := fn reading, files ]. fn ] reading rest
mkobetic
parents: 45
diff changeset
    79
	"
mkobetic
parents: 45
diff changeset
    80
	^(Array with: self with: aReadStream) reading stitching
mkobetic
parents: 45
diff changeset
    81
! !
mkobetic
parents: 45
diff changeset
    82
!Xtreams::ReadStream methodsFor:'substreaming'!
mkobetic
parents: 45
diff changeset
    83
mkobetic
parents: 45
diff changeset
    84
closing: aBlock
mkobetic
parents: 45
diff changeset
    85
	^(PositionReadSubstream on: self)
mkobetic
parents: 45
diff changeset
    86
		closeBlock: aBlock;
mkobetic
parents: 45
diff changeset
    87
		yourself
mkobetic
parents: 45
diff changeset
    88
! !
mkobetic
parents: 45
diff changeset
    89
!Xtreams::ReadStream methodsFor:'substreaming'!
mkobetic
parents: 45
diff changeset
    90
mkobetic
parents: 45
diff changeset
    91
ending: aMatchable
mkobetic
parents: 45
diff changeset
    92
	"Creates a substream that will end when aMatchable finds a match in the content passing through. aMatchable is either
mkobetic
parents: 45
diff changeset
    93
		* a block that is evaluated with each element; the stream ends when the block returns true
mkobetic
parents: 45
diff changeset
    94
		* a collection that is matched against the last elements read, the stream ends when the collection matches
mkobetic
parents: 45
diff changeset
    95
		* any other object, the stream matches when an equal object is read from the stream"
mkobetic
parents: 45
diff changeset
    96
	"	aMatchable	<BlockClosure | Collection | Object>
mkobetic
parents: 45
diff changeset
    97
		^<TransformReadStream>
mkobetic
parents: 45
diff changeset
    98
	""
mkobetic
parents: 45
diff changeset
    99
		('abcdefghijklmnopqrstuvxyz' reading ending: $j) rest.
mkobetic
parents: 45
diff changeset
   100
	""
mkobetic
parents: 45
diff changeset
   101
		('abcdefghijklmnopqrstuvxyz' reading ending: 'mno') rest
mkobetic
parents: 45
diff changeset
   102
	""
mkobetic
parents: 45
diff changeset
   103
		('abcdefghijklmnopqrstuvxyz' reading ending: [ :e | 'gmt' includes: e ]) rest
mkobetic
parents: 45
diff changeset
   104
	"
mkobetic
parents: 45
diff changeset
   105
	^self ending: aMatchable inclusive: false
mkobetic
parents: 45
diff changeset
   106
! !
mkobetic
parents: 45
diff changeset
   107
!Xtreams::ReadStream methodsFor:'substreaming'!
mkobetic
parents: 45
diff changeset
   108
mkobetic
parents: 45
diff changeset
   109
ending: aMatchable inclusive: inclusive
mkobetic
parents: 45
diff changeset
   110
	"Creates a substream that will end when aMatchable finds a match in the content passing through. aMatchable is either
mkobetic
parents: 45
diff changeset
   111
		* a block that is evaluated with each element - the stream ends when the block returns true
mkobetic
parents: 45
diff changeset
   112
		* a collection that is matched against the last elements read - the stream ends when the collection matches
mkobetic
parents: 45
diff changeset
   113
		* any other object - the stream matches when an equal object is read from the stream
mkobetic
parents: 45
diff changeset
   114
	The inclusive parameter determins if the elements matching the end condition should be included in the substream contents or not."
mkobetic
parents: 45
diff changeset
   115
	"	aMatchable	<BlockClosure | Collection | Object>	the substream ending criteria
mkobetic
parents: 45
diff changeset
   116
		inclusive	<Boolean> should the content matching the end condition be included in the substream
mkobetic
parents: 45
diff changeset
   117
		^<TransformReadStream>
mkobetic
parents: 45
diff changeset
   118
	""
mkobetic
parents: 45
diff changeset
   119
		('abcdefghijklmnopqrstuvxyz' reading ending: $j inclusive: true) rest.
mkobetic
parents: 45
diff changeset
   120
	""
mkobetic
parents: 45
diff changeset
   121
		('abcdefghijklmnopqrstuvxyz' reading ending: 'mno' inclusive: true) rest
mkobetic
parents: 45
diff changeset
   122
	""
mkobetic
parents: 45
diff changeset
   123
		('abcdefghijklmnopqrstuvxyz' reading ending: [ :e | 'gmt' includes: e ] inclusive: true) rest
mkobetic
parents: 45
diff changeset
   124
	"
mkobetic
parents: 45
diff changeset
   125
	^aMatchable streamingReadMatching: self inclusive: inclusive
mkobetic
parents: 45
diff changeset
   126
! !
mkobetic
parents: 45
diff changeset
   127
!Xtreams::ReadStream methodsFor:'substreaming'!
mkobetic
parents: 45
diff changeset
   128
mkobetic
parents: 45
diff changeset
   129
limiting: limit
mkobetic
parents: 45
diff changeset
   130
	"Create a substream that will allow at most @limit number of elements to be read from the source."
mkobetic
parents: 45
diff changeset
   131
	"	limit		<Integer>	maximum number of elements that can be read from the source
mkobetic
parents: 45
diff changeset
   132
		^<LimitReadStream>"
mkobetic
parents: 45
diff changeset
   133
	"
mkobetic
parents: 45
diff changeset
   134
		('abcdefghi' reading limiting: 5) rest
mkobetic
parents: 45
diff changeset
   135
	"
mkobetic
parents: 45
diff changeset
   136
	^LimitReadSubstream on: self limit: limit
mkobetic
parents: 45
diff changeset
   137
! !
mkobetic
parents: 45
diff changeset
   138
!Xtreams::ReadStream methodsFor:'substreaming'!
mkobetic
parents: 45
diff changeset
   139
mkobetic
parents: 45
diff changeset
   140
slicing
mkobetic
parents: 45
diff changeset
   141
	"From a readable stream, return a readable stream that acts as a prototype factory for the readable stream."
mkobetic
parents: 45
diff changeset
   142
	"	^<ReadStream>"
mkobetic
parents: 45
diff changeset
   143
	"
mkobetic
parents: 45
diff changeset
   144
		((1 to: 100) reading limiting: 10) slicing do: [:substream | Transcript cr; print: substream rest]
mkobetic
parents: 45
diff changeset
   145
	"
mkobetic
parents: 45
diff changeset
   146
	| substream |
mkobetic
parents: 45
diff changeset
   147
	substream := nil.
mkobetic
parents: 45
diff changeset
   148
	^[substream == nil ifFalse:
mkobetic
parents: 45
diff changeset
   149
		[substream substreamClosed ifFalse: [substream close].
mkobetic
parents: 45
diff changeset
   150
		substream subseekend.
mkobetic
parents: 45
diff changeset
   151
		substream sourceAtEnd ifTrue: [Incomplete zero raise]].
mkobetic
parents: 45
diff changeset
   152
	substream := self copy]
mkobetic
parents: 45
diff changeset
   153
		reading
mkobetic
parents: 45
diff changeset
   154
			closeBlock: [source close];
mkobetic
parents: 45
diff changeset
   155
			yourself
mkobetic
parents: 45
diff changeset
   156
! !
mkobetic
parents: 45
diff changeset
   157
!Xtreams::ReadStream methodsFor:'substreaming'!
mkobetic
parents: 45
diff changeset
   158
mkobetic
parents: 45
diff changeset
   159
stitching
mkobetic
parents: 45
diff changeset
   160
	"From a stream that returns streams (either read or write streams), stitch them together sequencially such that they appear to be one contiguous stream."
mkobetic
parents: 45
diff changeset
   161
	"^ <StitchReadStream>"
mkobetic
parents: 45
diff changeset
   162
	"
mkobetic
parents: 45
diff changeset
   163
		| data current |
mkobetic
parents: 45
diff changeset
   164
		data := (1 to: 100) reading.
mkobetic
parents: 45
diff changeset
   165
		current := nil.
mkobetic
parents: 45
diff changeset
   166
		[	(current notNil and: [ current position < 10 ]) ifTrue: [ Incomplete zero raise ].
mkobetic
parents: 45
diff changeset
   167
			current := data limiting: 10
mkobetic
parents: 45
diff changeset
   168
		] reading stitching rest
mkobetic
parents: 45
diff changeset
   169
	"
mkobetic
parents: 45
diff changeset
   170
	| first |
mkobetic
parents: 45
diff changeset
   171
	first := self get.
mkobetic
parents: 45
diff changeset
   172
	first isReadable ifTrue: [^StitchReadStream on: self first: first].
mkobetic
parents: 45
diff changeset
   173
	first isWritable ifTrue: [^StitchWriteStream on: self first: first].
mkobetic
parents: 45
diff changeset
   174
	^self error: 'Cannot read or write to this stream, what is it?'
mkobetic
parents: 45
diff changeset
   175
! !
45
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   176
!Xtreams::WriteStream methodsFor:'substreaming'!
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   177
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   178
closing: aBlock
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   179
	^(PositionWriteSubstream on: self)
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   180
		closeBlock: aBlock;
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   181
		yourself
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   182
! !
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   183
!Xtreams::WriteStream methodsFor:'substreaming'!
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   184
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   185
ending: aMatchable
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   186
	"Creates a substream that will end when aMatchable finds a match in the content passing through. aMatchable is either
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   187
		* a block that is evaluated with each element - the stream ends when the block returns true
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   188
		* a collection that is matched against the last elements written - the stream ends when the collection matches
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   189
		* any other object - the stream ends when an equal object is written into the stream"
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   190
	"	aMatchable	<BlockClosure | Collection | Object> the substream ending criteria
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   191
		^<TransformWriteStream>
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   192
	""	
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   193
		| stream slice |
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   194
		stream := String new writing.
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   195
		slice := stream ending: $j.
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   196
		[ slice write: 'abcdefghijklmnopqrstuvxyz' ] on: Incomplete do: [].
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   197
		stream conclusion
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   198
	""
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   199
		| stream slice |
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   200
		stream := String new writing.
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   201
		slice := stream ending: 'mno'.
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   202
		[ slice write: 'abcdefghijklmnopqrstuvxyz' ] on: Incomplete do: [].
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   203
		stream conclusion
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   204
	""
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   205
		| stream slice |
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   206
		stream := String new writing.
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   207
		slice := stream ending: [ :e | 'gmt' includes: e ].
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   208
		[ slice write: 'abcdefghijklmnopqrstuvxyz' ] on: Incomplete do: [].
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   209
		stream conclusion
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   210
	"
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   211
	^self ending: aMatchable inclusive: false
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   212
! !
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   213
!Xtreams::WriteStream methodsFor:'substreaming'!
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   214
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   215
ending: aMatchable inclusive: inclusive
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   216
	"Creates a substream that will end when aMatchable finds a match in the content passing through. aMatchable is either
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   217
		* a block that is evaluated with each element - the stream ends when the block returns true
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   218
		* a collection that is matched against the last elements written - the stream ends when the collection matches
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   219
		* any other object - the stream ends when an equal object is written into the stream"
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   220
	"	aMatchable	<BlockClosure | Collection | Object> the substream ending criteria
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   221
		inclusive <Boolean> should the matched elements be included in the stream contents or not
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   222
		^<TransformWriteStream>
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   223
	""
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   224
		| stream slice |
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   225
		stream := String new writing.
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   226
		slice := stream ending: $j inclusive: true.
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   227
		[ slice write: 'abcdefghijklmnopqrstuvxyz' ] on: Incomplete do: [].
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   228
		stream conclusion
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   229
	""
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   230
		| stream slice |
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   231
		stream := String new writing.
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   232
		slice := stream ending: 'mno' inclusive: true.
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   233
		[ slice write: 'abcdefghijklmnopqrstuvxyz' ] on: Incomplete do: [].
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   234
		stream conclusion
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   235
	""
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   236
		| stream slice |
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   237
		stream := String new writing.
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   238
		slice := stream ending: [ :e | 'gmt' includes: e ] inclusive: true.
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   239
		[ slice write: 'abcdefghijklmnopqrstuvxyz' ] on: Incomplete do: [].
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   240
		stream conclusion
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   241
	"
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   242
	^aMatchable streamingWriteMatching: self inclusive: inclusive
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   243
! !
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   244
!Xtreams::WriteStream methodsFor:'substreaming'!
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   245
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   246
limiting: limit
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   247
	"Create a substream that will allow at most @limit number of elements written into the destination."
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   248
	"	limit	<Integer>	maximum number of elements that can be written into destination
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   249
		^<LimitWriteStream>"
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   250
	"
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   251
		| stream slice |
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   252
		stream := String new writing.
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   253
		slice := stream limiting: 5.
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   254
		[ slice write: 'abcdefghi' ] on: Incomplete do: [].
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   255
		stream conclusion
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   256
	"
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   257
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   258
	^LimitWriteSubstream on: self limit: limit
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   259
! !
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   260
!Xtreams::WriteStream methodsFor:'substreaming'!
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   261
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   262
slicing
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   263
	"From a writable stream, return a readable stream that acts as a prototype factory for the writable stream."
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   264
	"	^<ReadStream>"
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   265
	"(destination limiting: 10) slicing"
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   266
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   267
	| substream |
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   268
	substream := nil.
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   269
	^[substream == nil ifFalse:
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   270
		[substream substreamClosed ifFalse: [substream close].
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   271
		substream subseekend.
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   272
		substream destinationAtEnd ifTrue: [Incomplete zero raise]].
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   273
		substream := self copy]
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   274
		reading
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   275
			closeBlock: [destination close];
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   276
			yourself
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   277
! !
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   278
!Xtreams::WriteStream methodsFor:'substreaming'!
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   279
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   280
stitching
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   281
	^self error: 'You can only stitch a read stream, however that read stream can return write streams and in so doing, you will create a stitching write stream.'
ceaa8ff12b41 packaging
mkobetic
parents: 37
diff changeset
   282
! !