Skip to content

Commit 8c20209

Browse files
lmiqStefanKarpinskistevengj
authored andcommitted
Accept AbstractChar in count and findall (::Char, ::String) (JuliaLang#38675)
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 Co-authored-by: Stefan Karpinski <[email protected]> Co-authored-by: Steven G. Johnson <[email protected]>
1 parent 27a4e4c commit 8c20209

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

NEWS.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Julia v1.7 Release Notes
33

44
New language features
55
---------------------
6-
6+
* `count` and `findall` now accept an `AbstractChar` argument to search for a character in a string ([#38675]).
77

88
Language changes
99
----------------

base/regex.jl

+25-2
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,29 @@ function findall(t::Union{AbstractString,AbstractPattern}, s::AbstractString; ov
396396
return found
397397
end
398398

399+
"""
400+
findall(c::AbstractChar, s::AbstractString)
401+
402+
Return a vector `I` of the indices of `s` where `s[i] == c`. If there are no such
403+
elements in `s`, return an empty array.
404+
405+
# Examples
406+
```jldoctest
407+
julia> findall('a', "batman")
408+
2-element Vector{Int64}:
409+
2
410+
5
411+
```
412+
413+
!!! compat "Julia 1.7"
414+
This method requires at least Julia 1.7.
415+
"""
416+
findall(c::AbstractChar, s::AbstractString) = findall(isequal(c),s)
417+
418+
399419
"""
400420
count(
401-
pattern::Union{AbstractString,AbstractPattern},
421+
pattern::Union{AbstractChar,AbstractString,AbstractPattern},
402422
string::AbstractString;
403423
overlap::Bool = false,
404424
)
@@ -411,8 +431,11 @@ original string, otherwise they must be from disjoint character ranges.
411431
412432
!!! compat "Julia 1.3"
413433
This method requires at least Julia 1.3.
434+
435+
!!! compat "Julia 1.7"
436+
Using a character as the pattern requires at least Julia 1.7.
414437
"""
415-
function count(t::Union{AbstractString,AbstractPattern}, s::AbstractString; overlap::Bool=false)
438+
function count(t::Union{AbstractChar,AbstractString,AbstractPattern}, s::AbstractString; overlap::Bool=false)
416439
n = 0
417440
i, e = firstindex(s), lastindex(s)
418441
while true

test/regex.jl

+10
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,22 @@
6464
@test findall(r"\w+", "foo bar", overlap=true) == [1:3, 2:3, 3:3, 5:7, 6:7, 7:7]
6565
@test all(findall(r"\w*", "foo bar") .=== [1:3, 4:3, 5:7, 8:7]) # use === to compare empty ranges
6666
@test all(findall(r"\b", "foo bar") .=== [1:0, 4:3, 5:4, 8:7]) # use === to compare empty ranges
67+
# with Char as argument
68+
@test findall('a', "batman") == [2, 5]
69+
@test findall('', "OH⁻ + H₃CBr → HOH₃CBr⁻ → HOCH₃ + Br⁻") == [17, 35]
70+
@test findall('a', "") == Int[]
71+
@test findall('c', "batman") == Int[]
6772

6873
# count
6974
@test count(r"\w+", "foo bar") == 2
7075
@test count(r"\w+", "foo bar", overlap=true) == 6
7176
@test count(r"\w*", "foo bar") == 4
7277
@test count(r"\b", "foo bar") == 4
78+
# count with char as argument
79+
@test count('a', "batman") == 2
80+
@test count('a', "aaa", overlap=true) == 3
81+
@test count('a', "") == 0
82+
@test count('', "OH⁻ + H₃CBr → (HOH₃CBr⁻)† → HOCH₃ + Br⁻") == 2
7383

7484
# Unnamed subpatterns
7585
let m = match(r"(.)(.)(.)", "xyz")

0 commit comments

Comments
 (0)