CharacterWriteStream.st
branchjv
changeset 18608 7d521f25267c
parent 18285 7aab8c3dab19
parent 18598 913ebad954df
child 18617 fbfd2d411738
--- a/CharacterWriteStream.st	Sun Jul 12 06:35:39 2015 +0200
+++ b/CharacterWriteStream.st	Mon Jul 20 17:01:03 2015 +0100
@@ -99,41 +99,35 @@
 
 !CharacterWriteStream methodsFor:'private'!
 
-characterSizeChanged:aCharacterOrString size:additionalSize
-    "change aCollection to fit the size of aCharacter"
+characterSizeChangedTo:newCharacterSize size:additionalSize
+    "change aCollection to fit the size of newCharacterSize"
 
-    |sz newSz bitsPerCharacter|
+    |sz newSz|
 
-    bitsPerCharacter := aCharacterOrString bitsPerCharacter. 
-    currentCharacterSize < bitsPerCharacter ifTrue:[
-        sz := collection size.
+    currentCharacterSize < newCharacterSize ifTrue:[
+        newSz := sz := collection size.
         position + additionalSize >= sz ifTrue:[
-            newSz := sz + additionalSize.
-        ] ifFalse:[
-            newSz := sz.
+            newSz := newSz + additionalSize + 100.          "add some more space, maybe more will be added"
         ].
-        collection := (aCharacterOrString stringSpecies new:newSz) 
+        collection := ((CharacterArray speciesForCharacterSize:newCharacterSize) new:newSz) 
                         replaceFrom:1 to:sz with:collection startingAt:1.
-        currentCharacterSize := bitsPerCharacter.
+        currentCharacterSize := collection bitsPerCharacter.
     ].
 ! !
 
 !CharacterWriteStream methodsFor:'private-accessing'!
 
 on:aCollection
-
     currentCharacterSize := aCollection bitsPerCharacter.
     ^ super on:aCollection.
 !
 
 on:aCollection from:start to:stop
-
     currentCharacterSize := aCollection bitsPerCharacter.
     ^ super on:aCollection from:start to:stop.
 !
 
 with:aCollection
-
     currentCharacterSize := aCollection bitsPerCharacter.
     ^ super with:aCollection.
 ! !
@@ -145,42 +139,36 @@
      Redefined to avoid count grows of the underlying collection -
      instead a single grow on the final size is performed."
 
-    aCharacter bitsPerCharacter > currentCharacterSize ifTrue:[
-        self characterSizeChanged:aCharacter size:count.
+    |needCharacterSize|
+
+    needCharacterSize := aCharacter characterSize.
+    needCharacterSize > currentCharacterSize ifTrue:[
+        self characterSizeChangedTo:needCharacterSize size:count.
     ].
     super next:count put:aCharacter
 !
 
 nextPut:aCharacter
     "append the argument, aCharacter to the stream.
-     Specially tuned for appending to String, ByteArray and Array streams."
+     Specially tuned for appending to String, Unicode16String and Unicode32String streams."
 
 %{  /* NOCONTEXT */
 
 #ifndef NO_PRIM_STREAM
-    REGISTER int pos;
-    unsigned ch;
-    OBJ coll;
-    OBJ p, wL, rL;
-    int __readLimit = -1;
-
-    coll = __INST(collection);
-    p = __INST(position);
+    OBJ coll = __INST(collection);
+    OBJ p = __INST(position);
 
     if (__isNonNilObject(coll) && __isSmallInteger(p) && __isCharacter(aCharacter)) {
-        pos = __intVal(p);
-        /* make 1-based */
-        pos = pos + 1;
-        wL = __INST(writeLimit);
+        REGISTER int pos = __intVal(p) + 1;      /* make 1-based */
+        OBJ wL = __INST(writeLimit);
 
         if ((wL == nil)
          || (__isSmallInteger(wL) && (pos <= __intVal(wL)))) {
-            OBJ cls;
+            OBJ cls = __qClass(coll);
+            unsigned ch = __intVal(__characterVal(aCharacter));
+            OBJ rL = __INST(readLimit);
+            int __readLimit = -1;
 
-            cls = __qClass(coll);
-            ch = __intVal(__characterVal(aCharacter));
-
-            rL = __INST(readLimit);
             if (__isSmallInteger(rL)) {
                 __readLimit = __intVal(rL);
             }
@@ -228,10 +216,14 @@
 
     (writeLimit isNil
      or:[(position + 1) <= writeLimit]) ifTrue:[
-        currentCharacterSize < aCharacter bitsPerCharacter ifTrue:[
-            self characterSizeChanged:aCharacter size:1.
+        |needCharacterSize|
+
+        needCharacterSize := aCharacter characterSize.
+        needCharacterSize > currentCharacterSize ifTrue:[
+            self characterSizeChangedTo:needCharacterSize size:1.
+        ] ifFalse:[
+            (position >= collection size) ifTrue:[self growCollection].
         ].
-        (position >= collection size) ifTrue:[self growCollection].
         collection at:(position + 1) put:aCharacter.
         (position >= readLimit) ifTrue:[readLimit := (position + 1)].
         position := position + 1.
@@ -245,15 +237,21 @@
     "append aCollection to the receiver.
      Redefined to convert to a string of the needed charcter size."
 
-    aCollection bitsPerCharacter > currentCharacterSize ifTrue:[
-        self characterSizeChanged:aCollection size:aCollection size.
+    |needCharacterSize|
+
+    needCharacterSize := aCollection characterSize.
+    needCharacterSize > currentCharacterSize ifTrue:[
+        self characterSizeChangedTo:needCharacterSize size:aCollection size.
     ].
     super nextPutAll:aCollection
 !
 
 nextPutAll:aCollection startingAt:start to:stop
-    aCollection bitsPerCharacter > currentCharacterSize ifTrue:[
-        self characterSizeChanged:aCollection size:stop-start+1.
+    |needCharacterSize|
+
+    needCharacterSize := aCollection characterSize.
+    needCharacterSize > currentCharacterSize ifTrue:[
+        self characterSizeChangedTo:needCharacterSize size:stop-start+1.
     ].
     ^ super nextPutAll:aCollection startingAt:start to:stop
 !
@@ -269,10 +267,10 @@
 !CharacterWriteStream class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/CharacterWriteStream.st,v 1.15 2015-04-24 12:11:13 stefan Exp $'
+    ^ '$Header$'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic/CharacterWriteStream.st,v 1.15 2015-04-24 12:11:13 stefan Exp $'
+    ^ '$Header$'
 ! !