ReadStream.st
changeset 7051 984d8271d06b
parent 6814 eb7703491da0
child 7114 acc13967229e
--- a/ReadStream.st	Mon Feb 24 18:01:32 2003 +0100
+++ b/ReadStream.st	Tue Feb 25 10:05:53 2003 +0100
@@ -40,7 +40,7 @@
     ReadStream defines protocol for reading streamwise over collections. 
 
     [author:]
-        Claus Gittinger
+	Claus Gittinger
 
 "
 ! !
@@ -62,19 +62,19 @@
      Streams on collections which nothing at all about emphasises, 
      will report an error."
 
-    ^ collection emphasisAt:position.
+    ^ collection emphasisAt:(position + 1 - ZeroPosition).
 
     "
      |t s|
 
      t := 'hello world' asText
-                emphasizeFrom:1 to:5 with:#bold;
-                emphasizeFrom:7 to:11 with:#italic.
+		emphasizeFrom:1 to:5 with:#bold;
+		emphasizeFrom:7 to:11 with:#italic.
 
      s := t readStream.
      [s atEnd] whileFalse:[
-        Transcript show:(s emphasis); show:' '.
-        Transcript show:''''; show:(s next); showCR:''''.
+	Transcript show:(s emphasis); show:' '.
+	Transcript show:''''; show:(s next); showCR:''''.
      ].
     "
 
@@ -143,35 +143,36 @@
 
     if (__isNonNilObject(coll) && __bothSmallInteger(p, l)) {
 
-        pos = __intVal(p);
-        if (pos > 0 && pos <= __intVal(l)) {
-            OBJ cls;
+	pos = __intVal(p);
+	/* make 1-based */
+	pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition));
+	if (pos > 0 && pos <= __intVal(l)) {
+	    OBJ cls;
 
-            cls = __qClass(coll);
-            if (cls == @global(String)) {
-                if (pos <= __stringSize(coll)) {
-                    __INST(position) = __MKSMALLINT(pos + 1);
-                    ch = __stringVal(coll)[pos-1];
-                    RETURN ( __MKCHARACTER(ch) );
-                }
-            } else if (cls == @global(ByteArray)) {
-                if (pos <= __byteArraySize(coll)) {
-                    __INST(position) = __MKSMALLINT(pos + 1);
-                    ch = __ByteArrayInstPtr(coll)->ba_element[pos-1];
-                    RETURN ( __MKSMALLINT(ch) );
-                }
-            } else if (cls == @global(Array)) {
-                if (pos <= __arraySize(coll)) {
-                    __INST(position) = __MKSMALLINT(pos + 1);
-                    RETURN ( __ArrayInstPtr(coll)->a_element[pos-1]);
-                }
-            }
-        }
+	    cls = __qClass(coll);
+	    if (cls == @global(String)) {
+		if (pos <= __stringSize(coll)) {
+		    __INST(position) = __MKSMALLINT(__intVal(__INST(position)) + 1);
+		    ch = __stringVal(coll)[pos-1];
+		    RETURN ( __MKCHARACTER(ch) );
+		}
+	    } else if (cls == @global(ByteArray)) {
+		if (pos <= __byteArraySize(coll)) {
+		    __INST(position) = __MKSMALLINT(__intVal(__INST(position)) + 1);
+		    ch = __ByteArrayInstPtr(coll)->ba_element[pos-1];
+		    RETURN ( __MKSMALLINT(ch) );
+		}
+	    } else if (cls == @global(Array)) {
+		if (pos <= __arraySize(coll)) {
+		    __INST(position) = __MKSMALLINT(__intVal(__INST(position)) + 1);
+		    RETURN ( __ArrayInstPtr(coll)->a_element[pos-1]);
+		}
+	    }
+	}
     }
-%}
-.
-    (position > readLimit) ifTrue:[^ self pastEnd].
-    ret := collection at:position.
+%}.
+    ((position + 1 - ZeroPosition) > readLimit) ifTrue:[^ self pastEnd].
+    ret := collection at:(position + 1 - ZeroPosition).
     position := position + 1.
     ^ ret
 !
@@ -198,6 +199,9 @@
     if (__isString(coll) && __bothSmallInteger(p, l)) {
 
 	pos = __intVal(p);
+	/* make 1-based */
+	pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition));
+
 	limit = __intVal(l);
 	sz = __qSize(coll) - OHDR_SIZE;
 	if (sz < limit)
@@ -234,6 +238,8 @@
 	    cp++;
 	}
 
+	/* make ZeroPosition-based */
+	pos = pos - 1 + __intVal( @global(PositionableStream:ZeroPosition));
 	__INST(position) = __MKSMALLINT(pos);
 	buffer[len] = '\0';
 	RETURN ( (len != 0) ? __MKSTRING_L(buffer, len) : nil );
@@ -263,34 +269,35 @@
     if (__isNonNilObject(coll) && __bothSmallInteger(p, l)) {
 
 	pos = __intVal(p);
+	/* make 1-based */
+	pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition));
 	if (pos > 0 && pos <= __intVal(l)) {
 	    OBJ cls;
 
 	    cls = __qClass(coll);
 	    if (cls == @global(String)) {
 		if (pos <= __stringSize(coll)) {
-		    __INST(position) = __MKSMALLINT(pos + 1);
+		    __INST(position) = __MKSMALLINT(__intVal(__INST(position)) + 1);
 		    ch = __stringVal(coll)[pos-1];
 		    RETURN ( __MKSMALLINT(ch) );
 		}
 	    } else if (cls == @global(ByteArray)) {
 		if (pos <= __byteArraySize(coll)) {
-		    __INST(position) = __MKSMALLINT(pos + 1);
+		    __INST(position) = __MKSMALLINT(__intVal(__INST(position)) + 1);
 		    ch = __ByteArrayInstPtr(coll)->ba_element[pos-1];
 		    RETURN ( __MKSMALLINT(ch) );
 		}
 	    } else if (cls == @global(Array)) {
 		if (pos <= __arraySize(coll)) {
-		    __INST(position) = __MKSMALLINT(pos + 1);
+		    __INST(position) = __MKSMALLINT(__intVal(__INST(position)) + 1);
 		    RETURN ( __ArrayInstPtr(coll)->a_element[pos-1]);
 		}
 	    }
 	}
     }
-%}
-.
-    (position > readLimit) ifTrue:[^ self pastEnd].
-    ret := collection at:position.
+%}.
+    ((position + 1 - ZeroPosition) > readLimit) ifTrue:[^ self pastEnd].
+    ret := collection at:(position + 1 -ZeroPosition).
     position := position + 1.
     ^ ret
 !
@@ -314,6 +321,8 @@
     if (__isString(coll) && __bothSmallInteger(p, l)) {
 
 	pos = __intVal(p);
+	/* make 1-based */
+	pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition));
 	limit = __intVal(l);
 	sz = __qSize(coll) - OHDR_SIZE;
 	if (sz < limit)
@@ -330,6 +339,8 @@
 	    if (val > (_MAX_INT / 10)) goto oops;
 	    cp++;
 	}
+	/* make ZeroPosition-based */
+	pos = pos - 1 + __intVal( @global(PositionableStream:ZeroPosition));
 	__INST(position) = __MKSMALLINT(pos);
 	RETURN (__MKSMALLINT(val));
     }
@@ -362,23 +373,26 @@
     l = __INST(readLimit);
 
     if (__isString(coll) && __bothSmallInteger(p, l)) {
-        REGISTER int pos;
-        unsigned ch;
+	REGISTER int pos;
+	unsigned ch;
 
-        pos = __intVal(p);
-        if ((pos > 0) && (pos < __intVal(l)) && (pos < __stringSize(coll))) {
-            pos += 1;
-            __INST(position) = __MKSMALLINT(pos);
-            ch = __stringVal(coll)[pos-1];
-            RETURN ( __MKCHARACTER(ch) );
-        }
+	pos = __intVal(p);
+	/* make 1-based */
+	pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition));
+	if ((pos > 0) && (pos < __intVal(l)) && (pos < __stringSize(coll))) {
+	    pos = pos + 1;
+	    ch = __stringVal(coll)[pos-1];
+	    /* make ZeroPosition-based */
+	    pos = pos - 1 + __intVal( @global(PositionableStream:ZeroPosition));
+	    __INST(position) = __MKSMALLINT(pos);
+	    RETURN ( __MKCHARACTER(ch) );
+	}
     }
-%}
-.
-    (position > readLimit) ifTrue:[^ self pastEnd].
+%}.
+    ((position + 1 - ZeroPosition) > readLimit) ifTrue:[^ self pastEnd].
     position := position + 1.
-    (position > readLimit) ifTrue:[^ self pastEnd].
-    ^ collection at:position
+    ((position + 1 - ZeroPosition) > readLimit) ifTrue:[^ self pastEnd].
+    ^ collection at:(position + 1 - ZeroPosition)
 !
 
 nextSymbol
@@ -400,6 +414,8 @@
     if (__isString(coll) && __bothSmallInteger(p, l)) {
 
 	pos = __intVal(p);
+	/* make 1-based */
+	pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition));
 	limit = __intVal(l);
 	sz = __qSize(coll) - OHDR_SIZE;
 	if (sz < limit)
@@ -425,6 +441,8 @@
 	    cp++;
 	}
 
+	/* make ZeroPosition-based */
+	pos = pos - 1 + __intVal( @global(PositionableStream:ZeroPosition));
 	__INST(position) = __MKSMALLINT(pos);
 	buffer[len] = '\0';
 	RETURN ( (len != 0) ? __MKSTRING_L(buffer, len) : nil );
@@ -452,30 +470,31 @@
 
     if (__isNonNilObject(coll) && __bothSmallInteger(p, l)) {
 
-        pos = __intVal(p);
-        if (pos <= __intVal(l) && pos > 0) {
-            cls = __qClass(coll);
-            if (cls == @global(String)) {
-                if (pos <= __stringSize(coll)) {
-                    ch = __stringVal(coll)[pos-1];
-                    RETURN ( __MKCHARACTER(ch) );
-                }
-            } else if (cls == @global(ByteArray)) {
-                if (pos <= __byteArraySize(coll)) {
-                    ch = __ByteArrayInstPtr(coll)->ba_element[pos-1];
-                    RETURN ( __MKSMALLINT(ch) );
-                }
-            } else if (cls == @global(Array)) {
-                if (pos <= __arraySize(coll)) {
-                    RETURN ( __ArrayInstPtr(coll)->a_element[pos-1]);
-                }
-            }
-        }
+	pos = __intVal(p);
+	/* make 1-based */
+	pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition));
+	if (pos <= __intVal(l) && pos > 0) {
+	    cls = __qClass(coll);
+	    if (cls == @global(String)) {
+		if (pos <= __stringSize(coll)) {
+		    ch = __stringVal(coll)[pos-1];
+		    RETURN ( __MKCHARACTER(ch) );
+		}
+	    } else if (cls == @global(ByteArray)) {
+		if (pos <= __byteArraySize(coll)) {
+		    ch = __ByteArrayInstPtr(coll)->ba_element[pos-1];
+		    RETURN ( __MKSMALLINT(ch) );
+		}
+	    } else if (cls == @global(Array)) {
+		if (pos <= __arraySize(coll)) {
+		    RETURN ( __ArrayInstPtr(coll)->a_element[pos-1]);
+		}
+	    }
+	}
     }
-%}
-.
-    (position > readLimit) ifTrue:[^ self pastEnd].
-    ^ collection at:position
+%}.
+    ((position + 1 - ZeroPosition) > readLimit) ifTrue:[^ self pastEnd].
+    ^ collection at:(position + 1 - ZeroPosition)
 !
 
 peekOrNil
@@ -498,33 +517,34 @@
 
     if (__isNonNilObject(coll) && __bothSmallInteger(p, l)) {
 
-        pos = __intVal(p);
-        if (pos <= __intVal(l) && pos > 0) {
-            cls = __qClass(coll);
-            if (cls == @global(String)) {
-                if (pos <= __stringSize(coll)) {
-                    ch = __stringVal(coll)[pos-1];
-                    RETURN ( __MKCHARACTER(ch) );
-                }
-                RETURN ( nil );
-            } else if (cls == @global(ByteArray)) {
-                if (pos <= __byteArraySize(coll)) {
-                    ch = __ByteArrayInstPtr(coll)->ba_element[pos-1];
-                    RETURN ( __MKSMALLINT(ch) );
-                }
-                RETURN ( nil );
-            } else if (cls == @global(Array)) {
-                if (pos <= __arraySize(coll)) {
-                    RETURN ( __ArrayInstPtr(coll)->a_element[pos-1]);
-                }
-                RETURN ( nil );
-            }
-        }
+	pos = __intVal(p);
+	/* make 1-based */
+	pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition));
+	if (pos <= __intVal(l) && pos > 0) {
+	    cls = __qClass(coll);
+	    if (cls == @global(String)) {
+		if (pos <= __stringSize(coll)) {
+		    ch = __stringVal(coll)[pos-1];
+		    RETURN ( __MKCHARACTER(ch) );
+		}
+		RETURN ( nil );
+	    } else if (cls == @global(ByteArray)) {
+		if (pos <= __byteArraySize(coll)) {
+		    ch = __ByteArrayInstPtr(coll)->ba_element[pos-1];
+		    RETURN ( __MKSMALLINT(ch) );
+		}
+		RETURN ( nil );
+	    } else if (cls == @global(Array)) {
+		if (pos <= __arraySize(coll)) {
+		    RETURN ( __ArrayInstPtr(coll)->a_element[pos-1]);
+		}
+		RETURN ( nil );
+	    }
+	}
     }
-%}
-.
-    (position > readLimit) ifTrue:[^ nil].
-    ^ collection at:position
+%}.
+    ((position + 1 - ZeroPosition) > readLimit) ifTrue:[^ nil].
+    ^ collection at:(position + 1 - ZeroPosition)
 !
 
 skipSeparators
@@ -540,40 +560,46 @@
     l = __INST(readLimit);
 
     if (__isString(coll) && __bothSmallInteger(p, l)) {
-        REGISTER unsigned char *chars;
-        REGISTER unsigned ch;
-        REGISTER int pos;
-        int limit;
-        int sz;
+	REGISTER unsigned char *chars;
+	REGISTER unsigned ch;
+	REGISTER int pos;
+	int limit;
+	int sz;
 
-        pos = __intVal(p);
-        if (pos <= 0) {
-            RETURN ( nil );
-        }
+	pos = __intVal(p);
+	/* make 1-based */
+	pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition));
+	if (pos <= 0) {
+	    RETURN ( nil );
+	}
 
-        limit = __intVal(l);
-        sz = __qSize(coll) - OHDR_SIZE;
-        if (limit > sz) {
-            limit = sz;
-        }
+	limit = __intVal(l);
+	sz = __qSize(coll) - OHDR_SIZE;
+	if (limit > sz) {
+	    limit = sz;
+	}
 
-        chars = (unsigned char *)(__stringVal(coll) + pos - 1);
-        while (pos <= limit) {
-            pos++;
-            ch = *chars++;
-            if ((ch > 0x20)
-             || ((ch != ' ')
-                 && (ch != '\t')
-                 && (ch != '\r')
-                 && (ch != '\n')
-                 && (ch != '\f')
-                 && (ch != 0x0B))) {
-                __INST(position) = __MKSMALLINT(pos-1);
-                RETURN ( __MKCHARACTER(ch) );
-            }
-        }
-        __INST(position) = __MKSMALLINT(pos);
-        RETURN ( nil );
+	chars = (unsigned char *)(__stringVal(coll) + pos - 1);
+	while (pos <= limit) {
+	    pos++;
+	    ch = *chars++;
+	    if ((ch > 0x20)
+	     || ((ch != ' ')
+		 && (ch != '\t')
+		 && (ch != '\r')
+		 && (ch != '\n')
+		 && (ch != '\f')
+		 && (ch != 0x0B))) {
+		/* make ZeroPosition-based */
+		pos = pos - 1 + __intVal( @global(PositionableStream:ZeroPosition));
+		__INST(position) = __MKSMALLINT(pos-1);
+		RETURN ( __MKCHARACTER(ch) );
+	    }
+	}
+	/* make ZeroPosition-based */
+	pos = pos - 1 + __intVal( @global(PositionableStream:ZeroPosition));
+	__INST(position) = __MKSMALLINT(pos);
+	RETURN ( nil );
     }
 %}.
     ^ super skipSeparators
@@ -601,37 +627,43 @@
     l = __INST(readLimit);
 
     if (__isString(coll) && __bothSmallInteger(p, l)) {
-        REGISTER unsigned char *chars;
-        REGISTER unsigned ch;
-        REGISTER int pos;
-        int limit;
+	REGISTER unsigned char *chars;
+	REGISTER unsigned ch;
+	REGISTER int pos;
+	int limit;
 
-        pos = __intVal(p);
-        if (pos <= 0) {
-            RETURN ( nil );
-        }
+	pos = __intVal(p);
+	/* make 1-based */
+	pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition));
+	if (pos <= 0) {
+	    RETURN ( nil );
+	}
 
-        limit = __intVal(l);
-        if (limit > (__qSize(coll) - OHDR_SIZE))
-            limit = __qSize(coll) - OHDR_SIZE;
+	limit = __intVal(l);
+	if (limit > (__qSize(coll) - OHDR_SIZE))
+	    limit = __qSize(coll) - OHDR_SIZE;
 
-        chars = (unsigned char *)(__stringVal(coll) + pos - 1);
-        while (pos <= limit) {
-            ch = *chars++;
-            if (((int)ch > 0x20)
-             || (
-                 (ch != ' ') 
-                 && (ch != '\t')
-                 && (ch != '\f')
-                 && (ch != '\b')
-                 && (ch != 0x0B))) {
-                __INST(position) = __MKSMALLINT(pos);
-                RETURN ( __MKCHARACTER(ch) );
-            }
-            pos++;
-        }
-        __INST(position) = __MKSMALLINT(pos);
-        RETURN ( nil );
+	chars = (unsigned char *)(__stringVal(coll) + pos - 1);
+	while (pos <= limit) {
+	    ch = *chars++;
+	    if (((int)ch > 0x20)
+	     || (
+		 (ch != ' ') 
+		 && (ch != '\t')
+		 && (ch != '\f')
+		 && (ch != '\b')
+		 && (ch != 0x0B))) {
+		/* make ZeroPosition-based */
+		pos = pos - 1 + __intVal( @global(PositionableStream:ZeroPosition));
+		__INST(position) = __MKSMALLINT(pos);
+		RETURN ( __MKCHARACTER(ch) );
+	    }
+	    pos++;
+	}
+	/* make ZeroPosition-based */
+	pos = pos - 1 + __intVal( @global(PositionableStream:ZeroPosition));
+	__INST(position) = __MKSMALLINT(pos);
+	RETURN ( nil );
     }
 %}
 .
@@ -655,34 +687,40 @@
     if (__isString(coll)
      && __isCharacter(anObject)
      && __bothSmallInteger(p, l)) {
-        REGISTER unsigned char *chars;
-        REGISTER int pos, limit;
-        unsigned ch;
-        int sz;
+	REGISTER unsigned char *chars;
+	REGISTER int pos, limit;
+	unsigned ch;
+	int sz;
 
-        pos = __intVal(p);
-        if (pos <= 0) {
-            RETURN ( nil );
-        }
+	pos = __intVal(p);
+	/* make 1-based */
+	pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition));
+	if (pos <= 0) {
+	    RETURN ( nil );
+	}
 
-        limit = __intVal(l);
-        sz = __stringSize(coll);
-        if (limit > sz) limit = sz;
+	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 ( self );
-            }
-            chars++;
-            pos++;
-        }
-        __INST(position) = __MKSMALLINT(pos+1);
-        RETURN ( nil );
+	chars = (unsigned char *)(__stringVal(coll) + pos - 1);
+	ch = __intVal(_characterVal(anObject)) & 0xFF;
+	while (pos < limit) {
+	    if (*chars == ch) {
+		ch = *++chars;
+		pos++;
+		/* make ZeroPosition-based */
+		pos = pos - 1 + __intVal( @global(PositionableStream:ZeroPosition));
+		__INST(position) = __MKSMALLINT(pos);
+		RETURN ( self );
+	    }
+	    chars++;
+	    pos++;
+	}
+	/* make ZeroPosition-based */
+	pos = pos - 1 + __intVal( @global(PositionableStream:ZeroPosition));
+	__INST(position) = __MKSMALLINT(pos+1);
+	RETURN ( nil );
     }
 %}.
     ^ super skipThrough:anObject
@@ -699,5 +737,5 @@
 !ReadStream class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/ReadStream.st,v 1.45 2002-10-29 10:55:29 james Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/ReadStream.st,v 1.46 2003-02-25 09:05:53 cg Exp $'
 ! !