Skip to content

String(v::Memory{UInt8}) doesn't make a copy #54369

@nhz2

Description

@nhz2

The docstring of String says:

To avoid truncation
of Vector{UInt8} data, use String(copy(v)); for other AbstractVector types,
String(v) already makes a copy.

Since Memory is an AbstractVector, String should make a copy. However, it doesn't look like this is happening in:

function String(v::Memory{UInt8})
len = length(v)
len == 0 && return ""
return ccall(:jl_genericmemory_to_string, Ref{String}, (Any, Int), v, len)
end

julia> v = Memory{UInt8}(undef, 10);

julia> fill!(v, 0x01);

julia> String(v)
"\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01"

julia> v
0-element Memory{UInt8}

Maybe the current String(v::Memory{UInt8}) method could be renamed to something like take_string! to make it clear that it can mutate v.

Metadata

Metadata

Assignees

No one assigned

    Labels

    arrays[a, r, r, a, y, s]

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions