-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rename IOBuffer/SimpleIOBuffer to AbstractIOBuffer/IOBuffer, to minim…
…ize unnecessary API changes
- Loading branch information
Showing
9 changed files
with
84 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
StefanKarpinski
Member
|
||
data::T # T should support: getindex, setindex!, length, copy!, resize!, and T() | ||
readable::Bool | ||
writable::Bool | ||
|
@@ -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, ", ", | ||
|
@@ -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()) | ||
|
@@ -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()) | ||
|
@@ -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")) | ||
|
@@ -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 | ||
|
@@ -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")) | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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) | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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] | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
I have to say, I find it very confusing that
AbstractIOBuffer{T}
is a concrete type. Couldn't we have this be anabstract
type?