forked from purescript/purescript-strings
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCodeUnits.lua
More file actions
128 lines (128 loc) · 3.39 KB
/
Copy pathCodeUnits.lua
File metadata and controls
128 lines (128 loc) · 3.39 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
-- PureScript indices are 0-based, Lua string positions are 1-based;
-- the exports below convert between the two. Pattern arguments are
-- literal strings, hence string.find in plain mode. Index clamping
-- mirrors the upstream JS implementation (String.prototype.indexOf,
-- lastIndexOf, slice and substring).
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 >= 0 and i < #s then
return just(s:sub(i + 1, i + 1))
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 - 1)
else
return nothing
end
end
end
end
end),
_indexOfStartingAt = (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 = s:find(x, startAt + 1, true)
if i then
return just(i - 1)
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 - #x + 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 from = math.max(0, math.min(startAt, #s))
local init = math.max(1, #s - #x + 1 - from)
local i = s:reverse():find(x:reverse(), init, true)
if i then
return just(#s - i - #x + 1)
else
return nothing
end
end
end
end
end
end),
take = (function(n) return function(s) return s:sub(1, math.max(n, 0)) end end),
drop = (function(n) return function(s) return s:sub(math.max(n, 0) + 1) end end),
slice = (function(b)
return function(e)
return function(s)
local len = #s
local from = b < 0 and math.max(len + b, 0) or math.min(b, len)
local to = e < 0 and math.max(len + e, 0) or math.min(e, len)
if to <= from then return "" end
return s:sub(from + 1, to)
end
end
end),
splitAt = (function(i)
return function(s)
local k = math.max(i, 0)
return {before = s:sub(1, k), after = s:sub(k + 1)}
end
end)
}