forked from purescript/purescript-strings
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommon.lua
More file actions
68 lines (68 loc) · 2.03 KB
/
Copy pathCommon.lua
File metadata and controls
68 lines (68 loc) · 2.03 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
-- Pattern and Replacement are literal strings in PureScript, so the
-- implementations below must not interpret them as Lua patterns:
-- string.find is used with its `plain` flag and results are spliced
-- together by hand.
return {
_localeCompare = (function(lt)
return function(eq)
return
function(gt) return function(s1) return function(s2) return (s1 < s2) and lt or (s2 < s1) and gt or eq end end end
end
end),
replace = (function(pattern)
return function(replacement)
return function(s)
if pattern == "" then return replacement .. s end
local a, b = s:find(pattern, 1, true)
if a == nil then return s end
return s:sub(1, a - 1) .. replacement .. s:sub(b + 1)
end
end
end),
replaceAll = (function(pattern)
return function(replacement)
return function(s)
if pattern == "" then
local out = {replacement}
for i = 1, #s do
out[#out + 1] = s:sub(i, i)
out[#out + 1] = replacement
end
return table.concat(out)
end
local out, i = {}, 1
while true do
local a, b = s:find(pattern, i, true)
if a == nil then break end
out[#out + 1] = s:sub(i, a - 1)
out[#out + 1] = replacement
i = b + 1
end
out[#out + 1] = s:sub(i)
return table.concat(out)
end
end
end),
split = (function(sep)
return function(s)
local t = {}
if sep == "" then
for i = 1, #s do t[i] = s:sub(i, i) end
return t
end
local i = 1
while true do
local a, b = s:find(sep, i, true)
if a == nil then break end
t[#t + 1] = s:sub(i, a - 1)
i = b + 1
end
t[#t + 1] = s:sub(i)
return t
end
end),
toLower = (function(s) return s:lower() end),
toUpper = (function(s) return s:upper() end),
trim = (function(s) return s:match("^%s*(.-)%s*$") end),
joinWith = (function(sep) return function(xs) return table.concat(xs, sep) end end)
}