From fe4543e194941e464e9468a7b1a3d4ca64ca8086 Mon Sep 17 00:00:00 2001 From: Simon Kornblith Date: Sun, 6 Nov 2016 13:11:33 -0500 Subject: [PATCH] Transcode strings to UTF-16 before passing them to MATLAB --- src/mxarray.jl | 13 +++++++++---- test/mxarray.jl | 5 +++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/mxarray.jl b/src/mxarray.jl index 2fae6bd..146bf54 100644 --- a/src/mxarray.jl +++ b/src/mxarray.jl @@ -264,8 +264,8 @@ const _mx_create_logical_scalar = mxfunc(:mxCreateLogicalScalar) const _mx_create_sparse = mxfunc(:mxCreateSparse_730) const _mx_create_sparse_logical = mxfunc(:mxCreateSparseLogicalMatrix_730) -const _mx_create_string = mxfunc(:mxCreateString) -#const _mx_create_char_array = mxfunc(:mxCreateCharArray_730) +# const _mx_create_string = mxfunc(:mxCreateString) +const _mx_create_char_array = mxfunc(:mxCreateCharArray_730) const _mx_create_cell_array = mxfunc(:mxCreateCellArray_730) @@ -406,8 +406,13 @@ end # char arrays and string function mxarray(s::String) - pm = ccall(_mx_create_string, Ptr{Void}, (Ptr{UInt8},), s) - MxArray(pm) + utf16string = transcode(UInt16, s) + pm = ccall(_mx_create_char_array, Ptr{Void}, (mwSize, Ptr{mwSize},), 2, + _dims_to_mwSize((1, length(utf16string)))) + mx = MxArray(pm) + ccall(:memcpy, Ptr{Void}, (Ptr{Void}, Ptr{Void}, UInt), data_ptr(mx), utf16string, + length(utf16string)*sizeof(UInt16)) + mx end # cell arrays diff --git a/test/mxarray.jl b/test/mxarray.jl index ab6d1f8..18db5f4 100644 --- a/test/mxarray.jl +++ b/test/mxarray.jl @@ -372,6 +372,11 @@ delete(x) @test isequal(y["efg"], [1, 2, 3]) @test y["xyz"] == "MATLAB" +# Test string encoding +str = "λ α γ" +@test jstring(mxarray(str)) == str +@test mat"all($str == [955 32 945 32 947])" + gc()