Skip to content

Commit 5aeafeb

Browse files
committed
accept Ptr{Int8} in unsafe_string and unsafe_wrap (closes #244)
1 parent 12c36d7 commit 5aeafeb

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

src/Compat.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,11 +1301,11 @@ end
13011301

13021302
if VERSION < v"0.5.0-dev+4612"
13031303
export unsafe_string, unsafe_wrap
1304-
unsafe_wrap(::Type{Compat.String}, p::Ptr, own::Bool=false) = pointer_to_string(p, own)
1305-
unsafe_wrap(::Type{Compat.String}, p::Ptr, len, own::Bool=false) = pointer_to_string(p, len, own)
1304+
unsafe_wrap(::Type{Compat.String}, p::@compat(Union{Ptr{Int8},Ptr{UInt8}}), own::Bool=false) = pointer_to_string(convert(Ptr{UInt8}, p), own)
1305+
unsafe_wrap(::Type{Compat.String}, p::@compat(Union{Ptr{Int8},Ptr{UInt8}}), len, own::Bool=false) = pointer_to_string(convert(Ptr{UInt8}, p), len, own)
13061306
unsafe_wrap(::Type{Array}, p::Ptr, dims, own::Bool=false) = pointer_to_array(p, dims, own)
1307-
unsafe_string(p::Ptr{UInt8}) = bytestring(p)
1308-
unsafe_string(p::Ptr{UInt8}, len) = bytestring(p, len)
1307+
unsafe_string(p::@compat(Union{Ptr{Int8},Ptr{UInt8}})) = bytestring(p)
1308+
unsafe_string(p::@compat(Union{Ptr{Int8},Ptr{UInt8}}), len) = bytestring(p, len)
13091309
if Cstring != Ptr{UInt8}
13101310
unsafe_string(p::Cstring) = unsafe_string(Ptr{UInt8}(p))
13111311
end

test/runtests.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,8 @@ let
12421242
@test ptr == pointer(wrapped_str) # Test proper pointer aliasing behavior
12431243
@test ptr pointer(new_str)
12441244
@test ptr pointer(new_str2)
1245+
@test unsafe_string(convert(Ptr{Int8}, ptr)) == "test"
1246+
@test unsafe_wrap(Compat.String, convert(Ptr{Int8}, ptr)) == "test"
12451247
x = [1, 2]
12461248
@test unsafe_wrap(Array, pointer(x), 2) == [1, 2]
12471249
end

0 commit comments

Comments
 (0)