-
Notifications
You must be signed in to change notification settings - Fork 5
/
match_on_tail.exs
63 lines (46 loc) · 1.23 KB
/
match_on_tail.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
defmodule Tail do
import Kernel, except: [match?: 2]
def match?("", _) do
false
end
def match?(word, word) do
true
end
def match?(<<_::utf8, rest::binary>>, ending) do
match?(rest, ending)
end
def trunc(text, ending) do
do_trunc("", text, ending)
end
defp do_trunc(stuff, text, text) do
stuff
end
defp do_trunc(stuff, "", _) do
stuff
end
defp do_trunc(accum, <<h::utf8, rest::binary>>, ending) do
do_trunc(<<accum::binary, h::utf8>>, rest, ending)
end
def rstrip(text, strip_char \\ " ") do
do_rstrip(strip_char, "", "", text)
end
defp do_rstrip(sc, whitespace, accum, text) do
end
# run this inline suite with "elixir #{__ENV__.file} test"
if System.argv |> List.first == "test" do
ExUnit.start
defmodule TailTest do
use ExUnit.Case, async: true
alias Tail, as: T
test "match end" do
assert T.match?("Peter was here", "here")
refute T.match?("Peter was here", "herp")
refute T.match?("Peter was here", "Peter")
end
test "trunc end" do
assert T.trunc("Peter was here", "here") == "Peter was "
assert T.trunc("Peter was here", "was") == "Peter was here"
assert T.trunc("Peter was", "Peter was") == ""
end
end
end