@@ -18,17 +18,18 @@ const ByteArray = Union{Vector{UInt8},Vector{Int8}}
18
18
"""
19
19
String(v::AbstractVector{UInt8})
20
20
21
- Create a new `String` from a vector `v` of bytes containing
22
- UTF-8 encoded characters.
23
-
24
- When possible, the data in `v` will be used directly rather than making
25
- a copy. This is the case for `Vector{UInt8}` arrays returned by [`take!`](@ref)
26
- on a writable [`IOBuffer`](@ref) or by [`read(io, nb)`](@ref). Else, a copy is made.
27
- In all cases, one should assume that the function takes "ownership" of the array
28
- and that its contents should not be subsequently modified. Implementations may choose
29
- to resize `v` to a zero length to enforce this (which is the case when `v` is a `Vector`).
30
-
31
- If you need to subsequently modify `v`, use `String(copy(v))` instead.
21
+ Create a new `String` from a byte vector `v` containing UTF-8 encoded
22
+ characters. If `v` is a mutable vector, it will be truncated and any future
23
+ modification of `v` cannot affect the contents of the resulting `String` object.
24
+ If `v` is an immutable vector it will not be truncated (it cannot be since it is
25
+ immutable). To avoid truncation of mutable `v` use `String(copy(v))`.
26
+
27
+ When possible, the data in `v` will be used directly when creating the `String`
28
+ object without making a copy. This is guaranteed to be the case for byte vectors
29
+ returned by [`take!`](@ref) on a writable [`IOBuffer`](@ref) and by calls to
30
+ [`read(io, nb)`](@ref). This allows zero-copy conversion of I/O data to strings.
31
+ In other cases mutable vector may be copied, but they are still truncated in
32
+ either case to guarantee consistent behavior.
32
33
"""
33
34
String (v:: AbstractVector{UInt8} ) = String (copyto! (StringVector (length (v)), v))
34
35
String (v:: Vector{UInt8} ) = ccall (:jl_array_to_string , Ref{String}, (Any,), v)
0 commit comments