ExternalBytes.st
changeset 19422 a8a0b538f7da
parent 19419 362d601fee24
child 19431 3e697e4bcbf5
child 19495 885347ee3b18
--- a/ExternalBytes.st	Wed Mar 23 14:38:20 2016 +0100
+++ b/ExternalBytes.st	Wed Mar 23 14:50:57 2016 +0100
@@ -450,10 +450,33 @@
 !
 
 newNullTerminatedFromString:aString
-    ^ ((self new:aString size+1)
-	replaceBytesFrom:1 to:aString size with:aString startingAt:1)
-	at:aString size+1 put:0;
-	yourself
+    "allocate a null terminated string containing the chars of aString"
+
+    |nChars extBytes|
+
+    nChars := aString size.
+    self assert:(aString bitsPerCharacter == 8).
+
+    extBytes := self new:nChars+1.
+    extBytes replaceBytesFrom:1 to:nChars with:aString startingAt:1.
+    extBytes at:nChars+1 put:0.
+    ^ extBytes
+!
+
+newNullTerminatedFromWideString:aString
+    "allocate a null terminated wide string containing the chars of aString"
+
+    |nChars extBytes|
+
+    nChars := aString size.
+    self assert:(aString bitsPerCharacter <= 16).
+
+    extBytes := self new:((nChars+1)*2).
+    1 to:nChars do:[:i |
+        extBytes unsignedInt16At:(i*2) put:(aString at:i) codePoint.
+    ].
+    extBytes unsignedInt16At:((nChars+1)*2) put:0.
+    ^ extBytes
 !
 
 unprotectedNew:numberOfBytes