Skip to content

Commit cd12ef9

Browse files
committed
accept Ptr{Int8} in unsafe_string and unsafe_wrap (closes #244)
1 parent 0a37517 commit cd12ef9

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/Compat.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -1277,7 +1277,7 @@ else
12771277
end
12781278

12791279
if !isdefined(Base, :view)
1280-
const view = slice
1280+
const view = slice
12811281
end
12821282

12831283
if !isdefined(Base, :pointer_to_string)
@@ -1295,11 +1295,11 @@ end
12951295

12961296
if VERSION < v"0.5.0-dev+4612"
12971297
export unsafe_string, unsafe_wrap
1298-
unsafe_wrap(::Type{Compat.String}, p::Ptr, own::Bool=false) = pointer_to_string(p, own)
1299-
unsafe_wrap(::Type{Compat.String}, p::Ptr, len, own::Bool=false) = pointer_to_string(p, len, own)
1298+
unsafe_wrap(::Type{Compat.String}, p::@compat(Union{Ptr{Int8},Ptr{UInt8}}), own::Bool=false) = pointer_to_string(convert(Ptr{UInt8}, p), own)
1299+
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)
13001300
unsafe_wrap(::Type{Array}, p::Ptr, dims, own::Bool=false) = pointer_to_array(p, dims, own)
1301-
unsafe_string(p::Ptr{UInt8}) = bytestring(p)
1302-
unsafe_string(p::Ptr{UInt8}, len) = bytestring(p, len)
1301+
unsafe_string(p::@compat(Union{Ptr{Int8},Ptr{UInt8}})) = bytestring(p)
1302+
unsafe_string(p::@compat(Union{Ptr{Int8},Ptr{UInt8}}), len) = bytestring(p, len)
13031303
if Cstring != Ptr{UInt8}
13041304
unsafe_string(p::Cstring) = unsafe_string(Ptr{UInt8}(p))
13051305
end

test/runtests.jl

+5-1
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,10 @@ let
12341234
@test ptr == pointer(wrapped_str) # Test proper pointer aliasing behavior
12351235
@test ptr pointer(new_str)
12361236
@test ptr pointer(new_str2)
1237+
@test unsafe_string(convert(Ptr{Int8}, ptr)) == "test"
1238+
wrapped_str = unsafe_wrap(Compat.String, convert(Ptr{Int8}, ptr))
1239+
@test wrapped_str == "test"
1240+
@test ptr == pointer(wrapped_str)
12371241
x = [1, 2]
12381242
@test unsafe_wrap(Array, pointer(x), 2) == [1, 2]
12391243
end
@@ -1246,5 +1250,5 @@ end
12461250

12471251
# Add test for Base.view
12481252
let a = rand(10,10)
1249-
@test view(a, :, 1) == a[:,1]
1253+
@test view(a, :, 1) == a[:,1]
12501254
end

0 commit comments

Comments
 (0)