3 local _obj_0 = require("containers")
4 List, Dict = _obj_0.List, _obj_0.Dict
6 local reverse, upper, lower, find, byte, match, gmatch, gsub, sub, format, rep, char
9 reverse, upper, lower, find, byte, match, gmatch, gsub, sub, format, rep, char = _obj_0.reverse, _obj_0.upper, _obj_0.lower, _obj_0.find, _obj_0.byte, _obj_0.match, _obj_0.gmatch, _obj_0.gsub, _obj_0.sub, _obj_0.format, _obj_0.rep, _obj_0.char
12 isplit = function(self, sep)
17 step = function(self, i)
18 local start = self.pos
23 local nl = find(self.str, self.sep, start)
24 self.pos = nl and (nl + 1) or nil
25 local line = sub(self.str, start, nl and (nl - 1) or #self.str)
26 return i, line, start, (nl and (nl - 1) or #self.str)
34 local lua_keywords = {
59 is_lua_id = function(str)
60 return match(str, "^[_a-zA-Z][_a-zA-Z0-9]*$") and not lua_keywords[str]
63 as_lua_id = function(str)
64 str = gsub(str, "x([0-9A-F][0-9A-F])", "x78%1")
65 str = gsub(str, "%W", function(c)
69 return format("x%02X", byte(c))
72 if not (is_lua_id(match(str, "^_*(.*)$"))) then
78 from_lua_id = function(str)
79 if not (is_lua_id(match(str, "^_*(.*)$"))) then
82 str = gsub(str, "_", " ")
83 str = gsub(str, "x([0-9A-F][0-9A-F])", function(hex)
84 return char(tonumber(hex, 16))
93 is_lua_id = is_lua_id,
94 capitalized = function(self)
95 return (gsub(self, '%l', upper, 1))
98 as_a_number = tonumber,
99 as_a_base_1_number = tonumber,
100 bytes = function(self, i, j)
102 byte(self, i or 1, j or -1)
105 split = function(self, sep)
106 return List((function()
109 for i, chunk in isplit(self, sep) do
110 _accum_0[_len_0] = chunk
116 starts_with = function(self, s)
117 return sub(self, 1, #s) == s
119 ends_with = function(self, s)
120 return #self >= #s and sub(self, #self - #s, -1) == s
122 lines = function(self)
123 return List((function()
126 for i, line in isplit(self, '\n') do
127 _accum_0[_len_0] = line
133 line = function(self, line_num)
134 for i, line, start in isplit(self, '\n') do
135 if i == line_num then
140 line_info_at = function(self, pos)
141 assert(type(pos) == 'number', "Invalid string position")
142 for i, line, start, stop in isplit(self, '\n') do
143 if stop + 1 >= pos then
144 return line, i, (pos - start + 1)
148 indented = function(self, indent)
149 if indent == nil then
152 return indent .. (gsub(self, "\n", "\n" .. indent))
154 as_lua = function(self)
155 local escaped = gsub(self, "\\", "\\\\")
156 escaped = gsub(escaped, "\n", "\\n")
157 escaped = gsub(escaped, '"', '\\"')
158 escaped = gsub(escaped, "[^ %g]", function(c)
159 return format("\\%03d", byte(c, 1))
161 return '"' .. escaped .. '"'
163 as_nomsu = function(self)
166 formatted_with = format,
168 position_of = (function(...)
171 position_of_1_after = (function(...)
174 as_a_lua_identifier = as_lua_id,
175 is_a_lua_identifier = is_lua_id,
176 as_lua_id = as_lua_id,
177 as_a_lua_id = as_lua_id,
178 is_a_lua_id = is_lua_id,
179 is_lua_id = is_lua_id,
180 from_lua_id = from_lua_id,
181 bytes_1_to = function(self, start, stop)
183 byte(self, start, stop)
186 [as_lua_id("with 1 ->")] = function(...)
189 bytes = function(self)
194 wrapped_to = function(self, maxlen, margin)
195 if maxlen == nil then
198 if margin == nil then
202 local _list_0 = self:lines()
203 for _index_0 = 1, #_list_0 do
204 local line = _list_0[_index_0]
205 while #line > maxlen do
206 local chunk = sub(line, 1, maxlen)
207 local split = find(chunk, ' ', maxlen - margin, true) or maxlen
208 chunk = sub(line, 1, split)
209 line = sub(line, split + 1, -1)
210 lines[#lines + 1] = chunk
212 lines[#lines + 1] = line
214 return table.concat(lines, "\n")
216 line_at = function(self, i)
217 return (self:line_info_at(i))
219 line_number_at = function(self, i)
220 return select(2, self:line_info_at(i))
222 line_position_at = function(self, i)
223 return select(3, self:line_info_at(i))
225 matches = function(self, patt)
226 return match(self, patt) and true or false
228 matching = function(self, patt)
229 return (match(self, patt))
231 matching_groups = function(self, patt)
236 [as_lua_id("* 1")] = function(self, n)
239 all_matches_of = function(self, patt)
241 local stepper, x, i = gmatch(self, patt)
250 result[#result + 1] = (#tmp == 1) and tmp[1] or tmp
256 character = function(self, i)
257 return sub(self, i, i)
260 for k, v in pairs(string) do
261 Text[k] = Text[k] or v
264 _1_as_text = function(x)
274 __call = function(self, ...)
278 for i = 1, select("#", ...) do
279 ret[i] = _1_as_text(ret[i])
281 return table.concat(ret)
284 debug.setmetatable("", {
286 __index = function(self, k)
287 return Text[k] or (type(k) == 'number' and sub(self, k, k) or nil)
289 __add = function(self, x)
290 return _1_as_text(self) .. _1_as_text(x)