Skip to content

Commit

Permalink
replace memio with IOBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Jun 18, 2013
1 parent 8a5af7e commit 9c7a8b0
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 26 deletions.
2 changes: 2 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ export PipeString
@deprecate >(a::AbstractCmd,b::Redirectable) (a|>b)
@deprecate >(a::AbstractCmd,b::ASCIIString) (a|>b)

@deprecate memio(args...) IOBuffer()

# note removed macros: str, B_str, I_str, E_str, L_str, L_mstr, I_mstr, E_mstr

# renamings
Expand Down
1 change: 0 additions & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,6 @@ export
hton,
ltoh,
ntoh,
memio,
mmap,
mmap_array,
mmap_bitarray,
Expand Down
10 changes: 5 additions & 5 deletions base/grisu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function grisu(x::Float64, mode::Integer, ndigits::Integer)
if !isfinite(x); error("non-finite value: $x"); end
if ndigits < 0; error("negative digits requested"); end
@grisu_ccall x mode ndigits
NEG[1], DIGITS[1:LEN[1]], POINT[1]
NEG[1], DIGITS[1:LEN[1]], int(POINT[1])
end

grisu(x::Float64) = grisu(x, SHORTEST, int32(0))
Expand All @@ -56,8 +56,8 @@ function _show(io::IO, x::FloatingPoint, mode::Int32, n::Int)
@grisu_ccall x mode n
pdigits = pointer(DIGITS)
neg = NEG[1]
len = LEN[1]
pt = POINT[1]
len = int(LEN[1])
pt = int(POINT[1])
if mode == PRECISION
while len > 1 && DIGITS[len] == '0'
len -= 1
Expand Down Expand Up @@ -120,8 +120,8 @@ function _print_shortest(io::IO, x::FloatingPoint, dot::Bool, mode::Int32)
if isinf(x); return write(io, isa(x,Float32) ? "Inf32" : "Inf"); end
@grisu_ccall x mode 0
pdigits = pointer(DIGITS)
len = LEN[1]
pt = POINT[1]
len = int(LEN[1])
pt = int(POINT[1])
e = pt-len
k = -9<=e<=9 ? 1 : 2
if -pt > k+1 || e+dot > k+1
Expand Down
39 changes: 24 additions & 15 deletions base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function readuntil(s::IO, delim::Char)
enc = byte_string_classify(data)
return (enc==1) ? ASCIIString(data) : UTF8String(data)
end
out = memio()
out = IOBuffer()
while !eof(s)
c = read(s, Char)
write(out, c)
Expand All @@ -161,7 +161,7 @@ end
readline(s::IO) = readuntil(s, '\n')

function readall(s::IO)
out = memio()
out = IOBuffer()
while !eof(s)
a = read(s, Uint8)
write(out, a)
Expand Down Expand Up @@ -300,14 +300,6 @@ function open(f::Function, args...)
end
end

function memio(x::Integer, finalize::Bool)
s = IOStream("<memio>", finalize)
ccall(:ios_mem, Ptr{Void}, (Ptr{Uint8}, Uint), s.ios, x)
return s
end
memio(x::Integer) = memio(x, true)
memio() = memio(0, true)

## low-level calls ##

write(s::IOStream, b::Uint8) = int(ccall(:jl_putc, Int32, (Uint8, Ptr{Void}), b, s.ios))
Expand Down Expand Up @@ -381,15 +373,16 @@ function takebuf_raw(s::IOStream)
end

function sprint(size::Integer, f::Function, args...)
s = memio(size, false)
s = IOBuffer(Array(Uint8,size), true, true)
truncate(s,0)
f(s, args...)
takebuf_string(s)
end

sprint(f::Function, args...) = sprint(0, f, args...)

function repr(x)
s = memio(0, false)
s = IOBuffer()
show(s, x)
takebuf_string(s)
end
Expand All @@ -400,10 +393,26 @@ function readuntil(s::IOStream, delim::Uint8)
ccall(:jl_readuntil, Array{Uint8,1}, (Ptr{Void}, Uint8), s.ios, delim)
end

function readbytes(s::IOStream)
n = 65536
b = Array(Uint8, n)
p = 1
while true
nr = int(ccall(:ios_readall, Uint,
(Ptr{Void}, Ptr{Void}, Uint), s.ios, pointer(b,p), n))
if eof(s)
resize!(b, p+nr-1)
break
end
p += nr
resize!(b, p+n-1)
end
b
end

function readall(s::IOStream)
dest = memio()
ccall(:ios_copyall, Uint, (Ptr{Void}, Ptr{Void}), dest.ios, s.ios)
takebuf_string(dest)
b = readbytes(s)
return is_valid_ascii(b) ? ASCIIString(b) : UTF8String(b)
end
readall(filename::String) = open(readall, filename)

Expand Down
12 changes: 8 additions & 4 deletions base/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ next(s::String, i::Integer) = next(s,int(i))
## conversion of general objects to strings ##

function print_to_string(xs...)
s = memio(isa(xs[1],String) ? endof(xs[1]) : 0, false)
s = IOBuffer(Array(Uint8,isa(xs[1],String) ? endof(xs[1]) : 0), true, true)
truncate(s,0)
for x in xs
print(s, x)
end
Expand Down Expand Up @@ -519,7 +520,8 @@ lcfirst(s::String) = islower(s[1]) ? s : string(lowercase(s[1]),s[nextind(s,1):e
## string map, filter, has ##

function map(f::Function, s::String)
out = memio(endof(s))
out = IOBuffer(Array(Uint8,endof(s)),true,true)
truncate(out,0)
for c in s
c2 = f(c)
if !isa(c2,Char)
Expand All @@ -531,7 +533,8 @@ function map(f::Function, s::String)
end

function filter(f::Function, s::String)
out = memio(endof(s))
out = IOBuffer(Array(Uint8,endof(s)),true,true)
truncate(out,0)
for c in s
if f(c)
write(out, c)
Expand Down Expand Up @@ -699,7 +702,8 @@ function indentation(s::String)
end

function unindent(s::String, indent::Int)
buf = memio(endof(s), false)
buf = IOBuffer(Array(Uint8,endof(s)), true, true)
truncate(buf,0)
a = i = start(s)
cutting = false
cut = 0
Expand Down
2 changes: 1 addition & 1 deletion test/remote.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# issue #1770
let
a = ['T', 'e', 's', 't']
f = memio()
f = IOBuffer()
serialize(f, a)
seek(f, 0)
@test deserialize(f) == a
Expand Down

0 comments on commit 9c7a8b0

Please sign in to comment.