1
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1988-93 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
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 |
PositionableStream subclass:#ReadStream
|
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Streams'
|
|
18 |
!
|
|
19 |
|
|
20 |
ReadStream comment:'
|
|
21 |
|
|
22 |
COPYRIGHT (c) 1988-93 by Claus Gittinger
|
|
23 |
All Rights Reserved
|
|
24 |
|
|
25 |
ReadStream defines protocol for reading streamwise over collections. Code here
|
|
26 |
is specially tuned for streaming over strings.
|
|
27 |
|
|
28 |
%W% %E%
|
|
29 |
'!
|
|
30 |
|
|
31 |
!ReadStream methodsFor:'access-reading'!
|
|
32 |
|
|
33 |
peek
|
|
34 |
"return the next element; do NOT advance read pointer.
|
2
|
35 |
return nil, if there is no next element.
|
|
36 |
- reimplemented for speed on String/ByteArray-Streams"
|
1
|
37 |
|
|
38 |
%{ /* NOCONTEXT */
|
|
39 |
|
|
40 |
REGISTER int pos;
|
|
41 |
unsigned ch;
|
2
|
42 |
OBJ coll;
|
|
43 |
extern OBJ ByteArray;
|
1
|
44 |
|
2
|
45 |
coll = _INST(collection);
|
|
46 |
if (_isNonNilObject(coll)
|
|
47 |
&& ((_qClass(coll) == String) || (_qClass(coll) == ByteArray))
|
1
|
48 |
&& _isSmallInteger(_INST(position))
|
|
49 |
&& _isSmallInteger(_INST(readLimit))) {
|
|
50 |
|
|
51 |
pos = _intVal(_INST(position));
|
|
52 |
if (pos > _intVal(_INST(readLimit))) {
|
|
53 |
RETURN ( nil );
|
|
54 |
}
|
2
|
55 |
if (pos > 0) {
|
|
56 |
if (_qClass(coll) == String) {
|
|
57 |
if (pos < (_qSize(coll) - OHDR_SIZE)) {
|
|
58 |
ch = _stringVal(coll)[pos-1];
|
|
59 |
RETURN ( _MKCHARACTER(ch) );
|
|
60 |
}
|
|
61 |
} else {
|
|
62 |
if (pos <= (_qSize(coll) - OHDR_SIZE)) {
|
|
63 |
ch = _ByteArrayInstPtr(coll)->ba_element[pos-1];
|
|
64 |
RETURN ( _MKSMALLINT(ch) );
|
|
65 |
}
|
|
66 |
}
|
1
|
67 |
}
|
|
68 |
}
|
|
69 |
%}
|
|
70 |
.
|
|
71 |
(position > readLimit) ifTrue:[^ nil].
|
|
72 |
^ collection at:position
|
|
73 |
!
|
|
74 |
|
|
75 |
next
|
|
76 |
"return the next element; advance read pointer.
|
2
|
77 |
return nil, if there is no next element.
|
|
78 |
- reimplemented for speed on String/ByteArray-Streams"
|
1
|
79 |
|
|
80 |
|ret|
|
|
81 |
|
|
82 |
%{ /* NOCONTEXT */
|
|
83 |
|
|
84 |
REGISTER int pos;
|
|
85 |
unsigned ch;
|
2
|
86 |
OBJ coll;
|
|
87 |
extern OBJ ByteArray;
|
1
|
88 |
|
2
|
89 |
coll = _INST(collection);
|
|
90 |
if (_isNonNilObject(coll)
|
|
91 |
&& ((_qClass(coll) == String) || (_qClass(coll) == ByteArray))
|
1
|
92 |
&& _isSmallInteger(_INST(position))
|
|
93 |
&& _isSmallInteger(_INST(readLimit))) {
|
|
94 |
|
|
95 |
pos = _intVal(_INST(position));
|
|
96 |
if (pos > _intVal(_INST(readLimit))) {
|
|
97 |
RETURN ( nil );
|
|
98 |
}
|
2
|
99 |
if (pos > 0) {
|
|
100 |
if (_qClass(coll) == String) {
|
|
101 |
if (pos < (_qSize(coll) - OHDR_SIZE)) {
|
|
102 |
_INST(position) = _MKSMALLINT(pos + 1);
|
|
103 |
ch = _stringVal(coll)[pos-1];
|
|
104 |
RETURN ( _MKCHARACTER(ch) );
|
|
105 |
}
|
|
106 |
} else {
|
|
107 |
if (pos <= (_qSize(coll) - OHDR_SIZE)) {
|
|
108 |
_INST(position) = _MKSMALLINT(pos + 1);
|
|
109 |
ch = _ByteArrayInstPtr(coll)->ba_element[pos-1];
|
|
110 |
RETURN ( _MKSMALLINT(ch) );
|
|
111 |
}
|
|
112 |
}
|
1
|
113 |
}
|
|
114 |
}
|
|
115 |
%}
|
|
116 |
.
|
|
117 |
(position > readLimit) ifTrue:[^ nil].
|
|
118 |
ret := collection at:position.
|
|
119 |
position := position + 1.
|
|
120 |
^ ret
|
|
121 |
!
|
|
122 |
|
2
|
123 |
next:count
|
|
124 |
"return the next count elements of the stream as a collection.
|
|
125 |
Redefined, to return a collection of the streamed-upon collections
|
|
126 |
species."
|
|
127 |
|
|
128 |
|coll|
|
|
129 |
|
|
130 |
coll := (collection species) new:count.
|
|
131 |
1 to:count do: [:index |
|
|
132 |
coll at:index put:(self next)
|
|
133 |
].
|
|
134 |
^ coll
|
|
135 |
!
|
|
136 |
|
1
|
137 |
nextPeek
|
|
138 |
"advance read pointer return the peek element.
|
|
139 |
this is equivalent to (self next; peek).
|
2
|
140 |
- reimplemented for speed on String-Streams for faster scanning"
|
1
|
141 |
|
|
142 |
%{ /* NOCONTEXT */
|
|
143 |
|
|
144 |
if (_isString(_INST(collection))
|
|
145 |
&& _isSmallInteger(_INST(position))
|
|
146 |
&& _isSmallInteger(_INST(readLimit))) {
|
|
147 |
REGISTER int pos;
|
|
148 |
unsigned ch;
|
|
149 |
|
|
150 |
pos = _intVal(_INST(position));
|
|
151 |
if (pos > _intVal(_INST(readLimit))) {
|
|
152 |
RETURN ( nil );
|
|
153 |
}
|
|
154 |
if ((pos > 0)
|
|
155 |
&& (pos < _qSize(_INST(collection)) - OHDR_SIZE)) {
|
|
156 |
_INST(position) = _MKSMALLINT(pos + 1);
|
|
157 |
pos = pos + 1;
|
|
158 |
if (pos < _qSize(_INST(collection)) - OHDR_SIZE) {
|
|
159 |
ch = _stringVal(_INST(collection))[pos-1];
|
|
160 |
RETURN ( _MKCHARACTER(ch) );
|
|
161 |
}
|
|
162 |
RETURN ( nil );
|
|
163 |
}
|
|
164 |
}
|
|
165 |
%}
|
|
166 |
.
|
|
167 |
(position > readLimit) ifTrue:[^ nil].
|
|
168 |
position := position + 1.
|
|
169 |
(position > readLimit) ifTrue:[^ nil].
|
|
170 |
^ collection at:position
|
|
171 |
!
|
|
172 |
|
|
173 |
nextDecimalInteger
|
|
174 |
"read the next integer in radix 10. dont skip whitespace.
|
2
|
175 |
- reimplemented for speed on String-Streams for faster scanning"
|
1
|
176 |
|
|
177 |
|value|
|
|
178 |
%{
|
|
179 |
int pos, limit, sz;
|
|
180 |
register unsigned char *cp;
|
|
181 |
register unsigned ch;
|
|
182 |
int val = 0;
|
|
183 |
|
|
184 |
if (_isString(_INST(collection))
|
|
185 |
&& _isSmallInteger(_INST(position))
|
|
186 |
&& _isSmallInteger(_INST(readLimit))) {
|
|
187 |
pos = _intVal(_INST(position));
|
|
188 |
limit = _intVal(_INST(readLimit));
|
|
189 |
sz = _qSize(_INST(collection)) - OHDR_SIZE;
|
|
190 |
if (sz < limit)
|
|
191 |
limit = sz;
|
|
192 |
cp = _stringVal(_INST(collection)) + pos - 1;
|
|
193 |
|
|
194 |
for (;;) {
|
|
195 |
if (pos > limit) break;
|
|
196 |
ch = *cp;
|
|
197 |
|
|
198 |
if ((ch < '0') || (ch > '9')) break;
|
|
199 |
val = val * 10 + (ch - '0');
|
|
200 |
pos++;
|
|
201 |
if (val > (_MAX_INT / 10)) goto oops;
|
|
202 |
cp++;
|
|
203 |
}
|
|
204 |
_INST(position) = _MKSMALLINT(pos);
|
|
205 |
return _MKSMALLINT(val);
|
|
206 |
}
|
|
207 |
oops:
|
|
208 |
value = _MKSMALLINT(val);
|
|
209 |
%}
|
|
210 |
.
|
|
211 |
[self peek notNil and:[self peek isDigitRadix:10]] whileTrue:[
|
|
212 |
value = (value * 10) + self peek digitValue.
|
|
213 |
self next
|
|
214 |
].
|
|
215 |
^ value
|
|
216 |
!
|
|
217 |
|
|
218 |
nextWord
|
|
219 |
"read the next word (i.e. up to non letter-or-digit).
|
|
220 |
return a string containing those characters.
|
2
|
221 |
- reimplemented for speed on String-Streams for faster scanning"
|
1
|
222 |
%{
|
|
223 |
/* speedup, if collection is a string */
|
|
224 |
|
|
225 |
int pos, limit, sz;
|
|
226 |
int len;
|
|
227 |
char buffer[1024];
|
|
228 |
register unsigned char *cp;
|
|
229 |
register unsigned ch;
|
|
230 |
|
|
231 |
if (_isString(_INST(collection))
|
|
232 |
&& _isSmallInteger(_INST(position))
|
|
233 |
&& _isSmallInteger(_INST(readLimit))) {
|
|
234 |
pos = _intVal(_INST(position));
|
|
235 |
limit = _intVal(_INST(readLimit));
|
|
236 |
sz = _qSize(_INST(collection)) - OHDR_SIZE;
|
|
237 |
if (sz < limit)
|
|
238 |
limit = sz;
|
|
239 |
cp = _stringVal(_INST(collection)) + pos - 1;
|
|
240 |
|
|
241 |
for (;;) {
|
|
242 |
if (pos > limit) break;
|
|
243 |
ch = *cp;
|
|
244 |
|
|
245 |
if (ch > ' ') break;
|
|
246 |
if ((ch != ' ') && (ch != '\t') && (ch != '\r')
|
|
247 |
&& (ch != '\n') && (ch != 0x0b)) break;
|
|
248 |
cp++;
|
|
249 |
pos++;
|
|
250 |
}
|
|
251 |
|
|
252 |
len = 0;
|
|
253 |
for (;;) {
|
|
254 |
if (pos > limit) break;
|
|
255 |
ch = *cp & 0xFF;
|
|
256 |
|
|
257 |
if (! (((ch >= 'a') && (ch <= 'z')) ||
|
|
258 |
((ch >= 'A') && (ch <= 'Z')) ||
|
|
259 |
((ch >= '0') && (ch <= '9'))))
|
|
260 |
break;
|
|
261 |
buffer[len++] = ch;
|
|
262 |
if (len >= (sizeof(buffer)-1)) {
|
|
263 |
/* emergency */
|
|
264 |
break;
|
|
265 |
}
|
|
266 |
pos++;
|
|
267 |
cp++;
|
|
268 |
}
|
|
269 |
|
|
270 |
_INST(position) = _MKSMALLINT(pos);
|
|
271 |
buffer[len] = '\0';
|
|
272 |
RETURN ( (len != 0) ? _MKSTRING(buffer COMMA_CON) : nil );
|
|
273 |
}
|
|
274 |
%}
|
|
275 |
.
|
|
276 |
^ super nextWord
|
|
277 |
!
|
|
278 |
|
|
279 |
nextSymbol
|
|
280 |
"read the next selector-symbol (i.e. up to non letter-or-digit).
|
|
281 |
return a string containing those characters.
|
2
|
282 |
- reimplemented for speed on String-Streams for faster scanning"
|
1
|
283 |
%{
|
|
284 |
int pos, limit, sz;
|
|
285 |
int len;
|
|
286 |
char buffer[1024];
|
|
287 |
register unsigned char *cp;
|
|
288 |
register unsigned ch;
|
|
289 |
|
|
290 |
if (_isString(_INST(collection))
|
|
291 |
&& _isSmallInteger(_INST(position))
|
|
292 |
&& _isSmallInteger(_INST(readLimit))) {
|
|
293 |
pos = _intVal(_INST(position));
|
|
294 |
limit = _intVal(_INST(readLimit));
|
|
295 |
sz = _qSize(_INST(collection)) - OHDR_SIZE;
|
|
296 |
if (sz < limit)
|
|
297 |
limit = sz;
|
|
298 |
cp = _stringVal(_INST(collection)) + pos - 1;
|
|
299 |
|
|
300 |
len = 0;
|
|
301 |
for (;;) {
|
|
302 |
if (pos > limit) break;
|
|
303 |
ch = *cp;
|
|
304 |
|
|
305 |
if (! (((ch >= 'a') && (ch <= 'z')) ||
|
|
306 |
((ch >= 'A') && (ch <= 'Z')) ||
|
|
307 |
((ch >= '0') && (ch <= '9')) ||
|
|
308 |
(ch == ':')))
|
|
309 |
break;
|
|
310 |
buffer[len++] = ch;
|
|
311 |
if (len >= (sizeof(buffer)-1)) {
|
|
312 |
/* emergency */
|
|
313 |
break;
|
|
314 |
}
|
|
315 |
pos++;
|
|
316 |
cp++;
|
|
317 |
}
|
|
318 |
|
|
319 |
_INST(position) = _MKSMALLINT(pos);
|
|
320 |
buffer[len] = '\0';
|
|
321 |
RETURN ( (len != 0) ? _MKSTRING(buffer COMMA_CON) : nil );
|
|
322 |
}
|
|
323 |
%}
|
|
324 |
.
|
|
325 |
^ super nextSymbol
|
|
326 |
!
|
|
327 |
|
2
|
328 |
nextMatchFor:anObject
|
|
329 |
"skip all objects up-to and including anObject, return anObject on success,
|
|
330 |
nil if end-of-file is reached before. The next read operation will return
|
|
331 |
the element after anObject.
|
|
332 |
- reimplemented for speed on String-Streams for faster scanning"
|
1
|
333 |
|
|
334 |
%{ /* NOCONTEXT */
|
|
335 |
|
|
336 |
if (_isString(_INST(collection))
|
|
337 |
&& _isCharacter(anObject)
|
|
338 |
&& _isSmallInteger(_INST(position))
|
|
339 |
&& _isSmallInteger(_INST(readLimit))) {
|
|
340 |
REGISTER unsigned char *chars;
|
|
341 |
REGISTER int pos, limit;
|
2
|
342 |
unsigned ch;
|
1
|
343 |
|
|
344 |
pos = _intVal(_INST(position));
|
|
345 |
if (pos <= 0) {
|
|
346 |
RETURN ( nil );
|
|
347 |
}
|
|
348 |
|
|
349 |
limit = _intVal(_INST(readLimit));
|
|
350 |
if (limit > (_qSize(_INST(collection)) - OHDR_SIZE))
|
|
351 |
limit = _qSize(_INST(collection)) - OHDR_SIZE;
|
|
352 |
|
|
353 |
chars = (unsigned char *)(_stringVal(_INST(collection)) + pos - 1);
|
|
354 |
ch = _intVal(_characterVal(anObject)) & 0xFF;
|
|
355 |
while (pos < limit) {
|
|
356 |
if (*chars == ch) {
|
|
357 |
ch = *++chars;
|
|
358 |
pos++;
|
|
359 |
_INST(position) = _MKSMALLINT(pos);
|
2
|
360 |
RETURN ( anObject );
|
1
|
361 |
}
|
|
362 |
chars++;
|
|
363 |
pos++;
|
|
364 |
}
|
|
365 |
RETURN ( nil );
|
|
366 |
}
|
|
367 |
%}
|
|
368 |
.
|
2
|
369 |
^ super nextMatchFor:anObject
|
1
|
370 |
!
|
|
371 |
|
|
372 |
|
|
373 |
skipSeparators
|
|
374 |
"skip all whitespace; next will return next non-white-space element.
|
2
|
375 |
- reimplemented for speed on String-Streams for faster scanning"
|
1
|
376 |
|
|
377 |
%{ /* NOCONTEXT */
|
|
378 |
|
|
379 |
if (_isString(_INST(collection))
|
|
380 |
&& _isSmallInteger(_INST(position))
|
|
381 |
&& _isSmallInteger(_INST(readLimit))) {
|
|
382 |
REGISTER unsigned char *chars;
|
|
383 |
REGISTER unsigned ch;
|
|
384 |
REGISTER int pos;
|
|
385 |
int limit;
|
|
386 |
|
|
387 |
pos = _intVal(_INST(position));
|
|
388 |
if (pos <= 0) {
|
|
389 |
RETURN ( nil );
|
|
390 |
}
|
|
391 |
|
|
392 |
limit = _intVal(_INST(readLimit));
|
|
393 |
if (limit > (_qSize(_INST(collection)) - OHDR_SIZE))
|
|
394 |
limit = _qSize(_INST(collection)) - OHDR_SIZE;
|
|
395 |
|
|
396 |
chars = (unsigned char *)(_stringVal(_INST(collection)) + pos - 1);
|
|
397 |
while (pos <= limit) {
|
|
398 |
ch = *chars++;
|
|
399 |
if ((ch != ' ') && (ch != '\t') && (ch != '\r')
|
|
400 |
&& (ch != '\n') && (ch != 0x0B)) {
|
|
401 |
_INST(position) = _MKSMALLINT(pos);
|
|
402 |
RETURN ( _MKCHARACTER(ch) );
|
|
403 |
}
|
|
404 |
pos++;
|
|
405 |
}
|
|
406 |
_INST(position) = _MKSMALLINT(pos);
|
|
407 |
RETURN ( nil );
|
|
408 |
}
|
|
409 |
%}
|
|
410 |
.
|
|
411 |
^ super skipSeparators
|
|
412 |
!
|
|
413 |
|
|
414 |
skipToAll:aCollection
|
|
415 |
"skip for the sequence given by the argument, aCollection;
|
|
416 |
return nil if not found, self otherwise. On a successful match, next read
|
|
417 |
will return elements of aCollection."
|
|
418 |
|
|
419 |
|oldPos buffer l first idx|
|
|
420 |
|
|
421 |
oldPos := self position.
|
|
422 |
l := aCollection size.
|
|
423 |
first := aCollection at:1.
|
|
424 |
[self atEnd] whileFalse:[
|
|
425 |
buffer := self next:l.
|
|
426 |
buffer = aCollection ifTrue:[
|
|
427 |
self position:(self position - l).
|
|
428 |
^ self
|
|
429 |
].
|
|
430 |
idx := buffer indexOf:first startingAt:2.
|
|
431 |
idx == 0 ifFalse:[
|
|
432 |
self position:(self position - l + idx - 1)
|
|
433 |
]
|
|
434 |
].
|
|
435 |
self position:oldPos.
|
|
436 |
^ nil
|
|
437 |
|
|
438 |
"|s|
|
|
439 |
s := ReadStream on:'12345678901234567890'.
|
|
440 |
s skipToAll:'901'.
|
|
441 |
s next:4"
|
|
442 |
! !
|
|
443 |
|
|
444 |
!ReadStream methodsFor:'access-writing'!
|
|
445 |
|
|
446 |
nextPut:anElement
|
|
447 |
^ self error:'ReadStreams cannot write'
|
|
448 |
! !
|