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