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