class: CharacterEncoderImplementations::ISO10646_to_UTF8
authorStefan Vogel <sv@exept.de>
Mon, 20 Jul 2015 13:19:51 +0200
changeset 18604 54caf7b64994
parent 18603 907f3fa1c5d7
child 18605 4f0a114fed00
class: CharacterEncoderImplementations::ISO10646_to_UTF8 class definition added: #flushSingleton #new make class a singleton. Preallocate more space in Stream #theOneAndOnlyInstance changed: #encodeString:
CharacterEncoderImplementations__ISO10646_to_UTF8.st
--- a/CharacterEncoderImplementations__ISO10646_to_UTF8.st	Sat Jul 18 12:05:38 2015 +0200
+++ b/CharacterEncoderImplementations__ISO10646_to_UTF8.st	Mon Jul 20 13:19:51 2015 +0200
@@ -1,3 +1,5 @@
+"{ Encoding: utf8 }"
+
 "
  COPYRIGHT (c) 2004 by eXept Software AG
 	      All Rights Reserved
@@ -20,6 +22,13 @@
 	category:'Collections-Text-Encodings'
 !
 
+ISO10646_to_UTF8 class instanceVariableNames:'theOneAndOnlyInstance'
+
+"
+ No other class instance variables are inherited by this class.
+"
+!
+
 !ISO10646_to_UTF8 class methodsFor:'documentation'!
 
 copyright
@@ -45,11 +54,41 @@
   Decoding (utf8 to unicode):
      |t|
 
-     t := ISO10646_to_UTF8 encodeString:'Helloœ'.
+     t := ISO10646_to_UTF8 encodeString:'Helloœ'.
      ISO10646_to_UTF8 decodeString:t.
 "
 ! !
 
+!ISO10646_to_UTF8 class methodsFor:'instance creation'!
+
+flushSingleton
+    "flushes the cached singleton"
+
+    theOneAndOnlyInstance := nil
+
+    "
+     self flushSingleton
+    "
+!
+
+new
+    "returns a singleton"
+
+    theOneAndOnlyInstance isNil ifTrue:[
+        theOneAndOnlyInstance := self basicNew initialize.
+    ].
+    ^ theOneAndOnlyInstance.
+!
+
+theOneAndOnlyInstance
+    "returns a singleton"
+
+    theOneAndOnlyInstance isNil ifTrue:[
+        theOneAndOnlyInstance := self basicNew initialize.
+    ].
+    ^ theOneAndOnlyInstance.
+! !
+
 !ISO10646_to_UTF8 methodsFor:'encoding & decoding'!
 
 decode:aCode
@@ -311,12 +350,12 @@
 
     |s|
 
-    "/ avoid creation of new strings
+    "/ avoid creation of new strings if possible
     aUnicodeString containsNon7BitAscii ifFalse:[
         ^ aUnicodeString asSingleByteString
     ].
 
-    s := WriteStream on:(String uninitializedNew:aUnicodeString size).
+    s := WriteStream on:(String uninitializedNew:(aUnicodeString size * 3 // 2)).
     aUnicodeString do:[:eachCharacter |
         |codePoint "{Class: SmallInteger }" b1 b2 b3 b4 b5 v "{Class: SmallInteger }"|