Skip to content

Commit 060df75

Browse files
author
José Valim
committed
Ensure String bool functions do not fail on empty lists
1 parent 66548df commit 060df75

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

lib/elixir/lib/string.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,6 +1275,10 @@ defmodule String do
12751275
true
12761276
end
12771277

1278+
def starts_with?(_string, []) do
1279+
false
1280+
end
1281+
12781282
def starts_with?(string, prefix) when is_list(prefix) or is_binary(prefix) do
12791283
Kernel.match?({0, _}, :binary.match(string, prefix))
12801284
end
@@ -1366,6 +1370,10 @@ defmodule String do
13661370
true
13671371
end
13681372

1373+
def contains?(_string, []) do
1374+
false
1375+
end
1376+
13691377
def contains?(string, contents) do
13701378
:binary.match(string, contents) != :nomatch
13711379
end

lib/elixir/test/elixir/string_test.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ defmodule StringTest do
435435
test :starts_with? do
436436
assert String.starts_with? "hello", "he"
437437
assert String.starts_with? "hello", "hello"
438+
refute String.starts_with? "hello", []
438439
assert String.starts_with? "hello", ["hellö", "hell"]
439440
assert String.starts_with? "エリクシア", "エリ"
440441
refute String.starts_with? "hello", "lo"
@@ -446,6 +447,7 @@ defmodule StringTest do
446447
test :ends_with? do
447448
assert String.ends_with? "hello", "lo"
448449
assert String.ends_with? "hello", "hello"
450+
refute String.ends_with? "hello", []
449451
assert String.ends_with? "hello", ["hell", "lo", "xx"]
450452
assert String.ends_with? "hello", ["hellö", "lo"]
451453
assert String.ends_with? "エリクシア", "シア"
@@ -458,6 +460,7 @@ defmodule StringTest do
458460
test :contains? do
459461
assert String.contains? "elixir of life", "of"
460462
assert String.contains? "エリクシア", "シ"
463+
refute String.contains? "elixir of life", []
461464
assert String.contains? "elixir of life", ["mercury", "life"]
462465
refute String.contains? "elixir of life", "death"
463466
refute String.contains? "エリクシア", "仙"

0 commit comments

Comments
 (0)