Skip to content

Commit ecccfa0

Browse files
authored
Rename split keyword from keep to keepempty (#26647)
This is a much clearer name. I've also enabled various forms of split/rsplit to be consistent with the docs.
1 parent 359f39a commit ecccfa0

File tree

6 files changed

+102
-49
lines changed

6 files changed

+102
-49
lines changed

base/deprecated.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,6 +1570,11 @@ function search(buf::IOBuffer, delim::UInt8)
15701570
return Int(q-p+1)
15711571
end
15721572

1573+
# PR #26647
1574+
# The `keep` argument in `split` and `rpslit` has been renamed to `keepempty`.
1575+
# To remove this deprecation, remove the `keep` argument from the function signatures as well as
1576+
# the internal logic that deals with the renaming. These live in base/strings/util.jl.
1577+
15731578
# END 0.7 deprecations
15741579

15751580
# BEGIN 1.0 deprecations

base/strings/util.jl

Lines changed: 68 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ function rpad(
239239
end
240240

241241
"""
242-
split(s::AbstractString; limit::Integer=0, keep::Bool=false)
243-
split(s::AbstractString, chars; limit::Integer=0, keep::Bool=true)
242+
split(s::AbstractString; limit::Integer=0, keepempty::Bool=false)
243+
split(s::AbstractString, chars; limit::Integer=0, keepempty::Bool=true)
244244
245245
Return an array of substrings by splitting the given string on occurrences of the given
246246
character delimiters, which may be specified in any of the formats allowed by
@@ -251,9 +251,11 @@ If `chars` is omitted, it defaults to the set of all space characters.
251251
252252
The optional keyword arguments are:
253253
- `limit`: the maximum size of the result. `limit=0` implies no maximum (default)
254-
- `keep`: whether empty fields should be kept in the result. Default is `false` without
254+
- `keepempty`: whether empty fields should be kept in the result. Default is `false` without
255255
a `chars` argument, `true` with a `chars` argument.
256256
257+
See also [`rsplit`](@ref).
258+
257259
# Examples
258260
```jldoctest
259261
julia> a = "Ma.rch"
@@ -267,25 +269,40 @@ julia> split(a,".")
267269
"""
268270
function split end
269271

270-
split(str::T, splitter;
271-
limit::Integer=0, keep::Bool=true) where {T<:AbstractString} =
272-
_split(str, splitter, limit, keep, T <: SubString ? T[] : SubString{T}[])
273-
split(str::T, splitter::Union{Tuple{Vararg{<:AbstractChar}},AbstractVector{<:AbstractChar},Set{<:AbstractChar}};
274-
limit::Integer=0, keep::Bool=true) where {T<:AbstractString} =
275-
_split(str, in(splitter), limit, keep, T <: SubString ? T[] : SubString{T}[])
276-
split(str::T, splitter::AbstractChar;
277-
limit::Integer=0, keep::Bool=true) where {T<:AbstractString} =
278-
_split(str, isequal(splitter), limit, keep, T <: SubString ? T[] : SubString{T}[])
279-
280-
function _split(str::AbstractString, splitter, limit::Integer, keep_empty::Bool, strs::Array)
272+
function split(str::T, splitter;
273+
limit::Integer=0, keepempty::Bool=true, keep::Union{Nothing,Bool}=nothing) where {T<:AbstractString}
274+
if keep !== nothing
275+
Base.depwarn("The `keep` keyword argument is deprecated; use `keepempty` instead", :split)
276+
keepempty = keep
277+
end
278+
_split(str, splitter, limit, keepempty, T <: SubString ? T[] : SubString{T}[])
279+
end
280+
function split(str::T, splitter::Union{Tuple{Vararg{<:AbstractChar}},AbstractVector{<:AbstractChar},Set{<:AbstractChar}};
281+
limit::Integer=0, keepempty::Bool=true, keep::Union{Nothing,Bool}=nothing) where {T<:AbstractString}
282+
if keep !== nothing
283+
Base.depwarn("The `keep` keyword argument is deprecated; use `keepempty` instead", :split)
284+
keepempty = keep
285+
end
286+
_split(str, in(splitter), limit, keepempty, T <: SubString ? T[] : SubString{T}[])
287+
end
288+
function split(str::T, splitter::AbstractChar;
289+
limit::Integer=0, keepempty::Bool=true, keep::Union{Nothing,Bool}=nothing) where {T<:AbstractString}
290+
if keep !== nothing
291+
Base.depwarn("The `keep` keyword argument is deprecated; use `keepempty` instead", :split)
292+
keepempty = keep
293+
end
294+
_split(str, isequal(splitter), limit, keepempty, T <: SubString ? T[] : SubString{T}[])
295+
end
296+
297+
function _split(str::AbstractString, splitter, limit::Integer, keepempty::Bool, strs::Array)
281298
i = 1 # firstindex(str)
282299
n = lastindex(str)
283300
r = coalesce(findfirst(splitter,str), 0)
284301
if r != 0:-1
285302
j, k = first(r), nextind(str,last(r))
286303
while 0 < j <= n && length(strs) != limit-1
287304
if i < k
288-
if keep_empty || i < j
305+
if keepempty || i < j
289306
push!(strs, SubString(str,i,prevind(str,j)))
290307
end
291308
i = k
@@ -296,17 +313,20 @@ function _split(str::AbstractString, splitter, limit::Integer, keep_empty::Bool,
296313
j, k = first(r), nextind(str,last(r))
297314
end
298315
end
299-
if keep_empty || !done(str,i)
316+
if keepempty || !done(str,i)
300317
push!(strs, SubString(str,i))
301318
end
302319
return strs
303320
end
304321

305322
# a bit oddball, but standard behavior in Perl, Ruby & Python:
306-
split(str::AbstractString) = split(str, _default_delims; limit=0, keep=false)
323+
split(str::AbstractString;
324+
limit::Integer=0, keepempty::Bool=false) =
325+
split(str, _default_delims; limit=limit, keepempty=keepempty)
307326

308327
"""
309-
rsplit(s::AbstractString, [chars]; limit::Integer=0, keep::Bool=true)
328+
rsplit(s::AbstractString; limit::Integer=0, keepempty::Bool=false)
329+
rsplit(s::AbstractString, chars; limit::Integer=0, keepempty::Bool=true)
310330
311331
Similar to [`split`](@ref), but starting from the end of the string.
312332
@@ -335,29 +355,47 @@ julia> rsplit(a,".";limit=2)
335355
"""
336356
function rsplit end
337357

338-
rsplit(str::T, splitter; limit::Integer=0, keep::Bool=true) where {T<:AbstractString} =
339-
_rsplit(str, splitter, limit, keep, T <: SubString ? T[] : SubString{T}[])
340-
rsplit(str::T, splitter::Union{Tuple{Vararg{<:AbstractChar}},AbstractVector{<:AbstractChar},Set{<:AbstractChar}};
341-
limit::Integer=0, keep::Bool=true) where {T<:AbstractString} =
342-
_rsplit(str, in(splitter), limit, keep, T <: SubString ? T[] : SubString{T}[])
343-
rsplit(str::T, splitter::AbstractChar;
344-
limit::Integer=0, keep::Bool=true) where {T<:AbstractString} =
345-
_rsplit(str, isequal(splitter), limit, keep, T <: SubString ? T[] : SubString{T}[])
358+
function rsplit(str::T, splitter;
359+
limit::Integer=0, keepempty::Bool=true, keep::Union{Nothing,Bool}=nothing) where {T<:AbstractString}
360+
if keep !== nothing
361+
Base.depwarn("The `keep` keyword argument is deprecated; use `keepempty` instead", :rsplit)
362+
keepempty = keep
363+
end
364+
_rsplit(str, splitter, limit, keepempty, T <: SubString ? T[] : SubString{T}[])
365+
end
366+
function rsplit(str::T, splitter::Union{Tuple{Vararg{<:AbstractChar}},AbstractVector{<:AbstractChar},Set{<:AbstractChar}};
367+
limit::Integer=0, keepempty::Bool=true, keep::Union{Nothing,Bool}=nothing) where {T<:AbstractString}
368+
if keep !== nothing
369+
Base.depwarn("The `keep` keyword argument is deprecated; use `keepempty` instead", :rsplit)
370+
keepempty = keep
371+
end
372+
_rsplit(str, in(splitter), limit, keepempty, T <: SubString ? T[] : SubString{T}[])
373+
end
374+
function rsplit(str::T, splitter::AbstractChar;
375+
limit::Integer=0, keepempty::Bool=true, keep::Union{Nothing,Bool}=nothing) where {T<:AbstractString}
376+
if keep !== nothing
377+
Base.depwarn("The `keep` keyword argument is deprecated; use `keepempty` instead", :rsplit)
378+
keepempty = keep
379+
end
380+
_rsplit(str, isequal(splitter), limit, keepempty, T <: SubString ? T[] : SubString{T}[])
381+
end
346382

347-
function _rsplit(str::AbstractString, splitter, limit::Integer, keep_empty::Bool, strs::Array)
383+
function _rsplit(str::AbstractString, splitter, limit::Integer, keepempty::Bool, strs::Array)
348384
n = lastindex(str)
349385
r = coalesce(findlast(splitter, str), 0)
350386
j, k = first(r), last(r)
351387
while j > 0 && k > 0 && length(strs) != limit-1
352-
(keep_empty || k < n) && pushfirst!(strs, SubString(str,nextind(str,k),n))
388+
(keepempty || k < n) && pushfirst!(strs, SubString(str,nextind(str,k),n))
353389
n = prevind(str, j)
354390
r = coalesce(findprev(splitter,str,n), 0)
355391
j, k = first(r), last(r)
356392
end
357-
(keep_empty || n > 0) && pushfirst!(strs, SubString(str,1,n))
393+
(keepempty || n > 0) && pushfirst!(strs, SubString(str,1,n))
358394
return strs
359395
end
360-
#rsplit(str::AbstractString) = rsplit(str, _default_delims, 0, false)
396+
rsplit(str::AbstractString;
397+
limit::Integer=0, keepempty::Bool=false) =
398+
rsplit(str, _default_delims; limit=limit, keepempty=keepempty)
361399

362400
_replace(io, repl, str, r, pattern) = print(io, repl)
363401
_replace(io, repl::Function, str, r, pattern) =

stdlib/Distributed/src/cluster.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,8 +1153,8 @@ end
11531153

11541154
function load_machine_file(path::AbstractString)
11551155
machines = []
1156-
for line in split(read(path, String),'\n'; keep=false)
1157-
s = split(line, '*'; keep = false)
1156+
for line in split(read(path, String),'\n'; keepempty=false)
1157+
s = split(line, '*'; keepempty=false)
11581158
map!(strip, s, s)
11591159
if length(s) > 1
11601160
cnt = all(isdigit, s[1]) ? parse(Int,s[1]) : Symbol(s[1])

stdlib/Pkg3/src/REPLMode.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ end
802802
function completions(full, index)
803803
pre = full[1:index]
804804

805-
pre_words = split(pre, ' ', keep=true)
805+
pre_words = split(pre, ' ', keepempty=true)
806806

807807
# first word should always be a command
808808
if isempty(pre_words)

stdlib/REPL/docs/src/index.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,13 @@ max(x, y) in Base at operators.jl:215
290290
max(a, b, c, xs...) in Base at operators.jl:281
291291
```
292292

293-
Keywords are also displayed in the suggested methods, see second line after `;` where `limit`
294-
and `keep` are keyword arguments:
293+
Keywords are also displayed in the suggested methods after `;`, see below line where `limit`
294+
and `keepempty` are keyword arguments:
295295

296296
```julia-repl
297297
julia> split("1 1 1", [TAB]
298-
split(str::AbstractString) in Base at strings/util.jl:302
299-
split(str::T, splitter; limit, keep) where T<:AbstractString in Base at strings/util.jl:277
298+
split(str::AbstractString; limit, keepempty) in Base at strings/util.jl:302
299+
split(str::T, splitter; limit, keepempty) where T<:AbstractString in Base at strings/util.jl:277
300300
```
301301

302302
The completion of the methods uses type inference and can therefore see if the arguments match

test/strings/util.jl

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,18 @@ end
8888
@test split("", ',') == [""]
8989
@test split(",", ',') == ["",""]
9090
@test split(",,", ',') == ["","",""]
91-
@test split("", ',' ; keep=false) == []
92-
@test split(",", ',' ; keep=false) == []
93-
@test split(",,", ','; keep=false) == []
91+
@test split("", ',' ; keepempty=false) == []
92+
@test split(",", ',' ; keepempty=false) == []
93+
@test split(",,", ','; keepempty=false) == []
9494

9595
@test split("a b c") == ["a","b","c"]
9696
@test split("a b \t c\n") == ["a","b","c"]
9797

98+
@test split("a b c"; limit=2) == ["a","b c"]
99+
@test split("a b \t c\n"; limit=3) == ["a","b","\t c\n"]
100+
@test split("a b c"; keepempty=true) == ["a","b","c"]
101+
@test split("a b \t c\n"; keepempty=true) == ["a","","b","","","c",""]
102+
98103
@test rsplit("foo,bar,baz", 'x') == ["foo,bar,baz"]
99104
@test rsplit("foo,bar,baz", ',') == ["foo","bar","baz"]
100105
@test rsplit("foo,bar,baz", ",") == ["foo","bar","baz"]
@@ -108,24 +113,29 @@ end
108113
@test rsplit(",", ',') == ["",""]
109114
@test rsplit(",,", ',') == ["","",""]
110115
@test rsplit(",,", ','; limit=2) == [",",""]
111-
@test rsplit("", ',' ; keep=false) == []
112-
@test rsplit(",", ',' ; keep=false) == []
113-
@test rsplit(",,", ','; keep=false) == []
116+
@test rsplit("", ',' ; keepempty=false) == []
117+
@test rsplit(",", ',' ; keepempty=false) == []
118+
@test rsplit(",,", ','; keepempty=false) == []
119+
120+
@test rsplit("a b c") == ["a","b","c"]
121+
@test rsplit("a b \t c\n") == ["a","b","c"]
114122

115-
#@test rsplit("a b c") == ["a","b","c"]
116-
#@test rsplit("a b \t c\n") == ["a","b","c"]
123+
@test rsplit("a b c"; limit=2) == ["a b", "c"]
124+
@test rsplit("a b \t c\n"; limit=3) == ["a ","b","c"]
125+
@test rsplit("a b c"; keepempty=true) == ["a","b","c"]
126+
@test rsplit("a b \t c\n"; keepempty=true) == ["a","","b","","","c",""]
117127

118128
let str = "a.:.ba..:..cba.:.:.dcba.:."
119129
@test split(str, ".:.") == ["a","ba.",".cba",":.dcba",""]
120-
@test split(str, ".:."; keep=false) == ["a","ba.",".cba",":.dcba"]
130+
@test split(str, ".:."; keepempty=false) == ["a","ba.",".cba",":.dcba"]
121131
@test split(str, ".:.") == ["a","ba.",".cba",":.dcba",""]
122132
@test split(str, r"\.(:\.)+") == ["a","ba.",".cba","dcba",""]
123-
@test split(str, r"\.(:\.)+"; keep=false) == ["a","ba.",".cba","dcba"]
133+
@test split(str, r"\.(:\.)+"; keepempty=false) == ["a","ba.",".cba","dcba"]
124134
@test split(str, r"\.+:\.+") == ["a","ba","cba",":.dcba",""]
125-
@test split(str, r"\.+:\.+"; keep=false) == ["a","ba","cba",":.dcba"]
135+
@test split(str, r"\.+:\.+"; keepempty=false) == ["a","ba","cba",":.dcba"]
126136

127137
@test rsplit(str, ".:.") == ["a","ba.",".cba.:","dcba",""]
128-
@test rsplit(str, ".:."; keep=false) == ["a","ba.",".cba.:","dcba"]
138+
@test rsplit(str, ".:."; keepempty=false) == ["a","ba.",".cba.:","dcba"]
129139
@test rsplit(str, ".:."; limit=2) == ["a.:.ba..:..cba.:.:.dcba", ""]
130140
@test rsplit(str, ".:."; limit=3) == ["a.:.ba..:..cba.:", "dcba", ""]
131141
@test rsplit(str, ".:."; limit=4) == ["a.:.ba.", ".cba.:", "dcba", ""]

0 commit comments

Comments
 (0)