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