Skip to content

Commit

Permalink
rename IOBuffer/SimpleIOBuffer to AbstractIOBuffer/IOBuffer, to minim…
Browse files Browse the repository at this point in the history
…ize unnecessary API changes
  • Loading branch information
vtjnash committed Jun 6, 2015
1 parent f607ba2 commit b67b0d1
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 83 deletions.
10 changes: 5 additions & 5 deletions base/LineEdit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using ..Terminals
import ..Terminals: raw!, width, height, cmove, getX,
getY, clear_line, beep

import Base: ensureroom, peek, show, AnyDict, SimpleIOBuffer
import Base: ensureroom, peek, show, AnyDict

abstract TextInterface
abstract ModeState
Expand Down Expand Up @@ -56,7 +56,7 @@ end
type PromptState <: ModeState
terminal::TextTerminal
p::Prompt
input_buffer::SimpleIOBuffer
input_buffer::IOBuffer
ias::InputAreaState
indent::Int
end
Expand Down Expand Up @@ -996,8 +996,8 @@ type SearchState <: ModeState
histprompt
#rsearch (true) or ssearch (false)
backward::Bool
query_buffer::SimpleIOBuffer
response_buffer::SimpleIOBuffer
query_buffer::IOBuffer
response_buffer::IOBuffer
ias::InputAreaState
#The prompt whose input will be replaced by the matched history
parent
Expand Down Expand Up @@ -1049,7 +1049,7 @@ type PrefixSearchState <: ModeState
terminal
histprompt
prefix::ByteString
response_buffer::SimpleIOBuffer
response_buffer::IOBuffer
ias::InputAreaState
indent::Int
#The prompt whose input will be replaced by the matched history
Expand Down
5 changes: 2 additions & 3 deletions base/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import Base:
Display,
display,
writemime,
AnyDict,
SimpleIOBuffer
AnyDict

import ..LineEdit:
CompletionProvider,
Expand Down Expand Up @@ -299,7 +298,7 @@ type REPLHistoryProvider <: HistoryProvider
history_file
cur_idx::Int
last_idx::Int
last_buffer::SimpleIOBuffer
last_buffer::IOBuffer
last_mode
mode_mapping
modes::Array{Symbol,1}
Expand Down
4 changes: 2 additions & 2 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -501,8 +501,8 @@ end

# 11554

read!(from::IOBuffer, p::Ptr, nb::Integer) = read!(from, p, Int(nb))
function read!(from::IOBuffer, p::Ptr, nb::Int)
read!(from::AbstractIOBuffer, p::Ptr, nb::Integer) = read!(from, p, Int(nb))
function read!(from::AbstractIOBuffer, p::Ptr, nb::Int)
depwarn("read!(::IOBuffer, ::Ptr) is unsafe and therefore deprecated", :read!)
from.readable || throw(ArgumentError("read failed, IOBuffer is not readable"))
avail = nb_available(from)
Expand Down
118 changes: 60 additions & 58 deletions base/iobuffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## work with AbstractVector{UInt8} via I/O primitives ##

# Stateful string
type IOBuffer{T<:AbstractVector{UInt8}} <: IO
type AbstractIOBuffer{T<:AbstractVector{UInt8}} <: IO

This comment has been minimized.

Copy link
@stevengj

stevengj Feb 12, 2016

Member

I have to say, I find it very confusing that AbstractIOBuffer{T} is a concrete type. Couldn't we have this be an abstract type?

This comment has been minimized.

Copy link
@StefanKarpinski

StefanKarpinski Feb 12, 2016

Member

Yes, that's crazy talk. You can't call a concrete type AbstractWhatever.

This comment has been minimized.

Copy link
@vtjnash

vtjnash Feb 12, 2016

Author Member

it's an abstract type with fields

This comment has been minimized.

Copy link
@StefanKarpinski

StefanKarpinski Feb 12, 2016

Member

It is not an abstract type.

This comment has been minimized.

Copy link
@JeffBezanson

JeffBezanson Feb 12, 2016

Member

We could call it something like IOBufferBase, GeneralIOBuffer, etc.

This comment has been minimized.

Copy link
@stevengj

stevengj Feb 13, 2016

Member

Why not just call it IOBuffer? That is what its constructor is called. Just have

abstract AbstractIOBuffer{T<:AbstractVector{UInt8}} <: IO
type IOBuffer{T} <: AbstractIOBuffer{T}
   ....
end

?

data::T # T should support: getindex, setindex!, length, copy!, resize!, and T()
readable::Bool
writable::Bool
Expand All @@ -14,22 +14,35 @@ type IOBuffer{T<:AbstractVector{UInt8}} <: IO
ptr::Int # read (and maybe write) pointer
mark::Int # reset mark location for ptr (or <0 for no mark)

IOBuffer(data::T,readable::Bool,writable::Bool,seekable::Bool,append::Bool,maxsize::Int) =
AbstractIOBuffer(data::T,readable::Bool,writable::Bool,seekable::Bool,append::Bool,maxsize::Int) =
new(data,readable,writable,seekable,append,length(data),maxsize,1,-1)
end
typealias SimpleIOBuffer IOBuffer{Vector{UInt8}}
IOBuffer{T<:AbstractVector{UInt8}}(data::T, readable::Bool, writable::Bool, seekable::Bool, append::Bool, maxsize::Int) =
IOBuffer{T}(data, readable, writable, seekable, append, maxsize)
typealias IOBuffer AbstractIOBuffer{Vector{UInt8}}

function copy(b::IOBuffer)
AbstractIOBuffer{T<:AbstractVector{UInt8}}(data::T, readable::Bool, writable::Bool, seekable::Bool, append::Bool, maxsize::Int) =
AbstractIOBuffer{T}(data, readable, writable, seekable, append, maxsize)

# IOBuffers behave like Files. They are typically readable and writable. They are seekable. (They can be appendable).
IOBuffer(data::AbstractVector{UInt8}, readable::Bool=true, writable::Bool=false, maxsize::Int=typemax(Int)) =
AbstractIOBuffer(data, readable, writable, true, false, maxsize)
IOBuffer(readable::Bool, writable::Bool) = IOBuffer(UInt8[], readable, writable)
IOBuffer() = IOBuffer(true, true)
IOBuffer(maxsize::Int) = (x=IOBuffer(Array(UInt8,maxsize), true, true, maxsize); x.size=0; x)

# PipeBuffers behave like Unix Pipes. They are typically readable and writable, they act appendable, and are not seekable.
PipeBuffer(data::Vector{UInt8}=UInt8[], maxsize::Int=typemax(Int)) =
AbstractIOBuffer(data,true,true,false,true,maxsize)
PipeBuffer(maxsize::Int) = (x = PipeBuffer(Array(UInt8,maxsize),maxsize); x.size=0; x)

function copy(b::AbstractIOBuffer)
ret = typeof(b)(b.writable ? copy(b.data) : b.data,
b.readable, b.writable, b.seekable, b.append, b.maxsize)
ret.size = b.size
ret.ptr = b.ptr
ret
end

show(io::IO, b::IOBuffer) = print(io, "IOBuffer(data=UInt8[...], ",
show(io::IO, b::AbstractIOBuffer) = print(io, "IOBuffer(data=UInt8[...], ",
"readable=", b.readable, ", ",
"writable=", b.writable, ", ",
"seekable=", b.seekable, ", ",
Expand All @@ -39,23 +52,12 @@ show(io::IO, b::IOBuffer) = print(io, "IOBuffer(data=UInt8[...], ",
"ptr=", b.ptr, ", ",
"mark=", b.mark, ")")

# PipeBuffers behave like Unix Pipes. They are typically readable and writable, the act appendable, and not seekable.
PipeBuffer(data::Vector{UInt8}=UInt8[], maxsize::Int=typemax(Int)) = IOBuffer(data,true,true,false,true,maxsize)
PipeBuffer(maxsize::Int) = (x = PipeBuffer(Array(UInt8,maxsize),maxsize); x.size=0; x)
is_maxsize_unlimited(io::AbstractIOBuffer) = (io.maxsize == typemax(Int))
maxsize(io::AbstractIOBuffer) = io.maxsize

# IOBuffers behave like Files. They are typically readable and writable. They are seekable. (They can be appendable).
IOBuffer(data::AbstractVector{UInt8}, readable::Bool=true, writable::Bool=false, maxsize::Int=typemax(Int)) =
IOBuffer(data, readable, writable, true, false, maxsize)
IOBuffer(readable::Bool, writable::Bool) = IOBuffer(UInt8[], readable, writable)
IOBuffer() = IOBuffer(true, true)
IOBuffer(maxsize::Int) = (x=IOBuffer(Array(UInt8,maxsize), true, true, maxsize); x.size=0; x)

is_maxsize_unlimited(io::IOBuffer) = (io.maxsize == typemax(Int))
maxsize(io::IOBuffer) = io.maxsize
read!(from::AbstractIOBuffer, a::Array) = read_sub(from, a, 1, length(a))

read!(from::IOBuffer, a::Array) = read_sub(from, a, 1, length(a))

function read_sub{T}(from::IOBuffer, a::AbstractArray{T}, offs, nel)
function read_sub{T}(from::AbstractIOBuffer, a::AbstractArray{T}, offs, nel)
from.readable || throw(ArgumentError("read failed, IOBuffer is not readable"))
if offs+nel-1 > length(a) || offs < 1 || nel < 0
throw(BoundsError())
Expand All @@ -81,7 +83,7 @@ function read_sub{T}(from::IOBuffer, a::AbstractArray{T}, offs, nel)
return a
end

function read(from::IOBuffer, ::Type{UInt8})
function read(from::AbstractIOBuffer, ::Type{UInt8})
from.readable || throw(ArgumentError("read failed, IOBuffer is not readable"))
if from.ptr > from.size
throw(EOFError())
Expand All @@ -91,51 +93,51 @@ function read(from::IOBuffer, ::Type{UInt8})
return byte
end

function peek(from::IOBuffer)
function peek(from::AbstractIOBuffer)
from.readable || throw(ArgumentError("read failed, IOBuffer is not readable"))
if from.ptr > from.size
throw(EOFError())
end
return from.data[from.ptr]
end

read{T}(from::IOBuffer, ::Type{Ptr{T}}) = convert(Ptr{T}, read(from, UInt))
read{T}(from::AbstractIOBuffer, ::Type{Ptr{T}}) = convert(Ptr{T}, read(from, UInt))

isreadable(io::IOBuffer) = io.readable
iswritable(io::IOBuffer) = io.writable
isreadable(io::AbstractIOBuffer) = io.readable
iswritable(io::AbstractIOBuffer) = io.writable

# TODO: IOBuffer is not iterable, so doesn't really have a length.
# TODO: AbstractIOBuffer is not iterable, so doesn't really have a length.
# This should maybe be sizeof() instead.
#length(io::IOBuffer) = (io.seekable ? io.size : nb_available(io))
nb_available(io::IOBuffer) = io.size - io.ptr + 1
position(io::IOBuffer) = io.ptr-1
#length(io::AbstractIOBuffer) = (io.seekable ? io.size : nb_available(io))
nb_available(io::AbstractIOBuffer) = io.size - io.ptr + 1
position(io::AbstractIOBuffer) = io.ptr-1

function skip(io::IOBuffer, n::Integer)
function skip(io::AbstractIOBuffer, n::Integer)
seekto = io.ptr + n
n < 0 && return seek(io, seekto-1) # Does error checking
io.ptr = min(seekto, io.size+1)
return io
end

function seek(io::IOBuffer, n::Integer)
function seek(io::AbstractIOBuffer, n::Integer)
if !io.seekable
ismarked(io) || throw(ArgumentError("seek failed, IOBuffer is not seekable and is not marked"))
n == io.mark || throw(ArgumentError("seek failed, IOBuffer is not seekable and n != mark"))
end
# TODO: REPL.jl relies on the fact that this does not throw (by seeking past the beginning or end
# of an IOBuffer), so that would need to be fixed in order to throw an error here
# of an AbstractIOBuffer), so that would need to be fixed in order to throw an error here
#(n < 0 || n > io.size) && throw(ArgumentError("Attempted to seek outside IOBuffer boundaries."))
#io.ptr = n+1
io.ptr = max(min(n+1, io.size+1), 1)
return io
end

function seekend(io::IOBuffer)
function seekend(io::AbstractIOBuffer)
io.ptr = io.size+1
return io
end

function truncate(io::IOBuffer, n::Integer)
function truncate(io::AbstractIOBuffer, n::Integer)
io.writable || throw(ArgumentError("truncate failed, IOBuffer is not writeable"))
io.seekable || throw(ArgumentError("truncate failed, IOBuffer is not seekable"))
n < 0 && throw(ArgumentError("truncate failed, n bytes must be ≥ 0, got $n"))
Expand All @@ -150,7 +152,7 @@ function truncate(io::IOBuffer, n::Integer)
return io
end

function compact(io::IOBuffer)
function compact(io::AbstractIOBuffer)
io.writable || throw(ArgumentError("compact failed, IOBuffer is not writeable"))
io.seekable && throw(ArgumentError("compact failed, IOBuffer is seekable"))
local ptr::Int, bytes_to_move::Int
Expand All @@ -169,7 +171,7 @@ function compact(io::IOBuffer)
return io
end

function ensureroom(io::IOBuffer, nshort::Int)
function ensureroom(io::AbstractIOBuffer, nshort::Int)
io.writable || throw(ArgumentError("ensureroom failed, IOBuffer is not writeable"))
if !io.seekable
nshort >= 0 || throw(ArgumentError("ensureroom failed, requested number of bytes must be ≥ 0, got $nshort"))
Expand All @@ -194,9 +196,9 @@ function ensureroom(io::IOBuffer, nshort::Int)
return io
end

eof(io::IOBuffer) = (io.ptr-1 == io.size)
eof(io::AbstractIOBuffer) = (io.ptr-1 == io.size)

function close{T}(io::IOBuffer{T})
function close{T}(io::AbstractIOBuffer{T})
io.readable = false
io.writable = false
io.seekable = false
Expand All @@ -212,16 +214,16 @@ function close{T}(io::IOBuffer{T})
nothing
end

isopen(io::IOBuffer) = io.readable || io.writable || io.seekable || nb_available(io) > 0
isopen(io::AbstractIOBuffer) = io.readable || io.writable || io.seekable || nb_available(io) > 0

function bytestring(io::IOBuffer)
function bytestring(io::AbstractIOBuffer)
io.readable || throw(ArgumentError("bytestring read failed, IOBuffer is not readable"))
io.seekable || throw(ArgumentError("bytestring read failed, IOBuffer is not seekable"))
b = copy!(Array(UInt8, io.size), 1, io.data, 1, io.size)
return isvalid(ASCIIString, b) ? ASCIIString(b) : UTF8String(b)
end

function takebuf_array(io::IOBuffer)
function takebuf_array(io::AbstractIOBuffer)
ismarked(io) && unmark(io)
if io.seekable
nbytes = io.size
Expand All @@ -236,7 +238,7 @@ function takebuf_array(io::IOBuffer)
end
data
end
function takebuf_array(io::SimpleIOBuffer)
function takebuf_array(io::IOBuffer)
ismarked(io) && unmark(io)
if io.seekable
data = io.data
Expand All @@ -258,12 +260,12 @@ function takebuf_array(io::SimpleIOBuffer)
end
data
end
function takebuf_string(io::IOBuffer)
function takebuf_string(io::AbstractIOBuffer)
b = takebuf_array(io)
return isvalid(ASCIIString, b) ? ASCIIString(b) : UTF8String(b)
end

function write(to::IOBuffer, from::IOBuffer)
function write(to::AbstractIOBuffer, from::AbstractIOBuffer)
if to === from
from.ptr = from.size + 1
return 0
Expand All @@ -273,8 +275,8 @@ function write(to::IOBuffer, from::IOBuffer)
written
end

write(to::IOBuffer, p::Ptr, nb::Integer) = write(to, p, Int(nb))
function write(to::IOBuffer, p::Ptr, nb::Int)
write(to::AbstractIOBuffer, p::Ptr, nb::Integer) = write(to, p, Int(nb))
function write(to::AbstractIOBuffer, p::Ptr, nb::Int)
ensureroom(to, nb)
ptr = (to.append ? to.size+1 : to.ptr)
written = min(nb, length(to.data) - ptr + 1)
Expand All @@ -287,7 +289,7 @@ function write(to::IOBuffer, p::Ptr, nb::Int)
written
end

function write_sub{T}(to::IOBuffer, a::AbstractArray{T}, offs, nel)
function write_sub{T}(to::AbstractIOBuffer, a::AbstractArray{T}, offs, nel)
if offs+nel-1 > length(a) || offs < 1 || nel < 0
throw(BoundsError())
end
Expand All @@ -313,9 +315,9 @@ function write_sub{T}(to::IOBuffer, a::AbstractArray{T}, offs, nel)
written
end

write(to::IOBuffer, a::Array) = write_sub(to, a, 1, length(a))
write(to::AbstractIOBuffer, a::Array) = write_sub(to, a, 1, length(a))

function write(to::IOBuffer, a::UInt8)
function write(to::AbstractIOBuffer, a::UInt8)
ensureroom(to, 1)
ptr = (to.append ? to.size+1 : to.ptr)
if ptr > to.maxsize
Expand All @@ -328,26 +330,26 @@ function write(to::IOBuffer, a::UInt8)
sizeof(UInt8)
end

write(to::IOBuffer, p::Ptr) = write(to, convert(UInt, p))
write(to::AbstractIOBuffer, p::Ptr) = write(to, convert(UInt, p))

function readbytes!(io::IOBuffer, b::Array{UInt8}, nb=length(b))
function readbytes!(io::AbstractIOBuffer, b::Array{UInt8}, nb=length(b))
nr = min(nb, nb_available(io))
if length(b) < nr
resize!(b, nr)
end
read_sub(io, b, 1, nr)
return nr
end
readbytes(io::IOBuffer) = read!(io, Array(UInt8, nb_available(io)))
readbytes(io::IOBuffer, nb) = read!(io, Array(UInt8, min(nb, nb_available(io))))
readbytes(io::AbstractIOBuffer) = read!(io, Array(UInt8, nb_available(io)))
readbytes(io::AbstractIOBuffer, nb) = read!(io, Array(UInt8, min(nb, nb_available(io))))

function search(buf::SimpleIOBuffer, delim::UInt8)
function search(buf::IOBuffer, delim::UInt8)
p = pointer(buf.data, buf.ptr)
q = ccall(:memchr,Ptr{UInt8},(Ptr{UInt8},Int32,Csize_t),p,delim,nb_available(buf))
nb = (q == C_NULL ? 0 : q-p+1)
end

function search(buf::IOBuffer, delim::UInt8)
function search(buf::AbstractIOBuffer, delim::UInt8)
data = buf.data
for i = buf.ptr : buf.size
@inbounds b = data[i]
Expand All @@ -358,7 +360,7 @@ function search(buf::IOBuffer, delim::UInt8)
return 0
end

function readuntil(io::IOBuffer, delim::UInt8)
function readuntil(io::AbstractIOBuffer, delim::UInt8)
lb = 70
A = Array(UInt8, lb)
n = 0
Expand Down
2 changes: 1 addition & 1 deletion base/markdown/Julia/interp.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is a part of Julia. License is MIT: http://julialang.org/license

function Base.parse(stream::IOBuffer; greedy::Bool = true, raise::Bool = true)
function Base.parse(stream::IO; greedy::Bool = true, raise::Bool = true)
pos = position(stream)
ex, Δ = Base.parse(readall(stream), 1, greedy = greedy, raise = raise)
seek(stream, pos + Δ - 1)
Expand Down
4 changes: 2 additions & 2 deletions base/socket.jl
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,14 @@ type TCPSocket <: Socket
handle::Ptr{Void}
status::Int
line_buffered::Bool
buffer::SimpleIOBuffer
buffer::IOBuffer
readcb::Callback
readnotify::Condition
ccb::Callback
connectnotify::Condition
closecb::Callback
closenotify::Condition
sendbuf::Nullable{SimpleIOBuffer}
sendbuf::Nullable{IOBuffer}
lock::ReentrantLock

TCPSocket(handle) = new(
Expand Down
Loading

0 comments on commit b67b0d1

Please sign in to comment.