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