--- a/ReadStream.st Thu Nov 23 02:27:21 1995 +0100
+++ b/ReadStream.st Thu Nov 23 02:32:01 1995 +0100
@@ -11,10 +11,10 @@
"
PositionableStream subclass:#ReadStream
- instanceVariableNames:''
- classVariableNames:''
- poolDictionaries:''
- category:'Streams'
+ instanceVariableNames:''
+ classVariableNames:''
+ poolDictionaries:''
+ category:'Streams'
!
!ReadStream class methodsFor:'documentation'!
@@ -33,14 +33,14 @@
"
!
-version
- ^ '$Header: /cvs/stx/stx/libbasic/ReadStream.st,v 1.19 1995-11-14 21:43:27 cg Exp $'
-!
-
documentation
"
ReadStream defines protocol for reading streamwise over collections.
"
+!
+
+version
+ ^ '$Header: /cvs/stx/stx/libbasic/ReadStream.st,v 1.20 1995-11-23 01:31:34 cg Exp $'
! !
!ReadStream methodsFor:'converting'!
@@ -60,55 +60,6 @@
!ReadStream methodsFor:'reading'!
-peek
- "return the next element; do NOT advance read pointer.
- return nil, if there is no next element.
- - tuned for a bit more speed on String/ByteArray/Array-Streams"
-
-%{ /* NOCONTEXT */
-
- REGISTER int pos;
- unsigned ch;
- OBJ coll;
- OBJ cls, p, l;
- extern OBJ ByteArray;
-
- coll = _INST(collection);
- p = _INST(position);
- l = _INST(readLimit);
-
- if (__isNonNilObject(coll) && __bothSmallInteger(p, l)) {
-
- pos = _intVal(p);
- if (pos > _intVal(l)) {
- RETURN ( nil );
- }
-
- if (pos > 0) {
- cls = __qClass(coll);
- if (cls == String) {
- if (pos <= _stringSize(coll)) {
- ch = _stringVal(coll)[pos-1];
- RETURN ( _MKCHARACTER(ch) );
- }
- } else if (cls == ByteArray) {
- if (pos <= _byteArraySize(coll)) {
- ch = _ByteArrayInstPtr(coll)->ba_element[pos-1];
- RETURN ( _MKSMALLINT(ch) );
- }
- } else if (cls == Array) {
- if (pos <= _arraySize(coll)) {
- RETURN ( _ArrayInstPtr(coll)->a_element[pos-1]);
- }
- }
- }
- }
-%}
-.
- (position > readLimit) ifTrue:[^ nil].
- ^ collection at:position
-!
-
next
"return the next element; advance read pointer.
return nil, if there is no next element.
@@ -164,101 +115,6 @@
^ ret
!
-nextPeek
- "advance read pointer return the peek element.
- this is equivalent to (self next; peek).
- - tuned for speed on String-Streams for faster scanning"
-
-%{ /* NOCONTEXT */
- OBJ coll, l, p;
-
- coll = _INST(collection);
- p = _INST(position);
- l = _INST(readLimit);
-
- if (__isString(coll) && __bothSmallInteger(p, l)) {
- REGISTER int pos;
- unsigned ch;
-
- pos = _intVal(p);
- if (pos > _intVal(l)) {
- RETURN ( nil );
- }
- if ((pos > 0) && (pos <= _stringSize(coll))) {
- _INST(position) = _MKSMALLINT(pos + 1);
- pos = pos + 1;
- if (pos > _intVal(l)) {
- RETURN ( nil );
- }
- if (pos <= _stringSize(coll)) {
- ch = _stringVal(coll)[pos-1];
- RETURN ( _MKCHARACTER(ch) );
- }
- RETURN ( nil );
- }
- }
-%}
-.
- (position > readLimit) ifTrue:[^ nil].
- position := position + 1.
- (position > readLimit) ifTrue:[^ nil].
- ^ collection at:position
-!
-
-nextDecimalInteger
- "read the next integer in radix 10. dont skip whitespace.
- - tuned for speed on String-Streams for faster scanning"
-
- |value nextOne|
-%{
- int pos, limit, sz;
- REGISTER unsigned char *cp;
- REGISTER unsigned ch;
- int val = 0;
- OBJ coll, p, l;
-
- coll = _INST(collection);
- p = _INST(position);
- l = _INST(readLimit);
-
- if (__isString(coll) && __bothSmallInteger(p, l)) {
-
- pos = _intVal(p);
- limit = _intVal(l);
- sz = __qSize(coll) - OHDR_SIZE;
- if (sz < limit)
- limit = sz;
- cp = _stringVal(coll) + pos - 1;
-
- for (;;) {
- if (pos > limit) break;
- ch = *cp;
-
- if ((ch < '0') || (ch > '9')) break;
- val = val * 10 + (ch - '0');
- pos++;
- if (val > (_MAX_INT / 10)) goto oops;
- cp++;
- }
- _INST(position) = _MKSMALLINT(pos);
- return _MKSMALLINT(val);
- }
-oops:
- value = _MKSMALLINT(val);
-%}
-.
- "fall-back for non-string streams - we have to continue where
- above primitive left off, in case of a large integer ...
- (instead of doing a super nextDecimalInteger)"
-
- nextOne := self peek.
- [nextOne notNil and:[nextOne isDigitRadix:10]] whileTrue:[
- value = (value * 10) + nextOne digitValue.
- nextOne := self nextPeek
- ].
- ^ value
-!
-
nextAlphaNumericWord
"read the next word (i.e. up to non letter-or-digit).
return a string containing those characters.
@@ -325,6 +181,101 @@
^ super nextAlphaNumericWord
!
+nextDecimalInteger
+ "read the next integer in radix 10. dont skip whitespace.
+ - tuned for speed on String-Streams for faster scanning"
+
+ |value nextOne|
+%{
+ int pos, limit, sz;
+ REGISTER unsigned char *cp;
+ REGISTER unsigned ch;
+ int val = 0;
+ OBJ coll, p, l;
+
+ coll = _INST(collection);
+ p = _INST(position);
+ l = _INST(readLimit);
+
+ if (__isString(coll) && __bothSmallInteger(p, l)) {
+
+ pos = _intVal(p);
+ limit = _intVal(l);
+ sz = __qSize(coll) - OHDR_SIZE;
+ if (sz < limit)
+ limit = sz;
+ cp = _stringVal(coll) + pos - 1;
+
+ for (;;) {
+ if (pos > limit) break;
+ ch = *cp;
+
+ if ((ch < '0') || (ch > '9')) break;
+ val = val * 10 + (ch - '0');
+ pos++;
+ if (val > (_MAX_INT / 10)) goto oops;
+ cp++;
+ }
+ _INST(position) = _MKSMALLINT(pos);
+ return _MKSMALLINT(val);
+ }
+oops:
+ value = _MKSMALLINT(val);
+%}
+.
+ "fall-back for non-string streams - we have to continue where
+ above primitive left off, in case of a large integer ...
+ (instead of doing a super nextDecimalInteger)"
+
+ nextOne := self peek.
+ [nextOne notNil and:[nextOne isDigitRadix:10]] whileTrue:[
+ value = (value * 10) + nextOne digitValue.
+ nextOne := self nextPeek
+ ].
+ ^ value
+!
+
+nextPeek
+ "advance read pointer return the peek element.
+ this is equivalent to (self next; peek).
+ - tuned for speed on String-Streams for faster scanning"
+
+%{ /* NOCONTEXT */
+ OBJ coll, l, p;
+
+ coll = _INST(collection);
+ p = _INST(position);
+ l = _INST(readLimit);
+
+ if (__isString(coll) && __bothSmallInteger(p, l)) {
+ REGISTER int pos;
+ unsigned ch;
+
+ pos = _intVal(p);
+ if (pos > _intVal(l)) {
+ RETURN ( nil );
+ }
+ if ((pos > 0) && (pos <= _stringSize(coll))) {
+ _INST(position) = _MKSMALLINT(pos + 1);
+ pos = pos + 1;
+ if (pos > _intVal(l)) {
+ RETURN ( nil );
+ }
+ if (pos <= _stringSize(coll)) {
+ ch = _stringVal(coll)[pos-1];
+ RETURN ( _MKCHARACTER(ch) );
+ }
+ RETURN ( nil );
+ }
+ }
+%}
+.
+ (position > readLimit) ifTrue:[^ nil].
+ position := position + 1.
+ (position > readLimit) ifTrue:[^ nil].
+ ^ collection at:position
+!
+
nextSymbol
"read the next selector-symbol (i.e. up to non letter-or-digit).
return a string containing those characters.
@@ -379,53 +330,53 @@
^ super nextSymbol
!
-skipThrough:anObject
- "skip all objects up-to and including anObject, return anObject on success,
- nil if end-of-stream is reached before.
- On success, the next read operation will return the element after anObject.
- - reimplemented for speed on String-Streams for faster scanning"
+peek
+ "return the next element; do NOT advance read pointer.
+ return nil, if there is no next element.
+ - tuned for a bit more speed on String/ByteArray/Array-Streams"
%{ /* NOCONTEXT */
- OBJ coll, p, l;
+
+ REGISTER int pos;
+ unsigned ch;
+ OBJ coll;
+ OBJ cls, p, l;
+ extern OBJ ByteArray;
coll = _INST(collection);
p = _INST(position);
l = _INST(readLimit);
- if (__isString(coll)
- && __isCharacter(anObject)
- && __bothSmallInteger(p, l)) {
- REGISTER unsigned char *chars;
- REGISTER int pos, limit;
- unsigned ch;
- int sz;
+ if (__isNonNilObject(coll) && __bothSmallInteger(p, l)) {
pos = _intVal(p);
- if (pos <= 0) {
+ if (pos > _intVal(l)) {
RETURN ( nil );
}
- limit = _intVal(l);
- sz = _stringSize(coll);
- if (limit > sz) limit = sz;
-
- chars = (unsigned char *)(_stringVal(coll) + pos - 1);
- ch = _intVal(_characterVal(anObject)) & 0xFF;
- while (pos < limit) {
- if (*chars == ch) {
- ch = *++chars;
- pos++;
- _INST(position) = _MKSMALLINT(pos);
- RETURN ( anObject );
+ if (pos > 0) {
+ cls = __qClass(coll);
+ if (cls == String) {
+ if (pos <= _stringSize(coll)) {
+ ch = _stringVal(coll)[pos-1];
+ RETURN ( _MKCHARACTER(ch) );
+ }
+ } else if (cls == ByteArray) {
+ if (pos <= _byteArraySize(coll)) {
+ ch = _ByteArrayInstPtr(coll)->ba_element[pos-1];
+ RETURN ( _MKSMALLINT(ch) );
+ }
+ } else if (cls == Array) {
+ if (pos <= _arraySize(coll)) {
+ RETURN ( _ArrayInstPtr(coll)->a_element[pos-1]);
+ }
}
- chars++;
- pos++;
}
- RETURN ( nil );
}
%}
.
- ^ super skipThrough:anObject
+ (position > readLimit) ifTrue:[^ nil].
+ ^ collection at:position
!
skipSeparators
@@ -518,6 +469,55 @@
^ super skipSeparatorsExceptCR
!
+skipThrough:anObject
+ "skip all objects up-to and including anObject, return anObject on success,
+ nil if end-of-stream is reached before.
+ On success, the next read operation will return the element after anObject.
+ - reimplemented for speed on String-Streams for faster scanning"
+
+%{ /* NOCONTEXT */
+ OBJ coll, p, l;
+
+ coll = _INST(collection);
+ p = _INST(position);
+ l = _INST(readLimit);
+
+ if (__isString(coll)
+ && __isCharacter(anObject)
+ && __bothSmallInteger(p, l)) {
+ REGISTER unsigned char *chars;
+ REGISTER int pos, limit;
+ unsigned ch;
+ int sz;
+
+ pos = _intVal(p);
+ if (pos <= 0) {
+ RETURN ( nil );
+ }
+
+ limit = _intVal(l);
+ sz = _stringSize(coll);
+ if (limit > sz) limit = sz;
+
+ chars = (unsigned char *)(_stringVal(coll) + pos - 1);
+ ch = _intVal(_characterVal(anObject)) & 0xFF;
+ while (pos < limit) {
+ if (*chars == ch) {
+ ch = *++chars;
+ pos++;
+ _INST(position) = _MKSMALLINT(pos);
+ RETURN ( anObject );
+ }
+ chars++;
+ pos++;
+ }
+ RETURN ( nil );
+ }
+%}
+.
+ ^ super skipThrough:anObject
+!
+
skipToAll:aCollection
"skip for the sequence given by the argument, aCollection;
return nil if not found, self otherwise. On a successful match, next read
@@ -557,3 +557,4 @@
self shouldNotImplement
! !
+