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