Skip to content

Commit

Permalink
Add IOBuffer keyword support (#501)
Browse files Browse the repository at this point in the history
  • Loading branch information
TotalVerb authored Feb 25, 2018
1 parent e39939a commit 3bac86f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
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 @@ -1310,7 +1310,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 @@ -1325,7 +1325,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 @@ -1519,7 +1519,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 @@ -1565,6 +1565,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)
if maxsize < 0
throw(ArgumentError("negative maxsize: $(maxsize)"))
end
if sizehint !== nothing
sizehint!(data, sizehint)
end
buf = if data !== nothing
Base.IOBuffer(data, read, write, Int(maxsize))
else
size = maxsize == typemax(Int) ? 32 : Int(maxsize)
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 @@ -1432,4 +1432,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

0 comments on commit 3bac86f

Please sign in to comment.