From 6d7a3cb4919fc7db96d58653f4fd78db00327c6e Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Fri, 9 Mar 2018 11:02:23 +0100 Subject: [PATCH] fix for find(first|last|next|prev) returning nothing rather than 0:-1 --- README.md | 4 +++- src/Compat.jl | 1 + test/runtests.jl | 25 +++++++++++++------------ 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 7406aa4c3..82d534b98 100644 --- a/README.md +++ b/README.md @@ -353,7 +353,8 @@ Currently, the `@compat` macro supports the following syntaxes: sometimes combined with `equalto` or `occursin` ([#24673]). * `Compat.findfirst`, `Compat.findnext`, `Compat.findlast` and `Compat.findprev`, - return `nothing` when no match is found (rather than `0`) as on Julia 0.7 ([#24673]). + return `nothing` when no match is found (rather than `0` or `0:-1`) + as on Julia 0.7 ([#24673], [#26149]). * `findin(a, b)` is now `findall(occursin(b), a)` ([#24673]). @@ -586,4 +587,5 @@ includes this fix. Find the minimum version from there. [#25990]: https://github.com/JuliaLang/julia/issues/25990 [#26069]: https://github.com/JuliaLang/julia/issues/26069 [#26089]: https://github.com/JuliaLang/julia/issues/26089 +[#26149]: https://github.com/JuliaLang/julia/issues/26149 [#26156]: https://github.com/JuliaLang/julia/issues/26156 diff --git a/src/Compat.jl b/src/Compat.jl index 965669013..2d72a2554 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -1498,6 +1498,7 @@ else export occursin zero2nothing(x::Integer) = x == 0 ? nothing : x + zero2nothing(x::AbstractUnitRange{<:Integer}) = isempty(x) ? nothing : x zero2nothing(x) = x findnext(xs...) = zero2nothing(Base.findnext(xs...)) diff --git a/test/runtests.jl b/test/runtests.jl index 3844f2200..35809e131 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1378,18 +1378,19 @@ for (f1, f2, i) in ((Compat.findfirst, Compat.findnext, 1), @test f2(occursin(chars), "bx", i) == f1(occursin(chars), "bx") == nothing end end -for (f1, f2, i) in ((findfirst, findnext, 1), - (findlast, findprev, 2), - (Compat.findfirst, Compat.findnext, 1), - (Compat.findlast, Compat.findprev, 2)) - @test f2("a", "ba", i) == f1("a", "ba") == 2:2 - @test f2("z", "ba", i) == f1("z", "ba") == 0:-1 -end -for (f1, f2, i) in ((findfirst, findnext, 1), - (Compat.findfirst, Compat.findnext, 1)) - @test f2(r"a", "ba", 1) == f1(r"a", "ba") == 2:2 - @test f2(r"z", "ba", 1) == f1(r"z", "ba") == 0:-1 -end +@test findnext("a", "ba", 1) == findfirst("a", "ba") == 2:2 +@test findnext("z", "ba", 1) == findfirst("z", "ba") == (VERSION < v"0.7.0-DEV.4480" ? (0:-1) : nothing) +@test findprev("a", "ba", 2) == findlast("a", "ba") == 2:2 +@test findprev("z", "ba", 2) == findlast("z", "ba") == (VERSION < v"0.7.0-DEV.4480" ? (0:-1) : nothing) +@test Compat.findnext("a", "ba", 1) == Compat.findfirst("a", "ba") == 2:2 +@test Compat.findnext("z", "ba", 1) == Compat.findfirst("z", "ba") == nothing +@test Compat.findprev("a", "ba", 2) == Compat.findlast("a", "ba") == 2:2 +@test Compat.findprev("z", "ba", 2) == Compat.findlast("z", "ba") == nothing + +@test findnext(r"a", "ba", 1) == findfirst(r"a", "ba") == 2:2 +@test findnext(r"z", "ba", 1) == findfirst(r"z", "ba") == (VERSION < v"0.7.0-DEV.4480" ? (0:-1) : nothing) +@test Compat.findnext(r"a", "ba", 1) == Compat.findfirst(r"a", "ba") == 2:2 +@test Compat.findnext(r"z", "ba", 1) == Compat.findfirst(r"z", "ba") == nothing @test Compat.findfirst(equalto(UInt8(0)), IOBuffer(UInt8[1, 0])) == 2 @test Compat.findfirst(equalto(UInt8(9)), IOBuffer(UInt8[1, 0])) == nothing