1 Star 3 Fork 0

lalawue/mooncake

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
utils.mooc 4.40 KB
一键复制 编辑 原始数据 按行查看 历史
--
-- Copyright (c) 2021 lalawue
--
-- This library is free software; you can redistribute it and/or modify it
-- under the terms of the MIT license. See LICENSE for details.
--
local ipairs = ipairs
class Utils {
static fn printValue(v) {
tv = type(v)
if tv == "string" {
first = v:sub(1, 1)
if first == '"' or first == "'" or first == '[' {
return v
} else {
return '"' .. v .. '"'
}
} else {
return tostring(v)
}
}
static fn format(c, p, v, is_table) {
return is_table and (c[v][2] >= p) and Self.serializeTable(v, p + 1, c) or Self.printValue(v)
}
static fn serializeTable(t, p, c) {
n = 0
for i, v in next, t {
n = n + 1
}
ti = 1
e = n > 0
str = ""
_table = Utils.serializeTable
_format = Utils.format
_srep = string.rep
c = c or {}
p = p or 1
c[t] = {t, 0}
for i, v in next, t {
typ_i, typ_v = type(i) == 'table', type(v) == 'table'
c[i], c[v] = (not c[i] and typ_i) and {i, p} or c[i], (not c[v] and typ_v) and {v, p} or c[v]
str = str .. _srep(' ', p) .. '[' .. _format(c, p, i, typ_i) .. '] = ' .. _format(c, p, v, typ_v) .. (ti < n and ',' or '') .. '\n'
ti = ti + 1
}
return ('{' .. (e and '\n' or '')) .. str .. (e and _srep(' ', p - 1) or '') .. '}'
}
static fn split(self, sep, max, regex) {
assert(sep ~= "")
assert(max == nil or max >= 1)
record = {}
if self:len() > 0 {
plain = not regex
max = max or -1
field, start = 1, 1
first, last = self:find(sep, start, plain)
while first and max ~= 0 {
record[field] = self:sub(start, first - 1)
field = field + 1
start = last + 1
first, last = self:find(sep, start, plain)
max = max - 1
}
record[field] = self:sub(start)
} else {
record[1] = ""
}
return record
}
static fn set(tbl) {
s = {}
for _, v in ipairs(tbl) {
s[v] = true
}
return s
}
static fn seqReduce(tbl, init, func) {
for i, v in ipairs(tbl) {
init = func(init, i, v)
}
return init
}
read_option = _VERSION == "Lua 5.1" and "*a" or "a"
static fn readFile(file_path) {
f, err = io.open(file_path, "rb")
if not f {
return nil, err
}
data = f:read(Utils.read_option)
f:close()
return data
}
static fn writeFile(file_path, content) {
f = io.open(file_path, "wb")
if not f {
return
}
f:write(content)
f:close()
return true
}
static fn copy(it) {
ot = {}
for k, v in pairs(it) {
ot[k] = v
}
return ot
}
static fn suffix(str) {
for i = str:len(), 1, -1 {
if str:sub(i, i) == '.' {
return str:sub(i + 1, str:len())
}
}
return ""
}
static fn debug(str) {
io.write(str .. "\n")
}
static fn dump(t) {
Utils.debug(Utils.serializeTable(t))
}
-- position line in content
static fn posLine(content, lpos) {
assert(type(content) == "string", "Invalid content")
assert(type(lpos) == "number", "Invalid pos")
ln_lnum = 1
for _ in content:sub(1, lpos):gmatch("\n") {
ln_lnum += 1
}
lnum = ln_lnum
ln_content = ""
lcount = 0
for line in content:gmatch("([^\n]*\n?)") {
if lnum == 1 {
ln_content = line
break
}
lnum -= 1
lcount += line:len()
}
return {line = ln_lnum, column = lpos - lcount, message = ln_content:gsub('[\n\r]', '') }
}
-- return error message
static fn errorMessage(content, pos, msg, fname) {
ct = Utils.posLine(content, pos)
return string.format("Error: %s\nFile: %s\nLine: %d (Pos: %d)\nSource: %s\n%s",
msg, fname or '_', ct.line, pos, ct.message,
string.rep(' ',8) .. ct.message:gsub('[^%s]',' '):sub(1,math.max(0,ct.column)) .. '^')
}
}
return Utils
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Lua
1
https://gitee.com/lalawue/mooncake.git
git@gitee.com:lalawue/mooncake.git
lalawue
mooncake
mooncake
master

搜索帮助