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