author | Claus Gittinger <cg@exept.de> |
Tue, 14 Nov 1995 20:02:34 +0100 | |
changeset 544 | d78012b20769 |
parent 530 | 07d0bce293c9 |
child 611 | 80bb0f1a7bab |
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 |
|
14 |
instanceVariableNames:'' |
|
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Streams' |
|
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 |
version |
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
427
diff
changeset
|
37 |
^ '$Header: /cvs/stx/stx/libbasic/Attic/PeekStr.st,v 1.11 1995-11-11 15:25:33 cg Exp $' |
88 | 38 |
! |
39 |
||
40 |
documentation |
|
41 |
" |
|
42 |
abstract superclass for all Stream which support read-ahead |
|
43 |
(i.e. peeking) of one element. |
|
44 |
Concrete subclasses must implement a peek method. |
|
45 |
" |
|
46 |
! ! |
|
58 | 47 |
|
48 |
!PeekableStream methodsFor:'reading'! |
|
49 |
||
50 |
peek |
|
51 |
"return the next element of the stream without advancing (i.e. |
|
52 |
the following send of next will return this element again.) |
|
53 |
- we do not know here how to do it, it must be redefined in subclass" |
|
54 |
||
55 |
^ self subclassResponsibility |
|
56 |
! |
|
57 |
||
58 |
peekFor:anObject |
|
369 | 59 |
"if the next-to-be-read object is equal to the argument, anObject, read it |
60 |
and return true. Otherwise, leave the receiver unaffected and return false." |
|
58 | 61 |
|
62 |
self peek = anObject ifTrue:[ |
|
379 | 63 |
self next. |
64 |
^ true |
|
58 | 65 |
]. |
66 |
^ false |
|
67 |
! |
|
68 |
||
69 |
nextPeek |
|
70 |
"advance to next element and return the peeked element" |
|
71 |
||
72 |
self next. |
|
73 |
^ self peek |
|
74 |
! |
|
75 |
||
68 | 76 |
skipAny:skipCollection |
77 |
"skip all characters included in the argument-set. |
|
78 |
returns the next peeked element or nil, if the end-of-stream was reached." |
|
79 |
||
80 |
|nextOne| |
|
81 |
||
82 |
nextOne := self peek. |
|
83 |
[nextOne notNil and:[skipCollection includes:nextOne]] whileTrue:[ |
|
379 | 84 |
self next. |
85 |
nextOne := self peek |
|
68 | 86 |
]. |
87 |
^ nextOne |
|
88 |
||
89 |
" |
|
90 |
|s skipChars| |
|
91 |
||
92 |
s := ReadStream on:'some numbers1234with\in other99 stuff' withCRs. |
|
93 |
skipChars := 'abcdefghijklmnopqrstuvwxyz\ ' withCRs. |
|
94 |
s skipAny:skipChars. |
|
95 |
Transcript showCr:(Integer readFrom:s). |
|
96 |
s skipAny:skipChars. |
|
97 |
Transcript showCr:(Integer readFrom:s). |
|
98 |
" |
|
99 |
! |
|
100 |
||
101 |
skipSpaces |
|
102 |
"skip all spaces; returns the next peeked element or |
|
103 |
nil, if the end-of-stream was reached. |
|
104 |
The streams elements should be characters. |
|
105 |
Notice: this one skips only spaces (i.e. no cr, tabs etc) |
|
379 | 106 |
usually, skipSeparators is what you want." |
68 | 107 |
|
108 |
|nextOne| |
|
109 |
||
110 |
nextOne := self peek. |
|
111 |
[nextOne notNil and:[nextOne == Character space]] whileTrue:[ |
|
379 | 112 |
self next. |
113 |
nextOne := self peek |
|
68 | 114 |
]. |
115 |
^ nextOne |
|
116 |
||
117 |
" |
|
118 |
|s| |
|
119 |
||
120 |
s := ReadStream on:'one two\three' withCRs. |
|
121 |
s skipSpaces. |
|
122 |
Transcript showCr:(s nextWord). |
|
123 |
s skipSpaces. |
|
124 |
Transcript showCr:(s nextWord). |
|
125 |
s skipSpaces. |
|
126 |
Transcript showCr:(s next displayString). |
|
127 |
" |
|
128 |
! |
|
129 |
||
58 | 130 |
skipSeparators |
131 |
"skip all whitespace; returns the next peeked element or |
|
132 |
nil, if the end-of-stream was reached. |
|
68 | 133 |
The streams elements should be characters. |
134 |
Notice: compare this method to skipSpaces" |
|
58 | 135 |
|
136 |
|nextOne| |
|
137 |
||
138 |
nextOne := self peek. |
|
139 |
[nextOne notNil and:[nextOne isSeparator]] whileTrue:[ |
|
379 | 140 |
self next. |
141 |
nextOne := self peek |
|
58 | 142 |
]. |
143 |
^ nextOne |
|
282 | 144 |
|
68 | 145 |
" |
146 |
|s| |
|
147 |
||
148 |
s := ReadStream on:'one two\three' withCRs. |
|
149 |
s skipSeparators. |
|
150 |
Transcript showCr:(s nextWord). |
|
151 |
s skipSeparators. |
|
152 |
Transcript showCr:(s nextWord). |
|
153 |
s skipSeparators. |
|
154 |
Transcript showCr:(s next displayString). |
|
155 |
" |
|
58 | 156 |
! |
157 |
||
158 |
skipSeparatorsExceptCR |
|
159 |
"skip all whitespace except carriage return; returns the |
|
160 |
next peeked element or nil, if the end-of-stream was reached. |
|
68 | 161 |
The streams elements should be characters. |
162 |
Notice: compare this method to skipSpaces and skipSeparators" |
|
58 | 163 |
|
164 |
|nextOne| |
|
165 |
||
166 |
nextOne := self peek. |
|
167 |
[nextOne notNil |
|
168 |
and:[nextOne isSeparator |
|
169 |
and:[nextOne ~~ Character cr]]] whileTrue:[ |
|
379 | 170 |
self next. |
171 |
nextOne := self peek |
|
58 | 172 |
]. |
173 |
^ nextOne |
|
174 |
! |
|
175 |
||
68 | 176 |
upToSeparator |
177 |
"Return the next elements up to but not including the next separator. |
|
178 |
The elements are supposed to understand isSeparator." |
|
179 |
||
180 |
|answerStream element| |
|
181 |
||
182 |
answerStream := WriteStream on:(self contentsSpecies new). |
|
183 |
[self atEnd] whileFalse: [ |
|
379 | 184 |
element := self peek. |
185 |
element isSeparator ifTrue: [^answerStream contents]. |
|
186 |
answerStream nextPut: element. |
|
187 |
self next. |
|
68 | 188 |
]. |
189 |
^ answerStream contents |
|
190 |
! |
|
191 |
||
58 | 192 |
nextDecimalInteger |
193 |
"read the next integer in radix 10. dont skip whitespace. |
|
68 | 194 |
The streams elements should be characters." |
58 | 195 |
|
196 |
|nextOne value| |
|
197 |
||
198 |
nextOne := self peek. |
|
199 |
value := 0. |
|
200 |
[nextOne notNil and:[nextOne isDigitRadix:10]] whileTrue:[ |
|
379 | 201 |
value := (value * 10) + nextOne digitValue. |
202 |
nextOne := self nextPeek |
|
58 | 203 |
]. |
204 |
^ value |
|
205 |
! ! |