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