-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathstring.ex
More file actions
201 lines (158 loc) · 3.44 KB
/
string.ex
File metadata and controls
201 lines (158 loc) · 3.44 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
defmodule ElixirScript.String do
@moduledoc false
import Kernel, except: [length: 1]
def to_atom(str) do
:erlang.binary_to_atom(str, :utf8)
end
def to_existing_atom(str) do
:erlang.binary_to_existing_atom(str, :utf8)
end
def to_char_list(str) do
str.split("")
end
def to_float(str) do
:erlang.binary_to_float(str)
end
def to_integer(str) do
:erlang.binary_to_integer(str)
end
def to_integer(str, base) do
:erlang.binary_to_integer(str, base)
end
def upcase(str) do
str.toUpperCase()
end
def downcase(str) do
str.toLowerCase()
end
def at(str, pos) do
case pos > length(str) do
true ->
nil
_ ->
str[pos]
end
end
def capitalize(str) do
first = str[0].toUpperCase()
rest = str.substr(1).toLowerCase()
first <> rest
end
def split(str) do
str.split()
end
def split(str, replace, options \\ []) do
limit = Keyword.get(options, :parts, -1)
trim = Keyword.get(options, :trim, false)
split = str.split(replace, limit)
Enum.map(split, fn(x) ->
if trim do
x.trim()
else
x
end
end)
end
def next_grapheme(nil), do: nil
def next_grapheme(""), do: nil
def next_grapheme(str) do
{ str[0], str.substr(1) }
end
def first(nil), do: nil
def first(str) do
str[0]
end
def last(nil), do: nil
def last(str) do
str[length(str) - 1]
end
def graphemes(str) do
str.split('')
end
def length(str) do
str.length()
end
def match?(str, regex) do
str.match(regex) != nil
end
def next_codepoint(nil), do: nil
def next_codepoint(""), do: nil
def next_codepoint(str) do
{ str[0].codePointAt(0), str.substr(1) }
end
def reverse(str) do
do_reverse(str, "")
end
defp do_reverse("", str) do
str
end
defp do_reverse(str, reverse_str) do
do_reverse(str.substr(1), reverse_str <> last(str))
end
def starts_with?(str, prefix) when is_binary(prefix) do
str.startsWith(prefix)
end
def starts_with?(str, prefixes) when is_list(prefixes) do
do_starts_with?(str, prefixes)
end
def do_starts_with?(_, []) do
false
end
def do_starts_with?(str, prefixes) do
case starts_with?(str, hd(prefixes)) do
true ->
true
_ ->
do_starts_with?(str, tl(prefixes))
end
end
def ends_with?(str, suffix) when is_binary(suffix) do
str.endsWith(suffix)
end
def ends_with?(str, suffixes) when is_list(suffixes) do
do_ends_with?(str, suffixes)
end
def do_ends_with?(_, []) do
false
end
def do_ends_with?(str, suffixes) do
case ends_with?(str, hd(suffixes)) do
true ->
true
_ ->
do_ends_with?(str, tl(suffixes))
end
end
def duplicate(str, n) do
str.repeat(n)
end
def contains?(str, s) when is_binary(s) do
str.indexOf(s) > -1
end
def contains?(str, s) when is_list(s) do
do_contains?(str, s)
end
def do_contains?(_, []) do
false
end
def do_contains?(str, prefixes) do
case contains?(str, hd(prefixes)) do
true ->
true
_ ->
do_contains?(str, tl(prefixes))
end
end
def codepoints(str) do
do_codepoints(str, [])
end
def do_codepoints("", codepoint_list) do
codepoint_list
end
def do_codepoints(str, codepoint_list) do
do_codepoints(str.substr(1), codepoint_list ++ [first(str).codePointAt(0)])
end
def valid?(str) do
is_binary(str)
end
end