Skip to content
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

Searching a character in a string #705

Merged
merged 5 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
44 changes: 44 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down