Skip to content

Commit 6d7a3cb

Browse files
committed
fix for find(first|last|next|prev) returning nothing rather than 0:-1
1 parent fd3b92a commit 6d7a3cb

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,8 @@ Currently, the `@compat` macro supports the following syntaxes:
353353
sometimes combined with `equalto` or `occursin` ([#24673]).
354354

355355
* `Compat.findfirst`, `Compat.findnext`, `Compat.findlast` and `Compat.findprev`,
356-
return `nothing` when no match is found (rather than `0`) as on Julia 0.7 ([#24673]).
356+
return `nothing` when no match is found (rather than `0` or `0:-1`)
357+
as on Julia 0.7 ([#24673], [#26149]).
357358

358359
* `findin(a, b)` is now `findall(occursin(b), a)` ([#24673]).
359360

@@ -586,4 +587,5 @@ includes this fix. Find the minimum version from there.
586587
[#25990]: https://github.com/JuliaLang/julia/issues/25990
587588
[#26069]: https://github.com/JuliaLang/julia/issues/26069
588589
[#26089]: https://github.com/JuliaLang/julia/issues/26089
590+
[#26149]: https://github.com/JuliaLang/julia/issues/26149
589591
[#26156]: https://github.com/JuliaLang/julia/issues/26156

src/Compat.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,6 +1498,7 @@ else
14981498
export occursin
14991499

15001500
zero2nothing(x::Integer) = x == 0 ? nothing : x
1501+
zero2nothing(x::AbstractUnitRange{<:Integer}) = isempty(x) ? nothing : x
15011502
zero2nothing(x) = x
15021503

15031504
findnext(xs...) = zero2nothing(Base.findnext(xs...))

test/runtests.jl

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,18 +1378,19 @@ for (f1, f2, i) in ((Compat.findfirst, Compat.findnext, 1),
13781378
@test f2(occursin(chars), "bx", i) == f1(occursin(chars), "bx") == nothing
13791379
end
13801380
end
1381-
for (f1, f2, i) in ((findfirst, findnext, 1),
1382-
(findlast, findprev, 2),
1383-
(Compat.findfirst, Compat.findnext, 1),
1384-
(Compat.findlast, Compat.findprev, 2))
1385-
@test f2("a", "ba", i) == f1("a", "ba") == 2:2
1386-
@test f2("z", "ba", i) == f1("z", "ba") == 0:-1
1387-
end
1388-
for (f1, f2, i) in ((findfirst, findnext, 1),
1389-
(Compat.findfirst, Compat.findnext, 1))
1390-
@test f2(r"a", "ba", 1) == f1(r"a", "ba") == 2:2
1391-
@test f2(r"z", "ba", 1) == f1(r"z", "ba") == 0:-1
1392-
end
1381+
@test findnext("a", "ba", 1) == findfirst("a", "ba") == 2:2
1382+
@test findnext("z", "ba", 1) == findfirst("z", "ba") == (VERSION < v"0.7.0-DEV.4480" ? (0:-1) : nothing)
1383+
@test findprev("a", "ba", 2) == findlast("a", "ba") == 2:2
1384+
@test findprev("z", "ba", 2) == findlast("z", "ba") == (VERSION < v"0.7.0-DEV.4480" ? (0:-1) : nothing)
1385+
@test Compat.findnext("a", "ba", 1) == Compat.findfirst("a", "ba") == 2:2
1386+
@test Compat.findnext("z", "ba", 1) == Compat.findfirst("z", "ba") == nothing
1387+
@test Compat.findprev("a", "ba", 2) == Compat.findlast("a", "ba") == 2:2
1388+
@test Compat.findprev("z", "ba", 2) == Compat.findlast("z", "ba") == nothing
1389+
1390+
@test findnext(r"a", "ba", 1) == findfirst(r"a", "ba") == 2:2
1391+
@test findnext(r"z", "ba", 1) == findfirst(r"z", "ba") == (VERSION < v"0.7.0-DEV.4480" ? (0:-1) : nothing)
1392+
@test Compat.findnext(r"a", "ba", 1) == Compat.findfirst(r"a", "ba") == 2:2
1393+
@test Compat.findnext(r"z", "ba", 1) == Compat.findfirst(r"z", "ba") == nothing
13931394

13941395
@test Compat.findfirst(equalto(UInt8(0)), IOBuffer(UInt8[1, 0])) == 2
13951396
@test Compat.findfirst(equalto(UInt8(9)), IOBuffer(UInt8[1, 0])) == nothing

0 commit comments

Comments
 (0)