From 6a5550ffe217b912bc2cf5ea2e210e1cfe27d0ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20Mart=C3=ADnez?= <31046348+lmiq@users.noreply.github.com> Date: Thu, 3 Dec 2020 09:04:05 -0300 Subject: [PATCH 01/22] Added test for count(::Char,::String) --- test/regex.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/regex.jl b/test/regex.jl index 595a3417cd33f..087673805b7f8 100644 --- a/test/regex.jl +++ b/test/regex.jl @@ -70,6 +70,7 @@ @test count(r"\w+", "foo bar", overlap=true) == 6 @test count(r"\w*", "foo bar") == 4 @test count(r"\b", "foo bar") == 4 + @test count('a',"batman") == 2 # Unnamed subpatterns let m = match(r"(.)(.)(.)", "xyz") From 0b83779ee742a688b58897320a346c8a105c5159 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20Mart=C3=ADnez?= <31046348+lmiq@users.noreply.github.com> Date: Thu, 3 Dec 2020 09:15:44 -0300 Subject: [PATCH 02/22] Accept Char as input to count(t,::String) This change solves an issue raised in this post, in which searching for a char in a string results in a Method error: https://discourse.julialang.org/t/method-for-counting-characters-in-string-count-a-batman-vs-count-a-batman/51153 --- base/regex.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/base/regex.jl b/base/regex.jl index 8ee07fbc006cc..ecf2f08014344 100644 --- a/base/regex.jl +++ b/base/regex.jl @@ -398,7 +398,7 @@ end """ count( - pattern::Union{AbstractString,AbstractPattern}, + pattern::Union{AbstractChar,AbstractString,AbstractPattern}, string::AbstractString; overlap::Bool = false, ) @@ -412,7 +412,7 @@ original string, otherwise they must be from disjoint character ranges. !!! compat "Julia 1.3" This method requires at least Julia 1.3. """ -function count(t::Union{AbstractString,AbstractPattern}, s::AbstractString; overlap::Bool=false) +function count(t::Union{AbstractChar,AbstractString,AbstractPattern}, s::AbstractString; overlap::Bool=false) n = 0 i, e = firstindex(s), lastindex(s) while true From 289ec69423e93db018b12c0cf854f4cc267b418b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20Mart=C3=ADnez?= <31046348+lmiq@users.noreply.github.com> Date: Thu, 3 Dec 2020 10:55:34 -0300 Subject: [PATCH 03/22] Added tests for count with char as argument with overlap=true and empty string --- test/regex.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/regex.jl b/test/regex.jl index 087673805b7f8..71f009b00005d 100644 --- a/test/regex.jl +++ b/test/regex.jl @@ -70,7 +70,10 @@ @test count(r"\w+", "foo bar", overlap=true) == 6 @test count(r"\w*", "foo bar") == 4 @test count(r"\b", "foo bar") == 4 + # count with char as argument @test count('a',"batman") == 2 + @test count('a',"aaa",overlap=true) == 3 + @test count('a',"") == 0 # Unnamed subpatterns let m = match(r"(.)(.)(.)", "xyz") From 1bcf8cf55675c0e5ad7fdce7502e0dd9a5847202 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20Mart=C3=ADnez?= <31046348+lmiq@users.noreply.github.com> Date: Thu, 3 Dec 2020 13:01:43 -0300 Subject: [PATCH 04/22] Update base/regex.jl Co-authored-by: Stefan Karpinski --- base/regex.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/regex.jl b/base/regex.jl index ecf2f08014344..f18ff887aad3e 100644 --- a/base/regex.jl +++ b/base/regex.jl @@ -410,7 +410,7 @@ If `overlap=true`, the matching sequences are allowed to overlap indices in the original string, otherwise they must be from disjoint character ranges. !!! compat "Julia 1.3" - This method requires at least Julia 1.3. + This method requires at least Julia 1.3; using a character as the pattern requires Julia 1.6+. """ function count(t::Union{AbstractChar,AbstractString,AbstractPattern}, s::AbstractString; overlap::Bool=false) n = 0 From cee7e883e565e19ebffb4a4836016cbc05760823 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20Mart=C3=ADnez?= <31046348+lmiq@users.noreply.github.com> Date: Thu, 3 Dec 2020 13:18:01 -0300 Subject: [PATCH 05/22] added test with non-ascii chars --- test/regex.jl | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/regex.jl b/test/regex.jl index 71f009b00005d..9c8580f1ce264 100644 --- a/test/regex.jl +++ b/test/regex.jl @@ -71,10 +71,11 @@ @test count(r"\w*", "foo bar") == 4 @test count(r"\b", "foo bar") == 4 # count with char as argument - @test count('a',"batman") == 2 - @test count('a',"aaa",overlap=true) == 3 - @test count('a',"") == 0 - + @test count('a', "batman") == 2 + @test count('a', "aaa",overlap=true) == 3 + @test count('a', "") == 0 + @test count('→', "OH⁻ + H₃CBr → (HOH₃CBr⁻)† → HOCH₃ + Br⁻") == 2 + # Unnamed subpatterns let m = match(r"(.)(.)(.)", "xyz") @test haskey(m, 1) From bdd70713d0f95b5b34f2bbb7c591fb66a834ff6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20Mart=C3=ADnez?= <31046348+lmiq@users.noreply.github.com> Date: Thu, 3 Dec 2020 13:49:01 -0300 Subject: [PATCH 06/22] added method findall(::Char,::String) --- base/regex.jl | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/base/regex.jl b/base/regex.jl index f18ff887aad3e..289c209943879 100644 --- a/base/regex.jl +++ b/base/regex.jl @@ -396,6 +396,27 @@ function findall(t::Union{AbstractString,AbstractPattern}, s::AbstractString; ov return found end +""" + findall(c :: AbstractChar, s :: AbstractString) + +Return a vector `I` of the indices of `s` where `s[i] == c`. If there are no such +elements in `s`, return an empty array. + +# Examples +```jldoctest +julia> findall('a', "batman") +2-element Vector{Int64}: + 2 + 5 +``` + +!!! compat "Julia 1.6" + This method requires at least Julia 1.6. + +""" +findall(c :: AbstractChar, s :: AbstractString) = findall(isequal(c),s) + + """ count( pattern::Union{AbstractChar,AbstractString,AbstractPattern}, From 2a5049ee395bcf57b57597c0e0d0594c6430cfe9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20Mart=C3=ADnez?= <31046348+lmiq@users.noreply.github.com> Date: Thu, 3 Dec 2020 13:52:41 -0300 Subject: [PATCH 07/22] added tests for findall with Char as argument --- test/regex.jl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/regex.jl b/test/regex.jl index 9c8580f1ce264..268573edbce2c 100644 --- a/test/regex.jl +++ b/test/regex.jl @@ -64,6 +64,11 @@ @test findall(r"\w+", "foo bar", overlap=true) == [1:3, 2:3, 3:3, 5:7, 6:7, 7:7] @test all(findall(r"\w*", "foo bar") .=== [1:3, 4:3, 5:7, 8:7]) # use === to compare empty ranges @test all(findall(r"\b", "foo bar") .=== [1:0, 4:3, 5:4, 8:7]) # use === to compare empty ranges + # with Char as argument + @test findall('a', "batman") == [2, 5] + @test findall('→', "OH⁻ + H₃CBr → HOH₃CBr⁻ → HOCH₃ + Br⁻") == [17, 35] + @test findall('a', "") == Int[] + @test findall('c', "batman") == Int[] # count @test count(r"\w+", "foo bar") == 2 From d5e9c2a839d5a6fc1230ba9ea6dc1c7dfbf36d44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20Mart=C3=ADnez?= <31046348+lmiq@users.noreply.github.com> Date: Thu, 3 Dec 2020 14:11:45 -0300 Subject: [PATCH 08/22] Update test/regex.jl Co-authored-by: Stefan Karpinski --- test/regex.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/regex.jl b/test/regex.jl index 268573edbce2c..cccb7a58abb57 100644 --- a/test/regex.jl +++ b/test/regex.jl @@ -77,7 +77,7 @@ @test count(r"\b", "foo bar") == 4 # count with char as argument @test count('a', "batman") == 2 - @test count('a', "aaa",overlap=true) == 3 + @test count('a', "aaa", overlap=true) == 3 @test count('a', "") == 0 @test count('→', "OH⁻ + H₃CBr → (HOH₃CBr⁻)† → HOCH₃ + Br⁻") == 2 From 08afd207a590a8aa28ea428c724a2921eefe1c18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20Mart=C3=ADnez?= <31046348+lmiq@users.noreply.github.com> Date: Thu, 3 Dec 2020 14:32:11 -0300 Subject: [PATCH 09/22] removed spaces on type declarations --- base/regex.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/base/regex.jl b/base/regex.jl index 289c209943879..ff49597de9f2a 100644 --- a/base/regex.jl +++ b/base/regex.jl @@ -397,7 +397,7 @@ function findall(t::Union{AbstractString,AbstractPattern}, s::AbstractString; ov end """ - findall(c :: AbstractChar, s :: AbstractString) + findall(c::AbstractChar, s::AbstractString) Return a vector `I` of the indices of `s` where `s[i] == c`. If there are no such elements in `s`, return an empty array. @@ -414,7 +414,7 @@ julia> findall('a', "batman") This method requires at least Julia 1.6. """ -findall(c :: AbstractChar, s :: AbstractString) = findall(isequal(c),s) +findall(c::AbstractChar, s::AbstractString) = findall(isequal(c),s) """ From e0d605fc500d75ad538f44e51c28d59b82560066 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20Mart=C3=ADnez?= <31046348+lmiq@users.noreply.github.com> Date: Thu, 3 Dec 2020 16:05:17 -0300 Subject: [PATCH 10/22] update compat entry --- base/regex.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/base/regex.jl b/base/regex.jl index ff49597de9f2a..80944117e488f 100644 --- a/base/regex.jl +++ b/base/regex.jl @@ -412,7 +412,6 @@ julia> findall('a', "batman") !!! compat "Julia 1.6" This method requires at least Julia 1.6. - """ findall(c::AbstractChar, s::AbstractString) = findall(isequal(c),s) @@ -431,7 +430,10 @@ If `overlap=true`, the matching sequences are allowed to overlap indices in the original string, otherwise they must be from disjoint character ranges. !!! compat "Julia 1.3" - This method requires at least Julia 1.3; using a character as the pattern requires Julia 1.6+. + This method requires at least Julia 1.3. + +!!! compat "Julia 1.6" + Using a character as the pattern requires at least Julia 1.6. """ function count(t::Union{AbstractChar,AbstractString,AbstractPattern}, s::AbstractString; overlap::Bool=false) n = 0 From 0c8f92a74def6f0298214a39b39501f59ab0d1e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20Mart=C3=ADnez?= <31046348+lmiq@users.noreply.github.com> Date: Sat, 5 Dec 2020 16:47:16 -0300 Subject: [PATCH 11/22] fixed white spaces --- base/regex.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/base/regex.jl b/base/regex.jl index 80944117e488f..f1dd97bdd9c45 100644 --- a/base/regex.jl +++ b/base/regex.jl @@ -398,8 +398,8 @@ end """ findall(c::AbstractChar, s::AbstractString) - -Return a vector `I` of the indices of `s` where `s[i] == c`. If there are no such + +Return a vector `I` of the indices of `s` where `s[i] == c`. If there are no such elements in `s`, return an empty array. # Examples @@ -414,7 +414,7 @@ julia> findall('a', "batman") This method requires at least Julia 1.6. """ findall(c::AbstractChar, s::AbstractString) = findall(isequal(c),s) - + """ count( From 3c1977a337182481a17f16cf7f0537d7ac8e43e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20Mart=C3=ADnez?= <31046348+lmiq@users.noreply.github.com> Date: Sat, 5 Dec 2020 16:49:16 -0300 Subject: [PATCH 12/22] fixed white spaces --- test/regex.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/regex.jl b/test/regex.jl index cccb7a58abb57..77a7545eaecef 100644 --- a/test/regex.jl +++ b/test/regex.jl @@ -80,7 +80,7 @@ @test count('a', "aaa", overlap=true) == 3 @test count('a', "") == 0 @test count('→', "OH⁻ + H₃CBr → (HOH₃CBr⁻)† → HOCH₃ + Br⁻") == 2 - + # Unnamed subpatterns let m = match(r"(.)(.)(.)", "xyz") @test haskey(m, 1) From ff09c415bc649545fe065c88fe07bc13b3e266e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20Mart=C3=ADnez?= <31046348+lmiq@users.noreply.github.com> Date: Wed, 9 Dec 2020 14:22:03 -0300 Subject: [PATCH 13/22] Update base/regex.jl --- base/regex.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/regex.jl b/base/regex.jl index f1dd97bdd9c45..beeee42fc2e3e 100644 --- a/base/regex.jl +++ b/base/regex.jl @@ -410,7 +410,7 @@ julia> findall('a', "batman") 5 ``` -!!! compat "Julia 1.6" +!!! compat "Julia 1.7" This method requires at least Julia 1.6. """ findall(c::AbstractChar, s::AbstractString) = findall(isequal(c),s) From ca7b4c15294a73a2b6beea887ee76ffe15949492 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20Mart=C3=ADnez?= <31046348+lmiq@users.noreply.github.com> Date: Wed, 9 Dec 2020 14:22:14 -0300 Subject: [PATCH 14/22] Update base/regex.jl --- base/regex.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/regex.jl b/base/regex.jl index beeee42fc2e3e..d35aa5b9e9ee7 100644 --- a/base/regex.jl +++ b/base/regex.jl @@ -411,7 +411,7 @@ julia> findall('a', "batman") ``` !!! compat "Julia 1.7" - This method requires at least Julia 1.6. + This method requires at least Julia 1.7. """ findall(c::AbstractChar, s::AbstractString) = findall(isequal(c),s) From 5f374f221ea37e943ae871514afd3781ad10934f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20Mart=C3=ADnez?= <31046348+lmiq@users.noreply.github.com> Date: Wed, 9 Dec 2020 14:22:30 -0300 Subject: [PATCH 15/22] Update base/regex.jl --- base/regex.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/regex.jl b/base/regex.jl index d35aa5b9e9ee7..6cc53b2259752 100644 --- a/base/regex.jl +++ b/base/regex.jl @@ -433,7 +433,7 @@ original string, otherwise they must be from disjoint character ranges. This method requires at least Julia 1.3. !!! compat "Julia 1.6" - Using a character as the pattern requires at least Julia 1.6. + Using a character as the pattern requires at least Julia 1.7. """ function count(t::Union{AbstractChar,AbstractString,AbstractPattern}, s::AbstractString; overlap::Bool=false) n = 0 From 1bd19ff5b5d6f3da8efc43aa7ec4496a03888916 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20Mart=C3=ADnez?= <31046348+lmiq@users.noreply.github.com> Date: Wed, 9 Dec 2020 14:22:41 -0300 Subject: [PATCH 16/22] Update base/regex.jl --- base/regex.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/regex.jl b/base/regex.jl index 6cc53b2259752..d03938f3177ad 100644 --- a/base/regex.jl +++ b/base/regex.jl @@ -432,7 +432,7 @@ original string, otherwise they must be from disjoint character ranges. !!! compat "Julia 1.3" This method requires at least Julia 1.3. -!!! compat "Julia 1.6" +!!! compat "Julia 1.7" Using a character as the pattern requires at least Julia 1.7. """ function count(t::Union{AbstractChar,AbstractString,AbstractPattern}, s::AbstractString; overlap::Bool=false) From ec0285db6ca31cece5feeb2422c05714027b0a0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20Mart=C3=ADnez?= <31046348+lmiq@users.noreply.github.com> Date: Wed, 9 Dec 2020 16:29:49 -0300 Subject: [PATCH 17/22] count and findall support for Char --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 40f3e00e02a28..0c52bba606b0f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,7 +3,7 @@ Julia v1.7 Release Notes New language features --------------------- - +* count and findall now accept AbstractChars as first argument to search for characaters in strings. Language changes ---------------- From 4224af1f90580889423e90eab8f837071c731136 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20Mart=C3=ADnez?= <31046348+lmiq@users.noreply.github.com> Date: Wed, 9 Dec 2020 16:31:37 -0300 Subject: [PATCH 18/22] Update NEWS.md --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 0c52bba606b0f..f8ecc715b3440 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,7 +3,7 @@ Julia v1.7 Release Notes New language features --------------------- -* count and findall now accept AbstractChars as first argument to search for characaters in strings. +* count and findall now accept AbstractChars as first argument to search for characaters in strings ([#38675]). Language changes ---------------- From b8b454646e9e72245d2c8dd5afc21025237a35d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20Mart=C3=ADnez?= <31046348+lmiq@users.noreply.github.com> Date: Wed, 9 Dec 2020 16:34:17 -0300 Subject: [PATCH 19/22] Update NEWS.md --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index f8ecc715b3440..e043992d45b89 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,7 +3,7 @@ Julia v1.7 Release Notes New language features --------------------- -* count and findall now accept AbstractChars as first argument to search for characaters in strings ([#38675]). +* count and findall now accept AbstractChars as first argument to search for characaters in strings ((https://github.com/JuliaLang/julia/pull/38675)[#38675]). Language changes ---------------- From 40b2002256f26ae09ce9d47e2cccedcd4baf027b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20Mart=C3=ADnez?= <31046348+lmiq@users.noreply.github.com> Date: Wed, 9 Dec 2020 16:52:59 -0300 Subject: [PATCH 20/22] findall and count accept Char --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index e043992d45b89..425e7b4abfffe 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,7 +3,7 @@ Julia v1.7 Release Notes New language features --------------------- -* count and findall now accept AbstractChars as first argument to search for characaters in strings ((https://github.com/JuliaLang/julia/pull/38675)[#38675]). +* count and findall now accept AbstractChars as first argument to search for characaters in strings ([#38675](https://github.com/JuliaLang/julia/pull/38675)). Language changes ---------------- From 9a15719434a29fc7aa61aa0ce8d182b643ed97d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20Mart=C3=ADnez?= <31046348+lmiq@users.noreply.github.com> Date: Wed, 9 Dec 2020 16:53:32 -0300 Subject: [PATCH 21/22] Update NEWS.md --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 425e7b4abfffe..856b8d9a779d0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,7 +3,7 @@ Julia v1.7 Release Notes New language features --------------------- -* count and findall now accept AbstractChars as first argument to search for characaters in strings ([#38675](https://github.com/JuliaLang/julia/pull/38675)). +* count and findall now accept AbstractChar as first argument to search for characters in strings ([#38675](https://github.com/JuliaLang/julia/pull/38675)). Language changes ---------------- From 7199124b49593da275f2ec3fcd010bb3adef5edc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20Mart=C3=ADnez?= <31046348+lmiq@users.noreply.github.com> Date: Wed, 9 Dec 2020 20:16:10 -0300 Subject: [PATCH 22/22] Update NEWS.md Co-authored-by: Steven G. Johnson --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 856b8d9a779d0..48aebc9344980 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,7 +3,7 @@ Julia v1.7 Release Notes New language features --------------------- -* count and findall now accept AbstractChar as first argument to search for characters in strings ([#38675](https://github.com/JuliaLang/julia/pull/38675)). +* `count` and `findall` now accept an `AbstractChar` argument to search for a character in a string ([#38675]). Language changes ----------------