Read a file separated by blank lines?

String(take!(buf)) already constructs the string without making a copy — from a Vector{UInt} in the UTF-8 encoding, not a Vector{Char}.

(Vector{Char} requires 4 bytes per character, similar to UTF-32, which is different from the encoding used by String, and is not generally a recommended way to store strings.)

More generally, it is possible to construct a String(vec) from a vec::Vector{UInt8} without making a copy of vec, but only if the Vector{UInt8} is specially allocated — this special allocation is done by IOBuffer objects and also by read(io, numbytes) as documented in the String docstring, but can also be accomplished using the undocumented vec = Base.StringVector(numbytes) constructor. See also Document/export copy-free string allocation? · Issue #19945 · JuliaLang/julia · GitHub and Conversion of Vector{UInt8} to String without copy

You can also use StringViews.jl to create a String-like object (another subtype of AbstractString) from an AbstractVector{UInt8} (e.g. a subarray) without making a copy.

1 Like