Skip to content

Commit

Permalink
Add keep argument to readuntil, readlines, readline and eachline
Browse files Browse the repository at this point in the history
  • Loading branch information
nalimilan committed Jun 17, 2018
1 parent b1e6668 commit f64c2b3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
33 changes: 20 additions & 13 deletions src/StringEncodings.jl
Expand Up @@ -413,38 +413,45 @@ Base.read(s::IO, ::Type{String}, enc::Encoding) = read(StringDecoder(s, enc), St
Base.read(filename::AbstractString, ::Type{String}, enc::Encoding) = open(io->read(io, String, enc), filename)

"""
readline(stream::IO, enc::Encoding)
readline(filename::AbstractString, enc::Encoding)
readline(stream::IO, enc::Encoding; keep::Bool=false)
readline(filename::AbstractString, enc::Encoding; keep::Bool=false)
Methods to read text in character encoding `enc`.
"""
readline(s::IO, enc::Encoding) = readline(StringDecoder(s, enc))
readline(filename::AbstractString, enc::Encoding) = open(io->readline(io, enc), filename)
readline(s::IO, enc::Encoding; keep::Bool=false) =
readline(StringDecoder(s, enc), keep=keep)
readline(filename::AbstractString, enc::Encoding; keep::Bool=false) =
open(io->readline(io, enc, keep=keep), filename)

"""
readlines(stream::IO, enc::Encoding)
readlines(filename::AbstractString, enc::Encoding)
readlines(stream::IO, enc::Encoding; keep::Bool=false)
readlines(filename::AbstractString, enc::Encoding; keep::Bool=false)
Methods to read text in character encoding `enc`.
"""
readlines(s::IO, enc::Encoding) = readlines(StringDecoder(s, enc))
readlines(filename::AbstractString, enc::Encoding) = open(io->readlines(io, enc), filename)
readlines(s::IO, enc::Encoding; keep::Bool=false) =
readlines(StringDecoder(s, enc), keep=keep)
readlines(filename::AbstractString, enc::Encoding; keep::Bool=false) =
open(io->readlines(io, enc, keep=keep), filename)

"""
readuntil(stream::IO, enc::Encoding, delim)
readuntil(filename::AbstractString, enc::Encoding, delim)
readuntil(stream::IO, enc::Encoding, delim; keep::Bool=false)
readuntil(filename::AbstractString, enc::Encoding, delim; keep::Bool=false)
Methods to read text in character encoding `enc`.
"""
readuntil(s::IO, enc::Encoding, delim) = readuntil(StringDecoder(s, enc), delim)
readuntil(filename::AbstractString, enc::Encoding, delim) = open(io->readuntil(io, enc, delim), filename)
readuntil(s::IO, enc::Encoding, delim; keep::Bool=false) =
readuntil(StringDecoder(s, enc), delim, keep=keep)
readuntil(filename::AbstractString, enc::Encoding, delim; keep::Bool=false) =
open(io->readuntil(io, enc, delim, keep=keep), filename)

"""
eachline(stream::IO, enc::Encoding; keep=false)
eachline(filename::AbstractString, enc::Encoding; keep=false)
Methods to read text in character encoding `enc`. Decoding is performed on the fly.
"""
eachline(s::IO, enc::Encoding; keep=false) = eachline(StringDecoder(s, enc); keep=false)
eachline(s::IO, enc::Encoding; keep=false) = eachline(StringDecoder(s, enc), keep=keep)
function eachline(filename::AbstractString, enc::Encoding; keep=false)
s = open(filename, enc)
Base.EachLine(s, ondone=()->close(s), keep=keep)
Expand Down
28 changes: 14 additions & 14 deletions test/runtests.jl
@@ -1,4 +1,4 @@
using Base.Test
using Test
using StringEncodings

# Test round-trip to Unicode formats
Expand Down Expand Up @@ -147,27 +147,27 @@ mktemp() do path, io

@test readuntil(path, enc"ISO-2022-JP", '\0') == "a string "
@test open(io->readuntil(io, enc"ISO-2022-JP", '\0'), path) == "a string "
@test open(io->readuntil(io, enc"ISO-2022-JP", '\0', keep=true), path) == "a string \0"
@test readuntil(path, enc"ISO-2022-JP", "チャ") == "a string \0"
@test open(io->readuntil(io, enc"ISO-2022-JP", "チャ"), path) == "a string \0"
@test open(io->readuntil(io, enc"ISO-2022-JP", "チャ", keep=true), path) == "a string \0チャ"

@test readline(path, enc"ISO-2022-JP") == split(s, '\n')[1]
@test readline(path, enc"ISO-2022-JP", keep=true) == split(s, '\n', )[1] * '\n'
@test open(readline, path, enc"ISO-2022-JP") == split(s, '\n')[1]

if VERSION >= v"0.6.0-dev.2467"
@test readline(path, enc"ISO-2022-JP") == split(s, '\n')[1]
@test open(readline, path, enc"ISO-2022-JP") == split(s, '\n')[1]
else
@test readline(path, enc"ISO-2022-JP") == string(split(s, '\n')[1], '\n')
@test open(readline, path, enc"ISO-2022-JP") == string(split(s, '\n')[1], '\n')
end
a = readlines(path, enc"ISO-2022-JP")
b = open(readlines, path, enc"ISO-2022-JP")

c = collect(eachline(path, enc"ISO-2022-JP"))
d = open(io->collect(eachline(io, enc"ISO-2022-JP")), path)
@test a[1] == b[1] == c[1] == d[1] == split(s, '\n')[1]
@test a[2] == b[2] == c[2] == d[2] == split(s, '\n')[2]

if VERSION >= v"0.6.0-dev.2467"
@test a[1] == b[1] == c[1] == d[1] == split(s, '\n')[1]
else
@test a[1] == b[1] == c[1] == d[1] == string(split(s, '\n')[1], '\n')
end
a = readlines(path, enc"ISO-2022-JP", keep=true)
b = open(io->readlines(io, keep=true), path, enc"ISO-2022-JP")
c = collect(eachline(path, enc"ISO-2022-JP", keep=true))
d = open(io->collect(eachline(io, enc"ISO-2022-JP", keep=true)), path)
@test a[1] == b[1] == c[1] == d[1] == split(s, '\n')[1] * '\n'
@test a[2] == b[2] == c[2] == d[2] == split(s, '\n')[2]

# Test alternative syntaxes for open()
Expand Down

0 comments on commit f64c2b3

Please sign in to comment.