forked from Unisay/purescript-lua-strings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeUnits.lua
More file actions
105 lines (105 loc) · 2.58 KB
/
CodeUnits.lua
File metadata and controls
105 lines (105 loc) · 2.58 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
return {
fromCharArray = (function(a) return table.concat(a) end),
toCharArray = (function(s)
local t = {}
for i = 1, #s do t[i] = s:sub(i, i) end
return t
end),
singleton = (function(c) return c end),
_charAt = (function(just)
return function(nothing)
return function(i)
return function(s)
if i >= 1 and i <= #s then
return just(s:sub(i, i))
else
return nothing
end
end
end
end
end),
_toChar = (function(just)
return function(nothing)
return function(s)
if #s == 1 then
return just(s)
else
return nothing
end
end
end
end),
length = (function(s) return #s end),
countPrefix = (function(p)
return function(s)
local i = 1
while i <= #s and p(s:sub(i, i)) do i = i + 1 end
return i - 1
end
end),
_indexOf = (function(just)
return function(nothing)
return function(x)
return function(s)
local i = s:find(x, 1, true)
if i then
return just(i)
else
return nothing
end
end
end
end
end),
_indexOfStartingAt = (function(just)
return function(nothing)
return function(x)
return function(startAt)
return function(s)
local i = s:find(x, startAt, true)
if i then
return just(i)
else
return nothing
end
end
end
end
end
end),
_lastIndexOf = (function(just)
return function(nothing)
return function(x)
return function(s)
local i = s:reverse():find(x:reverse(), 1, true)
if i then
return just(#s - i + 1)
else
return nothing
end
end
end
end
end),
_lastIndexOfStartingAt = (function(just)
return function(nothing)
return function(x)
return function(startAt)
return function(s)
local i = s:reverse():find(x:reverse(), #s - startAt + 1, true)
if i then
return just(#s - i + 1)
else
return nothing
end
end
end
end
end
end),
take = (function(n) return function(s) return s:sub(1, n) end end),
drop = (function(n) return function(s) return s:sub(n + 1) end end),
slice = (function(b) return function(e) return function(s) return s:sub(b + 1, e) end end end),
splitAt = (function(i) return function(s) return {before = s:sub(1, i), after = s:sub(i + 1)} end end)
}