Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exclude delimiter by default in readuntil; rename chomp option to keep #25646

Merged
merged 2 commits into from
Jan 22, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
rename chomp keyword argument to keep with inverted meaning
  • Loading branch information
JeffBezanson committed Jan 22, 2018
commit 51eea579d00f319d11faac9eaafb17c79bb3ddbd
2 changes: 1 addition & 1 deletion base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ parse_input_line(s::AbstractString) = parse_input_line(String(s))
function parse_input_line(io::IO)
s = ""
while !eof(io)
s *= readline(io, chomp=false)
s *= readline(io, keep=true)
e = parse_input_line(s)
if !(isa(e,Expr) && e.head === :incomplete)
return e
Expand Down
2 changes: 1 addition & 1 deletion base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ end

@deprecate read(cmd::AbstractCmd, stdin::Redirectable) read(pipeline(stdin, cmd))
@deprecate readstring(cmd::AbstractCmd, stdin::Redirectable) readstring(pipeline(stdin, cmd))
@deprecate eachline(cmd::AbstractCmd, stdin; chomp::Bool=true) eachline(pipeline(stdin, cmd), chomp=chomp)
@deprecate eachline(cmd::AbstractCmd, stdin; kw...) eachline(pipeline(stdin, cmd), kw...)

@deprecate showall(x) show(x)
@deprecate showall(io, x) show(IOContext(io, :limit => false), x)
Expand Down
72 changes: 45 additions & 27 deletions base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -322,14 +322,14 @@ julia> rm("my_file.txt")
readuntil(filename::AbstractString, args...) = open(io->readuntil(io, args...), filename)

"""
readline(io::IO=STDIN; chomp::Bool=true)
readline(filename::AbstractString; chomp::Bool=true)
readline(io::IO=STDIN; keep::Bool=false)
readline(filename::AbstractString; keep::Bool=false)

Read a single line of text from the given I/O stream or file (defaults to `STDIN`).
When reading from a file, the text is assumed to be encoded in UTF-8. Lines in the
input end with `'\\n'` or `"\\r\\n"` or the end of an input stream. When `chomp` is
true (as it is by default), these trailing newline characters are removed from the
line before it is returned. When `chomp` is false, they are returned as part of the
input end with `'\\n'` or `"\\r\\n"` or the end of an input stream. When `keep` is
false (as it is by default), these trailing newline characters are removed from the
line before it is returned. When `keep` is true, they are returned as part of the
line.

# Examples
Expand All @@ -342,22 +342,30 @@ julia> open("my_file.txt", "w") do io
julia> readline("my_file.txt")
"JuliaLang is a GitHub organization."

julia> readline("my_file.txt", chomp=false)
julia> readline("my_file.txt", keep=true)
"JuliaLang is a GitHub organization.\\n"

julia> rm("my_file.txt")
```
"""
function readline(filename::AbstractString; chomp::Bool=true)
function readline(filename::AbstractString; chomp=nothing, keep::Bool=false)
if chomp !== nothing
keep = !chomp
depwarn("The `chomp=$chomp` argument to `readline` is deprecated in favor of `keep=$keep`.", :readline)
end
open(filename) do f
readline(f, chomp=chomp)
readline(f, keep=keep)
end
end

function readline(s::IO=STDIN; chomp::Bool=true)
function readline(s::IO=STDIN; chomp=nothing, keep::Bool=false)
if chomp !== nothing
keep = !chomp
depwarn("The `chomp=$chomp` argument to `readline` is deprecated in favor of `keep=$keep`.", :readline)
end
line = readuntil(s, 0x0a)
i = length(line)
if !chomp || i == 0 || line[i] != 0x0a
if keep || i == 0 || line[i] != 0x0a
return String(line)
elseif i < 2 || line[i-1] != 0x0d
return String(resize!(line,i-1))
Expand All @@ -367,8 +375,8 @@ function readline(s::IO=STDIN; chomp::Bool=true)
end

"""
readlines(io::IO=STDIN; chomp::Bool=true)
readlines(filename::AbstractString; chomp::Bool=true)
readlines(io::IO=STDIN; keep::Bool=false)
readlines(filename::AbstractString; keep::Bool=false)

Read all lines of an I/O stream or a file as a vector of strings. Behavior is
equivalent to saving the result of reading [`readline`](@ref) repeatedly with the same
Expand All @@ -386,20 +394,20 @@ julia> readlines("my_file.txt")
"JuliaLang is a GitHub organization."
"It has many members."

julia> readlines("my_file.txt", chomp=false)
julia> readlines("my_file.txt", keep=true)
2-element Array{String,1}:
"JuliaLang is a GitHub organization.\\n"
"It has many members.\\n"

julia> rm("my_file.txt")
```
"""
function readlines(filename::AbstractString; chomp::Bool=true)
function readlines(filename::AbstractString; kw...)
open(filename) do f
readlines(f, chomp=chomp)
readlines(f; kw...)
end
end
readlines(s=STDIN; chomp::Bool=true) = collect(eachline(s, chomp=chomp))
readlines(s=STDIN; kw...) = collect(eachline(s; kw...))

## byte-order mark, ntoh & hton ##

Expand Down Expand Up @@ -797,20 +805,20 @@ read(s::IO, T::Type) = error("The IO stream does not support reading objects of
mutable struct EachLine
stream::IO
ondone::Function
chomp::Bool
keep::Bool

EachLine(stream::IO=STDIN; ondone::Function=()->nothing, chomp::Bool=true) =
new(stream, ondone, chomp)
EachLine(stream::IO=STDIN; ondone::Function=()->nothing, keep::Bool=false) =
new(stream, ondone, keep)
end

"""
eachline(io::IO=STDIN; chomp::Bool=true)
eachline(filename::AbstractString; chomp::Bool=true)
eachline(io::IO=STDIN; keep::Bool=false)
eachline(filename::AbstractString; keep::Bool=false)

Create an iterable `EachLine` object that will yield each line from an I/O stream
or a file. Iteration calls [`readline`](@ref) on the stream argument repeatedly with
`chomp` passed through, determining whether trailing end-of-line characters are
removed. When called with a file name, the file is opened once at the beginning of
`keep` passed through, determining whether trailing end-of-line characters are
retained. When called with a file name, the file is opened once at the beginning of
iteration and closed at the end. If iteration is interrupted, the file will be
closed when the `EachLine` object is garbage collected.

Expand All @@ -828,11 +836,21 @@ JuliaLang is a GitHub organization. It has many members.
julia> rm("my_file.txt");
```
"""
eachline(stream::IO=STDIN; chomp::Bool=true) = EachLine(stream, chomp=chomp)::EachLine
function eachline(stream::IO=STDIN; chomp=nothing, keep::Bool=false)
if chomp !== nothing
keep = !chomp
depwarn("The `chomp=$chomp` argument to `eachline` is deprecated in favor of `keep=$keep`.", :eachline)
end
EachLine(stream, keep=keep)::EachLine
end

function eachline(filename::AbstractString; chomp::Bool=true)
function eachline(filename::AbstractString; chomp=nothing, keep::Bool=false)
if chomp !== nothing
keep = !chomp
depwarn("The `chomp=$chomp` argument to `eachline` is deprecated in favor of `keep=$keep`.", :eachline)
end
s = open(filename)
EachLine(s, ondone=()->close(s), chomp=chomp)::EachLine
EachLine(s, ondone=()->close(s), keep=keep)::EachLine
end

start(itr::EachLine) = nothing
Expand All @@ -841,7 +859,7 @@ function done(itr::EachLine, ::Nothing)
itr.ondone()
true
end
next(itr::EachLine, ::Nothing) = (readline(itr.stream, chomp=itr.chomp), nothing)
next(itr::EachLine, ::Nothing) = (readline(itr.stream, keep=itr.keep), nothing)

eltype(::Type{EachLine}) = String

Expand Down
8 changes: 6 additions & 2 deletions base/iostream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,12 @@ function readuntil_string(s::IOStream, delim::UInt8)
ccall(:jl_readuntil, Ref{String}, (Ptr{Cvoid}, UInt8, UInt8, UInt8), s.ios, delim, 1, false)
end

function readline(s::IOStream; chomp::Bool=true)
ccall(:jl_readuntil, Ref{String}, (Ptr{Cvoid}, UInt8, UInt8, UInt8), s.ios, '\n', 1, chomp)
function readline(s::IOStream; chomp=nothing, keep::Bool=false)
if chomp !== nothing
keep = !chomp
depwarn("The `chomp=$chomp` argument to `readline` is deprecated in favor of `keep=$keep`.", :readline)
end
ccall(:jl_readuntil, Ref{String}, (Ptr{Cvoid}, UInt8, UInt8, UInt8), s.ios, '\n', 1, !keep)
end

function readbytes_all!(s::IOStream, b::Array{UInt8}, nb)
Expand Down
14 changes: 7 additions & 7 deletions base/markdown/Common/block.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function indentcode(stream::IO, block::MD)
buffer = IOBuffer()
while !eof(stream)
if startswith(stream, " ") || startswith(stream, "\t")
write(buffer, readline(stream, chomp=false))
write(buffer, readline(stream, keep=true))
elseif blankline(stream)
write(buffer, '\n')
else
Expand Down Expand Up @@ -139,10 +139,10 @@ function footnote(stream::IO, block::MD)
else
ref = match(regex, str).captures[1]
buffer = IOBuffer()
write(buffer, readline(stream, chomp=false))
write(buffer, readline(stream, keep=true))
while !eof(stream)
if startswith(stream, " ")
write(buffer, readline(stream, chomp=false))
write(buffer, readline(stream, keep=true))
elseif blankline(stream)
write(buffer, '\n')
else
Expand Down Expand Up @@ -174,7 +174,7 @@ function blockquote(stream::IO, block::MD)
empty = true
while eatindent(stream) && startswith(stream, '>')
startswith(stream, " ")
write(buffer, readline(stream, chomp=false))
write(buffer, readline(stream, keep=true))
empty = false
end
empty && return false
Expand Down Expand Up @@ -229,7 +229,7 @@ function admonition(stream::IO, block::MD)
buffer = IOBuffer()
while !eof(stream)
if startswith(stream, " ")
write(buffer, readline(stream, chomp=false))
write(buffer, readline(stream, keep=true))
elseif blankline(stream)
write(buffer, '\n')
else
Expand Down Expand Up @@ -305,7 +305,7 @@ function list(stream::IO, block::MD)
newline = false
if startswith(stream, " "^indent)
# Indented text that is part of the current list item.
print(buffer, readline(stream, chomp=false))
print(buffer, readline(stream, keep=true))
else
matched = startswith(stream, regex)
if isempty(matched)
Expand All @@ -316,7 +316,7 @@ function list(stream::IO, block::MD)
# Start of a new list item.
count += 1
count > 1 && pushitem!(list, buffer)
print(buffer, readline(stream, chomp=false))
print(buffer, readline(stream, keep=true))
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion base/markdown/GitHub/GitHub.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function fencedcode(stream::IO, block::MD)
seek(stream, line_start)
end
end
write(buffer, readline(stream, chomp=false))
write(buffer, readline(stream, keep=true))
end
return false
end
Expand Down
8 changes: 6 additions & 2 deletions base/process.jl
Original file line number Diff line number Diff line change
Expand Up @@ -561,13 +561,17 @@ Run a command object asynchronously, returning the resulting `Process` object.
spawn(cmds::AbstractCmd, args...; chain::Union{ProcessChain, Nothing}=nothing) =
spawn(cmds, spawn_opts_swallow(args...)...; chain=chain)

function eachline(cmd::AbstractCmd; chomp::Bool=true)
function eachline(cmd::AbstractCmd; chomp=nothing, keep::Bool=false)
if chomp !== nothing
keep = !chomp
depwarn("The `chomp=$chomp` argument to `eachline` is deprecated in favor of `keep=$keep`.", :eachline)
end
stdout = Pipe()
processes = spawn(cmd, (DevNull,stdout,STDERR))
close(stdout.in)
out = stdout.out
# implicitly close after reading lines, since we opened
return EachLine(out, chomp=chomp,
return EachLine(out, keep=keep,
ondone=()->(close(out); success(processes) || pipeline_error(processes)))::EachLine
end

Expand Down
2 changes: 1 addition & 1 deletion base/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ function prompt(message::AbstractString; default::AbstractString="", password::B
uinput = getpass(msg)
else
print(msg)
uinput = readline(chomp=false)
uinput = readline(keep=true)
isempty(uinput) && return nothing # Encountered an EOF
uinput = chomp(uinput)
end
Expand Down
2 changes: 1 addition & 1 deletion stdlib/REPL/src/LineEdit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ function refresh_multi_line(termbuf::TerminalBuffer, terminal::UnixTerminal, buf
seek(buf, 0)
moreinput = true # add a blank line if there is a trailing newline on the last line
while moreinput
l = readline(buf, chomp=false)
l = readline(buf, keep=true)
moreinput = endswith(l, "\n")
# We need to deal with on-screen characters, so use textwidth to compute occupied columns
llength = textwidth(l)
Expand Down
6 changes: 3 additions & 3 deletions stdlib/REPL/src/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ function run_frontend(repl::BasicREPL, backend::REPLBackendRef)
interrupted = false
while true
try
line *= readline(repl.terminal, chomp=false)
line *= readline(repl.terminal, keep=true)
catch e
if isa(e,InterruptException)
try # raise the debugger if present
Expand Down Expand Up @@ -378,7 +378,7 @@ An editor may have converted tabs to spaces at line """

function hist_getline(file)
while !eof(file)
line = readline(file, chomp=false)
line = readline(file, keep=true)
isempty(line) && return line
line[1] in "\r\n" || return line
end
Expand Down Expand Up @@ -1112,7 +1112,7 @@ function run_frontend(repl::StreamREPL, backend::REPLBackendRef)
if have_color
print(repl.stream, input_color(repl))
end
line = readline(repl.stream, chomp=false)
line = readline(repl.stream, keep=true)
if !isempty(line)
ast = Base.parse_input_line(line)
if have_color
Expand Down
2 changes: 1 addition & 1 deletion test/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ end
emptyfile = joinpath(dir, "empty")
touch(emptyfile)
emptyf = open(emptyfile)
@test isempty(readlines(emptyf, chomp=false))
@test isempty(readlines(emptyf, keep=true))
close(emptyf)
rm(emptyfile)

Expand Down
24 changes: 12 additions & 12 deletions test/iobuffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,29 +81,29 @@ Base.compact(io)
@test write(io,"pancakes\nwaffles\nblueberries\n") > 0
@test readlines(io) == String["pancakes", "waffles", "blueberries"]
write(io,"\n\r\n\n\r \n") > 0
@test readlines(io, chomp=false) == String["\n", "\r\n", "\n", "\r \n"]
@test readlines(io, keep=true) == String["\n", "\r\n", "\n", "\r \n"]
write(io,"\n\r\n\n\r \n") > 0
@test readlines(io, chomp=true) == String["", "", "", "\r "]
@test readlines(io, keep=false) == String["", "", "", "\r "]
@test write(io,"α\nβ\nγ\nδ") > 0
@test readlines(io, chomp=false) == String["α\n","β\n","γ\n","δ"]
@test readlines(io, keep=true) == String["α\n","β\n","γ\n","δ"]
@test write(io,"α\nβ\nγ\nδ") > 0
@test readlines(io, chomp=true) == String["α", "β", "γ", "δ"]
@test readlines(IOBuffer(""), chomp=false) == []
@test readlines(IOBuffer(""), chomp=true) == []
@test readlines(IOBuffer("first\nsecond"), chomp=false) == String["first\n", "second"]
@test readlines(IOBuffer("first\nsecond"), chomp=true) == String["first", "second"]
@test readlines(io, keep=false) == String["α", "β", "γ", "δ"]
@test readlines(IOBuffer(""), keep=true) == []
@test readlines(IOBuffer(""), keep=false) == []
@test readlines(IOBuffer("first\nsecond"), keep=true) == String["first\n", "second"]
@test readlines(IOBuffer("first\nsecond"), keep=false) == String["first", "second"]

let fname = tempname()
for dochomp in [true, false],
for dokeep in [true, false],
endline in ["\n", "\r\n"],
i in -5:5

ref = ("1"^(2^17 - i)) * endline
open(fname, "w") do io
write(io, ref)
end
x = readlines(fname, chomp = dochomp)
if dochomp
x = readlines(fname, keep = dokeep)
if !dokeep
ref = chomp(ref)
end
@test ref == x[1]
Expand Down Expand Up @@ -170,7 +170,7 @@ let io = IOBuffer("abcdef"),
@test eof(io)
end

@test isempty(readlines(IOBuffer(), chomp=false))
@test isempty(readlines(IOBuffer(), keep=true))

# issue #8193
let io=IOBuffer("asdf")
Expand Down
4 changes: 2 additions & 2 deletions test/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ end
let b = IOBuffer("foo\n")
@test collect(EachLine(b)) == ["foo"]
seek(b, 0)
@test collect(EachLine(b, chomp=false)) == ["foo\n"]
@test collect(EachLine(b, keep=true)) == ["foo\n"]
seek(b, 0)
@test collect(EachLine(b, ondone=()->0)) == ["foo"]
seek(b, 0)
@test collect(EachLine(b, chomp=false, ondone=()->0)) == ["foo\n"]
@test collect(EachLine(b, keep=true, ondone=()->0)) == ["foo\n"]
end

# enumerate (issue #6284)
Expand Down
Loading