From eaf8989f0a57607bcd12ceae994cc2393919c30d Mon Sep 17 00:00:00 2001 From: Lukasz Samson Date: Tue, 5 May 2026 22:20:41 +0200 Subject: [PATCH 1/2] Add missing String.count/2 clause for empty pattern list --- lib/elixir/lib/string.ex | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/elixir/lib/string.ex b/lib/elixir/lib/string.ex index 79178427ec..fa7807628a 100644 --- a/lib/elixir/lib/string.ex +++ b/lib/elixir/lib/string.ex @@ -756,6 +756,8 @@ defmodule String do @doc since: "1.19.0" def count(string, <<>>), do: length(string) + 1 + def count(_string, []), do: 0 + def count(string, pattern) when is_struct(pattern, Regex) do Kernel.length(Regex.scan(pattern, string, return: :index)) end From b255fc0ed7563627a05a80533549640e3d468908 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 6 May 2026 08:35:36 +0200 Subject: [PATCH 2/2] Clarify String.count pattern parameter in docs Updated documentation to reflect that the pattern can be a list, including examples. --- lib/elixir/lib/string.ex | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/elixir/lib/string.ex b/lib/elixir/lib/string.ex index fa7807628a..2acbf6408d 100644 --- a/lib/elixir/lib/string.ex +++ b/lib/elixir/lib/string.ex @@ -745,7 +745,15 @@ defmodule String do iex> String.count("hello world", "") 12 - The `pattern` can also be a compiled pattern: + The `pattern` can also be a list: + + iex> String.count("hello world", ["e", "o"]) + 3 + + iex> String.count("hello world", []) + 0 + + Or a compiled pattern: iex> pattern = :binary.compile_pattern([" ", "!"]) iex> String.count("foo bar baz!!", pattern)