author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Mon, 12 Jul 2010 17:20:36 +0100 | |
branch | jv |
changeset 17780 | b6e42c92eba0 |
parent 17772 | 6e0b3c06c364 |
child 17807 | 06cc6c49e291 |
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 |
" |
|
5810 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
1 | 14 |
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
|
15 |
instanceVariableNames:'' |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
16 |
classVariableNames:'' |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
17 |
poolDictionaries:'' |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
18 |
category:'Streams' |
1 | 19 |
! |
20 |
||
2162
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
21 |
!ReadStream class methodsFor:'documentation'! |
88 | 22 |
|
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
202 | 26 |
All Rights Reserved |
1 | 27 |
|
88 | 28 |
This software is furnished under a license and may be used |
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
1 | 36 |
|
88 | 37 |
documentation |
38 |
" |
|
39 |
ReadStream defines protocol for reading streamwise over collections. |
|
1295 | 40 |
|
41 |
[author:] |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
42 |
Claus Gittinger |
1295 | 43 |
|
88 | 44 |
" |
45 |
! ! |
|
1 | 46 |
|
9305
6836bc44f3c3
warn when ReadStream with: is used.
Claus Gittinger <cg@exept.de>
parents:
8987
diff
changeset
|
47 |
!ReadStream class methodsFor:'blocked instance creation'! |
6836bc44f3c3
warn when ReadStream with: is used.
Claus Gittinger <cg@exept.de>
parents:
8987
diff
changeset
|
48 |
|
6836bc44f3c3
warn when ReadStream with: is used.
Claus Gittinger <cg@exept.de>
parents:
8987
diff
changeset
|
49 |
with:aCollection |
9458
ea81e322a954
off-by-one error in #nextBytes:into:startingAt:
Stefan Vogel <sv@exept.de>
parents:
9457
diff
changeset
|
50 |
"with on readStream makes no sense |
ea81e322a954
off-by-one error in #nextBytes:into:startingAt:
Stefan Vogel <sv@exept.de>
parents:
9457
diff
changeset
|
51 |
- what do you want to read from the end of a collection?" |
ea81e322a954
off-by-one error in #nextBytes:into:startingAt:
Stefan Vogel <sv@exept.de>
parents:
9457
diff
changeset
|
52 |
<resource:#obsolete> |
ea81e322a954
off-by-one error in #nextBytes:into:startingAt:
Stefan Vogel <sv@exept.de>
parents:
9457
diff
changeset
|
53 |
|
ea81e322a954
off-by-one error in #nextBytes:into:startingAt:
Stefan Vogel <sv@exept.de>
parents:
9457
diff
changeset
|
54 |
self obsoleteMethodWarning:'WARNING: with: should not be used for ReadStreams - use #on:'. |
9305
6836bc44f3c3
warn when ReadStream with: is used.
Claus Gittinger <cg@exept.de>
parents:
8987
diff
changeset
|
55 |
^ super with:aCollection |
6836bc44f3c3
warn when ReadStream with: is used.
Claus Gittinger <cg@exept.de>
parents:
8987
diff
changeset
|
56 |
! ! |
8987
b0d7be646112
#nextBytes:into:startingAt: now works, when fetching into a bit array.
Stefan Vogel <sv@exept.de>
parents:
8914
diff
changeset
|
57 |
|
202 | 58 |
!ReadStream methodsFor:'converting'! |
59 |
||
60 |
readStream |
|
61 |
"return a readStream from the receiver. Since this is already |
|
62 |
a readStream, return self." |
|
63 |
||
64 |
^ self |
|
8140
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
65 |
! |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
66 |
|
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
67 |
readStreamOrNil |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
68 |
"return a readStream from the receiver. Since this is already |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
69 |
a readStream, return self. |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
70 |
|
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
71 |
This method has been defined for protocol copmatibility with Filename" |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
72 |
|
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8098
diff
changeset
|
73 |
^ self |
202 | 74 |
! ! |
75 |
||
1400 | 76 |
!ReadStream methodsFor:'emphasis'! |
77 |
||
78 |
emphasis |
|
79 |
"return the emphasis of the current (i.e. next returned by #next) |
|
80 |
element. Streams on a string will return nil for all elements. |
|
81 |
Streams on collections which nothing at all about emphasises, |
|
82 |
will report an error." |
|
83 |
||
7114 | 84 |
^ collection emphasisAt:(position - ZeroPosition + 1). |
1400 | 85 |
|
86 |
" |
|
87 |
|t s| |
|
88 |
||
89 |
t := 'hello world' asText |
|
7114 | 90 |
emphasizeFrom:1 to:5 with:#bold; |
91 |
emphasizeFrom:7 to:11 with:#italic. |
|
1400 | 92 |
|
93 |
s := t readStream. |
|
94 |
[s atEnd] whileFalse:[ |
|
7114 | 95 |
Transcript show:(s emphasis); show:' '. |
96 |
Transcript show:''''; show:(s next); showCR:''''. |
|
1400 | 97 |
]. |
98 |
" |
|
99 |
||
100 |
"Modified: 15.5.1996 / 17:30:33 / cg" |
|
101 |
! ! |
|
102 |
||
345 | 103 |
!ReadStream methodsFor:'queries'! |
104 |
||
5310 | 105 |
copyFrom:beginning to:end |
106 |
^ collection copyFrom:beginning to:end |
|
107 |
! |
|
108 |
||
345 | 109 |
isReadable |
2997 | 110 |
"return true, if reading is supported by the recevier. |
111 |
Here, true is always returned." |
|
112 |
||
345 | 113 |
^ true |
2997 | 114 |
|
115 |
"Modified: 4.10.1997 / 17:59:21 / cg" |
|
1665
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
116 |
! |
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
117 |
|
6315 | 118 |
isWritable |
119 |
"return true, if writing is supported by the recevier. |
|
120 |
This has to be redefined in concrete subclasses." |
|
121 |
||
122 |
^ false |
|
123 |
! |
|
124 |
||
1665
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
125 |
size |
2997 | 126 |
"return the number of elements in the streamed collection." |
127 |
||
1665
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
128 |
^ collection size |
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
129 |
|
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
130 |
"Created: 13.9.1996 / 18:14:35 / cg" |
2997 | 131 |
"Modified: 4.10.1997 / 17:59:45 / cg" |
345 | 132 |
! ! |
133 |
||
59 | 134 |
!ReadStream methodsFor:'reading'! |
1 | 135 |
|
136 |
next |
|
137 |
"return the next element; advance read pointer. |
|
2 | 138 |
return nil, if there is no next element. |
345 | 139 |
- tuned for a bit more speed on String/ByteArray/Array-Streams" |
1 | 140 |
|
141 |
|ret| |
|
142 |
||
143 |
%{ /* NOCONTEXT */ |
|
144 |
||
145 |
REGISTER int pos; |
|
146 |
unsigned ch; |
|
252 | 147 |
OBJ coll, p, l; |
1 | 148 |
|
1133 | 149 |
coll = __INST(collection); |
150 |
p = __INST(position); |
|
151 |
l = __INST(readLimit); |
|
1 | 152 |
|
252 | 153 |
if (__isNonNilObject(coll) && __bothSmallInteger(p, l)) { |
154 |
||
8098
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
155 |
pos = __intVal(p); |
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
156 |
/* make 1-based */ |
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
157 |
pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition)); |
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
158 |
if (pos > 0 && pos <= __intVal(l)) { |
8614 | 159 |
OBJ cls, ret; |
59 | 160 |
|
8098
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
161 |
cls = __qClass(coll); |
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
162 |
if (cls == @global(String)) { |
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
163 |
if (pos <= __stringSize(coll)) { |
8614 | 164 |
ch = __stringVal(coll)[pos-1]; |
165 |
ret = __MKCHARACTER(ch); |
|
8913 | 166 |
__INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1); |
8614 | 167 |
RETURN ( ret ); |
8098
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
168 |
} |
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
169 |
} else if (cls == @global(ByteArray)) { |
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
170 |
if (pos <= __byteArraySize(coll)) { |
8614 | 171 |
ch = __ByteArrayInstPtr(coll)->ba_element[pos-1]; |
8913 | 172 |
ret = __mkSmallInteger(ch); |
173 |
__INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1); |
|
8614 | 174 |
RETURN ( ret ); |
8098
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
175 |
} |
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
176 |
} else if (cls == @global(Unicode16String)) { |
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
177 |
if (pos <= __unicode16StringSize(coll)) { |
8614 | 178 |
ch = __Unicode16StringInstPtr(coll)->s_element[pos-1]; |
179 |
ret = __MKUCHARACTER(ch); |
|
8913 | 180 |
__INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1); |
8614 | 181 |
RETURN ( ret ); |
8098
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
182 |
} |
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
183 |
} else if (cls == @global(Array)) { |
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
184 |
if (pos <= __arraySize(coll)) { |
8614 | 185 |
ret = __ArrayInstPtr(coll)->a_element[pos-1]; |
8913 | 186 |
__INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1); |
8614 | 187 |
RETURN ( ret ); |
8098
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
188 |
} |
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
189 |
} |
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
190 |
} |
1 | 191 |
} |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
192 |
%}. |
8614 | 193 |
((position + 1 - ZeroPosition) > readLimit) ifTrue:[^ self pastEndRead]. |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
194 |
ret := collection at:(position + 1 - ZeroPosition). |
1 | 195 |
position := position + 1. |
196 |
^ ret |
|
197 |
! |
|
198 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
199 |
next:count |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
200 |
"return the next count elements of the stream as aCollection, |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
201 |
which depends on the streams type - (see #contentsSpecies)." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
202 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
203 |
|answer| |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
204 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
205 |
self contentsSpecies = collection class ifTrue:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
206 |
((position + count - ZeroPosition) > readLimit) ifFalse:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
207 |
answer := collection copyFrom:position+1 to:position+count. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
208 |
position := position+count. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
209 |
^ answer |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
210 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
211 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
212 |
^ super next:count |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
213 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
214 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
215 |
#[1 2 3 4 5 6 7 8 9] readStream |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
216 |
next; |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
217 |
next:5; |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
218 |
next. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
219 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
220 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
221 |
|
252 | 222 |
nextAlphaNumericWord |
1 | 223 |
"read the next word (i.e. up to non letter-or-digit). |
224 |
return a string containing those characters. |
|
3112
ccf7121ddae8
nextAlphaNumericWord changed
Claus Gittinger <cg@exept.de>
parents:
2997
diff
changeset
|
225 |
Skips any non-alphanumeric chars first. |
59 | 226 |
- tuned for speed on String-Streams for faster scanning" |
1 | 227 |
%{ |
228 |
/* speedup, if collection is a string */ |
|
229 |
||
230 |
int pos, limit, sz; |
|
231 |
int len; |
|
13 | 232 |
char buffer[256]; |
233 |
REGISTER unsigned char *cp; |
|
234 |
REGISTER unsigned ch; |
|
252 | 235 |
OBJ coll, p, l; |
1 | 236 |
|
1133 | 237 |
coll = __INST(collection); |
238 |
p = __INST(position); |
|
239 |
l = __INST(readLimit); |
|
252 | 240 |
|
17735 | 241 |
if (__isStringLike(coll) && __bothSmallInteger(p, l)) { |
252 | 242 |
|
17735 | 243 |
pos = __intVal(p); |
244 |
/* make 1-based */ |
|
245 |
pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition)); |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
246 |
|
17735 | 247 |
limit = __intVal(l); |
248 |
sz = __qSize(coll) - OHDR_SIZE; |
|
249 |
if (sz < limit) |
|
250 |
limit = sz; |
|
251 |
cp = __stringVal(coll) + pos - 1; |
|
1 | 252 |
|
17735 | 253 |
for (;;) { |
254 |
if (pos > limit) break; |
|
255 |
ch = *cp; |
|
1 | 256 |
|
17735 | 257 |
if (((ch >= 'a') && (ch <= 'z')) || |
258 |
((ch >= 'A') && (ch <= 'Z')) || |
|
259 |
((ch >= '0') && (ch <= '9'))) |
|
260 |
break; |
|
261 |
cp++; |
|
262 |
pos++; |
|
263 |
} |
|
1 | 264 |
|
17735 | 265 |
len = 0; |
266 |
for (;;) { |
|
267 |
if (pos > limit) break; |
|
268 |
ch = *cp & 0xFF; |
|
1 | 269 |
|
17735 | 270 |
if (! (((ch >= 'a') && (ch <= 'z')) || |
271 |
((ch >= 'A') && (ch <= 'Z')) || |
|
272 |
((ch >= '0') && (ch <= '9')))) |
|
273 |
break; |
|
274 |
buffer[len++] = ch; |
|
275 |
if (len >= (sizeof(buffer)-1)) { |
|
276 |
/* emergency */ |
|
277 |
break; |
|
278 |
} |
|
279 |
pos++; |
|
280 |
cp++; |
|
281 |
} |
|
1 | 282 |
|
17735 | 283 |
/* make ZeroPosition-based */ |
284 |
pos = pos - 1 + __intVal( @global(PositionableStream:ZeroPosition)); |
|
285 |
__INST(position) = __mkSmallInteger(pos); |
|
286 |
buffer[len] = '\0'; |
|
287 |
RETURN ( (len != 0) ? __MKSTRING_L(buffer, len) : nil ); |
|
1 | 288 |
} |
289 |
%} |
|
290 |
. |
|
252 | 291 |
^ super nextAlphaNumericWord |
1 | 292 |
! |
293 |
||
1665
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
294 |
nextByte |
4826
ff30434f0feb
#nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents:
4522
diff
changeset
|
295 |
"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
|
296 |
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
|
297 |
- 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
|
298 |
|
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
299 |
|ret| |
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
300 |
|
4826
ff30434f0feb
#nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents:
4522
diff
changeset
|
301 |
%{ /* NOCONTEXT */ |
ff30434f0feb
#nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents:
4522
diff
changeset
|
302 |
|
ff30434f0feb
#nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents:
4522
diff
changeset
|
303 |
REGISTER int pos; |
ff30434f0feb
#nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents:
4522
diff
changeset
|
304 |
unsigned ch; |
ff30434f0feb
#nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents:
4522
diff
changeset
|
305 |
OBJ coll, p, l; |
ff30434f0feb
#nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents:
4522
diff
changeset
|
306 |
|
ff30434f0feb
#nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents:
4522
diff
changeset
|
307 |
coll = __INST(collection); |
ff30434f0feb
#nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents:
4522
diff
changeset
|
308 |
p = __INST(position); |
ff30434f0feb
#nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents:
4522
diff
changeset
|
309 |
l = __INST(readLimit); |
ff30434f0feb
#nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents:
4522
diff
changeset
|
310 |
|
ff30434f0feb
#nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents:
4522
diff
changeset
|
311 |
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
|
312 |
|
8098
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
313 |
pos = __intVal(p); |
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
314 |
/* make 1-based */ |
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
315 |
pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition)); |
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
316 |
if (pos > 0 && pos <= __intVal(l)) { |
8614 | 317 |
OBJ cls, ret; |
4826
ff30434f0feb
#nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents:
4522
diff
changeset
|
318 |
|
8098
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
319 |
cls = __qClass(coll); |
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
320 |
if (cls == @global(String)) { |
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
321 |
if (pos <= __stringSize(coll)) { |
8614 | 322 |
ch = __stringVal(coll)[pos-1]; |
8913 | 323 |
ret = __mkSmallInteger(ch); |
324 |
__INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1); |
|
8614 | 325 |
RETURN ( ret ); |
8098
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
326 |
} |
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
327 |
} else if (cls == @global(ByteArray)) { |
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
328 |
if (pos <= __byteArraySize(coll)) { |
8614 | 329 |
ch = __ByteArrayInstPtr(coll)->ba_element[pos-1]; |
8913 | 330 |
ret = __mkSmallInteger(ch); |
331 |
__INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1); |
|
8614 | 332 |
RETURN ( ret ); |
8098
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
333 |
} |
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
334 |
} else if (cls == @global(Array)) { |
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
335 |
if (pos <= __arraySize(coll)) { |
8614 | 336 |
ret = __ArrayInstPtr(coll)->a_element[pos-1]; |
8913 | 337 |
__INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1); |
8614 | 338 |
RETURN ( ret ); |
8098
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
339 |
} |
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
340 |
} |
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
341 |
} |
4826
ff30434f0feb
#nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents:
4522
diff
changeset
|
342 |
} |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
343 |
%}. |
8614 | 344 |
((position + 1 - ZeroPosition) > readLimit) ifTrue:[^ self pastEndRead]. |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
345 |
ret := collection at:(position + 1 -ZeroPosition). |
4826
ff30434f0feb
#nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents:
4522
diff
changeset
|
346 |
position := position + 1. |
8098
8d1d5cd8529b
nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents:
7483
diff
changeset
|
347 |
^ ret asInteger |
1665
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
348 |
! |
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
349 |
|
8530
edcadeea17a1
tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents:
8140
diff
changeset
|
350 |
nextBytes:numBytes into:aCollection startingAt:initialIndex |
edcadeea17a1
tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents:
8140
diff
changeset
|
351 |
"return the next numBytes from the stream. If the end is |
edcadeea17a1
tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents:
8140
diff
changeset
|
352 |
reached before, only that many bytes are copyied into the |
edcadeea17a1
tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents:
8140
diff
changeset
|
353 |
collection. |
edcadeea17a1
tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents:
8140
diff
changeset
|
354 |
Returns the number of bytes that have been actually read. |
edcadeea17a1
tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents:
8140
diff
changeset
|
355 |
The receiver must support reading of binary bytes. |
edcadeea17a1
tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents:
8140
diff
changeset
|
356 |
|
edcadeea17a1
tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents:
8140
diff
changeset
|
357 |
Notice: this method is provided here for protocol completeness |
edcadeea17a1
tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents:
8140
diff
changeset
|
358 |
with externalStreams - it is normally not used with other |
edcadeea17a1
tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents:
8140
diff
changeset
|
359 |
streams." |
edcadeea17a1
tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents:
8140
diff
changeset
|
360 |
|
9458
ea81e322a954
off-by-one error in #nextBytes:into:startingAt:
Stefan Vogel <sv@exept.de>
parents:
9457
diff
changeset
|
361 |
((initialIndex + numBytes - 1) <= aCollection size |
ea81e322a954
off-by-one error in #nextBytes:into:startingAt:
Stefan Vogel <sv@exept.de>
parents:
9457
diff
changeset
|
362 |
and:[(position + numBytes) <= readLimit |
ea81e322a954
off-by-one error in #nextBytes:into:startingAt:
Stefan Vogel <sv@exept.de>
parents:
9457
diff
changeset
|
363 |
and:[collection isByteCollection |
ea81e322a954
off-by-one error in #nextBytes:into:startingAt:
Stefan Vogel <sv@exept.de>
parents:
9457
diff
changeset
|
364 |
and:[aCollection isByteCollection |
ea81e322a954
off-by-one error in #nextBytes:into:startingAt:
Stefan Vogel <sv@exept.de>
parents:
9457
diff
changeset
|
365 |
]]]) ifTrue:[ |
ea81e322a954
off-by-one error in #nextBytes:into:startingAt:
Stefan Vogel <sv@exept.de>
parents:
9457
diff
changeset
|
366 |
"do it the fast way" |
ea81e322a954
off-by-one error in #nextBytes:into:startingAt:
Stefan Vogel <sv@exept.de>
parents:
9457
diff
changeset
|
367 |
aCollection |
ea81e322a954
off-by-one error in #nextBytes:into:startingAt:
Stefan Vogel <sv@exept.de>
parents:
9457
diff
changeset
|
368 |
replaceBytesFrom:initialIndex to:(initialIndex + numBytes - 1) |
ea81e322a954
off-by-one error in #nextBytes:into:startingAt:
Stefan Vogel <sv@exept.de>
parents:
9457
diff
changeset
|
369 |
with:collection startingAt:position+1. |
ea81e322a954
off-by-one error in #nextBytes:into:startingAt:
Stefan Vogel <sv@exept.de>
parents:
9457
diff
changeset
|
370 |
position := position + numBytes. |
ea81e322a954
off-by-one error in #nextBytes:into:startingAt:
Stefan Vogel <sv@exept.de>
parents:
9457
diff
changeset
|
371 |
^ numBytes |
8530
edcadeea17a1
tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents:
8140
diff
changeset
|
372 |
]. |
9458
ea81e322a954
off-by-one error in #nextBytes:into:startingAt:
Stefan Vogel <sv@exept.de>
parents:
9457
diff
changeset
|
373 |
"do it the hard way" |
8530
edcadeea17a1
tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents:
8140
diff
changeset
|
374 |
^ super nextBytes:numBytes into:aCollection startingAt:initialIndex |
edcadeea17a1
tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents:
8140
diff
changeset
|
375 |
|
edcadeea17a1
tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents:
8140
diff
changeset
|
376 |
" |
edcadeea17a1
tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents:
8140
diff
changeset
|
377 |
|s n buffer| |
edcadeea17a1
tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents:
8140
diff
changeset
|
378 |
|
edcadeea17a1
tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents:
8140
diff
changeset
|
379 |
buffer := ByteArray new:10. |
edcadeea17a1
tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents:
8140
diff
changeset
|
380 |
|
edcadeea17a1
tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents:
8140
diff
changeset
|
381 |
s := ReadStream on:#[1 2 3 4 5 6 7 8 9]. |
edcadeea17a1
tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents:
8140
diff
changeset
|
382 |
s next:3. |
edcadeea17a1
tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents:
8140
diff
changeset
|
383 |
n := s nextBytes:9 into:buffer startingAt:1. |
edcadeea17a1
tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents:
8140
diff
changeset
|
384 |
Transcript showCR:('n = %1; buffer = <%2>' bindWith:n with:buffer) |
edcadeea17a1
tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents:
8140
diff
changeset
|
385 |
" |
edcadeea17a1
tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents:
8140
diff
changeset
|
386 |
! |
edcadeea17a1
tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents:
8140
diff
changeset
|
387 |
|
611 | 388 |
nextDecimalInteger |
389 |
"read the next integer in radix 10. dont skip whitespace. |
|
390 |
- tuned for speed on String-Streams for faster scanning" |
|
391 |
||
392 |
|value nextOne| |
|
393 |
%{ |
|
394 |
int pos, limit, sz; |
|
395 |
REGISTER unsigned char *cp; |
|
396 |
REGISTER unsigned ch; |
|
8914 | 397 |
INT val = 0; |
611 | 398 |
OBJ coll, p, l; |
399 |
||
1133 | 400 |
coll = __INST(collection); |
401 |
p = __INST(position); |
|
402 |
l = __INST(readLimit); |
|
611 | 403 |
|
17735 | 404 |
if (__isStringLike(coll) && __bothSmallInteger(p, l)) { |
611 | 405 |
|
17735 | 406 |
pos = __intVal(p); |
407 |
/* make 1-based */ |
|
408 |
pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition)); |
|
409 |
limit = __intVal(l); |
|
410 |
sz = __qSize(coll) - OHDR_SIZE; |
|
411 |
if (sz < limit) |
|
412 |
limit = sz; |
|
413 |
cp = __stringVal(coll) + pos - 1; |
|
611 | 414 |
|
17735 | 415 |
for (;;) { |
416 |
if (pos > limit) break; |
|
417 |
ch = *cp; |
|
611 | 418 |
|
17735 | 419 |
if ((ch < '0') || (ch > '9')) break; |
420 |
val = val * 10 + (ch - '0'); |
|
421 |
pos++; |
|
422 |
if (val > (_MAX_INT / 10)) goto oops; |
|
423 |
cp++; |
|
424 |
} |
|
425 |
/* make ZeroPosition-based */ |
|
426 |
pos = pos - 1 + __intVal( @global(PositionableStream:ZeroPosition)); |
|
427 |
__INST(position) = __mkSmallInteger(pos); |
|
428 |
RETURN (__mkSmallInteger(val)); |
|
611 | 429 |
} |
430 |
oops: |
|
8913 | 431 |
value = __mkSmallInteger(val); |
611 | 432 |
%} |
433 |
. |
|
434 |
"fall-back for non-string streams - we have to continue where |
|
435 |
above primitive left off, in case of a large integer ... |
|
436 |
(instead of doing a super nextDecimalInteger)" |
|
437 |
||
438 |
nextOne := self peek. |
|
439 |
[nextOne notNil and:[nextOne isDigitRadix:10]] whileTrue:[ |
|
17735 | 440 |
value := (value * 10) + nextOne digitValue. |
441 |
nextOne := self nextPeek |
|
611 | 442 |
]. |
443 |
^ value |
|
444 |
! |
|
445 |
||
9457 | 446 |
nextOrNil |
447 |
"return the next element; advance read pointer. |
|
448 |
return nil, if there is no next element. |
|
449 |
- tuned for a bit more speed on String/ByteArray/Array-Streams" |
|
450 |
||
451 |
|ret| |
|
452 |
||
453 |
%{ /* NOCONTEXT */ |
|
454 |
||
455 |
REGISTER int pos; |
|
456 |
unsigned ch; |
|
457 |
OBJ coll, p, l; |
|
458 |
||
459 |
coll = __INST(collection); |
|
460 |
p = __INST(position); |
|
461 |
l = __INST(readLimit); |
|
462 |
||
463 |
if (__isNonNilObject(coll) && __bothSmallInteger(p, l)) { |
|
464 |
pos = __intVal(p); |
|
465 |
/* make 1-based */ |
|
466 |
pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition)); |
|
467 |
if (pos > 0) { |
|
468 |
OBJ cls, ret; |
|
469 |
||
470 |
if (pos > __intVal(l)) { |
|
471 |
RETURN(nil); |
|
472 |
} |
|
473 |
||
474 |
cls = __qClass(coll); |
|
475 |
if (cls == @global(String)) { |
|
476 |
if (pos <= __stringSize(coll)) { |
|
477 |
ch = __stringVal(coll)[pos-1]; |
|
478 |
ret = __MKCHARACTER(ch); |
|
479 |
__INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1); |
|
480 |
RETURN ( ret ); |
|
481 |
} |
|
482 |
} else if (cls == @global(ByteArray)) { |
|
483 |
if (pos <= __byteArraySize(coll)) { |
|
484 |
ch = __ByteArrayInstPtr(coll)->ba_element[pos-1]; |
|
485 |
ret = __mkSmallInteger(ch); |
|
486 |
__INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1); |
|
487 |
RETURN ( ret ); |
|
488 |
} |
|
489 |
} else if (cls == @global(Unicode16String)) { |
|
490 |
if (pos <= __unicode16StringSize(coll)) { |
|
491 |
ch = __Unicode16StringInstPtr(coll)->s_element[pos-1]; |
|
492 |
ret = __MKUCHARACTER(ch); |
|
493 |
__INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1); |
|
494 |
RETURN ( ret ); |
|
495 |
} |
|
496 |
} else if (cls == @global(Array)) { |
|
497 |
if (pos <= __arraySize(coll)) { |
|
498 |
ret = __ArrayInstPtr(coll)->a_element[pos-1]; |
|
499 |
__INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1); |
|
500 |
RETURN ( ret ); |
|
501 |
} |
|
502 |
} |
|
503 |
} |
|
504 |
} |
|
505 |
%}. |
|
506 |
ret := collection at:(position + 1 - ZeroPosition). |
|
507 |
position := position + 1. |
|
508 |
^ ret |
|
509 |
! |
|
510 |
||
611 | 511 |
nextPeek |
512 |
"advance read pointer return the peek element. |
|
513 |
this is equivalent to (self next; peek). |
|
514 |
- tuned for speed on String-Streams for faster scanning" |
|
515 |
||
516 |
%{ /* NOCONTEXT */ |
|
517 |
OBJ coll, l, p; |
|
518 |
||
1133 | 519 |
coll = __INST(collection); |
520 |
p = __INST(position); |
|
521 |
l = __INST(readLimit); |
|
611 | 522 |
|
17735 | 523 |
if (__isStringLike(coll) && __bothSmallInteger(p, l)) { |
8614 | 524 |
REGISTER int pos; |
525 |
unsigned ch; |
|
611 | 526 |
|
8614 | 527 |
pos = __intVal(p); |
528 |
/* make 1-based */ |
|
529 |
pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition)); |
|
530 |
if ((pos > 0) && (pos < __intVal(l)) && (pos < __stringSize(coll))) { |
|
531 |
pos = pos + 1; |
|
532 |
ch = __stringVal(coll)[pos-1]; |
|
533 |
/* make ZeroPosition-based */ |
|
534 |
pos = pos - 1 + __intVal( @global(PositionableStream:ZeroPosition)); |
|
8913 | 535 |
__INST(position) = __mkSmallInteger(pos); |
8614 | 536 |
RETURN ( __MKCHARACTER(ch) ); |
537 |
} |
|
611 | 538 |
} |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
539 |
%}. |
8614 | 540 |
((position + 1 - ZeroPosition) > readLimit) ifTrue:[^ self pastEndRead]. |
611 | 541 |
position := position + 1. |
8614 | 542 |
((position + 1 - ZeroPosition) > readLimit) ifTrue:[^ self pastEndRead]. |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
543 |
^ collection at:(position + 1 - ZeroPosition) |
611 | 544 |
! |
545 |
||
546 |
peek |
|
547 |
"return the next element; do NOT advance read pointer. |
|
548 |
return nil, if there is no next element. |
|
549 |
- tuned for a bit more speed on String/ByteArray/Array-Streams" |
|
1 | 550 |
|
551 |
%{ /* NOCONTEXT */ |
|
611 | 552 |
|
553 |
REGISTER int pos; |
|
554 |
unsigned ch; |
|
555 |
OBJ coll; |
|
556 |
OBJ cls, p, l; |
|
1 | 557 |
|
1133 | 558 |
coll = __INST(collection); |
559 |
p = __INST(position); |
|
560 |
l = __INST(readLimit); |
|
252 | 561 |
|
611 | 562 |
if (__isNonNilObject(coll) && __bothSmallInteger(p, l)) { |
1 | 563 |
|
8614 | 564 |
pos = __intVal(p); |
565 |
/* make 1-based */ |
|
566 |
pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition)); |
|
567 |
if (pos <= __intVal(l) && pos > 0) { |
|
568 |
cls = __qClass(coll); |
|
569 |
if (cls == @global(String)) { |
|
570 |
if (pos <= __stringSize(coll)) { |
|
571 |
ch = __stringVal(coll)[pos-1]; |
|
572 |
RETURN ( __MKCHARACTER(ch) ); |
|
573 |
} |
|
574 |
} else if (cls == @global(ByteArray)) { |
|
575 |
if (pos <= __byteArraySize(coll)) { |
|
576 |
ch = __ByteArrayInstPtr(coll)->ba_element[pos-1]; |
|
8913 | 577 |
RETURN ( __mkSmallInteger(ch) ); |
8614 | 578 |
} |
579 |
} else if (cls == @global(Array)) { |
|
580 |
if (pos <= __arraySize(coll)) { |
|
581 |
RETURN ( __ArrayInstPtr(coll)->a_element[pos-1]); |
|
582 |
} |
|
583 |
} |
|
584 |
} |
|
1 | 585 |
} |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
586 |
%}. |
8614 | 587 |
((position + 1 - ZeroPosition) > readLimit) ifTrue:[^ self pastEndRead]. |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
588 |
^ collection at:(position + 1 - ZeroPosition) |
1 | 589 |
! |
590 |
||
3316 | 591 |
peekOrNil |
592 |
"return the next element; do NOT advance read pointer. |
|
593 |
return nil, if there is no next element. |
|
594 |
This is much like #peek - |
|
595 |
However, unlike #peek, this does not raise an atEnd-query signal - even |
|
596 |
if handled. Instead, nil is returned immediately." |
|
597 |
||
598 |
%{ /* NOCONTEXT */ |
|
599 |
||
600 |
REGISTER int pos; |
|
601 |
unsigned ch; |
|
602 |
OBJ coll; |
|
603 |
OBJ cls, p, l; |
|
604 |
||
605 |
coll = __INST(collection); |
|
606 |
p = __INST(position); |
|
607 |
l = __INST(readLimit); |
|
608 |
||
609 |
if (__isNonNilObject(coll) && __bothSmallInteger(p, l)) { |
|
610 |
||
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
611 |
pos = __intVal(p); |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
612 |
/* make 1-based */ |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
613 |
pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition)); |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
614 |
if (pos <= __intVal(l) && pos > 0) { |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
615 |
cls = __qClass(coll); |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
616 |
if (cls == @global(String)) { |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
617 |
if (pos <= __stringSize(coll)) { |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
618 |
ch = __stringVal(coll)[pos-1]; |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
619 |
RETURN ( __MKCHARACTER(ch) ); |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
620 |
} |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
621 |
RETURN ( nil ); |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
622 |
} else if (cls == @global(ByteArray)) { |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
623 |
if (pos <= __byteArraySize(coll)) { |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
624 |
ch = __ByteArrayInstPtr(coll)->ba_element[pos-1]; |
8913 | 625 |
RETURN ( __mkSmallInteger(ch) ); |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
626 |
} |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
627 |
RETURN ( nil ); |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
628 |
} else if (cls == @global(Array)) { |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
629 |
if (pos <= __arraySize(coll)) { |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
630 |
RETURN ( __ArrayInstPtr(coll)->a_element[pos-1]); |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
631 |
} |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
632 |
RETURN ( nil ); |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
633 |
} |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
634 |
} |
3316 | 635 |
} |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
636 |
%}. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
637 |
((position + 1 - ZeroPosition) > readLimit) ifTrue:[^ nil]. |
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
638 |
^ collection at:(position + 1 - ZeroPosition) |
3316 | 639 |
! |
640 |
||
1 | 641 |
skipSeparators |
642 |
"skip all whitespace; next will return next non-white-space element. |
|
59 | 643 |
Return the peeked character or nil, if the end-of-stream was reached. |
2 | 644 |
- reimplemented for speed on String-Streams for faster scanning" |
1 | 645 |
|
646 |
%{ /* NOCONTEXT */ |
|
252 | 647 |
OBJ coll, p, l; |
1 | 648 |
|
1133 | 649 |
coll = __INST(collection); |
650 |
p = __INST(position); |
|
651 |
l = __INST(readLimit); |
|
252 | 652 |
|
17735 | 653 |
if (__isStringLike(coll) && __bothSmallInteger(p, l)) { |
654 |
REGISTER unsigned char *chars; |
|
655 |
REGISTER unsigned ch; |
|
656 |
REGISTER int pos; |
|
657 |
int limit; |
|
658 |
int sz; |
|
1 | 659 |
|
17735 | 660 |
pos = __intVal(p); |
661 |
/* make 1-based */ |
|
662 |
pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition)); |
|
663 |
if (pos <= 0) { |
|
664 |
RETURN ( nil ); |
|
665 |
} |
|
1 | 666 |
|
17735 | 667 |
limit = __intVal(l); |
668 |
sz = __qSize(coll) - OHDR_SIZE; |
|
669 |
if (limit > sz) { |
|
670 |
limit = sz; |
|
671 |
} |
|
1 | 672 |
|
17735 | 673 |
chars = (unsigned char *)(__stringVal(coll) + pos - 1); |
674 |
while (pos <= limit) { |
|
675 |
pos++; |
|
676 |
ch = *chars++; |
|
677 |
if ((ch > 0x20) |
|
678 |
|| ((ch != ' ') |
|
679 |
&& (ch != '\t') |
|
680 |
&& (ch != '\r') |
|
681 |
&& (ch != '\n') |
|
682 |
&& (ch != '\f') |
|
683 |
&& (ch != 0x0B))) { |
|
684 |
/* make ZeroPosition-based */ |
|
685 |
pos = pos - 1 + __intVal( @global(PositionableStream:ZeroPosition)); |
|
686 |
__INST(position) = __mkSmallInteger(pos-1); |
|
687 |
RETURN ( __MKCHARACTER(ch) ); |
|
688 |
} |
|
689 |
} |
|
690 |
/* make ZeroPosition-based */ |
|
691 |
pos = pos - 1 + __intVal( @global(PositionableStream:ZeroPosition)); |
|
692 |
__INST(position) = __mkSmallInteger(pos); |
|
693 |
RETURN ( nil ); |
|
1 | 694 |
} |
1406 | 695 |
%}. |
1 | 696 |
^ super skipSeparators |
1406 | 697 |
|
698 |
" |
|
699 |
|s| |
|
700 |
||
701 |
s := ' hello world ' readStream. |
|
702 |
s skipSeparators. |
|
703 |
s next. |
|
704 |
" |
|
1 | 705 |
! |
706 |
||
13 | 707 |
skipSeparatorsExceptCR |
708 |
"skip all whitespace except newlines; |
|
202 | 709 |
next will return next non-white-space element. |
710 |
- reimplemented for speed on String-Streams for faster scanning" |
|
711 |
||
13 | 712 |
%{ /* NOCONTEXT */ |
713 |
||
252 | 714 |
OBJ coll, p, l; |
715 |
||
1133 | 716 |
coll = __INST(collection); |
717 |
p = __INST(position); |
|
718 |
l = __INST(readLimit); |
|
252 | 719 |
|
17735 | 720 |
if (__isStringLike(coll) && __bothSmallInteger(p, l)) { |
721 |
REGISTER unsigned char *chars; |
|
722 |
REGISTER unsigned ch; |
|
723 |
REGISTER int pos; |
|
724 |
int limit; |
|
13 | 725 |
|
17735 | 726 |
pos = __intVal(p); |
727 |
/* make 1-based */ |
|
728 |
pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition)); |
|
729 |
if (pos <= 0) { |
|
730 |
RETURN ( nil ); |
|
731 |
} |
|
2162
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
732 |
|
17735 | 733 |
limit = __intVal(l); |
734 |
if (limit > (__qSize(coll) - OHDR_SIZE)) |
|
735 |
limit = __qSize(coll) - OHDR_SIZE; |
|
13 | 736 |
|
17735 | 737 |
chars = (unsigned char *)(__stringVal(coll) + pos - 1); |
738 |
while (pos <= limit) { |
|
739 |
ch = *chars++; |
|
740 |
if (((int)ch > 0x20) |
|
741 |
|| ( |
|
742 |
(ch != ' ') |
|
743 |
&& (ch != '\t') |
|
744 |
&& (ch != '\f') |
|
745 |
&& (ch != '\b') |
|
746 |
&& (ch != 0x0B))) { |
|
747 |
/* make ZeroPosition-based */ |
|
748 |
pos = pos - 1 + __intVal( @global(PositionableStream:ZeroPosition)); |
|
749 |
__INST(position) = __mkSmallInteger(pos); |
|
750 |
RETURN ( __MKCHARACTER(ch) ); |
|
751 |
} |
|
752 |
pos++; |
|
753 |
} |
|
754 |
/* make ZeroPosition-based */ |
|
755 |
pos = pos - 1 + __intVal( @global(PositionableStream:ZeroPosition)); |
|
756 |
__INST(position) = __mkSmallInteger(pos); |
|
757 |
RETURN ( nil ); |
|
13 | 758 |
} |
759 |
%} |
|
760 |
. |
|
761 |
^ super skipSeparatorsExceptCR |
|
762 |
! |
|
763 |
||
611 | 764 |
skipThrough:anObject |
6011
3b91d7834ca5
fixed: skipThrough dod not position atEnd if the element was not found.
Claus Gittinger <cg@exept.de>
parents:
5810
diff
changeset
|
765 |
"skip all objects up-to and including anObject. |
3b91d7834ca5
fixed: skipThrough dod not position atEnd if the element was not found.
Claus Gittinger <cg@exept.de>
parents:
5810
diff
changeset
|
766 |
Return the receiver if skip was successful, |
3b91d7834ca5
fixed: skipThrough dod not position atEnd if the element was not found.
Claus Gittinger <cg@exept.de>
parents:
5810
diff
changeset
|
767 |
otherwise (i.e. if not found) return nil and leave the stream positioned at the end. |
611 | 768 |
On success, the next read operation will return the element after anObject. |
769 |
- reimplemented for speed on String-Streams for faster scanning" |
|
770 |
||
771 |
%{ /* NOCONTEXT */ |
|
772 |
OBJ coll, p, l; |
|
773 |
||
1133 | 774 |
coll = __INST(collection); |
775 |
p = __INST(position); |
|
776 |
l = __INST(readLimit); |
|
611 | 777 |
|
17735 | 778 |
if (__isStringLike(coll) |
611 | 779 |
&& __isCharacter(anObject) |
780 |
&& __bothSmallInteger(p, l)) { |
|
17735 | 781 |
REGISTER unsigned char *chars; |
782 |
REGISTER int pos, limit; |
|
783 |
unsigned ch; |
|
784 |
int sz; |
|
611 | 785 |
|
17735 | 786 |
pos = __intVal(p); |
787 |
/* make 1-based */ |
|
788 |
pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition)); |
|
789 |
if (pos <= 0) { |
|
790 |
RETURN ( nil ); |
|
791 |
} |
|
611 | 792 |
|
17735 | 793 |
limit = __intVal(l); |
794 |
sz = __stringSize(coll); |
|
795 |
if (limit > sz) limit = sz; |
|
611 | 796 |
|
17735 | 797 |
chars = (unsigned char *)(__stringVal(coll) + pos - 1); |
798 |
ch = __intVal(_characterVal(anObject)) & 0xFF; |
|
799 |
while (pos < limit) { |
|
800 |
if (*chars == ch) { |
|
801 |
ch = *++chars; |
|
802 |
pos++; |
|
803 |
/* make ZeroPosition-based */ |
|
804 |
pos = pos - 1 + __intVal( @global(PositionableStream:ZeroPosition)); |
|
805 |
__INST(position) = __mkSmallInteger(pos); |
|
806 |
RETURN ( self ); |
|
807 |
} |
|
808 |
chars++; |
|
809 |
pos++; |
|
810 |
} |
|
811 |
/* make ZeroPosition-based */ |
|
812 |
pos = pos - 1 + __intVal( @global(PositionableStream:ZeroPosition)); |
|
813 |
__INST(position) = __mkSmallInteger(pos+1); |
|
814 |
RETURN ( nil ); |
|
611 | 815 |
} |
6011
3b91d7834ca5
fixed: skipThrough dod not position atEnd if the element was not found.
Claus Gittinger <cg@exept.de>
parents:
5810
diff
changeset
|
816 |
%}. |
611 | 817 |
^ super skipThrough:anObject |
1 | 818 |
! ! |
819 |
||
59 | 820 |
!ReadStream methodsFor:'writing'! |
1 | 821 |
|
822 |
nextPut:anElement |
|
10 | 823 |
"catch write access to readstreams - report an error" |
824 |
||
825 |
self shouldNotImplement |
|
1 | 826 |
! ! |
611 | 827 |
|
2162
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
828 |
!ReadStream class methodsFor:'documentation'! |
701 | 829 |
|
830 |
version |
|
17780 | 831 |
^ '$Id: ReadStream.st 10544 2010-07-12 16:20:36Z vranyj1 $' |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
832 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
833 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
834 |
version_CVS |
17780 | 835 |
^ 'Header: /cvs/stx/stx/libbasic/ReadStream.st,v 1.64 2009/11/05 16:25:23 stefan Exp ' |
17761
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17735
diff
changeset
|
836 |
! |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
837 |
|
17761
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17735
diff
changeset
|
838 |
version_SVN |
17780 | 839 |
^ '$Id: ReadStream.st 10544 2010-07-12 16:20:36Z vranyj1 $' |
17761
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17735
diff
changeset
|
840 |
! ! |
17767 | 841 |
|
17772
6e0b3c06c364
Method reverted to r10532
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17767
diff
changeset
|
842 |
|
17780 | 843 |