Module:Feature: Difference between revisions
Jump to navigation
Jump to search
m Protected "Module:Feature" ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite)) |
No edit summary |
||
| Line 1: | Line 1: | ||
-- Module copied from the Genshin Impact wiki and Zenless Zone Zero wiki | -- Module copied from the Genshin Impact wiki and Zenless Zone Zero wiki | ||
-- Revision as of | -- Revision as of 09-03-2025 | ||
--- Miscellaneous useful functions. | --- Miscellaneous useful functions. | ||
local lib = {} | local lib = {} | ||
| Line 42: | Line 42: | ||
function lib.gsplit(text, delim, opt) | function lib.gsplit(text, delim, opt) | ||
checkType('Feature.gsplit', 1, text, 'string') | checkType('Feature.gsplit', 1, text, 'string') | ||
checkType('Feature.gsplit', 2, delim, 'string', NIL_OK) | |||
checkType('Feature.gsplit', 3, opt, 'table', NIL_OK) | checkType('Feature.gsplit', 3, opt, 'table', NIL_OK) | ||
if delim == nil then delim = " " end | if delim == nil then delim = " " end | ||
if opt == nil then opt = {} end | if opt == nil then opt = {} end | ||
-- the mediawiki implementation uses ustring, which is slower than string | |||
-- and also not necessary if delim isn't a pattern. | |||
-- https://help.fandom.com/wiki/Extension:Scribunto#mw.text.split_is_very_slow | |||
-- | -- local g = mw.text.gsplit(text, delim, true) | ||
-- local function f() | |||
-- local value = g() | |||
end | -- if value and not opt.noTrim then -- value is nil when the generator ends | ||
-- value = mw.text.trim(value) | |||
-- end | |||
-- if value == "" and opt.removeEmpty then | |||
-- return f() | |||
-- end | |||
-- return value | |||
-- end | |||
-- return f, nil, nil | |||
-- based on https://github.com/wikimedia/mediawiki-extensions-Scribunto/blob/1eecdac6def6418fb36829cc2f20b464c30e4b37/includes/Engines/LuaCommon/lualib/mw.text.lua#L222 | -- based on https://github.com/wikimedia/mediawiki-extensions-Scribunto/blob/1eecdac6def6418fb36829cc2f20b464c30e4b37/includes/Engines/LuaCommon/lualib/mw.text.lua#L222 | ||
if delim==';' and text:find('(%&[#%d%w]+);') then | |||
text = text:gsub('(%&[#%d%w]+);', '%1‡'); | |||
end | |||
local s, l = 1, #text | local s, l = 1, #text | ||
local function f() | local function f() | ||
if s then | if s then | ||
local e, n = string.find( text, delim, s, true ) | |||
local e | |||
local ret | local ret | ||
if not e then | if not e then | ||
| Line 93: | Line 93: | ||
if ret == '' and opt.removeEmpty then | if ret == '' and opt.removeEmpty then | ||
return f() | return f() | ||
end | |||
if delim==';' and ret:find('(%&[#%d%w]+)‡') then | |||
ret = ret:gsub('(%&[#%d%w]+)‡', '%1;'); | |||
end | end | ||
return ret | return ret | ||
| Line 115: | Line 118: | ||
function lib.split(text, delim, opt) | function lib.split(text, delim, opt) | ||
checkType('Feature.split', 1, text, 'string') | checkType('Feature.split', 1, text, 'string') | ||
checkType('Feature.split', 2, delim, 'string', NIL_OK) | |||
checkType('Feature.split', 3, opt, 'table', NIL_OK) | checkType('Feature.split', 3, opt, 'table', NIL_OK) | ||
local output = {} | local output = {} | ||
for item in lib.gsplit(text, delim, opt) do | for item in lib.gsplit(text, delim, opt) do | ||
table.insert(output, item) | if opt ~= nil and opt.keyTable then | ||
output[item] = true | |||
else | |||
table.insert(output, item) | |||
end | |||
end | end | ||
return output | return output | ||
| Line 272: | Line 279: | ||
local i, j, minus, int, fraction = tostring(n):find('([-]?)(%d+)([.]?%d*)') | local i, j, minus, int, fraction = tostring(n):find('([-]?)(%d+)([.]?%d*)') | ||
if not int then return n end | |||
-- reverse the int-string and append a comma to all blocks of 3 digits | -- reverse the int-string and append a comma to all blocks of 3 digits | ||
int = int:reverse():gsub("(%d%d%d)", "%1,") | int = int:reverse():gsub("(%d%d%d)", "%1,") | ||
| Line 322: | Line 329: | ||
return false | return false | ||
end | end | ||
-- nullable number | -- nullable number | ||