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