-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Add chomp option to readline(s) #19944
Changes from 13 commits
dac3fc4
cdf10df
4620721
3590288
2d07745
98fa88c
88f32eb
2214b93
c6673dc
cd48d62
b8ab89a
aafa71a
b5d9a98
1c349bb
354ada4
ff5fde8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,9 @@ Breaking changes | |
|
||
This section lists changes that do not have deprecation warnings. | ||
|
||
* `readline`, `readlines` and `eachline` return lines without line ends by default. | ||
You can use `readline(s, false`) to get the old behavior and include EOL character(s). ([#19944]). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Misplaced closing backtick: should be after the ) |
||
|
||
* `String`s no longer have a `.data` field (as part of a significant performance | ||
improvement). Use `Vector{UInt8}(str)` to access a string as a byte array. | ||
However, allocating the `Vector` object has overhead. You can also use | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,24 +167,53 @@ The text is assumed to be encoded in UTF-8. | |
readuntil(filename::AbstractString, args...) = open(io->readuntil(io, args...), filename) | ||
|
||
""" | ||
readline(stream::IO=STDIN) | ||
readline(filename::AbstractString) | ||
readline() | ||
readline(stream, chomp::Bool=true) | ||
readline(filename::AbstractString, chomp::Bool=true) | ||
|
||
Read a single line of text, including a trailing newline character (if one is reached before | ||
the end of the input), 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. | ||
Read a single line of text from the given I/O stream or file (defaults to `STDIN`). | ||
Lines in the input can end in `'\\n'` or `"\\r\\n"`. When reading from a file, the text is | ||
assumed to be encoded in UTF-8. | ||
|
||
If `chomp=false` trailing newline character(s) will be included in the output | ||
(if reached before the end of the input); otherwise newline characters(s) | ||
are stripped from result. | ||
""" | ||
readline(filename::AbstractString) = open(readline, filename) | ||
function readline(filename::AbstractString, chomp::Bool=true) | ||
open(filename) do f | ||
readline(f, chomp) | ||
end | ||
end | ||
readline() = readline(STDIN, false) | ||
|
||
function readline(s::IO, chomp::Bool=true) | ||
line = readuntil(s, 0x0a) | ||
i = length(line) | ||
if !chomp || i == 0 || line[i] != 0x0a | ||
return String(line) | ||
elseif i < 2 || line[i-1] != 0x0d | ||
return String(resize!(line,i-1)) | ||
else | ||
return String(resize!(line,i-2)) | ||
end | ||
end | ||
|
||
""" | ||
readlines(stream::IO) | ||
readlines(filename::AbstractString) | ||
readlines(stream::IO, chomp::Bool=true) | ||
readlines(filename::AbstractString, chomp::Bool=true) | ||
|
||
Read all lines of an I/O stream or a file as a vector of strings. | ||
Lines in the input can end in `'\\n'` or `"\\r\\n"`. | ||
The text is assumed to be encoded in UTF-8. | ||
""" | ||
readlines(filename::AbstractString) = open(readlines, filename) | ||
|
||
If `chomp=false` trailing newline character(s) will be included in the output; | ||
otherwise newline characters(s) are stripped from result. | ||
""" | ||
function readlines(filename::AbstractString, chomp::Bool=true) | ||
open(filename) do f | ||
readlines(f, chomp) | ||
end | ||
end | ||
|
||
## byte-order mark, ntoh & hton ## | ||
|
||
|
@@ -447,9 +476,6 @@ function readuntil(s::IO, t::AbstractString) | |
return String(take!(out)) | ||
end | ||
|
||
readline() = readline(STDIN) | ||
readline(s::IO) = readuntil(s, '\n') | ||
|
||
""" | ||
readchomp(x) | ||
|
||
|
@@ -512,22 +538,28 @@ readstring(filename::AbstractString) = open(readstring, filename) | |
|
||
type EachLine | ||
stream::IO | ||
chomp::Bool | ||
ondone::Function | ||
EachLine(stream) = EachLine(stream, ()->nothing) | ||
EachLine(stream, ondone) = new(stream, ondone) | ||
EachLine(stream, chomp) = EachLine(stream, chomp, ()->nothing) | ||
EachLine(stream, chomp, ondone) = new(stream, chomp, ondone) | ||
end | ||
|
||
""" | ||
eachline(stream::IO) | ||
eachline(filename::AbstractString) | ||
eachline(stream::IO, chomp::Bool=true) | ||
eachline(filename::AbstractString, chomp::Bool=true) | ||
|
||
Create an iterable object that will yield each line from an I/O stream or a file. | ||
Lines in the input can end in `'\\n'` or `"\\r\\n"`. | ||
The text is assumed to be encoded in UTF-8. | ||
|
||
If `chomp=false` trailing newline character(s) will be included in the output; | ||
otherwise newline characters(s) are stripped from result. | ||
""" | ||
eachline(stream::IO) = EachLine(stream) | ||
function eachline(filename::AbstractString) | ||
eachline(stream::IO, chomp::Bool=true) = EachLine(stream, chomp) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A single new line is probably enough, isn't it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, thanks, fixed |
||
function eachline(filename::AbstractString, chomp::Bool=true) | ||
s = open(filename) | ||
EachLine(s, ()->close(s)) | ||
EachLine(s, chomp, ()->close(s)) | ||
end | ||
|
||
start(itr::EachLine) = nothing | ||
|
@@ -538,10 +570,11 @@ function done(itr::EachLine, nada) | |
itr.ondone() | ||
true | ||
end | ||
next(itr::EachLine, nada) = (readline(itr.stream), nothing) | ||
|
||
next(itr::EachLine, nada) = (readline(itr.stream, itr.chomp), nothing) | ||
eltype(::Type{EachLine}) = String | ||
|
||
readlines(s=STDIN) = collect(eachline(s)) | ||
readlines(s::IO, chomp::Bool=true) = collect(eachline(s, chomp)) | ||
|
||
iteratorsize(::Type{EachLine}) = SizeUnknown() | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,7 @@ function prompt(msg::AbstractString; default::AbstractString="", password::Bool= | |
Base.getpass(msg) | ||
else | ||
print(msg) | ||
chomp(readline(STDIN)) | ||
readline(STDIN, true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe leave off |
||
end | ||
isempty(uinput) ? default : uinput | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ end | |
|
||
function parserow(stream::IO) | ||
withstream(stream) do | ||
line = readline(stream) |> chomp | ||
line = readline(stream, true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe leave off |
||
row = split(line, r"(?<!\\)\|") | ||
length(row) == 1 && return | ||
row[1] == "" && shift!(row) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,7 +46,7 @@ function linecontains(io::IO, chars; allow_whitespace = true, | |
eat = true, | ||
allowempty = false) | ||
start = position(io) | ||
l = readline(io) |> chomp | ||
l = readline(io, true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe leave off |
||
length(l) == 0 && return allowempty | ||
|
||
result = allowempty | ||
|
@@ -99,7 +99,7 @@ function startswith(stream::IO, r::Regex; eat = true, padding = false) | |
@assert Base.startswith(r.pattern, "^") | ||
start = position(stream) | ||
padding && skipwhitespace(stream) | ||
line = chomp(readline(stream)) | ||
line = readline(stream, true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe leave off |
||
seek(stream, start) | ||
m = match(r, line) | ||
m === nothing && return "" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,7 +67,7 @@ end | |
|
||
function getmetabranch() | ||
try | ||
chomp(readline(joinpath(path(),"META_BRANCH"))) | ||
readline(joinpath(path(),"META_BRANCH"), true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe leave off |
||
catch err | ||
META_BRANCH | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"line endings" instead of "line ends" might be clearer