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