Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make IOBuffer(array) writable by default #24430

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions base/iobuffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ StringVector(n::Integer) = Vector{UInt8}(_string_n(n))
# IOBuffers behave like Files. They are typically readable and writable. They are seekable. (They can be appendable).

"""
IOBuffer([data, ][readable::Bool=true, writable::Bool=false[, maxsize::Int=typemax(Int)]])
IOBuffer([data, ][readable::Bool=true, writable::Bool=true[, maxsize::Int=typemax(Int)]])

Create an `IOBuffer`, which may optionally operate on a pre-existing array. If the
readable/writable arguments are given, they restrict whether or not the buffer may be read
from or written to respectively. The last argument optionally specifies a size beyond which
the buffer may not be grown.
"""
IOBuffer(data::AbstractVector{UInt8}, readable::Bool=true, writable::Bool=false, maxsize::Integer=typemax(Int)) =
IOBuffer(data::AbstractVector{UInt8}, readable::Bool=true, writable::Bool=true, maxsize::Integer=typemax(Int)) =
GenericIOBuffer(data, readable, writable, true, false, maxsize)
function IOBuffer(readable::Bool, writable::Bool)
b = IOBuffer(StringVector(32), readable, writable)
Expand Down
4 changes: 2 additions & 2 deletions base/strings/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ julia> String(take!(io))
"Haho"
```
"""
IOBuffer(str::String) = IOBuffer(Vector{UInt8}(str))
IOBuffer(s::SubString{String}) = IOBuffer(view(Vector{UInt8}(s.string), s.offset + 1 : s.offset + sizeof(s)))
IOBuffer(str::String) = IOBuffer(Vector{UInt8}(str), true, false)
IOBuffer(s::SubString{String}) = IOBuffer(view(Vector{UInt8}(s.string), s.offset + 1 : s.offset + sizeof(s)), true, false)

# join is implemented using IO

Expand Down
9 changes: 9 additions & 0 deletions test/iobuffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,12 @@ let
io = IOBuffer(Int64(10))
@test io isa IOBuffer
end

let
# IOBuffer(array) is writable by default.
buf = IOBuffer(UInt8[])
@test write(buf, b"foobar") == 6
# IOBuffer(string) is not writable.
buf = IOBuffer("foo")
@test_throws ArgumentError write(buf, b"foobar")
end