-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 not given, the buffer will be both readable and writable by default. | ||
|
||
# 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) | ||
|
||
|
@@ -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.") | ||
|
@@ -60,57 +72,45 @@ 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) | ||
|
||
""" | ||
IOBuffer(size::Integer) | ||
|
||
Create a fixed size IOBuffer. The buffer will not grow dynamically. | ||
|
||
# Examples | ||
```jldoctest | ||
julia> io = IOBuffer(12) | ||
IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=0, maxsize=12, ptr=1, mark=-1) | ||
|
||
julia> write(io, "Hello world.") | ||
12 | ||
|
||
julia> String(take!(io)) | ||
"Hello world." | ||
|
||
julia> write(io, "Hello world again.") | ||
12 | ||
|
||
julia> String(take!(io)) | ||
"Hello world " | ||
``` | ||
""" | ||
IOBuffer(maxsize::Integer) = (x=IOBuffer(StringVector(maxsize), true, true, maxsize); x.size=0; x) | ||
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 | ||
if flags.truncate | ||
buf.size = 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not important, just wondering: since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it is. Fixed in #25919. |
||
end | ||
return buf | ||
end | ||
|
||
# PipeBuffers behave like Unix Pipes. They are typically readable and writable, they act appendable, and are not seekable. | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -512,7 +512,7 @@ isascii(s::AbstractString) = all(isascii, s) | |
## string map, filter, has ## | ||
|
||
function map(f, s::AbstractString) | ||
out = IOBuffer(StringVector(sizeof(s)), true, true) | ||
out = IOBuffer(StringVector(sizeof(s)), read=true, write=true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Passing a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, I'll take care of it. |
||
truncate(out, 0) | ||
for c in s | ||
c′ = f(c) | ||
|
@@ -525,7 +525,7 @@ function map(f, s::AbstractString) | |
end | ||
|
||
function filter(f, s::AbstractString) | ||
out = IOBuffer(StringVector(sizeof(s)), true, true) | ||
out = IOBuffer(StringVector(sizeof(s)), read=true, write=true) | ||
truncate(out, 0) | ||
for c in s | ||
f(c) && write(out, c) | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.