Skip to content

Commit 81e4251

Browse files
committed
deprecate unsafe_wrap for String
1 parent 3f54acd commit 81e4251

File tree

10 files changed

+38
-47
lines changed

10 files changed

+38
-47
lines changed

base/c.jl

-4
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,6 @@ pointer(p::Cwstring) = convert(Ptr{Cwchar_t}, p)
7373
==(x::Union{Cstring,Cwstring}, y::Ptr) = pointer(x) == y
7474
==(x::Ptr, y::Union{Cstring,Cwstring}) = x == pointer(y)
7575

76-
# here, not in pointer.jl, to avoid bootstrapping problems in coreimg.jl
77-
unsafe_wrap(::Type{String}, p::Cstring, own::Bool=false) = unsafe_wrap(String, convert(Ptr{UInt8}, p), own)
78-
unsafe_wrap(::Type{String}, p::Cstring, len::Integer, own::Bool=false) =
79-
unsafe_wrap(String, convert(Ptr{UInt8}, p), len, own)
8076
unsafe_string(s::Cstring) = unsafe_string(convert(Ptr{UInt8}, s))
8177

8278
# convert strings to String etc. to pass as pointers

base/deprecated.jl

+13
Original file line numberDiff line numberDiff line change
@@ -1483,4 +1483,17 @@ end
14831483
# Calling promote_op is likely a bad idea, so deprecate its convenience wrapper promote_eltype_op
14841484
@deprecate promote_eltype_op(op, As...) promote_op(op, map(eltype, As)...)
14851485

1486+
function unsafe_wrap(::Type{String}, p::Union{Ptr{UInt8},Ptr{Int8}}, len::Integer, own::Bool=false)
1487+
Base.depwarn("unsafe_wrap(String, ...) is deprecated; use `unsafe_string` instead.", :unsafe_wrap)
1488+
#ccall(:jl_array_to_string, Ref{String}, (Any,),
1489+
# ccall(:jl_ptr_to_array_1d, Vector{UInt8}, (Any, Ptr{UInt8}, Csize_t, Cint),
1490+
# Vector{UInt8}, p, len, own))
1491+
unsafe_string(p, len)
1492+
end
1493+
unsafe_wrap(::Type{String}, p::Union{Ptr{UInt8},Ptr{Int8}}, own::Bool=false) =
1494+
unsafe_wrap(String, p, ccall(:strlen, Csize_t, (Ptr{UInt8},), p), own)
1495+
unsafe_wrap(::Type{String}, p::Cstring, own::Bool=false) = unsafe_wrap(String, convert(Ptr{UInt8}, p), own)
1496+
unsafe_wrap(::Type{String}, p::Cstring, len::Integer, own::Bool=false) =
1497+
unsafe_wrap(String, convert(Ptr{UInt8}, p), len, own)
1498+
14861499
# End deprecations scheduled for 0.6

base/fft/FFTW.jl

+4-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,10 @@ sprint_plan_{T<:fftwDouble}(plan::FFTWPlan{T}) =
282282
sprint_plan_{T<:fftwSingle}(plan::FFTWPlan{T}) =
283283
ccall((:fftwf_sprint_plan,libfftwf), Ptr{UInt8}, (PlanPtr,), plan)
284284
function sprint_plan(plan::FFTWPlan)
285-
unsafe_wrap(String, sprint_plan_(plan), true)
285+
p = sprint_plan_(plan)
286+
str = unsafe_string(p)
287+
Libc.free(p)
288+
return str
286289
end
287290

288291
function show{T,K,inplace}(io::IO, p::cFFTWPlan{T,K,inplace})

base/gmp.jl

+4-2
Original file line numberDiff line numberDiff line change
@@ -532,8 +532,10 @@ hex(n::BigInt, pad::Int) = base(16, n, pad)
532532

533533
function base(b::Integer, n::BigInt)
534534
2 <= b <= 62 || throw(ArgumentError("base must be 2 ≤ base ≤ 62, got $b"))
535-
p = ccall((:__gmpz_get_str,:libgmp), Ptr{UInt8}, (Ptr{UInt8}, Cint, Ptr{BigInt}), C_NULL, b, &n)
536-
unsafe_wrap(String, p, true)
535+
nd = ndigits(n, b)
536+
str = Base._string_n(n < 0 ? nd+1 : nd)
537+
ccall((:__gmpz_get_str,:libgmp), Ptr{UInt8}, (Ptr{UInt8}, Cint, Ptr{BigInt}), str, b, &n)
538+
return str
537539
end
538540

539541
function base(b::Integer, n::BigInt, pad::Integer)

base/path.jl

+3-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,9 @@ else # !windows
288288
function realpath(path::AbstractString)
289289
p = ccall(:realpath, Ptr{UInt8}, (Cstring, Ptr{UInt8}), path, C_NULL)
290290
systemerror(:realpath, p == C_NULL)
291-
return unsafe_wrap(String, p, true)
291+
str = unsafe_string(p)
292+
Libc.free(p)
293+
return str
292294
end
293295
end # os-test
294296

base/pointer.jl

-26
Original file line numberDiff line numberDiff line change
@@ -93,32 +93,6 @@ program, in the same manner as C.
9393
unsafe_store!(p::Ptr{Any}, x::ANY, i::Integer=1) = pointerset(p, x, Int(i), 1)
9494
unsafe_store!{T}(p::Ptr{T}, x, i::Integer=1) = pointerset(p, convert(T,x), Int(i), 1)
9595

96-
# unsafe pointer to string conversions (don't make a copy, unlike unsafe_string)
97-
# (Cstring versions are in c.jl)
98-
"""
99-
unsafe_wrap(String, p::Ptr{UInt8}, [length,] own=false)
100-
101-
Wrap a pointer `p` to an array of bytes in a `String` object,
102-
interpreting the bytes as UTF-8 encoded characters *without making a
103-
copy*. The optional `length` argument indicates the length in bytes of
104-
the pointer's data; if it is omitted, the data is assumed to be
105-
NUL-terminated. The `own` argument optionally specifies whether Julia
106-
should take ownership of the memory, calling `free` on the pointer
107-
when the array is no longer referenced.
108-
109-
This function is labelled "unsafe" because it will crash if `p` is not
110-
a valid memory address to data of the requested length.
111-
112-
See also [`unsafe_string`](@ref), which takes a pointer
113-
and makes a copy of the data.
114-
"""
115-
unsafe_wrap(::Type{String}, p::Union{Ptr{UInt8},Ptr{Int8}}, len::Integer, own::Bool=false) =
116-
ccall(:jl_array_to_string, Ref{String}, (Any,),
117-
ccall(:jl_ptr_to_array_1d, Vector{UInt8}, (Any, Ptr{UInt8}, Csize_t, Cint),
118-
Vector{UInt8}, p, len, own))
119-
unsafe_wrap(::Type{String}, p::Union{Ptr{UInt8},Ptr{Int8}}, own::Bool=false) =
120-
unsafe_wrap(String, p, ccall(:strlen, Csize_t, (Ptr{UInt8},), p), own)
121-
12296
# convert a raw Ptr to an object reference, and vice-versa
12397
"""
12498
unsafe_pointer_to_objref(p::Ptr)

base/strings/string.jl

-3
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ Copy a string from the address of a C-style (NUL-terminated) string encoded as U
3030
3131
This function is labelled "unsafe" because it will crash if `p` is not
3232
a valid memory address to data of the requested length.
33-
34-
See also [`unsafe_wrap(String, p, [length])`](@ref), which takes a pointer
35-
and wraps a string object around it without making a copy.
3633
"""
3734
function unsafe_string(p::Union{Ptr{UInt8},Ptr{Int8}}, len::Integer)
3835
p == C_NULL && throw(ArgumentError("cannot convert NULL to string"))

base/strings/utf8proc.jl

+13-8
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,19 @@ const UTF8PROC_STRIPMARK = (1<<13)
6868

6969
############################################################################
7070

71-
function utf8proc_map(s::String, flags::Integer)
72-
p = Ref{Ptr{UInt8}}()
73-
result = ccall(:utf8proc_map, Cssize_t,
74-
(Ptr{UInt8}, Cssize_t, Ref{Ptr{UInt8}}, Cint),
75-
s, sizeof(s), p, flags)
76-
result < 0 && error(unsafe_string(ccall(:utf8proc_errmsg, Cstring,
77-
(Cssize_t,), result)))
78-
unsafe_wrap(String, p[], result, true)::String
71+
utf8proc_error(result) = error(unsafe_string(ccall(:utf8proc_errmsg, Cstring, (Cssize_t,), result)))
72+
73+
function utf8proc_map(str::String, options::Integer)
74+
nwords = ccall(:utf8proc_decompose, Int, (Ptr{UInt8}, Int, Ptr{UInt8}, Int, Cint),
75+
str, sizeof(str), C_NULL, 0, options)
76+
nwords < 0 && utf8proc_error(nwords)
77+
buffer = Base.StringVector(nwords*4)
78+
nwords = ccall(:utf8proc_decompose, Int, (Ptr{UInt8}, Int, Ptr{UInt8}, Int, Cint),
79+
str, sizeof(str), buffer, nwords, options)
80+
nwords < 0 && utf8proc_error(nwords)
81+
nbytes = ccall(:utf8proc_reencode, Int, (Ptr{UInt8}, Int, Cint), buffer, nwords, options)
82+
nbytes < 0 && utf8proc_error(nbytes)
83+
return String(resize!(buffer, nbytes))
7984
end
8085

8186
utf8proc_map(s::AbstractString, flags::Integer) = utf8proc_map(String(s), flags)

doc/src/stdlib/strings.md

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ Base.repr
1010
Core.String(::AbstractString)
1111
Base.transcode
1212
Base.unsafe_string
13-
Base.unsafe_wrap(::Type{String}, ::Union{Ptr{Int8}, Ptr{UInt8}}, ::Integer, ::Bool)
1413
Base.codeunit(::AbstractString, ::Integer)
1514
Base.ascii
1615
Base.@r_str

test/strings/basic.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ end
245245

246246
cstrdup(s) = @static is_windows() ? ccall(:_strdup, Cstring, (Cstring,), s) : ccall(:strdup, Cstring, (Cstring,), s)
247247
let p = cstrdup("hello")
248-
@test unsafe_string(p) == "hello" == unsafe_wrap(String, cstrdup(p), true)
248+
@test unsafe_string(p) == "hello"
249249
Libc.free(p)
250250
end
251251

0 commit comments

Comments
 (0)