Skip to content

Commit 2202c9b

Browse files
authored
Transcode strings to UTF-16 before passing them to MATLAB (#80)
1 parent 36fc4b3 commit 2202c9b

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/mxarray.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,8 @@ const _mx_create_logical_scalar = mxfunc(:mxCreateLogicalScalar)
264264
const _mx_create_sparse = mxfunc(:mxCreateSparse_730)
265265
const _mx_create_sparse_logical = mxfunc(:mxCreateSparseLogicalMatrix_730)
266266

267-
const _mx_create_string = mxfunc(:mxCreateString)
268-
#const _mx_create_char_array = mxfunc(:mxCreateCharArray_730)
267+
# const _mx_create_string = mxfunc(:mxCreateString)
268+
const _mx_create_char_array = mxfunc(:mxCreateCharArray_730)
269269

270270
const _mx_create_cell_array = mxfunc(:mxCreateCellArray_730)
271271

@@ -406,8 +406,13 @@ end
406406
# char arrays and string
407407

408408
function mxarray(s::String)
409-
pm = ccall(_mx_create_string, Ptr{Void}, (Ptr{UInt8},), s)
410-
MxArray(pm)
409+
utf16string = transcode(UInt16, s)
410+
pm = ccall(_mx_create_char_array, Ptr{Void}, (mwSize, Ptr{mwSize},), 2,
411+
_dims_to_mwSize((1, length(utf16string))))
412+
mx = MxArray(pm)
413+
ccall(:memcpy, Ptr{Void}, (Ptr{Void}, Ptr{Void}, UInt), data_ptr(mx), utf16string,
414+
length(utf16string)*sizeof(UInt16))
415+
mx
411416
end
412417

413418
# cell arrays

test/mxarray.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,4 +372,9 @@ delete(x)
372372
@test isequal(y["efg"], [1, 2, 3])
373373
@test y["xyz"] == "MATLAB"
374374

375+
# Test string encoding
376+
str = "λ α γ"
377+
@test jstring(mxarray(str)) == str
378+
@test mat"all($str == [955 32 945 32 947])"
379+
375380
gc()

0 commit comments

Comments
 (0)