Modul:String: Unterschied zwischen den Versionen
Aus FürthWiki
(+p.ilist()) |
(+ p.split2()) |
||
| Zeile 31: | Zeile 31: | ||
-- mw.logObject(result) -- Debugging only | -- mw.logObject(result) -- Debugging only | ||
return result | return result | ||
end | |||
function p.split2(s, delimiter) | |||
-- same as p.split(), but with emptiness-check | |||
-- to do: merge with p.split() | |||
-- mw.log("split2(" .. (s or "nil") .. ", " .. (delimiter or "nil") .. ")") | |||
if not p.isEmpty(s) then | |||
delimiter = delimiter or " " | |||
local start = 1 | |||
local delim_start, delim_end = string.find(s, delimiter, start, true) -- true = plain find (keine Patterns) | |||
while delim_start do | |||
table.insert(result, string.sub(s, start, delim_start - 1)) | |||
start = delim_end + 1 | |||
delim_start, delim_end = string.find(s, delimiter, start, true) | |||
end | |||
table.insert(result, string.sub(s, start)) | |||
end | |||
-- mw.logObject(result) | |||
return result | |||
end | end | ||
Version vom 26. September 2025, 10:18 Uhr
local p = {}
local c = require("Module:Common")
function p.replace(s, old, new, count)
local result
-- Wenn count angegeben ist, wird string.gsub mit dem limitierten Ersetzungszähler verwendet
if count then
result = string.gsub(s, old, new, count)
else
-- Wenn kein count angegeben ist, wird string.gsub alle Vorkommen ersetzen
result = string.gsub(s, old, new)
end
return result
end
function p.split(s, delimiter)
-- splits string s into pieces with delimiter and returns table
-- it's the counterpart of list()
delimiter = delimiter or " "
local result = {}
local start = 1
local delim_start, delim_end = string.find(s, delimiter, start, true) -- true = plain find (keine Patterns)
while delim_start do
table.insert(result, string.sub(s, start, delim_start - 1))
start = delim_end + 1
delim_start, delim_end = string.find(s, delimiter, start, true)
end
table.insert(result, string.sub(s, start))
-- mw.logObject(result) -- Debugging only
return result
end
function p.split2(s, delimiter)
-- same as p.split(), but with emptiness-check
-- to do: merge with p.split()
-- mw.log("split2(" .. (s or "nil") .. ", " .. (delimiter or "nil") .. ")")
if not p.isEmpty(s) then
delimiter = delimiter or " "
local start = 1
local delim_start, delim_end = string.find(s, delimiter, start, true) -- true = plain find (keine Patterns)
while delim_start do
table.insert(result, string.sub(s, start, delim_start - 1))
start = delim_end + 1
delim_start, delim_end = string.find(s, delimiter, start, true)
end
table.insert(result, string.sub(s, start))
end
-- mw.logObject(result)
return result
end
function p.list(table, separator)
-- converts a keyed table into a list with delimiters
-- the indexes/keys gonna lost and the list may be unsorted
-- it's the counterpart of split()
local list = ""
if not c.isEmpty(table) then
if type(table) == "table" then
separator = separator or " "
for _, v in pairs(table) do
if #list ~= 0 then
list = list .. separator
end
list = list .. v
end
else
list = table
end
end
-- mw.log(list) -- Debugging only
return list
end
function p.ilist(table, separator)
-- converts a indexed table into a list with delimiters
-- the indexes gonna lost
-- it's the counterpart of split()
local list = ""
if not c.isEmpty(table) then
if type(table) == "table" then
separator = separator or " "
for _, v in ipairs(table) do
if #list ~= 0 then
list = list .. separator
end
list = list .. v
end
else
list = table
end
end
-- mw.log(list) -- Debugging only
return list
end
function p.strip(s)
-- Removes all spaces at the start and at the end of a string
return s:match("^%s*(.-)%s*$")
end
function p.lstrip(s)
-- Removes all spaces at the start of a string
return s:match("^%s*(.-)$")
end
function p.rstrip(s)
-- Removes all spaces at the end of a string
return s:gsub("%s+$", "")
end
return p