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