Stream.st
changeset 8054 7f31b0651027
parent 7687 7fb8c65990e6
child 8071 311e72cba17b
--- a/Stream.st	Wed Mar 03 22:11:13 2004 +0100
+++ b/Stream.st	Wed Mar 03 22:12:03 2004 +0100
@@ -1,3 +1,5 @@
+"{ Encoding: utf8 }"
+
 "
  COPYRIGHT (c) 1989 by Claus Gittinger
 	      All Rights Reserved
@@ -2612,10 +2614,113 @@
     "Modified: 11.7.1996 / 10:02:10 / cg"
 ! !
 
+!Stream methodsFor:'writing-chunks'!
+
+nextChunkPut:aString
+    "put aString as a chunk onto the receiver;
+     double all exclamation marks except within primitives and append a 
+     single delimiting exclamation mark at the end.
+     This modification of the chunk format (not doubling exclas in primitive code)
+     was done to have primitive code more readable and easier be edited in the fileBrowser
+     or other editors.
+     Its no incompatibility, since inline primitives are an ST/X special
+     and code containing ST/X primitives cannot be loaded into other smalltalks anyway."
+
+    self nextPutAllAsChunk:aString.
+    self nextPut:(self class chunkSeparator)
+
+    "Modified: 9.12.1995 / 15:56:54 / cg"
+!
+
+nextPutAllAsChunk:aString
+    "put aString as a chunk onto the receiver;
+     double all exclamation marks except within primitives.
+     This modification of the chunk format (not doubling exclas in primitive code)
+     was done to have primitive code more readable and easier be edited in the fileBrowser
+     or other editors.
+     Its no incompatibility, since inline primitives are an ST/X special
+     and code containing ST/X primitives cannot be loaded into other smalltalks anyway."
+
+    |sep stopChars inPrimitive character
+     index    "{ Class:SmallInteger }"
+     endIndex "{ Class:SmallInteger }"
+     stop     "{ Class:SmallInteger }"
+     next     "{ Class:SmallInteger }"|
+
+    endIndex := aString size.
+    endIndex == 0 ifTrue:[^ self].
+
+    sep := self class chunkSeparator.
+    stopChars := '{}' copyWith:sep.
+
+    inPrimitive := false.
+    index := 1.
+    stop := endIndex + 1.
+
+    [index <= endIndex] whileTrue:[
+	"
+	 find position of next interesting character; 
+	 output stuff up to that one in one piece
+	"
+	next := aString indexOfAny:stopChars startingAt:index ifAbsent:stop.
+
+	((index == 1) and:[next == stop]) ifTrue:[
+	    self nextPutAll:aString
+	] ifFalse:[
+	    self nextPutAll:aString startingAt:index to:(next - 1)
+	].
+
+	index := next.
+	(index <= endIndex) ifTrue:[
+	    character := aString at:index.
+
+	    (character == ${ ) ifTrue:[
+		"/ starts a primitive
+		((index > 1) and:[(aString at:index-1) == $%]) ifTrue:[
+		    inPrimitive := true
+		]
+	    ] ifFalse:[
+		"/ ends a primitive
+		(character == $} ) ifTrue:[
+		    ((index > 1) and:[(aString at:index-1) == $%]) ifTrue:[
+			inPrimitive := false
+		    ]
+		] ifFalse:[
+		    "/
+		    "/ exclas have to be doubled - except if within a primitive
+		    "/
+		    inPrimitive ifFalse:[
+			(character == sep) ifTrue:[
+			    self nextPut:sep
+			]
+		    ]
+		]
+	    ].
+
+	    self nextPut:character.
+	    index := index + 1.
+	].
+    ].
+    (aString endsWith:Character cr) ifFalse:[
+	self cr.
+    ].
+
+    "Modified: / 21.4.1998 / 17:22:47 / cg"
+    "/ foo
+!
+
+nextPutChunkSeparator
+    "append a chunk separator character"
+
+    self nextPut:(self class chunkSeparator)
+
+    "Created: 13.9.1995 / 17:39:26 / claus"
+! !
+
 !Stream class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Stream.st,v 1.124 2003-10-24 06:48:31 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Stream.st,v 1.125 2004-03-03 21:12:03 cg Exp $'
 ! !
 
 Stream initialize!