Skip to content

Commit

Permalink
Remove need for readuntil file
Browse files Browse the repository at this point in the history
  • Loading branch information
omus committed Mar 23, 2017
1 parent 67a5ed8 commit a55cbf7
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 86 deletions.
84 changes: 84 additions & 0 deletions base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,90 @@ function readuntil{T}(s::IO, delim::T)
return out
end

mutable struct Backtrack{T, C<:AbstractArray}
target::Vector{T}
pos::Int
len::Int
cache::C
max_pos::Int
end

function Backtrack(target::AbstractString)
t = collect(target)
len = length(t)
# Setting max_pos to 1 means that backtrack[1] == 0
Backtrack(t, 0, len, spzeros(Int, len), 1)
end

function Backtrack(target::String)
t = Vector{UInt8}(target) # convert String to a utf8-byte-iterator
len = length(t)
Backtrack(t, 0, len, spzeros(Int, len), 1)
end

function backtrack(bt::Backtrack, index::Integer)
for i = (bt.max_pos + 1):index
b = bt.cache[i - 1] + 1
# Requires that i > b. We can avoid this check since we start max_pos at 1.
if bt.target[i] == bt.target[b]
bt.cache[i] = b
end
end
bt.max_pos = index
return bt.cache[index]
end

@inline function record{T}(bt::Backtrack{T}, c::T)
i = bt.pos
# Backtrack until the next target character matches what was found
while i > 0 && c != bt.target[i + 1]
i = backtrack(bt, i)
end
if c == bt.target[i + 1]
i += 1
end
bt.pos = i
return i == bt.len
end

function readuntil(io::IO, target::AbstractString)
i = start(target)
if done(target, i)
return ""
end
c, i = next(target, i)
if done(target, i) && c < Char(0x80)
return readuntil_string(s, c % UInt8)
end
backtrack = Backtrack(target)
out = IOBuffer()
while !eof(io)
c = read(io, Char)
write(out, c)
record(backtrack, c) && break
end
return String(take!(out))
end

function readuntil(s::IO, target::String)
i = start(target)
if done(target, i)
return ""
end
c, i = next(target, i)
if done(target, i) && c < Char(0x80)
return readuntil_string(s, c % UInt8)
end
backtrack = Backtrack(target)
out = Base.StringVector(0)
while !eof(s)
byte = read(s, UInt8)
push!(out, byte)
record(backtrack, byte) && break
end
return String(out)
end

"""
readchomp(x)
Expand Down
85 changes: 0 additions & 85 deletions base/readuntil.jl

This file was deleted.

1 change: 0 additions & 1 deletion base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,6 @@ import .Dates: Date, DateTime, DateFormat, @dateformat_str, now
include("sparse/sparse.jl")
importall .SparseArrays

include("readuntil.jl")
include("asyncmap.jl")

include("distributed/Distributed.jl")
Expand Down

0 comments on commit a55cbf7

Please sign in to comment.