模块:Utils/String:修订间差异
来自「荏苒之境」
无编辑摘要 |
无编辑摘要 |
||
第20行: | 第20行: | ||
local t = {} | local t = {} | ||
for str in string.gmatch(str, "([^"..sep.."]+)") do | for str in string.gmatch(str, "([^"..sep.."]+)") do | ||
t[#t+1] = str | |||
end | end | ||
return t | return t | ||
第28行: | 第28行: | ||
local t = {} | local t = {} | ||
for str in string.gmatch(str, "([^%s]+)") do | for str in string.gmatch(str, "([^%s]+)") do | ||
t[#t+1] = str | |||
end | end | ||
return t | return t | ||
end | |||
utils.unlines = function(str) | |||
local t = {} | |||
for line, newline in string.gmatch(str, "([^\n]*)(\n?)") do | |||
if line == "" and newline == "" then | |||
break | |||
elseif string.sub(line, #line) == "\r" then | |||
line = string.sub(line, 1, #line - 1) | |||
end | |||
t[#t+1] = line | |||
end | |||
return t | |||
end | end | ||
2025年8月21日 (四) 18:36的版本
此模块的文档可以在模块:Utils/String/doc创建
local utils = {}
utils.escape_regex = function(str)
return str:gsub("[%(%)%.%%%+%-%*%?%[%^%$%]]", "%%%1")
end
utils.replace = function(str, this, that, count)
return string.gsub(str, utils.escape_regex(this), that:gsub("%%", "%%%%"), count)
end
utils.trim = function(str)
local res = string.gsub(str, "^%s*(.-)%s*$", "%1")
return res
end
utils.split = function(str, sep)
if sep == nil then
sep = "%s"
end
local t = {}
for str in string.gmatch(str, "([^"..sep.."]+)") do
t[#t+1] = str
end
return t
end
utils.unword = function(str)
local t = {}
for str in string.gmatch(str, "([^%s]+)") do
t[#t+1] = str
end
return t
end
utils.unlines = function(str)
local t = {}
for line, newline in string.gmatch(str, "([^\n]*)(\n?)") do
if line == "" and newline == "" then
break
elseif string.sub(line, #line) == "\r" then
line = string.sub(line, 1, #line - 1)
end
t[#t+1] = line
end
return t
end
utils.match_xml_noattr = function(str, tag)
local res = string.match(str, "<"..tag..">(.-)</"..tag..">")
return res
end
utils.sub_xml_noattr = function(str, tag, rep)
local res = string.gsub(str, "<"..tag..">(.-)</"..tag..">", rep)
return res
end
utils.expand_xml_noattr = function(str, tag)
return utils.sub_xml_noattr(str, tag, "%1")
end
return utils