-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Add splitlines #20390
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 splitlines #20390
Conversation
line before it is returned. When `chomp` is false, they are returned as part of the | ||
line. | ||
""" | ||
splitlines(str::AbstractString; chomp = true) = readlines(IOBuffer(str), chomp = chomp) |
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.
Could you add a doctest here showing how it is used?
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.
LGTM, aside from a doctest example being nice (especially since folks have been working so hard at adding doctests to so many things).
Or maybe just implement |
julia> readlines("/usr/share/dict/words")
235886-element Array{String,1}:
"A"
"a"
"aa"
"aal"
"aalii"
"aam"
"Aani"
"aardvark"
"aardwolf"
⋮
"zymurgy"
"Zyrenian"
"Zyrian"
"Zyryan"
"zythem"
"Zythia"
"zythum"
"Zyzomys"
"Zyzzogeton" |
Oh, right, I forgot that you can pass the filename to |
I'm not sure calling |
The immutable LinebreakSplitter; chomp::Bool; end
function search(s::AbstractString, splitter::LinebreakSplitter, i::Integer)
newline = search(s, '\n', i)
newline < i && return newline:newline
splitter.chomp || return nextind(s, newline):newline
prev = prevind(s, newline)
return prev ≥ i && s[prev] == '\r' ? (prev:newline) : (newline:newline)
end
splitlines(s::AbstractString; chomp::Bool=true) = split(s, LinebreakSplitter(chomp)) Not only does this re-use the |
Do we actually need the splitlines(str; limit=0, keep=true) = split(str, r"\n|\r\n", limit=limit, keep=keep) would be a simple implementation that is compatible with |
I agree that |
I'm not sure how important it is in practice, but Python's version has the equivalent of the |
The |
So it looks like as of Julia 1.8.3 this is now provided by split:
so I think we can close this, but if anyone disagrees (or wants to add an alias |
Does this mean |
Ah I missed the point |
Quite conflicted for such a small change. The implementation here is already mentioned now in the docstring for
|
Adds
splitlines
as suggested in #19759