author | Stefan Vogel <sv@exept.de> |
Mon, 19 Feb 1996 16:07:27 +0100 | |
changeset 970 | 3b59c9b38dbb |
parent 829 | fc386319f41c |
child 1133 | 961f2b095c22 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1988 by Claus Gittinger |
202 | 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 |
" |
|
12 |
||
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
13 |
'From Smalltalk/X, Version:2.10.8 on 6-feb-1996 at 15:31:28' ! |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
14 |
|
1 | 15 |
PositionableStream subclass:#ReadStream |
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
16 |
instanceVariableNames:'' |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
17 |
classVariableNames:'' |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
18 |
poolDictionaries:'' |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
19 |
category:'Streams' |
1 | 20 |
! |
21 |
||
88 | 22 |
!ReadStream class methodsFor:'documentation'! |
23 |
||
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
202 | 27 |
All Rights Reserved |
1 | 28 |
|
88 | 29 |
This software is furnished under a license and may be used |
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
1 | 37 |
|
88 | 38 |
documentation |
39 |
" |
|
40 |
ReadStream defines protocol for reading streamwise over collections. |
|
41 |
" |
|
42 |
! ! |
|
1 | 43 |
|
202 | 44 |
!ReadStream methodsFor:'converting'! |
45 |
||
46 |
readStream |
|
47 |
"return a readStream from the receiver. Since this is already |
|
48 |
a readStream, return self." |
|
49 |
||
50 |
^ self |
|
51 |
! ! |
|
52 |
||
345 | 53 |
!ReadStream methodsFor:'queries'! |
54 |
||
55 |
isReadable |
|
56 |
^ true |
|
57 |
! ! |
|
58 |
||
59 | 59 |
!ReadStream methodsFor:'reading'! |
1 | 60 |
|
61 |
next |
|
62 |
"return the next element; advance read pointer. |
|
2 | 63 |
return nil, if there is no next element. |
345 | 64 |
- tuned for a bit more speed on String/ByteArray/Array-Streams" |
1 | 65 |
|
66 |
|ret| |
|
67 |
||
68 |
%{ /* NOCONTEXT */ |
|
69 |
||
70 |
REGISTER int pos; |
|
71 |
unsigned ch; |
|
252 | 72 |
OBJ coll, p, l; |
1 | 73 |
|
2 | 74 |
coll = _INST(collection); |
252 | 75 |
p = _INST(position); |
76 |
l = _INST(readLimit); |
|
1 | 77 |
|
252 | 78 |
if (__isNonNilObject(coll) && __bothSmallInteger(p, l)) { |
79 |
||
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
80 |
pos = _intVal(p); |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
81 |
if (pos > 0 && pos <= _intVal(l)) { |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
82 |
OBJ cls; |
59 | 83 |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
84 |
cls = __qClass(coll); |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
85 |
if (cls == String) { |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
86 |
if (pos <= _stringSize(coll)) { |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
87 |
_INST(position) = _MKSMALLINT(pos + 1); |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
88 |
ch = _stringVal(coll)[pos-1]; |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
89 |
RETURN ( _MKCHARACTER(ch) ); |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
90 |
} |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
91 |
} else if (cls == ByteArray) { |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
92 |
if (pos <= _byteArraySize(coll)) { |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
93 |
_INST(position) = _MKSMALLINT(pos + 1); |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
94 |
ch = _ByteArrayInstPtr(coll)->ba_element[pos-1]; |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
95 |
RETURN ( _MKSMALLINT(ch) ); |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
96 |
} |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
97 |
} else if (cls == Array) { |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
98 |
if (pos <= _arraySize(coll)) { |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
99 |
_INST(position) = _MKSMALLINT(pos + 1); |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
100 |
RETURN ( _ArrayInstPtr(coll)->a_element[pos-1]); |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
101 |
} |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
102 |
} |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
103 |
} |
1 | 104 |
} |
105 |
%} |
|
106 |
. |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
107 |
(position > readLimit) ifTrue:[^ self pastEnd]. |
1 | 108 |
ret := collection at:position. |
109 |
position := position + 1. |
|
110 |
^ ret |
|
111 |
! |
|
112 |
||
252 | 113 |
nextAlphaNumericWord |
1 | 114 |
"read the next word (i.e. up to non letter-or-digit). |
115 |
return a string containing those characters. |
|
59 | 116 |
- tuned for speed on String-Streams for faster scanning" |
1 | 117 |
%{ |
118 |
/* speedup, if collection is a string */ |
|
119 |
||
120 |
int pos, limit, sz; |
|
121 |
int len; |
|
13 | 122 |
char buffer[256]; |
123 |
REGISTER unsigned char *cp; |
|
124 |
REGISTER unsigned ch; |
|
252 | 125 |
OBJ coll, p, l; |
521
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
384
diff
changeset
|
126 |
OBJ __MKSTRING_L(); |
1 | 127 |
|
252 | 128 |
coll = _INST(collection); |
129 |
p = _INST(position); |
|
130 |
l = _INST(readLimit); |
|
131 |
||
132 |
if (__isString(coll) && __bothSmallInteger(p, l)) { |
|
133 |
||
134 |
pos = _intVal(p); |
|
135 |
limit = _intVal(l); |
|
329 | 136 |
sz = __qSize(coll) - OHDR_SIZE; |
202 | 137 |
if (sz < limit) |
138 |
limit = sz; |
|
252 | 139 |
cp = _stringVal(coll) + pos - 1; |
1 | 140 |
|
202 | 141 |
for (;;) { |
142 |
if (pos > limit) break; |
|
143 |
ch = *cp; |
|
1 | 144 |
|
202 | 145 |
if (ch > ' ') break; |
146 |
if ((ch != ' ') && (ch != '\t') && (ch != '\r') |
|
147 |
&& (ch != '\n') && (ch != 0x0b)) break; |
|
148 |
cp++; |
|
149 |
pos++; |
|
150 |
} |
|
1 | 151 |
|
202 | 152 |
len = 0; |
153 |
for (;;) { |
|
154 |
if (pos > limit) break; |
|
155 |
ch = *cp & 0xFF; |
|
1 | 156 |
|
202 | 157 |
if (! (((ch >= 'a') && (ch <= 'z')) || |
158 |
((ch >= 'A') && (ch <= 'Z')) || |
|
159 |
((ch >= '0') && (ch <= '9')))) |
|
160 |
break; |
|
161 |
buffer[len++] = ch; |
|
162 |
if (len >= (sizeof(buffer)-1)) { |
|
163 |
/* emergency */ |
|
164 |
break; |
|
165 |
} |
|
166 |
pos++; |
|
167 |
cp++; |
|
168 |
} |
|
1 | 169 |
|
202 | 170 |
_INST(position) = _MKSMALLINT(pos); |
171 |
buffer[len] = '\0'; |
|
521
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
384
diff
changeset
|
172 |
RETURN ( (len != 0) ? __MKSTRING_L(buffer, len COMMA_CON) : nil ); |
1 | 173 |
} |
174 |
%} |
|
175 |
. |
|
252 | 176 |
^ super nextAlphaNumericWord |
1 | 177 |
! |
178 |
||
611 | 179 |
nextDecimalInteger |
180 |
"read the next integer in radix 10. dont skip whitespace. |
|
181 |
- tuned for speed on String-Streams for faster scanning" |
|
182 |
||
183 |
|value nextOne| |
|
184 |
%{ |
|
185 |
int pos, limit, sz; |
|
186 |
REGISTER unsigned char *cp; |
|
187 |
REGISTER unsigned ch; |
|
188 |
int val = 0; |
|
189 |
OBJ coll, p, l; |
|
190 |
||
191 |
coll = _INST(collection); |
|
192 |
p = _INST(position); |
|
193 |
l = _INST(readLimit); |
|
194 |
||
195 |
if (__isString(coll) && __bothSmallInteger(p, l)) { |
|
196 |
||
197 |
pos = _intVal(p); |
|
198 |
limit = _intVal(l); |
|
199 |
sz = __qSize(coll) - OHDR_SIZE; |
|
200 |
if (sz < limit) |
|
201 |
limit = sz; |
|
202 |
cp = _stringVal(coll) + pos - 1; |
|
203 |
||
204 |
for (;;) { |
|
205 |
if (pos > limit) break; |
|
206 |
ch = *cp; |
|
207 |
||
208 |
if ((ch < '0') || (ch > '9')) break; |
|
209 |
val = val * 10 + (ch - '0'); |
|
210 |
pos++; |
|
211 |
if (val > (_MAX_INT / 10)) goto oops; |
|
212 |
cp++; |
|
213 |
} |
|
214 |
_INST(position) = _MKSMALLINT(pos); |
|
215 |
return _MKSMALLINT(val); |
|
216 |
} |
|
217 |
oops: |
|
218 |
value = _MKSMALLINT(val); |
|
219 |
%} |
|
220 |
. |
|
221 |
"fall-back for non-string streams - we have to continue where |
|
222 |
above primitive left off, in case of a large integer ... |
|
223 |
(instead of doing a super nextDecimalInteger)" |
|
224 |
||
225 |
nextOne := self peek. |
|
226 |
[nextOne notNil and:[nextOne isDigitRadix:10]] whileTrue:[ |
|
227 |
value = (value * 10) + nextOne digitValue. |
|
228 |
nextOne := self nextPeek |
|
229 |
]. |
|
230 |
^ value |
|
231 |
! |
|
232 |
||
233 |
nextPeek |
|
234 |
"advance read pointer return the peek element. |
|
235 |
this is equivalent to (self next; peek). |
|
236 |
- tuned for speed on String-Streams for faster scanning" |
|
237 |
||
238 |
%{ /* NOCONTEXT */ |
|
239 |
OBJ coll, l, p; |
|
240 |
||
241 |
coll = _INST(collection); |
|
242 |
p = _INST(position); |
|
243 |
l = _INST(readLimit); |
|
244 |
||
245 |
if (__isString(coll) && __bothSmallInteger(p, l)) { |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
246 |
REGISTER int pos; |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
247 |
unsigned ch; |
611 | 248 |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
249 |
pos = _intVal(p); |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
250 |
if ((pos > 0) && (pos < _intVal(l)) && (pos < _stringSize(coll))) { |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
251 |
pos += 1; |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
252 |
_INST(position) = _MKSMALLINT(pos); |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
253 |
ch = _stringVal(coll)[pos-1]; |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
254 |
RETURN ( _MKCHARACTER(ch) ); |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
255 |
} |
611 | 256 |
} |
257 |
%} |
|
258 |
. |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
259 |
(position > readLimit) ifTrue:[^ self pastEnd]. |
611 | 260 |
position := position + 1. |
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
261 |
(position > readLimit) ifTrue:[^ self pastEnd]. |
611 | 262 |
^ collection at:position |
263 |
! |
|
264 |
||
1 | 265 |
nextSymbol |
266 |
"read the next selector-symbol (i.e. up to non letter-or-digit). |
|
267 |
return a string containing those characters. |
|
59 | 268 |
- tuned for speed on String-Streams for faster scanning" |
1 | 269 |
%{ |
270 |
int pos, limit, sz; |
|
271 |
int len; |
|
13 | 272 |
char buffer[256]; |
273 |
REGISTER unsigned char *cp; |
|
274 |
REGISTER unsigned ch; |
|
252 | 275 |
OBJ coll, p, l; |
521
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
384
diff
changeset
|
276 |
OBJ __MKSTRING_L(); |
1 | 277 |
|
252 | 278 |
coll = _INST(collection); |
279 |
p = _INST(position); |
|
280 |
l = _INST(readLimit); |
|
281 |
||
282 |
if (__isString(coll) && __bothSmallInteger(p, l)) { |
|
283 |
||
284 |
pos = _intVal(p); |
|
285 |
limit = _intVal(l); |
|
329 | 286 |
sz = __qSize(coll) - OHDR_SIZE; |
202 | 287 |
if (sz < limit) |
288 |
limit = sz; |
|
252 | 289 |
cp = _stringVal(coll) + pos - 1; |
1 | 290 |
|
202 | 291 |
len = 0; |
292 |
for (;;) { |
|
293 |
if (pos > limit) break; |
|
294 |
ch = *cp; |
|
1 | 295 |
|
202 | 296 |
if (! (((ch >= 'a') && (ch <= 'z')) || |
297 |
((ch >= 'A') && (ch <= 'Z')) || |
|
298 |
((ch >= '0') && (ch <= '9')) || |
|
299 |
(ch == ':'))) |
|
300 |
break; |
|
301 |
buffer[len++] = ch; |
|
302 |
if (len >= (sizeof(buffer)-1)) { |
|
303 |
/* emergency */ |
|
304 |
break; |
|
305 |
} |
|
306 |
pos++; |
|
307 |
cp++; |
|
308 |
} |
|
1 | 309 |
|
202 | 310 |
_INST(position) = _MKSMALLINT(pos); |
311 |
buffer[len] = '\0'; |
|
521
70533ec40482
use new MKSTRING_L if length is known (avoids a useless strlen)
Claus Gittinger <cg@exept.de>
parents:
384
diff
changeset
|
312 |
RETURN ( (len != 0) ? __MKSTRING_L(buffer, len COMMA_CON) : nil ); |
1 | 313 |
} |
314 |
%} |
|
315 |
. |
|
316 |
^ super nextSymbol |
|
317 |
! |
|
318 |
||
611 | 319 |
peek |
320 |
"return the next element; do NOT advance read pointer. |
|
321 |
return nil, if there is no next element. |
|
322 |
- tuned for a bit more speed on String/ByteArray/Array-Streams" |
|
1 | 323 |
|
324 |
%{ /* NOCONTEXT */ |
|
611 | 325 |
|
326 |
REGISTER int pos; |
|
327 |
unsigned ch; |
|
328 |
OBJ coll; |
|
329 |
OBJ cls, p, l; |
|
1 | 330 |
|
59 | 331 |
coll = _INST(collection); |
252 | 332 |
p = _INST(position); |
333 |
l = _INST(readLimit); |
|
334 |
||
611 | 335 |
if (__isNonNilObject(coll) && __bothSmallInteger(p, l)) { |
1 | 336 |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
337 |
pos = _intVal(p); |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
338 |
if (pos <= _intVal(l) && pos > 0) { |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
339 |
cls = __qClass(coll); |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
340 |
if (cls == String) { |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
341 |
if (pos <= _stringSize(coll)) { |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
342 |
ch = _stringVal(coll)[pos-1]; |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
343 |
RETURN ( _MKCHARACTER(ch) ); |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
344 |
} |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
345 |
} else if (cls == ByteArray) { |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
346 |
if (pos <= _byteArraySize(coll)) { |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
347 |
ch = _ByteArrayInstPtr(coll)->ba_element[pos-1]; |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
348 |
RETURN ( _MKSMALLINT(ch) ); |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
349 |
} |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
350 |
} else if (cls == Array) { |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
351 |
if (pos <= _arraySize(coll)) { |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
352 |
RETURN ( _ArrayInstPtr(coll)->a_element[pos-1]); |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
353 |
} |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
354 |
} |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
355 |
} |
1 | 356 |
} |
357 |
%} |
|
358 |
. |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
359 |
(position > readLimit) ifTrue:[^ self pastEnd]. |
611 | 360 |
^ collection at:position |
1 | 361 |
! |
362 |
||
363 |
skipSeparators |
|
364 |
"skip all whitespace; next will return next non-white-space element. |
|
59 | 365 |
Return the peeked character or nil, if the end-of-stream was reached. |
2 | 366 |
- reimplemented for speed on String-Streams for faster scanning" |
1 | 367 |
|
368 |
%{ /* NOCONTEXT */ |
|
252 | 369 |
OBJ coll, p, l; |
1 | 370 |
|
252 | 371 |
coll = _INST(collection); |
372 |
p = _INST(position); |
|
373 |
l = _INST(readLimit); |
|
374 |
||
375 |
if (__isString(coll) && __bothSmallInteger(p, l)) { |
|
202 | 376 |
REGISTER unsigned char *chars; |
377 |
REGISTER unsigned ch; |
|
378 |
REGISTER int pos; |
|
379 |
int limit; |
|
1 | 380 |
|
252 | 381 |
pos = _intVal(p); |
202 | 382 |
if (pos <= 0) { |
383 |
RETURN ( nil ); |
|
384 |
} |
|
1 | 385 |
|
252 | 386 |
limit = _intVal(l); |
329 | 387 |
if (limit > (__qSize(coll) - OHDR_SIZE)) |
388 |
limit = __qSize(coll) - OHDR_SIZE; |
|
1 | 389 |
|
252 | 390 |
chars = (unsigned char *)(_stringVal(coll) + pos - 1); |
202 | 391 |
while (pos <= limit) { |
392 |
ch = *chars++; |
|
393 |
if ((ch != ' ') && (ch != '\t') && (ch != '\r') |
|
394 |
&& (ch != '\n') && (ch != 0x0B)) { |
|
395 |
_INST(position) = _MKSMALLINT(pos); |
|
396 |
RETURN ( _MKCHARACTER(ch) ); |
|
397 |
} |
|
398 |
pos++; |
|
399 |
} |
|
400 |
_INST(position) = _MKSMALLINT(pos); |
|
401 |
RETURN ( nil ); |
|
1 | 402 |
} |
403 |
%} |
|
404 |
. |
|
405 |
^ super skipSeparators |
|
406 |
! |
|
407 |
||
13 | 408 |
skipSeparatorsExceptCR |
409 |
"skip all whitespace except newlines; |
|
202 | 410 |
next will return next non-white-space element. |
411 |
- reimplemented for speed on String-Streams for faster scanning" |
|
412 |
||
13 | 413 |
%{ /* NOCONTEXT */ |
414 |
||
252 | 415 |
OBJ coll, p, l; |
416 |
||
417 |
coll = _INST(collection); |
|
418 |
p = _INST(position); |
|
419 |
l = _INST(readLimit); |
|
420 |
||
421 |
if (__isString(coll) && __bothSmallInteger(p, l)) { |
|
202 | 422 |
REGISTER unsigned char *chars; |
423 |
REGISTER unsigned ch; |
|
424 |
REGISTER int pos; |
|
425 |
int limit; |
|
13 | 426 |
|
252 | 427 |
pos = _intVal(p); |
202 | 428 |
if (pos <= 0) { |
429 |
RETURN ( nil ); |
|
430 |
} |
|
13 | 431 |
|
252 | 432 |
limit = _intVal(l); |
329 | 433 |
if (limit > (__qSize(coll) - OHDR_SIZE)) |
434 |
limit = __qSize(coll) - OHDR_SIZE; |
|
13 | 435 |
|
252 | 436 |
chars = (unsigned char *)(_stringVal(coll) + pos - 1); |
202 | 437 |
while (pos <= limit) { |
438 |
ch = *chars++; |
|
439 |
if ((ch != ' ') && (ch != '\t') && (ch != '\r') && (ch != 0x0B)) { |
|
440 |
_INST(position) = _MKSMALLINT(pos); |
|
441 |
RETURN ( _MKCHARACTER(ch) ); |
|
442 |
} |
|
443 |
pos++; |
|
444 |
} |
|
445 |
_INST(position) = _MKSMALLINT(pos); |
|
446 |
RETURN ( nil ); |
|
13 | 447 |
} |
448 |
%} |
|
449 |
. |
|
450 |
^ super skipSeparatorsExceptCR |
|
451 |
! |
|
452 |
||
611 | 453 |
skipThrough:anObject |
454 |
"skip all objects up-to and including anObject, return anObject on success, |
|
455 |
nil if end-of-stream is reached before. |
|
456 |
On success, the next read operation will return the element after anObject. |
|
457 |
- reimplemented for speed on String-Streams for faster scanning" |
|
458 |
||
459 |
%{ /* NOCONTEXT */ |
|
460 |
OBJ coll, p, l; |
|
461 |
||
462 |
coll = _INST(collection); |
|
463 |
p = _INST(position); |
|
464 |
l = _INST(readLimit); |
|
465 |
||
466 |
if (__isString(coll) |
|
467 |
&& __isCharacter(anObject) |
|
468 |
&& __bothSmallInteger(p, l)) { |
|
469 |
REGISTER unsigned char *chars; |
|
470 |
REGISTER int pos, limit; |
|
471 |
unsigned ch; |
|
472 |
int sz; |
|
473 |
||
474 |
pos = _intVal(p); |
|
475 |
if (pos <= 0) { |
|
476 |
RETURN ( nil ); |
|
477 |
} |
|
478 |
||
479 |
limit = _intVal(l); |
|
480 |
sz = _stringSize(coll); |
|
481 |
if (limit > sz) limit = sz; |
|
482 |
||
483 |
chars = (unsigned char *)(_stringVal(coll) + pos - 1); |
|
484 |
ch = _intVal(_characterVal(anObject)) & 0xFF; |
|
485 |
while (pos < limit) { |
|
486 |
if (*chars == ch) { |
|
487 |
ch = *++chars; |
|
488 |
pos++; |
|
489 |
_INST(position) = _MKSMALLINT(pos); |
|
490 |
RETURN ( anObject ); |
|
491 |
} |
|
492 |
chars++; |
|
493 |
pos++; |
|
494 |
} |
|
495 |
RETURN ( nil ); |
|
496 |
} |
|
497 |
%} |
|
498 |
. |
|
499 |
^ super skipThrough:anObject |
|
500 |
! |
|
501 |
||
1 | 502 |
skipToAll:aCollection |
503 |
"skip for the sequence given by the argument, aCollection; |
|
504 |
return nil if not found, self otherwise. On a successful match, next read |
|
505 |
will return elements of aCollection." |
|
506 |
||
507 |
|oldPos buffer l first idx| |
|
508 |
||
509 |
oldPos := self position. |
|
510 |
l := aCollection size. |
|
511 |
first := aCollection at:1. |
|
512 |
[self atEnd] whileFalse:[ |
|
202 | 513 |
buffer := self next:l. |
514 |
buffer = aCollection ifTrue:[ |
|
515 |
self position:(self position - l). |
|
516 |
^ self |
|
517 |
]. |
|
518 |
idx := buffer indexOf:first startingAt:2. |
|
519 |
idx == 0 ifFalse:[ |
|
520 |
self position:(self position - l + idx - 1) |
|
521 |
] |
|
1 | 522 |
]. |
523 |
self position:oldPos. |
|
524 |
^ nil |
|
525 |
||
59 | 526 |
" |
527 |
|s| |
|
1 | 528 |
s := ReadStream on:'12345678901234567890'. |
529 |
s skipToAll:'901'. |
|
59 | 530 |
s next:4 |
531 |
" |
|
1 | 532 |
! ! |
533 |
||
59 | 534 |
!ReadStream methodsFor:'writing'! |
1 | 535 |
|
536 |
nextPut:anElement |
|
10 | 537 |
"catch write access to readstreams - report an error" |
538 |
||
539 |
self shouldNotImplement |
|
1 | 540 |
! ! |
611 | 541 |
|
701 | 542 |
!ReadStream class methodsFor:'documentation'! |
543 |
||
544 |
version |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
545 |
^ '$Header: /cvs/stx/stx/libbasic/ReadStream.st,v 1.23 1996-02-19 15:07:26 stefan Exp $' |
701 | 546 |
! ! |