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