Skip to content

Commit 88b1248

Browse files
String(v): some rewording of NEWS and docs
1 parent de014c9 commit 88b1248

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

NEWS.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,10 @@ Library improvements
434434
* The function `thisind(s::AbstractString, i::Integer)` returns the largest valid index
435435
less or equal than `i` in the string `s` or `0` if no such index exists ([#24414]).
436436

437-
* `String(array)` now accepts an arbitrary `AbstractVector{UInt8}`, and resizes
438-
`Vector{UInt8}` inputs to a zero length to prevent incorrect modification ([#26093]).
437+
* `String(array)` now accepts an arbitrary `AbstractVector{UInt8}` and "steals" the
438+
memory buffer of mutable arrays, leaving the byte vector with an empty buffer which
439+
is guaranteed not to be shared with the `String` object; if the byte vector is
440+
immutable, it simply shares memory with the string and is not truncated ([#26093]).
439441

440442
* `Irrational` is now a subtype of `AbstractIrrational` ([#24245]).
441443

base/strings/string.jl

+12-11
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,18 @@ const ByteArray = Union{Vector{UInt8},Vector{Int8}}
1818
"""
1919
String(v::AbstractVector{UInt8})
2020
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.
3233
"""
3334
String(v::AbstractVector{UInt8}) = String(copyto!(StringVector(length(v)), v))
3435
String(v::Vector{UInt8}) = ccall(:jl_array_to_string, Ref{String}, (Any,), v)

0 commit comments

Comments
 (0)