Skip to content

Commit 16f5174

Browse files
authored
Add missing String.count/2 clause for empty pattern list (#15342)
1 parent 1254dae commit 16f5174

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

lib/elixir/lib/string.ex

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,15 @@ defmodule String do
745745
iex> String.count("hello world", "")
746746
12
747747
748-
The `pattern` can also be a compiled pattern:
748+
The `pattern` can also be a list:
749+
750+
iex> String.count("hello world", ["e", "o"])
751+
3
752+
753+
iex> String.count("hello world", [])
754+
0
755+
756+
Or a compiled pattern:
749757
750758
iex> pattern = :binary.compile_pattern([" ", "!"])
751759
iex> String.count("foo bar baz!!", pattern)
@@ -756,6 +764,8 @@ defmodule String do
756764
@doc since: "1.19.0"
757765
def count(string, <<>>), do: length(string) + 1
758766

767+
def count(_string, []), do: 0
768+
759769
def count(string, pattern) when is_struct(pattern, Regex) do
760770
Kernel.length(Regex.scan(pattern, string, return: :index))
761771
end

0 commit comments

Comments
 (0)