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

add keyword arguments to IOBuffer's constructors #25872

Merged
merged 5 commits into from
Feb 6, 2018
Merged
Changes from 1 commit
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
Next Next commit
add keyword arguments to IOBuffer's constructors
  • Loading branch information
bicycle1885 committed Feb 4, 2018
commit 3906356d7d591237b2a4490fc2d2cf8469b4271a
90 changes: 59 additions & 31 deletions base/iobuffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,27 @@ StringVector(n::Integer) = unsafe_wrap(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::AbstractVector{UInt8}]; keywords...) -> IOBuffer

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.
Create an in-memory I/O stream, which may optionally operate on a pre-existing array.

It may take optional keyword arguments:
- `read`, `write`, `append`: restricts operations to the buffer; see `open` for details.
- `truncate`: truncates the buffer size to zero length.
- `maxsize`: specifies a size beyond which the buffer may not be grown.

When `data` is given, the buffer will be both readable and writable by default.
Copy link
Member

@JeffBezanson JeffBezanson Feb 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is true --- the keyword arguments default to nothing, which open_flags interprets as read-only. And that's probably a good thing; for example we have

IOBuffer(str::String) = IOBuffer(unsafe_wrap(Vector{UInt8}, str))

which we want to remain read-only.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. I meant "When data is not given, ...". Fixed.


# Examples
```jldoctest
julia> io = IOBuffer();

julia> write(io, "JuliaLang is a GitHub organization.", " It has many members.")
56

julia> String(take!(io))
"JuliaLang is a GitHub organization. It has many members."

julia> io = IOBuffer("JuliaLang is a GitHub organization.")
IOBuffer(data=UInt8[...], readable=true, writable=false, seekable=true, append=false, size=35, maxsize=Inf, ptr=1, mark=-1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The examples don't include the truncate flag. As an aside, I guess I should write the code to figure out a minimal set of keywords to reproduce a particular set of open flags – this output is a bit on the long side.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. I added some examples in #25919.


Expand All @@ -50,7 +62,7 @@ julia> read(io, String)
julia> write(io, "This isn't writable.")
ERROR: ArgumentError: ensureroom failed, IOBuffer is not writeable

julia> io = IOBuffer(UInt8[], true, true, 34)
julia> io = IOBuffer(UInt8[], read=true, write=true, maxsize=34)
IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=0, maxsize=34, ptr=1, mark=-1)

julia> write(io, "JuliaLang is a GitHub organization.")
Expand All @@ -60,33 +72,49 @@ julia> String(take!(io))
"JuliaLang is a GitHub organization"
```
"""
IOBuffer(data::AbstractVector{UInt8}, readable::Bool=true, writable::Bool=false, maxsize::Integer=typemax(Int)) =
GenericIOBuffer(data, readable, writable, true, false, maxsize)
function IOBuffer(readable::Bool, writable::Bool)
b = IOBuffer(StringVector(32), readable, writable)
b.data[:] = 0
b.size = 0
return b
function IOBuffer(
data::AbstractVector{UInt8};
read::Union{Bool,Nothing}=nothing,
write::Union{Bool,Nothing}=nothing,
append::Union{Bool,Nothing}=nothing,
truncate::Union{Bool,Nothing}=nothing,
maxsize::Integer=typemax(Int))
if maxsize < 0
throw(ArgumentError("negative maxsize: $(maxsize)"))
end
flags = open_flags(read=read, write=write, append=append, truncate=truncate)
buf = GenericIOBuffer(data, flags.read, flags.write, true, flags.append, Int(maxsize))
if flags.truncate
buf.size = 0
end
return buf
end

"""
IOBuffer() -> IOBuffer

Create an in-memory I/O stream, which is both readable and writable.

# Examples
```jldoctest
julia> io = IOBuffer();

julia> write(io, "JuliaLang is a GitHub organization.", " It has many members.")
56

julia> String(take!(io))
"JuliaLang is a GitHub organization. It has many members."
```
"""
IOBuffer() = IOBuffer(true, true)
function IOBuffer(;
read::Union{Bool,Nothing}=true,
write::Union{Bool,Nothing}=true,
append::Union{Bool,Nothing}=nothing,
truncate::Union{Bool,Nothing}=true,
maxsize::Integer=typemax(Int))
size = maxsize == typemax(Int) ? 32 : Int(maxsize)
flags = open_flags(read=read, write=write, append=append, truncate=truncate)
buf = IOBuffer(
StringVector(size),
read=flags.read,
write=flags.write,
append=flags.append,
truncate=flags.truncate,
maxsize=maxsize)
buf.data[:] = 0 # TODO: Is this really needed?
if flags.truncate
buf.size = 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not important, just wondering: since truncate is also passed to the constructor above, is this redundant?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is. Fixed in #25919.

end
return buf
end

# TODO: deprecate these methods?
IOBuffer(data::AbstractVector{UInt8}, read::Bool, write::Bool, maxsize::Integer=typemax(Int)) = IOBuffer(data, read=read, write=write, maxsize=maxsize)
IOBuffer(read::Bool, write::Bool) = IOBuffer(read=read, write=write)
"""
IOBuffer(size::Integer)

Expand All @@ -110,7 +138,7 @@ julia> String(take!(io))
"Hello world "
```
"""
IOBuffer(maxsize::Integer) = (x=IOBuffer(StringVector(maxsize), true, true, maxsize); x.size=0; x)
IOBuffer(maxsize::Integer) = IOBuffer(maxsize=maxsize)

# PipeBuffers behave like Unix Pipes. They are typically readable and writable, they act appendable, and are not seekable.

Expand Down