String from iterator (HasEltype(), which is UInt8, HasLength())

This question refers to v0.7.

I have an ex ante known number n of integers 0 \le \ldots \le 127, which are computed from something (I can make an iterator). They represent a string (of ASCII characters).

The number of characters is small (1 to 8). I am looking for a fast way to construct a String.

I can build up a Vector{UInt8}, but I am unsure if String(::Vector{UInt8}) copies, or if I need to do something special to make it not copy, eg somehow guarantee that the vector does not share structure.

Example:

# imagine I have an iterator with HasLength() and HasEltype(), latter is UInt8
iter = (i for i in UInt8.(97:99))

# does this copy the result of collect? it is unclear to me.
String(collect(iter))```
  String(v::AbstractVector{UInt8})

  Create a new String object from a byte vector v containing UTF-8 encoded characters. If v is Vector{UInt8} it will be truncated to zero length and future modification of v cannot affect the
  contents of the resulting string. To avoid truncation use String(copy(v)).
julia> b = collect(iter)
3-element Array{UInt8,1}:
 0x61
 0x62
 0x63

julia> String(b)
"abc"

julia> b
0-element Array{UInt8,1}
1 Like

Though standard Vector{UInt8} inputs are copied. They are resized to only enforce a consistent behavior. You can use Base.StringVector to create a special Vector{UInt8} whose contents will be reused without copying by String.

2 Likes