Unicode32String.st
changeset 4965 cb05c61f9204
parent 3872 af31f9e3a160
child 5265 5f6a992925c2
--- a/Unicode32String.st	Tue May 28 11:09:39 2019 +0200
+++ b/Unicode32String.st	Tue May 28 14:47:31 2019 +0200
@@ -1,3 +1,5 @@
+"{ Encoding: utf8 }"
+
 "
  COPYRIGHT (c) 2004 by eXept Software AG 
               All Rights Reserved
@@ -77,14 +79,40 @@
     ^ self readSmalltalkStringFrom:aStreamOrString onError:exceptionBlock
 
     "
-        self readFrom:'abcäöü' storeString
-        String readFrom:'abcäöü' storeString
+        self readFrom:'abcäöü' storeString
+        String readFrom:'abcäöü' storeString
     "
 ! !
 
 
 !Unicode32String methodsFor:'conversion'!
 
+asUnicode16StringReplaceInvalidWith:replacementCharacter
+    "return the receiver converted to a 'normal' string,
+     with invalid characters replaced by replacementCharacter.
+     Can be used to convert from 16-bit strings to 8-bit strings
+     and replace characters above code-255 with some replacement."
+
+    |newString sz "{ Class:SmallInteger }"|
+
+    sz := self size.
+
+    newString := Unicode16String new:sz.
+    1 to:sz do:[:idx |
+        |char|
+
+        char := self at:idx.
+        char codePoint <= 16rFFFF ifTrue:[
+            newString at:idx put:char
+        ] ifFalse:[
+            newString at:idx put:replacementCharacter
+        ].
+    ].
+    ^ newString
+
+    "Created: / 28-05-2019 / 12:13:57 / Stefan Vogel"
+!
+
 asUnicode32String
     "as the receiver already is a unicode-32 string, return it"
 
@@ -125,7 +153,7 @@
         ].
 
         String streamContents:[:s|
-            'hello -öäüß' asUnicode32String storeOn:s
+            'hello -öäüß' asUnicode32String storeOn:s
         ].
     "
 
@@ -155,8 +183,8 @@
 
     "
         'hello' asUnicode32String storeString
-        'hello -öäüß' storeString
-        'hello -öäüß' asUnicode32String storeString
+        'hello -öäüß' storeString
+        'hello -öäüß' asUnicode32String storeString
     "
 !