diff --git a/README.md b/README.md index 71a6724d3..ee611674e 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,8 @@ Please check the list below for the specific syntax you need. ## Supported features +* Search a character in a string with `findfirst`, `findnext`, `findlast` and `findprev` ([#31664]). (since Compat 3.12.0) + * `∘(f) = f` is defined ([#34251]). (since Compat 3.11.0) * `union` supports `Base.OneTo` ([#35577]). (since Compat 3.11.0) @@ -140,6 +142,7 @@ Note that you should specify the correct minimum version for `Compat` in the [#29442]: https://github.com/JuliaLang/julia/issues/29442 [#29674]: https://github.com/JuliaLang/julia/issues/29674 [#29749]: https://github.com/JuliaLang/julia/issues/29749 +[#31664]: https://github.com/JuliaLang/julia/issues/31664 [#32628]: https://github.com/JuliaLang/julia/issues/32628 [#32739]: https://github.com/JuliaLang/julia/issues/32739 [#32753]: https://github.com/JuliaLang/julia/issues/32753 diff --git a/src/Compat.jl b/src/Compat.jl index 9f638652c..76596c381 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -65,6 +65,15 @@ if VERSION < v"1.2.0-DEV.272" export hasproperty end +if VERSION < v"1.3.0-DEV.349" + Base.findfirst(ch::AbstractChar, string::AbstractString) = findfirst(==(ch), string) + Base.findnext(ch::AbstractChar, string::AbstractString, ind::Integer) = + findnext(==(ch), string, ind) + Base.findlast(ch::AbstractChar, string::AbstractString) = findlast(==(ch), string) + Base.findprev(ch::AbstractChar, string::AbstractString, ind::Integer) = + findprev(==(ch), string, ind) +end + # https://github.com/JuliaLang/julia/pull/29259 if VERSION < v"1.1.0-DEV.594" Base.merge(a::NamedTuple, b::NamedTuple, cs::NamedTuple...) = merge(merge(a, b), cs...) diff --git a/test/runtests.jl b/test/runtests.jl index c99392b48..bfbb37079 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -105,6 +105,50 @@ end @test_throws DivideError mod(3, 1:0) end +# https://github.com/JuliaLang/julia/pull/31664 +@testset "search character in strings" begin + let astr = "Hello, world.\n", + u8str = "∀ ε > 0, ∃ δ > 0: |x-y| < δ ⇒ |f(x)-f(y)| < ε" + @test_throws BoundsError findnext('z', astr, 0) + @test_throws BoundsError findnext('∀', astr, 0) + @test findfirst('x', astr) == nothing + @test findfirst('\0', astr) == nothing + @test findfirst('\u80', astr) == nothing + @test findfirst('∀', astr) == nothing + @test findfirst('∀', u8str) == 1 + @test findfirst('ε', u8str) == 5 + @test findfirst('H', astr) == 1 + @test findfirst('l', astr) == 3 + @test findfirst('e', astr) == 2 + @test findfirst('u', astr) == nothing + @test findnext('l', astr, 4) == 4 + @test findnext('l', astr, 5) == 11 + @test findnext('l', astr, 12) == nothing + @test findfirst(',', astr) == 6 + @test findnext(',', astr, 7) == nothing + @test findfirst('\n', astr) == 14 + @test findnext('\n', astr, 15) == nothing + @test_throws BoundsError findnext('ε', astr, nextind(astr,lastindex(astr))+1) + @test_throws BoundsError findnext('a', astr, nextind(astr,lastindex(astr))+1) + @test findlast('x', astr) == nothing + @test findlast('\0', astr) == nothing + @test findlast('\u80', astr) == nothing + @test findlast('∀', astr) == nothing + @test findlast('∀', u8str) == 1 + @test findlast('ε', u8str) == 54 + @test findlast('H', astr) == 1 + @test findprev('H', astr, 0) == nothing + @test findlast('l', astr) == 11 + @test findprev('l', astr, 5) == 4 + @test findprev('l', astr, 4) == 4 + @test findprev('l', astr, 3) == 3 + @test findprev('l', astr, 2) == nothing + @test findlast(',', astr) == 6 + @test findprev(',', astr, 5) == nothing + @test findlast('\n', astr) == 14 + end +end + using LinearAlgebra @testset "generalized dot #32739" begin