--- a/ReadStream.st Sat Oct 04 13:27:11 2014 +0200
+++ b/ReadStream.st Sat Oct 04 22:33:07 2014 +0200
@@ -161,7 +161,7 @@
%{ /* NOCONTEXT */
- REGISTER int pos;
+ REGISTER INT pos;
unsigned ch;
OBJ coll, p, l;
@@ -172,37 +172,35 @@
if (__isNonNilObject(coll) && __bothSmallInteger(p, l)) {
pos = __intVal(p);
- /* make 1-based */
- pos = pos + 1;
- if (pos > 0 && pos <= __intVal(l)) {
+ if (pos >= 0 && pos < __intVal(l)) {
OBJ cls, ret;
cls = __qClass(coll);
if (cls == @global(String)) {
- if (pos <= __stringSize(coll)) {
- ch = __stringVal(coll)[pos-1];
+ if (pos < __stringSize(coll)) {
+ ch = __stringVal(coll)[pos];
ret = __MKCHARACTER(ch);
- __INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1);
+ __INST(position) = __mkSmallInteger(pos+1);
RETURN ( ret );
}
} else if (cls == @global(ByteArray)) {
- if (pos <= __byteArraySize(coll)) {
- ch = __ByteArrayInstPtr(coll)->ba_element[pos-1];
+ if (pos < __byteArraySize(coll)) {
+ ch = __ByteArrayInstPtr(coll)->ba_element[pos];
ret = __mkSmallInteger(ch);
- __INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1);
+ __INST(position) = __mkSmallInteger(pos+1);
RETURN ( ret );
}
} else if (cls == @global(Unicode16String)) {
- if (pos <= __unicode16StringSize(coll)) {
- ch = __Unicode16StringInstPtr(coll)->s_element[pos-1];
+ if (pos < __unicode16StringSize(coll)) {
+ ch = __Unicode16StringInstPtr(coll)->s_element[pos];
ret = __MKUCHARACTER(ch);
- __INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1);
+ __INST(position) = __mkSmallInteger(pos+1);
RETURN ( ret );
}
} else if (cls == @global(Array)) {
- if (pos <= __arraySize(coll)) {
- ret = __ArrayInstPtr(coll)->a_element[pos-1];
- __INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1);
+ if (pos < __arraySize(coll)) {
+ ret = __ArrayInstPtr(coll)->a_element[pos];
+ __INST(position) = __mkSmallInteger(pos+1);
RETURN ( ret );
}
}
@@ -210,8 +208,8 @@
}
%}.
(position >= readLimit) ifTrue:[^ self pastEndRead].
- ret := collection at:(position + 1).
position := position + 1.
+ ret := collection at:position.
^ ret
!
@@ -246,8 +244,8 @@
%{
/* speedup, if collection is a string */
- int pos, limit, sz;
- int len;
+ INT pos, limit, sz;
+ INT len;
char buffer[256];
REGISTER unsigned char *cp;
REGISTER unsigned ch;
@@ -317,7 +315,7 @@
%{ /* NOCONTEXT */
- REGISTER int pos;
+ REGISTER INT pos;
unsigned ch;
OBJ coll, p, l;
@@ -328,31 +326,29 @@
if (__isNonNilObject(coll) && __bothSmallInteger(p, l)) {
pos = __intVal(p);
- /* make 1-based */
- pos = pos + 1;
- if (pos > 0 && pos <= __intVal(l)) {
+ if (pos >= 0 && pos < __intVal(l)) {
OBJ cls, ret;
cls = __qClass(coll);
if (cls == @global(String)) {
- if (pos <= __stringSize(coll)) {
- ch = __stringVal(coll)[pos-1];
+ if (pos < __stringSize(coll)) {
+ ch = __stringVal(coll)[pos];
ret = __mkSmallInteger(ch);
- __INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1);
+ __INST(position) = __mkSmallInteger(pos+1);
RETURN ( ret );
}
} else if (cls == @global(ByteArray)) {
- if (pos <= __byteArraySize(coll)) {
- ch = __ByteArrayInstPtr(coll)->ba_element[pos-1];
+ if (pos < __byteArraySize(coll)) {
+ ch = __ByteArrayInstPtr(coll)->ba_element[pos];
ret = __mkSmallInteger(ch);
- __INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1);
+ __INST(position) = __mkSmallInteger(pos+1);
RETURN ( ret );
}
} else if (cls == @global(Array)) {
- if (pos <= __arraySize(coll)) {
- ret = __ArrayInstPtr(coll)->a_element[pos-1];
+ if (pos < __arraySize(coll)) {
+ ret = __ArrayInstPtr(coll)->a_element[pos];
if (!__isSmallInteger(ret) || __intVal(ret) > 255) goto out;
- __INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1);
+ __INST(position) = __mkSmallInteger(pos+1);
RETURN ( ret );
}
}
@@ -410,12 +406,18 @@
!
nextDecimalInteger
- "read the next integer in radix 10. dont skip whitespace.
- - tuned for speed on String-Streams for faster scanning"
+ "read the next integer in radix 10.
+ Does NOT skip initial whitespace.
+ The streams elements should be characters.
+
+ Be careful - this method returns 0 if not positioned on a digit intitially
+ or if the end of the stream is encountered.
+
+ Tuned for speed on String-Streams for faster scanning"
|value nextOne|
%{
- int pos, limit, sz;
+ INT pos, limit, sz;
REGISTER unsigned char *cp;
REGISTER unsigned ch;
INT val = 0;
@@ -429,7 +431,7 @@
pos = __intVal(p);
/* make 1-based */
- pos = pos + 1;
+ pos++;
limit = __intVal(l);
sz = __qSize(coll) - OHDR_SIZE;
if (sz < limit)
@@ -446,7 +448,7 @@
if (val > (_MAX_INT / 10)) goto oops;
cp++;
}
- pos = pos - 1;
+ pos--;
__INST(position) = __mkSmallInteger(pos);
RETURN (__mkSmallInteger(val));
}
@@ -475,7 +477,7 @@
%{ /* NOCONTEXT */
- REGISTER int pos;
+ REGISTER INT pos;
unsigned ch;
OBJ coll, p, l;
@@ -485,49 +487,47 @@
if (__isNonNilObject(coll) && __bothSmallInteger(p, l)) {
pos = __intVal(p);
- /* make 1-based */
- pos = pos + 1;
- if (pos > 0) {
+ if (pos >= 0) {
OBJ cls, ret;
- if (pos > __intVal(l)) {
+ if (pos >= __intVal(l)) {
RETURN(nil);
}
cls = __qClass(coll);
if (cls == @global(String)) {
- if (pos <= __stringSize(coll)) {
- ch = __stringVal(coll)[pos-1];
+ if (pos < __stringSize(coll)) {
+ ch = __stringVal(coll)[pos];
ret = __MKCHARACTER(ch);
- __INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1);
+ __INST(position) = __mkSmallInteger(pos + 1);
RETURN ( ret );
}
} else if (cls == @global(ByteArray)) {
- if (pos <= __byteArraySize(coll)) {
- ch = __ByteArrayInstPtr(coll)->ba_element[pos-1];
+ if (pos < __byteArraySize(coll)) {
+ ch = __ByteArrayInstPtr(coll)->ba_element[pos];
ret = __mkSmallInteger(ch);
- __INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1);
+ __INST(position) = __mkSmallInteger(pos + 1);
RETURN ( ret );
}
} else if (cls == @global(Unicode16String)) {
- if (pos <= __unicode16StringSize(coll)) {
- ch = __Unicode16StringInstPtr(coll)->s_element[pos-1];
+ if (pos < __unicode16StringSize(coll)) {
+ ch = __Unicode16StringInstPtr(coll)->s_element[pos];
ret = __MKUCHARACTER(ch);
- __INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1);
+ __INST(position) = __mkSmallInteger(pos + 1);
RETURN ( ret );
}
} else if (cls == @global(Array)) {
- if (pos <= __arraySize(coll)) {
- ret = __ArrayInstPtr(coll)->a_element[pos-1];
- __INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1);
+ if (pos < __arraySize(coll)) {
+ ret = __ArrayInstPtr(coll)->a_element[pos];
+ __INST(position) = __mkSmallInteger(pos + 1);
RETURN ( ret );
}
}
}
}
%}.
- ret := collection at:(position + 1).
position := position + 1.
+ ret := collection at:position.
^ ret
!
@@ -544,16 +544,13 @@
l = __INST(readLimit);
if (__isStringLike(coll) && __bothSmallInteger(p, l)) {
- REGISTER int pos;
+ REGISTER INT pos;
unsigned ch;
pos = __intVal(p);
- /* make 1-based */
- pos = pos + 1;
+ pos++;
if ((pos > 0) && (pos < __intVal(l)) && (pos < __stringSize(coll))) {
- pos = pos + 1;
- ch = __stringVal(coll)[pos-1];
- pos = pos - 1;
+ ch = __stringVal(coll)[pos];
__INST(position) = __mkSmallInteger(pos);
RETURN ( __MKCHARACTER(ch) );
}
@@ -572,7 +569,7 @@
%{ /* NOCONTEXT */
- REGISTER int pos;
+ REGISTER INT pos;
unsigned ch;
OBJ coll;
OBJ cls, p, l;
@@ -584,23 +581,21 @@
if (__isNonNilObject(coll) && __bothSmallInteger(p, l)) {
pos = __intVal(p);
- /* make 1-based */
- pos = pos + 1;
- if (pos <= __intVal(l) && pos > 0) {
+ if ((pos < __intVal(l)) && (pos >= 0)) {
cls = __qClass(coll);
if (cls == @global(String)) {
- if (pos <= __stringSize(coll)) {
- ch = __stringVal(coll)[pos-1];
+ if (pos < __stringSize(coll)) {
+ ch = __stringVal(coll)[pos];
RETURN ( __MKCHARACTER(ch) );
}
} else if (cls == @global(ByteArray)) {
- if (pos <= __byteArraySize(coll)) {
- ch = __ByteArrayInstPtr(coll)->ba_element[pos-1];
+ if (pos < __byteArraySize(coll)) {
+ ch = __ByteArrayInstPtr(coll)->ba_element[pos];
RETURN ( __mkSmallInteger(ch) );
}
} else if (cls == @global(Array)) {
- if (pos <= __arraySize(coll)) {
- RETURN ( __ArrayInstPtr(coll)->a_element[pos-1]);
+ if (pos < __arraySize(coll)) {
+ RETURN ( __ArrayInstPtr(coll)->a_element[pos]);
}
}
}
@@ -619,7 +614,7 @@
%{ /* NOCONTEXT */
- REGISTER int pos;
+ REGISTER INT pos;
unsigned ch;
OBJ coll;
OBJ cls, p, l;
@@ -631,25 +626,23 @@
if (__isNonNilObject(coll) && __bothSmallInteger(p, l)) {
pos = __intVal(p);
- /* make 1-based */
- pos = pos + 1;
- if (pos <= __intVal(l) && pos > 0) {
+ if ((pos < __intVal(l)) && (pos >= 0)) {
cls = __qClass(coll);
if (cls == @global(String)) {
- if (pos <= __stringSize(coll)) {
- ch = __stringVal(coll)[pos-1];
+ if (pos < __stringSize(coll)) {
+ ch = __stringVal(coll)[pos];
RETURN ( __MKCHARACTER(ch) );
}
RETURN ( nil );
} else if (cls == @global(ByteArray)) {
- if (pos <= __byteArraySize(coll)) {
- ch = __ByteArrayInstPtr(coll)->ba_element[pos-1];
+ if (pos < __byteArraySize(coll)) {
+ ch = __ByteArrayInstPtr(coll)->ba_element[pos];
RETURN ( __mkSmallInteger(ch) );
}
RETURN ( nil );
} else if (cls == @global(Array)) {
- if (pos <= __arraySize(coll)) {
- RETURN ( __ArrayInstPtr(coll)->a_element[pos-1]);
+ if (pos < __arraySize(coll)) {
+ RETURN ( __ArrayInstPtr(coll)->a_element[pos]);
}
RETURN ( nil );
}
@@ -675,13 +668,13 @@
if (__isStringLike(coll) && __bothSmallInteger(p, l)) {
REGISTER unsigned char *chars;
REGISTER unsigned ch;
- REGISTER int pos;
- int limit;
- int sz;
+ REGISTER INT pos;
+ INT limit;
+ INT sz;
pos = __intVal(p);
/* make 1-based */
- pos = pos + 1;
+ pos++;
if (pos <= 0) {
RETURN ( nil );
}
@@ -694,7 +687,6 @@
chars = (unsigned char *)(__stringVal(coll) + pos - 1);
while (pos <= limit) {
- pos++;
ch = *chars++;
if ((ch > 0x20)
|| ((ch != ' ')
@@ -703,12 +695,12 @@
&& (ch != '\n')
&& (ch != '\f')
&& (ch != 0x0B))) {
- pos = pos - 1;
__INST(position) = __mkSmallInteger(pos-1);
RETURN ( __MKCHARACTER(ch) );
}
+ pos++;
}
- pos = pos - 1;
+ pos--;
__INST(position) = __mkSmallInteger(pos);
RETURN ( nil );
}
@@ -740,8 +732,9 @@
if (__isStringLike(coll) && __bothSmallInteger(p, l)) {
REGISTER unsigned char *chars;
REGISTER unsigned ch;
- REGISTER int pos;
- int limit;
+ REGISTER INT pos;
+ INT limit;
+ INT sz;
pos = __intVal(p);
/* make 1-based */
@@ -751,8 +744,9 @@
}
limit = __intVal(l);
- if (limit > (__qSize(coll) - OHDR_SIZE))
- limit = __qSize(coll) - OHDR_SIZE;
+ sz = __qSize(coll) - OHDR_SIZE;
+ if (limit > sz)
+ limit = sz;
chars = (unsigned char *)(__stringVal(coll) + pos - 1);
while (pos <= limit) {
@@ -764,13 +758,13 @@
&& (ch != '\f')
&& (ch != '\b')
&& (ch != 0x0B))) {
- pos = pos - 1;
+ pos--;
__INST(position) = __mkSmallInteger(pos);
RETURN ( __MKCHARACTER(ch) );
}
pos++;
}
- pos = pos - 1;
+ pos--;
__INST(position) = __mkSmallInteger(pos);
RETURN ( nil );
}
@@ -797,9 +791,9 @@
&& __isCharacter(anObject)
&& __bothSmallInteger(p, l)) {
REGISTER unsigned char *chars;
- REGISTER int pos, limit;
+ REGISTER INT pos, limit;
unsigned ch;
- int sz;
+ INT sz;
ch = __intVal(__characterVal(anObject));
if (ch <= 0xFF) {
@@ -817,16 +811,13 @@
while (pos < limit) {
if (*chars == ch) {
ch = *++chars;
- pos++;
- pos = pos - 1;
__INST(position) = __mkSmallInteger(pos);
RETURN ( self );
}
chars++;
pos++;
}
- pos = pos - 1;
- __INST(position) = __mkSmallInteger(pos+1);
+ __INST(position) = __mkSmallInteger(pos);
RETURN ( nil );
}
}
@@ -843,12 +834,15 @@
unsigned int ch = __intVal(__characterVal(anObject));
if (ch <= 0xFF) {
- int _startPos = __intVal(__INST(position));
- int _endIndex;
+ INT _startPos = __intVal(__INST(position));
+ INT _endIndex;
char *startPtr = __stringVal(_collection) + _startPos;
char *foundPtr;
OBJ rslt;
-
+ INT nMax;
+#ifdef __osx__
+ extern char*memchr(char *, int, long);
+#endif
_endIndex = __stringSize(_collection);
if (__isInteger(__INST(readLimit))) {
int _readLimit = __intVal(__INST(readLimit));
@@ -856,15 +850,18 @@
if (_readLimit < _endIndex) _endIndex = _readLimit;
}
- foundPtr = memchr( startPtr, ch, _endIndex-_startPos);
+ nMax = _endIndex-_startPos;
+ foundPtr = memchr( startPtr, ch, (long)nMax);
if (foundPtr == 0) {
// not found
- rslt = __MKSTRING_L(startPtr, _endIndex-_startPos);
+ rslt = __MKSTRING_L(startPtr, nMax);
__INST(position) = __mkSmallInteger(_endIndex);
} else {
+ INT n = foundPtr-startPtr;
+
// found at foundPtr
- rslt = __MKSTRING_L(startPtr, foundPtr-startPtr);
- __INST(position) = __mkSmallInteger(_startPos + foundPtr-startPtr + 1);
+ rslt = __MKSTRING_L(startPtr, n);
+ __INST(position) = __mkSmallInteger(_startPos + n + 1);
}
RETURN (rslt);
}
@@ -884,9 +881,9 @@
!ReadStream class methodsFor:'documentation'!
version
- ^ '$Header: /cvs/stx/stx/libbasic/ReadStream.st,v 1.77 2014-09-26 13:52:50 ca Exp $'
+ ^ '$Header: /cvs/stx/stx/libbasic/ReadStream.st,v 1.78 2014-10-04 20:33:07 cg Exp $'
!
version_CVS
- ^ '$Header: /cvs/stx/stx/libbasic/ReadStream.st,v 1.77 2014-09-26 13:52:50 ca Exp $'
+ ^ '$Header: /cvs/stx/stx/libbasic/ReadStream.st,v 1.78 2014-10-04 20:33:07 cg Exp $'
! !