author | Claus Gittinger <cg@exept.de> |
Thu, 18 Jan 2018 15:27:49 +0100 | |
changeset 22446 | f3eddd51bba2 |
parent 22236 | 14865585b530 |
child 22491 | e3e465028518 |
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 |
|
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 |
19436 | 161 |
"return true, if reading is supported by the receiver. |
2997 | 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 |
19436 | 170 |
"return true, if writing is supported by the receiver. |
6315 | 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 |
||
19924 | 262 |
collection notNil ifTrue:[ |
263 |
self contentsSpecies == collection class ifTrue:[ |
|
264 |
((position + count) > readLimit) ifFalse:[ |
|
265 |
answer := collection copyFrom:position+1 to:position+count. |
|
266 |
position := position+count. |
|
267 |
^ answer |
|
268 |
]. |
|
269 |
]. |
|
12176 | 270 |
]. |
271 |
^ super next:count |
|
272 |
||
273 |
" |
|
274 |
#[1 2 3 4 5 6 7 8 9] readStream |
|
19924 | 275 |
next; |
276 |
next:5; |
|
277 |
next. |
|
12176 | 278 |
" |
279 |
! |
|
280 |
||
252 | 281 |
nextAlphaNumericWord |
1 | 282 |
"read the next word (i.e. up to non letter-or-digit). |
283 |
return a string containing those characters. |
|
3112
ccf7121ddae8
nextAlphaNumericWord changed
Claus Gittinger <cg@exept.de>
parents:
2997
diff
changeset
|
284 |
Skips any non-alphanumeric chars first. |
59 | 285 |
- tuned for speed on String-Streams for faster scanning" |
1 | 286 |
%{ |
287 |
/* speedup, if collection is a string */ |
|
288 |
||
16882 | 289 |
INT pos, limit, sz; |
290 |
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
|
291 |
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
|
292 |
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
|
293 |
int bufferSize = sizeof(quickBuffer); |
13 | 294 |
REGISTER unsigned char *cp; |
295 |
REGISTER unsigned ch; |
|
252 | 296 |
OBJ coll, p, l; |
1 | 297 |
|
1133 | 298 |
coll = __INST(collection); |
299 |
p = __INST(position); |
|
300 |
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
|
301 |
|
12479
62b11c1f462e
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
12176
diff
changeset
|
302 |
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
|
303 |
OBJ retVal; |
252 | 304 |
|
16862 | 305 |
pos = __intVal(p); |
306 |
/* make 1-based */ |
|
307 |
pos = pos + 1; |
|
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
308 |
|
16862 | 309 |
limit = __intVal(l); |
310 |
sz = __qSize(coll) - OHDR_SIZE; |
|
311 |
if (sz < limit) |
|
312 |
limit = sz; |
|
313 |
cp = __stringVal(coll) + pos - 1; |
|
1 | 314 |
|
17597
a78c4a2bf366
ouch - bad call of MKSTRING_L passing a pointer into object memory
Claus Gittinger <cg@exept.de>
parents:
16882
diff
changeset
|
315 |
/* skip over non-alphanumeric characters */ |
16862 | 316 |
for (;;) { |
317 |
if (pos > limit) break; |
|
318 |
ch = *cp; |
|
1 | 319 |
|
16862 | 320 |
if (((ch >= 'a') && (ch <= 'z')) || |
321 |
((ch >= 'A') && (ch <= 'Z')) || |
|
322 |
((ch >= '0') && (ch <= '9'))) |
|
323 |
break; |
|
324 |
cp++; |
|
325 |
pos++; |
|
326 |
} |
|
1 | 327 |
|
17597
a78c4a2bf366
ouch - bad call of MKSTRING_L passing a pointer into object memory
Claus Gittinger <cg@exept.de>
parents:
16882
diff
changeset
|
328 |
/* collect non-alphanumeric characters */ |
16862 | 329 |
len = 0; |
330 |
for (;;) { |
|
331 |
if (pos > limit) break; |
|
332 |
ch = *cp & 0xFF; |
|
1 | 333 |
|
16862 | 334 |
if (! (((ch >= 'a') && (ch <= 'z')) || |
335 |
((ch >= 'A') && (ch <= 'Z')) || |
|
336 |
((ch >= '0') && (ch <= '9')))) |
|
337 |
break; |
|
338 |
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
|
339 |
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
|
340 |
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
|
341 |
|
a78c4a2bf366
ouch - bad call of MKSTRING_L passing a pointer into object memory
Claus Gittinger <cg@exept.de>
parents:
16882
diff
changeset
|
342 |
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
|
343 |
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
|
344 |
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
|
345 |
} else { |
a78c4a2bf366
ouch - bad call of MKSTRING_L passing a pointer into object memory
Claus Gittinger <cg@exept.de>
parents:
16882
diff
changeset
|
346 |
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
|
347 |
} |
a78c4a2bf366
ouch - bad call of MKSTRING_L passing a pointer into object memory
Claus Gittinger <cg@exept.de>
parents:
16882
diff
changeset
|
348 |
bufferSize = newBufferSize; |
16862 | 349 |
} |
350 |
pos++; |
|
351 |
cp++; |
|
352 |
} |
|
1 | 353 |
|
16862 | 354 |
pos = pos - 1; |
355 |
__INST(position) = __mkSmallInteger(pos); |
|
356 |
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
|
357 |
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
|
358 |
RETURN (nil); |
a78c4a2bf366
ouch - bad call of MKSTRING_L passing a pointer into object memory
Claus Gittinger <cg@exept.de>
parents:
16882
diff
changeset
|
359 |
} |
a78c4a2bf366
ouch - bad call of MKSTRING_L passing a pointer into object memory
Claus Gittinger <cg@exept.de>
parents:
16882
diff
changeset
|
360 |
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
|
361 |
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
|
362 |
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
|
363 |
} |
a78c4a2bf366
ouch - bad call of MKSTRING_L passing a pointer into object memory
Claus Gittinger <cg@exept.de>
parents:
16882
diff
changeset
|
364 |
RETURN ( retVal ); |
1 | 365 |
} |
366 |
%} |
|
367 |
. |
|
252 | 368 |
^ super nextAlphaNumericWord |
1 | 369 |
! |
370 |
||
1665
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
371 |
nextByte |
15840 | 372 |
"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
|
373 |
- 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
|
374 |
|
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
375 |
|ret| |
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
376 |
|
4826
ff30434f0feb
#nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents:
4522
diff
changeset
|
377 |
%{ /* NOCONTEXT */ |
ff30434f0feb
#nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents:
4522
diff
changeset
|
378 |
|
16882 | 379 |
REGISTER INT pos; |
4826
ff30434f0feb
#nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents:
4522
diff
changeset
|
380 |
unsigned ch; |
ff30434f0feb
#nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents:
4522
diff
changeset
|
381 |
OBJ coll, p, l; |
ff30434f0feb
#nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents:
4522
diff
changeset
|
382 |
|
ff30434f0feb
#nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents:
4522
diff
changeset
|
383 |
coll = __INST(collection); |
ff30434f0feb
#nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents:
4522
diff
changeset
|
384 |
p = __INST(position); |
ff30434f0feb
#nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents:
4522
diff
changeset
|
385 |
l = __INST(readLimit); |
ff30434f0feb
#nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents:
4522
diff
changeset
|
386 |
|
ff30434f0feb
#nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents:
4522
diff
changeset
|
387 |
if (__isNonNilObject(coll) && __bothSmallInteger(p, l)) { |
18266 | 388 |
pos = __intVal(p); |
389 |
if (pos >= 0 && pos < __intVal(l)) { |
|
390 |
OBJ cls, ret; |
|
4826
ff30434f0feb
#nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents:
4522
diff
changeset
|
391 |
|
18266 | 392 |
cls = __qClass(coll); |
393 |
if (cls == @global(String) || cls == @global(ImmutableString) || cls == @global(Symbol)) { |
|
394 |
if (pos < __stringSize(coll)) { |
|
395 |
ch = __stringVal(coll)[pos]; |
|
396 |
ret = __mkSmallInteger(ch); |
|
397 |
__INST(position) = __mkSmallInteger(pos+1); |
|
398 |
RETURN ( ret ); |
|
399 |
} |
|
22236 | 400 |
} else if (cls == @global(ByteArray) || cls == @global(ImmutableByteArray)) { |
18266 | 401 |
if (pos < __byteArraySize(coll)) { |
402 |
ch = __ByteArrayInstPtr(coll)->ba_element[pos]; |
|
403 |
ret = __mkSmallInteger(ch); |
|
404 |
__INST(position) = __mkSmallInteger(pos+1); |
|
405 |
RETURN ( ret ); |
|
406 |
} |
|
22236 | 407 |
} else if (cls == @global(Array) || cls == @global(ImmutableArray)) { |
18266 | 408 |
if (pos < __arraySize(coll)) { |
409 |
ret = __ArrayInstPtr(coll)->a_element[pos]; |
|
410 |
if (!__isSmallInteger(ret) || __intVal(ret) > 255) goto out; |
|
411 |
__INST(position) = __mkSmallInteger(pos+1); |
|
412 |
RETURN ( ret ); |
|
413 |
} |
|
414 |
} |
|
415 |
} |
|
4826
ff30434f0feb
#nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents:
4522
diff
changeset
|
416 |
} |
15840 | 417 |
out:; |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
418 |
%}. |
15356 | 419 |
(position >= readLimit) ifTrue:[^ self pastEndRead]. |
15840 | 420 |
ret := (collection at:(position + 1)) asInteger. |
421 |
ret > 255 ifTrue:[ |
|
18266 | 422 |
ret := ConversionError |
423 |
raiseRequestWith:self |
|
424 |
errorString:' - #nextByte trying to read a non-byte'. |
|
15840 | 425 |
]. |
4826
ff30434f0feb
#nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents:
4522
diff
changeset
|
426 |
position := position + 1. |
15840 | 427 |
^ ret |
1665
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
428 |
! |
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
429 |
|
9457 | 430 |
nextOrNil |
431 |
"return the next element; advance read pointer. |
|
432 |
return nil, if there is no next element. |
|
433 |
- tuned for a bit more speed on String/ByteArray/Array-Streams" |
|
434 |
||
435 |
|ret| |
|
436 |
||
437 |
%{ /* NOCONTEXT */ |
|
438 |
||
16882 | 439 |
REGISTER INT pos; |
9457 | 440 |
unsigned ch; |
441 |
OBJ coll, p, l; |
|
442 |
||
443 |
coll = __INST(collection); |
|
444 |
p = __INST(position); |
|
445 |
l = __INST(readLimit); |
|
446 |
||
447 |
if (__isNonNilObject(coll) && __bothSmallInteger(p, l)) { |
|
18266 | 448 |
pos = __intVal(p); |
449 |
if (pos >= 0) { |
|
450 |
OBJ cls, ret; |
|
9457 | 451 |
|
18266 | 452 |
if (pos >= __intVal(l)) { |
453 |
RETURN(nil); |
|
454 |
} |
|
9457 | 455 |
|
18266 | 456 |
cls = __qClass(coll); |
457 |
if (cls == @global(String) || cls == @global(ImmutableString) || cls == @global(Symbol)) { |
|
458 |
if (pos < __stringSize(coll)) { |
|
459 |
ch = __stringVal(coll)[pos]; |
|
460 |
ret = __MKCHARACTER(ch); |
|
461 |
__INST(position) = __mkSmallInteger(pos + 1); |
|
462 |
RETURN ( ret ); |
|
463 |
} |
|
464 |
} else if (cls == @global(ByteArray)) { |
|
465 |
if (pos < __byteArraySize(coll)) { |
|
466 |
ch = __ByteArrayInstPtr(coll)->ba_element[pos]; |
|
467 |
ret = __mkSmallInteger(ch); |
|
468 |
__INST(position) = __mkSmallInteger(pos + 1); |
|
469 |
RETURN ( ret ); |
|
470 |
} |
|
471 |
} else if (cls == @global(Unicode16String)) { |
|
472 |
if (pos < __unicode16StringSize(coll)) { |
|
473 |
ch = __unicode16StringVal(coll)[pos]; |
|
474 |
ret = __MKUCHARACTER(ch); |
|
475 |
__INST(position) = __mkSmallInteger(pos + 1); |
|
476 |
RETURN ( ret ); |
|
477 |
} |
|
478 |
} else if (cls == @global(Array)) { |
|
479 |
if (pos < __arraySize(coll)) { |
|
480 |
ret = __ArrayInstPtr(coll)->a_element[pos]; |
|
481 |
__INST(position) = __mkSmallInteger(pos + 1); |
|
482 |
RETURN ( ret ); |
|
483 |
} |
|
484 |
} |
|
485 |
} |
|
9457 | 486 |
} |
487 |
%}. |
|
488 |
position := position + 1. |
|
16882 | 489 |
ret := collection at:position. |
9457 | 490 |
^ ret |
491 |
! |
|
492 |
||
611 | 493 |
nextPeek |
494 |
"advance read pointer return the peek element. |
|
495 |
this is equivalent to (self next; peek). |
|
496 |
- tuned for speed on String-Streams for faster scanning" |
|
497 |
||
498 |
%{ /* NOCONTEXT */ |
|
20129 | 499 |
OBJ coll = __INST(collection); |
500 |
OBJ p = __INST(position); |
|
501 |
OBJ l = __INST(readLimit); |
|
611 | 502 |
|
12479
62b11c1f462e
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
12176
diff
changeset
|
503 |
if (__isStringLike(coll) && __bothSmallInteger(p, l)) { |
20129 | 504 |
REGISTER INT pos = __intVal(p) + 1; |
611 | 505 |
|
20129 | 506 |
if ((pos > 0) && (pos < __intVal(l)) && (pos < __stringSize(coll))) { |
507 |
unsigned int ch = __stringVal(coll)[pos]; |
|
508 |
__INST(position) = __mkSmallInteger(pos); |
|
509 |
RETURN ( __MKCHARACTER(ch) ); |
|
510 |
} |
|
611 | 511 |
} |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
512 |
%}. |
15356 | 513 |
(position >= readLimit) ifTrue:[^ self pastEndRead]. |
611 | 514 |
position := position + 1. |
15356 | 515 |
(position >= readLimit) ifTrue:[^ self pastEndRead]. |
15351 | 516 |
^ collection at:(position + 1) |
611 | 517 |
! |
518 |
||
20129 | 519 |
nextPeekOrNil |
520 |
"advance read pointer return the peek element. |
|
521 |
this is equivalent to (self next; peekOrNil). |
|
522 |
- tuned for speed on String-Streams for faster scanning" |
|
523 |
||
524 |
%{ /* NOCONTEXT */ |
|
525 |
OBJ coll = __INST(collection); |
|
526 |
OBJ p = __INST(position); |
|
527 |
OBJ l = __INST(readLimit); |
|
528 |
||
529 |
if (__isStringLike(coll) && __bothSmallInteger(p, l)) { |
|
530 |
REGISTER INT pos = __intVal(p) + 1; |
|
531 |
||
532 |
if ((pos > 0) && (pos < __intVal(l)) && (pos < __stringSize(coll))) { |
|
533 |
unsigned int ch = __stringVal(coll)[pos]; |
|
534 |
__INST(position) = __mkSmallInteger(pos); |
|
535 |
RETURN ( __MKCHARACTER(ch) ); |
|
536 |
} |
|
537 |
} |
|
538 |
%}. |
|
539 |
(position >= readLimit) ifTrue:[^ nil]. |
|
540 |
position := position + 1. |
|
541 |
(position >= readLimit) ifTrue:[^ nil]. |
|
542 |
^ collection at:(position + 1) |
|
543 |
! |
|
544 |
||
19232 | 545 |
nextUnicode16CharacterMSB:msb |
19560 | 546 |
^ Character value:(self nextUnsignedInt16MSB:msb) |
19232 | 547 |
|
548 |
" |
|
549 |
#[16r00 16r51] readStream nextUnicode16CharacterMSB:true |
|
550 |
#[16r00 16r51] readStream nextUnicode16CharacterMSB:false |
|
551 |
" |
|
552 |
! |
|
553 |
||
554 |
nextUnicode16Characters:count MSB:msb |
|
19560 | 555 |
"easily tuned, if heavily used" |
556 |
||
19232 | 557 |
^ (1 to:count) |
558 |
collect:[:i | self nextUnicode16CharacterMSB:msb] |
|
559 |
as:Unicode16String |
|
560 |
||
561 |
" |
|
562 |
#[16r00 16r51] readStream nextUnicode16Characters:1 MSB:true |
|
563 |
#[16r00 16r51] readStream nextUnicode16Characters:1 MSB:false |
|
564 |
" |
|
565 |
! |
|
566 |
||
611 | 567 |
peek |
568 |
"return the next element; do NOT advance read pointer. |
|
569 |
return nil, if there is no next element. |
|
570 |
- tuned for a bit more speed on String/ByteArray/Array-Streams" |
|
1 | 571 |
|
572 |
%{ /* NOCONTEXT */ |
|
611 | 573 |
|
16882 | 574 |
REGISTER INT pos; |
611 | 575 |
unsigned ch; |
576 |
OBJ coll; |
|
577 |
OBJ cls, p, l; |
|
1 | 578 |
|
1133 | 579 |
coll = __INST(collection); |
580 |
p = __INST(position); |
|
581 |
l = __INST(readLimit); |
|
252 | 582 |
|
611 | 583 |
if (__isNonNilObject(coll) && __bothSmallInteger(p, l)) { |
1 | 584 |
|
18266 | 585 |
pos = __intVal(p); |
586 |
if ((pos < __intVal(l)) && (pos >= 0)) { |
|
587 |
cls = __qClass(coll); |
|
588 |
if (cls == @global(String) || cls == @global(ImmutableString) || cls == @global(Symbol)) { |
|
589 |
if (pos < __stringSize(coll)) { |
|
590 |
ch = __stringVal(coll)[pos]; |
|
591 |
RETURN ( __MKCHARACTER(ch) ); |
|
592 |
} |
|
593 |
} else if (cls == @global(ByteArray)) { |
|
594 |
if (pos < __byteArraySize(coll)) { |
|
595 |
ch = __ByteArrayInstPtr(coll)->ba_element[pos]; |
|
596 |
RETURN ( __mkSmallInteger(ch) ); |
|
597 |
} |
|
598 |
} else if (cls == @global(Unicode16String)) { |
|
599 |
if (pos < __unicode16StringSize(coll)) { |
|
600 |
ch = __unicode16StringVal(coll)[pos]; |
|
601 |
RETURN(__MKUCHARACTER(ch)); |
|
602 |
} |
|
603 |
} else if (cls == @global(Array)) { |
|
604 |
if (pos < __arraySize(coll)) { |
|
605 |
RETURN ( __ArrayInstPtr(coll)->a_element[pos]); |
|
606 |
} |
|
607 |
} |
|
608 |
} |
|
1 | 609 |
} |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
610 |
%}. |
15356 | 611 |
(position >= readLimit) ifTrue:[^ self pastEndRead]. |
15351 | 612 |
^ collection at:(position + 1) |
1 | 613 |
! |
614 |
||
3316 | 615 |
peekOrNil |
616 |
"return the next element; do NOT advance read pointer. |
|
617 |
return nil, if there is no next element. |
|
618 |
This is much like #peek - |
|
619 |
However, unlike #peek, this does not raise an atEnd-query signal - even |
|
620 |
if handled. Instead, nil is returned immediately." |
|
621 |
||
622 |
%{ /* NOCONTEXT */ |
|
623 |
||
16882 | 624 |
REGISTER INT pos; |
3316 | 625 |
unsigned ch; |
626 |
OBJ coll; |
|
627 |
OBJ cls, p, l; |
|
628 |
||
629 |
coll = __INST(collection); |
|
630 |
p = __INST(position); |
|
631 |
l = __INST(readLimit); |
|
632 |
||
633 |
if (__isNonNilObject(coll) && __bothSmallInteger(p, l)) { |
|
18266 | 634 |
pos = __intVal(p); |
635 |
if ((pos < __intVal(l)) && (pos >= 0)) { |
|
636 |
cls = __qClass(coll); |
|
637 |
if (cls == @global(String) || cls == @global(ImmutableString) || cls == @global(Symbol)) { |
|
638 |
if (pos < __stringSize(coll)) { |
|
639 |
ch = __stringVal(coll)[pos]; |
|
640 |
RETURN ( __MKCHARACTER(ch) ); |
|
641 |
} |
|
642 |
RETURN ( nil ); |
|
643 |
} else if (cls == @global(ByteArray)) { |
|
644 |
if (pos < __byteArraySize(coll)) { |
|
645 |
ch = __ByteArrayInstPtr(coll)->ba_element[pos]; |
|
646 |
RETURN ( __mkSmallInteger(ch) ); |
|
647 |
} |
|
648 |
RETURN ( nil ); |
|
649 |
} else if (cls == @global(Unicode16String)) { |
|
650 |
if (pos < __unicode16StringSize(coll)) { |
|
651 |
ch = __unicode16StringVal(coll)[pos]; |
|
652 |
RETURN(__MKUCHARACTER(ch)); |
|
653 |
} |
|
654 |
RETURN ( nil ); |
|
655 |
} else if (cls == @global(Array)) { |
|
656 |
if (pos < __arraySize(coll)) { |
|
657 |
RETURN ( __ArrayInstPtr(coll)->a_element[pos]); |
|
658 |
} |
|
659 |
RETURN ( nil ); |
|
660 |
} |
|
661 |
} |
|
3316 | 662 |
} |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
663 |
%}. |
15356 | 664 |
(position >= readLimit) ifTrue:[^ nil]. |
15351 | 665 |
^ collection at:(position + 1) |
3316 | 666 |
! |
667 |
||
1 | 668 |
skipSeparators |
669 |
"skip all whitespace; next will return next non-white-space element. |
|
59 | 670 |
Return the peeked character or nil, if the end-of-stream was reached. |
2 | 671 |
- reimplemented for speed on String-Streams for faster scanning" |
1 | 672 |
|
673 |
%{ /* NOCONTEXT */ |
|
252 | 674 |
OBJ coll, p, l; |
1 | 675 |
|
1133 | 676 |
coll = __INST(collection); |
677 |
p = __INST(position); |
|
678 |
l = __INST(readLimit); |
|
252 | 679 |
|
12479
62b11c1f462e
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
12176
diff
changeset
|
680 |
if (__isStringLike(coll) && __bothSmallInteger(p, l)) { |
16862 | 681 |
REGISTER unsigned char *chars; |
682 |
REGISTER unsigned ch; |
|
16882 | 683 |
REGISTER INT pos; |
684 |
INT limit; |
|
685 |
INT sz; |
|
1 | 686 |
|
16862 | 687 |
pos = __intVal(p); |
688 |
/* make 1-based */ |
|
16882 | 689 |
pos++; |
16862 | 690 |
if (pos <= 0) { |
691 |
RETURN ( nil ); |
|
692 |
} |
|
1 | 693 |
|
16862 | 694 |
limit = __intVal(l); |
695 |
sz = __qSize(coll) - OHDR_SIZE; |
|
696 |
if (limit > sz) { |
|
697 |
limit = sz; |
|
698 |
} |
|
1 | 699 |
|
16862 | 700 |
chars = (unsigned char *)(__stringVal(coll) + pos - 1); |
701 |
while (pos <= limit) { |
|
702 |
ch = *chars++; |
|
703 |
if ((ch > 0x20) |
|
704 |
|| ((ch != ' ') |
|
705 |
&& (ch != '\t') |
|
706 |
&& (ch != '\r') |
|
707 |
&& (ch != '\n') |
|
708 |
&& (ch != '\f') |
|
709 |
&& (ch != 0x0B))) { |
|
710 |
__INST(position) = __mkSmallInteger(pos-1); |
|
711 |
RETURN ( __MKCHARACTER(ch) ); |
|
712 |
} |
|
16882 | 713 |
pos++; |
16862 | 714 |
} |
16882 | 715 |
pos--; |
16862 | 716 |
__INST(position) = __mkSmallInteger(pos); |
717 |
RETURN ( nil ); |
|
1 | 718 |
} |
1406 | 719 |
%}. |
1 | 720 |
^ super skipSeparators |
1406 | 721 |
|
722 |
" |
|
723 |
|s| |
|
724 |
||
725 |
s := ' hello world ' readStream. |
|
726 |
s skipSeparators. |
|
727 |
s next. |
|
728 |
" |
|
1 | 729 |
! |
730 |
||
13 | 731 |
skipSeparatorsExceptCR |
732 |
"skip all whitespace except newlines; |
|
202 | 733 |
next will return next non-white-space element. |
734 |
- reimplemented for speed on String-Streams for faster scanning" |
|
735 |
||
13 | 736 |
%{ /* NOCONTEXT */ |
737 |
||
252 | 738 |
OBJ coll, p, l; |
739 |
||
1133 | 740 |
coll = __INST(collection); |
741 |
p = __INST(position); |
|
742 |
l = __INST(readLimit); |
|
252 | 743 |
|
12479
62b11c1f462e
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
12176
diff
changeset
|
744 |
if (__isStringLike(coll) && __bothSmallInteger(p, l)) { |
16862 | 745 |
REGISTER unsigned char *chars; |
746 |
REGISTER unsigned ch; |
|
16882 | 747 |
REGISTER INT pos; |
748 |
INT limit; |
|
749 |
INT sz; |
|
13 | 750 |
|
16862 | 751 |
pos = __intVal(p); |
752 |
/* make 1-based */ |
|
753 |
pos = pos + 1; |
|
754 |
if (pos <= 0) { |
|
755 |
RETURN ( nil ); |
|
756 |
} |
|
2162
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
757 |
|
16862 | 758 |
limit = __intVal(l); |
16882 | 759 |
sz = __qSize(coll) - OHDR_SIZE; |
760 |
if (limit > sz) |
|
761 |
limit = sz; |
|
13 | 762 |
|
16862 | 763 |
chars = (unsigned char *)(__stringVal(coll) + pos - 1); |
764 |
while (pos <= limit) { |
|
765 |
ch = *chars++; |
|
766 |
if (((int)ch > 0x20) |
|
767 |
|| ( |
|
768 |
(ch != ' ') |
|
769 |
&& (ch != '\t') |
|
770 |
&& (ch != '\f') |
|
771 |
&& (ch != '\b') |
|
772 |
&& (ch != 0x0B))) { |
|
16882 | 773 |
pos--; |
16862 | 774 |
__INST(position) = __mkSmallInteger(pos); |
775 |
RETURN ( __MKCHARACTER(ch) ); |
|
776 |
} |
|
777 |
pos++; |
|
778 |
} |
|
16882 | 779 |
pos--; |
16862 | 780 |
__INST(position) = __mkSmallInteger(pos); |
781 |
RETURN ( nil ); |
|
13 | 782 |
} |
783 |
%} |
|
784 |
. |
|
785 |
^ super skipSeparatorsExceptCR |
|
786 |
! |
|
787 |
||
611 | 788 |
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
|
789 |
"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
|
790 |
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
|
791 |
otherwise (i.e. if not found) return nil and leave the stream positioned at the end. |
611 | 792 |
On success, the next read operation will return the element after anObject. |
793 |
- reimplemented for speed on String-Streams for faster scanning" |
|
794 |
||
795 |
%{ /* NOCONTEXT */ |
|
796 |
OBJ coll, p, l; |
|
797 |
||
1133 | 798 |
coll = __INST(collection); |
799 |
p = __INST(position); |
|
800 |
l = __INST(readLimit); |
|
611 | 801 |
|
12479
62b11c1f462e
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
12176
diff
changeset
|
802 |
if (__isStringLike(coll) |
611 | 803 |
&& __isCharacter(anObject) |
804 |
&& __bothSmallInteger(p, l)) { |
|
16862 | 805 |
REGISTER unsigned char *chars; |
16882 | 806 |
REGISTER INT pos, limit; |
16862 | 807 |
unsigned ch; |
16882 | 808 |
INT sz; |
611 | 809 |
|
16862 | 810 |
ch = __intVal(__characterVal(anObject)); |
811 |
if (ch <= 0xFF) { |
|
812 |
pos = __intVal(p); |
|
813 |
pos = pos + 1; |
|
814 |
if (pos <= 0) { |
|
815 |
RETURN ( nil ); |
|
816 |
} |
|
611 | 817 |
|
16862 | 818 |
limit = __intVal(l); |
819 |
sz = __stringSize(coll); |
|
820 |
if (limit > sz) limit = sz; |
|
611 | 821 |
|
16862 | 822 |
chars = (unsigned char *)(__stringVal(coll) + pos - 1); |
823 |
while (pos < limit) { |
|
824 |
if (*chars == ch) { |
|
825 |
ch = *++chars; |
|
826 |
__INST(position) = __mkSmallInteger(pos); |
|
827 |
RETURN ( self ); |
|
828 |
} |
|
829 |
chars++; |
|
830 |
pos++; |
|
831 |
} |
|
16882 | 832 |
__INST(position) = __mkSmallInteger(pos); |
16862 | 833 |
RETURN ( nil ); |
834 |
} |
|
611 | 835 |
} |
6011
3b91d7834ca5
fixed: skipThrough dod not position atEnd if the element was not found.
Claus Gittinger <cg@exept.de>
parents:
5810
diff
changeset
|
836 |
%}. |
611 | 837 |
^ super skipThrough:anObject |
16861 | 838 |
! |
839 |
||
840 |
upTo:anObject |
|
841 |
%{ |
|
842 |
OBJ _collection = __INST(collection); |
|
843 |
||
844 |
if (__isString(_collection) |
|
845 |
&& __isCharacter(anObject)) { |
|
16862 | 846 |
unsigned int ch = __intVal(__characterVal(anObject)); |
16861 | 847 |
|
16862 | 848 |
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
|
849 |
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
|
850 |
int _endIndex; |
16862 | 851 |
char *startPtr = __stringVal(_collection) + _startPos; |
852 |
char *foundPtr; |
|
853 |
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
|
854 |
int nMax; |
a78c4a2bf366
ouch - bad call of MKSTRING_L passing a pointer into object memory
Claus Gittinger <cg@exept.de>
parents:
16882
diff
changeset
|
855 |
|
16862 | 856 |
_endIndex = __stringSize(_collection); |
857 |
if (__isInteger(__INST(readLimit))) { |
|
858 |
int _readLimit = __intVal(__INST(readLimit)); |
|
16861 | 859 |
|
16862 | 860 |
if (_readLimit < _endIndex) _endIndex = _readLimit; |
861 |
} |
|
16861 | 862 |
|
16882 | 863 |
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
|
864 |
foundPtr = (char *)memchr( startPtr, ch, (long)nMax); |
16862 | 865 |
if (foundPtr == 0) { |
866 |
// 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
|
867 |
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
|
868 |
// 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
|
869 |
_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
|
870 |
memcpy((void *)__stringVal(rslt), (void *)(__stringVal(_collection) + _startPos), (size_t)nMax); |
16862 | 871 |
__INST(position) = __mkSmallInteger(_endIndex); |
872 |
} else { |
|
17597
a78c4a2bf366
ouch - bad call of MKSTRING_L passing a pointer into object memory
Claus Gittinger <cg@exept.de>
parents:
16882
diff
changeset
|
873 |
// 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
|
874 |
int n = foundPtr-startPtr; |
16882 | 875 |
|
17597
a78c4a2bf366
ouch - bad call of MKSTRING_L passing a pointer into object memory
Claus Gittinger <cg@exept.de>
parents:
16882
diff
changeset
|
876 |
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
|
877 |
// 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
|
878 |
_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
|
879 |
memcpy((void *)__stringVal(rslt), (void *)(__stringVal(_collection) + _startPos), (size_t)n); |
16882 | 880 |
__INST(position) = __mkSmallInteger(_startPos + n + 1); |
16862 | 881 |
} |
882 |
RETURN (rslt); |
|
883 |
} |
|
16861 | 884 |
} |
885 |
%}. |
|
886 |
^ super upTo:anObject |
|
1 | 887 |
! ! |
888 |
||
21134 | 889 |
!ReadStream methodsFor:'reading-numbers'! |
890 |
||
891 |
nextDecimalInteger |
|
892 |
"read the next integer in radix 10. |
|
893 |
Does NOT skip initial whitespace. |
|
894 |
The streams elements should be characters. |
|
895 |
||
21755 | 896 |
Be careful - this method returns 0 if not positioned on a digit initially |
21134 | 897 |
or if the end of the stream is encountered. |
898 |
||
899 |
Tuned for speed on String-Streams for faster scanning" |
|
900 |
||
901 |
|value nextOne| |
|
902 |
%{ |
|
903 |
INT pos, limit, sz; |
|
904 |
REGISTER unsigned char *cp; |
|
905 |
REGISTER unsigned ch; |
|
906 |
INT val = 0; |
|
907 |
OBJ coll, p, l; |
|
908 |
||
909 |
coll = __INST(collection); |
|
910 |
p = __INST(position); |
|
911 |
l = __INST(readLimit); |
|
912 |
||
913 |
if (__isStringLike(coll) && __bothSmallInteger(p, l)) { |
|
914 |
||
21755 | 915 |
pos = __intVal(p); |
916 |
/* make 1-based */ |
|
917 |
pos++; |
|
918 |
limit = __intVal(l); |
|
919 |
sz = __qSize(coll) - OHDR_SIZE; |
|
920 |
if (sz < limit) |
|
921 |
limit = sz; |
|
922 |
cp = __stringVal(coll) + pos - 1; |
|
21134 | 923 |
|
21755 | 924 |
for (;;) { |
925 |
if (pos > limit) break; |
|
926 |
ch = *cp; |
|
21134 | 927 |
|
21755 | 928 |
if ((ch < '0') || (ch > '9')) break; |
929 |
val = val * 10 + (ch - '0'); |
|
930 |
pos++; |
|
931 |
if (val > (_MAX_INT / 10)) goto oops; |
|
932 |
cp++; |
|
933 |
} |
|
934 |
pos--; |
|
935 |
__INST(position) = __mkSmallInteger(pos); |
|
936 |
RETURN (__mkSmallInteger(val)); |
|
21134 | 937 |
} |
938 |
oops: |
|
939 |
value = __mkSmallInteger(val); |
|
940 |
%} |
|
941 |
. |
|
942 |
"fall-back for non-string streams - we have to continue where |
|
943 |
above primitive left off, in case of a large integer ... |
|
944 |
(instead of doing a super nextDecimalInteger)" |
|
945 |
||
946 |
nextOne := self peek. |
|
947 |
[nextOne notNil and:[nextOne isDigitRadix:10]] whileTrue:[ |
|
21755 | 948 |
value := (value * 10) + nextOne digitValue. |
949 |
nextOne := self nextPeek |
|
21134 | 950 |
]. |
951 |
^ value |
|
21755 | 952 |
|
953 |
"Modified: / 17-05-2017 / 15:12:46 / mawalch" |
|
21134 | 954 |
! ! |
955 |
||
59 | 956 |
!ReadStream methodsFor:'writing'! |
1 | 957 |
|
958 |
nextPut:anElement |
|
10 | 959 |
"catch write access to readstreams - report an error" |
960 |
||
961 |
self shouldNotImplement |
|
1 | 962 |
! ! |
611 | 963 |
|
2162
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
964 |
!ReadStream class methodsFor:'documentation'! |
701 | 965 |
|
966 |
version |
|
19232 | 967 |
^ '$Header$' |
12176 | 968 |
! |
969 |
||
970 |
version_CVS |
|
19232 | 971 |
^ '$Header$' |
701 | 972 |
! ! |
17604 | 973 |