forked from purescript/purescript-strings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString.lua
More file actions
210 lines (186 loc) · 4.04 KB
/
String.lua
File metadata and controls
210 lines (186 loc) · 4.04 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
202
203
204
205
206
207
208
209
210
-- module Data.String
local String = {}
String["_charAt"] = function (just)
return function (nothing)
return function (i)
return function (s)
return i >= 0 and i < #s and just(string.sub(s, i+1, i+1)) or nothing
end
end
end
end
String.singleton = function(c)
return c
end
String["_charCodeAt"] = function (just)
return function (nothing)
return function (i)
return function (s)
return i >= 0 and i < #s and just(byte(string.sub(s, i+1, i+1))) or nothing
end
end
end
end
String["_toChar"] = function (just)
return function (nothing)
return function (s)
return #s == 1 and just(s) or nothing
end
end
end
String.fromCharArray = function (a)
local result = ""
for i = 1, #a do
result = result .. a[i]
end
return result
end
String["_indexOf"] = function (just)
return function (nothing)
return function (x)
return function (s)
local i = string.find(s, x)
return i == nil and nothing or just(i-1)
end
end
end
end
String["_indexOf'"] = function (just)
return function (nothing)
return function (x)
return function (startAt)
return function (s)
if startAt < 0 or startAt > #s then return nothing end
local i = string.find(s, x, startAt+1)
return i == nil and nothing or just(i)
end
end
end
end
end
String["_lastIndexOf"] = function (just)
return function (nothing)
return function (x)
return function (s)
local res = {string.find(s, x)}
local i = res[#res]
return i == nil and nothing or just(i)
end
end
end
end
String["_lastIndexOf'"] = function (just)
return function (nothing)
return function (x)
return function (startAt)
return function (s)
if startAt < 0 or startAt > #s then return nothing end
local res = {string.find(s, x, startAt+1)}
local i = res[#res]
return i == nil and nothing or just(i)
end
end
end
end
end
String.length = function (s)
return #s
end
String["_localeCompare"] = function (lt)
return function (eq)
return function (gt)
return function (s1)
return function (s2)
if s1 == s2 then return eq end
if s1 < s2 then
return lt
else
return gt
end
end
end
end
end
end
String.replace = function (pattern)
return function (replace)
return function (s)
return string.gsub(s, pattern, replace)
end
end
end
String.take = function (n)
return function (s)
return s.substr(0, n)
return string.sub(s, 1, n)
end
end
String.drop = function (n)
return function (s)
return string.sub(n+1)
end
end
String.count = function (p)
return function (s)
local n = 0
for i=1, #s do
if p(s[i]) then
n = n + 1
else
return n
end
end
return n
end
end
String.split = function (sep)
return function (s)
if sep == "" then
return String.toCharArray(s)
end
local result = {}
local s_ = s
while s_ ~= "" do
local nextSep = string.find(s_, sep)
if not nextSep then
result[#result+1] = s_
return result
end
result[#result+1] = string.sub(s_, 1, nextSep-1)
s_ = string.sub(s_, nextSep+#sep)
while string.find(s_, sep) == 1 do
s_ = string.sub(s_, 2)
end
end
return result
end
end
String.toCharArray = function (s)
local result = {}
for i = 1, #s do result[i] = string.sub(s, i, i) end
return result
end
String.toLower = function (s)
return string.lower(s)
end
String.toUpper = function (s)
return string.upper(s)
end
String.trim = function (s)
local ltrim = string.gsub(s, "^%s*", "")
local rtrim = string.gsub(ltrim, "%s*$", "")
return rtrim
end
String.joinWith = function (s)
return function (xs)
local result = ""
for i=1, #xs do
result = result .. xs[i]
if i ~= #xs then
result = result .. s
end
end
return result
end
end
return String