author | Claus Gittinger <cg@exept.de> |
Wed, 27 Mar 2019 14:55:09 +0100 | |
changeset 23985 | 5dffbe3433bc |
parent 23949 | 320a57c5dad1 |
child 23986 | 27dd00ebc6bd |
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 |
|
23985 | 706 |
if (__bothSmallInteger(p, l)) { |
707 |
REGISTER INT pos; |
|
708 |
INT limit; |
|
709 |
||
710 |
pos = __intVal(p); |
|
711 |
if (pos < 0) { |
|
712 |
goto out; |
|
713 |
} |
|
714 |
||
715 |
limit = __intVal(l); |
|
716 |
||
717 |
if (__isStringLike(coll)) { |
|
718 |
REGISTER unsigned char *chars; |
|
719 |
REGISTER unsigned ch; |
|
720 |
INT sz; |
|
721 |
||
722 |
sz = __stringSize(coll); |
|
723 |
if (limit > sz) { |
|
724 |
limit = sz; |
|
725 |
} |
|
1 | 726 |
|
23985 | 727 |
// because pos and limit are 1-based |
728 |
chars = (unsigned char *)(__stringVal(coll)); |
|
729 |
while (pos < limit) { |
|
730 |
ch = chars[pos]; |
|
731 |
if ((ch > 0x20) |
|
732 |
|| ((ch != ' ') |
|
733 |
&& (ch != '\t') |
|
734 |
&& (ch != '\r') |
|
735 |
&& (ch != '\n') |
|
736 |
&& (ch != '\f') |
|
737 |
&& (ch != 0x0B))) { |
|
738 |
__INST(position) = __mkSmallInteger(pos); |
|
739 |
RETURN ( __MKCHARACTER(ch) ); |
|
740 |
} |
|
741 |
pos++; |
|
742 |
} |
|
743 |
__INST(position) = __mkSmallInteger(pos); |
|
744 |
RETURN ( nil ); |
|
745 |
} |
|
746 |
if (__isTwoByteString(coll) || __isUnicode16String(coll)) { |
|
747 |
REGISTER unsigned short *chars; |
|
748 |
REGISTER unsigned ch; |
|
749 |
INT sz; |
|
1 | 750 |
|
23985 | 751 |
sz = __unicode16StringSize(coll); |
752 |
if (limit > sz) { |
|
753 |
limit = sz; |
|
754 |
} |
|
1 | 755 |
|
23985 | 756 |
chars = (unsigned short *)(__unicode16StringVal(coll)); |
757 |
while (pos < limit) { |
|
758 |
ch = chars[pos]; |
|
759 |
if ((ch > 0x20) |
|
760 |
|| ((ch != ' ') |
|
761 |
&& (ch != '\t') |
|
762 |
&& (ch != '\r') |
|
763 |
&& (ch != '\n') |
|
764 |
&& (ch != '\f') |
|
765 |
&& (ch != 0x0B))) { |
|
766 |
__INST(position) = __mkSmallInteger(pos); |
|
767 |
RETURN ( __MKCHARACTER(ch) ); |
|
768 |
} |
|
769 |
pos++; |
|
770 |
} |
|
771 |
__INST(position) = __mkSmallInteger(pos); |
|
772 |
RETURN ( nil ); |
|
773 |
} |
|
774 |
if (/* __isFourByteString(coll) || */ __isUnicode32String(coll)) { |
|
775 |
REGISTER unsigned int *chars; |
|
776 |
REGISTER unsigned ch; |
|
777 |
INT sz; |
|
778 |
||
779 |
sz = __unicode32StringSize(coll); |
|
780 |
if (limit > sz) { |
|
781 |
limit = sz; |
|
782 |
} |
|
783 |
||
784 |
chars = (unsigned int *)(__unicode32StringVal(coll)); |
|
785 |
while (pos < limit) { |
|
786 |
ch = chars[pos]; |
|
787 |
if ((ch > 0x20) |
|
788 |
|| ((ch != ' ') |
|
789 |
&& (ch != '\t') |
|
790 |
&& (ch != '\r') |
|
791 |
&& (ch != '\n') |
|
792 |
&& (ch != '\f') |
|
793 |
&& (ch != 0x0B))) { |
|
794 |
__INST(position) = __mkSmallInteger(pos); |
|
795 |
RETURN ( __MKCHARACTER(ch) ); |
|
796 |
} |
|
797 |
pos++; |
|
798 |
} |
|
799 |
__INST(position) = __mkSmallInteger(pos); |
|
800 |
RETURN ( nil ); |
|
801 |
} |
|
1 | 802 |
} |
23985 | 803 |
|
804 |
out: ; |
|
1406 | 805 |
%}. |
1 | 806 |
^ super skipSeparators |
1406 | 807 |
|
808 |
" |
|
809 |
|s| |
|
23985 | 810 |
s := ' hello world ' asUnicode32String readStream. |
811 |
s skipSeparators. |
|
812 |
s next. -> $h |
|
1406 | 813 |
|
23985 | 814 |
|s| |
815 |
s := 'a h' asUnicode32String readStream. |
|
816 |
s next. |
|
1406 | 817 |
s skipSeparators. |
23985 | 818 |
s next. -> $h |
819 |
||
820 |
|s| |
|
821 |
s := 'a ' asUnicode32String readStream. |
|
1406 | 822 |
s next. |
23985 | 823 |
s skipSeparators. |
824 |
s next. -> nil |
|
1406 | 825 |
" |
23985 | 826 |
|
827 |
"Modified: / 27-03-2019 / 14:34:04 / Claus Gittinger" |
|
1 | 828 |
! |
829 |
||
13 | 830 |
skipSeparatorsExceptCR |
831 |
"skip all whitespace except newlines; |
|
202 | 832 |
next will return next non-white-space element. |
833 |
- reimplemented for speed on String-Streams for faster scanning" |
|
834 |
||
13 | 835 |
%{ /* NOCONTEXT */ |
836 |
||
252 | 837 |
OBJ coll, p, l; |
838 |
||
1133 | 839 |
coll = __INST(collection); |
840 |
p = __INST(position); |
|
841 |
l = __INST(readLimit); |
|
252 | 842 |
|
12479
62b11c1f462e
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
12176
diff
changeset
|
843 |
if (__isStringLike(coll) && __bothSmallInteger(p, l)) { |
16862 | 844 |
REGISTER unsigned char *chars; |
845 |
REGISTER unsigned ch; |
|
16882 | 846 |
REGISTER INT pos; |
847 |
INT limit; |
|
848 |
INT sz; |
|
13 | 849 |
|
16862 | 850 |
pos = __intVal(p); |
851 |
/* make 1-based */ |
|
852 |
pos = pos + 1; |
|
853 |
if (pos <= 0) { |
|
854 |
RETURN ( nil ); |
|
855 |
} |
|
2162
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
856 |
|
16862 | 857 |
limit = __intVal(l); |
16882 | 858 |
sz = __qSize(coll) - OHDR_SIZE; |
859 |
if (limit > sz) |
|
860 |
limit = sz; |
|
13 | 861 |
|
16862 | 862 |
chars = (unsigned char *)(__stringVal(coll) + pos - 1); |
863 |
while (pos <= limit) { |
|
864 |
ch = *chars++; |
|
865 |
if (((int)ch > 0x20) |
|
866 |
|| ( |
|
867 |
(ch != ' ') |
|
868 |
&& (ch != '\t') |
|
869 |
&& (ch != '\f') |
|
870 |
&& (ch != '\b') |
|
871 |
&& (ch != 0x0B))) { |
|
16882 | 872 |
pos--; |
16862 | 873 |
__INST(position) = __mkSmallInteger(pos); |
874 |
RETURN ( __MKCHARACTER(ch) ); |
|
875 |
} |
|
876 |
pos++; |
|
877 |
} |
|
16882 | 878 |
pos--; |
16862 | 879 |
__INST(position) = __mkSmallInteger(pos); |
880 |
RETURN ( nil ); |
|
13 | 881 |
} |
882 |
%} |
|
883 |
. |
|
884 |
^ super skipSeparatorsExceptCR |
|
885 |
! |
|
886 |
||
611 | 887 |
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
|
888 |
"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
|
889 |
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
|
890 |
otherwise (i.e. if not found) return nil and leave the stream positioned at the end. |
611 | 891 |
On success, the next read operation will return the element after anObject. |
892 |
- reimplemented for speed on String-Streams for faster scanning" |
|
893 |
||
894 |
%{ /* NOCONTEXT */ |
|
895 |
OBJ coll, p, l; |
|
896 |
||
1133 | 897 |
coll = __INST(collection); |
898 |
p = __INST(position); |
|
899 |
l = __INST(readLimit); |
|
611 | 900 |
|
12479
62b11c1f462e
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
12176
diff
changeset
|
901 |
if (__isStringLike(coll) |
611 | 902 |
&& __isCharacter(anObject) |
903 |
&& __bothSmallInteger(p, l)) { |
|
16862 | 904 |
REGISTER unsigned char *chars; |
16882 | 905 |
REGISTER INT pos, limit; |
16862 | 906 |
unsigned ch; |
16882 | 907 |
INT sz; |
611 | 908 |
|
16862 | 909 |
ch = __intVal(__characterVal(anObject)); |
910 |
if (ch <= 0xFF) { |
|
911 |
pos = __intVal(p); |
|
912 |
pos = pos + 1; |
|
913 |
if (pos <= 0) { |
|
914 |
RETURN ( nil ); |
|
915 |
} |
|
611 | 916 |
|
16862 | 917 |
limit = __intVal(l); |
918 |
sz = __stringSize(coll); |
|
919 |
if (limit > sz) limit = sz; |
|
611 | 920 |
|
16862 | 921 |
chars = (unsigned char *)(__stringVal(coll) + pos - 1); |
922 |
while (pos < limit) { |
|
923 |
if (*chars == ch) { |
|
924 |
ch = *++chars; |
|
925 |
__INST(position) = __mkSmallInteger(pos); |
|
926 |
RETURN ( self ); |
|
927 |
} |
|
928 |
chars++; |
|
929 |
pos++; |
|
930 |
} |
|
16882 | 931 |
__INST(position) = __mkSmallInteger(pos); |
16862 | 932 |
RETURN ( nil ); |
933 |
} |
|
611 | 934 |
} |
6011
3b91d7834ca5
fixed: skipThrough dod not position atEnd if the element was not found.
Claus Gittinger <cg@exept.de>
parents:
5810
diff
changeset
|
935 |
%}. |
611 | 936 |
^ super skipThrough:anObject |
16861 | 937 |
! |
938 |
||
939 |
upTo:anObject |
|
940 |
%{ |
|
941 |
OBJ _collection = __INST(collection); |
|
942 |
||
23949 | 943 |
if (__isStringLike(_collection) |
16861 | 944 |
&& __isCharacter(anObject)) { |
23949 | 945 |
unsigned int ch = __intVal(__characterVal(anObject)); |
16861 | 946 |
|
23949 | 947 |
if (ch <= 0xFF) { |
948 |
int _startPos = __intVal(__INST(position)); |
|
949 |
int _endIndex; |
|
950 |
char *startPtr = __stringVal(_collection) + _startPos; |
|
951 |
char *foundPtr; |
|
952 |
OBJ rslt; |
|
953 |
int nMax; |
|
17597
a78c4a2bf366
ouch - bad call of MKSTRING_L passing a pointer into object memory
Claus Gittinger <cg@exept.de>
parents:
16882
diff
changeset
|
954 |
|
23949 | 955 |
_endIndex = __stringSize(_collection); |
956 |
if (__isInteger(__INST(readLimit))) { |
|
957 |
int _readLimit = __intVal(__INST(readLimit)); |
|
16861 | 958 |
|
23949 | 959 |
if (_readLimit < _endIndex) _endIndex = _readLimit; |
960 |
} |
|
16861 | 961 |
|
23949 | 962 |
nMax = _endIndex-_startPos; |
963 |
foundPtr = (char *)memchr( startPtr, ch, (long)nMax); |
|
964 |
if (foundPtr == 0) { |
|
965 |
// not found |
|
966 |
rslt = __MKEMPTYSTRING(nMax); |
|
967 |
// refetch - may GC |
|
968 |
_collection = __INST(collection); |
|
969 |
memcpy((void *)__stringVal(rslt), (void *)(__stringVal(_collection) + _startPos), (size_t)nMax); |
|
970 |
__INST(position) = __mkSmallInteger(_endIndex); |
|
971 |
} else { |
|
972 |
// found at foundPtr |
|
973 |
int n = foundPtr-startPtr; |
|
16882 | 974 |
|
23949 | 975 |
rslt = __MKEMPTYSTRING(n); |
976 |
// refetch - may GC |
|
977 |
_collection = __INST(collection); |
|
978 |
memcpy((void *)__stringVal(rslt), (void *)(__stringVal(_collection) + _startPos), (size_t)n); |
|
979 |
__INST(position) = __mkSmallInteger(_startPos + n + 1); |
|
980 |
} |
|
981 |
RETURN (rslt); |
|
982 |
} |
|
16861 | 983 |
} |
984 |
%}. |
|
985 |
^ super upTo:anObject |
|
23949 | 986 |
|
987 |
"Modified: / 22-03-2019 / 03:01:19 / Claus Gittinger" |
|
1 | 988 |
! ! |
989 |
||
21134 | 990 |
!ReadStream methodsFor:'reading-numbers'! |
991 |
||
992 |
nextDecimalInteger |
|
993 |
"read the next integer in radix 10. |
|
994 |
Does NOT skip initial whitespace. |
|
22789 | 995 |
Does NOT care for an initial sign. |
996 |
The stream's elements should be characters. |
|
21134 | 997 |
|
21755 | 998 |
Be careful - this method returns 0 if not positioned on a digit initially |
21134 | 999 |
or if the end of the stream is encountered. |
1000 |
||
1001 |
Tuned for speed on String-Streams for faster scanning" |
|
1002 |
||
1003 |
|value nextOne| |
|
1004 |
%{ |
|
1005 |
INT pos, limit, sz; |
|
1006 |
REGISTER unsigned char *cp; |
|
1007 |
REGISTER unsigned ch; |
|
1008 |
INT val = 0; |
|
1009 |
OBJ coll, p, l; |
|
1010 |
||
1011 |
coll = __INST(collection); |
|
1012 |
p = __INST(position); |
|
1013 |
l = __INST(readLimit); |
|
1014 |
||
1015 |
if (__isStringLike(coll) && __bothSmallInteger(p, l)) { |
|
1016 |
||
21755 | 1017 |
pos = __intVal(p); |
1018 |
/* make 1-based */ |
|
1019 |
pos++; |
|
1020 |
limit = __intVal(l); |
|
1021 |
sz = __qSize(coll) - OHDR_SIZE; |
|
1022 |
if (sz < limit) |
|
1023 |
limit = sz; |
|
1024 |
cp = __stringVal(coll) + pos - 1; |
|
21134 | 1025 |
|
21755 | 1026 |
for (;;) { |
1027 |
if (pos > limit) break; |
|
1028 |
ch = *cp; |
|
21134 | 1029 |
|
21755 | 1030 |
if ((ch < '0') || (ch > '9')) break; |
1031 |
val = val * 10 + (ch - '0'); |
|
1032 |
pos++; |
|
1033 |
if (val > (_MAX_INT / 10)) goto oops; |
|
1034 |
cp++; |
|
1035 |
} |
|
1036 |
pos--; |
|
1037 |
__INST(position) = __mkSmallInteger(pos); |
|
1038 |
RETURN (__mkSmallInteger(val)); |
|
21134 | 1039 |
} |
1040 |
oops: |
|
1041 |
value = __mkSmallInteger(val); |
|
1042 |
%} |
|
1043 |
. |
|
1044 |
"fall-back for non-string streams - we have to continue where |
|
1045 |
above primitive left off, in case of a large integer ... |
|
1046 |
(instead of doing a super nextDecimalInteger)" |
|
1047 |
||
1048 |
nextOne := self peek. |
|
1049 |
[nextOne notNil and:[nextOne isDigitRadix:10]] whileTrue:[ |
|
21755 | 1050 |
value := (value * 10) + nextOne digitValue. |
1051 |
nextOne := self nextPeek |
|
21134 | 1052 |
]. |
1053 |
^ value |
|
21755 | 1054 |
|
1055 |
"Modified: / 17-05-2017 / 15:12:46 / mawalch" |
|
21134 | 1056 |
! ! |
1057 |
||
59 | 1058 |
!ReadStream methodsFor:'writing'! |
1 | 1059 |
|
1060 |
nextPut:anElement |
|
10 | 1061 |
"catch write access to readstreams - report an error" |
1062 |
||
1063 |
self shouldNotImplement |
|
1 | 1064 |
! ! |
611 | 1065 |
|
2162
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
1066 |
!ReadStream class methodsFor:'documentation'! |
701 | 1067 |
|
1068 |
version |
|
19232 | 1069 |
^ '$Header$' |
12176 | 1070 |
! |
1071 |
||
1072 |
version_CVS |
|
19232 | 1073 |
^ '$Header$' |
701 | 1074 |
! ! |
17604 | 1075 |