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