luci: optimization code

This commit is contained in:
xiaorouji 2023-10-11 21:51:41 +08:00 committed by sbwml
parent b4f401e891
commit 351664eb90
2 changed files with 24 additions and 22 deletions

View File

@ -106,6 +106,29 @@ function trim(s)
return (s:gsub("^%s*(.-)%s*$", "%1")) return (s:gsub("^%s*(.-)%s*$", "%1"))
end end
-- 分割字符串
function split(full, sep)
if full then
full = full:gsub("%z", "") -- 这里不是很清楚 有时候结尾带个\0
local off, result = 1, {}
while true do
local nStart, nEnd = full:find(sep, off)
if not nEnd then
local res = string.sub(full, off, string.len(full))
if #res > 0 then -- 过滤掉 \0
table.insert(result, res)
end
break
else
table.insert(result, string.sub(full, off, nStart - 1))
off = nEnd + 1
end
end
return result
end
return {}
end
function is_exist(table, value) function is_exist(table, value)
for index, k in ipairs(table) do for index, k in ipairs(table) do
if k == value then if k == value then

View File

@ -16,6 +16,7 @@ local datatypes = require "luci.cbi.datatypes"
-- so caching them is worth the effort -- so caching them is worth the effort
local tinsert = table.insert local tinsert = table.insert
local ssub, slen, schar, sbyte, sformat, sgsub = string.sub, string.len, string.char, string.byte, string.format, string.gsub local ssub, slen, schar, sbyte, sformat, sgsub = string.sub, string.len, string.char, string.byte, string.format, string.gsub
local split = api.split
local jsonParse, jsonStringify = luci.jsonc.parse, luci.jsonc.stringify local jsonParse, jsonStringify = luci.jsonc.parse, luci.jsonc.stringify
local base64Decode = api.base64Decode local base64Decode = api.base64Decode
local uci = luci.model.uci.cursor() local uci = luci.model.uci.cursor()
@ -317,28 +318,6 @@ do
end end
end end
-- 分割字符串
local function split(full, sep)
if full then
full = full:gsub("%z", "") -- 这里不是很清楚 有时候结尾带个\0
local off, result = 1, {}
while true do
local nStart, nEnd = full:find(sep, off)
if not nEnd then
local res = ssub(full, off, slen(full))
if #res > 0 then -- 过滤掉 \0
tinsert(result, res)
end
break
else
tinsert(result, ssub(full, off, nStart - 1))
off = nEnd + 1
end
end
return result
end
return {}
end
-- urlencode -- urlencode
-- local function get_urlencode(c) return sformat("%%%02X", sbyte(c)) end -- local function get_urlencode(c) return sformat("%%%02X", sbyte(c)) end