Skip to content

Commit 71c9211

Browse files
committed
Remove takestring!(::Memory)
This commit builds upon JuliaLang#54438, re-using the old String(::Vector{UInt8}) impl, which now no longer truncates. Also remove takestring!(::Memory) - that function probably should not be defined
1 parent d623923 commit 71c9211

File tree

1 file changed

+7
-20
lines changed

1 file changed

+7
-20
lines changed

base/strings/string.jl

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ String(v::AbstractVector{UInt8}) = takestring!(copyto!(StringMemory(length(v)),
6868
function String(v::Vector{UInt8})
6969
len = length(v)
7070
len == 0 && return ""
71-
# This method copies the content of the Memory such that the underlying
72-
# Memory is unchanged, but then empties the Vector and re-assigns a new
73-
# empty memory to the string.
74-
mem = StringMemory(len)
75-
GC.@preserve mem v unsafe_copyto!(pointer(mem), pointer(v), len)
76-
str = takestring!(mem)
71+
ref = v.ref
72+
if ref.ptr_or_offset == ref.mem.ptr
73+
str = ccall(:jl_genericmemory_to_string, Ref{String}, (Any, Int), ref.mem, len)
74+
else
75+
str = ccall(:jl_pchar_to_string, Ref{String}, (Ptr{UInt8}, Int), ref, len)
76+
end
7777
# optimized empty!(v); sizehint!(v, 0) calls
7878
setfield!(v, :size, (0,))
7979
setfield!(v, :ref, MemoryRef(Memory{UInt8}()))
@@ -96,20 +96,7 @@ julia> isempty(v)
9696
true
9797
```
9898
"""
99-
function takestring! end
100-
101-
function takestring!(v::Memory{UInt8})
102-
len = length(v)
103-
len == 0 && return ""
104-
# This function will truncate the memory to zero length
105-
return ccall(:jl_genericmemory_to_string, Ref{String}, (Any, Int), v, len)
106-
end
107-
108-
# Note: Currently, this constructor truncates for Vector, but not for AbstractVector.
109-
# See issue 32528
110-
function takestring!(v::Vector{UInt8})
111-
String(v)
112-
end
99+
takestring!(v::Vector{UInt8}) = String(v)
113100

114101
"""
115102
unsafe_string(p::Ptr{UInt8}, [length::Integer])

0 commit comments

Comments
 (0)