CharacterArray.st
changeset 23971 1443e8e91a1b
parent 23945 79fc1428a627
child 23973 868df53eb2f5
--- a/CharacterArray.st	Mon Mar 25 16:25:09 2019 +0100
+++ b/CharacterArray.st	Mon Mar 25 16:28:54 2019 +0100
@@ -3485,6 +3485,35 @@
     "Modified (comment): / 03-02-2019 / 13:08:15 / Claus Gittinger"
 !
 
+asDenseUnicodeString
+    "return the receiver as single-byte, double byte or 4-byte unicode string,
+     depending on the number of bits required to hold all characters in myself.
+     Use this to extract non-wide parts from a wide string,
+     i.e. after a substring has been copied out of a wide string"
+
+    |nb|
+
+    nb := self bytesPerCharacterNeeded.
+    (nb ~~ self bytesPerCharacter) ifTrue:[
+        nb == 1 ifTrue:[
+            ^ self asSingleByteString
+        ].    
+        nb == 2 ifTrue:[
+            ^ self asUnicode16String
+        ].
+    ].
+    ^ self
+    
+    "
+     'abc' asUnicode16String asDenseUnicodeString
+     'abc' asUnicode32String asDenseUnicodeString
+     ('abc',(Character value:16r165)) asDenseUnicodeString
+     ('abc',(Character value:16r165)) asUnicode32String asDenseUnicodeString
+    "
+
+    "Created: / 25-03-2019 / 16:28:02 / Claus Gittinger"
+!
+
 asFilename
     "return a Filename with pathname taken from the receiver"
 
@@ -6434,8 +6463,7 @@
 !
 
 bytesPerCharacter
-    "return the underlying string's required
-     bytesPerCharacter
+    "return the underlying string's bytesPerCharacter
      (i.e. is it a regular String or a TwoByteString)"
 
     |string max|
@@ -6455,6 +6483,38 @@
      'hello' asUnicode16String bytesPerCharacter
      'hello' asText allBold bytesPerCharacter
     "
+
+    "Modified (comment): / 25-03-2019 / 16:24:02 / Claus Gittinger"
+!
+
+bytesPerCharacterNeeded
+    "return the actual underlying string's required bytesPerCharacter
+     (i.e. checks if all characters really need that depth)"
+
+    |string max mySize|
+
+    (string := self string) ~~ self ifTrue:[
+        ^ string bytesPerCharacterNeeded
+    ].
+
+    mySize := self bytesPerCharacter. 
+    
+    max := 1.
+    self do:[:eachCharacter |
+        max := max max:(eachCharacter bytesPerCharacter).
+        max == mySize ifTrue:[^ max].
+    ].
+    ^ max
+
+    "
+     'hello' bytesPerCharacter       -> 1
+     'hello' bytesPerCharacterNeeded -> 1
+     
+     'hello' asUnicode16String bytesPerCharacter       -> 2
+     'hello' asUnicode16String bytesPerCharacterNeeded -> 1
+    "
+
+    "Created: / 25-03-2019 / 16:22:00 / Claus Gittinger"
 !
 
 characterSize