author | Claus Gittinger <cg@exept.de> |
Sat, 25 Sep 1999 15:20:37 +0200 | |
changeset 4814 | 7825b9141f01 |
parent 4432 | 5267a659f8d9 |
child 5394 | f877659e09f7 |
permissions | -rw-r--r-- |
58 | 1 |
" |
2 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
379 | 3 |
All Rights Reserved |
58 | 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 |
" |
|
12 |
||
13 |
Stream subclass:#PeekableStream |
|
1295 | 14 |
instanceVariableNames:'' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Streams' |
|
58 | 18 |
! |
19 |
||
88 | 20 |
!PeekableStream class methodsFor:'documentation'! |
21 |
||
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
379 | 25 |
All Rights Reserved |
58 | 26 |
|
88 | 27 |
This software is furnished under a license and may be used |
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
58 | 35 |
|
88 | 36 |
documentation |
37 |
" |
|
38 |
abstract superclass for all Stream which support read-ahead |
|
39 |
(i.e. peeking) of one element. |
|
40 |
Concrete subclasses must implement a peek method. |
|
1295 | 41 |
|
42 |
[author:] |
|
43 |
Claus Gittinger |
|
88 | 44 |
" |
45 |
! ! |
|
58 | 46 |
|
4404 | 47 |
!PeekableStream methodsFor:'positioning'! |
58 | 48 |
|
68 | 49 |
skipAny:skipCollection |
50 |
"skip all characters included in the argument-set. |
|
51 |
returns the next peeked element or nil, if the end-of-stream was reached." |
|
52 |
||
53 |
|nextOne| |
|
54 |
||
4432
5267a659f8d9
avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents:
4404
diff
changeset
|
55 |
nextOne := self peekOrNil. |
68 | 56 |
[nextOne notNil and:[skipCollection includes:nextOne]] whileTrue:[ |
4432
5267a659f8d9
avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents:
4404
diff
changeset
|
57 |
self next. |
5267a659f8d9
avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents:
4404
diff
changeset
|
58 |
nextOne := self peekOrNil |
68 | 59 |
]. |
60 |
^ nextOne |
|
61 |
||
62 |
" |
|
63 |
|s skipChars| |
|
64 |
||
65 |
s := ReadStream on:'some numbers1234with\in other99 stuff' withCRs. |
|
66 |
skipChars := 'abcdefghijklmnopqrstuvwxyz\ ' withCRs. |
|
67 |
s skipAny:skipChars. |
|
1422 | 68 |
Transcript showCR:(Integer readFrom:s). |
68 | 69 |
s skipAny:skipChars. |
1422 | 70 |
Transcript showCR:(Integer readFrom:s). |
68 | 71 |
" |
72 |
! |
|
73 |
||
611 | 74 |
skipSeparators |
75 |
"skip all whitespace; returns the next peeked element or |
|
76 |
nil, if the end-of-stream was reached. |
|
77 |
The streams elements should be characters. |
|
78 |
Notice: compare this method to skipSpaces" |
|
79 |
||
80 |
|nextOne| |
|
81 |
||
4432
5267a659f8d9
avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents:
4404
diff
changeset
|
82 |
nextOne := self peekOrNil. |
611 | 83 |
[nextOne notNil and:[nextOne isSeparator]] whileTrue:[ |
4432
5267a659f8d9
avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents:
4404
diff
changeset
|
84 |
self next. |
5267a659f8d9
avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents:
4404
diff
changeset
|
85 |
nextOne := self peekOrNil |
611 | 86 |
]. |
87 |
^ nextOne |
|
88 |
||
89 |
" |
|
90 |
|s| |
|
91 |
||
92 |
s := ReadStream on:'one two\three' withCRs. |
|
93 |
s skipSeparators. |
|
1422 | 94 |
Transcript showCR:(s nextWord). |
611 | 95 |
s skipSeparators. |
1422 | 96 |
Transcript showCR:(s nextWord). |
611 | 97 |
s skipSeparators. |
1422 | 98 |
Transcript showCR:(s next displayString). |
611 | 99 |
" |
100 |
! |
|
101 |
||
102 |
skipSeparatorsExceptCR |
|
103 |
"skip all whitespace except carriage return; returns the |
|
104 |
next peeked element or nil, if the end-of-stream was reached. |
|
105 |
The streams elements should be characters. |
|
106 |
Notice: compare this method to skipSpaces and skipSeparators" |
|
107 |
||
108 |
|nextOne| |
|
109 |
||
4432
5267a659f8d9
avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents:
4404
diff
changeset
|
110 |
nextOne := self peekOrNil. |
611 | 111 |
[nextOne notNil |
112 |
and:[nextOne isSeparator |
|
113 |
and:[nextOne ~~ Character cr]]] whileTrue:[ |
|
4432
5267a659f8d9
avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents:
4404
diff
changeset
|
114 |
self next. |
5267a659f8d9
avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents:
4404
diff
changeset
|
115 |
nextOne := self peekOrNil |
611 | 116 |
]. |
117 |
^ nextOne |
|
118 |
! |
|
119 |
||
68 | 120 |
skipSpaces |
121 |
"skip all spaces; returns the next peeked element or |
|
122 |
nil, if the end-of-stream was reached. |
|
123 |
The streams elements should be characters. |
|
124 |
Notice: this one skips only spaces (i.e. no cr, tabs etc) |
|
4432
5267a659f8d9
avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents:
4404
diff
changeset
|
125 |
usually, skipSeparators is what you want." |
68 | 126 |
|
127 |
|nextOne| |
|
128 |
||
4432
5267a659f8d9
avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents:
4404
diff
changeset
|
129 |
nextOne := self peekOrNil. |
68 | 130 |
[nextOne notNil and:[nextOne == Character space]] whileTrue:[ |
4432
5267a659f8d9
avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents:
4404
diff
changeset
|
131 |
self next. |
5267a659f8d9
avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents:
4404
diff
changeset
|
132 |
nextOne := self peekOrNil |
68 | 133 |
]. |
134 |
^ nextOne |
|
135 |
||
136 |
" |
|
137 |
|s| |
|
138 |
||
139 |
s := ReadStream on:'one two\three' withCRs. |
|
140 |
s skipSpaces. |
|
1422 | 141 |
Transcript showCR:(s nextWord). |
68 | 142 |
s skipSpaces. |
1422 | 143 |
Transcript showCR:(s nextWord). |
68 | 144 |
s skipSpaces. |
1422 | 145 |
Transcript showCR:(s next displayString). |
68 | 146 |
" |
4404 | 147 |
! ! |
148 |
||
149 |
!PeekableStream methodsFor:'reading'! |
|
150 |
||
151 |
nextDecimalInteger |
|
152 |
"read the next integer in radix 10. Does NOT skip initial whitespace. |
|
153 |
The streams elements should be characters. |
|
4432
5267a659f8d9
avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents:
4404
diff
changeset
|
154 |
Be careful - this method returns 0 if not posiioned on a digit intitially |
5267a659f8d9
avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents:
4404
diff
changeset
|
155 |
or if the end of the stream is encountered." |
4404 | 156 |
|
157 |
|nextOne value| |
|
158 |
||
4432
5267a659f8d9
avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents:
4404
diff
changeset
|
159 |
nextOne := self peekOrNil. |
4404 | 160 |
value := 0. |
161 |
[nextOne notNil and:[nextOne isDigitRadix:10]] whileTrue:[ |
|
162 |
value := (value * 10) + nextOne digitValue. |
|
4432
5267a659f8d9
avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents:
4404
diff
changeset
|
163 |
self next. |
5267a659f8d9
avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents:
4404
diff
changeset
|
164 |
nextOne := self peekOrNil |
4404 | 165 |
]. |
166 |
^ value |
|
167 |
||
168 |
" |
|
169 |
|s| |
|
170 |
||
171 |
s := '1234 5678' readStream. |
|
172 |
s nextDecimalInteger. |
|
173 |
" |
|
174 |
||
175 |
" |
|
176 |
|s| |
|
177 |
||
178 |
s := '1234 5678' readStream. |
|
179 |
s nextDecimalInteger. |
|
180 |
s skipSpaces. |
|
181 |
s nextDecimalInteger. |
|
182 |
" |
|
183 |
! |
|
184 |
||
185 |
nextDelimited:terminator |
|
186 |
"return the contents of the receiver, up to the next terminator character. |
|
187 |
Doubled terminators indicate an embedded terminator character. |
|
188 |
For example: 'this '' was a quote'. |
|
189 |
Start postioned before the initial terminator." |
|
190 |
||
191 |
| out ch | |
|
192 |
||
193 |
out := WriteStream on: (String new: 1000). |
|
194 |
self atEnd ifTrue: [^ '']. |
|
195 |
self next == terminator ifFalse: [self skip: -1]. "absorb initial terminator" |
|
196 |
[(ch := self next) == nil] whileFalse: [ |
|
197 |
(ch == terminator) ifTrue: [ |
|
198 |
self peek == terminator ifFalse: [ |
|
199 |
^ out contents "terminator is not doubled; we're done!!" |
|
200 |
]. |
|
201 |
self next. "skip doubled terminator" |
|
202 |
]. |
|
203 |
out nextPut: ch. |
|
204 |
]. |
|
205 |
^ out contents |
|
206 |
||
207 |
" |
|
208 |
('*foo bar baz* more foo' readStream nextDelimited:$*) |
|
209 |
('*foo bar **baz***' readStream nextDelimited:$*) |
|
210 |
" |
|
211 |
! |
|
212 |
||
213 |
nextPeek |
|
214 |
"advance to next element and return the peeked element" |
|
215 |
||
216 |
self next. |
|
217 |
^ self peek |
|
218 |
! |
|
219 |
||
220 |
peek |
|
221 |
"return the next element of the stream without advancing (i.e. |
|
222 |
the following send of next will return this element again.) |
|
223 |
- we do not know here how to do it, it must be redefined in subclass" |
|
224 |
||
225 |
^ self subclassResponsibility |
|
226 |
! |
|
227 |
||
228 |
peekFor:anObject |
|
229 |
"if the next-to-be-read object is equal to the argument, anObject, read it |
|
230 |
and return true. Otherwise, leave the receiver unaffected and return false." |
|
231 |
||
232 |
self peek = anObject ifTrue:[ |
|
233 |
self next. |
|
234 |
^ true |
|
235 |
]. |
|
236 |
^ false |
|
68 | 237 |
! |
238 |
||
2419 | 239 |
upToMatching:aBlock |
240 |
"Return the next elements up to but not including the next element |
|
241 |
for which aBlock returns true. |
|
242 |
The next read will return that matching element." |
|
243 |
||
244 |
|answerStream element| |
|
245 |
||
246 |
answerStream := WriteStream on:(self contentsSpecies new). |
|
247 |
[self atEnd] whileFalse: [ |
|
248 |
element := self peek. |
|
249 |
(aBlock value:element) ifTrue: [^ answerStream contents]. |
|
250 |
answerStream nextPut:element. |
|
251 |
self next. |
|
252 |
]. |
|
253 |
^ answerStream contents |
|
254 |
||
255 |
" |
|
256 |
'hello world' readStream upToMatching:[:c | c isSeparator]. |
|
257 |
" |
|
4404 | 258 |
" |
259 |
|s| |
|
260 |
||
261 |
s := 'hello world' readStream. |
|
262 |
s upToMatching:[:c | c isSeparator]. |
|
263 |
s upToEnd |
|
264 |
" |
|
2419 | 265 |
|
2421 | 266 |
"Modified: 26.2.1997 / 12:20:57 / cg" |
2419 | 267 |
! |
268 |
||
68 | 269 |
upToSeparator |
270 |
"Return the next elements up to but not including the next separator. |
|
4404 | 271 |
The next read will return the separator. |
272 |
If no separator is encountered, the contents up to the end is returned. |
|
273 |
The elements are supposed to understand #isSeparator |
|
274 |
(i.e. the receiver is supposed to be a character-stream)." |
|
68 | 275 |
|
4404 | 276 |
^ self upToMatching:[:ch | ch isSeparator] |
2060 | 277 |
|
278 |
" |
|
4404 | 279 |
'hello world' readStream upToSeparator |
280 |
'helloworld' readStream upToSeparator |
|
281 |
'helloworld' readStream upToSeparator |
|
282 |
'' readStream upToSeparator |
|
283 |
||
284 |
|s| |
|
285 |
s := 'hello world' readStream. |
|
286 |
s upToSeparator. |
|
287 |
s upToEnd |
|
2060 | 288 |
" |
289 |
||
290 |
"Modified: 4.1.1997 / 23:38:05 / cg" |
|
611 | 291 |
! ! |
58 | 292 |
|
701 | 293 |
!PeekableStream class methodsFor:'documentation'! |
294 |
||
295 |
version |
|
4432
5267a659f8d9
avoid raising pastEnd exceptions in skip-methods
Claus Gittinger <cg@exept.de>
parents:
4404
diff
changeset
|
296 |
^ '$Header: /cvs/stx/stx/libbasic/PeekableStream.st,v 1.20 1999-07-22 16:29:18 cg Exp $' |
701 | 297 |
! ! |