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 IOBuffer keyword support #501

Merged
merged 1 commit into from
Feb 25, 2018
Merged
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ Currently, the `@compat` macro supports the following syntaxes:

* `Compat.names` supporting keyword arguments for `all` and `imported` ([#25647]).

* `Compat.IOBuffer` supporting keyword arguments ([#25873]).


## Renaming

Expand Down Expand Up @@ -571,5 +573,6 @@ includes this fix. Find the minimum version from there.
[#25738]: https://github.com/JuliaLang/julia/issues/25738
[#25780]: https://github.com/JuliaLang/julia/issues/25780
[#25819]: https://github.com/JuliaLang/julia/issues/25819
[#25873]: https://github.com/JuliaLang/julia/issues/25873
[#25990]: https://github.com/JuliaLang/julia/issues/25990
[#26089]: https://github.com/JuliaLang/julia/issues/26089
37 changes: 34 additions & 3 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1304,7 +1304,7 @@ enable_debug(x::Bool) = DEBUG[] = x
@static if !isdefined(Base, Symbol("@debug"))
function debug(msg)
DEBUG[] || return
buf = IOBuffer()
buf = Base.IOBuffer()
iob = Base.redirect(IOContext(buf, STDERR), Base.log_info_to, :debug)
print_with_color(:blue, iob, "Debug: "; bold = true)
Base.println_with_color(:blue, iob, chomp(string(msg)))
Expand All @@ -1319,7 +1319,7 @@ else
end
@static if !isdefined(Base, Symbol("@error"))
function _error(msg)
buf = IOBuffer()
buf = Base.IOBuffer()
iob = Base.redirect(IOContext(buf, STDERR), Base.log_error_to, :error)
print_with_color(Base.error_color(), iob, "Error: "; bold = true)
Base.println_with_color(Base.error_color(), iob, chomp(string(msg)))
Expand Down Expand Up @@ -1513,7 +1513,7 @@ else
Base.findnext(t::AbstractString, s::AbstractString, i::Integer) = search(s, t, i)
Base.findfirst(t::AbstractString, s::AbstractString) = search(s, t)

Base.findfirst(delim::EqualTo{UInt8}, buf::IOBuffer) = search(buf, delim.x)
Base.findfirst(delim::EqualTo{UInt8}, buf::Base.IOBuffer) = search(buf, delim.x)

Base.findprev(c::EqualTo{Char}, s::AbstractString, i::Integer) = rsearch(s, c.x, i)
Base.findlast(c::EqualTo{Char}, s::AbstractString) = rsearch(s, c.x)
Expand Down Expand Up @@ -1559,6 +1559,37 @@ else
end
end

# https://github.com/JuliaLang/julia/pull/25872
if VERSION < v"0.7.0-DEV.3734"
function IOBuffer(
data::Union{AbstractVector{UInt8},Nothing}=nothing;
read::Union{Bool,Nothing}=nothing,
write::Union{Bool,Nothing}=nothing,
truncate::Union{Bool,Nothing}=nothing,
maxsize::Integer=typemax(Int),
sizehint::Union{Integer,Nothing}=nothing)
write === nothing && (write = false)
read === nothing && (read = !write)
truncate === nothing && (truncate = false)
Copy link
Member

Choose a reason for hiding this comment

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

if maxsize < 0
throw(ArgumentError("negative maxsize: $(maxsize)"))
end
if sizehint !== nothing
sizehint!(data, sizehint)
Copy link
Member

Choose a reason for hiding this comment

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

This is wrong is data === nothing

end
buf = if data !== nothing
Base.IOBuffer(data, read, write, Int(maxsize))
else
size = maxsize == typemax(Int) ? 32 : Int(maxsize)
Copy link
Member

Choose a reason for hiding this comment

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

Base.IOBuffer(StringVector(size), read, write, Int(maxsize))
end
if truncate
buf.size = 0
end
return buf
end
end

include("deprecated.jl")

end # module Compat
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1427,4 +1427,11 @@ end
@test :foo in Compat.names(TestNames)
@test :bar in Compat.names(TestNames, all=true)

# 0.7.0-DEV.3734
let buf = Compat.IOBuffer(read=true, write=false, maxsize=25)
@test buf.readable
@test !buf.writable
@test buf.maxsize == 25
end

nothing