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