author | Claus Gittinger <cg@exept.de> |
Wed, 27 Mar 2019 15:38:08 +0100 | |
changeset 23986 | 27dd00ebc6bd |
parent 23985 | 5dffbe3433bc |
child 23987 | bba7ed9be3c1 |
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; |
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 |
} |
|
23986 | 269 |
} else if (cls == @global(Unicode32String)) { |
270 |
if (pos < __unicode32StringSize(coll)) { |
|
271 |
ch = __unicode32StringVal(coll)[pos]; |
|
272 |
ret = __MKUCHARACTER(ch); |
|
273 |
__INST(position) = __mkSmallInteger(pos+1); |
|
274 |
RETURN ( ret ); |
|
275 |
} |
|
18266 | 276 |
} else if (cls == @global(Array)) { |
277 |
if (pos < __arraySize(coll)) { |
|
278 |
ret = __ArrayInstPtr(coll)->a_element[pos]; |
|
279 |
__INST(position) = __mkSmallInteger(pos+1); |
|
280 |
RETURN ( ret ); |
|
281 |
} |
|
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 |
|
291 |
"Modified: / 27-03-2019 / 15:34:48 / 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; |
|
1 | 616 |
|
1133 | 617 |
coll = __INST(collection); |
618 |
p = __INST(position); |
|
619 |
l = __INST(readLimit); |
|
252 | 620 |
|
611 | 621 |
if (__isNonNilObject(coll) && __bothSmallInteger(p, l)) { |
1 | 622 |
|
18266 | 623 |
pos = __intVal(p); |
624 |
if ((pos < __intVal(l)) && (pos >= 0)) { |
|
625 |
cls = __qClass(coll); |
|
626 |
if (cls == @global(String) || cls == @global(ImmutableString) || cls == @global(Symbol)) { |
|
627 |
if (pos < __stringSize(coll)) { |
|
628 |
ch = __stringVal(coll)[pos]; |
|
629 |
RETURN ( __MKCHARACTER(ch) ); |
|
630 |
} |
|
631 |
} else if (cls == @global(ByteArray)) { |
|
632 |
if (pos < __byteArraySize(coll)) { |
|
633 |
ch = __ByteArrayInstPtr(coll)->ba_element[pos]; |
|
634 |
RETURN ( __mkSmallInteger(ch) ); |
|
635 |
} |
|
636 |
} else if (cls == @global(Unicode16String)) { |
|
637 |
if (pos < __unicode16StringSize(coll)) { |
|
638 |
ch = __unicode16StringVal(coll)[pos]; |
|
639 |
RETURN(__MKUCHARACTER(ch)); |
|
640 |
} |
|
641 |
} else if (cls == @global(Array)) { |
|
642 |
if (pos < __arraySize(coll)) { |
|
643 |
RETURN ( __ArrayInstPtr(coll)->a_element[pos]); |
|
644 |
} |
|
645 |
} |
|
646 |
} |
|
1 | 647 |
} |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
648 |
%}. |
15356 | 649 |
(position >= readLimit) ifTrue:[^ self pastEndRead]. |
15351 | 650 |
^ collection at:(position + 1) |
1 | 651 |
! |
652 |
||
3316 | 653 |
peekOrNil |
654 |
"return the next element; do NOT advance read pointer. |
|
655 |
return nil, if there is no next element. |
|
656 |
This is much like #peek - |
|
657 |
However, unlike #peek, this does not raise an atEnd-query signal - even |
|
658 |
if handled. Instead, nil is returned immediately." |
|
659 |
||
660 |
%{ /* NOCONTEXT */ |
|
661 |
||
16882 | 662 |
REGISTER INT pos; |
3316 | 663 |
unsigned ch; |
664 |
OBJ coll; |
|
665 |
OBJ cls, p, l; |
|
666 |
||
667 |
coll = __INST(collection); |
|
668 |
p = __INST(position); |
|
669 |
l = __INST(readLimit); |
|
670 |
||
671 |
if (__isNonNilObject(coll) && __bothSmallInteger(p, l)) { |
|
18266 | 672 |
pos = __intVal(p); |
673 |
if ((pos < __intVal(l)) && (pos >= 0)) { |
|
674 |
cls = __qClass(coll); |
|
675 |
if (cls == @global(String) || cls == @global(ImmutableString) || cls == @global(Symbol)) { |
|
676 |
if (pos < __stringSize(coll)) { |
|
677 |
ch = __stringVal(coll)[pos]; |
|
678 |
RETURN ( __MKCHARACTER(ch) ); |
|
679 |
} |
|
680 |
RETURN ( nil ); |
|
681 |
} else if (cls == @global(ByteArray)) { |
|
682 |
if (pos < __byteArraySize(coll)) { |
|
683 |
ch = __ByteArrayInstPtr(coll)->ba_element[pos]; |
|
684 |
RETURN ( __mkSmallInteger(ch) ); |
|
685 |
} |
|
686 |
RETURN ( nil ); |
|
687 |
} else if (cls == @global(Unicode16String)) { |
|
688 |
if (pos < __unicode16StringSize(coll)) { |
|
689 |
ch = __unicode16StringVal(coll)[pos]; |
|
690 |
RETURN(__MKUCHARACTER(ch)); |
|
691 |
} |
|
692 |
RETURN ( nil ); |
|
693 |
} else if (cls == @global(Array)) { |
|
694 |
if (pos < __arraySize(coll)) { |
|
695 |
RETURN ( __ArrayInstPtr(coll)->a_element[pos]); |
|
696 |
} |
|
697 |
RETURN ( nil ); |
|
698 |
} |
|
699 |
} |
|
3316 | 700 |
} |
7051
984d8271d06b
preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents:
6814
diff
changeset
|
701 |
%}. |
15356 | 702 |
(position >= readLimit) ifTrue:[^ nil]. |
15351 | 703 |
^ collection at:(position + 1) |
3316 | 704 |
! |
705 |
||
1 | 706 |
skipSeparators |
707 |
"skip all whitespace; next will return next non-white-space element. |
|
59 | 708 |
Return the peeked character or nil, if the end-of-stream was reached. |
2 | 709 |
- reimplemented for speed on String-Streams for faster scanning" |
1 | 710 |
|
711 |
%{ /* NOCONTEXT */ |
|
252 | 712 |
OBJ coll, p, l; |
1 | 713 |
|
1133 | 714 |
coll = __INST(collection); |
715 |
p = __INST(position); |
|
716 |
l = __INST(readLimit); |
|
252 | 717 |
|
23985 | 718 |
if (__bothSmallInteger(p, l)) { |
719 |
REGISTER INT pos; |
|
720 |
INT limit; |
|
721 |
||
722 |
pos = __intVal(p); |
|
723 |
if (pos < 0) { |
|
724 |
goto out; |
|
725 |
} |
|
726 |
||
727 |
limit = __intVal(l); |
|
728 |
||
729 |
if (__isStringLike(coll)) { |
|
730 |
REGISTER unsigned char *chars; |
|
731 |
REGISTER unsigned ch; |
|
732 |
INT sz; |
|
733 |
||
734 |
sz = __stringSize(coll); |
|
735 |
if (limit > sz) { |
|
736 |
limit = sz; |
|
737 |
} |
|
1 | 738 |
|
23985 | 739 |
// because pos and limit are 1-based |
740 |
chars = (unsigned char *)(__stringVal(coll)); |
|
741 |
while (pos < limit) { |
|
742 |
ch = chars[pos]; |
|
743 |
if ((ch > 0x20) |
|
744 |
|| ((ch != ' ') |
|
745 |
&& (ch != '\t') |
|
746 |
&& (ch != '\r') |
|
747 |
&& (ch != '\n') |
|
748 |
&& (ch != '\f') |
|
749 |
&& (ch != 0x0B))) { |
|
750 |
__INST(position) = __mkSmallInteger(pos); |
|
751 |
RETURN ( __MKCHARACTER(ch) ); |
|
752 |
} |
|
753 |
pos++; |
|
754 |
} |
|
755 |
__INST(position) = __mkSmallInteger(pos); |
|
756 |
RETURN ( nil ); |
|
757 |
} |
|
758 |
if (__isTwoByteString(coll) || __isUnicode16String(coll)) { |
|
759 |
REGISTER unsigned short *chars; |
|
760 |
REGISTER unsigned ch; |
|
761 |
INT sz; |
|
1 | 762 |
|
23985 | 763 |
sz = __unicode16StringSize(coll); |
764 |
if (limit > sz) { |
|
765 |
limit = sz; |
|
766 |
} |
|
1 | 767 |
|
23985 | 768 |
chars = (unsigned short *)(__unicode16StringVal(coll)); |
769 |
while (pos < limit) { |
|
770 |
ch = chars[pos]; |
|
771 |
if ((ch > 0x20) |
|
772 |
|| ((ch != ' ') |
|
773 |
&& (ch != '\t') |
|
774 |
&& (ch != '\r') |
|
775 |
&& (ch != '\n') |
|
776 |
&& (ch != '\f') |
|
777 |
&& (ch != 0x0B))) { |
|
778 |
__INST(position) = __mkSmallInteger(pos); |
|
779 |
RETURN ( __MKCHARACTER(ch) ); |
|
780 |
} |
|
781 |
pos++; |
|
782 |
} |
|
783 |
__INST(position) = __mkSmallInteger(pos); |
|
784 |
RETURN ( nil ); |
|
785 |
} |
|
786 |
if (/* __isFourByteString(coll) || */ __isUnicode32String(coll)) { |
|
787 |
REGISTER unsigned int *chars; |
|
788 |
REGISTER unsigned ch; |
|
789 |
INT sz; |
|
790 |
||
791 |
sz = __unicode32StringSize(coll); |
|
792 |
if (limit > sz) { |
|
793 |
limit = sz; |
|
794 |
} |
|
795 |
||
796 |
chars = (unsigned int *)(__unicode32StringVal(coll)); |
|
797 |
while (pos < limit) { |
|
798 |
ch = chars[pos]; |
|
799 |
if ((ch > 0x20) |
|
800 |
|| ((ch != ' ') |
|
801 |
&& (ch != '\t') |
|
802 |
&& (ch != '\r') |
|
803 |
&& (ch != '\n') |
|
804 |
&& (ch != '\f') |
|
805 |
&& (ch != 0x0B))) { |
|
806 |
__INST(position) = __mkSmallInteger(pos); |
|
807 |
RETURN ( __MKCHARACTER(ch) ); |
|
808 |
} |
|
809 |
pos++; |
|
810 |
} |
|
811 |
__INST(position) = __mkSmallInteger(pos); |
|
812 |
RETURN ( nil ); |
|
813 |
} |
|
1 | 814 |
} |
23985 | 815 |
|
816 |
out: ; |
|
1406 | 817 |
%}. |
1 | 818 |
^ super skipSeparators |
1406 | 819 |
|
820 |
" |
|
821 |
|s| |
|
23985 | 822 |
s := ' hello world ' asUnicode32String readStream. |
823 |
s skipSeparators. |
|
824 |
s next. -> $h |
|
1406 | 825 |
|
23985 | 826 |
|s| |
827 |
s := 'a h' asUnicode32String readStream. |
|
828 |
s next. |
|
1406 | 829 |
s skipSeparators. |
23985 | 830 |
s next. -> $h |
831 |
||
832 |
|s| |
|
833 |
s := 'a ' asUnicode32String readStream. |
|
1406 | 834 |
s next. |
23985 | 835 |
s skipSeparators. |
836 |
s next. -> nil |
|
1406 | 837 |
" |
23985 | 838 |
|
839 |
"Modified: / 27-03-2019 / 14:34:04 / Claus Gittinger" |
|
1 | 840 |
! |
841 |
||
13 | 842 |
skipSeparatorsExceptCR |
843 |
"skip all whitespace except newlines; |
|
202 | 844 |
next will return next non-white-space element. |
845 |
- reimplemented for speed on String-Streams for faster scanning" |
|
846 |
||
13 | 847 |
%{ /* NOCONTEXT */ |
848 |
||
252 | 849 |
OBJ coll, p, l; |
850 |
||
1133 | 851 |
coll = __INST(collection); |
852 |
p = __INST(position); |
|
853 |
l = __INST(readLimit); |
|
252 | 854 |
|
12479
62b11c1f462e
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
12176
diff
changeset
|
855 |
if (__isStringLike(coll) && __bothSmallInteger(p, l)) { |
16862 | 856 |
REGISTER unsigned char *chars; |
857 |
REGISTER unsigned ch; |
|
16882 | 858 |
REGISTER INT pos; |
859 |
INT limit; |
|
860 |
INT sz; |
|
13 | 861 |
|
16862 | 862 |
pos = __intVal(p); |
863 |
/* make 1-based */ |
|
864 |
pos = pos + 1; |
|
865 |
if (pos <= 0) { |
|
866 |
RETURN ( nil ); |
|
867 |
} |
|
2162
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
868 |
|
16862 | 869 |
limit = __intVal(l); |
16882 | 870 |
sz = __qSize(coll) - OHDR_SIZE; |
871 |
if (limit > sz) |
|
872 |
limit = sz; |
|
13 | 873 |
|
16862 | 874 |
chars = (unsigned char *)(__stringVal(coll) + pos - 1); |
875 |
while (pos <= limit) { |
|
876 |
ch = *chars++; |
|
877 |
if (((int)ch > 0x20) |
|
878 |
|| ( |
|
879 |
(ch != ' ') |
|
880 |
&& (ch != '\t') |
|
881 |
&& (ch != '\f') |
|
882 |
&& (ch != '\b') |
|
883 |
&& (ch != 0x0B))) { |
|
16882 | 884 |
pos--; |
16862 | 885 |
__INST(position) = __mkSmallInteger(pos); |
886 |
RETURN ( __MKCHARACTER(ch) ); |
|
887 |
} |
|
888 |
pos++; |
|
889 |
} |
|
16882 | 890 |
pos--; |
16862 | 891 |
__INST(position) = __mkSmallInteger(pos); |
892 |
RETURN ( nil ); |
|
13 | 893 |
} |
894 |
%} |
|
895 |
. |
|
896 |
^ super skipSeparatorsExceptCR |
|
897 |
! |
|
898 |
||
611 | 899 |
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
|
900 |
"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
|
901 |
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
|
902 |
otherwise (i.e. if not found) return nil and leave the stream positioned at the end. |
611 | 903 |
On success, the next read operation will return the element after anObject. |
904 |
- reimplemented for speed on String-Streams for faster scanning" |
|
905 |
||
906 |
%{ /* NOCONTEXT */ |
|
907 |
OBJ coll, p, l; |
|
908 |
||
1133 | 909 |
coll = __INST(collection); |
910 |
p = __INST(position); |
|
911 |
l = __INST(readLimit); |
|
611 | 912 |
|
12479
62b11c1f462e
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
12176
diff
changeset
|
913 |
if (__isStringLike(coll) |
611 | 914 |
&& __isCharacter(anObject) |
915 |
&& __bothSmallInteger(p, l)) { |
|
16862 | 916 |
REGISTER unsigned char *chars; |
16882 | 917 |
REGISTER INT pos, limit; |
16862 | 918 |
unsigned ch; |
16882 | 919 |
INT sz; |
611 | 920 |
|
16862 | 921 |
ch = __intVal(__characterVal(anObject)); |
922 |
if (ch <= 0xFF) { |
|
923 |
pos = __intVal(p); |
|
924 |
pos = pos + 1; |
|
925 |
if (pos <= 0) { |
|
926 |
RETURN ( nil ); |
|
927 |
} |
|
611 | 928 |
|
16862 | 929 |
limit = __intVal(l); |
930 |
sz = __stringSize(coll); |
|
931 |
if (limit > sz) limit = sz; |
|
611 | 932 |
|
16862 | 933 |
chars = (unsigned char *)(__stringVal(coll) + pos - 1); |
934 |
while (pos < limit) { |
|
935 |
if (*chars == ch) { |
|
936 |
ch = *++chars; |
|
937 |
__INST(position) = __mkSmallInteger(pos); |
|
938 |
RETURN ( self ); |
|
939 |
} |
|
940 |
chars++; |
|
941 |
pos++; |
|
942 |
} |
|
16882 | 943 |
__INST(position) = __mkSmallInteger(pos); |
16862 | 944 |
RETURN ( nil ); |
945 |
} |
|
611 | 946 |
} |
6011
3b91d7834ca5
fixed: skipThrough dod not position atEnd if the element was not found.
Claus Gittinger <cg@exept.de>
parents:
5810
diff
changeset
|
947 |
%}. |
611 | 948 |
^ super skipThrough:anObject |
16861 | 949 |
! |
950 |
||
951 |
upTo:anObject |
|
952 |
%{ |
|
953 |
OBJ _collection = __INST(collection); |
|
954 |
||
23949 | 955 |
if (__isStringLike(_collection) |
16861 | 956 |
&& __isCharacter(anObject)) { |
23949 | 957 |
unsigned int ch = __intVal(__characterVal(anObject)); |
16861 | 958 |
|
23949 | 959 |
if (ch <= 0xFF) { |
960 |
int _startPos = __intVal(__INST(position)); |
|
961 |
int _endIndex; |
|
962 |
char *startPtr = __stringVal(_collection) + _startPos; |
|
963 |
char *foundPtr; |
|
964 |
OBJ rslt; |
|
965 |
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
|
966 |
|
23949 | 967 |
_endIndex = __stringSize(_collection); |
968 |
if (__isInteger(__INST(readLimit))) { |
|
969 |
int _readLimit = __intVal(__INST(readLimit)); |
|
16861 | 970 |
|
23949 | 971 |
if (_readLimit < _endIndex) _endIndex = _readLimit; |
972 |
} |
|
16861 | 973 |
|
23949 | 974 |
nMax = _endIndex-_startPos; |
975 |
foundPtr = (char *)memchr( startPtr, ch, (long)nMax); |
|
976 |
if (foundPtr == 0) { |
|
977 |
// not found |
|
978 |
rslt = __MKEMPTYSTRING(nMax); |
|
979 |
// refetch - may GC |
|
980 |
_collection = __INST(collection); |
|
981 |
memcpy((void *)__stringVal(rslt), (void *)(__stringVal(_collection) + _startPos), (size_t)nMax); |
|
982 |
__INST(position) = __mkSmallInteger(_endIndex); |
|
983 |
} else { |
|
984 |
// found at foundPtr |
|
985 |
int n = foundPtr-startPtr; |
|
16882 | 986 |
|
23949 | 987 |
rslt = __MKEMPTYSTRING(n); |
988 |
// refetch - may GC |
|
989 |
_collection = __INST(collection); |
|
990 |
memcpy((void *)__stringVal(rslt), (void *)(__stringVal(_collection) + _startPos), (size_t)n); |
|
991 |
__INST(position) = __mkSmallInteger(_startPos + n + 1); |
|
992 |
} |
|
993 |
RETURN (rslt); |
|
994 |
} |
|
16861 | 995 |
} |
996 |
%}. |
|
997 |
^ super upTo:anObject |
|
23949 | 998 |
|
999 |
"Modified: / 22-03-2019 / 03:01:19 / Claus Gittinger" |
|
1 | 1000 |
! ! |
1001 |
||
21134 | 1002 |
!ReadStream methodsFor:'reading-numbers'! |
1003 |
||
1004 |
nextDecimalInteger |
|
1005 |
"read the next integer in radix 10. |
|
1006 |
Does NOT skip initial whitespace. |
|
22789 | 1007 |
Does NOT care for an initial sign. |
1008 |
The stream's elements should be characters. |
|
21134 | 1009 |
|
21755 | 1010 |
Be careful - this method returns 0 if not positioned on a digit initially |
21134 | 1011 |
or if the end of the stream is encountered. |
1012 |
||
1013 |
Tuned for speed on String-Streams for faster scanning" |
|
1014 |
||
1015 |
|value nextOne| |
|
1016 |
%{ |
|
1017 |
INT pos, limit, sz; |
|
1018 |
REGISTER unsigned char *cp; |
|
1019 |
REGISTER unsigned ch; |
|
1020 |
INT val = 0; |
|
1021 |
OBJ coll, p, l; |
|
1022 |
||
1023 |
coll = __INST(collection); |
|
1024 |
p = __INST(position); |
|
1025 |
l = __INST(readLimit); |
|
1026 |
||
1027 |
if (__isStringLike(coll) && __bothSmallInteger(p, l)) { |
|
1028 |
||
21755 | 1029 |
pos = __intVal(p); |
1030 |
/* make 1-based */ |
|
1031 |
pos++; |
|
1032 |
limit = __intVal(l); |
|
1033 |
sz = __qSize(coll) - OHDR_SIZE; |
|
1034 |
if (sz < limit) |
|
1035 |
limit = sz; |
|
1036 |
cp = __stringVal(coll) + pos - 1; |
|
21134 | 1037 |
|
21755 | 1038 |
for (;;) { |
1039 |
if (pos > limit) break; |
|
1040 |
ch = *cp; |
|
21134 | 1041 |
|
21755 | 1042 |
if ((ch < '0') || (ch > '9')) break; |
1043 |
val = val * 10 + (ch - '0'); |
|
1044 |
pos++; |
|
1045 |
if (val > (_MAX_INT / 10)) goto oops; |
|
1046 |
cp++; |
|
1047 |
} |
|
1048 |
pos--; |
|
1049 |
__INST(position) = __mkSmallInteger(pos); |
|
1050 |
RETURN (__mkSmallInteger(val)); |
|
21134 | 1051 |
} |
1052 |
oops: |
|
1053 |
value = __mkSmallInteger(val); |
|
1054 |
%} |
|
1055 |
. |
|
1056 |
"fall-back for non-string streams - we have to continue where |
|
1057 |
above primitive left off, in case of a large integer ... |
|
1058 |
(instead of doing a super nextDecimalInteger)" |
|
1059 |
||
1060 |
nextOne := self peek. |
|
1061 |
[nextOne notNil and:[nextOne isDigitRadix:10]] whileTrue:[ |
|
21755 | 1062 |
value := (value * 10) + nextOne digitValue. |
1063 |
nextOne := self nextPeek |
|
21134 | 1064 |
]. |
1065 |
^ value |
|
21755 | 1066 |
|
1067 |
"Modified: / 17-05-2017 / 15:12:46 / mawalch" |
|
21134 | 1068 |
! ! |
1069 |
||
59 | 1070 |
!ReadStream methodsFor:'writing'! |
1 | 1071 |
|
1072 |
nextPut:anElement |
|
10 | 1073 |
"catch write access to readstreams - report an error" |
1074 |
||
1075 |
self shouldNotImplement |
|
1 | 1076 |
! ! |
611 | 1077 |
|
2162
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
1078 |
!ReadStream class methodsFor:'documentation'! |
701 | 1079 |
|
1080 |
version |
|
19232 | 1081 |
^ '$Header$' |
12176 | 1082 |
! |
1083 |
||
1084 |
version_CVS |
|
19232 | 1085 |
^ '$Header$' |
701 | 1086 |
! ! |
17604 | 1087 |