Skip to content

Commit 3c4b6a6

Browse files
author
José Valim
committed
Merge pull request #1117 from pragdave/master
Fix for a problem with String.ends_with?
2 parents c3dd400 + 239c41b commit 3c4b6a6

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

lib/elixir/lib/string.ex

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -835,10 +835,14 @@ defmodule String do
835835
"""
836836
@spec ends_with?(t, t | [t]) :: boolean
837837

838-
def ends_with?(string, suffixes) do
838+
def ends_with?(string, suffixes) when is_list(suffixes) do
839839
string_size = size(string)
840-
matches = :binary.matches(string, suffixes)
841-
Enum.any? matches, fn({pos, len})-> pos + len == string_size end
840+
Enum.any? suffixes, fn suffix ->
841+
suffix_size = size(suffix)
842+
(suffix_size <= string_size) and suffix == :binary.part(string, {string_size, -size(suffix)})
843+
end
842844
end
843845

846+
def ends_with?(string, suffix), do: ends_with?(string, [ suffix ])
847+
844848
end

lib/elixir/test/elixir/string_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ defmodule StringTest do
294294
test :ends_with? do
295295
assert String.ends_with? "hello", "lo"
296296
assert String.ends_with? "hello", "hello"
297+
assert String.ends_with? "hello", ["hell", "lo", "xx"]
297298
assert String.ends_with? "hello", ["hellö", "lo"]
298299
assert String.ends_with? "エリクシア", "シア"
299300
refute String.ends_with? "hello", "he"

0 commit comments

Comments
 (0)