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