@tilua/compiler 2.4.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,54 @@
1
+ local __NAME__ = {}
2
+
3
+ -- `known` is the object's keys in the order its type wrote them, handed over
4
+ -- by the lowering; `open` says the type also has an indexer, whose keys
5
+ -- follow the named ones. With no one order to give (`known` is nil), every
6
+ -- key is listed, in the order `pairs` walks them.
7
+ local function keysOf(known, open, t)
8
+ local out = {}
9
+ if known == nil then
10
+ for key in pairs(t) do
11
+ out[#out + 1] = key
12
+ end
13
+ return out
14
+ end
15
+ for i = 1, #known do
16
+ out[i] = known[i]
17
+ end
18
+ if open then
19
+ local listed = {}
20
+ for i = 1, #known do
21
+ listed[known[i]] = true
22
+ end
23
+ for key in pairs(t) do
24
+ if not listed[key] then
25
+ out[#out + 1] = key
26
+ end
27
+ end
28
+ end
29
+ return out
30
+ end
31
+
32
+ function __NAME__.keys(known, open, t)
33
+ return keysOf(known, open, t)
34
+ end
35
+
36
+ -- Indexed, not appended: a missing optional member leaves its place `nil`
37
+ -- rather than moving everything after it up one.
38
+ function __NAME__.values(known, open, t)
39
+ local keys = keysOf(known, open, t)
40
+ local out = {}
41
+ for i = 1, #keys do
42
+ out[i] = t[keys[i]]
43
+ end
44
+ return out
45
+ end
46
+
47
+ function __NAME__.entries(known, open, t)
48
+ local keys = keysOf(known, open, t)
49
+ local out = {}
50
+ for i = 1, #keys do
51
+ out[i] = { keys[i], t[keys[i]] }
52
+ end
53
+ return out
54
+ end
@@ -0,0 +1,119 @@
1
+ local __NAME__ = {}
2
+
3
+ function __NAME__.trim(s)
4
+ return (string.gsub(string.gsub(s, "^%s+", ""), "%s+$", ""))
5
+ end
6
+
7
+ function __NAME__.trimStart(s)
8
+ return (string.gsub(s, "^%s+", ""))
9
+ end
10
+
11
+ function __NAME__.trimEnd(s)
12
+ return (string.gsub(s, "%s+$", ""))
13
+ end
14
+
15
+ function __NAME__.startsWith(s, text)
16
+ return string.sub(s, 1, #text) == text
17
+ end
18
+
19
+ function __NAME__.endsWith(s, text)
20
+ return #text == 0 or string.sub(s, -#text) == text
21
+ end
22
+
23
+ function __NAME__.includes(s, text)
24
+ return string.find(s, text, 1, true) ~= nil
25
+ end
26
+
27
+ function __NAME__.indexOf(s, text, start)
28
+ local at = string.find(s, text, start or 1, true)
29
+ return at
30
+ end
31
+
32
+ function __NAME__.slice(s, start, stop)
33
+ return string.sub(s, start or 1, stop or -1)
34
+ end
35
+
36
+ -- Plain text, not a pattern: `replace("a.b", ".", "-")` changes the dot.
37
+ function __NAME__.replace(s, text, replacement)
38
+ if text == "" then
39
+ return replacement .. s
40
+ end
41
+ local at, to = string.find(s, text, 1, true)
42
+ if not at then
43
+ return s
44
+ end
45
+ return string.sub(s, 1, at - 1) .. replacement .. string.sub(s, to + 1)
46
+ end
47
+
48
+ function __NAME__.replaceAll(s, text, replacement)
49
+ if text == "" then
50
+ return s
51
+ end
52
+ local out = {}
53
+ local at = 1
54
+ while true do
55
+ local from, to = string.find(s, text, at, true)
56
+ if not from then
57
+ break
58
+ end
59
+ out[#out + 1] = string.sub(s, at, from - 1)
60
+ out[#out + 1] = replacement
61
+ at = to + 1
62
+ end
63
+ out[#out + 1] = string.sub(s, at)
64
+ return table.concat(out)
65
+ end
66
+
67
+ function __NAME__.padStart(s, length, padding)
68
+ local pad = padding or " "
69
+ if #s >= length or pad == "" then
70
+ return s
71
+ end
72
+ local filler = string.rep(pad, math.ceil((length - #s) / #pad))
73
+ return string.sub(filler, 1, length - #s) .. s
74
+ end
75
+
76
+ function __NAME__.padEnd(s, length, padding)
77
+ local pad = padding or " "
78
+ if #s >= length or pad == "" then
79
+ return s
80
+ end
81
+ local filler = string.rep(pad, math.ceil((length - #s) / #pad))
82
+ return s .. string.sub(filler, 1, length - #s)
83
+ end
84
+
85
+ -- Lua's own, where Lua answers several values and tilua one: the positions
86
+ -- and captures as an array, or nil; the new string without the count.
87
+ local function values(...)
88
+ if select("#", ...) <= 1 and (...) == nil then
89
+ return nil
90
+ end
91
+ return { ... }
92
+ end
93
+
94
+ function __NAME__.find(s, pattern, init, plain)
95
+ return values(string.find(s, pattern, init, plain))
96
+ end
97
+
98
+ function __NAME__.match(s, pattern, init)
99
+ return values(string.match(s, pattern, init))
100
+ end
101
+
102
+ function __NAME__.gsub(s, pattern, replacement, n)
103
+ return (string.gsub(s, pattern, replacement, n))
104
+ end
105
+
106
+ function __NAME__.byte(s, i, j)
107
+ if j == nil then
108
+ return (string.byte(s, i))
109
+ end
110
+ return { string.byte(s, i, j) }
111
+ end
112
+
113
+ -- An iterator function: each call is one match's captures, as an array.
114
+ function __NAME__.gmatch(s, pattern)
115
+ local step = string.gmatch(s, pattern)
116
+ return function()
117
+ return values(step())
118
+ end
119
+ end