Skip to content

Commit effb685

Browse files
milmazzJosé Valim
authored andcommitted
Do not re-compile a compiled pattern for String.split/3 (#5127)
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
1 parent a51c1be commit effb685

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

lib/elixir/lib/string.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ defmodule String do
403403
end
404404

405405
defp maybe_compile_pattern(""), do: ""
406+
defp maybe_compile_pattern(pattern) when is_tuple(pattern), do: pattern
406407
defp maybe_compile_pattern(pattern), do: :binary.compile_pattern(pattern)
407408

408409
@doc """

lib/elixir/test/elixir/string_test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ defmodule StringTest do
8282
assert String.split("a,b", ~r{\.}) == ["a,b"]
8383
end
8484

85+
test "split with compiled pattern" do
86+
pattern = :binary.compile_pattern("-")
87+
88+
assert String.split("x-", pattern) == ["x", ""]
89+
assert String.split("x-", pattern, parts: 2, trim: true) == ["x"]
90+
assert String.split("x-x-", pattern, parts: 3, trim: true) == ["x", "x"]
91+
end
92+
8593
test "splitter" do
8694
assert String.splitter("a,b,c", ",") |> Enum.to_list == ["a", "b", "c"]
8795
assert String.splitter("a,b", ".") |> Enum.to_list == ["a,b"]

0 commit comments

Comments
 (0)