From 25e903bba6249cb3a8bb1c92c1a78ef17cdfed05 Mon Sep 17 00:00:00 2001 From: eulerkochy Date: Wed, 10 Apr 2019 11:19:33 +0530 Subject: [PATCH 01/15] add findfirst, findlast for searching char in strings --- base/strings/search.jl | 35 ++++++++++++++++++++++++++-- test/strings/search.jl | 52 ++++++++++++++++++++++-------------------- 2 files changed, 60 insertions(+), 27 deletions(-) diff --git a/base/strings/search.jl b/base/strings/search.jl index 8b62b34bc2d07..b8d3d6e707188 100644 --- a/base/strings/search.jl +++ b/base/strings/search.jl @@ -101,8 +101,23 @@ julia> findfirst("Julia", "JuliaLang") 1:5 ``` """ -findfirst(pattern::AbstractString, string::AbstractString) = - findnext(pattern, string, firstindex(string)) +findfirst(pattern::AbstractString, string::AbstractString) = findnext(pattern, string, firstindex(string)) + +""" + findfirst(ch::Char, string::AbstractString) + +Find the first occurrence of `ch` in `string`. Equivalent to +[`findfirst(isequal(ch), string)`](@ref). + +# Examples +```jldoctest +julia> findfirst('a', "happy") +2 + +julia> findfirst('z', "happy") # returns nothing, but not printed in the REPL +``` +""" +findfirst(ch::Char, string::AbstractString) = findfirst(isequal(ch), string) # AbstractString implementation of the generic findnext interface function findnext(testf::Function, s::AbstractString, i::Integer) @@ -273,6 +288,22 @@ julia> findfirst("Julia", "JuliaLang") findlast(pattern::AbstractString, string::AbstractString) = findprev(pattern, string, lastindex(string)) +""" + findlast(ch::Char, string::AbstractString) + +Find the last occurrence of `ch` in `string`. Equivalent to +[`findlast(isequal(ch), string)`](@ref). + +# Examples +```jldoctest +julia> findlast('p', "happy") +4 + +julia> findlast('z', "happy") # returns nothing, but not printed in the REPL +``` +""" +findlast(ch::Char, string::AbstractString) = findlast(isequal(ch), string) + # AbstractString implementation of the generic findprev interface function findprev(testf::Function, s::AbstractString, i::Integer) if i < 1 diff --git a/test/strings/search.jl b/test/strings/search.jl index e9b6a678baa3a..7bdbca681fdf0 100644 --- a/test/strings/search.jl +++ b/test/strings/search.jl @@ -24,41 +24,43 @@ end # ascii forward search for str in [astr, GenericString(astr)] - @test_throws BoundsError findnext(isequal('z'), str, 0) - @test_throws BoundsError findnext(isequal('∀'), str, 0) - @test findfirst(isequal('x'), str) == nothing - @test findfirst(isequal('\0'), str) == nothing - @test findfirst(isequal('\u80'), str) == nothing - @test findfirst(isequal('∀'), str) == nothing - @test findfirst(isequal('H'), str) == 1 - @test findfirst(isequal('l'), str) == 3 - @test findnext(isequal('l'), str, 4) == 4 - @test findnext(isequal('l'), str, 5) == 11 - @test findnext(isequal('l'), str, 12) == nothing - @test findfirst(isequal(','), str) == 6 - @test findnext(isequal(','), str, 7) == nothing - @test findfirst(isequal('\n'), str) == 14 - @test findnext(isequal('\n'), str, 15) == nothing - @test_throws BoundsError findnext(isequal('ε'), str, nextind(str,lastindex(str))+1) - @test_throws BoundsError findnext(isequal('a'), str, nextind(str,lastindex(str))+1) + @test_throws BoundsError findnext(isequal('z'), str, 0) + @test_throws BoundsError findnext(isequal('∀'), str, 0) + @test findfirst('x', str) == nothing + @test findfirst('\0', str) == nothing + @test findfirst('\u80', str) == nothing + @test findfirst('∀', str) == nothing + @test findfirst('H', str) == 1 + @test findfirst('l', str) == 3 + @test findfirst('e', str) == 2 + @test findfirst('u', str) == nothing + @test findnext(isequal('l'), str, 4) == 4 + @test findnext(isequal('l'), str, 5) == 11 + @test findnext(isequal('l'), str, 12) == nothing + @test findfirst(',', str) == 6 + @test findnext(isequal(','), str, 7) == nothing + @test findfirst('\n', str) == 14 + @test findnext(isequal('\n'), str, 15) == nothing + @test_throws BoundsError findnext(isequal('ε'), str, nextind(str,lastindex(str))+1) + @test_throws BoundsError findnext(isequal('a'), str, nextind(str,lastindex(str))+1) end # ascii backward search for str in [astr] - @test findlast(isequal('x'), str) == nothing - @test findlast(isequal('\0'), str) == nothing - @test findlast(isequal('\u80'), str) == nothing - @test findlast(isequal('∀'), str) == nothing - @test findlast(isequal('H'), str) == 1 + @test findlast('x', str) == nothing + @test findlast('\0', str) == nothing + @test findlast('\u80', str) == nothing + @test findlast('∀', str) == nothing + @test findlast('H', str) == 1 @test findprev(isequal('H'), str, 0) == nothing - @test findlast(isequal('l'), str) == 11 + @test findlast('l', str) == 11 @test findprev(isequal('l'), str, 5) == 4 @test findprev(isequal('l'), str, 4) == 4 @test findprev(isequal('l'), str, 3) == 3 @test findprev(isequal('l'), str, 2) == nothing - @test findlast(isequal(','), str) == 6 + @test findlast(',', str) == 6 @test findprev(isequal(','), str, 5) == nothing - @test findlast(isequal('\n'), str) == 14 + @test findlast('\n', str) == 14 end # utf-8 forward search From b70976333972f9b83d30ae4a917617f0b7f146e9 Mon Sep 17 00:00:00 2001 From: Koustav Chowdhury Date: Wed, 10 Apr 2019 23:05:01 +0530 Subject: [PATCH 02/15] Fix indentation --- test/strings/search.jl | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/test/strings/search.jl b/test/strings/search.jl index 7bdbca681fdf0..f0727e783ced7 100644 --- a/test/strings/search.jl +++ b/test/strings/search.jl @@ -24,25 +24,25 @@ end # ascii forward search for str in [astr, GenericString(astr)] - @test_throws BoundsError findnext(isequal('z'), str, 0) - @test_throws BoundsError findnext(isequal('∀'), str, 0) - @test findfirst('x', str) == nothing - @test findfirst('\0', str) == nothing - @test findfirst('\u80', str) == nothing - @test findfirst('∀', str) == nothing - @test findfirst('H', str) == 1 - @test findfirst('l', str) == 3 - @test findfirst('e', str) == 2 - @test findfirst('u', str) == nothing - @test findnext(isequal('l'), str, 4) == 4 - @test findnext(isequal('l'), str, 5) == 11 - @test findnext(isequal('l'), str, 12) == nothing - @test findfirst(',', str) == 6 - @test findnext(isequal(','), str, 7) == nothing - @test findfirst('\n', str) == 14 - @test findnext(isequal('\n'), str, 15) == nothing - @test_throws BoundsError findnext(isequal('ε'), str, nextind(str,lastindex(str))+1) - @test_throws BoundsError findnext(isequal('a'), str, nextind(str,lastindex(str))+1) + @test_throws BoundsError findnext(isequal('z'), str, 0) + @test_throws BoundsError findnext(isequal('∀'), str, 0) + @test findfirst('x', str) == nothing + @test findfirst('\0', str) == nothing + @test findfirst('\u80', str) == nothing + @test findfirst('∀', str) == nothing + @test findfirst('H', str) == 1 + @test findfirst('l', str) == 3 + @test findfirst('e', str) == 2 + @test findfirst('u', str) == nothing + @test findnext(isequal('l'), str, 4) == 4 + @test findnext(isequal('l'), str, 5) == 11 + @test findnext(isequal('l'), str, 12) == nothing + @test findfirst(',', str) == 6 + @test findnext(isequal(','), str, 7) == nothing + @test findfirst('\n', str) == 14 + @test findnext(isequal('\n'), str, 15) == nothing + @test_throws BoundsError findnext(isequal('ε'), str, nextind(str,lastindex(str))+1) + @test_throws BoundsError findnext(isequal('a'), str, nextind(str,lastindex(str))+1) end # ascii backward search From 1f99cd8d0696999c74a2b59dcee1a9696681ab83 Mon Sep 17 00:00:00 2001 From: Koustav Chowdhury Date: Wed, 10 Apr 2019 23:08:23 +0530 Subject: [PATCH 03/15] Update docstrings --- base/strings/search.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/base/strings/search.jl b/base/strings/search.jl index b8d3d6e707188..2bd8f178b8e3f 100644 --- a/base/strings/search.jl +++ b/base/strings/search.jl @@ -114,7 +114,8 @@ Find the first occurrence of `ch` in `string`. Equivalent to julia> findfirst('a', "happy") 2 -julia> findfirst('z', "happy") # returns nothing, but not printed in the REPL +julia> findfirst('z', "happy") === nothing +true ``` """ findfirst(ch::Char, string::AbstractString) = findfirst(isequal(ch), string) @@ -299,7 +300,8 @@ Find the last occurrence of `ch` in `string`. Equivalent to julia> findlast('p', "happy") 4 -julia> findlast('z', "happy") # returns nothing, but not printed in the REPL +julia> findlast('z', "happy") === nothing +true ``` """ findlast(ch::Char, string::AbstractString) = findlast(isequal(ch), string) From c0660a5fc00905c1e15bdf25b37b91c96079ff56 Mon Sep 17 00:00:00 2001 From: eulerkochy Date: Mon, 15 Apr 2019 02:56:53 +0530 Subject: [PATCH 04/15] Add compat for Julia-1.3 --- base/strings/search.jl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/base/strings/search.jl b/base/strings/search.jl index 2bd8f178b8e3f..346698ae86c20 100644 --- a/base/strings/search.jl +++ b/base/strings/search.jl @@ -109,6 +109,9 @@ findfirst(pattern::AbstractString, string::AbstractString) = findnext(pattern, s Find the first occurrence of `ch` in `string`. Equivalent to [`findfirst(isequal(ch), string)`](@ref). +!!! compat "Julia 1.3" + This function requires at least Julia 1.3. + # Examples ```jldoctest julia> findfirst('a', "happy") @@ -295,6 +298,9 @@ findlast(pattern::AbstractString, string::AbstractString) = Find the last occurrence of `ch` in `string`. Equivalent to [`findlast(isequal(ch), string)`](@ref). +!!! compat "Julia 1.3" + This function requires at least Julia 1.3. + # Examples ```jldoctest julia> findlast('p', "happy") From 3e9bdbff846c0b57dd75a283e229dee5dffa062e Mon Sep 17 00:00:00 2001 From: eulerkochy Date: Mon, 15 Apr 2019 03:02:47 +0530 Subject: [PATCH 05/15] Add news --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index 2afaf9e2adfd2..583f4605199f6 100644 --- a/NEWS.md +++ b/NEWS.md @@ -50,6 +50,7 @@ New library functions * Added `Base.hasproperty` and `Base.hasfield` ([#28850]). * One argument `!=(x)`, `>(x)`, `>=(x)`, `<(x)`, `<=(x)` has been added for currying, similar to the existing `==(x)` and `isequal(x)` methods ([#30915]). +* `findfirst` and `findlast` now addresses search for a character in a string ([#31664]). Standard library changes ------------------------ From d82a03d63be0fa7957e2302de73731b82a14ae30 Mon Sep 17 00:00:00 2001 From: Koustav Chowdhury Date: Mon, 15 Apr 2019 03:17:07 +0530 Subject: [PATCH 06/15] Update search.jl --- base/strings/search.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/base/strings/search.jl b/base/strings/search.jl index 346698ae86c20..04beec0847aa2 100644 --- a/base/strings/search.jl +++ b/base/strings/search.jl @@ -101,7 +101,8 @@ julia> findfirst("Julia", "JuliaLang") 1:5 ``` """ -findfirst(pattern::AbstractString, string::AbstractString) = findnext(pattern, string, firstindex(string)) +findfirst(pattern::AbstractString, string::AbstractString) = + findnext(pattern, string, firstindex(string)) """ findfirst(ch::Char, string::AbstractString) From 8d2fba38a56ab2db431ef86bc44dfd4ae9f8aa4f Mon Sep 17 00:00:00 2001 From: eulerkochy Date: Mon, 15 Apr 2019 04:00:15 +0530 Subject: [PATCH 07/15] add findnext, findprev for char ; update tests --- base/strings/search.jl | 42 ++++++++++++++++++++++++++++++++++++++++-- test/strings/search.jl | 30 +++++++++++++++--------------- 2 files changed, 55 insertions(+), 17 deletions(-) diff --git a/base/strings/search.jl b/base/strings/search.jl index 04beec0847aa2..9b8a4c38ca13a 100644 --- a/base/strings/search.jl +++ b/base/strings/search.jl @@ -122,7 +122,7 @@ julia> findfirst('z', "happy") === nothing true ``` """ -findfirst(ch::Char, string::AbstractString) = findfirst(isequal(ch), string) +findfirst(ch::Char, string::AbstractString) = findfirst(==(ch), string) # AbstractString implementation of the generic findnext interface function findnext(testf::Function, s::AbstractString, i::Integer) @@ -275,6 +275,25 @@ julia> findnext("Lang", "JuliaLang", 2) """ findnext(t::AbstractString, s::AbstractString, i::Integer) = _search(s, t, i) +""" + findnext(ch::Char, string::AbstractString, start::Integer) + +Find the next occurrence of `ch` in `string` starting at position `start`. + +!!! compat "Julia 1.3" + This function requires at least Julia 1.3. + +# Examples +```jldoctest +julia> findnext(`z`, "Hello to the world", 1) === nothing +true + +julia> findnext(`o`, "Hello to the world", 6) +8 +``` +""" +findnext(ch::Char, string::AbstractString, ind::Integer) = findnext(==(ch), string, ind) + """ findlast(pattern::AbstractString, string::AbstractString) @@ -311,7 +330,7 @@ julia> findlast('z', "happy") === nothing true ``` """ -findlast(ch::Char, string::AbstractString) = findlast(isequal(ch), string) +findlast(ch::Char, string::AbstractString) = findlast(==(ch), string) # AbstractString implementation of the generic findprev interface function findprev(testf::Function, s::AbstractString, i::Integer) @@ -468,6 +487,25 @@ julia> findprev("Julia", "JuliaLang", 6) """ findprev(t::AbstractString, s::AbstractString, i::Integer) = _rsearch(s, t, i) +""" + findprev(ch::Char, string::AbstractString, start::Integer) + +Find the previous occurrence of `ch` in `string` starting at position `start`. + +!!! compat "Julia 1.3" + This function requires at least Julia 1.3. + +# Examples +```jldoctest +julia> findprev('z', "Hello to the world", 18) === nothing +true + +julia> findprev('o', "Hello to the world", 18) +15 +``` +""" +findprev(ch::Char, string::AbstractString, ind::Integer) = findprev(==(ch), string, ind) + """ occursin(needle::Union{AbstractString,Regex,AbstractChar}, haystack::AbstractString) diff --git a/test/strings/search.jl b/test/strings/search.jl index f0727e783ced7..629e7c9f1f708 100644 --- a/test/strings/search.jl +++ b/test/strings/search.jl @@ -24,8 +24,8 @@ end # ascii forward search for str in [astr, GenericString(astr)] - @test_throws BoundsError findnext(isequal('z'), str, 0) - @test_throws BoundsError findnext(isequal('∀'), str, 0) + @test_throws BoundsError findnext('z', str, 0) + @test_throws BoundsError findnext('∀', str, 0) @test findfirst('x', str) == nothing @test findfirst('\0', str) == nothing @test findfirst('\u80', str) == nothing @@ -34,15 +34,15 @@ for str in [astr, GenericString(astr)] @test findfirst('l', str) == 3 @test findfirst('e', str) == 2 @test findfirst('u', str) == nothing - @test findnext(isequal('l'), str, 4) == 4 - @test findnext(isequal('l'), str, 5) == 11 - @test findnext(isequal('l'), str, 12) == nothing + @test findnext('l', str, 4) == 4 + @test findnext('l', str, 5) == 11 + @test findnext('l', str, 12) == nothing @test findfirst(',', str) == 6 - @test findnext(isequal(','), str, 7) == nothing + @test findnext(',', str, 7) == nothing @test findfirst('\n', str) == 14 - @test findnext(isequal('\n'), str, 15) == nothing - @test_throws BoundsError findnext(isequal('ε'), str, nextind(str,lastindex(str))+1) - @test_throws BoundsError findnext(isequal('a'), str, nextind(str,lastindex(str))+1) + @test findnext('\n', str, 15) == nothing + @test_throws BoundsError findnext('ε', str, nextind(str,lastindex(str))+1) + @test_throws BoundsError findnext('a', str, nextind(str,lastindex(str))+1) end # ascii backward search @@ -52,14 +52,14 @@ for str in [astr] @test findlast('\u80', str) == nothing @test findlast('∀', str) == nothing @test findlast('H', str) == 1 - @test findprev(isequal('H'), str, 0) == nothing + @test findprev('H', str, 0) == nothing @test findlast('l', str) == 11 - @test findprev(isequal('l'), str, 5) == 4 - @test findprev(isequal('l'), str, 4) == 4 - @test findprev(isequal('l'), str, 3) == 3 - @test findprev(isequal('l'), str, 2) == nothing + @test findprev('l', str, 5) == 4 + @test findprev('l', str, 4) == 4 + @test findprev('l', str, 3) == 3 + @test findprev('l', str, 2) == nothing @test findlast(',', str) == 6 - @test findprev(isequal(','), str, 5) == nothing + @test findprev(',', str, 5) == nothing @test findlast('\n', str) == 14 end From 5d6150e9235a07c1576a780caeb9c5689c3a73d9 Mon Sep 17 00:00:00 2001 From: eulerkochy Date: Mon, 15 Apr 2019 04:03:42 +0530 Subject: [PATCH 08/15] update news --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index b77adc719e4bd..bf58c934fd493 100644 --- a/NEWS.md +++ b/NEWS.md @@ -20,7 +20,7 @@ Build system changes New library functions --------------------- -* `findfirst` and `findlast` now addresses search for a character in a string ([#31664]). +* `findfirst`, `findlast`, `findnext` and `findprev` now addresses search for a character in a string ([#31664]). Standard library changes ------------------------ From c6f14f34e1ba6ce85ee93bc21761163f6b6ccf1b Mon Sep 17 00:00:00 2001 From: Milan Bouchet-Valat Date: Tue, 16 Apr 2019 01:01:31 +0530 Subject: [PATCH 09/15] Update NEWS.md Co-Authored-By: eulerkochy --- NEWS.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index bf58c934fd493..8c9b2e7c84d85 100644 --- a/NEWS.md +++ b/NEWS.md @@ -20,7 +20,8 @@ Build system changes New library functions --------------------- -* `findfirst`, `findlast`, `findnext` and `findprev` now addresses search for a character in a string ([#31664]). +* `findfirst`, `findlast`, `findnext` and `findprev` now accept a character as first argument + to search for that character in a string passed as the second argument ([#31664]). Standard library changes ------------------------ From 6702542cd767524dcf4f61f5ac67240dbb1f7a48 Mon Sep 17 00:00:00 2001 From: Milan Bouchet-Valat Date: Tue, 16 Apr 2019 01:01:50 +0530 Subject: [PATCH 10/15] Update base/strings/search.jl Co-Authored-By: eulerkochy --- base/strings/search.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/strings/search.jl b/base/strings/search.jl index 9b8a4c38ca13a..090f25be6e376 100644 --- a/base/strings/search.jl +++ b/base/strings/search.jl @@ -101,7 +101,7 @@ julia> findfirst("Julia", "JuliaLang") 1:5 ``` """ -findfirst(pattern::AbstractString, string::AbstractString) = +findfirst(pattern::AbstractString, string::AbstractString) = findnext(pattern, string, firstindex(string)) """ From 3ee9a616fa4e7e09b9898f9434edd0b5e0414b6c Mon Sep 17 00:00:00 2001 From: Koustav Chowdhury Date: Tue, 16 Apr 2019 01:03:21 +0530 Subject: [PATCH 11/15] Update search.jl --- base/strings/search.jl | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/base/strings/search.jl b/base/strings/search.jl index 090f25be6e376..5c85639d2e577 100644 --- a/base/strings/search.jl +++ b/base/strings/search.jl @@ -107,8 +107,7 @@ findfirst(pattern::AbstractString, string::AbstractString) = """ findfirst(ch::Char, string::AbstractString) -Find the first occurrence of `ch` in `string`. Equivalent to -[`findfirst(isequal(ch), string)`](@ref). +Find the first occurrence of `ch` in `string`. !!! compat "Julia 1.3" This function requires at least Julia 1.3. @@ -315,8 +314,7 @@ findlast(pattern::AbstractString, string::AbstractString) = """ findlast(ch::Char, string::AbstractString) -Find the last occurrence of `ch` in `string`. Equivalent to -[`findlast(isequal(ch), string)`](@ref). +Find the last occurrence of `ch` in `string`. !!! compat "Julia 1.3" This function requires at least Julia 1.3. From c48e47248422cc7c8c971e807aaa085be2ecb388 Mon Sep 17 00:00:00 2001 From: Koustav Chowdhury Date: Tue, 16 Apr 2019 05:01:40 +0530 Subject: [PATCH 12/15] Update search.jl --- test/strings/search.jl | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/test/strings/search.jl b/test/strings/search.jl index 629e7c9f1f708..5b9860630b078 100644 --- a/test/strings/search.jl +++ b/test/strings/search.jl @@ -23,6 +23,26 @@ end @test_throws BoundsError findnext(isequal(0x1),b"\x1\x2",0) # ascii forward search +for str in [astr, GenericString(astr)] + @test_throws BoundsError findnext(isequal('z'), str, 0) + @test_throws BoundsError findnext(isequal('∀'), str, 0) + @test findfirst(isequal('x'), str) == nothing + @test findfirst(isequal('\0'), str) == nothing + @test findfirst(isequal('\u80'), str) == nothing + @test findfirst(isequal('∀'), str) == nothing + @test findfirst(isequal('H'), str) == 1 + @test findfirst(isequal('l'), str) == 3 + @test findnext(isequal('l'), str, 4) == 4 + @test findnext(isequal('l'), str, 5) == 11 + @test findnext(isequal('l'), str, 12) == nothing + @test findfirst(isequal(','), str) == 6 + @test findnext(isequal(','), str, 7) == nothing + @test findfirst(isequal('\n'), str) == 14 + @test findnext(isequal('\n'), str, 15) == nothing + @test_throws BoundsError findnext(isequal('ε'), str, nextind(str,lastindex(str))+1) + @test_throws BoundsError findnext(isequal('a'), str, nextind(str,lastindex(str))+1) +end + for str in [astr, GenericString(astr)] @test_throws BoundsError findnext('z', str, 0) @test_throws BoundsError findnext('∀', str, 0) @@ -46,6 +66,23 @@ for str in [astr, GenericString(astr)] end # ascii backward search +for str in [astr] + @test findlast(isequal('x'), str) == nothing + @test findlast(isequal('\0'), str) == nothing + @test findlast(isequal('\u80'), str) == nothing + @test findlast(isequal('∀'), str) == nothing + @test findlast(isequal('H'), str) == 1 + @test findprev(isequal('H'), str, 0) == nothing + @test findlast(isequal('l'), str) == 11 + @test findprev(isequal('l'), str, 5) == 4 + @test findprev(isequal('l'), str, 4) == 4 + @test findprev(isequal('l'), str, 3) == 3 + @test findprev(isequal('l'), str, 2) == nothing + @test findlast(isequal(','), str) == 6 + @test findprev(isequal(','), str, 5) == nothing + @test findlast(isequal('\n'), str) == 14 +end + for str in [astr] @test findlast('x', str) == nothing @test findlast('\0', str) == nothing From 623638978805f08d1176304ea6c2a30e1b76129e Mon Sep 17 00:00:00 2001 From: Milan Bouchet-Valat Date: Tue, 16 Apr 2019 14:01:59 +0200 Subject: [PATCH 13/15] Use AbstractChar --- base/strings/search.jl | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/base/strings/search.jl b/base/strings/search.jl index 5c85639d2e577..66291a89002b7 100644 --- a/base/strings/search.jl +++ b/base/strings/search.jl @@ -105,9 +105,9 @@ findfirst(pattern::AbstractString, string::AbstractString) = findnext(pattern, string, firstindex(string)) """ - findfirst(ch::Char, string::AbstractString) + findfirst(ch::AbstractChar, string::AbstractString) -Find the first occurrence of `ch` in `string`. +Find the first occurrence of character `ch` in `string`. !!! compat "Julia 1.3" This function requires at least Julia 1.3. @@ -121,7 +121,7 @@ julia> findfirst('z', "happy") === nothing true ``` """ -findfirst(ch::Char, string::AbstractString) = findfirst(==(ch), string) +findfirst(ch::AbstractChar, string::AbstractString) = findfirst(==(ch), string) # AbstractString implementation of the generic findnext interface function findnext(testf::Function, s::AbstractString, i::Integer) @@ -275,9 +275,9 @@ julia> findnext("Lang", "JuliaLang", 2) findnext(t::AbstractString, s::AbstractString, i::Integer) = _search(s, t, i) """ - findnext(ch::Char, string::AbstractString, start::Integer) + findnext(ch::AbstractChar, string::AbstractString, start::Integer) -Find the next occurrence of `ch` in `string` starting at position `start`. +Find the next occurrence of character `ch` in `string` starting at position `start`. !!! compat "Julia 1.3" This function requires at least Julia 1.3. @@ -291,7 +291,8 @@ julia> findnext(`o`, "Hello to the world", 6) 8 ``` """ -findnext(ch::Char, string::AbstractString, ind::Integer) = findnext(==(ch), string, ind) +findnext(ch::AbstractChar, string::AbstractString, ind::Integer) = + findnext(==(ch), string, ind) """ findlast(pattern::AbstractString, string::AbstractString) @@ -312,9 +313,9 @@ findlast(pattern::AbstractString, string::AbstractString) = findprev(pattern, string, lastindex(string)) """ - findlast(ch::Char, string::AbstractString) + findlast(ch::AbstractChar, string::AbstractString) -Find the last occurrence of `ch` in `string`. +Find the last occurrence of character `ch` in `string`. !!! compat "Julia 1.3" This function requires at least Julia 1.3. @@ -328,7 +329,7 @@ julia> findlast('z', "happy") === nothing true ``` """ -findlast(ch::Char, string::AbstractString) = findlast(==(ch), string) +findlast(ch::AbstractChar, string::AbstractString) = findlast(==(ch), string) # AbstractString implementation of the generic findprev interface function findprev(testf::Function, s::AbstractString, i::Integer) @@ -486,9 +487,9 @@ julia> findprev("Julia", "JuliaLang", 6) findprev(t::AbstractString, s::AbstractString, i::Integer) = _rsearch(s, t, i) """ - findprev(ch::Char, string::AbstractString, start::Integer) + findprev(ch::AbstractChar, string::AbstractString, start::Integer) -Find the previous occurrence of `ch` in `string` starting at position `start`. +Find the previous occurrence of character `ch` in `string` starting at position `start`. !!! compat "Julia 1.3" This function requires at least Julia 1.3. @@ -502,7 +503,8 @@ julia> findprev('o', "Hello to the world", 18) 15 ``` """ -findprev(ch::Char, string::AbstractString, ind::Integer) = findprev(==(ch), string, ind) +findprev(ch::AbstractChar, string::AbstractString, ind::Integer) = + findprev(==(ch), string, ind) """ occursin(needle::Union{AbstractString,Regex,AbstractChar}, haystack::AbstractString) From 19c352fb910b17c7121fec8a8421661d35d37bcf Mon Sep 17 00:00:00 2001 From: Koustav Chowdhury Date: Wed, 29 May 2019 18:54:30 +0530 Subject: [PATCH 14/15] Update base/strings/search.jl Co-Authored-By: Stefan Karpinski --- base/strings/search.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/strings/search.jl b/base/strings/search.jl index 66291a89002b7..054efdbdee655 100644 --- a/base/strings/search.jl +++ b/base/strings/search.jl @@ -492,7 +492,7 @@ findprev(t::AbstractString, s::AbstractString, i::Integer) = _rsearch(s, t, i) Find the previous occurrence of character `ch` in `string` starting at position `start`. !!! compat "Julia 1.3" - This function requires at least Julia 1.3. + This method requires at least Julia 1.3. # Examples ```jldoctest From 9a635dc09ac2e1017be9f73e273c25ef5f920703 Mon Sep 17 00:00:00 2001 From: Koustav Chowdhury Date: Wed, 29 May 2019 18:55:12 +0530 Subject: [PATCH 15/15] Apply suggestions from code review Co-Authored-By: Stefan Karpinski --- base/strings/search.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/base/strings/search.jl b/base/strings/search.jl index 054efdbdee655..6e3ff7e2133a0 100644 --- a/base/strings/search.jl +++ b/base/strings/search.jl @@ -110,7 +110,7 @@ findfirst(pattern::AbstractString, string::AbstractString) = Find the first occurrence of character `ch` in `string`. !!! compat "Julia 1.3" - This function requires at least Julia 1.3. + This method requires at least Julia 1.3. # Examples ```jldoctest @@ -280,7 +280,7 @@ findnext(t::AbstractString, s::AbstractString, i::Integer) = _search(s, t, i) Find the next occurrence of character `ch` in `string` starting at position `start`. !!! compat "Julia 1.3" - This function requires at least Julia 1.3. + This method requires at least Julia 1.3. # Examples ```jldoctest @@ -318,7 +318,7 @@ findlast(pattern::AbstractString, string::AbstractString) = Find the last occurrence of character `ch` in `string`. !!! compat "Julia 1.3" - This function requires at least Julia 1.3. + This method requires at least Julia 1.3. # Examples ```jldoctest