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