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

add findall(char, str) and count(char, str) #35137

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 4 additions & 0 deletions base/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ function findall(t::Union{AbstractString,Regex}, s::AbstractString; overlap::Boo
return found
end

findall(ch::AbstractChar, s::AbstractString) = findall(==(ch), s)

"""
count(
pattern::Union{AbstractString,Regex},
Expand Down Expand Up @@ -399,6 +401,8 @@ function count(t::Union{AbstractString,Regex}, s::AbstractString; overlap::Bool=
return n
end

count(ch::AbstractChar, s::AbstractString) = count(==(ch), s)

"""
SubstitutionString(substr)

Expand Down
7 changes: 7 additions & 0 deletions test/strings/search.jl
Original file line number Diff line number Diff line change
Expand Up @@ -381,4 +381,11 @@ s_18109 = "fooα🐨βcd3"
@test findall("αβ", "blαh blαβ blαββy") == findall("αβ", "blαh blαβ blαββy", overlap=true) == [9:11, 16:18]
@test findall("aa", "aaaaaa") == [1:2, 3:4, 5:6]
@test findall("aa", "aaaaaa", overlap=true) == [1:2, 2:3, 3:4, 4:5, 5:6]

@test findall('z', "language") == Int[]
@test findall('l', "language") == [1]
@test findall('a', "language") == [2, 6]
@test count('z', "language") == 0
@test count('l', "language") == 1
@test count('a', "language") == 2
end