Skip to content

Commit

Permalink
ExteranlAddress>>fromString: converts string to UTF-8 first
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-krivanek committed May 1, 2024
1 parent fa9e2f8 commit 1ab6d62
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ TFUFFIDerivedTypeMarshallingTest >> testMarshallingStringReturnValue [
| cString originalString returnedString |
[
originalString := 'áèïô å∫'.
cString := ExternalAddress fromString: originalString utf8Encoded asString.
cString := ExternalAddress fromString: originalString.
returnedString := self pointerToString: cString.
self assert: returnedString equals: originalString.
] ensure: [ cString free ]
Expand All @@ -182,7 +182,7 @@ TFUFFIDerivedTypeMarshallingTest >> testMarshallingStringReturnValueWithAlias [
| cString originalString returnedString |
[
originalString := 'áèïô å∫'.
cString := ExternalAddress fromString: originalString utf8Encoded asString.
cString := ExternalAddress fromString: originalString.
returnedString := self pointerToStringWithAlias: cString.
self assert: returnedString equals: originalString.
] ensure: [ cString free ]
Expand Down
2 changes: 1 addition & 1 deletion src/ThreadedFFI/TFStringType.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Class {
TFStringType >> allocateString: aString [
| anExternalAddress |

anExternalAddress := (ExternalAddress fromString: aString utf8Encoded).
anExternalAddress := (ExternalAddress fromString: aString).
self allocatedStrings add: anExternalAddress.
^ anExternalAddress
]
Expand Down
5 changes: 3 additions & 2 deletions src/UnifiedFFI/ByteArray.extension.st
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ ByteArray >> utf8StringFromCString [
]

{ #category : '*UnifiedFFI' }
ByteArray >> writeString: aString [
LibC memCopy: aString to: self size: aString size
ByteArray >> writeString: aByteString [

LibC memCopy: aByteString to: self size: aByteString size
]
10 changes: 6 additions & 4 deletions src/UnifiedFFI/ExternalAddress.extension.st
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,14 @@ ExternalAddress >> fromAddress: aNumber [

{ #category : '*UnifiedFFI' }
ExternalAddress class >> fromString: aString [
| result |
| result utf8Encoded |

utf8Encoded := aString utf8Encoded.

result := self allocate: aString size + 1.
result := self allocate: utf8Encoded size + 1.
(self assert: result isNotNil).
result writeString: aString.
result unsignedByteAt: aString size + 1 put: 0.
result writeString: utf8Encoded.
result unsignedByteAt: utf8Encoded size + 1 put: 0.
^ result
]

Expand Down

0 comments on commit 1ab6d62

Please sign in to comment.