author | Stefan Vogel <sv@exept.de> |
Tue, 28 Apr 2020 14:41:21 +0200 | |
changeset 25372 | 389daab3ee10 |
parent 25266 | 21fa0b4e0930 |
permissions | -rw-r--r-- |
25266 | 1 |
"{ Encoding: utf8 }" |
2 |
||
1 | 3 |
" |
5 | 4 |
COPYRIGHT (c) 1989 by Claus Gittinger |
159 | 5 |
All Rights Reserved |
1 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
5393
7e2230bd4bad
moved some stuff to superclass
Claus Gittinger <cg@exept.de>
parents:
5308
diff
changeset
|
14 |
"{ Package: 'stx:libbasic' }" |
7e2230bd4bad
moved some stuff to superclass
Claus Gittinger <cg@exept.de>
parents:
5308
diff
changeset
|
15 |
|
17559 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
57 | 18 |
PeekableStream subclass:#PositionableStream |
973 | 19 |
instanceVariableNames:'collection position readLimit writeLimit' |
8443
7bc4348c059e
fileIn stuff moved to peekableStream
Claus Gittinger <cg@exept.de>
parents:
8363
diff
changeset
|
20 |
classVariableNames:'ZeroPosition InvalidPositionErrorSignal' |
973 | 21 |
poolDictionaries:'' |
22 |
category:'Streams' |
|
1 | 23 |
! |
24 |
||
1897 | 25 |
!PositionableStream class methodsFor:'documentation'! |
88 | 26 |
|
27 |
copyright |
|
28 |
" |
|
29 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
159 | 30 |
All Rights Reserved |
1 | 31 |
|
88 | 32 |
This software is furnished under a license and may be used |
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
! |
|
10 | 40 |
|
88 | 41 |
documentation |
42 |
" |
|
43 |
Instances of PositionableStream allow positioning the read pointer. |
|
44 |
The PositionableStream class also adds methods for source-chunk reading |
|
45 |
and writing, and for filing-in/out of source code. |
|
3249
8401c3e42165
checkin to force re-checkout.
Claus Gittinger <cg@exept.de>
parents:
3184
diff
changeset
|
46 |
|
88 | 47 |
This is an abstract class. |
1295 | 48 |
|
4405 | 49 |
Compatibility Notice: |
9434 | 50 |
In previous versions of ST/X, streams started with a 1-position (i.e. as in collections), |
51 |
while ST-80 has always been using 0-based postions for streams and 1-based positions for collections. |
|
4405 | 52 |
|
17559 | 53 |
THIS CERTAINLY IS BAD. |
54 |
||
55 |
Although this is confusing ST/X has been changed to now also uses 0-based stream positioning. |
|
9434 | 56 |
|
17559 | 57 |
[caveat:] |
58 |
Basing capabilities like readability/writability/positionability/peekability on inheritance makes |
|
59 |
the class hierarchy ugly and leads to strange and hard to teach redefinitions (aka. NonPositionableStream |
|
60 |
below PositionableStream or ExternalReadStream under WriteStream) |
|
4405 | 61 |
|
1295 | 62 |
[author:] |
9434 | 63 |
Claus Gittinger |
88 | 64 |
" |
65 |
! ! |
|
1 | 66 |
|
1897 | 67 |
!PositionableStream class methodsFor:'initialization'! |
1 | 68 |
|
10 | 69 |
initialize |
15353 | 70 |
ZeroPosition := 0. "/ changed with stx rel5.1; |
71 |
"/ rel6.2: no longer used (i.e. 0 has now ben hardcoded) |
|
72 |
"/ class variable to be eliminated. |
|
7363 | 73 |
|
8443
7bc4348c059e
fileIn stuff moved to peekableStream
Claus Gittinger <cg@exept.de>
parents:
8363
diff
changeset
|
74 |
InvalidPositionErrorSignal isNil ifTrue:[ |
7092
630807cd320f
Convert Object>>errorSignal -> Error
Stefan Vogel <sv@exept.de>
parents:
7054
diff
changeset
|
75 |
InvalidPositionErrorSignal := PositionOutOfBoundsError. |
630807cd320f
Convert Object>>errorSignal -> Error
Stefan Vogel <sv@exept.de>
parents:
7054
diff
changeset
|
76 |
InvalidPositionErrorSignal notifierString:'position out of bounds: '. |
10 | 77 |
] |
78 |
! ! |
|
1 | 79 |
|
1897 | 80 |
!PositionableStream class methodsFor:'instance creation'! |
1 | 81 |
|
82 |
on:aCollection |
|
83 |
"return a new PositionableStream streaming on aCollection" |
|
84 |
||
85 |
^ (self basicNew) on:aCollection |
|
86 |
! |
|
87 |
||
88 |
on:aCollection from:first to:last |
|
89 |
"return a new PositionableStream streaming on aCollection |
|
90 |
from first to last" |
|
91 |
||
369 | 92 |
^ (self basicNew) on:aCollection from:first to:last |
57 | 93 |
! |
94 |
||
95 |
with:aCollection |
|
96 |
"return a new PositionableStream streaming on aCollection, |
|
97 |
the stream is positioned to the end of the collection." |
|
98 |
||
362 | 99 |
^ (self basicNew) with:aCollection |
1 | 100 |
! ! |
101 |
||
3032
d267a0b1b48d
handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
102 |
!PositionableStream class methodsFor:'Signal constants'! |
d267a0b1b48d
handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
103 |
|
d267a0b1b48d
handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
104 |
invalidPositionErrorSignal |
d267a0b1b48d
handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
105 |
"return the signal raised if positioning is attempted to an |
d267a0b1b48d
handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
106 |
invalid position (i.e. before the begin of the stream or after |
d267a0b1b48d
handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
107 |
the end)" |
d267a0b1b48d
handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
108 |
|
8320
229adaeaf183
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
8318
diff
changeset
|
109 |
^ PositionOutOfBoundsError |
3032
d267a0b1b48d
handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
110 |
! ! |
d267a0b1b48d
handle empty chunks when filing in
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
111 |
|
1897 | 112 |
!PositionableStream class methodsFor:'constants'! |
1 | 113 |
|
4405 | 114 |
zeroPosition |
115 |
"return the number, which marks the initial position. |
|
116 |
Compatibility Notice: |
|
9434 | 117 |
In previous versions of ST/X, streams started with a 1-position (i.e. as in collections), |
118 |
while ST-80 has always been using 0-based postions for streams and 1-based positions for collections. |
|
4405 | 119 |
|
9434 | 120 |
Although this is confusing ST/X has been changed to now also use 0-based stream positioning. |
121 |
" |
|
4405 | 122 |
|
15353 | 123 |
^ 0 |
9434 | 124 |
|
125 |
"Modified: / 13-07-2006 / 20:36:54 / cg" |
|
360 | 126 |
! ! |
127 |
||
21034 | 128 |
!PositionableStream class methodsFor:'testing'! |
129 |
||
130 |
isAbstract |
|
131 |
^ self == PositionableStream |
|
132 |
! ! |
|
133 |
||
24944 | 134 |
|
7261 | 135 |
!PositionableStream methodsFor:'Compatibility-Dolphin'! |
6413 | 136 |
|
137 |
endChunk |
|
138 |
self nextPutChunkSeparator |
|
139 |
! ! |
|
140 |
||
9284 | 141 |
!PositionableStream methodsFor:'Compatibility-ST/V'! |
142 |
||
143 |
skipTo:anElement |
|
144 |
"ST/V compatibility: |
|
145 |
skip for the element given by the argument, anElement; |
|
146 |
return nil if not found, self otherwise. |
|
147 |
On a successful match, the next read will return the element after anElement." |
|
148 |
||
149 |
^ self skipThrough:anElement |
|
150 |
||
151 |
" |
|
152 |
|s| |
|
153 |
s := ReadStream on:'12345678901234567890'. |
|
154 |
s skipTo:$5. |
|
155 |
s copyFrom:1 to:(s position). |
|
156 |
s upToEnd |
|
157 |
" |
|
158 |
! ! |
|
159 |
||
16876
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
160 |
!PositionableStream methodsFor:'Compatibility-Squeak'! |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
161 |
|
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
162 |
back |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
163 |
"Go back one element and return it." |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
164 |
|
24744 | 165 |
self position == 0 ifTrue: [self positionError]. |
16876
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
166 |
self skip: -1. |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
167 |
^ self peek |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
168 |
|
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
169 |
"Created: / 03-10-2014 / 03:06:31 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
170 |
! |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
171 |
|
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
172 |
peekBack |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
173 |
"Return the element at the previous position, without changing position. Use indirect messages in case self is a StandardFileStream." |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
174 |
|
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
175 |
| element | |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
176 |
element := self back. |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
177 |
self skip: 1. |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
178 |
^ element |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
179 |
|
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
180 |
"Created: / 03-10-2014 / 03:06:26 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
181 |
! ! |
7e3ad01ca413
Added: #back / #peekBack for Squeak compatibility.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16840
diff
changeset
|
182 |
|
1 | 183 |
!PositionableStream methodsFor:'accessing'! |
184 |
||
7686
290cda6075ac
String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents:
7619
diff
changeset
|
185 |
collection |
19591 | 186 |
"return the underlying collection buffer. |
187 |
Notice, that this buffer may become invalid after being retrieved, |
|
188 |
if more data is written to the stream (because a bigger buffer might be |
|
189 |
allocated). Therefore, it should be only used in special situations, |
|
22404 | 190 |
where an already filled buffer needs to be backpatched later |
19591 | 191 |
(eg. before being sent out to some external stream or socket)" |
192 |
||
7686
290cda6075ac
String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents:
7619
diff
changeset
|
193 |
^ collection |
22404 | 194 |
|
195 |
"Modified (comment): / 02-01-2018 / 19:54:37 / stefan" |
|
7686
290cda6075ac
String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents:
7619
diff
changeset
|
196 |
! |
290cda6075ac
String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents:
7619
diff
changeset
|
197 |
|
1 | 198 |
contents |
199 |
"return the entire contents of the stream" |
|
200 |
||
201 |
^ collection |
|
202 |
! |
|
203 |
||
2432 | 204 |
peekForAll:aCollection |
205 |
"return true and advance if the next elements are the same |
|
206 |
as aCollection. |
|
207 |
otherwise stay and let the position unchanged" |
|
208 |
||
209 |
|oldPos| |
|
210 |
||
211 |
oldPos := self position. |
|
212 |
(self next:(aCollection size)) = aCollection ifTrue:[ |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6924
diff
changeset
|
213 |
^ true |
2432 | 214 |
]. |
215 |
self position:oldPos. |
|
216 |
^ false |
|
217 |
||
218 |
"Created: 1.3.1997 / 15:11:25 / cg" |
|
219 |
! |
|
220 |
||
3905
682fc38fd339
added read-access to readLimit.
Claus Gittinger <cg@exept.de>
parents:
3604
diff
changeset
|
221 |
readLimit |
20423 | 222 |
"return the read-limit; that's the position at which EOF is returned" |
3905
682fc38fd339
added read-access to readLimit.
Claus Gittinger <cg@exept.de>
parents:
3604
diff
changeset
|
223 |
|
682fc38fd339
added read-access to readLimit.
Claus Gittinger <cg@exept.de>
parents:
3604
diff
changeset
|
224 |
^ readLimit |
682fc38fd339
added read-access to readLimit.
Claus Gittinger <cg@exept.de>
parents:
3604
diff
changeset
|
225 |
|
682fc38fd339
added read-access to readLimit.
Claus Gittinger <cg@exept.de>
parents:
3604
diff
changeset
|
226 |
"Created: / 30.10.1998 / 16:47:04 / cg" |
682fc38fd339
added read-access to readLimit.
Claus Gittinger <cg@exept.de>
parents:
3604
diff
changeset
|
227 |
! |
682fc38fd339
added read-access to readLimit.
Claus Gittinger <cg@exept.de>
parents:
3604
diff
changeset
|
228 |
|
1 | 229 |
readLimit:aNumber |
20423 | 230 |
"set the read-limit; that's the position at which EOF is returned" |
1 | 231 |
|
232 |
readLimit := aNumber |
|
369 | 233 |
! |
234 |
||
14934 | 235 |
setCollection:newCollection |
236 |
collection := newCollection |
|
237 |
! |
|
238 |
||
369 | 239 |
writeLimit:aNumber |
20423 | 240 |
"set the writeLimit; that's the position after which writing is prohibited" |
369 | 241 |
|
242 |
writeLimit := aNumber |
|
10592 | 243 |
|
244 |
" |
|
245 |
|s| |
|
246 |
s := WriteStream on:String new. |
|
247 |
s nextPutAll:'hello world'. |
|
248 |
s contents. |
|
249 |
||
250 |
|s| |
|
251 |
s := WriteStream on:String new. |
|
252 |
s writeLimit:5. |
|
253 |
s nextPutAll:'hello world'. |
|
254 |
s contents. |
|
255 |
||
256 |
" |
|
257 |
||
258 |
"Modified: / 04-06-2007 / 17:21:55 / cg" |
|
1 | 259 |
! ! |
260 |
||
24927 | 261 |
!PositionableStream methodsFor:'comparing'! |
262 |
||
263 |
endsBeforePositionWith:aSequenceableCollection |
|
264 |
"answer true, if the elements in aSequenceableCollection |
|
265 |
are at the current end of the stream up to position." |
|
266 |
||
267 |
|sz pos| |
|
268 |
||
269 |
sz := aSequenceableCollection size. |
|
270 |
pos := self position. |
|
271 |
pos < sz ifTrue:[ |
|
272 |
^ false. |
|
273 |
]. |
|
274 |
self contentsSpecies == collection class ifTrue:[ |
|
275 |
^ collection sameContentsFrom:pos+1-sz to:pos as:aSequenceableCollection startingAt:1. |
|
276 |
]. |
|
277 |
self position:pos-sz. |
|
278 |
^ (self next:sz) = aSequenceableCollection. |
|
279 |
||
280 |
" |
|
281 |
('' writeStream nextPutAll:'Hello World') endsBeforePositionWith:'World' |
|
282 |
('' writeStream nextPutAll:'Hello World') endsBeforePositionWith:'Hello World' |
|
283 |
('' writeStream nextPutAll:'Hello World') endsBeforePositionWith:'Hello Worldx' |
|
284 |
('' writeStream nextPutAll:'Hello World') endsBeforePositionWith:'Bla' |
|
285 |
('' writeStream) endsBeforePositionWith:'Bla' |
|
286 |
('' writeStream) endsBeforePositionWith:'' |
|
287 |
'' endsWith:'' |
|
288 |
" |
|
289 |
! |
|
290 |
||
291 |
isContentsEqualTo:aSequenceableCollection |
|
292 |
"answer true, if the contents of myself from the current position |
|
293 |
to the end is equal to aStringOrByteArray." |
|
294 |
||
295 |
|sz pos| |
|
296 |
||
297 |
sz := aSequenceableCollection size. |
|
298 |
self remainingSize ~= sz ifTrue:[ |
|
299 |
^ false. |
|
300 |
]. |
|
301 |
||
302 |
self contentsSpecies == collection class ifTrue:[ |
|
303 |
pos := self position. |
|
304 |
^ collection sameContentsFrom:pos+1-sz to:pos as:aSequenceableCollection startingAt:1. |
|
305 |
]. |
|
306 |
||
307 |
^ super isContentsEqualTo:aSequenceableCollection. |
|
308 |
||
309 |
"Created: / 18-11-2019 / 16:04:59 / Stefan Vogel" |
|
310 |
! ! |
|
311 |
||
21136 | 312 |
!PositionableStream methodsFor:'non homogenous reading'! |
313 |
||
314 |
nextBytes:numBytes into:aCollection startingAt:initialIndex |
|
315 |
"return the next numBytes from the stream. If the end is |
|
22514 | 316 |
reached before, only that many bytes are copied into the |
21136 | 317 |
collection. |
318 |
Returns the number of bytes that have been actually read. |
|
319 |
The receiver must support reading of binary bytes. |
|
320 |
||
321 |
Notice: this method is provided here for protocol completeness |
|
322 |
with externalStreams - it is normally not used with other |
|
323 |
streams." |
|
324 |
||
325 |
|max| |
|
326 |
||
22514 | 327 |
(collection isSingleByteCollection |
22235 | 328 |
and:[aCollection class == collection class or:[aCollection isSingleByteCollection]] |
329 |
) ifTrue:[ |
|
21136 | 330 |
"do it the fast way" |
331 |
max := (readLimit - position) min: numBytes. |
|
332 |
aCollection |
|
22514 | 333 |
replaceBytesFrom:initialIndex |
21136 | 334 |
to:(initialIndex + max - 1) |
22514 | 335 |
with:collection |
21136 | 336 |
startingAt:position+1. |
337 |
position := position + max. |
|
338 |
^ max |
|
339 |
]. |
|
340 |
"do it the hard way" |
|
341 |
^ super nextBytes:numBytes into:aCollection startingAt:initialIndex |
|
342 |
||
343 |
" |
|
344 |
|s n buffer| |
|
345 |
||
346 |
buffer := ByteArray new:10. |
|
347 |
||
348 |
s := ReadStream on:#[1 2 3 4 5 6 7 8 9]. |
|
349 |
s next:3. |
|
350 |
n := s nextBytes:9 into:buffer startingAt:1. |
|
351 |
Transcript showCR:('n = %1; buffer = <%2>' bindWith:n with:buffer) |
|
352 |
" |
|
22514 | 353 |
|
354 |
"Modified (comment): / 29-01-2018 / 14:44:20 / mawalch" |
|
21136 | 355 |
! ! |
356 |
||
24922 | 357 |
!PositionableStream methodsFor:'obsolete positioning'! |
358 |
||
359 |
position0Based |
|
360 |
<resource: #obsolete> |
|
361 |
"return the read position 0-based" |
|
362 |
||
363 |
^ self position |
|
364 |
! |
|
365 |
||
366 |
position1Based |
|
367 |
<resource: #obsolete> |
|
368 |
"return the read position 1-based" |
|
369 |
||
370 |
^ self position + 1 |
|
371 |
! ! |
|
372 |
||
611 | 373 |
!PositionableStream methodsFor:'positioning'! |
374 |
||
375 |
backStep |
|
376 |
"move backward read position by one" |
|
377 |
||
4405 | 378 |
self position:(self position - 1) |
611 | 379 |
! |
380 |
||
16188 | 381 |
match: subCollection |
382 |
"Set the access position of the receiver to be past the next occurrence of the subCollection. |
|
383 |
Answer whether subCollection is found. |
|
384 |
No wildcards, and case does matter." |
|
385 |
||
386 |
| pattern startMatch | |
|
387 |
||
388 |
pattern := ReadStream on: subCollection. |
|
389 |
startMatch := nil. |
|
390 |
[pattern atEnd] whileFalse: [ |
|
391 |
self atEnd ifTrue: [^ false]. |
|
392 |
(self next) = (pattern next) |
|
24766 | 393 |
ifTrue: [pattern position == 1 ifTrue: [startMatch := self position]] |
16188 | 394 |
ifFalse: [ |
395 |
pattern position: 0. |
|
396 |
startMatch ifNotNil: [ |
|
397 |
self position: startMatch. |
|
398 |
startMatch := nil |
|
399 |
] |
|
400 |
] |
|
401 |
]. |
|
402 |
^ true |
|
403 |
||
404 |
" |
|
405 |
'abc def ghi' readStream match:'def'; upToEnd |
|
406 |
" |
|
407 |
! |
|
408 |
||
611 | 409 |
position |
15615 | 410 |
"return the read position (0-based)" |
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
411 |
|
15348 | 412 |
^ position |
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
413 |
! |
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
414 |
|
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
415 |
position0Based:index0Based |
15615 | 416 |
<resource: #obsolete> |
4405 | 417 |
"set the read (or write) position" |
611 | 418 |
|
15615 | 419 |
self position:index0Based |
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
420 |
! |
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
421 |
|
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
422 |
position1Based:index1Based |
15615 | 423 |
<resource: #obsolete> |
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
424 |
"set the read (or write) position" |
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
425 |
|
15615 | 426 |
self position:(index1Based - 1) |
4405 | 427 |
|
428 |
" |
|
429 |
|s| |
|
430 |
||
431 |
s := '1234567890' readStream. |
|
432 |
s next:5. |
|
433 |
s position:1. |
|
434 |
s next:7. |
|
435 |
" |
|
436 |
||
437 |
" |
|
438 |
|s| |
|
439 |
||
440 |
s := '' writeStream. |
|
441 |
s nextPutAll:'1234567890'. |
|
442 |
s position:5. |
|
443 |
s nextPutAll:'abcdefg'. |
|
444 |
s contents |
|
445 |
" |
|
446 |
||
447 |
" |
|
448 |
|s| |
|
449 |
||
450 |
s := '' writeStream. |
|
451 |
s nextPutAll:'1234567890'. |
|
452 |
s position:0. |
|
453 |
s nextPutAll:'abcdefg'. |
|
454 |
s contents |
|
455 |
" |
|
611 | 456 |
! |
457 |
||
15615 | 458 |
position:index0Based |
7052
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
459 |
"set the read (or write) position" |
207cc9c62982
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
460 |
|
15615 | 461 |
((index0Based > readLimit) or:[index0Based < 0]) ifTrue: [^ self positionError:index0Based]. |
462 |
position := index0Based |
|
6626
f7b08d6bee3f
#positionStartingAt0 and #positionStartingAt1
Stefan Vogel <sv@exept.de>
parents:
6538
diff
changeset
|
463 |
! |
f7b08d6bee3f
#positionStartingAt0 and #positionStartingAt1
Stefan Vogel <sv@exept.de>
parents:
6538
diff
changeset
|
464 |
|
611 | 465 |
reset |
466 |
"set the read position to the beginning of the collection" |
|
467 |
||
19885 | 468 |
position := 0. |
5308 | 469 |
|
470 |
" |
|
471 |
|s| |
|
472 |
||
473 |
s := 'hello world' readStream. |
|
474 |
Transcript showCR:(s next:5). |
|
475 |
s reset. |
|
476 |
Transcript showCR:(s next:10). |
|
477 |
" |
|
478 |
! |
|
479 |
||
480 |
resetPosition |
|
19885 | 481 |
<resource: #obsolete> |
5308 | 482 |
"set the read position to the beginning of the collection" |
483 |
||
15348 | 484 |
position := 0 |
4405 | 485 |
|
486 |
" |
|
487 |
|s| |
|
488 |
||
489 |
s := 'hello world' readStream. |
|
490 |
Transcript showCR:(s next:5). |
|
491 |
s reset. |
|
492 |
Transcript showCR:(s next:10). |
|
493 |
" |
|
611 | 494 |
! |
495 |
||
496 |
setToEnd |
|
11433 | 497 |
"set the read position to the end of the collection. |
498 |
#next will return EOF, #nextPut: will append to the stream. |
|
499 |
(same Behavior as FileStream." |
|
611 | 500 |
|
15348 | 501 |
position := readLimit |
611 | 502 |
! |
503 |
||
504 |
skip:numberToSkip |
|
505 |
"skip the next numberToSkip elements" |
|
506 |
||
5774 | 507 |
numberToSkip ~~ 0 ifTrue:[ |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6924
diff
changeset
|
508 |
self position:(self position + numberToSkip) |
5774 | 509 |
] |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
510 |
! |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
511 |
|
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
512 |
skipThroughAll:aCollection |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
513 |
"skip for and through the sequence given by the argument, aCollection; |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
514 |
return nil if not found, self otherwise. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
515 |
On a successful match, the next read will return elements after aCollection; |
2153
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
516 |
if no match was found, the receiver will be positioned at the end. |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
517 |
This is redefined here, to make use of positioning." |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
518 |
|
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
519 |
|buffer len first idx| |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
520 |
|
16186 | 521 |
self isPositionable ifFalse:[ |
522 |
"/ for non-positionable subclasses |
|
523 |
^ super skipThroughAll:aCollection |
|
524 |
]. |
|
525 |
||
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
526 |
len := aCollection size. |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
527 |
first := aCollection at:1. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
528 |
[self atEnd] whileFalse:[ |
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
529 |
buffer := self nextAvailable:len. |
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
530 |
buffer size == len ifTrue:[ |
16209 | 531 |
buffer = aCollection ifTrue:[ |
532 |
^ self |
|
533 |
]. |
|
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
534 |
"expect more input" |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
535 |
idx := buffer indexOf:first startingAt:2. |
16209 | 536 |
idx ~~ 0 ifTrue:[ |
537 |
self skip:(idx - len - 1) |
|
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
538 |
]. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
539 |
]. |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
540 |
]. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
541 |
^ nil |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
542 |
|
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
543 |
" |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
544 |
|s| |
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
545 |
s := ReadStream on:'12345678901234567890a'. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
546 |
s skipThroughAll:'90ab'. |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
547 |
s upToEnd |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
548 |
" |
2153
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
549 |
" |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
550 |
|s| |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
551 |
s := ReadStream on:'12345678901234567890'. |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
552 |
s skipThroughAll:'1234'. |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
553 |
s upToEnd |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
554 |
" |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
555 |
" |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
556 |
|s| |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
557 |
s := ReadStream on:'12345678901234567890'. |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
558 |
s skipThroughAll:'999'. |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
559 |
s atEnd |
244e36cbbd9b
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2130
diff
changeset
|
560 |
" |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
561 |
|
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
562 |
"Created: 26.6.1996 / 09:35:35 / cg" |
2154
89e96599e1e2
skipThroughAll: does not need the old position
Claus Gittinger <cg@exept.de>
parents:
2153
diff
changeset
|
563 |
"Modified: 11.1.1997 / 19:16:38 / cg" |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
564 |
! |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
565 |
|
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
566 |
skipToAll:aCollection |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
567 |
"skip for the sequence given by the argument, aCollection; |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
568 |
return nil if not found, self otherwise. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
569 |
On a successful match, the next read will return elements of aCollection." |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
570 |
|
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
571 |
|oldPos buffer len first idx| |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
572 |
|
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
573 |
oldPos := self position. |
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
574 |
len := aCollection size. |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
575 |
first := aCollection at:1. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
576 |
[self atEnd] whileFalse:[ |
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
577 |
buffer := self nextAvailable:len. |
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
578 |
buffer = aCollection ifTrue:[ |
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
579 |
self position:(self position - len). |
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
580 |
^ self |
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7053
diff
changeset
|
581 |
]. |
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
582 |
buffer size == len ifTrue:[ |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
583 |
"more input can be expected" |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
584 |
idx := buffer indexOf:first startingAt:2. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
585 |
idx == 0 ifFalse:[ |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
586 |
self position:(self position - len + idx - 1) |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
587 |
]. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
588 |
]. |
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
589 |
]. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
590 |
self position:oldPos. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
591 |
^ nil |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
592 |
|
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
593 |
" |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
594 |
|s| |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
595 |
s := ReadStream on:'12345678901234567890'. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
596 |
s skipToAll:'901'. |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
597 |
s upToEnd |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
598 |
" |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
599 |
|
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
600 |
" |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
601 |
|s| |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
602 |
s := ReadStream on:'1234567890'. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
603 |
s skipToAll:'901'. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
604 |
s upToEnd |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
605 |
" |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
606 |
|
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
607 |
" |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
608 |
|s| |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
609 |
s := 'Makefile' asFilename readStream. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
610 |
[ |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
611 |
(s skipToAll:'EDIT') notNil ifTrue:[ |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
612 |
s next:100. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
613 |
]. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
614 |
] ensure:[ |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
615 |
s close. |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
616 |
] |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
617 |
" |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
618 |
|
1489
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
619 |
"Modified: 26.6.1996 / 09:28:27 / cg" |
9e3b166483fb
moved some skip methods from readStream;
Claus Gittinger <cg@exept.de>
parents:
1420
diff
changeset
|
620 |
"Created: 26.6.1996 / 09:35:06 / cg" |
44 | 621 |
! ! |
611 | 622 |
|
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
623 |
!PositionableStream methodsFor:'printing & storing'! |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
624 |
|
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
625 |
printOn:aStream |
25266 | 626 |
|sz| |
627 |
||
24919 | 628 |
aStream |
629 |
nextPutAll:self className; |
|
630 |
nextPutAll:'(on:'. |
|
25266 | 631 |
(collection isString and:[(sz := self collectionSize) < 80]) ifTrue:[ |
632 |
aStream nextPutAll:(collection copyTo:sz) storeString |
|
24919 | 633 |
] ifFalse:[ |
634 |
aStream nextPutAll:collection classNameWithArticle |
|
635 |
]. |
|
25266 | 636 |
aStream nextPut:$|; print:position. |
24919 | 637 |
aStream nextPut:$) |
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
638 |
|
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
639 |
" |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
640 |
'' readStream printString |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
641 |
'' writeStream printString |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
642 |
" |
25266 | 643 |
|
644 |
"Modified: / 18-02-2020 / 23:37:40 / Stefan Vogel" |
|
10357
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
645 |
! ! |
66543e405aa4
Fix #skipToAll: (partial match at end of stream
Stefan Vogel <sv@exept.de>
parents:
10001
diff
changeset
|
646 |
|
611 | 647 |
!PositionableStream methodsFor:'private'! |
648 |
||
649 |
on:aCollection |
|
650 |
"setup for streaming on aCollection" |
|
651 |
||
652 |
collection := aCollection. |
|
653 |
readLimit := aCollection size. |
|
13379 | 654 |
readLimit == 0 ifTrue:[ |
655 |
self assert:(aCollection isCollection) |
|
656 |
]. |
|
15348 | 657 |
position := 0 |
611 | 658 |
! |
659 |
||
660 |
on:aCollection from:first to:last |
|
661 |
"setup for streaming on aCollection from first to last" |
|
662 |
||
663 |
collection := aCollection. |
|
15348 | 664 |
position := first - 1. |
611 | 665 |
readLimit := last |
666 |
! |
|
667 |
||
668 |
positionError |
|
1998 | 669 |
"{ Pragma: +optSpace }" |
670 |
||
2966 | 671 |
"report an error when positioning past the end |
672 |
or before the beginning." |
|
611 | 673 |
|
8320
229adaeaf183
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
8318
diff
changeset
|
674 |
^ PositionOutOfBoundsError raiseRequestWith:nil |
4452
3d050f505a0a
Raise proceedable signals proceedable.
Stefan Vogel <sv@exept.de>
parents:
4416
diff
changeset
|
675 |
|
3d050f505a0a
Raise proceedable signals proceedable.
Stefan Vogel <sv@exept.de>
parents:
4416
diff
changeset
|
676 |
"Modified: / 26.7.1999 / 10:59:13 / stefan" |
611 | 677 |
! |
678 |
||
6314 | 679 |
positionError:badPostition |
680 |
"{ Pragma: +optSpace }" |
|
681 |
||
682 |
"report an error when positioning past the end |
|
683 |
or before the beginning." |
|
684 |
||
8320
229adaeaf183
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
8318
diff
changeset
|
685 |
^ PositionOutOfBoundsError raiseRequestWith:badPostition |
6314 | 686 |
! |
687 |
||
611 | 688 |
with:aCollection |
689 |
"setup for streaming to the end of aCollection" |
|
690 |
||
691 |
collection := aCollection. |
|
11434 | 692 |
readLimit := aCollection size. |
611 | 693 |
self setToEnd |
11434 | 694 |
|
695 |
" |
|
696 |
(WriteStream with:#(1 2 3 4 5)) |
|
697 |
nextPut:6; |
|
698 |
contents |
|
699 |
" |
|
611 | 700 |
! ! |
701 |
||
16195 | 702 |
!PositionableStream methodsFor:'queries'! |
703 |
||
24927 | 704 |
collectionSize |
705 |
^ self subclassResponsibility. |
|
16195 | 706 |
|
24927 | 707 |
"Created: / 18-11-2019 / 16:09:57 / Stefan Vogel" |
708 |
! |
|
16195 | 709 |
|
24944 | 710 |
contentsSpecies |
711 |
"return a class of which instances will be returned, when |
|
712 |
parts of the collection are asked for. |
|
713 |
(see upTo-kind of methods in Stream)" |
|
714 |
||
715 |
^ collection species |
|
716 |
! |
|
717 |
||
24927 | 718 |
remainingSize |
719 |
^ self collectionSize - position. |
|
720 |
||
721 |
"Created: / 18-11-2019 / 16:07:14 / Stefan Vogel" |
|
16195 | 722 |
! ! |
723 |
||
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
724 |
!PositionableStream methodsFor:'reading'! |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
725 |
|
9003 | 726 |
nextAvailable:count |
22204 | 727 |
"return the next count elements of the stream as aCollection. |
728 |
If the stream reaches the end before count elements have been read, |
|
729 |
return what is available. (i.e. a shorter collection)." |
|
730 |
||
17606 | 731 |
collection notNil ifTrue:[ |
732 |
|end result| |
|
9003 | 733 |
|
17606 | 734 |
end := position + count. |
735 |
end >= readLimit ifTrue:[ |
|
736 |
end := readLimit. |
|
737 |
]. |
|
738 |
||
739 |
result := collection copyFrom:position+1 to:end. |
|
740 |
position := end. |
|
741 |
^ result. |
|
9003 | 742 |
]. |
743 |
||
17606 | 744 |
^ super nextAvailable:count. |
9003 | 745 |
|
746 |
" |
|
747 |
'abc' readStream nextAvailable:1. |
|
748 |
'abc' readStream nextAvailable:2. |
|
749 |
'abc' readStream nextAvailable:3. |
|
750 |
'abc' readStream nextAvailable:4. |
|
751 |
||
752 |
'abc' readStream nextAvailable:2; nextAvailable:2. |
|
753 |
'abc' readStream nextAvailable:3; nextAvailable:3. |
|
754 |
" |
|
22204 | 755 |
|
756 |
"Modified (format): / 22-08-2017 / 20:25:54 / cg" |
|
9003 | 757 |
! |
758 |
||
17611 | 759 |
nextAvailable:count into:aCollection startingAt:initialIndex |
760 |
"return the next count objects from the stream. If the end is |
|
22514 | 761 |
reached before, only that many objects are copied into the |
17611 | 762 |
collection. |
763 |
Returns the number of objects that have been actually read." |
|
764 |
||
765 |
| max | |
|
766 |
||
767 |
collection notNil ifTrue:[ |
|
768 |
max := (readLimit - position) min: count. |
|
769 |
aCollection |
|
770 |
replaceFrom: initialIndex |
|
771 |
to: initialIndex+max-1 |
|
772 |
with: collection |
|
773 |
startingAt: position+1. |
|
774 |
position := position + max. |
|
775 |
^ max. |
|
776 |
]. |
|
777 |
||
778 |
^ super nextAvailable:count into:aCollection startingAt:initialIndex. |
|
779 |
||
780 |
" |
|
781 |
|s n buffer| |
|
782 |
||
783 |
buffer := ByteArray new:10. |
|
784 |
||
785 |
s := ReadStream on:#[1 2 3 4 5 6 7 8 9]. |
|
786 |
s next:3. |
|
787 |
n := s nextBytes:9 into:buffer startingAt:1. |
|
788 |
Transcript showCR:('n = %1; buffer = <%2>' bindWith:n with:buffer) |
|
789 |
" |
|
22514 | 790 |
|
791 |
"Modified (comment): / 29-01-2018 / 14:43:56 / mawalch" |
|
17611 | 792 |
! |
793 |
||
21136 | 794 |
peek |
795 |
"look ahead for and return the next element" |
|
17611 | 796 |
|
21136 | 797 |
|peekObject| |
20691 | 798 |
|
21136 | 799 |
peekObject := self next. |
800 |
self backStep. |
|
801 |
^ peekObject |
|
20691 | 802 |
! |
803 |
||
22003 | 804 |
peek:n |
24842 | 805 |
"look ahead n elements and return them after positioning back to the |
22003 | 806 |
position we had before" |
807 |
||
808 |
|posBefore peekObjects| |
|
809 |
||
810 |
posBefore := self position. |
|
811 |
peekObjects := self next:n. |
|
812 |
self position:posBefore. |
|
813 |
^ peekObjects |
|
814 |
||
815 |
" |
|
816 |
|s| |
|
817 |
||
818 |
s := '1234567890' readStream. |
|
819 |
s peek:4. |
|
820 |
s next:5. |
|
821 |
" |
|
822 |
||
823 |
"Created: / 13-07-2017 / 12:32:03 / cg" |
|
824 |
! |
|
825 |
||
24852 | 826 |
upTo:anObject |
827 |
|anObjectIsCharacter| |
|
828 |
%{ |
|
24865 | 829 |
|
830 |
#include <stdlib.h> |
|
24864 | 831 |
|
24852 | 832 |
if (__isCharacter(anObject)) { |
833 |
unsigned int ch = __intVal(__characterVal(anObject)); |
|
24859
dfd3a41a35d5
Fix stc syntax errors (in last change)
Stefan Vogel <sv@exept.de>
parents:
24852
diff
changeset
|
834 |
OBJ _coll = __INST(collection); |
24852 | 835 |
|
836 |
anObjectIsCharacter = true; |
|
837 |
||
24865 | 838 |
if (__isStringLike(_coll) && ch <= 0xFF) { |
24852 | 839 |
int _startPos = __intVal(__INST(position)); |
24859
dfd3a41a35d5
Fix stc syntax errors (in last change)
Stefan Vogel <sv@exept.de>
parents:
24852
diff
changeset
|
840 |
int _endIndex = __stringSize(_coll); |
dfd3a41a35d5
Fix stc syntax errors (in last change)
Stefan Vogel <sv@exept.de>
parents:
24852
diff
changeset
|
841 |
char *startPtr = __stringVal(_coll) + _startPos; |
24852 | 842 |
char *foundPtr; |
843 |
OBJ rslt; |
|
844 |
size_t n, nMax; |
|
845 |
||
846 |
if (__isInteger(__INST(readLimit))) { |
|
24859
dfd3a41a35d5
Fix stc syntax errors (in last change)
Stefan Vogel <sv@exept.de>
parents:
24852
diff
changeset
|
847 |
int _readLim = __intVal(__INST(readLimit)); |
24852 | 848 |
|
24859
dfd3a41a35d5
Fix stc syntax errors (in last change)
Stefan Vogel <sv@exept.de>
parents:
24852
diff
changeset
|
849 |
if (_readLim < _endIndex) _endIndex = _readLim; |
dfd3a41a35d5
Fix stc syntax errors (in last change)
Stefan Vogel <sv@exept.de>
parents:
24852
diff
changeset
|
850 |
if (_startPos >= _readLim) { |
24852 | 851 |
RETURN(__MKEMPTYSTRING(0)); |
852 |
} |
|
853 |
} |
|
854 |
||
855 |
nMax = _endIndex - _startPos; |
|
856 |
foundPtr = (char *) memchr(startPtr, ch, nMax); |
|
857 |
if (foundPtr == 0) { |
|
858 |
// not found - copy to end |
|
859 |
n = nMax; |
|
860 |
__INST(position) = __mkSmallInteger(_endIndex); |
|
861 |
} else { |
|
862 |
// found at foundPtr |
|
863 |
n = foundPtr - startPtr; |
|
864 |
__INST(position) = __mkSmallInteger(_startPos + n + 1); |
|
865 |
} |
|
866 |
||
867 |
rslt = __MKEMPTYSTRING(n); |
|
868 |
// refetch - may GC |
|
24859
dfd3a41a35d5
Fix stc syntax errors (in last change)
Stefan Vogel <sv@exept.de>
parents:
24852
diff
changeset
|
869 |
_coll = __INST(collection); |
dfd3a41a35d5
Fix stc syntax errors (in last change)
Stefan Vogel <sv@exept.de>
parents:
24852
diff
changeset
|
870 |
memcpy((void *)__stringVal(rslt), (void *)(__stringVal(_coll) + _startPos), n); |
24852 | 871 |
|
872 |
RETURN (rslt); |
|
873 |
} |
|
874 |
} |
|
875 |
%}. |
|
876 |
(anObjectIsCharacter == true and:[collection isString]) ifTrue:[ |
|
877 |
"slightly less optimized version for UnicodeStrings" |
|
878 |
|pos1 pos2| |
|
879 |
||
880 |
position >= readLimit ifTrue:[ |
|
881 |
"atEnd" |
|
882 |
^ collection copyEmpty. |
|
883 |
]. |
|
884 |
pos1 := position + 1. |
|
885 |
pos2 := collection indexOf:anObject startingAt:pos1 endingAt:readLimit. |
|
886 |
pos2 == 0 ifTrue:[ |
|
887 |
pos2 := readLimit. |
|
888 |
]. |
|
889 |
position := pos2. |
|
24998 | 890 |
^ collection copyFrom:pos1 to:pos2-1. |
24852 | 891 |
]. |
892 |
||
893 |
"do it the hard way..." |
|
894 |
^ super upTo:anObject |
|
895 |
||
896 |
" |
|
24998 | 897 |
'12345678' readStream upTo:$5; upToEnd |
898 |
'12345678' asUnicodeString readStream upTo:$5; upToEnd |
|
24852 | 899 |
'12345678' readStream upTo:$z. |
900 |
'12345678' asUnicodeString readStream upTo:$z. |
|
901 |
" |
|
902 |
||
903 |
"Modified: / 22-03-2019 / 03:01:19 / Claus Gittinger" |
|
24998 | 904 |
"Modified (comment): / 27-11-2019 / 17:23:25 / Stefan Vogel" |
24852 | 905 |
! |
906 |
||
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
907 |
upToAll:aCollection |
16186 | 908 |
"read until a subcollection consisisting of the elements in aCollection is encountered. |
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
909 |
Return everything read excluding the elements in aCollection. |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
910 |
The position is left before the collection; i.e. the next |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
911 |
read operations will return those elements. |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
912 |
If no such subcollection is encountered, all elements up to the end |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
913 |
are read and returned. |
6494 | 914 |
See also #throughAll: which also reads up to some objects |
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
915 |
but positions behind it and DOES include it in the returned |
3184
d99302322139
Ok, #upToAll: positions after the collection.
Stefan Vogel <sv@exept.de>
parents:
3170
diff
changeset
|
916 |
collection. |
6494 | 917 |
See also #upToAllExcluding:, which returns the same, but leaves the |
918 |
read pointer after the matched subcollection. |
|
3184
d99302322139
Ok, #upToAll: positions after the collection.
Stefan Vogel <sv@exept.de>
parents:
3170
diff
changeset
|
919 |
|
d99302322139
Ok, #upToAll: positions after the collection.
Stefan Vogel <sv@exept.de>
parents:
3170
diff
changeset
|
920 |
Note: this behavior is inconsistent with the other upTo.. methods, |
15159
b94bdee97afa
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
14934
diff
changeset
|
921 |
which position after the found item. We implement the method |
b94bdee97afa
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
14934
diff
changeset
|
922 |
this way for the sake of ST80-compatibility." |
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
923 |
|
17611 | 924 |
"/ in the future, this will have the Squeak semantics, which is upToAllExcluding: |
16189 | 925 |
|
926 |
^ self upToAll_positionBefore:aCollection |
|
927 |
||
928 |
" |
|
929 |
|s| |
|
930 |
s := ReadStream on:'hello world'. |
|
931 |
Transcript show:'<'; show:(s upToAll:'wo'); showCR:'>'. |
|
932 |
Transcript showCR:s atEnd. |
|
933 |
Transcript show:'<'; show:(s upToEnd); showCR:'>'. |
|
934 |
" |
|
935 |
" |
|
936 |
|s| |
|
937 |
s := ReadStream on:'hello world'. |
|
938 |
Transcript show:'<'; show:(s upToAll:'wo'); showCR:'>'. |
|
939 |
Transcript showCR:s atEnd. |
|
940 |
Transcript show:'<'; show:(s upToAll:'wo'); showCR:'>'. |
|
941 |
" |
|
942 |
" |
|
943 |
|s| |
|
944 |
s := ReadStream on:'hello world'. |
|
945 |
Transcript show:'<'; show:(s upToAll:'xx'); showCR:'>'. |
|
946 |
Transcript showCR:s atEnd. |
|
947 |
Transcript show:'<'; show:(s upToEnd); showCR:'>'. |
|
948 |
" |
|
949 |
||
950 |
"Modified: / 12.1.1998 / 22:06:42 / cg" |
|
951 |
"Created: / 12.1.1998 / 22:07:01 / cg" |
|
952 |
! |
|
953 |
||
954 |
upToAll_positionBefore:aCollection |
|
955 |
"read until a subcollection consisisting of the elements in aCollection is encountered. |
|
956 |
Return everything read excluding the elements in aCollection. |
|
957 |
The position is left before the collection; i.e. the next |
|
958 |
read operations will return those elements. |
|
959 |
If no such subcollection is encountered, all elements up to the end |
|
960 |
are read and returned. |
|
961 |
See also #throughAll: which also reads up to some objects |
|
962 |
but positions behind it and DOES include it in the returned |
|
16840 | 963 |
collection." |
16189 | 964 |
|
16209 | 965 |
|answerStream element last| |
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
966 |
|
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
967 |
last := aCollection last. |
22410 | 968 |
answerStream := self contentsSpecies writeStream. |
16209 | 969 |
[(element := self nextOrNil) notNil] whileTrue:[ |
15159
b94bdee97afa
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
14934
diff
changeset
|
970 |
answerStream nextPut:element. |
16209 | 971 |
(element = last and:[answerStream endsBeforePositionWith:aCollection]) ifTrue:[ |
972 |
|backStep| |
|
973 |
backStep := aCollection size negated. |
|
974 |
self skip:backStep. |
|
975 |
answerStream skip:backStep. |
|
976 |
^ answerStream contents |
|
15159
b94bdee97afa
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
14934
diff
changeset
|
977 |
]. |
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
978 |
]. |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
979 |
^ answerStream contents |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
980 |
|
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
981 |
" |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
982 |
|s| |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
983 |
s := ReadStream on:'hello world'. |
16209 | 984 |
Transcript show:'<'; show:(s upToAll_positionBefore:'wo'); showCR:'>'. |
6494 | 985 |
Transcript showCR:s atEnd. |
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
986 |
Transcript show:'<'; show:(s upToEnd); showCR:'>'. |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
987 |
" |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
988 |
" |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
989 |
|s| |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
990 |
s := ReadStream on:'hello world'. |
16209 | 991 |
Transcript show:'<'; show:(s upToAll_positionBefore:'wo'); showCR:'>'. |
16186 | 992 |
Transcript showCR:s atEnd. |
16209 | 993 |
Transcript show:'<'; show:(s upToAll_positionBefore:'wo'); showCR:'>'. |
16186 | 994 |
" |
995 |
" |
|
996 |
|s| |
|
997 |
s := ReadStream on:'hello world'. |
|
16209 | 998 |
Transcript show:'<'; show:(s upToAll_positionBefore:'xx'); showCR:'>'. |
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
999 |
Transcript showCR:s atEnd. |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
1000 |
Transcript show:'<'; show:(s upToEnd); showCR:'>'. |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
1001 |
" |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
1002 |
|
22410 | 1003 |
"Created: / 12-01-1998 / 22:07:01 / cg" |
1004 |
"Modified: / 10-01-2018 / 18:30:07 / stefan" |
|
20101 | 1005 |
! |
1006 |
||
1007 |
upToEnd |
|
1008 |
collection notNil ifTrue:[ |
|
1009 |
|result| |
|
1010 |
||
1011 |
result := collection copyFrom:position+1 to:readLimit. |
|
1012 |
position := readLimit. |
|
1013 |
^ result. |
|
1014 |
]. |
|
1015 |
||
1016 |
^ super upToEnd. |
|
1017 |
||
1018 |
" |
|
1019 |
'abc' readStream upToEnd. |
|
1020 |
" |
|
3170
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
1021 |
! ! |
7b299d83c45a
moved upToAll: to positionableStream.
Claus Gittinger <cg@exept.de>
parents:
3032
diff
changeset
|
1022 |
|
21136 | 1023 |
!PositionableStream methodsFor:'reading-strings'! |
1024 |
||
1025 |
nextLine |
|
1026 |
"return the characters upTo (but excluding) the next cr (carriage return) |
|
1027 |
character (i.e. read a single line of text). |
|
1028 |
If the previous-to-last character is a cr, this is also removed, |
|
1029 |
so it's possible to read alien (i.e. ms-dos) text as well. |
|
1030 |
Added for protocol compatibility with externalStreams." |
|
1031 |
||
1032 |
|start "{ Class:SmallInteger }" |
|
1033 |
end "{ Class:SmallInteger }"| |
|
1034 |
||
1035 |
collection isString ifTrue:[ |
|
1036 |
position == readLimit ifTrue:[ |
|
1037 |
^ self pastEndRead |
|
1038 |
]. |
|
1039 |
start := position+1. |
|
1040 |
end := collection indexOf:Character cr startingAt:start. |
|
1041 |
||
1042 |
(end == 0 or:[end > readLimit]) ifTrue:[ |
|
1043 |
end := position := readLimit. |
|
1044 |
] ifFalse:[ |
|
1045 |
position := end. |
|
1046 |
end := end - 1. "skip lf" |
|
1047 |
]. |
|
1048 |
start > end ifTrue:[ |
|
1049 |
^ ''. |
|
1050 |
]. |
|
1051 |
(collection at:end) == Character return ifTrue:[ |
|
1052 |
end := end - 1. "skip return" |
|
1053 |
]. |
|
1054 |
^ collection copyFrom:start to:end. |
|
1055 |
]. |
|
1056 |
^ super nextLine. |
|
1057 |
||
1058 |
" |
|
1059 |
'12345678' readStream nextLine |
|
1060 |
'12345678' allBold readStream nextLine |
|
1061 |
'12\34\56\78' withCRs readStream nextLine |
|
1062 |
'12\34\56\78' withCRs readStream nextLine; nextLine |
|
1063 |
(ReadStream on:('12\34\56\78' withCRs) from:1 to:4) nextLine; nextLine |
|
1064 |
('12\' withCRs, Character return, '34') readStream nextLine; nextLine |
|
1065 |
Character cr asString readStream nextLine |
|
1066 |
Character return asString readStream nextLine |
|
1067 |
(Character return, Character cr) asString readStream nextLine |
|
1068 |
Character return asString readStream nextLine; nextLine |
|
1069 |
" |
|
1070 |
! ! |
|
1071 |
||
23895 | 1072 |
!PositionableStream methodsFor:'stream-to-stream copy'! |
1073 |
||
1074 |
copy:numberOfElementsOrNil into:aWriteStream |
|
1075 |
"read from the receiver, and write numberOfElementsOrNil data to another aWriteStream. |
|
1076 |
If numberOfElementsOrNil is nil, copy until the end of myself. |
|
1077 |
Return the number of elements which have been transferred. |
|
1078 |
Redefined here to avoid intermediate buffers/garbage." |
|
1079 |
||
1080 |
|endPosition cnt| |
|
1081 |
||
1082 |
collection notNil ifTrue:[ |
|
1083 |
numberOfElementsOrNil isNil ifTrue:[ |
|
1084 |
endPosition := readLimit. "/ read until end |
|
1085 |
] ifFalse:[ |
|
1086 |
endPosition := (position+numberOfElementsOrNil) min:readLimit. |
|
1087 |
]. |
|
1088 |
aWriteStream nextPutAll:collection startingAt:position+1 to:endPosition. |
|
1089 |
cnt := endPosition - position. |
|
1090 |
position := endPosition. |
|
1091 |
^ cnt. |
|
1092 |
]. |
|
1093 |
||
1094 |
^ super copy:numberOfElementsOrNil into:aWriteStream. |
|
1095 |
||
1096 |
||
1097 |
" |
|
1098 |
'hello world' readStream copy:5 into:'/tmp/mist' asFilename writeStream. |
|
1099 |
'hello world' readStream |
|
1100 |
copy:5 into:Transcript; |
|
1101 |
copy:20 into:Transcript. |
|
1102 |
'hello world' readStream copy:5 into:'' writeStream inspect. |
|
1103 |
#[1 2 3 4 5 6 7] readStream copy:2 into:'/tmp/mist' asFilename writeStream binary. |
|
1104 |
#[1 2 3 4 5 6 7] readStream copy:3 into:#[] writeStream. |
|
1105 |
" |
|
1106 |
||
1107 |
" |
|
1108 |
|rs ws cnt| |
|
1109 |
||
1110 |
ws := #() writeStream. |
|
1111 |
rs := #( 1 2 3 4 a nil true) readWriteStream. |
|
1112 |
rs next. |
|
1113 |
cnt := rs copyToEndInto:ws bufferSize:0. |
|
1114 |
Transcript show:cnt; show:' '; showCR:ws contents. |
|
1115 |
" |
|
1116 |
||
1117 |
"Modified (comment): / 13-03-2019 / 16:59:18 / Stefan Vogel" |
|
1118 |
! |
|
1119 |
||
1120 |
copy:numberOfElementsOrNil into:aWriteStream bufferSize:bufferSize |
|
1121 |
"read from the receiver, and write numberOfElementsOrNil to another aWriteStream. |
|
1122 |
If numberOfElementsOrNil is nil, copy until the end of myself. |
|
1123 |
Return the number of elements which have been transferred. |
|
1124 |
Redefined here to avoid intermediate buffers/garbage |
|
1125 |
- bufferSize does not matter here." |
|
1126 |
||
1127 |
collection notNil ifTrue:[ |
|
1128 |
^ self copy:numberOfElementsOrNil into:aWriteStream. |
|
1129 |
]. |
|
1130 |
^ super copy:numberOfElementsOrNil into:aWriteStream bufferSize:bufferSize. |
|
1131 |
||
1132 |
"Modified (comment): / 13-03-2019 / 17:00:11 / Stefan Vogel" |
|
1133 |
! ! |
|
1134 |
||
611 | 1135 |
!PositionableStream methodsFor:'testing'! |
1136 |
||
1137 |
atEnd |
|
1138 |
"return true, if the read-position is at the end" |
|
1139 |
||
15357 | 1140 |
^ position >= readLimit |
611 | 1141 |
! |
1142 |
||
1143 |
isEmpty |
|
1144 |
"return true, if the contents of the stream is empty" |
|
1145 |
||
1146 |
^ readLimit == 0 |
|
9006 | 1147 |
! |
1148 |
||
10577 | 1149 |
isInternalByteStream |
1150 |
"return true, if the stream is an internal stream reading bytes" |
|
1151 |
||
1152 |
^ collection class isBytes |
|
1153 |
||
24852 | 1154 |
" |
1155 |
'' readStream isInternalByteStream. |
|
1156 |
#[] readStream isInternalByteStream. |
|
1157 |
UnicodeString new readStream isInternalByteStream. |
|
1158 |
" |
|
1159 |
||
10577 | 1160 |
"Created: / 30-05-2007 / 16:16:12 / cg" |
24852 | 1161 |
"Modified (comment): / 22-10-2019 / 16:59:38 / Stefan Vogel" |
10577 | 1162 |
! |
1163 |
||
9006 | 1164 |
isPositionable |
1165 |
"return true, if the stream supports positioning (this one is)" |
|
1166 |
||
1167 |
^ true |
|
24944 | 1168 |
! |
1169 |
||
1170 |
isUnicodeEncoded |
|
1171 |
"return true, if the streamed collection is any string (8-, 16- or 32-bit), |
|
1172 |
which definitly is not using UTF-x or JIS or any other encoding. |
|
1173 |
I.e. 'self next' always returns a unicode character. |
|
1174 |
Note: the answer of false sows not mean that it is not unicode, |
|
1175 |
but it does mean that we don't know for sure." |
|
1176 |
||
1177 |
^ collection isString and:[(self objectAttributeAt:#isUnicodeEncoded) == true] |
|
1178 |
||
1179 |
" |
|
1180 |
'' readStream isUnicodeEncoded |
|
1181 |
" |
|
1182 |
||
1183 |
"Created: / 21-11-2019 / 18:32:37 / Stefan Vogel" |
|
611 | 1184 |
! ! |
1185 |
||
1897 | 1186 |
!PositionableStream class methodsFor:'documentation'! |
701 | 1187 |
|
1188 |
version |
|
19403 | 1189 |
^ '$Header$' |
13379 | 1190 |
! |
1191 |
||
1192 |
version_CVS |
|
19403 | 1193 |
^ '$Header$' |
701 | 1194 |
! ! |
6777
5efddf5358b2
currentFileInDirectory query
Claus Gittinger <cg@exept.de>
parents:
6707
diff
changeset
|
1195 |
|
14934 | 1196 |
|
611 | 1197 |
PositionableStream initialize! |