author | Claus Gittinger <cg@exept.de> |
Thu, 25 Apr 1996 19:02:53 +0200 | |
changeset 1295 | 83f594f05c52 |
parent 1138 | 993e6ffdbf51 |
child 1400 | 0ee12d945849 |
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 |
||
13 |
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
|
14 |
instanceVariableNames:'' |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
15 |
classVariableNames:'' |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
16 |
poolDictionaries:'' |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
17 |
category:'Streams' |
1 | 18 |
! |
19 |
||
88 | 20 |
!ReadStream class methodsFor:'documentation'! |
21 |
||
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
202 | 25 |
All Rights Reserved |
1 | 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 |
! |
|
1 | 35 |
|
88 | 36 |
documentation |
37 |
" |
|
38 |
ReadStream defines protocol for reading streamwise over collections. |
|
1295 | 39 |
|
40 |
[author:] |
|
41 |
Claus Gittinger |
|
42 |
||
88 | 43 |
" |
44 |
! ! |
|
1 | 45 |
|
202 | 46 |
!ReadStream methodsFor:'converting'! |
47 |
||
48 |
readStream |
|
49 |
"return a readStream from the receiver. Since this is already |
|
50 |
a readStream, return self." |
|
51 |
||
52 |
^ self |
|
53 |
! ! |
|
54 |
||
345 | 55 |
!ReadStream methodsFor:'queries'! |
56 |
||
57 |
isReadable |
|
58 |
^ true |
|
59 |
! ! |
|
60 |
||
59 | 61 |
!ReadStream methodsFor:'reading'! |
1 | 62 |
|
63 |
next |
|
64 |
"return the next element; advance read pointer. |
|
2 | 65 |
return nil, if there is no next element. |
345 | 66 |
- tuned for a bit more speed on String/ByteArray/Array-Streams" |
1 | 67 |
|
68 |
|ret| |
|
69 |
||
70 |
%{ /* NOCONTEXT */ |
|
71 |
||
72 |
REGISTER int pos; |
|
73 |
unsigned ch; |
|
252 | 74 |
OBJ coll, p, l; |
1 | 75 |
|
1133 | 76 |
coll = __INST(collection); |
77 |
p = __INST(position); |
|
78 |
l = __INST(readLimit); |
|
1 | 79 |
|
252 | 80 |
if (__isNonNilObject(coll) && __bothSmallInteger(p, l)) { |
81 |
||
1133 | 82 |
pos = __intVal(p); |
83 |
if (pos > 0 && pos <= __intVal(l)) { |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
84 |
OBJ cls; |
59 | 85 |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
86 |
cls = __qClass(coll); |
1133 | 87 |
if (cls == @global(String)) { |
88 |
if (pos <= __stringSize(coll)) { |
|
89 |
__INST(position) = __MKSMALLINT(pos + 1); |
|
90 |
ch = __stringVal(coll)[pos-1]; |
|
91 |
RETURN ( __MKCHARACTER(ch) ); |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
92 |
} |
1133 | 93 |
} else if (cls == @global(ByteArray)) { |
94 |
if (pos <= __byteArraySize(coll)) { |
|
95 |
__INST(position) = __MKSMALLINT(pos + 1); |
|
96 |
ch = __ByteArrayInstPtr(coll)->ba_element[pos-1]; |
|
97 |
RETURN ( __MKSMALLINT(ch) ); |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
98 |
} |
1133 | 99 |
} else if (cls == @global(Array)) { |
100 |
if (pos <= __arraySize(coll)) { |
|
101 |
__INST(position) = __MKSMALLINT(pos + 1); |
|
102 |
RETURN ( __ArrayInstPtr(coll)->a_element[pos-1]); |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
103 |
} |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
104 |
} |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
105 |
} |
1 | 106 |
} |
107 |
%} |
|
108 |
. |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
109 |
(position > readLimit) ifTrue:[^ self pastEnd]. |
1 | 110 |
ret := collection at:position. |
111 |
position := position + 1. |
|
112 |
^ ret |
|
113 |
! |
|
114 |
||
252 | 115 |
nextAlphaNumericWord |
1 | 116 |
"read the next word (i.e. up to non letter-or-digit). |
117 |
return a string containing those characters. |
|
59 | 118 |
- tuned for speed on String-Streams for faster scanning" |
1 | 119 |
%{ |
120 |
/* speedup, if collection is a string */ |
|
121 |
||
122 |
int pos, limit, sz; |
|
123 |
int len; |
|
13 | 124 |
char buffer[256]; |
125 |
REGISTER unsigned char *cp; |
|
126 |
REGISTER unsigned ch; |
|
252 | 127 |
OBJ coll, p, l; |
1 | 128 |
|
1133 | 129 |
coll = __INST(collection); |
130 |
p = __INST(position); |
|
131 |
l = __INST(readLimit); |
|
252 | 132 |
|
133 |
if (__isString(coll) && __bothSmallInteger(p, l)) { |
|
134 |
||
1133 | 135 |
pos = __intVal(p); |
136 |
limit = __intVal(l); |
|
329 | 137 |
sz = __qSize(coll) - OHDR_SIZE; |
202 | 138 |
if (sz < limit) |
139 |
limit = sz; |
|
1133 | 140 |
cp = __stringVal(coll) + pos - 1; |
1 | 141 |
|
202 | 142 |
for (;;) { |
143 |
if (pos > limit) break; |
|
144 |
ch = *cp; |
|
1 | 145 |
|
202 | 146 |
if (ch > ' ') break; |
147 |
if ((ch != ' ') && (ch != '\t') && (ch != '\r') |
|
148 |
&& (ch != '\n') && (ch != 0x0b)) break; |
|
149 |
cp++; |
|
150 |
pos++; |
|
151 |
} |
|
1 | 152 |
|
202 | 153 |
len = 0; |
154 |
for (;;) { |
|
155 |
if (pos > limit) break; |
|
156 |
ch = *cp & 0xFF; |
|
1 | 157 |
|
202 | 158 |
if (! (((ch >= 'a') && (ch <= 'z')) || |
159 |
((ch >= 'A') && (ch <= 'Z')) || |
|
160 |
((ch >= '0') && (ch <= '9')))) |
|
161 |
break; |
|
162 |
buffer[len++] = ch; |
|
163 |
if (len >= (sizeof(buffer)-1)) { |
|
164 |
/* emergency */ |
|
165 |
break; |
|
166 |
} |
|
167 |
pos++; |
|
168 |
cp++; |
|
169 |
} |
|
1 | 170 |
|
1133 | 171 |
__INST(position) = __MKSMALLINT(pos); |
202 | 172 |
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
|
173 |
RETURN ( (len != 0) ? __MKSTRING_L(buffer, len COMMA_CON) : nil ); |
1 | 174 |
} |
175 |
%} |
|
176 |
. |
|
252 | 177 |
^ super nextAlphaNumericWord |
1 | 178 |
! |
179 |
||
611 | 180 |
nextDecimalInteger |
181 |
"read the next integer in radix 10. dont skip whitespace. |
|
182 |
- tuned for speed on String-Streams for faster scanning" |
|
183 |
||
184 |
|value nextOne| |
|
185 |
%{ |
|
186 |
int pos, limit, sz; |
|
187 |
REGISTER unsigned char *cp; |
|
188 |
REGISTER unsigned ch; |
|
189 |
int val = 0; |
|
190 |
OBJ coll, p, l; |
|
191 |
||
1133 | 192 |
coll = __INST(collection); |
193 |
p = __INST(position); |
|
194 |
l = __INST(readLimit); |
|
611 | 195 |
|
196 |
if (__isString(coll) && __bothSmallInteger(p, l)) { |
|
197 |
||
1133 | 198 |
pos = __intVal(p); |
199 |
limit = __intVal(l); |
|
611 | 200 |
sz = __qSize(coll) - OHDR_SIZE; |
201 |
if (sz < limit) |
|
202 |
limit = sz; |
|
1133 | 203 |
cp = __stringVal(coll) + pos - 1; |
611 | 204 |
|
205 |
for (;;) { |
|
206 |
if (pos > limit) break; |
|
207 |
ch = *cp; |
|
208 |
||
209 |
if ((ch < '0') || (ch > '9')) break; |
|
210 |
val = val * 10 + (ch - '0'); |
|
211 |
pos++; |
|
212 |
if (val > (_MAX_INT / 10)) goto oops; |
|
213 |
cp++; |
|
214 |
} |
|
1133 | 215 |
__INST(position) = __MKSMALLINT(pos); |
216 |
return __MKSMALLINT(val); |
|
611 | 217 |
} |
218 |
oops: |
|
1133 | 219 |
value = __MKSMALLINT(val); |
611 | 220 |
%} |
221 |
. |
|
222 |
"fall-back for non-string streams - we have to continue where |
|
223 |
above primitive left off, in case of a large integer ... |
|
224 |
(instead of doing a super nextDecimalInteger)" |
|
225 |
||
226 |
nextOne := self peek. |
|
227 |
[nextOne notNil and:[nextOne isDigitRadix:10]] whileTrue:[ |
|
228 |
value = (value * 10) + nextOne digitValue. |
|
229 |
nextOne := self nextPeek |
|
230 |
]. |
|
231 |
^ value |
|
232 |
! |
|
233 |
||
234 |
nextPeek |
|
235 |
"advance read pointer return the peek element. |
|
236 |
this is equivalent to (self next; peek). |
|
237 |
- tuned for speed on String-Streams for faster scanning" |
|
238 |
||
239 |
%{ /* NOCONTEXT */ |
|
240 |
OBJ coll, l, p; |
|
241 |
||
1133 | 242 |
coll = __INST(collection); |
243 |
p = __INST(position); |
|
244 |
l = __INST(readLimit); |
|
611 | 245 |
|
246 |
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
|
247 |
REGISTER int pos; |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
248 |
unsigned ch; |
611 | 249 |
|
1133 | 250 |
pos = __intVal(p); |
251 |
if ((pos > 0) && (pos < __intVal(l)) && (pos < __stringSize(coll))) { |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
252 |
pos += 1; |
1133 | 253 |
__INST(position) = __MKSMALLINT(pos); |
254 |
ch = __stringVal(coll)[pos-1]; |
|
255 |
RETURN ( __MKCHARACTER(ch) ); |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
256 |
} |
611 | 257 |
} |
258 |
%} |
|
259 |
. |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
260 |
(position > readLimit) ifTrue:[^ self pastEnd]. |
611 | 261 |
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
|
262 |
(position > readLimit) ifTrue:[^ self pastEnd]. |
611 | 263 |
^ collection at:position |
264 |
! |
|
265 |
||
1 | 266 |
nextSymbol |
267 |
"read the next selector-symbol (i.e. up to non letter-or-digit). |
|
268 |
return a string containing those characters. |
|
59 | 269 |
- tuned for speed on String-Streams for faster scanning" |
1 | 270 |
%{ |
271 |
int pos, limit, sz; |
|
272 |
int len; |
|
13 | 273 |
char buffer[256]; |
274 |
REGISTER unsigned char *cp; |
|
275 |
REGISTER unsigned ch; |
|
252 | 276 |
OBJ coll, p, l; |
1 | 277 |
|
1133 | 278 |
coll = __INST(collection); |
279 |
p = __INST(position); |
|
280 |
l = __INST(readLimit); |
|
252 | 281 |
|
282 |
if (__isString(coll) && __bothSmallInteger(p, l)) { |
|
283 |
||
1133 | 284 |
pos = __intVal(p); |
285 |
limit = __intVal(l); |
|
329 | 286 |
sz = __qSize(coll) - OHDR_SIZE; |
202 | 287 |
if (sz < limit) |
288 |
limit = sz; |
|
1133 | 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 |
|
1133 | 310 |
__INST(position) = __MKSMALLINT(pos); |
202 | 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 |
|
1133 | 331 |
coll = __INST(collection); |
332 |
p = __INST(position); |
|
333 |
l = __INST(readLimit); |
|
252 | 334 |
|
611 | 335 |
if (__isNonNilObject(coll) && __bothSmallInteger(p, l)) { |
1 | 336 |
|
1133 | 337 |
pos = __intVal(p); |
338 |
if (pos <= __intVal(l) && pos > 0) { |
|
970
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); |
1133 | 340 |
if (cls == @global(String)) { |
341 |
if (pos <= __stringSize(coll)) { |
|
342 |
ch = __stringVal(coll)[pos-1]; |
|
343 |
RETURN ( __MKCHARACTER(ch) ); |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
344 |
} |
1133 | 345 |
} else if (cls == @global(ByteArray)) { |
346 |
if (pos <= __byteArraySize(coll)) { |
|
347 |
ch = __ByteArrayInstPtr(coll)->ba_element[pos-1]; |
|
348 |
RETURN ( __MKSMALLINT(ch) ); |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
349 |
} |
1133 | 350 |
} else if (cls == @global(Array)) { |
351 |
if (pos <= __arraySize(coll)) { |
|
352 |
RETURN ( __ArrayInstPtr(coll)->a_element[pos-1]); |
|
970
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 |
|
1133 | 371 |
coll = __INST(collection); |
372 |
p = __INST(position); |
|
373 |
l = __INST(readLimit); |
|
252 | 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 |
|
1133 | 381 |
pos = __intVal(p); |
202 | 382 |
if (pos <= 0) { |
383 |
RETURN ( nil ); |
|
384 |
} |
|
1 | 385 |
|
1133 | 386 |
limit = __intVal(l); |
329 | 387 |
if (limit > (__qSize(coll) - OHDR_SIZE)) |
388 |
limit = __qSize(coll) - OHDR_SIZE; |
|
1 | 389 |
|
1133 | 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)) { |
|
1133 | 395 |
__INST(position) = __MKSMALLINT(pos); |
396 |
RETURN ( __MKCHARACTER(ch) ); |
|
202 | 397 |
} |
398 |
pos++; |
|
399 |
} |
|
1133 | 400 |
__INST(position) = __MKSMALLINT(pos); |
202 | 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 |
||
1133 | 417 |
coll = __INST(collection); |
418 |
p = __INST(position); |
|
419 |
l = __INST(readLimit); |
|
252 | 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 |
|
1133 | 427 |
pos = __intVal(p); |
202 | 428 |
if (pos <= 0) { |
429 |
RETURN ( nil ); |
|
430 |
} |
|
13 | 431 |
|
1133 | 432 |
limit = __intVal(l); |
329 | 433 |
if (limit > (__qSize(coll) - OHDR_SIZE)) |
434 |
limit = __qSize(coll) - OHDR_SIZE; |
|
13 | 435 |
|
1133 | 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)) { |
|
1133 | 440 |
__INST(position) = __MKSMALLINT(pos); |
441 |
RETURN ( __MKCHARACTER(ch) ); |
|
202 | 442 |
} |
443 |
pos++; |
|
444 |
} |
|
1133 | 445 |
__INST(position) = __MKSMALLINT(pos); |
202 | 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 |
||
1133 | 462 |
coll = __INST(collection); |
463 |
p = __INST(position); |
|
464 |
l = __INST(readLimit); |
|
611 | 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 |
||
1133 | 474 |
pos = __intVal(p); |
611 | 475 |
if (pos <= 0) { |
476 |
RETURN ( nil ); |
|
477 |
} |
|
478 |
||
1133 | 479 |
limit = __intVal(l); |
480 |
sz = __stringSize(coll); |
|
611 | 481 |
if (limit > sz) limit = sz; |
482 |
||
1133 | 483 |
chars = (unsigned char *)(__stringVal(coll) + pos - 1); |
484 |
ch = __intVal(_characterVal(anObject)) & 0xFF; |
|
611 | 485 |
while (pos < limit) { |
486 |
if (*chars == ch) { |
|
487 |
ch = *++chars; |
|
488 |
pos++; |
|
1133 | 489 |
__INST(position) = __MKSMALLINT(pos); |
611 | 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 |
|
1295 | 545 |
^ '$Header: /cvs/stx/stx/libbasic/Attic/ReadStr.st,v 1.26 1996-04-25 17:00:45 cg Exp $' |
701 | 546 |
! ! |